Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / webkit / browser / fileapi / obfuscated_file_util.h
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.
4
5 #ifndef WEBKIT_BROWSER_FILEAPI_OBFUSCATED_FILE_UTIL_H_
6 #define WEBKIT_BROWSER_FILEAPI_OBFUSCATED_FILE_UTIL_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
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 "base/platform_file.h"
20 #include "webkit/browser/fileapi/file_system_file_util.h"
21 #include "webkit/browser/fileapi/file_system_url.h"
22 #include "webkit/browser/fileapi/sandbox_directory_database.h"
23 #include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h"
24 #include "webkit/browser/webkit_storage_browser_export.h"
25 #include "webkit/common/blob/shareable_file_reference.h"
26 #include "webkit/common/fileapi/file_system_types.h"
27
28 namespace base {
29 class SequencedTaskRunner;
30 class TimeTicks;
31 }
32
33 namespace content {
34 class ObfuscatedFileUtilTest;
35 }
36
37 namespace quota {
38 class SpecialStoragePolicy;
39 }
40
41 class GURL;
42
43 namespace fileapi {
44
45 class FileSystemOperationContext;
46 class SandboxOriginDatabaseInterface;
47 class TimedTaskHelper;
48
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.
56 //
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.
63 //
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.
69 //
70 // This class must be deleted on the FILE thread, because that's where
71 // DropDatabases needs to be called.
72 class WEBKIT_STORAGE_BROWSER_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() {}
80
81     // Returns the next origin.  Returns empty if there are no more origins.
82     virtual GURL Next() = 0;
83
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;
87   };
88
89   typedef base::Callback<std::string(const FileSystemURL&)>
90       GetTypeStringForURLCallback;
91
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.
96   //
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.
103   //
104   ObfuscatedFileUtil(
105       quota::SpecialStoragePolicy* special_storage_policy,
106       const base::FilePath& file_system_directory,
107       leveldb::Env* env_override,
108       base::SequencedTaskRunner* file_task_runner,
109       const GetTypeStringForURLCallback& get_type_string_for_url,
110       const std::set<std::string>& known_type_strings,
111       SandboxFileSystemBackendDelegate* sandbox_delegate);
112   virtual ~ObfuscatedFileUtil();
113
114   // FileSystemFileUtil overrides.
115   virtual base::File::Error CreateOrOpen(
116       FileSystemOperationContext* context,
117       const FileSystemURL& url,
118       int file_flags,
119       base::PlatformFile* file_handle,
120       bool* created) OVERRIDE;
121   virtual base::File::Error Close(
122       FileSystemOperationContext* context,
123       base::PlatformFile file) OVERRIDE;
124   virtual base::File::Error EnsureFileExists(
125       FileSystemOperationContext* context,
126       const FileSystemURL& url, bool* created) OVERRIDE;
127   virtual base::File::Error CreateDirectory(
128       FileSystemOperationContext* context,
129       const FileSystemURL& url,
130       bool exclusive,
131       bool recursive) OVERRIDE;
132   virtual base::File::Error GetFileInfo(
133       FileSystemOperationContext* context,
134       const FileSystemURL& url,
135       base::File::Info* file_info,
136       base::FilePath* platform_file) OVERRIDE;
137   virtual scoped_ptr<AbstractFileEnumerator> CreateFileEnumerator(
138       FileSystemOperationContext* context,
139       const FileSystemURL& root_url) OVERRIDE;
140   virtual base::File::Error GetLocalFilePath(
141       FileSystemOperationContext* context,
142       const FileSystemURL& file_system_url,
143       base::FilePath* local_path) OVERRIDE;
144   virtual base::File::Error Touch(
145       FileSystemOperationContext* context,
146       const FileSystemURL& url,
147       const base::Time& last_access_time,
148       const base::Time& last_modified_time) OVERRIDE;
149   virtual base::File::Error Truncate(
150       FileSystemOperationContext* context,
151       const FileSystemURL& url,
152       int64 length) OVERRIDE;
153   virtual base::File::Error CopyOrMoveFile(
154       FileSystemOperationContext* context,
155       const FileSystemURL& src_url,
156       const FileSystemURL& dest_url,
157       CopyOrMoveOption option,
158       bool copy) OVERRIDE;
159   virtual base::File::Error CopyInForeignFile(
160         FileSystemOperationContext* context,
161         const base::FilePath& src_file_path,
162         const FileSystemURL& dest_url) OVERRIDE;
163   virtual base::File::Error DeleteFile(
164       FileSystemOperationContext* context,
165       const FileSystemURL& url) OVERRIDE;
166   virtual base::File::Error DeleteDirectory(
167       FileSystemOperationContext* context,
168       const FileSystemURL& url) OVERRIDE;
169   virtual webkit_blob::ScopedFile CreateSnapshotFile(
170       FileSystemOperationContext* context,
171       const FileSystemURL& url,
172       base::File::Error* error,
173       base::File::Info* file_info,
174       base::FilePath* platform_path) OVERRIDE;
175
176   // Same as the other CreateFileEnumerator, but with recursive support.
177   scoped_ptr<AbstractFileEnumerator> CreateFileEnumerator(
178       FileSystemOperationContext* context,
179       const FileSystemURL& root_url,
180       bool recursive);
181
182   // Returns true if the directory |url| is empty.
183   bool IsDirectoryEmpty(
184       FileSystemOperationContext* context,
185       const FileSystemURL& url);
186
187   // Gets the topmost directory specific to this origin and type.  This will
188   // contain both the directory database's files and all the backing file
189   // subdirectories.
190   // Returns the topmost origin directory if |type_string| is empty.
191   // Returns an empty path if the directory is undefined.
192   // If the directory is defined, it will be returned, even if
193   // there is a file system error (e.g. the directory doesn't exist on disk and
194   // |create| is false). Callers should always check |error_code| to make sure
195   // the returned path is usable.
196   base::FilePath GetDirectoryForOriginAndType(
197       const GURL& origin,
198       const std::string& type_string,
199       bool create,
200       base::File::Error* error_code);
201
202   // Deletes the topmost directory specific to this origin and type.  This will
203   // delete its directory database.
204   // Deletes the topmost origin directory if |type_string| is empty.
205   bool DeleteDirectoryForOriginAndType(
206       const GURL& origin,
207       const std::string& type_string);
208
209   // This method and all methods of its returned class must be called only on
210   // the FILE thread.  The caller is responsible for deleting the returned
211   // object.
212   AbstractOriginEnumerator* CreateOriginEnumerator();
213
214   // Deletes a directory database from the database list in the ObfuscatedFSFU
215   // and destroys the database on the disk.
216   bool DestroyDirectoryDatabase(const GURL& origin,
217                                 const std::string& type_string);
218
219   // Computes a cost for storing a given file in the obfuscated FSFU.
220   // As the cost of a file is independent of the cost of its parent directories,
221   // this ignores all but the BaseName of the supplied path.  In order to
222   // compute the cost of adding a multi-segment directory recursively, call this
223   // on each path segment and add the results.
224   static int64 ComputeFilePathCost(const base::FilePath& path);
225
226   // Tries to prepopulate directory database for the given type strings.
227   // This tries from the first one in the given type_strings and stops
228   // once it succeeds to do so for one database (i.e. it prepopulates
229   // at most one database).
230   void MaybePrepopulateDatabase(
231       const std::vector<std::string>& type_strings_to_prepopulate);
232
233  private:
234   typedef SandboxDirectoryDatabase::FileId FileId;
235   typedef SandboxDirectoryDatabase::FileInfo FileInfo;
236
237   friend class ObfuscatedFileEnumerator;
238   friend class QuotaBackendImplTest;
239   friend class content::ObfuscatedFileUtilTest;
240
241   // Helper method to create an obfuscated file util for regular
242   // (temporary, persistent) file systems. Used only for testing.
243   // Note: this is implemented in sandbox_file_system_backend_delegate.cc.
244   static ObfuscatedFileUtil* CreateForTesting(
245       quota::SpecialStoragePolicy* special_storage_policy,
246       const base::FilePath& file_system_directory,
247       leveldb::Env* env_override,
248       base::SequencedTaskRunner* file_task_runner);
249
250   base::FilePath GetDirectoryForURL(
251       const FileSystemURL& url,
252       bool create,
253       base::File::Error* error_code);
254
255   // This just calls get_type_string_for_url_ callback that is given in ctor.
256   std::string CallGetTypeStringForURL(const FileSystemURL& url);
257
258   base::File::Error GetFileInfoInternal(
259       SandboxDirectoryDatabase* db,
260       FileSystemOperationContext* context,
261       const FileSystemURL& url,
262       FileId file_id,
263       FileInfo* local_info,
264       base::File::Info* file_info,
265       base::FilePath* platform_file_path);
266
267   // Creates a new file, both the underlying backing file and the entry in the
268   // database.  |dest_file_info| is an in-out parameter.  Supply the name and
269   // parent_id; data_path is ignored.  On success, data_path will
270   // always be set to the relative path [from the root of the type-specific
271   // filesystem directory] of a NEW backing file, and handle, if supplied, will
272   // hold open PlatformFile for the backing file, which the caller is
273   // responsible for closing.  If you supply a path in |source_path|, it will be
274   // used as a source from which to COPY data.
275   // Caveat: do not supply handle if you're also supplying a data path.  It was
276   // easier not to support this, and no code has needed it so far, so it will
277   // DCHECK and handle will hold base::kInvalidPlatformFileValue.
278   base::File::Error CreateFile(
279       FileSystemOperationContext* context,
280       const base::FilePath& source_file_path,
281       const FileSystemURL& dest_url,
282       FileInfo* dest_file_info,
283       int file_flags,
284       base::PlatformFile* handle);
285
286   // This converts from a relative path [as is stored in the FileInfo.data_path
287   // field] to an absolute platform path that can be given to the native
288   // filesystem.
289   base::FilePath DataPathToLocalPath(
290       const FileSystemURL& url,
291       const base::FilePath& data_file_path);
292
293   std::string GetDirectoryDatabaseKey(const GURL& origin,
294                                       const std::string& type_string);
295
296   // This returns NULL if |create| flag is false and a filesystem does not
297   // exist for the given |url|.
298   // For read operations |create| should be false.
299   SandboxDirectoryDatabase* GetDirectoryDatabase(const FileSystemURL& url,
300                                                  bool create);
301
302   // Gets the topmost directory specific to this origin.  This will
303   // contain both the filesystem type subdirectories.
304   base::FilePath GetDirectoryForOrigin(const GURL& origin,
305                                        bool create,
306                                        base::File::Error* error_code);
307
308   void InvalidateUsageCache(FileSystemOperationContext* context,
309                             const GURL& origin,
310                             FileSystemType type);
311
312   void MarkUsed();
313   void DropDatabases();
314
315   // Initializes the origin database. |origin_hint| may be used as a hint
316   // for initializing database if it's not empty.
317   bool InitOriginDatabase(const GURL& origin_hint, bool create);
318
319   base::File::Error GenerateNewLocalPath(
320       SandboxDirectoryDatabase* db,
321       FileSystemOperationContext* context,
322       const FileSystemURL& url,
323       base::FilePath* local_path);
324
325   base::File::Error CreateOrOpenInternal(
326       FileSystemOperationContext* context,
327       const FileSystemURL& url,
328       int file_flags,
329       base::PlatformFile* file_handle,
330       bool* created);
331
332   bool HasIsolatedStorage(const GURL& origin);
333
334   typedef std::map<std::string, SandboxDirectoryDatabase*> DirectoryMap;
335   DirectoryMap directories_;
336   scoped_ptr<SandboxOriginDatabaseInterface> origin_database_;
337   scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_;
338   base::FilePath file_system_directory_;
339   leveldb::Env* env_override_;
340
341   // Used to delete database after a certain period of inactivity.
342   int64 db_flush_delay_seconds_;
343
344   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
345   scoped_ptr<TimedTaskHelper> timer_;
346
347   GetTypeStringForURLCallback get_type_string_for_url_;
348   std::set<std::string> known_type_strings_;
349
350   // Not owned.
351   SandboxFileSystemBackendDelegate* sandbox_delegate_;
352
353   DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtil);
354 };
355
356 }  // namespace fileapi
357
358 #endif  // WEBKIT_BROWSER_FILEAPI_OBFUSCATED_FILE_UTIL_H_