Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: webkit/fileapi/isolated_context.h

Issue 11648027: Extract external file systems handling from isolated context. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: few nits Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 // Returns a set of dragged MountPointInfos registered for the
181 // without '/isolated' or '/external' prefix) and populates the 122 // |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 123 // The filesystem_id must be pointing to a dragged file system
200 // (i.e. must be the one registered by RegisterDraggedFileSystem). 124 // (i.e. must be the one registered by RegisterDraggedFileSystem).
201 // Returns false if the |filesystem_id| is not valid. 125 // Returns false if the |filesystem_id| is not valid.
202 bool GetDraggedFileInfo(const std::string& filesystem_id, 126 bool GetDraggedFileInfo(const std::string& filesystem_id,
203 std::vector<FileInfo>* files) const; 127 std::vector<MountPointInfo>* files) const;
204 128
205 // Returns the file path registered for the |filesystem_id|. 129 // MountPoints override.
206 // The filesystem_id must NOT be pointing to a dragged file system 130 virtual bool RevokeFileSystem(const std::string& filesystem_id) OVERRIDE;
207 // (i.e. must be the one registered by RegisterFileSystemForPath). 131 virtual bool GetRegisteredPath(const std::string& filesystem_id,
208 // Returns false if the |filesystem_id| is not valid. 132 FilePath* path) const OVERRIDE;
209 bool GetRegisteredPath(const std::string& filesystem_id, 133 virtual bool CrackVirtualPath(const FilePath& virtual_path,
210 FilePath* path) const; 134 std::string* filesystem_id,
135 FileSystemType* type,
136 FilePath* path) const OVERRIDE;
211 137
212 // Returns the virtual root path that looks like /<filesystem_id>. 138 // Returns the virtual root path that looks like /<filesystem_id>.
213 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; 139 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const;
214 140
215 private: 141 private:
216 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; 142 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>;
217 143
218 // Represents each file system instance (defined in the .cc). 144 // Represents each file system instance (defined in the .cc).
219 class Instance; 145 class Instance;
220 146
221 typedef std::map<std::string, Instance*> IDToInstance; 147 typedef std::map<std::string, Instance*> IDToInstance;
222 148
223 // Reverse map from registered path to IDs. 149 // Reverse map from registered path to IDs.
224 typedef std::map<FilePath, std::set<std::string> > PathToID; 150 typedef std::map<FilePath, std::set<std::string> > PathToID;
225 151
226 // Obtain an instance of this class via GetInstance(). 152 // Obtain an instance of this class via GetInstance().
227 IsolatedContext(); 153 IsolatedContext();
228 ~IsolatedContext(); 154 virtual ~IsolatedContext();
229 155
230 // Unregisters a file system of given |filesystem_id|. Must be called with 156 // Unregisters a file system of given |filesystem_id|. Must be called with
231 // lock_ held. Returns true if the file system is unregistered. 157 // lock_ held. Returns true if the file system is unregistered.
232 bool UnregisterFileSystem(const std::string& filesystem_id); 158 bool UnregisterFileSystem(const std::string& filesystem_id);
233 159
234 // Returns a new filesystem_id. Called with lock. 160 // Returns a new filesystem_id. Called with lock.
235 std::string GetNewFileSystemId() const; 161 std::string GetNewFileSystemId() const;
236 162
237 // This lock needs to be obtained when accessing the instance_map_. 163 // This lock needs to be obtained when accessing the instance_map_.
238 mutable base::Lock lock_; 164 mutable base::Lock lock_;
239 165
240 IDToInstance instance_map_; 166 IDToInstance instance_map_;
241 PathToID path_to_id_map_; 167 PathToID path_to_id_map_;
242 168
243 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); 169 DISALLOW_COPY_AND_ASSIGN(IsolatedContext);
244 }; 170 };
245 171
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 172 } // namespace fileapi
261 173
262 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ 174 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698