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