1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef WEBKIT_BROWSER_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ 6 #define WEBKIT_BROWSER_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/memory/ref_counted.h" 13 #include "base/synchronization/lock.h" 14 #include "webkit/browser/fileapi/mount_points.h" 15 #include "webkit/browser/webkit_storage_browser_export.h" 16 #include "webkit/common/fileapi/file_system_mount_option.h" 17 #include "webkit/common/fileapi/file_system_types.h" 18 19 namespace base { 20 class FilePath; 21 } 22 23 namespace fileapi { 24 class FileSystemURL; 25 } 26 27 namespace fileapi { 28 29 // Manages external filesystem namespaces that are identified by 'mount name' 30 // and are persisted until RevokeFileSystem is called. 31 // Files in an external filesystem are identified by a filesystem URL like: 32 // 33 // filesystem:<origin>/external/<mount_name>/relative/path 34 // 35 class WEBKIT_STORAGE_BROWSER_EXPORT ExternalMountPoints 36 : public base::RefCountedThreadSafe<ExternalMountPoints>, 37 public MountPoints { 38 public: 39 static ExternalMountPoints* GetSystemInstance(); 40 static scoped_refptr<ExternalMountPoints> CreateRefCounted(); 41 42 // Registers a new named external filesystem. 43 // The |path| is registered as the root path of the mount point which 44 // is identified by a URL "filesystem:.../external/mount_name". 45 // 46 // For example, if the path "/media/removable" is registered with 47 // the mount_name "removable", a filesystem URL like 48 // "filesystem:.../external/removable/a/b" will be resolved as 49 // "/media/removable/a/b". 50 // 51 // The |mount_name| should NOT contain a path separator '/'. 52 // Returns false if the given name is already registered. 53 // 54 // Overlapping mount points in a single MountPoints instance are not allowed. 55 // Adding mount point whose path overlaps with an existing mount point will 56 // fail. 57 // 58 // If not empty, |path| must be absolute. It is allowed for the path to be 59 // empty, but |GetVirtualPath| will not work for those mount points. 60 // 61 // An external file system registered by this method can be revoked 62 // by calling RevokeFileSystem with |mount_name|. 63 bool RegisterFileSystem(const std::string& mount_name, 64 FileSystemType type, 65 const FileSystemMountOption& mount_option, 66 const base::FilePath& path); 67 68 // MountPoints overrides. 69 virtual bool HandlesFileSystemMountType(FileSystemType type) const OVERRIDE; 70 virtual bool RevokeFileSystem(const std::string& mount_name) OVERRIDE; 71 virtual bool GetRegisteredPath(const std::string& mount_name, 72 base::FilePath* path) const OVERRIDE; 73 virtual bool CrackVirtualPath( 74 const base::FilePath& virtual_path, 75 std::string* mount_name, 76 FileSystemType* type, 77 base::FilePath* path, 78 FileSystemMountOption* mount_option) const OVERRIDE; 79 virtual FileSystemURL CrackURL(const GURL& url) const OVERRIDE; 80 virtual FileSystemURL CreateCrackedFileSystemURL( 81 const GURL& origin, 82 FileSystemType type, 83 const base::FilePath& path) const OVERRIDE; 84 85 // Returns a list of registered MountPointInfos (of <mount_name, path>). 86 void AddMountPointInfosTo(std::vector<MountPointInfo>* mount_points) const; 87 88 // Converts a path on a registered file system to virtual path relative to the 89 // file system root. E.g. if 'Downloads' file system is mapped to 90 // '/usr/local/home/Downloads', and |absolute| path is set to 91 // '/usr/local/home/Downloads/foo', the method will set |virtual_path| to 92 // 'Downloads/foo'. 93 // Returns false if the path cannot be resolved (e.g. if the path is not 94 // part of any registered filesystem). 95 // 96 // Returned virtual_path will have normalized path separators. 97 bool GetVirtualPath(const base::FilePath& absolute_path, 98 base::FilePath* virtual_path) const; 99 100 // Returns the virtual root path that looks like /<mount_name>. 101 base::FilePath CreateVirtualRootPath(const std::string& mount_name) const; 102 103 FileSystemURL CreateExternalFileSystemURL( 104 const GURL& origin, 105 const std::string& mount_name, 106 const base::FilePath& path) const; 107 108 // Revoke all registered filesystems. Used only by testing (for clean-ups). 109 void RevokeAllFileSystems(); 110 111 private: 112 friend class base::RefCountedThreadSafe<ExternalMountPoints>; 113 114 // Represents each file system instance (defined in the .cc). 115 class Instance; 116 117 typedef std::map<std::string, Instance*> NameToInstance; 118 119 // Reverse map from registered path to its corresponding mount name. 120 typedef std::map<base::FilePath, std::string> PathToName; 121 122 // Use |GetSystemInstance| of |CreateRefCounted| to get an instance. 123 ExternalMountPoints(); 124 virtual ~ExternalMountPoints(); 125 126 // MountPoint overrides. 127 virtual FileSystemURL CrackFileSystemURL( 128 const FileSystemURL& url) const OVERRIDE; 129 130 // Performs sanity checks on the new mount point. 131 // Checks the following: 132 // - there is no registered mount point with mount_name 133 // - path does not contain a reference to a parent 134 // - path is absolute 135 // - path does not overlap with an existing mount point path. 136 // 137 // |lock_| should be taken before calling this method. 138 bool ValidateNewMountPoint(const std::string& mount_name, 139 const base::FilePath& path); 140 141 // This lock needs to be obtained when accessing the instance_map_. 142 mutable base::Lock lock_; 143 144 NameToInstance instance_map_; 145 PathToName path_to_name_map_; 146 147 DISALLOW_COPY_AND_ASSIGN(ExternalMountPoints); 148 }; 149 150 } // namespace fileapi 151 152 #endif // WEBKIT_BROWSER_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ 153