OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ | 5 #ifndef WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ |
6 #define WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ | 6 #define WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "webkit/fileapi/file_system_types.h" | 18 #include "webkit/fileapi/file_system_types.h" |
| 19 #include "webkit/fileapi/mount_points.h" |
19 #include "webkit/storage/webkit_storage_export.h" | 20 #include "webkit/storage/webkit_storage_export.h" |
20 | 21 |
21 namespace fileapi { | 22 namespace fileapi { |
22 | 23 |
23 // Manages isolated filesystem namespaces. | 24 // Manages isolated filesystem mount points which have no well-known names |
24 // This context class is a singleton and access to the context is | 25 // and are identified by a string 'filesystem ID', which usually just looks |
25 // thread-safe (protected with a lock). | 26 // like random value. |
26 // | 27 // This type of filesystem can be created on the fly and may go away when it has |
27 // There are two types of filesystems managed by this context: | 28 // no references from renderers. |
28 // isolated and external. | 29 // Files in an isolated filesystem are registered with corresponding names and |
29 // The former is transient while the latter is persistent. | 30 // identified by a filesystem URL like: |
30 // | |
31 // * Transient isolated file systems have no name and are identified by | |
32 // string 'filesystem ID', which usually just looks like random value. | |
33 // This type of filesystem can be created on the fly and may go away | |
34 // when it has no references from renderers. | |
35 // Files in an isolated filesystem are registered with corresponding names | |
36 // and identified by a filesystem URL like: | |
37 // | 31 // |
38 // filesystem:<origin>/isolated/<filesystem_id>/<name>/relative/path | 32 // filesystem:<origin>/isolated/<filesystem_id>/<name>/relative/path |
39 // | 33 // |
40 // * Persistent external file systems are identified by 'mount name' | |
41 // and are persisted until RevokeFileSystem is called. | |
42 // Files in an external filesystem are identified by a filesystem URL like: | |
43 // | |
44 // filesystem:<origin>/external/<mount_name>/relative/path | |
45 // | |
46 // A filesystem instance is represented by IsolatedContext::Instance, and | |
47 // managed as a map instance_map_ which maps from filesystem ID (or name) | |
48 // to the instance. | |
49 // | |
50 // Some methods of this class are virtual just for mocking. | 34 // Some methods of this class are virtual just for mocking. |
51 // | 35 // |
52 // TODO(kinuko): This should have a better name since this handles both | 36 class WEBKIT_STORAGE_EXPORT IsolatedContext : public MountPoints { |
53 // isolated and external file systems. | |
54 // | |
55 class WEBKIT_STORAGE_EXPORT IsolatedContext { | |
56 public: | 37 public: |
57 struct WEBKIT_STORAGE_EXPORT FileInfo { | |
58 FileInfo(); | |
59 FileInfo(const std::string& name, const FilePath& path); | |
60 | |
61 // The name to be used to register the file. The registered file can | |
62 // be referred by a virtual path /<filesystem_id>/<name>. | |
63 // The name should NOT contain a path separator '/'. | |
64 std::string name; | |
65 | |
66 // The path of the file. | |
67 FilePath path; | |
68 | |
69 // For STL operation. | |
70 bool operator<(const FileInfo& that) const { return name < that.name; } | |
71 }; | |
72 | |
73 class WEBKIT_STORAGE_EXPORT FileInfoSet { | 38 class WEBKIT_STORAGE_EXPORT FileInfoSet { |
74 public: | 39 public: |
75 FileInfoSet(); | 40 FileInfoSet(); |
76 ~FileInfoSet(); | 41 ~FileInfoSet(); |
77 | 42 |
78 // Add the given |path| to the set and populates |registered_name| with | 43 // Add the given |path| to the set and populates |registered_name| with |
79 // the registered name assigned for the path. |path| needs to be | 44 // the registered name assigned for the path. |path| needs to be |
80 // absolute and should not contain parent references. | 45 // absolute and should not contain parent references. |
81 // Return false if the |path| is not valid and could not be added. | 46 // Return false if the |path| is not valid and could not be added. |
82 bool AddPath(const FilePath& path, std::string* registered_name); | 47 bool AddPath(const FilePath& path, std::string* registered_name); |
83 | 48 |
84 // Add the given |path| with the |name|. | 49 // Add the given |path| with the |name|. |
85 // Return false if the |name| is already registered in the set or | 50 // Return false if the |name| is already registered in the set or |
86 // is not valid and could not be added. | 51 // is not valid and could not be added. |
87 bool AddPathWithName(const FilePath& path, const std::string& name); | 52 bool AddPathWithName(const FilePath& path, const std::string& name); |
88 | 53 |
89 const std::set<FileInfo>& fileset() const { return fileset_; } | 54 const std::set<MountPointInfo>& fileset() const { return fileset_; } |
90 | 55 |
91 private: | 56 private: |
92 std::set<FileInfo> fileset_; | 57 std::set<MountPointInfo> fileset_; |
93 }; | 58 }; |
94 | 59 |
95 // The instance is lazily created per browser process. | 60 // The instance is lazily created per browser process. |
96 static IsolatedContext* GetInstance(); | 61 static IsolatedContext* GetInstance(); |
97 | 62 |
98 // Returns true if the given filesystem type is managed by IsolatedContext | 63 // Returns true if the given filesystem type is managed by IsolatedContext |
99 // (i.e. if the given |type| is Isolated or External). | 64 // (i.e. if the given |type| is Isolated or External). |
100 // TODO(kinuko): needs a better function name. | 65 // TODO(kinuko): needs a better function name. |
101 static bool IsIsolatedType(FileSystemType type); | 66 static bool IsIsolatedType(FileSystemType type); |
102 | 67 |
(...skipping 23 matching lines...) Expand all Loading... |
126 // Registers a new isolated filesystem for a given |path| of filesystem | 91 // Registers a new isolated filesystem for a given |path| of filesystem |
127 // |type| filesystem and returns a new filesystem ID. | 92 // |type| filesystem and returns a new filesystem ID. |
128 // |path| must be an absolute path which has no parent references ('..'). | 93 // |path| must be an absolute path which has no parent references ('..'). |
129 // If |register_name| is non-null and has non-empty string the path is | 94 // If |register_name| is non-null and has non-empty string the path is |
130 // registered as the given |register_name|, otherwise it is populated | 95 // registered as the given |register_name|, otherwise it is populated |
131 // with the name internally assigned to the path. | 96 // with the name internally assigned to the path. |
132 std::string RegisterFileSystemForPath(FileSystemType type, | 97 std::string RegisterFileSystemForPath(FileSystemType type, |
133 const FilePath& path, | 98 const FilePath& path, |
134 std::string* register_name); | 99 std::string* register_name); |
135 | 100 |
136 // Registers a new named external filesystem. | 101 // MountPoints override. |
137 // The |path| is registered as the root path of the mount point which | 102 virtual bool RevokeFileSystem(const std::string& filesystem_id) OVERRIDE; |
138 // is identified by a URL "filesystem:.../external/mount_name". | 103 virtual bool GetRegisteredPath(const std::string& filesystem_id, |
139 // | 104 FilePath* path) const OVERRIDE; |
140 // For example, if the path "/media/removable" is registered with | 105 virtual bool CrackVirtualPath(const FilePath& virtual_path, |
141 // the mount_name "removable", a filesystem URL like | 106 std::string* filesystem_id, |
142 // "filesystem:.../external/removable/a/b" will be resolved as | 107 FileSystemType* type, |
143 // "/media/removable/a/b". | 108 FilePath* path) const OVERRIDE; |
144 // | |
145 // The |mount_name| should NOT contain a path separator '/'. | |
146 // Returns false if the given name is already registered. | |
147 // | |
148 // An external file system registered by this method can be revoked | |
149 // by calling RevokeFileSystem with |mount_name|. | |
150 bool RegisterExternalFileSystem(const std::string& mount_name, | |
151 FileSystemType type, | |
152 const FilePath& path); | |
153 | |
154 // Returns a set of FilePath (of <mount_name, path>) registered as external. | |
155 std::vector<FileInfo> GetExternalMountPoints() const; | |
156 | |
157 // Revokes the filesystem |filesystem_id|. | |
158 // Returns false if the |filesystem_id| is not (no longer) registered. | |
159 bool RevokeFileSystem(const std::string& filesystem_id); | |
160 | 109 |
161 // Revokes all filesystem(s) registered for the given path. | 110 // Revokes all filesystem(s) registered for the given path. |
162 // This is assumed to be called when the registered path becomes | 111 // This is assumed to be called when the registered path becomes |
163 // globally invalid, e.g. when a device for the path is detached. | 112 // globally invalid, e.g. when a device for the path is detached. |
164 // | 113 // |
165 // Note that this revokes the filesystem no matter how many references it has. | 114 // Note that this revokes the filesystem no matter how many references it has. |
166 // It is ok to call this for the path that has no associated filesystems. | 115 // It is ok to call this for the path that has no associated filesystems. |
167 // Note that this only works for the filesystems registered by | 116 // Note that this only works for the filesystems registered by |
168 // |RegisterFileSystemForPath|. | 117 // |RegisterFileSystemForPath|. |
169 void RevokeFileSystemByPath(const FilePath& path); | 118 void RevokeFileSystemByPath(const FilePath& path); |
170 | 119 |
171 // Adds a reference to a filesystem specified by the given filesystem_id. | 120 // Adds a reference to a filesystem specified by the given filesystem_id. |
172 void AddReference(const std::string& filesystem_id); | 121 void AddReference(const std::string& filesystem_id); |
173 | 122 |
174 // Removes a reference to a filesystem specified by the given filesystem_id. | 123 // Removes a reference to a filesystem specified by the given filesystem_id. |
175 // If the reference count reaches 0 the isolated context gets destroyed. | 124 // If the reference count reaches 0 the isolated context gets destroyed. |
176 // It is ok to call this on the filesystem that has been already deleted | 125 // It is OK to call this on the filesystem that has been already deleted |
177 // (e.g. by RevokeFileSystemByPath). | 126 // (e.g. by RevokeFileSystemByPath). |
178 void RemoveReference(const std::string& filesystem_id); | 127 void RemoveReference(const std::string& filesystem_id); |
179 | 128 |
180 // Cracks the given |virtual_path| (which is the path part of a filesystem URL | 129 // Returns a set of dragged MountPointInfos registered for the |
181 // without '/isolated' or '/external' prefix) and populates the | 130 // |filesystem_id|. |
182 // |id_or_name|, |type|, and |path| if the <id_or_name> part embedded in | |
183 // the |virtual_path| (i.e. the first component of the |virtual_path|) is a | |
184 // valid registered filesystem ID or mount name for an isolated or external | |
185 // filesystem. | |
186 // | |
187 // Returns false if the given virtual_path or the cracked id_or_name | |
188 // is not valid. | |
189 // | |
190 // Note that |path| is set to empty paths if the filesystem type is isolated | |
191 // and |virtual_path| has no <relative_path> part (i.e. pointing to the | |
192 // virtual root). | |
193 bool CrackIsolatedPath(const FilePath& virtual_path, | |
194 std::string* id_or_name, | |
195 FileSystemType* type, | |
196 FilePath* path) const; | |
197 | |
198 // Returns a set of dragged FileInfo's registered for the |filesystem_id|. | |
199 // The filesystem_id must be pointing to a dragged file system | 131 // The filesystem_id must be pointing to a dragged file system |
200 // (i.e. must be the one registered by RegisterDraggedFileSystem). | 132 // (i.e. must be the one registered by RegisterDraggedFileSystem). |
201 // Returns false if the |filesystem_id| is not valid. | 133 // Returns false if the |filesystem_id| is not valid. |
202 bool GetDraggedFileInfo(const std::string& filesystem_id, | 134 bool GetDraggedFileInfo(const std::string& filesystem_id, |
203 std::vector<FileInfo>* files) const; | 135 std::vector<MountPointInfo>* files) const; |
204 | |
205 // Returns the file path registered for the |filesystem_id|. | |
206 // The filesystem_id must NOT be pointing to a dragged file system | |
207 // (i.e. must be the one registered by RegisterFileSystemForPath). | |
208 // Returns false if the |filesystem_id| is not valid. | |
209 bool GetRegisteredPath(const std::string& filesystem_id, | |
210 FilePath* path) const; | |
211 | 136 |
212 // Returns the virtual root path that looks like /<filesystem_id>. | 137 // Returns the virtual root path that looks like /<filesystem_id>. |
213 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; | 138 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; |
214 | 139 |
215 private: | 140 private: |
216 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; | 141 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; |
217 | 142 |
218 // Represents each file system instance (defined in the .cc). | 143 // Represents each file system instance (defined in the .cc). |
219 class Instance; | 144 class Instance; |
220 | 145 |
221 typedef std::map<std::string, Instance*> IDToInstance; | 146 typedef std::map<std::string, Instance*> IDToInstance; |
222 | 147 |
223 // Reverse map from registered path to IDs. | 148 // Reverse map from registered path to IDs. |
224 typedef std::map<FilePath, std::set<std::string> > PathToID; | 149 typedef std::map<FilePath, std::set<std::string> > PathToID; |
225 | 150 |
226 // Obtain an instance of this class via GetInstance(). | 151 // Obtain an instance of this class via GetInstance(). |
227 IsolatedContext(); | 152 IsolatedContext(); |
228 ~IsolatedContext(); | 153 virtual ~IsolatedContext(); |
229 | 154 |
230 // Unregisters a file system of given |filesystem_id|. Must be called with | 155 // Unregisters a file system of given |filesystem_id|. Must be called with |
231 // lock_ held. Returns true if the file system is unregistered. | 156 // lock_ held. Returns true if the file system is unregistered. |
232 bool UnregisterFileSystem(const std::string& filesystem_id); | 157 bool UnregisterFileSystem(const std::string& filesystem_id); |
233 | 158 |
234 // Returns a new filesystem_id. Called with lock. | 159 // Returns a new filesystem_id. Called with lock. |
235 std::string GetNewFileSystemId() const; | 160 std::string GetNewFileSystemId() const; |
236 | 161 |
237 // This lock needs to be obtained when accessing the instance_map_. | 162 // This lock needs to be obtained when accessing the instance_map_. |
238 mutable base::Lock lock_; | 163 mutable base::Lock lock_; |
239 | 164 |
240 IDToInstance instance_map_; | 165 IDToInstance instance_map_; |
241 PathToID path_to_id_map_; | 166 PathToID path_to_id_map_; |
242 | 167 |
243 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); | 168 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); |
244 }; | 169 }; |
245 | 170 |
246 // Registers a scoped external filesystem which gets revoked when it scopes out. | |
247 class WEBKIT_STORAGE_EXPORT ScopedExternalFileSystem { | |
248 public: | |
249 ScopedExternalFileSystem(const std::string& mount_name, | |
250 FileSystemType type, | |
251 const FilePath& path); | |
252 ~ScopedExternalFileSystem(); | |
253 | |
254 FilePath GetVirtualRootPath() const; | |
255 | |
256 private: | |
257 const std::string mount_name_; | |
258 }; | |
259 | |
260 } // namespace fileapi | 171 } // namespace fileapi |
261 | 172 |
262 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ | 173 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ |
OLD | NEW |