- add sources.
[platform/framework/web/crosswalk.git] / src / webkit / browser / fileapi / sandbox_directory_database.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_SANDBOX_DIRECTORY_DATABASE_H_
6 #define WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/platform_file.h"
14 #include "base/time/time.h"
15 #include "webkit/browser/webkit_storage_browser_export.h"
16
17 namespace tracked_objects {
18 class Location;
19 }
20
21 namespace leveldb {
22 class DB;
23 class Status;
24 class WriteBatch;
25 }
26
27 namespace fileapi {
28
29 // This class WILL NOT protect you against producing directory loops, giving an
30 // empty directory a backing data file, giving two files the same backing file,
31 // or pointing to a nonexistent backing file.  It does no file IO other than
32 // that involved with talking to its underlying database.  It does not create or
33 // in any way touch real files; it only creates path entries in its database.
34
35 // TODO(ericu): Safe mode, which does more checks such as the above on debug
36 // builds.
37 // TODO(ericu): Add a method that will give a unique filename for a data file.
38 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE SandboxDirectoryDatabase {
39  public:
40   typedef int64 FileId;
41
42   struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileInfo {
43     FileInfo();
44     ~FileInfo();
45
46     bool is_directory() const {
47       return data_path.empty();
48     }
49
50     FileId parent_id;
51     base::FilePath data_path;
52     base::FilePath::StringType name;
53     // This modification time is valid only for directories, not files, as
54     // FileWriter will get the files out of sync.
55     // For files, look at the modification time of the underlying data_path.
56     base::Time modification_time;
57   };
58
59   explicit SandboxDirectoryDatabase(
60       const base::FilePath& filesystem_data_directory);
61   ~SandboxDirectoryDatabase();
62
63   bool GetChildWithName(
64       FileId parent_id,
65       const base::FilePath::StringType& name,
66       FileId* child_id);
67   bool GetFileWithPath(const base::FilePath& path, FileId* file_id);
68   // ListChildren will succeed, returning 0 children, if parent_id doesn't
69   // exist.
70   bool ListChildren(FileId parent_id, std::vector<FileId>* children);
71   bool GetFileInfo(FileId file_id, FileInfo* info);
72   base::PlatformFileError AddFileInfo(const FileInfo& info, FileId* file_id);
73   bool RemoveFileInfo(FileId file_id);
74   // This does a full update of the FileInfo, and is what you'd use for moves
75   // and renames.  If you just want to update the modification_time, use
76   // UpdateModificationTime.
77   bool UpdateFileInfo(FileId file_id, const FileInfo& info);
78   bool UpdateModificationTime(
79       FileId file_id, const base::Time& modification_time);
80   // This is used for an overwriting move of a file [not a directory] on top of
81   // another file [also not a directory]; we need to alter two files' info in a
82   // single transaction to avoid weird backing file references in the event of a
83   // partial failure.
84   bool OverwritingMoveFile(FileId src_file_id, FileId dest_file_id);
85
86   // This produces the series 0, 1, 2..., starting at 0 when the underlying
87   // filesystem is first created, and maintaining state across
88   // creation/destruction of SandboxDirectoryDatabase objects.
89   bool GetNextInteger(int64* next);
90
91   // Returns true if the database looks consistent with local filesystem.
92   bool IsFileSystemConsistent();
93
94   static bool DestroyDatabase(const base::FilePath& path);
95
96  private:
97   enum RecoveryOption {
98     DELETE_ON_CORRUPTION,
99     REPAIR_ON_CORRUPTION,
100     FAIL_ON_CORRUPTION,
101   };
102
103   friend class ObfuscatedFileUtil;
104   friend class SandboxDirectoryDatabaseTest;
105
106   bool Init(RecoveryOption recovery_option);
107   bool RepairDatabase(const std::string& db_path);
108   void ReportInitStatus(const leveldb::Status& status);
109   bool StoreDefaultValues();
110   bool GetLastFileId(FileId* file_id);
111   bool VerifyIsDirectory(FileId file_id);
112   bool AddFileInfoHelper(
113       const FileInfo& info, FileId file_id, leveldb::WriteBatch* batch);
114   bool RemoveFileInfoHelper(FileId file_id, leveldb::WriteBatch* batch);
115   void HandleError(const tracked_objects::Location& from_here,
116                    const leveldb::Status& status);
117
118   const base::FilePath filesystem_data_directory_;
119   scoped_ptr<leveldb::DB> db_;
120   base::Time last_reported_time_;
121   DISALLOW_COPY_AND_ASSIGN(SandboxDirectoryDatabase);
122 };
123
124 }  // namespace fileapi
125
126 #endif  // WEBKIT_BROWSER_FILEAPI_SANDBOX_DIRECTORY_DATABASE_H_