OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ |
| 6 #define WEBKIT_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/fileapi/file_system_types.h" |
| 15 #include "webkit/fileapi/mount_points.h" |
| 16 #include "webkit/storage/webkit_storage_export.h" |
| 17 |
| 18 class FilePath; |
| 19 |
| 20 namespace fileapi { |
| 21 class RemoteFileSystemProxyInterface; |
| 22 } |
| 23 |
| 24 namespace fileapi { |
| 25 |
| 26 // Manages external filesystem namespaces that are identified by 'mount name' |
| 27 // and are persisted until RevokeFileSystem is called. |
| 28 // Files in an external filesystem are identified by a filesystem URL like: |
| 29 // |
| 30 // filesystem:<origin>/external/<mount_name>/relative/path |
| 31 // |
| 32 class WEBKIT_STORAGE_EXPORT ExternalMountPoints |
| 33 : public base::RefCountedThreadSafe<ExternalMountPoints>, |
| 34 public MountPoints { |
| 35 public: |
| 36 static ExternalMountPoints* GetSystemInstance(); |
| 37 static scoped_refptr<ExternalMountPoints> CreateRefCounted(); |
| 38 |
| 39 // Registers a new named external filesystem. |
| 40 // The |path| is registered as the root path of the mount point which |
| 41 // is identified by a URL "filesystem:.../external/mount_name". |
| 42 // |
| 43 // For example, if the path "/media/removable" is registered with |
| 44 // the mount_name "removable", a filesystem URL like |
| 45 // "filesystem:.../external/removable/a/b" will be resolved as |
| 46 // "/media/removable/a/b". |
| 47 // |
| 48 // The |mount_name| should NOT contain a path separator '/'. |
| 49 // Returns false if the given name is already registered. |
| 50 // |
| 51 // Overlapping mount points in a single MountPoints instance are not allowed. |
| 52 // Adding mount point whose path overlaps with an existing mount point will |
| 53 // fail. |
| 54 // |
| 55 // If not empty, |path| must be absolute. It is allowed for the path to be |
| 56 // empty, but |GetVirtualPath| will not work for those mount points. |
| 57 // |
| 58 // An external file system registered by this method can be revoked |
| 59 // by calling RevokeFileSystem with |mount_name|. |
| 60 bool RegisterFileSystem(const std::string& mount_name, |
| 61 FileSystemType type, |
| 62 const FilePath& path); |
| 63 |
| 64 // Same as |RegisterExternalFileSystem|, but registers a remote file system |
| 65 // proxy for the file system. |
| 66 bool RegisterRemoteFileSystem(const std::string& mount_name, |
| 67 FileSystemType type, |
| 68 RemoteFileSystemProxyInterface* remote_proxy, |
| 69 const FilePath& path); |
| 70 |
| 71 // Retrieves the remote file system proxy for the registered file system. |
| 72 // Returns NULL if there is no file system with given name, or if the file |
| 73 // system does not have a remote file system proxy. |
| 74 RemoteFileSystemProxyInterface* GetRemoteFileSystemProxy( |
| 75 const std::string& mount_name) const; |
| 76 |
| 77 // Returns a list of registered MountPointInfos (of <mount_name, path>). |
| 78 void AddMountPointInfosTo(std::vector<MountPointInfo>* mount_points) const; |
| 79 |
| 80 // MountPoints overrides. |
| 81 virtual bool CanCrackMountType(FileSystemType type) const OVERRIDE; |
| 82 virtual bool GetRegisteredPath(const std::string& mount_name, |
| 83 FilePath* path) const OVERRIDE; |
| 84 virtual bool RevokeFileSystem(const std::string& mount_name) OVERRIDE; |
| 85 |
| 86 // Converts a path on a registered file system to virtual path relative to the |
| 87 // file system root. E.g. if 'Downloads' file system is mapped to |
| 88 // '/usr/local/home/Downloads', and |absolute| path is set to |
| 89 // 'usr/local/home/Downloads/foo', the method will set |virtual_path| to |
| 90 // 'Downloads/foo'. |
| 91 bool GetVirtualPath(const FilePath& absolute_path, FilePath* virtual_path); |
| 92 |
| 93 // Cracks the given |virtual_path| (which is the path part of a filesystem URL |
| 94 // without /'/external' prefix part) and populates the |
| 95 // |mount_name|, |type|, and |path| if the <mount_name> part embedded in |
| 96 // the |virtual_path| (i.e. the first component of the |virtual_path|) is a |
| 97 // valid registered filesystem ID or mount name for an external filesystem. |
| 98 // |
| 99 // Returns false if the given virtual_path or the cracked mount_name |
| 100 // is not valid. |
| 101 bool CrackExternalPath(const FilePath& virtual_path, |
| 102 std::string* mount_name, |
| 103 FileSystemType* type, |
| 104 FilePath* path) const; |
| 105 |
| 106 // Returns the virtual root path that looks like /<mount_name>. |
| 107 FilePath CreateVirtualRootPath(const std::string& mount_name) const; |
| 108 |
| 109 private: |
| 110 friend class base::RefCountedThreadSafe<ExternalMountPoints>; |
| 111 |
| 112 // Represents each file system instance (defined in the .cc). |
| 113 class Instance; |
| 114 |
| 115 typedef std::map<std::string, Instance*> NameToInstance; |
| 116 |
| 117 // Reverse map from registered path to its corresponding mount name. |
| 118 typedef std::map<FilePath, std::string> PathToName; |
| 119 |
| 120 // Use |GetSystemInstance| of |CreateRefCounted| to get an instance. |
| 121 explicit ExternalMountPoints(); |
| 122 virtual ~ExternalMountPoints(); |
| 123 |
| 124 // Performs sanity checks on the new mount point. |
| 125 // Checks following: |
| 126 // - there is no registered mount point with mount_name |
| 127 // - path does not reference parent |
| 128 // - path is absolute |
| 129 // - path does not overlap with an existing mount point path. |
| 130 // |
| 131 // |lock_| should be taken before calling this method. |
| 132 bool ValidateNewMountPoint(const std::string& mount_name, |
| 133 const FilePath& path); |
| 134 |
| 135 // This lock needs to be obtained when accessing the instance_map_. |
| 136 mutable base::Lock lock_; |
| 137 |
| 138 NameToInstance instance_map_; |
| 139 PathToName path_to_name_map_; |
| 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(ExternalMountPoints); |
| 142 }; |
| 143 |
| 144 // Registers a scoped external filesystem which gets revoked when it scopes out. |
| 145 class WEBKIT_STORAGE_EXPORT ScopedExternalFileSystem { |
| 146 public: |
| 147 ScopedExternalFileSystem(const std::string& mount_name, |
| 148 FileSystemType type, |
| 149 const FilePath& path); |
| 150 ~ScopedExternalFileSystem(); |
| 151 |
| 152 FilePath GetVirtualRootPath() const; |
| 153 |
| 154 private: |
| 155 const std::string mount_name_; |
| 156 }; |
| 157 |
| 158 } // namespace fileapi |
| 159 |
| 160 #endif // WEBKIT_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ |
OLD | NEW |