OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_OBFUSCATED_FILE_SYSTEM_FILE_UTIL_H_ | 5 #ifndef WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_H_ |
6 #define WEBKIT_FILEAPI_OBFUSCATED_FILE_SYSTEM_FILE_UTIL_H_ | 6 #define WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util_proxy.h" | 13 #include "base/file_util_proxy.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
(...skipping 21 matching lines...) Expand all Loading... |
38 // | 38 // |
39 // This class is RefCountedThreadSafe because it may gain a reference on the IO | 39 // This class is RefCountedThreadSafe because it may gain a reference on the IO |
40 // thread, but must be deleted on the FILE thread because that's where | 40 // thread, but must be deleted on the FILE thread because that's where |
41 // DropDatabases needs to be called. References will be held by the | 41 // DropDatabases needs to be called. References will be held by the |
42 // SandboxMountPointProvider [and the task it uses to drop the reference] and | 42 // SandboxMountPointProvider [and the task it uses to drop the reference] and |
43 // SandboxMountPointProvider::GetFileSystemRootPathTask. Without that last one, | 43 // SandboxMountPointProvider::GetFileSystemRootPathTask. Without that last one, |
44 // we wouldn't need ref counting. | 44 // we wouldn't need ref counting. |
45 // | 45 // |
46 // TODO(ericu): We don't ever update directory mtimes; which operations should | 46 // TODO(ericu): We don't ever update directory mtimes; which operations should |
47 // do that? | 47 // do that? |
48 class ObfuscatedFileSystemFileUtil : public FileSystemFileUtil, | 48 class ObfuscatedFileUtil : public FileSystemFileUtil, |
49 public base::RefCountedThreadSafe<ObfuscatedFileSystemFileUtil> { | 49 public base::RefCountedThreadSafe<ObfuscatedFileUtil> { |
50 public: | 50 public: |
| 51 // Origin enumerator interface. |
| 52 // An instance of this interface is assumed to be called on the file thread. |
| 53 class AbstractOriginEnumerator { |
| 54 public: |
| 55 virtual ~AbstractOriginEnumerator() {} |
| 56 |
| 57 // Returns the next origin. Returns empty if there are no more origins. |
| 58 virtual GURL Next() = 0; |
| 59 |
| 60 // Returns the current origin's information. |
| 61 virtual bool HasFileSystemType(FileSystemType type) const = 0; |
| 62 }; |
| 63 |
51 // |underlying_file_util| is owned by the instance. It will be deleted by | 64 // |underlying_file_util| is owned by the instance. It will be deleted by |
52 // the owner instance. For example, it can be instanciated as follows: | 65 // the owner instance. For example, it can be instanciated as follows: |
53 // FileSystemFileUtil* file_system_file_util = | 66 // FileSystemFileUtil* file_util = |
54 // new ObfuscatedFileSystemFileUtil(new FileSystemFileUtil()); | 67 // new ObfuscatedFileUtil(new NativeFileUtil()); |
55 ObfuscatedFileSystemFileUtil( | 68 ObfuscatedFileUtil(const FilePath& file_system_directory, |
56 const FilePath& file_system_directory, | 69 FileSystemFileUtil* underlying_file_util); |
57 FileSystemFileUtil* underlying_file_util); | 70 virtual ~ObfuscatedFileUtil(); |
58 virtual ~ObfuscatedFileSystemFileUtil(); | |
59 | 71 |
60 virtual base::PlatformFileError CreateOrOpen( | 72 virtual base::PlatformFileError CreateOrOpen( |
61 FileSystemOperationContext* context, | 73 FileSystemOperationContext* context, |
62 const FilePath& file_path, | 74 const FilePath& file_path, |
63 int file_flags, | 75 int file_flags, |
64 base::PlatformFile* file_handle, | 76 base::PlatformFile* file_handle, |
65 bool* created) OVERRIDE; | 77 bool* created) OVERRIDE; |
66 | 78 |
67 virtual base::PlatformFileError EnsureFileExists( | 79 virtual base::PlatformFileError EnsureFileExists( |
68 FileSystemOperationContext* context, | 80 FileSystemOperationContext* context, |
69 const FilePath& file_path, bool* created) OVERRIDE; | 81 const FilePath& file_path, bool* created) OVERRIDE; |
70 | 82 |
71 virtual base::PlatformFileError GetLocalFilePath( | 83 virtual base::PlatformFileError CreateDirectory( |
72 FileSystemOperationContext* context, | 84 FileSystemOperationContext* context, |
73 const FilePath& virtual_file, | 85 const FilePath& file_path, |
74 FilePath* local_path) OVERRIDE; | 86 bool exclusive, |
| 87 bool recursive) OVERRIDE; |
75 | 88 |
76 virtual base::PlatformFileError GetFileInfo( | 89 virtual base::PlatformFileError GetFileInfo( |
77 FileSystemOperationContext* context, | 90 FileSystemOperationContext* context, |
78 const FilePath& file, | 91 const FilePath& file, |
79 base::PlatformFileInfo* file_info, | 92 base::PlatformFileInfo* file_info, |
80 FilePath* platform_file) OVERRIDE; | 93 FilePath* platform_file) OVERRIDE; |
81 | 94 |
82 virtual base::PlatformFileError ReadDirectory( | 95 virtual base::PlatformFileError ReadDirectory( |
83 FileSystemOperationContext* context, | 96 FileSystemOperationContext* context, |
84 const FilePath& file_path, | 97 const FilePath& file_path, |
85 std::vector<base::FileUtilProxy::Entry>* entries) OVERRIDE; | 98 std::vector<base::FileUtilProxy::Entry>* entries) OVERRIDE; |
86 | 99 |
87 virtual base::PlatformFileError CreateDirectory( | 100 virtual AbstractFileEnumerator* CreateFileEnumerator( |
88 FileSystemOperationContext* context, | 101 FileSystemOperationContext* context, |
89 const FilePath& file_path, | 102 const FilePath& root_path) OVERRIDE; |
90 bool exclusive, | |
91 bool recursive) OVERRIDE; | |
92 | 103 |
93 virtual base::PlatformFileError CopyOrMoveFile( | 104 virtual base::PlatformFileError GetLocalFilePath( |
94 FileSystemOperationContext* context, | 105 FileSystemOperationContext* context, |
95 const FilePath& src_file_path, | 106 const FilePath& virtual_file, |
96 const FilePath& dest_file_path, | 107 FilePath* local_path) OVERRIDE; |
97 bool copy) OVERRIDE; | |
98 | |
99 virtual PlatformFileError CopyInForeignFile( | |
100 FileSystemOperationContext* context, | |
101 const FilePath& src_file_path, | |
102 const FilePath& dest_file_path) OVERRIDE; | |
103 | |
104 virtual base::PlatformFileError DeleteFile( | |
105 FileSystemOperationContext* context, | |
106 const FilePath& file_path) OVERRIDE; | |
107 | |
108 virtual base::PlatformFileError DeleteSingleDirectory( | |
109 FileSystemOperationContext* context, | |
110 const FilePath& file_path) OVERRIDE; | |
111 | 108 |
112 virtual base::PlatformFileError Touch( | 109 virtual base::PlatformFileError Touch( |
113 FileSystemOperationContext* context, | 110 FileSystemOperationContext* context, |
114 const FilePath& file_path, | 111 const FilePath& file_path, |
115 const base::Time& last_access_time, | 112 const base::Time& last_access_time, |
116 const base::Time& last_modified_time) OVERRIDE; | 113 const base::Time& last_modified_time) OVERRIDE; |
117 | 114 |
118 virtual base::PlatformFileError Truncate( | 115 virtual base::PlatformFileError Truncate( |
119 FileSystemOperationContext* context, | 116 FileSystemOperationContext* context, |
120 const FilePath& path, | 117 const FilePath& path, |
121 int64 length) OVERRIDE; | 118 int64 length) OVERRIDE; |
122 | 119 |
123 virtual bool PathExists( | 120 virtual bool PathExists( |
124 FileSystemOperationContext* context, | 121 FileSystemOperationContext* context, |
125 const FilePath& file_path) OVERRIDE; | 122 const FilePath& file_path) OVERRIDE; |
126 | 123 |
127 virtual bool DirectoryExists( | 124 virtual bool DirectoryExists( |
128 FileSystemOperationContext* context, | 125 FileSystemOperationContext* context, |
129 const FilePath& file_path) OVERRIDE; | 126 const FilePath& file_path) OVERRIDE; |
130 | 127 |
131 virtual bool IsDirectoryEmpty( | 128 virtual bool IsDirectoryEmpty( |
132 FileSystemOperationContext* context, | 129 FileSystemOperationContext* context, |
133 const FilePath& file_path) OVERRIDE; | 130 const FilePath& file_path) OVERRIDE; |
134 | 131 |
| 132 virtual base::PlatformFileError CopyOrMoveFile( |
| 133 FileSystemOperationContext* context, |
| 134 const FilePath& src_file_path, |
| 135 const FilePath& dest_file_path, |
| 136 bool copy) OVERRIDE; |
| 137 |
| 138 virtual PlatformFileError CopyInForeignFile( |
| 139 FileSystemOperationContext* context, |
| 140 const FilePath& src_file_path, |
| 141 const FilePath& dest_file_path) OVERRIDE; |
| 142 |
| 143 virtual base::PlatformFileError DeleteFile( |
| 144 FileSystemOperationContext* context, |
| 145 const FilePath& file_path) OVERRIDE; |
| 146 |
| 147 virtual base::PlatformFileError DeleteSingleDirectory( |
| 148 FileSystemOperationContext* context, |
| 149 const FilePath& file_path) OVERRIDE; |
| 150 |
135 // Gets the topmost directory specific to this origin and type. This will | 151 // Gets the topmost directory specific to this origin and type. This will |
136 // contain both the directory database's files and all the backing file | 152 // contain both the directory database's files and all the backing file |
137 // subdirectories. | 153 // subdirectories. |
138 FilePath GetDirectoryForOriginAndType( | 154 FilePath GetDirectoryForOriginAndType( |
139 const GURL& origin, FileSystemType type, bool create); | 155 const GURL& origin, FileSystemType type, bool create); |
140 | 156 |
141 // Deletes the topmost directory specific to this origin and type. This will | 157 // Deletes the topmost directory specific to this origin and type. This will |
142 // delete its directory database. | 158 // delete its directory database. |
143 bool DeleteDirectoryForOriginAndType(const GURL& origin, FileSystemType type); | 159 bool DeleteDirectoryForOriginAndType(const GURL& origin, FileSystemType type); |
144 | 160 |
145 // This will migrate a filesystem from the old passthrough sandbox into the | 161 // This will migrate a filesystem from the old passthrough sandbox into the |
146 // new obfuscated one. It won't obfuscate the old filenames [it will maintain | 162 // new obfuscated one. It won't obfuscate the old filenames [it will maintain |
147 // the old structure, but move it to a new root], but any new files created | 163 // the old structure, but move it to a new root], but any new files created |
148 // will go into the new standard locations. This will be completely | 164 // will go into the new standard locations. This will be completely |
149 // transparent to the user. This migration is atomic in that it won't alter | 165 // transparent to the user. This migration is atomic in that it won't alter |
150 // the source data until it's done, and that will be with a single directory | 166 // the source data until it's done, and that will be with a single directory |
151 // move [the directory with the unguessable name will move into the new | 167 // move [the directory with the unguessable name will move into the new |
152 // filesystem storage directory]. However, if this fails partway through, it | 168 // filesystem storage directory]. However, if this fails partway through, it |
153 // might leave a seemingly-valid database for this origin. When it starts up, | 169 // might leave a seemingly-valid database for this origin. When it starts up, |
154 // it will clear any such database, just in case. | 170 // it will clear any such database, just in case. |
155 bool MigrateFromOldSandbox( | 171 bool MigrateFromOldSandbox( |
156 const GURL& origin, FileSystemType type, const FilePath& root); | 172 const GURL& origin, FileSystemType type, const FilePath& root); |
157 | 173 |
158 // TODO(ericu): This doesn't really feel like it belongs in this class. | 174 // TODO(ericu): This doesn't really feel like it belongs in this class. |
159 // The previous version lives in FileSystemPathManager, but perhaps | 175 // The previous version lives in FileSystemPathManager, but perhaps |
160 // SandboxMountPointProvider would be better? | 176 // SandboxMountPointProvider would be better? |
161 static FilePath::StringType GetDirectoryNameForType(FileSystemType type); | 177 static FilePath::StringType GetDirectoryNameForType(FileSystemType type); |
162 | 178 |
163 // Origin enumerator interface. | |
164 // An instance of this interface is assumed to be called on the file thread. | |
165 class AbstractOriginEnumerator { | |
166 public: | |
167 virtual ~AbstractOriginEnumerator() {} | |
168 | |
169 // Returns the next origin. Returns empty if there are no more origins. | |
170 virtual GURL Next() = 0; | |
171 | |
172 // Returns the current origin's information. | |
173 virtual bool HasFileSystemType(FileSystemType type) const = 0; | |
174 }; | |
175 | |
176 // This method and all methods of its returned class must be called only on | 179 // This method and all methods of its returned class must be called only on |
177 // the FILE thread. The caller is responsible for deleting the returned | 180 // the FILE thread. The caller is responsible for deleting the returned |
178 // object. | 181 // object. |
179 AbstractOriginEnumerator* CreateOriginEnumerator(); | 182 AbstractOriginEnumerator* CreateOriginEnumerator(); |
180 | 183 |
181 virtual AbstractFileEnumerator* CreateFileEnumerator( | |
182 FileSystemOperationContext* context, | |
183 const FilePath& root_path) OVERRIDE; | |
184 | |
185 // Deletes a directory database from the database list in the ObfuscatedFSFU | 184 // Deletes a directory database from the database list in the ObfuscatedFSFU |
186 // and destroys the database on the disk. | 185 // and destroys the database on the disk. |
187 bool DestroyDirectoryDatabase(const GURL& origin, FileSystemType type); | 186 bool DestroyDirectoryDatabase(const GURL& origin, FileSystemType type); |
188 | 187 |
189 // Computes a cost for storing a given file in the obfuscated FSFU. | 188 // Computes a cost for storing a given file in the obfuscated FSFU. |
190 // As the cost of a file is independent of the cost of its parent directories, | 189 // As the cost of a file is independent of the cost of its parent directories, |
191 // this ignores all but the BaseName of the supplied path. In order to | 190 // this ignores all but the BaseName of the supplied path. In order to |
192 // compute the cost of adding a multi-segment directory recursively, call this | 191 // compute the cost of adding a multi-segment directory recursively, call this |
193 // on each path segment and add the results. | 192 // on each path segment and add the results. |
194 static int64 ComputeFilePathCost(const FilePath& path); | 193 static int64 ComputeFilePathCost(const FilePath& path); |
(...skipping 19 matching lines...) Expand all Loading... |
214 // responsible for closing. If you supply a path in source_path, it will be | 213 // responsible for closing. If you supply a path in source_path, it will be |
215 // used as a source from which to COPY data. | 214 // used as a source from which to COPY data. |
216 // Caveat: do not supply handle if you're also supplying a data path. It was | 215 // Caveat: do not supply handle if you're also supplying a data path. It was |
217 // easier not to support this, and no code has needed it so far, so it will | 216 // easier not to support this, and no code has needed it so far, so it will |
218 // DCHECK and handle will hold base::kInvalidPlatformFileValue. | 217 // DCHECK and handle will hold base::kInvalidPlatformFileValue. |
219 base::PlatformFileError CreateFile( | 218 base::PlatformFileError CreateFile( |
220 FileSystemOperationContext* context, | 219 FileSystemOperationContext* context, |
221 const GURL& origin_url, FileSystemType type, | 220 const GURL& origin_url, FileSystemType type, |
222 const FilePath& source_path, FileInfo* file_info, | 221 const FilePath& source_path, FileInfo* file_info, |
223 int file_flags, base::PlatformFile* handle); | 222 int file_flags, base::PlatformFile* handle); |
| 223 |
224 // Given the filesystem's root URL and a virtual path, produces a real, full | 224 // Given the filesystem's root URL and a virtual path, produces a real, full |
225 // local path to the underlying data file. This does a database lookup, and | 225 // local path to the underlying data file. This does a database lookup, and |
226 // verifies that the file exists. | 226 // verifies that the file exists. |
227 FilePath GetLocalPath( | 227 FilePath GetLocalPath( |
228 const GURL& origin_url, | 228 const GURL& origin_url, |
229 FileSystemType type, | 229 FileSystemType type, |
230 const FilePath& virtual_path); | 230 const FilePath& virtual_path); |
| 231 |
231 // This converts from a relative path [as is stored in the FileInfo.data_path | 232 // This converts from a relative path [as is stored in the FileInfo.data_path |
232 // field] to an absolute local path that can be given to the operating system. | 233 // field] to an absolute local path that can be given to the operating system. |
233 // It does no checks as to whether the file actually exists; it's pure path | 234 // It does no checks as to whether the file actually exists; it's pure path |
234 // manipulation. | 235 // manipulation. |
235 FilePath DataPathToLocalPath( | 236 FilePath DataPathToLocalPath( |
236 const GURL& origin, FileSystemType type, const FilePath& data_path); | 237 const GURL& origin, FileSystemType type, const FilePath& data_path); |
| 238 |
237 // This does the reverse of DataPathToLocalPath. | 239 // This does the reverse of DataPathToLocalPath. |
238 FilePath LocalPathToDataPath( | 240 FilePath LocalPathToDataPath( |
239 const GURL& origin, FileSystemType type, const FilePath& local_path); | 241 const GURL& origin, FileSystemType type, const FilePath& local_path); |
| 242 |
240 // This returns NULL if |create| flag is false and a filesystem does not | 243 // This returns NULL if |create| flag is false and a filesystem does not |
241 // exist for the given |origin_url| and |type|. | 244 // exist for the given |origin_url| and |type|. |
242 // For read operations |create| should be false. | 245 // For read operations |create| should be false. |
243 FileSystemDirectoryDatabase* GetDirectoryDatabase( | 246 FileSystemDirectoryDatabase* GetDirectoryDatabase( |
244 const GURL& origin_url, FileSystemType type, bool create); | 247 const GURL& origin_url, FileSystemType type, bool create); |
| 248 |
245 // Gets the topmost directory specific to this origin. This will | 249 // Gets the topmost directory specific to this origin. This will |
246 // contain both the filesystem type subdirectories. | 250 // contain both the filesystem type subdirectories. |
247 FilePath GetDirectoryForOrigin(const GURL& origin, bool create); | 251 FilePath GetDirectoryForOrigin(const GURL& origin, bool create); |
| 252 |
248 void MarkUsed(); | 253 void MarkUsed(); |
249 void DropDatabases(); | 254 void DropDatabases(); |
250 bool InitOriginDatabase(bool create); | 255 bool InitOriginDatabase(bool create); |
251 | 256 |
252 typedef std::map<std::string, FileSystemDirectoryDatabase*> DirectoryMap; | 257 typedef std::map<std::string, FileSystemDirectoryDatabase*> DirectoryMap; |
253 DirectoryMap directories_; | 258 DirectoryMap directories_; |
254 scoped_ptr<FileSystemOriginDatabase> origin_database_; | 259 scoped_ptr<FileSystemOriginDatabase> origin_database_; |
255 FilePath file_system_directory_; | 260 FilePath file_system_directory_; |
256 base::OneShotTimer<ObfuscatedFileSystemFileUtil> timer_; | 261 base::OneShotTimer<ObfuscatedFileUtil> timer_; |
257 scoped_ptr<FileSystemFileUtil> underlying_file_util_; | |
258 | 262 |
259 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtil); | 263 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtil); |
260 }; | 264 }; |
261 | 265 |
262 } // namespace fileapi | 266 } // namespace fileapi |
263 | 267 |
264 #endif // WEBKIT_FILEAPI_OBFUSCATED_FILE_SYSTEM_FILE_UTIL_H_ | 268 #endif // WEBKIT_FILEAPI_OBFUSCATED_FILE_UTIL_H_ |
OLD | NEW |