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); |
kinuko
2013/01/08 12:22:43
Can we deprecate this method?
tbarzic
2013/01/09 01:26:34
I think we'll be able to do that, but I'd leave it
| |
102 | 67 |
103 // Registers a new isolated filesystem with the given FileInfoSet |files| | 68 // Registers a new isolated filesystem with the given FileInfoSet |files| |
104 // and returns the new filesystem_id. The files are registered with their | 69 // and returns the new filesystem_id. The files are registered with their |
105 // register_name as their keys so that later we can resolve the full paths | 70 // register_name as their keys so that later we can resolve the full paths |
106 // for the given name. We only expose the name and the ID for the | 71 // for the given name. We only expose the name and the ID for the |
107 // newly created filesystem to the renderer for the sake of security. | 72 // newly created filesystem to the renderer for the sake of security. |
108 // | 73 // |
109 // The renderer will be sending filesystem requests with a virtual path like | 74 // The renderer will be sending filesystem requests with a virtual path like |
110 // '/<filesystem_id>/<registered_name>/<relative_path_from_the_dropped_path>' | 75 // '/<filesystem_id>/<registered_name>/<relative_path_from_the_dropped_path>' |
111 // for which we could crack in the browser process by calling | 76 // for which we could crack in the browser process by calling |
(...skipping 14 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. | |
137 // The |path| is registered as the root path of the mount point which | |
138 // is identified by a URL "filesystem:.../external/mount_name". | |
139 // | |
140 // For example, if the path "/media/removable" is registered with | |
141 // the mount_name "removable", a filesystem URL like | |
142 // "filesystem:.../external/removable/a/b" will be resolved as | |
143 // "/media/removable/a/b". | |
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 | 101 |
161 // Revokes all filesystem(s) registered for the given path. | 102 // Revokes all filesystem(s) registered for the given path. |
162 // This is assumed to be called when the registered path becomes | 103 // This is assumed to be called when the registered path becomes |
163 // globally invalid, e.g. when a device for the path is detached. | 104 // globally invalid, e.g. when a device for the path is detached. |
164 // | 105 // |
165 // Note that this revokes the filesystem no matter how many references it has. | 106 // 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. | 107 // 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 | 108 // Note that this only works for the filesystems registered by |
168 // |RegisterFileSystemForPath|. | 109 // |RegisterFileSystemForPath|. |
169 void RevokeFileSystemByPath(const FilePath& path); | 110 void RevokeFileSystemByPath(const FilePath& path); |
170 | 111 |
171 // Adds a reference to a filesystem specified by the given filesystem_id. | 112 // Adds a reference to a filesystem specified by the given filesystem_id. |
172 void AddReference(const std::string& filesystem_id); | 113 void AddReference(const std::string& filesystem_id); |
173 | 114 |
174 // Removes a reference to a filesystem specified by the given filesystem_id. | 115 // Removes a reference to a filesystem specified by the given filesystem_id. |
175 // If the reference count reaches 0 the isolated context gets destroyed. | 116 // 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 | 117 // It is ok to call this on the filesystem that has been already deleted |
177 // (e.g. by RevokeFileSystemByPath). | 118 // (e.g. by RevokeFileSystemByPath). |
178 void RemoveReference(const std::string& filesystem_id); | 119 void RemoveReference(const std::string& filesystem_id); |
179 | 120 |
180 // Cracks the given |virtual_path| (which is the path part of a filesystem URL | 121 // Cracks the given |virtual_path| (which is the path part of a filesystem URL |
181 // without '/isolated' or '/external' prefix) and populates the | 122 // without '/isolated' prefix) and populates the |filesystem_id|, |type|, and |
182 // |id_or_name|, |type|, and |path| if the <id_or_name> part embedded in | 123 // |path| if the <filesystem_id> part embedded in the |virtual_path| (i.e. the |
183 // the |virtual_path| (i.e. the first component of the |virtual_path|) is a | 124 // first component of the |virtual_path|) is a valid registered filesystem ID |
184 // valid registered filesystem ID or mount name for an isolated or external | 125 // for an isolated filesystem. |
185 // filesystem. | |
186 // | 126 // |
187 // Returns false if the given virtual_path or the cracked id_or_name | 127 // Returns false if the given virtual_path or the cracked filesystem_id is not |
188 // is not valid. | 128 // valid. |
189 // | 129 // |
190 // Note that |path| is set to empty paths if the filesystem type is isolated | 130 // 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 | 131 // and |virtual_path| has no <relative_path> part (i.e. pointing to the |
192 // virtual root). | 132 // virtual root). |
193 bool CrackIsolatedPath(const FilePath& virtual_path, | 133 bool CrackIsolatedPath(const FilePath& virtual_path, |
194 std::string* id_or_name, | 134 std::string* filesystem_id, |
195 FileSystemType* type, | 135 FileSystemType* type, |
196 FilePath* path) const; | 136 FilePath* path) const; |
197 | 137 |
198 // Returns a set of dragged FileInfo's registered for the |filesystem_id|. | 138 // Returns a set of dragged MountPointInfos registered for the |
139 // |filesystem_id|. | |
199 // The filesystem_id must be pointing to a dragged file system | 140 // The filesystem_id must be pointing to a dragged file system |
200 // (i.e. must be the one registered by RegisterDraggedFileSystem). | 141 // (i.e. must be the one registered by RegisterDraggedFileSystem). |
201 // Returns false if the |filesystem_id| is not valid. | 142 // Returns false if the |filesystem_id| is not valid. |
202 bool GetDraggedFileInfo(const std::string& filesystem_id, | 143 bool GetDraggedFileInfo(const std::string& filesystem_id, |
203 std::vector<FileInfo>* files) const; | 144 std::vector<MountPointInfo>* files) const; |
204 | 145 |
205 // Returns the file path registered for the |filesystem_id|. | 146 // MountPoints override. |
206 // The filesystem_id must NOT be pointing to a dragged file system | 147 virtual bool CanCrackMountType(FileSystemType type) const OVERRIDE; |
207 // (i.e. must be the one registered by RegisterFileSystemForPath). | 148 virtual bool GetRegisteredPath(const std::string& filesystem_id, |
208 // Returns false if the |filesystem_id| is not valid. | 149 FilePath* path) const OVERRIDE; |
209 bool GetRegisteredPath(const std::string& filesystem_id, | 150 virtual bool RevokeFileSystem(const std::string& filesystem_id) OVERRIDE; |
210 FilePath* path) const; | |
211 | 151 |
212 // Returns the virtual root path that looks like /<filesystem_id>. | 152 // Returns the virtual root path that looks like /<filesystem_id>. |
213 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; | 153 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; |
214 | 154 |
215 private: | 155 private: |
216 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; | 156 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; |
217 | 157 |
218 // Represents each file system instance (defined in the .cc). | 158 // Represents each file system instance (defined in the .cc). |
219 class Instance; | 159 class Instance; |
220 | 160 |
221 typedef std::map<std::string, Instance*> IDToInstance; | 161 typedef std::map<std::string, Instance*> IDToInstance; |
222 | 162 |
223 // Reverse map from registered path to IDs. | 163 // Reverse map from registered path to IDs. |
224 typedef std::map<FilePath, std::set<std::string> > PathToID; | 164 typedef std::map<FilePath, std::set<std::string> > PathToID; |
225 | 165 |
226 // Obtain an instance of this class via GetInstance(). | 166 // Obtain an instance of this class via GetInstance(). |
227 IsolatedContext(); | 167 IsolatedContext(); |
228 ~IsolatedContext(); | 168 virtual ~IsolatedContext(); |
229 | 169 |
230 // Unregisters a file system of given |filesystem_id|. Must be called with | 170 // Unregisters a file system of given |filesystem_id|. Must be called with |
231 // lock_ held. Returns true if the file system is unregistered. | 171 // lock_ held. Returns true if the file system is unregistered. |
232 bool UnregisterFileSystem(const std::string& filesystem_id); | 172 bool UnregisterFileSystem(const std::string& filesystem_id); |
233 | 173 |
234 // Returns a new filesystem_id. Called with lock. | 174 // Returns a new filesystem_id. Called with lock. |
235 std::string GetNewFileSystemId() const; | 175 std::string GetNewFileSystemId() const; |
236 | 176 |
237 // This lock needs to be obtained when accessing the instance_map_. | 177 // This lock needs to be obtained when accessing the instance_map_. |
238 mutable base::Lock lock_; | 178 mutable base::Lock lock_; |
239 | 179 |
240 IDToInstance instance_map_; | 180 IDToInstance instance_map_; |
241 PathToID path_to_id_map_; | 181 PathToID path_to_id_map_; |
242 | 182 |
243 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); | 183 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); |
244 }; | 184 }; |
245 | 185 |
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 | 186 } // namespace fileapi |
261 | 187 |
262 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ | 188 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ |
OLD | NEW |