Chromium Code Reviews| 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/fileapi_export.h" | 18 #include "webkit/fileapi/fileapi_export.h" |
| 19 | 19 |
| 20 namespace fileapi { | 20 namespace fileapi { |
| 21 | 21 |
| 22 // Manages isolated filename namespaces. A namespace is simply defined as a | 22 // Manages isolated filename namespaces. A namespace is simply defined as a |
| 23 // set of file paths and corresponding filesystem ID. This context class is | 23 // 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 | 24 // a singleton and access to the context is thread-safe (protected with a |
| 25 // lock). | 25 // lock). |
| 26 // Some methods of this class are virtual just for mocking. | 26 // Some methods of this class are virtual just for mocking. |
| 27 class FILEAPI_EXPORT IsolatedContext { | 27 class FILEAPI_EXPORT IsolatedContext { |
| 28 public: | 28 public: |
| 29 struct FILEAPI_EXPORT FileInfo { | |
| 30 FileInfo(); | |
| 31 FileInfo(const std::string& name, const FilePath& path); | |
| 32 | |
| 33 // The name of the file. | |
| 34 std::string name; | |
| 35 // The path of the file. | |
| 36 FilePath path; | |
| 37 }; | |
| 38 | |
| 29 // The instance is lazily created per browser process. | 39 // The instance is lazily created per browser process. |
| 30 static IsolatedContext* GetInstance(); | 40 static IsolatedContext* GetInstance(); |
| 31 | 41 |
| 32 // Registers a new file isolated filesystem with the given set of files | 42 // Get a convenient display name of the |path| which could be used as a key |
| 43 // in the toplevel_map_. Usually this simply returns path.BaseName() | |
| 44 // unless the |path| is a root directory (e.g. a device root). | |
| 45 // Not that this does not guarantee the uniqueness of the returned name. | |
| 46 static std::string GetNameForPath(const FilePath& path); | |
| 47 | |
| 48 // Registers a new isolated filesystem with the given set of FileInfo | |
| 33 // and returns the new filesystem_id. The files are registered with their | 49 // and returns the new filesystem_id. The files are registered with their |
| 34 // basenames as their keys so that later we can resolve the full paths | 50 // display_name as their names so that later we can resolve the full paths |
| 35 // for the given file name in the isolated filesystem. We only expose the | 51 // for the given file name in the isolated filesystem. We only expose the |
| 36 // key and the ID for the newly created filesystem to the renderer for | 52 // name and the ID for the newly created filesystem to the renderer for |
| 37 // the sake of security. | 53 // the sake of security. |
| 38 // | 54 // |
| 39 // The renderer will be sending filesystem requests with a virtual path like | 55 // The renderer will be sending filesystem requests with a virtual path like |
| 40 // '/<filesystem_id>/<relative_path_from_the_basename_of_dropped_path>' | 56 // '/<filesystem_id>/<relative_path_from_the_dropped_path>' |
| 41 // for which we could crack in the browser by calling CrackIsolatedPath to | 57 // for which we could crack in the browser by calling CrackIsolatedPath to |
| 42 // get the full path. | 58 // get the full path. |
| 43 // | 59 // |
| 44 // For example: if a dropped file has a path like '/a/b/foo' we register | 60 // For example: if a dropped file has a path like '/a/b/foo' and we register |
| 45 // the path with the key 'foo' in the newly created filesystem. | 61 // the path with the name 'foo' in the newly created filesystem. |
| 46 // Later if the context is asked to crack a virtual path like '/<fsid>/foo' | 62 // Later if the context is asked to crack a virtual path like '/<fsid>/foo' |
| 47 // it can properly return the original path '/a/b/foo' by looking up the | 63 // it can properly return the original path '/a/b/foo' by looking up the |
| 48 // internal mapping. Similarly if a dropped entry is a directory and its | 64 // internal mapping. Similarly if a dropped entry is a directory and its |
| 49 // path is like '/a/b/dir' a virtual path like '/<fsid>/dir/foo' can be | 65 // path is like '/a/b/dir' a virtual path like '/<fsid>/dir/foo' can be |
| 50 // cracked into '/a/b/dir/foo'. | 66 // cracked into '/a/b/dir/foo'. |
| 51 // | 67 // |
| 52 // Note that the path in |fileset| that contains '..' or is not an | 68 // Note that the path in |fileset| that contains '..' or is not an |
| 53 // absolute path is skipped and is not registerred. | 69 // absolute path is skipped and is not registerred. |
| 54 std::string RegisterIsolatedFileSystem(const std::set<FilePath>& fileset); | 70 std::string RegisterFileSystem(const std::vector<FileInfo>& files); |
|
benwells
2012/06/28 20:07:39
Is there a way to make this change that doesn't ne
kinuko
2012/06/29 10:11:32
The new API is trying to let the caller pick a nam
| |
| 71 | |
| 72 // Registers a new isolated filesystem for a given display name and file. | |
| 73 std::string RegisterFileSystemForFile(const std::string& display_name, | |
| 74 const FilePath& path); | |
| 55 | 75 |
| 56 // Revokes filesystem specified by the given filesystem_id. | 76 // Revokes filesystem specified by the given filesystem_id. |
| 57 // Note that this revokes the filesystem no matter how many references it has. | 77 // Note that this revokes the filesystem no matter how many references it has. |
| 58 // It is ok to call this on the filesystem that has been already deleted | 78 // It is ok to call this on the filesystem that has been already deleted |
| 59 // (if its reference count had reached 0). | 79 // (if its reference count had reached 0). |
| 60 void RevokeIsolatedFileSystem(const std::string& filesystem_id); | 80 void RevokeFileSystem(const std::string& filesystem_id); |
| 61 | 81 |
| 62 // Adds a reference to a filesystem specified by the given filesystem_id. | 82 // Adds a reference to a filesystem specified by the given filesystem_id. |
| 63 void AddReference(const std::string& filesystem_id); | 83 void AddReference(const std::string& filesystem_id); |
| 64 | 84 |
| 65 // Removes a reference to a filesystem specified by the given filesystem_id. | 85 // Removes a reference to a filesystem specified by the given filesystem_id. |
| 66 // If the reference count reaches 0 the isolated context gets destroyed. | 86 // If the reference count reaches 0 the isolated context gets destroyed. |
| 67 // It is ok to call this on the filesystem that has been already deleted | 87 // It is ok to call this on the filesystem that has been already deleted |
| 68 // (e.g. by RevokeIsolatedFileSystem). | 88 // (e.g. by RevokeFileSystem). |
| 69 void RemoveReference(const std::string& filesystem_id); | 89 void RemoveReference(const std::string& filesystem_id); |
| 70 | 90 |
| 71 // Cracks the given |virtual_path| (which should look like | 91 // Cracks the given |virtual_path| (which should look like |
| 72 // "/<filesystem_id>/<relative_path>") and populates the |filesystem_id| | 92 // "/<filesystem_id>/<relative_path>") and populates the |filesystem_id| |
| 73 // and |platform_path| if the embedded <filesystem_id> is registerred | 93 // and |platform_path| if the embedded <filesystem_id> is registerred |
| 74 // to this context. |root_path| is also populated to have the platform | 94 // to this context. |root_path| is also populated to have the registered |
| 75 // root (toplevel) path for the |virtual_path| | 95 // root (toplevel) file info for the |virtual_path|. |
| 76 // (i.e. |platform_path| = |root_path| + <relative_path>). | |
| 77 // | 96 // |
| 78 // Returns false if the given virtual_path or the cracked filesystem_id | 97 // Returns false if the given virtual_path or the cracked filesystem_id |
| 79 // is not valid. | 98 // is not valid. |
| 80 // | 99 // |
| 81 // Note that |root_path| and |platform_path| are set to empty paths if | 100 // Note that |root_info| and |platform_path| are set to empty paths if |
| 82 // |virtual_path| has no <relative_path> part (i.e. pointing to | 101 // |virtual_path| has no <relative_path> part (i.e. pointing to |
| 83 // the virtual root). | 102 // the virtual root). |
| 84 bool CrackIsolatedPath(const FilePath& virtual_path, | 103 bool CrackIsolatedPath(const FilePath& virtual_path, |
| 85 std::string* filesystem_id, | 104 std::string* filesystem_id, |
| 86 FilePath* root_path, | 105 FileInfo* root_info, |
| 87 FilePath* platform_path) const; | 106 FilePath* platform_path) const; |
| 88 | 107 |
| 89 // Returns a vector of the full paths of the top-level entry paths | 108 // Returns a vector of FileInfo registered for the |filesystem_id|. |
| 90 // registered for the |filesystem_id|. Returns false if the | 109 // Returns false if the |filesystem_id| is not valid. |
| 91 // |filesystem_is| is not valid. | 110 bool GetRegisteredFileInfo(const std::string& filesystem_id, |
| 92 bool GetTopLevelPaths(const std::string& filesystem_id, | 111 std::vector<FileInfo>* files) const; |
| 93 std::vector<FilePath>* paths) const; | |
| 94 | 112 |
| 95 // Returns the virtual path that looks like /<filesystem_id>/<relative_path>. | 113 // Returns the virtual root path that looks like /<filesystem_id>. |
| 96 FilePath CreateVirtualPath(const std::string& filesystem_id, | 114 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; |
| 97 const FilePath& relative_path) const; | |
| 98 | 115 |
| 99 // Set the filesystem writable if |writable| is true, non-writable | 116 // Set the filesystem writable if |writable| is true, non-writable |
| 100 // if it is false. Returns false if the |filesystem_id| is not valid. | 117 // if it is false. Returns false if the |filesystem_id| is not valid. |
| 101 bool SetWritable(const std::string& filesystem_id, bool writable); | 118 bool SetWritable(const std::string& filesystem_id, bool writable); |
| 102 | 119 |
| 103 // Returns true if the |filesystem_id| is writable. | 120 // Returns true if the |filesystem_id| is writable. |
| 104 bool IsWritable(const std::string& filesystem_id) const; | 121 bool IsWritable(const std::string& filesystem_id) const; |
| 105 | 122 |
| 106 private: | 123 private: |
| 107 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; | 124 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; |
| 108 | 125 |
| 109 // Maps from filesystem id to a path conversion map for top-level entries. | 126 // Maps from filesystem id to a path conversion map for top-level entries. |
| 110 typedef std::map<FilePath, FilePath> PathMap; | 127 typedef std::map<std::string, FileInfo> FileMap; |
|
tzik
2012/06/29 02:56:35
Can we use std::set instead?
kinuko
2012/06/29 08:31:22
Done.
| |
| 111 typedef std::map<std::string, PathMap> IDToPathMap; | 128 typedef std::map<std::string, FileMap> IDToFileMap; |
| 112 | 129 |
| 113 // Obtain an instance of this class via GetInstance(). | 130 // Obtain an instance of this class via GetInstance(). |
| 114 IsolatedContext(); | 131 IsolatedContext(); |
| 115 ~IsolatedContext(); | 132 ~IsolatedContext(); |
| 116 | 133 |
| 117 // Removes the given filesystem without locking. | 134 // Removes the given filesystem without locking. |
| 118 // (The caller must hold a lock) | 135 // (The caller must hold a lock) |
| 119 void RevokeWithoutLocking(const std::string& filesystem_id); | 136 void RevokeWithoutLocking(const std::string& filesystem_id); |
| 120 | 137 |
| 121 // Returns a new filesystem_id. Called with lock. | 138 // Returns a new filesystem_id. Called with lock. |
| 122 std::string GetNewFileSystemId() const; | 139 std::string GetNewFileSystemId() const; |
| 123 | 140 |
| 124 // This lock needs to be obtained when accessing the toplevel_map_. | 141 // This lock needs to be obtained when accessing the toplevel_map_. |
| 125 mutable base::Lock lock_; | 142 mutable base::Lock lock_; |
| 126 | 143 |
| 127 // Maps the toplevel entries to the filesystem id. | 144 // Maps the toplevel entries to the filesystem id. |
| 128 IDToPathMap toplevel_map_; | 145 IDToFileMap toplevel_map_; |
| 129 | 146 |
| 130 // Holds a set of writable ids. | 147 // Holds a set of writable ids. |
| 131 // Isolated file systems are created read-only by default, and this set | 148 // Isolated file systems are created read-only by default, and this set |
| 132 // holds a list of exceptions. | 149 // holds a list of exceptions. |
| 133 // Detailed filesystem permission may be provided by an external | 150 // Detailed filesystem permission may be provided by an external |
| 134 // security policy manager, e.g. ChildProcessSecurityPolicy. | 151 // security policy manager, e.g. ChildProcessSecurityPolicy. |
| 135 std::set<std::string> writable_ids_; | 152 std::set<std::string> writable_ids_; |
| 136 | 153 |
| 137 // Reference counts. Note that an isolated filesystem is created with ref==0. | 154 // Reference counts. Note that an isolated filesystem is created with ref==0. |
| 138 // and will get deleted when the ref count reaches <=0. | 155 // and will get deleted when the ref count reaches <=0. |
| 139 std::map<std::string, int> ref_counts_; | 156 std::map<std::string, int> ref_counts_; |
| 140 | 157 |
| 141 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); | 158 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); |
| 142 }; | 159 }; |
| 143 | 160 |
| 144 } // namespace fileapi | 161 } // namespace fileapi |
| 145 | 162 |
| 146 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ | 163 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ |
| OLD | NEW |