Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / storage / 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 STORAGE_BROWSER_FILEAPI_OBFUSCATED_FILE_UTIL_H_
6 #define STORAGE_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 "storage/browser/fileapi/file_system_file_util.h"
20 #include "storage/browser/fileapi/file_system_url.h"
21 #include "storage/browser/fileapi/sandbox_directory_database.h"
22 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
23 #include "storage/browser/storage_browser_export.h"
24 #include "storage/common/blob/shareable_file_reference.h"
25 #include "storage/common/fileapi/file_system_types.h"
26
27 namespace base {
28 class SequencedTaskRunner;
29 class TimeTicks;
30 }
31
32 namespace content {
33 class ObfuscatedFileUtilTest;
34 class QuotaBackendImplTest;
35 }
36
37 namespace storage {
38 class SpecialStoragePolicy;
39 }
40
41 class GURL;
42
43 namespace storage {
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 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() {}
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(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;
112
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;
159
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);
165
166   // Returns true if the directory |url| is empty.
167   bool IsDirectoryEmpty(
168       FileSystemOperationContext* context,
169       const FileSystemURL& url);
170
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);
185
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);
192
193   // This method and all methods of its returned class must be called only on
194   // the FILE thread.  The caller is responsible for deleting the returned
195   // object.
196   AbstractOriginEnumerator* CreateOriginEnumerator();
197
198   // Deletes a directory database from the database list in the ObfuscatedFSFU
199   // and destroys the database on the disk.
200   void DestroyDirectoryDatabase(const GURL& origin,
201                                 const std::string& type_string);
202
203   // Computes a cost for storing a given file in the obfuscated FSFU.
204   // As the cost of a file is independent of the cost of its parent directories,
205   // this ignores all but the BaseName of the supplied path.  In order to
206   // compute the cost of adding a multi-segment directory recursively, call this
207   // on each path segment and add the results.
208   static int64 ComputeFilePathCost(const base::FilePath& path);
209
210   // Tries to prepopulate directory database for the given type strings.
211   // This tries from the first one in the given type_strings and stops
212   // once it succeeds to do so for one database (i.e. it prepopulates
213   // at most one database).
214   void MaybePrepopulateDatabase(
215       const std::vector<std::string>& type_strings_to_prepopulate);
216
217  private:
218   typedef SandboxDirectoryDatabase::FileId FileId;
219   typedef SandboxDirectoryDatabase::FileInfo FileInfo;
220
221   friend class ObfuscatedFileEnumerator;
222   friend class content::ObfuscatedFileUtilTest;
223   friend class content::QuotaBackendImplTest;
224
225   // Helper method to create an obfuscated file util for regular
226   // (temporary, persistent) file systems. Used only for testing.
227   // Note: this is implemented in sandbox_file_system_backend_delegate.cc.
228   static ObfuscatedFileUtil* CreateForTesting(
229       storage::SpecialStoragePolicy* special_storage_policy,
230       const base::FilePath& file_system_directory,
231       leveldb::Env* env_override,
232       base::SequencedTaskRunner* file_task_runner);
233
234   base::FilePath GetDirectoryForURL(
235       const FileSystemURL& url,
236       bool create,
237       base::File::Error* error_code);
238
239   // This just calls get_type_string_for_url_ callback that is given in ctor.
240   std::string CallGetTypeStringForURL(const FileSystemURL& url);
241
242   base::File::Error GetFileInfoInternal(
243       SandboxDirectoryDatabase* db,
244       FileSystemOperationContext* context,
245       const FileSystemURL& url,
246       FileId file_id,
247       FileInfo* local_info,
248       base::File::Info* file_info,
249       base::FilePath* platform_file_path);
250
251   // Creates a new file, both the underlying backing file and the entry in the
252   // database.  |dest_file_info| is an in-out parameter.  Supply the name and
253   // parent_id; data_path is ignored.  On success, data_path will
254   // always be set to the relative path [from the root of the type-specific
255   // filesystem directory] of a NEW backing file.  Returns the new file.
256   base::File CreateAndOpenFile(
257       FileSystemOperationContext* context,
258       const FileSystemURL& dest_url,
259       FileInfo* dest_file_info,
260       int file_flags);
261
262   // The same as CreateAndOpenFile except that a file is not returned and if a
263   // path is provided in |source_path|, it will be used as a source from which
264   // to COPY data.
265   base::File::Error CreateFile(
266       FileSystemOperationContext* context,
267       const base::FilePath& source_file_path,
268       const FileSystemURL& dest_url,
269       FileInfo* dest_file_info);
270
271   // Updates |db| and |dest_file_info| at the end of creating a new file.
272   base::File::Error CommitCreateFile(
273     const base::FilePath& root,
274     const base::FilePath& local_path,
275     SandboxDirectoryDatabase* db,
276     FileInfo* dest_file_info);
277
278   // This converts from a relative path [as is stored in the FileInfo.data_path
279   // field] to an absolute platform path that can be given to the native
280   // filesystem.
281   base::FilePath DataPathToLocalPath(
282       const FileSystemURL& url,
283       const base::FilePath& data_file_path);
284
285   std::string GetDirectoryDatabaseKey(const GURL& origin,
286                                       const std::string& type_string);
287
288   // This returns NULL if |create| flag is false and a filesystem does not
289   // exist for the given |url|.
290   // For read operations |create| should be false.
291   SandboxDirectoryDatabase* GetDirectoryDatabase(const FileSystemURL& url,
292                                                  bool create);
293
294   // Gets the topmost directory specific to this origin.  This will
295   // contain both the filesystem type subdirectories.
296   base::FilePath GetDirectoryForOrigin(const GURL& origin,
297                                        bool create,
298                                        base::File::Error* error_code);
299
300   void InvalidateUsageCache(FileSystemOperationContext* context,
301                             const GURL& origin,
302                             FileSystemType type);
303
304   void MarkUsed();
305   void DropDatabases();
306
307   // Initializes the origin database. |origin_hint| may be used as a hint
308   // for initializing database if it's not empty.
309   bool InitOriginDatabase(const GURL& origin_hint, bool create);
310
311   base::File::Error GenerateNewLocalPath(
312       SandboxDirectoryDatabase* db,
313       FileSystemOperationContext* context,
314       const FileSystemURL& url,
315       base::FilePath* root,
316       base::FilePath* local_path);
317
318   base::File CreateOrOpenInternal(
319       FileSystemOperationContext* context,
320       const FileSystemURL& url,
321       int file_flags);
322
323   bool HasIsolatedStorage(const GURL& origin);
324
325   typedef std::map<std::string, SandboxDirectoryDatabase*> DirectoryMap;
326   DirectoryMap directories_;
327   scoped_ptr<SandboxOriginDatabaseInterface> origin_database_;
328   scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy_;
329   base::FilePath file_system_directory_;
330   leveldb::Env* env_override_;
331
332   // Used to delete database after a certain period of inactivity.
333   int64 db_flush_delay_seconds_;
334
335   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
336   scoped_ptr<TimedTaskHelper> timer_;
337
338   GetTypeStringForURLCallback get_type_string_for_url_;
339   std::set<std::string> known_type_strings_;
340
341   // Not owned.
342   SandboxFileSystemBackendDelegate* sandbox_delegate_;
343
344   DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtil);
345 };
346
347 }  // namespace storage
348
349 #endif  // STORAGE_BROWSER_FILEAPI_OBFUSCATED_FILE_UTIL_H_