| 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/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
| 18 #include "webkit/fileapi/file_system_types.h" |
| 18 #include "webkit/fileapi/fileapi_export.h" | 19 #include "webkit/fileapi/fileapi_export.h" |
| 19 | 20 |
| 20 namespace fileapi { | 21 namespace fileapi { |
| 21 | 22 |
| 22 // Manages isolated filename namespaces. A namespace is simply defined as a | 23 // Manages isolated filename namespaces. A namespace is simply defined as a |
| 23 // set of file paths and corresponding filesystem ID. This context class is | 24 // set of file paths and corresponding filesystem ID. This context class is |
| 24 // a singleton and access to the context is thread-safe (protected with a | 25 // a singleton and access to the context is thread-safe (protected with a |
| 25 // lock). | 26 // lock). |
| 26 // Some methods of this class are virtual just for mocking. | 27 // Some methods of this class are virtual just for mocking. |
| 27 class FILEAPI_EXPORT IsolatedContext { | 28 class FILEAPI_EXPORT IsolatedContext { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 40 | 41 |
| 41 // For STL operation. | 42 // For STL operation. |
| 42 bool operator<(const FileInfo& that) const { return name < that.name; } | 43 bool operator<(const FileInfo& that) const { return name < that.name; } |
| 43 }; | 44 }; |
| 44 | 45 |
| 45 class FILEAPI_EXPORT FileInfoSet { | 46 class FILEAPI_EXPORT FileInfoSet { |
| 46 public: | 47 public: |
| 47 FileInfoSet(); | 48 FileInfoSet(); |
| 48 ~FileInfoSet(); | 49 ~FileInfoSet(); |
| 49 | 50 |
| 50 // Add the given |path| to the set and returns the registered name | 51 // Add the given |path| to the set and populates |registered_name| with |
| 51 // assigned for the path. | 52 // the registered name assigned for the path. |path| needs to be |
| 52 std::string AddPath(const FilePath& path); | 53 // absolute and should not contain parent references. |
| 54 // Return false if the |path| is not valid and could not be added. |
| 55 bool AddPath(const FilePath& path, std::string* registered_name); |
| 53 | 56 |
| 54 // Add the given |path| with the |name|. | 57 // Add the given |path| with the |name|. |
| 55 // Returns false if the |name| is already registered in the set. | 58 // Return false if the |name| is already registered in the set or |
| 59 // is not valid and could not be added. |
| 56 bool AddPathWithName(const FilePath& path, const std::string& name); | 60 bool AddPathWithName(const FilePath& path, const std::string& name); |
| 57 | 61 |
| 58 const std::set<FileInfo>& fileset() const { return fileset_; } | 62 const std::set<FileInfo>& fileset() const { return fileset_; } |
| 59 | 63 |
| 60 private: | 64 private: |
| 61 std::set<FileInfo> fileset_; | 65 std::set<FileInfo> fileset_; |
| 62 }; | 66 }; |
| 63 | 67 |
| 64 // The instance is lazily created per browser process. | 68 // The instance is lazily created per browser process. |
| 65 static IsolatedContext* GetInstance(); | 69 static IsolatedContext* GetInstance(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 78 // 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 |
| 79 // the path with the name 'foo' in the newly created filesystem. | 83 // the path with the name 'foo' in the newly created filesystem. |
| 80 // 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' |
| 81 // 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 |
| 82 // 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 |
| 83 // 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 |
| 84 // cracked into '/a/b/dir/foo'. | 88 // cracked into '/a/b/dir/foo'. |
| 85 // | 89 // |
| 86 // 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 |
| 87 // absolute path is skipped and is not registerred. | 91 // absolute path is skipped and is not registerred. |
| 88 std::string RegisterFileSystem(const FileInfoSet& files); | 92 std::string RegisterDraggedFileSystem(const FileInfoSet& files); |
| 89 | 93 |
| 90 // Registers a new isolated filesystem for a given |path|. | 94 // Registers a new isolated filesystem for a given |path| of filesystem |
| 95 // |type| filesystem and returns a new filesystem ID. |
| 96 // |path| must be an absolute path which has no parent references ('..'). |
| 91 // 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 |
| 92 // registered as the given |register_name|, otherwise it is populated | 98 // registered as the given |register_name|, otherwise it is populated |
| 93 // with the name internally assigned to the path. | 99 // with the name internally assigned to the path. |
| 94 std::string RegisterFileSystemForFile(const FilePath& path, | 100 std::string RegisterFileSystemForPath(FileSystemType type, |
| 101 const FilePath& path, |
| 95 std::string* register_name); | 102 std::string* register_name); |
| 96 | 103 |
| 97 // Revokes filesystem specified by the given filesystem_id. | 104 // Revokes filesystem specified by the given filesystem_id. |
| 98 // Note that this revokes the filesystem no matter how many references it has. | 105 // Note that this revokes the filesystem no matter how many references it has. |
| 99 // It is ok to call this on the filesystem that has been already deleted | 106 // It is ok to call this on the filesystem that has been already deleted |
| 100 // (if its reference count had reached 0). | 107 // (if its reference count had reached 0). |
| 101 void RevokeFileSystem(const std::string& filesystem_id); | 108 void RevokeFileSystem(const std::string& filesystem_id); |
| 102 | 109 |
| 103 // Adds a reference to a filesystem specified by the given filesystem_id. | 110 // Adds a reference to a filesystem specified by the given filesystem_id. |
| 104 void AddReference(const std::string& filesystem_id); | 111 void AddReference(const std::string& filesystem_id); |
| 105 | 112 |
| 106 // Removes a reference to a filesystem specified by the given filesystem_id. | 113 // Removes a reference to a filesystem specified by the given filesystem_id. |
| 107 // If the reference count reaches 0 the isolated context gets destroyed. | 114 // If the reference count reaches 0 the isolated context gets destroyed. |
| 108 // It is ok to call this on the filesystem that has been already deleted | 115 // It is ok to call this on the filesystem that has been already deleted |
| 109 // (e.g. by RevokeFileSystem). | 116 // (e.g. by RevokeFileSystem). |
| 110 void RemoveReference(const std::string& filesystem_id); | 117 void RemoveReference(const std::string& filesystem_id); |
| 111 | 118 |
| 112 // Cracks the given |virtual_path| (which should look like | 119 // Cracks the given |virtual_path| (which should look like |
| 113 // "/<filesystem_id>/<registered_name>/<relative_path>") and populates | 120 // "/<filesystem_id>/<registered_name>/<relative_path>") and populates |
| 114 // the |filesystem_id| and |platform_path| if the embedded <filesystem_id> | 121 // the |filesystem_id| and |path| if the embedded <filesystem_id> |
| 115 // is registerred to this context. |root_path| is also populated to have | 122 // is registerred to this context. |root_path| is also populated to have |
| 116 // the registered root (toplevel) file info for the |virtual_path|. | 123 // the registered root (toplevel) file info for the |virtual_path|. |
| 117 // | 124 // |
| 118 // Returns false if the given virtual_path or the cracked filesystem_id | 125 // Returns false if the given virtual_path or the cracked filesystem_id |
| 119 // is not valid. | 126 // is not valid. |
| 120 // | 127 // |
| 121 // Note that |root_info| and |platform_path| are set to empty paths if | 128 // Note that |root_info| and |path| are set to empty paths if |
| 122 // |virtual_path| has no <relative_path> part (i.e. pointing to | 129 // |virtual_path| has no <relative_path> part (i.e. pointing to |
| 123 // the virtual root). | 130 // the virtual root). |
| 131 // |
| 132 // TODO(kinuko): Return filesystem type as well. |
| 124 bool CrackIsolatedPath(const FilePath& virtual_path, | 133 bool CrackIsolatedPath(const FilePath& virtual_path, |
| 125 std::string* filesystem_id, | 134 std::string* filesystem_id, |
| 126 FileInfo* root_info, | 135 FileInfo* root_info, |
| 127 FilePath* platform_path) const; | 136 FilePath* path) const; |
| 128 | 137 |
| 129 // Returns a set of FileInfo registered for the |filesystem_id|. | 138 // Returns a set of dragged FileInfo's registered for the |filesystem_id|. |
| 139 // The filesystem_id must be pointing to a dragged file system |
| 140 // (i.e. must be the one registered by RegisterDraggedFileSystem). |
| 130 // Returns false if the |filesystem_id| is not valid. | 141 // Returns false if the |filesystem_id| is not valid. |
| 131 bool GetRegisteredFileInfo(const std::string& filesystem_id, | 142 bool GetDraggedFileInfo(const std::string& filesystem_id, |
| 132 std::vector<FileInfo>* files) const; | 143 std::vector<FileInfo>* files) const; |
| 144 |
| 145 // Returns the file path registered for the |filesystem_id|. |
| 146 // The filesystem_id must NOT be pointing to a dragged file system |
| 147 // (i.e. must be the one registered by RegisterFileSystemForPath). |
| 148 // Returns false if the |filesystem_id| is not valid. |
| 149 bool GetRegisteredPath(const std::string& filesystem_id, |
| 150 FilePath* path) const; |
| 133 | 151 |
| 134 // Returns the virtual root path that looks like /<filesystem_id>. | 152 // Returns the virtual root path that looks like /<filesystem_id>. |
| 135 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; | 153 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; |
| 136 | 154 |
| 137 private: | 155 private: |
| 138 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; | 156 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; |
| 139 | 157 |
| 140 // Maps from filesystem id to a path conversion map for top-level entries. | 158 // Represents each isolated file system instance. |
| 141 typedef std::set<FileInfo> FileSet; | 159 class Instance { |
| 142 typedef std::map<std::string, FileSet> IDToFileSet; | 160 public: |
| 161 Instance(FileSystemType type, const FileInfo& file_info); |
| 162 explicit Instance(const std::set<FileInfo>& dragged_files); |
| 163 ~Instance(); |
| 164 |
| 165 FileSystemType type() const { return type_; } |
| 166 const FileInfo& file_info() const { return file_info_; } |
| 167 const std::set<FileInfo>& dragged_files() const { return dragged_files_; } |
| 168 int ref_counts() const { return ref_counts_; } |
| 169 |
| 170 void AddRef() { ++ref_counts_; } |
| 171 void RemoveRef() { --ref_counts_; } |
| 172 |
| 173 bool ResolvePathForName(const std::string& name, FilePath* path); |
| 174 |
| 175 private: |
| 176 const FileSystemType type_; |
| 177 const FileInfo file_info_; |
| 178 |
| 179 // For dragged file system. |
| 180 const std::set<FileInfo> dragged_files_; |
| 181 |
| 182 // Reference counts. Note that an isolated filesystem is created with ref==0 |
| 183 // and will get deleted when the ref count reaches <=0. |
| 184 int ref_counts_; |
| 185 |
| 186 DISALLOW_COPY_AND_ASSIGN(Instance); |
| 187 }; |
| 188 |
| 189 typedef std::map<std::string, Instance*> IDToInstance; |
| 143 | 190 |
| 144 // Obtain an instance of this class via GetInstance(). | 191 // Obtain an instance of this class via GetInstance(). |
| 145 IsolatedContext(); | 192 IsolatedContext(); |
| 146 ~IsolatedContext(); | 193 ~IsolatedContext(); |
| 147 | 194 |
| 148 // Removes the given filesystem without locking. | |
| 149 // (The caller must hold a lock) | |
| 150 void RevokeWithoutLocking(const std::string& filesystem_id); | |
| 151 | |
| 152 // Returns a new filesystem_id. Called with lock. | 195 // Returns a new filesystem_id. Called with lock. |
| 153 std::string GetNewFileSystemId() const; | 196 std::string GetNewFileSystemId() const; |
| 154 | 197 |
| 155 // This lock needs to be obtained when accessing the toplevel_map_. | 198 // This lock needs to be obtained when accessing the instance_map_. |
| 156 mutable base::Lock lock_; | 199 mutable base::Lock lock_; |
| 157 | 200 |
| 158 // Maps the toplevel entries to the filesystem id. | 201 IDToInstance instance_map_; |
| 159 IDToFileSet toplevel_map_; | |
| 160 | |
| 161 // Reference counts. Note that an isolated filesystem is created with ref==0. | |
| 162 // and will get deleted when the ref count reaches <=0. | |
| 163 std::map<std::string, int> ref_counts_; | |
| 164 | 202 |
| 165 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); | 203 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); |
| 166 }; | 204 }; |
| 167 | 205 |
| 168 } // namespace fileapi | 206 } // namespace fileapi |
| 169 | 207 |
| 170 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ | 208 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ |
| OLD | NEW |