Delete unused downloads page asset.
[chromium-blink-merge.git] / storage / browser / fileapi / obfuscated_file_util.h
blobb8b1a4af869c53ec9a02801222af20255b8795b2
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
3 // found in the LICENSE file.
5 #ifndef STORAGE_BROWSER_FILEAPI_OBFUSCATED_FILE_UTIL_H_
6 #define STORAGE_BROWSER_FILEAPI_OBFUSCATED_FILE_UTIL_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/callback_forward.h"
14 #include "base/files/file.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util_proxy.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "storage/browser/blob/shareable_file_reference.h"
20 #include "storage/browser/fileapi/file_system_file_util.h"
21 #include "storage/browser/fileapi/file_system_url.h"
22 #include "storage/browser/fileapi/sandbox_directory_database.h"
23 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
24 #include "storage/browser/storage_browser_export.h"
25 #include "storage/common/fileapi/file_system_types.h"
27 namespace base {
28 class SequencedTaskRunner;
29 class TimeTicks;
32 namespace content {
33 class ObfuscatedFileUtilTest;
34 class QuotaBackendImplTest;
37 namespace storage {
38 class SpecialStoragePolicy;
41 class GURL;
43 namespace storage {
45 class FileSystemOperationContext;
46 class SandboxOriginDatabaseInterface;
47 class TimedTaskHelper;
49 // This file util stores directory information in LevelDB to obfuscate
50 // and to neutralize virtual file paths given by arbitrary apps.
51 // Files are stored with two-level isolation: per-origin and per-type.
52 // The isolation is done by storing data in separate directory partitions.
53 // For example, a file in Temporary file system for origin 'www.example.com'
54 // is stored in a different partition for a file in Persistent file system
55 // for the same origin, or for Temporary file system for another origin.
57 // * Per-origin directory name information is stored in a separate LevelDB,
58 // which is maintained by SandboxOriginDatabase.
59 // * Per-type directory name information is given by
60 // GetTypeStringForURLCallback that is given in CTOR.
61 // We use a small static mapping (e.g. 't' for Temporary type) for
62 // regular sandbox filesystems.
64 // The overall implementation philosophy of this class is that partial failures
65 // should leave us with an intact database; we'd prefer to leak the occasional
66 // backing file than have a database entry whose backing file is missing. When
67 // doing FSCK operations, if you find a loose backing file with no reference,
68 // you may safely delete it.
70 // This class must be deleted on the FILE thread, because that's where
71 // DropDatabases needs to be called.
72 class STORAGE_EXPORT_PRIVATE ObfuscatedFileUtil
73 : public FileSystemFileUtil {
74 public:
75 // Origin enumerator interface.
76 // An instance of this interface is assumed to be called on the file thread.
77 class AbstractOriginEnumerator {
78 public:
79 virtual ~AbstractOriginEnumerator() {}
81 // Returns the next origin. Returns empty if there are no more origins.
82 virtual GURL Next() = 0;
84 // Returns the current origin's information.
85 // |type_string| must be ascii string.
86 virtual bool HasTypeDirectory(const std::string& type_string) const = 0;
89 typedef base::Callback<std::string(const FileSystemURL&)>
90 GetTypeStringForURLCallback;
92 // |get_type_string_for_url| is user-defined callback that should return
93 // a type string for the given FileSystemURL. The type string is used
94 // to provide per-type isolation in the sandboxed filesystem directory.
95 // Note that this method is called on file_task_runner.
97 // |known_type_strings| are known type string names that this file system
98 // should care about.
99 // This info is used to determine whether we could delete the entire
100 // origin directory or not in DeleteDirectoryForOriginAndType. If no directory
101 // for any known type exists the origin directory may get deleted when
102 // one origin/type pair is deleted.
104 ObfuscatedFileUtil(storage::SpecialStoragePolicy* special_storage_policy,
105 const base::FilePath& file_system_directory,
106 leveldb::Env* env_override,
107 base::SequencedTaskRunner* file_task_runner,
108 const GetTypeStringForURLCallback& get_type_string_for_url,
109 const std::set<std::string>& known_type_strings,
110 SandboxFileSystemBackendDelegate* sandbox_delegate);
111 ~ObfuscatedFileUtil() override;
113 // FileSystemFileUtil overrides.
114 base::File CreateOrOpen(FileSystemOperationContext* context,
115 const FileSystemURL& url,
116 int file_flags) override;
117 base::File::Error EnsureFileExists(FileSystemOperationContext* context,
118 const FileSystemURL& url,
119 bool* created) override;
120 base::File::Error CreateDirectory(FileSystemOperationContext* context,
121 const FileSystemURL& url,
122 bool exclusive,
123 bool recursive) override;
124 base::File::Error GetFileInfo(FileSystemOperationContext* context,
125 const FileSystemURL& url,
126 base::File::Info* file_info,
127 base::FilePath* platform_file) override;
128 scoped_ptr<AbstractFileEnumerator> CreateFileEnumerator(
129 FileSystemOperationContext* context,
130 const FileSystemURL& root_url) override;
131 base::File::Error GetLocalFilePath(FileSystemOperationContext* context,
132 const FileSystemURL& file_system_url,
133 base::FilePath* local_path) override;
134 base::File::Error Touch(FileSystemOperationContext* context,
135 const FileSystemURL& url,
136 const base::Time& last_access_time,
137 const base::Time& last_modified_time) override;
138 base::File::Error Truncate(FileSystemOperationContext* context,
139 const FileSystemURL& url,
140 int64 length) override;
141 base::File::Error CopyOrMoveFile(FileSystemOperationContext* context,
142 const FileSystemURL& src_url,
143 const FileSystemURL& dest_url,
144 CopyOrMoveOption option,
145 bool copy) override;
146 base::File::Error CopyInForeignFile(FileSystemOperationContext* context,
147 const base::FilePath& src_file_path,
148 const FileSystemURL& dest_url) override;
149 base::File::Error DeleteFile(FileSystemOperationContext* context,
150 const FileSystemURL& url) override;
151 base::File::Error DeleteDirectory(FileSystemOperationContext* context,
152 const FileSystemURL& url) override;
153 storage::ScopedFile CreateSnapshotFile(
154 FileSystemOperationContext* context,
155 const FileSystemURL& url,
156 base::File::Error* error,
157 base::File::Info* file_info,
158 base::FilePath* platform_path) override;
160 // Same as the other CreateFileEnumerator, but with recursive support.
161 scoped_ptr<AbstractFileEnumerator> CreateFileEnumerator(
162 FileSystemOperationContext* context,
163 const FileSystemURL& root_url,
164 bool recursive);
166 // Returns true if the directory |url| is empty.
167 bool IsDirectoryEmpty(
168 FileSystemOperationContext* context,
169 const FileSystemURL& url);
171 // Gets the topmost directory specific to this origin and type. This will
172 // contain both the directory database's files and all the backing file
173 // subdirectories.
174 // Returns the topmost origin directory if |type_string| is empty.
175 // Returns an empty path if the directory is undefined.
176 // If the directory is defined, it will be returned, even if
177 // there is a file system error (e.g. the directory doesn't exist on disk and
178 // |create| is false). Callers should always check |error_code| to make sure
179 // the returned path is usable.
180 base::FilePath GetDirectoryForOriginAndType(
181 const GURL& origin,
182 const std::string& type_string,
183 bool create,
184 base::File::Error* error_code);
186 // Deletes the topmost directory specific to this origin and type. This will
187 // delete its directory database.
188 // Deletes the topmost origin directory if |type_string| is empty.
189 bool DeleteDirectoryForOriginAndType(
190 const GURL& origin,
191 const std::string& type_string);
193 // Frees resources used by an origin's filesystem.
194 void CloseFileSystemForOriginAndType(const GURL& origin,
195 const std::string& type_string);
197 // This method and all methods of its returned class must be called only on
198 // the FILE thread. The caller is responsible for deleting the returned
199 // object.
200 AbstractOriginEnumerator* CreateOriginEnumerator();
202 // Deletes a directory database from the database list in the ObfuscatedFSFU
203 // and destroys the database on the disk.
204 void DestroyDirectoryDatabase(const GURL& origin,
205 const std::string& type_string);
207 // Computes a cost for storing a given file in the obfuscated FSFU.
208 // As the cost of a file is independent of the cost of its parent directories,
209 // this ignores all but the BaseName of the supplied path. In order to
210 // compute the cost of adding a multi-segment directory recursively, call this
211 // on each path segment and add the results.
212 static int64 ComputeFilePathCost(const base::FilePath& path);
214 // Tries to prepopulate directory database for the given type strings.
215 // This tries from the first one in the given type_strings and stops
216 // once it succeeds to do so for one database (i.e. it prepopulates
217 // at most one database).
218 void MaybePrepopulateDatabase(
219 const std::vector<std::string>& type_strings_to_prepopulate);
221 private:
222 typedef SandboxDirectoryDatabase::FileId FileId;
223 typedef SandboxDirectoryDatabase::FileInfo FileInfo;
225 friend class ObfuscatedFileEnumerator;
226 friend class content::ObfuscatedFileUtilTest;
227 friend class content::QuotaBackendImplTest;
229 // Helper method to create an obfuscated file util for regular
230 // (temporary, persistent) file systems. Used only for testing.
231 // Note: this is implemented in sandbox_file_system_backend_delegate.cc.
232 static ObfuscatedFileUtil* CreateForTesting(
233 storage::SpecialStoragePolicy* special_storage_policy,
234 const base::FilePath& file_system_directory,
235 leveldb::Env* env_override,
236 base::SequencedTaskRunner* file_task_runner);
238 base::FilePath GetDirectoryForURL(
239 const FileSystemURL& url,
240 bool create,
241 base::File::Error* error_code);
243 // This just calls get_type_string_for_url_ callback that is given in ctor.
244 std::string CallGetTypeStringForURL(const FileSystemURL& url);
246 base::File::Error GetFileInfoInternal(
247 SandboxDirectoryDatabase* db,
248 FileSystemOperationContext* context,
249 const FileSystemURL& url,
250 FileId file_id,
251 FileInfo* local_info,
252 base::File::Info* file_info,
253 base::FilePath* platform_file_path);
255 // Creates a new file, both the underlying backing file and the entry in the
256 // database. |dest_file_info| is an in-out parameter. Supply the name and
257 // parent_id; data_path is ignored. On success, data_path will
258 // always be set to the relative path [from the root of the type-specific
259 // filesystem directory] of a NEW backing file. Returns the new file.
260 base::File CreateAndOpenFile(
261 FileSystemOperationContext* context,
262 const FileSystemURL& dest_url,
263 FileInfo* dest_file_info,
264 int file_flags);
266 // The same as CreateAndOpenFile except that a file is not returned and if a
267 // path is provided in |source_path|, it will be used as a source from which
268 // to COPY data.
269 base::File::Error CreateFile(
270 FileSystemOperationContext* context,
271 const base::FilePath& source_file_path,
272 const FileSystemURL& dest_url,
273 FileInfo* dest_file_info);
275 // Updates |db| and |dest_file_info| at the end of creating a new file.
276 base::File::Error CommitCreateFile(
277 const base::FilePath& root,
278 const base::FilePath& local_path,
279 SandboxDirectoryDatabase* db,
280 FileInfo* dest_file_info);
282 // This converts from a relative path [as is stored in the FileInfo.data_path
283 // field] to an absolute platform path that can be given to the native
284 // filesystem.
285 base::FilePath DataPathToLocalPath(
286 const FileSystemURL& url,
287 const base::FilePath& data_file_path);
289 std::string GetDirectoryDatabaseKey(const GURL& origin,
290 const std::string& type_string);
292 // This returns NULL if |create| flag is false and a filesystem does not
293 // exist for the given |url|.
294 // For read operations |create| should be false.
295 SandboxDirectoryDatabase* GetDirectoryDatabase(const FileSystemURL& url,
296 bool create);
298 // Gets the topmost directory specific to this origin. This will
299 // contain both the filesystem type subdirectories.
300 base::FilePath GetDirectoryForOrigin(const GURL& origin,
301 bool create,
302 base::File::Error* error_code);
304 void InvalidateUsageCache(FileSystemOperationContext* context,
305 const GURL& origin,
306 FileSystemType type);
308 void MarkUsed();
309 void DropDatabases();
311 // Initializes the origin database. |origin_hint| may be used as a hint
312 // for initializing database if it's not empty.
313 bool InitOriginDatabase(const GURL& origin_hint, bool create);
315 base::File::Error GenerateNewLocalPath(
316 SandboxDirectoryDatabase* db,
317 FileSystemOperationContext* context,
318 const FileSystemURL& url,
319 base::FilePath* root,
320 base::FilePath* local_path);
322 base::File CreateOrOpenInternal(
323 FileSystemOperationContext* context,
324 const FileSystemURL& url,
325 int file_flags);
327 bool HasIsolatedStorage(const GURL& origin);
329 typedef std::map<std::string, SandboxDirectoryDatabase*> DirectoryMap;
330 DirectoryMap directories_;
331 scoped_ptr<SandboxOriginDatabaseInterface> origin_database_;
332 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
333 base::FilePath file_system_directory_;
334 leveldb::Env* env_override_;
336 // Used to delete database after a certain period of inactivity.
337 int64 db_flush_delay_seconds_;
339 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
340 scoped_ptr<TimedTaskHelper> timer_;
342 GetTypeStringForURLCallback get_type_string_for_url_;
343 std::set<std::string> known_type_strings_;
345 // Not owned.
346 SandboxFileSystemBackendDelegate* sandbox_delegate_;
348 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtil);
351 } // namespace storage
353 #endif // STORAGE_BROWSER_FILEAPI_OBFUSCATED_FILE_UTIL_H_