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> |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 // | 81 // |
82 // For example: if a dropped file has a path like '/a/b/foo' and we register | 82 // For example: if a dropped file has a path like '/a/b/foo' and we register |
83 // the path with the name 'foo' in the newly created filesystem. | 83 // the path with the name 'foo' in the newly created filesystem. |
84 // Later if the context is asked to crack a virtual path like '/<fsid>/foo' | 84 // Later if the context is asked to crack a virtual path like '/<fsid>/foo' |
85 // it can properly return the original path '/a/b/foo' by looking up the | 85 // it can properly return the original path '/a/b/foo' by looking up the |
86 // internal mapping. Similarly if a dropped entry is a directory and its | 86 // internal mapping. Similarly if a dropped entry is a directory and its |
87 // path is like '/a/b/dir' a virtual path like '/<fsid>/dir/foo' can be | 87 // path is like '/a/b/dir' a virtual path like '/<fsid>/dir/foo' can be |
88 // cracked into '/a/b/dir/foo'. | 88 // cracked into '/a/b/dir/foo'. |
89 // | 89 // |
90 // Note that the path in |fileset| that contains '..' or is not an | 90 // Note that the path in |fileset| that contains '..' or is not an |
91 // absolute path is skipped and is not registerred. | 91 // absolute path is skipped and is not registered. |
92 std::string RegisterDraggedFileSystem(const FileInfoSet& files); | 92 std::string RegisterDraggedFileSystem(const FileInfoSet& files); |
93 | 93 |
94 // Registers a new isolated filesystem for a given |path| of filesystem | 94 // Registers a new isolated filesystem for a given |path| of filesystem |
95 // |type| filesystem and returns a new filesystem ID. | 95 // |type| filesystem and returns a new filesystem ID. |
96 // |path| must be an absolute path which has no parent references ('..'). | 96 // |path| must be an absolute path which has no parent references ('..'). |
97 // If |register_name| is non-null and has non-empty string the path is | 97 // If |register_name| is non-null and has non-empty string the path is |
98 // registered as the given |register_name|, otherwise it is populated | 98 // registered as the given |register_name|, otherwise it is populated |
99 // with the name internally assigned to the path. | 99 // with the name internally assigned to the path. |
100 std::string RegisterFileSystemForPath(FileSystemType type, | 100 std::string RegisterFileSystemForPath(FileSystemType type, |
101 const FilePath& path, | 101 const FilePath& path, |
102 std::string* register_name); | 102 std::string* register_name); |
103 | 103 |
104 // Revokes filesystem specified by the given filesystem_id. | 104 // Revokes all filesystem(s) registered for the given path. |
| 105 // This is assumed to be called when the registered path becomes |
| 106 // globally invalid, e.g. when a device for the path is detached. |
| 107 // |
105 // Note that this revokes the filesystem no matter how many references it has. | 108 // Note that this revokes the filesystem no matter how many references it has. |
106 // It is ok to call this on the filesystem that has been already deleted | 109 // It is ok to call this for the path that has no associated filesystems. |
107 // (if its reference count had reached 0). | 110 // Note that this only works for the filesystems registered by |
108 void RevokeFileSystem(const std::string& filesystem_id); | 111 // |RegisterFileSystemForPath|. |
| 112 void RevokeFileSystemByPath(const FilePath& path); |
109 | 113 |
110 // Adds a reference to a filesystem specified by the given filesystem_id. | 114 // Adds a reference to a filesystem specified by the given filesystem_id. |
111 void AddReference(const std::string& filesystem_id); | 115 void AddReference(const std::string& filesystem_id); |
112 | 116 |
113 // Removes a reference to a filesystem specified by the given filesystem_id. | 117 // Removes a reference to a filesystem specified by the given filesystem_id. |
114 // If the reference count reaches 0 the isolated context gets destroyed. | 118 // If the reference count reaches 0 the isolated context gets destroyed. |
115 // It is ok to call this on the filesystem that has been already deleted | 119 // It is ok to call this on the filesystem that has been already deleted |
116 // (e.g. by RevokeFileSystem). | 120 // (e.g. by RevokeFileSystemByPath). |
117 void RemoveReference(const std::string& filesystem_id); | 121 void RemoveReference(const std::string& filesystem_id); |
118 | 122 |
119 // Cracks the given |virtual_path| (which should look like | 123 // Cracks the given |virtual_path| (which should look like |
120 // "/<filesystem_id>/<registered_name>/<relative_path>") and populates | 124 // "/<filesystem_id>/<registered_name>/<relative_path>") and populates |
121 // the |filesystem_id| and |path| if the embedded <filesystem_id> | 125 // the |filesystem_id| and |path| if the embedded <filesystem_id> |
122 // is registerred to this context. |root_path| is also populated to have | 126 // is registered to this context. |root_path| is also populated to have |
123 // the registered root (toplevel) file info for the |virtual_path|. | 127 // the registered root (toplevel) file info for the |virtual_path|. |
124 // | 128 // |
125 // Returns false if the given virtual_path or the cracked filesystem_id | 129 // Returns false if the given virtual_path or the cracked filesystem_id |
126 // is not valid. | 130 // is not valid. |
127 // | 131 // |
128 // Note that |path| is set to empty paths if |virtual_path| has no | 132 // Note that |path| is set to empty paths if |virtual_path| has no |
129 // <relative_path> part (i.e. pointing to the virtual root). | 133 // <relative_path> part (i.e. pointing to the virtual root). |
130 bool CrackIsolatedPath(const FilePath& virtual_path, | 134 bool CrackIsolatedPath(const FilePath& virtual_path, |
131 std::string* filesystem_id, | 135 std::string* filesystem_id, |
132 FileSystemType* type, | 136 FileSystemType* type, |
(...skipping 15 matching lines...) Expand all Loading... |
148 | 152 |
149 // Returns the virtual root path that looks like /<filesystem_id>. | 153 // Returns the virtual root path that looks like /<filesystem_id>. |
150 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; | 154 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; |
151 | 155 |
152 private: | 156 private: |
153 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; | 157 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; |
154 | 158 |
155 // Represents each isolated file system instance. | 159 // Represents each isolated file system instance. |
156 class Instance { | 160 class Instance { |
157 public: | 161 public: |
| 162 // For a single-path file system, which could be registered by |
| 163 // IsolatedContext::RegisterFileSystemForPath(). |
| 164 // Most of isolated file system contexts should be of this type. |
158 Instance(FileSystemType type, const FileInfo& file_info); | 165 Instance(FileSystemType type, const FileInfo& file_info); |
159 explicit Instance(const std::set<FileInfo>& dragged_files); | 166 |
| 167 // For a multi-paths file system. As of writing only file system |
| 168 // type which could have multi-paths is Dragged file system, and |
| 169 // could be registered by IsolatedContext::RegisterDraggedFileSystem(). |
| 170 Instance(FileSystemType type, const std::set<FileInfo>& files); |
| 171 |
160 ~Instance(); | 172 ~Instance(); |
161 | 173 |
162 FileSystemType type() const { return type_; } | 174 FileSystemType type() const { return type_; } |
163 const FileInfo& file_info() const { return file_info_; } | 175 const FileInfo& file_info() const { return file_info_; } |
164 const std::set<FileInfo>& dragged_files() const { return dragged_files_; } | 176 const std::set<FileInfo>& files() const { return files_; } |
165 int ref_counts() const { return ref_counts_; } | 177 int ref_counts() const { return ref_counts_; } |
166 | 178 |
167 void AddRef() { ++ref_counts_; } | 179 void AddRef() { ++ref_counts_; } |
168 void RemoveRef() { --ref_counts_; } | 180 void RemoveRef() { --ref_counts_; } |
169 | 181 |
170 bool ResolvePathForName(const std::string& name, FilePath* path); | 182 bool ResolvePathForName(const std::string& name, FilePath* path) const; |
| 183 |
| 184 // Returns true if the instance is a single-path instance. |
| 185 bool IsSinglePathInstance() const; |
171 | 186 |
172 private: | 187 private: |
173 const FileSystemType type_; | 188 const FileSystemType type_; |
| 189 |
| 190 // For single-path instance. |
174 const FileInfo file_info_; | 191 const FileInfo file_info_; |
175 | 192 |
176 // For dragged file system. | 193 // For multiple-path instance (e.g. dragged file system). |
177 const std::set<FileInfo> dragged_files_; | 194 const std::set<FileInfo> files_; |
178 | 195 |
179 // Reference counts. Note that an isolated filesystem is created with ref==0 | 196 // Reference counts. Note that an isolated filesystem is created with ref==0 |
180 // and will get deleted when the ref count reaches <=0. | 197 // and will get deleted when the ref count reaches <=0. |
181 int ref_counts_; | 198 int ref_counts_; |
182 | 199 |
183 DISALLOW_COPY_AND_ASSIGN(Instance); | 200 DISALLOW_COPY_AND_ASSIGN(Instance); |
184 }; | 201 }; |
185 | 202 |
186 typedef std::map<std::string, Instance*> IDToInstance; | 203 typedef std::map<std::string, Instance*> IDToInstance; |
187 | 204 |
| 205 // Reverse map from registered path to IDs. |
| 206 typedef std::map<FilePath, std::set<std::string> > PathToID; |
| 207 |
188 // Obtain an instance of this class via GetInstance(). | 208 // Obtain an instance of this class via GetInstance(). |
189 IsolatedContext(); | 209 IsolatedContext(); |
190 ~IsolatedContext(); | 210 ~IsolatedContext(); |
191 | 211 |
192 // Returns a new filesystem_id. Called with lock. | 212 // Returns a new filesystem_id. Called with lock. |
193 std::string GetNewFileSystemId() const; | 213 std::string GetNewFileSystemId() const; |
194 | 214 |
195 // This lock needs to be obtained when accessing the instance_map_. | 215 // This lock needs to be obtained when accessing the instance_map_. |
196 mutable base::Lock lock_; | 216 mutable base::Lock lock_; |
197 | 217 |
198 IDToInstance instance_map_; | 218 IDToInstance instance_map_; |
| 219 PathToID path_to_id_map_; |
199 | 220 |
200 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); | 221 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); |
201 }; | 222 }; |
202 | 223 |
203 } // namespace fileapi | 224 } // namespace fileapi |
204 | 225 |
205 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ | 226 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ |
OLD | NEW |