OLD | NEW |
(Empty) | |
| 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_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 also registers a remote file |
| 65 // system 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 // MountPoints overrides. |
| 72 virtual bool RevokeFileSystem(const std::string& mount_name) OVERRIDE; |
| 73 virtual bool GetRegisteredPath(const std::string& mount_name, |
| 74 FilePath* path) const OVERRIDE; |
| 75 virtual bool CrackVirtualPath(const FilePath& virtual_path, |
| 76 std::string* mount_name, |
| 77 FileSystemType* type, |
| 78 FilePath* path) const OVERRIDE; |
| 79 |
| 80 // Retrieves the remote file system proxy for the registered file system. |
| 81 // Returns NULL if there is no file system with the given name, or if the file |
| 82 // system does not have a remote file system proxy. |
| 83 RemoteFileSystemProxyInterface* GetRemoteFileSystemProxy( |
| 84 const std::string& mount_name) const; |
| 85 |
| 86 // Returns a list of registered MountPointInfos (of <mount_name, path>). |
| 87 void AddMountPointInfosTo(std::vector<MountPointInfo>* mount_points) const; |
| 88 |
| 89 // Converts a path on a registered file system to virtual path relative to the |
| 90 // file system root. E.g. if 'Downloads' file system is mapped to |
| 91 // '/usr/local/home/Downloads', and |absolute| path is set to |
| 92 // '/usr/local/home/Downloads/foo', the method will set |virtual_path| to |
| 93 // 'Downloads/foo'. |
| 94 // Returns false if the path cannot be resolved (e.g. if the path is not |
| 95 // part of any registered filesystem). |
| 96 // |
| 97 // Returned virtual_path will have normalized path separators. |
| 98 bool GetVirtualPath(const FilePath& absolute_path, FilePath* virtual_path); |
| 99 |
| 100 // Returns the virtual root path that looks like /<mount_name>. |
| 101 FilePath CreateVirtualRootPath(const std::string& mount_name) const; |
| 102 |
| 103 private: |
| 104 friend class base::RefCountedThreadSafe<ExternalMountPoints>; |
| 105 |
| 106 // Represents each file system instance (defined in the .cc). |
| 107 class Instance; |
| 108 |
| 109 typedef std::map<std::string, Instance*> NameToInstance; |
| 110 |
| 111 // Reverse map from registered path to its corresponding mount name. |
| 112 typedef std::map<FilePath, std::string> PathToName; |
| 113 |
| 114 // Use |GetSystemInstance| of |CreateRefCounted| to get an instance. |
| 115 ExternalMountPoints(); |
| 116 virtual ~ExternalMountPoints(); |
| 117 |
| 118 // Performs sanity checks on the new mount point. |
| 119 // Checks the following: |
| 120 // - there is no registered mount point with mount_name |
| 121 // - path does not contain a reference to a parent |
| 122 // - path is absolute |
| 123 // - path does not overlap with an existing mount point path. |
| 124 // |
| 125 // |lock_| should be taken before calling this method. |
| 126 bool ValidateNewMountPoint(const std::string& mount_name, |
| 127 const FilePath& path); |
| 128 |
| 129 // This lock needs to be obtained when accessing the instance_map_. |
| 130 mutable base::Lock lock_; |
| 131 |
| 132 NameToInstance instance_map_; |
| 133 PathToName path_to_name_map_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(ExternalMountPoints); |
| 136 }; |
| 137 |
| 138 // Registers a scoped external filesystem which gets revoked when it scopes out. |
| 139 class WEBKIT_STORAGE_EXPORT ScopedExternalFileSystem { |
| 140 public: |
| 141 ScopedExternalFileSystem(const std::string& mount_name, |
| 142 FileSystemType type, |
| 143 const FilePath& path); |
| 144 ~ScopedExternalFileSystem(); |
| 145 |
| 146 FilePath GetVirtualRootPath() const; |
| 147 |
| 148 private: |
| 149 const std::string mount_name_; |
| 150 }; |
| 151 |
| 152 } // namespace fileapi |
| 153 |
| 154 #endif // WEBKIT_FILEAPI_EXTERNAL_MOUNT_POINTS_H_ |
| 155 |
OLD | NEW |