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 to be used to register the file. The registered file can |
| 34 // be referred by a virtual path /<filesystem_id>/<name>. |
| 35 // The name should NOT contain a path separator '/'. |
| 36 std::string name; |
| 37 |
| 38 // The path of the file. |
| 39 FilePath path; |
| 40 |
| 41 // For STL operation. |
| 42 bool operator<(const FileInfo& that) const { return name < that.name; } |
| 43 }; |
| 44 |
| 45 class FILEAPI_EXPORT FileInfoSet { |
| 46 public: |
| 47 FileInfoSet(); |
| 48 ~FileInfoSet(); |
| 49 |
| 50 // Add the given |path| to the set and returns the registered name |
| 51 // assigned for the path. |
| 52 std::string AddPath(const FilePath& path); |
| 53 |
| 54 // Add the given |path| with the |name|. |
| 55 // Returns false if the |name| is already registered in the set. |
| 56 bool AddPathWithName(const FilePath& path, const std::string& name); |
| 57 |
| 58 const std::set<FileInfo>& fileset() const { return fileset_; } |
| 59 |
| 60 private: |
| 61 std::set<FileInfo> fileset_; |
| 62 }; |
| 63 |
29 // The instance is lazily created per browser process. | 64 // The instance is lazily created per browser process. |
30 static IsolatedContext* GetInstance(); | 65 static IsolatedContext* GetInstance(); |
31 | 66 |
32 // Registers a new file isolated filesystem with the given set of files | 67 // Registers a new isolated filesystem with the given FileInfoSet |files| |
33 // and returns the new filesystem_id. The files are registered with their | 68 // 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 | 69 // register_name as their keys so that later we can resolve the full paths |
35 // for the given file name in the isolated filesystem. We only expose the | 70 // for the given name. We only expose the name and the ID for the |
36 // key and the ID for the newly created filesystem to the renderer for | 71 // newly created filesystem to the renderer for the sake of security. |
37 // the sake of security. | |
38 // | 72 // |
39 // The renderer will be sending filesystem requests with a virtual path like | 73 // The renderer will be sending filesystem requests with a virtual path like |
40 // '/<filesystem_id>/<relative_path_from_the_basename_of_dropped_path>' | 74 // '/<filesystem_id>/<registered_name>/<relative_path_from_the_dropped_path>' |
41 // for which we could crack in the browser by calling CrackIsolatedPath to | 75 // for which we could crack in the browser process by calling |
42 // get the full path. | 76 // CrackIsolatedPath to get the full path. |
43 // | 77 // |
44 // For example: if a dropped file has a path like '/a/b/foo' we register | 78 // 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. | 79 // 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' | 80 // 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 | 81 // 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 | 82 // 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 | 83 // path is like '/a/b/dir' a virtual path like '/<fsid>/dir/foo' can be |
50 // cracked into '/a/b/dir/foo'. | 84 // cracked into '/a/b/dir/foo'. |
51 // | 85 // |
52 // Note that the path in |fileset| that contains '..' or is not an | 86 // Note that the path in |fileset| that contains '..' or is not an |
53 // absolute path is skipped and is not registerred. | 87 // absolute path is skipped and is not registerred. |
54 std::string RegisterIsolatedFileSystem(const std::set<FilePath>& fileset); | 88 std::string RegisterFileSystem(const FileInfoSet& files); |
| 89 |
| 90 // Registers a new isolated filesystem for a given |path|. |
| 91 // 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 |
| 93 // with the name internally assigned to the path. |
| 94 std::string RegisterFileSystemForFile(const FilePath& path, |
| 95 std::string* register_name); |
55 | 96 |
56 // Revokes filesystem specified by the given filesystem_id. | 97 // Revokes filesystem specified by the given filesystem_id. |
57 // Note that this revokes the filesystem no matter how many references it has. | 98 // 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 | 99 // It is ok to call this on the filesystem that has been already deleted |
59 // (if its reference count had reached 0). | 100 // (if its reference count had reached 0). |
60 void RevokeIsolatedFileSystem(const std::string& filesystem_id); | 101 void RevokeFileSystem(const std::string& filesystem_id); |
61 | 102 |
62 // Adds a reference to a filesystem specified by the given filesystem_id. | 103 // Adds a reference to a filesystem specified by the given filesystem_id. |
63 void AddReference(const std::string& filesystem_id); | 104 void AddReference(const std::string& filesystem_id); |
64 | 105 |
65 // Removes a reference to a filesystem specified by the given filesystem_id. | 106 // Removes a reference to a filesystem specified by the given filesystem_id. |
66 // If the reference count reaches 0 the isolated context gets destroyed. | 107 // 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 | 108 // It is ok to call this on the filesystem that has been already deleted |
68 // (e.g. by RevokeIsolatedFileSystem). | 109 // (e.g. by RevokeFileSystem). |
69 void RemoveReference(const std::string& filesystem_id); | 110 void RemoveReference(const std::string& filesystem_id); |
70 | 111 |
71 // Cracks the given |virtual_path| (which should look like | 112 // Cracks the given |virtual_path| (which should look like |
72 // "/<filesystem_id>/<relative_path>") and populates the |filesystem_id| | 113 // "/<filesystem_id>/<registered_name>/<relative_path>") and populates |
73 // and |platform_path| if the embedded <filesystem_id> is registerred | 114 // the |filesystem_id| and |platform_path| if the embedded <filesystem_id> |
74 // to this context. |root_path| is also populated to have the platform | 115 // is registerred to this context. |root_path| is also populated to have |
75 // root (toplevel) path for the |virtual_path| | 116 // the registered root (toplevel) file info for the |virtual_path|. |
76 // (i.e. |platform_path| = |root_path| + <relative_path>). | |
77 // | 117 // |
78 // Returns false if the given virtual_path or the cracked filesystem_id | 118 // Returns false if the given virtual_path or the cracked filesystem_id |
79 // is not valid. | 119 // is not valid. |
80 // | 120 // |
81 // Note that |root_path| and |platform_path| are set to empty paths if | 121 // 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 | 122 // |virtual_path| has no <relative_path> part (i.e. pointing to |
83 // the virtual root). | 123 // the virtual root). |
84 bool CrackIsolatedPath(const FilePath& virtual_path, | 124 bool CrackIsolatedPath(const FilePath& virtual_path, |
85 std::string* filesystem_id, | 125 std::string* filesystem_id, |
86 FilePath* root_path, | 126 FileInfo* root_info, |
87 FilePath* platform_path) const; | 127 FilePath* platform_path) const; |
88 | 128 |
89 // Returns a vector of the full paths of the top-level entry paths | 129 // Returns a set of FileInfo registered for the |filesystem_id|. |
90 // registered for the |filesystem_id|. Returns false if the | 130 // Returns false if the |filesystem_id| is not valid. |
91 // |filesystem_is| is not valid. | 131 bool GetRegisteredFileInfo(const std::string& filesystem_id, |
92 bool GetTopLevelPaths(const std::string& filesystem_id, | 132 std::vector<FileInfo>* files) const; |
93 std::vector<FilePath>* paths) const; | |
94 | 133 |
95 // Returns the virtual path that looks like /<filesystem_id>/<relative_path>. | 134 // Returns the virtual root path that looks like /<filesystem_id>. |
96 FilePath CreateVirtualPath(const std::string& filesystem_id, | 135 FilePath CreateVirtualRootPath(const std::string& filesystem_id) const; |
97 const FilePath& relative_path) const; | |
98 | 136 |
99 private: | 137 private: |
100 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; | 138 friend struct base::DefaultLazyInstanceTraits<IsolatedContext>; |
101 | 139 |
102 // Maps from filesystem id to a path conversion map for top-level entries. | 140 // Maps from filesystem id to a path conversion map for top-level entries. |
103 typedef std::map<FilePath, FilePath> PathMap; | 141 typedef std::set<FileInfo> FileSet; |
104 typedef std::map<std::string, PathMap> IDToPathMap; | 142 typedef std::map<std::string, FileSet> IDToFileSet; |
105 | 143 |
106 // Obtain an instance of this class via GetInstance(). | 144 // Obtain an instance of this class via GetInstance(). |
107 IsolatedContext(); | 145 IsolatedContext(); |
108 ~IsolatedContext(); | 146 ~IsolatedContext(); |
109 | 147 |
110 // Removes the given filesystem without locking. | 148 // Removes the given filesystem without locking. |
111 // (The caller must hold a lock) | 149 // (The caller must hold a lock) |
112 void RevokeWithoutLocking(const std::string& filesystem_id); | 150 void RevokeWithoutLocking(const std::string& filesystem_id); |
113 | 151 |
114 // Returns a new filesystem_id. Called with lock. | 152 // Returns a new filesystem_id. Called with lock. |
115 std::string GetNewFileSystemId() const; | 153 std::string GetNewFileSystemId() const; |
116 | 154 |
117 // This lock needs to be obtained when accessing the toplevel_map_. | 155 // This lock needs to be obtained when accessing the toplevel_map_. |
118 mutable base::Lock lock_; | 156 mutable base::Lock lock_; |
119 | 157 |
120 // Maps the toplevel entries to the filesystem id. | 158 // Maps the toplevel entries to the filesystem id. |
121 IDToPathMap toplevel_map_; | 159 IDToFileSet toplevel_map_; |
122 | 160 |
123 // Reference counts. Note that an isolated filesystem is created with ref==0. | 161 // Reference counts. Note that an isolated filesystem is created with ref==0. |
124 // and will get deleted when the ref count reaches <=0. | 162 // and will get deleted when the ref count reaches <=0. |
125 std::map<std::string, int> ref_counts_; | 163 std::map<std::string, int> ref_counts_; |
126 | 164 |
127 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); | 165 DISALLOW_COPY_AND_ASSIGN(IsolatedContext); |
128 }; | 166 }; |
129 | 167 |
130 } // namespace fileapi | 168 } // namespace fileapi |
131 | 169 |
132 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ | 170 #endif // WEBKIT_FILEAPI_ISOLATED_CONTEXT_H_ |
OLD | NEW |