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