- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend_v1 / drive_metadata_store.h
1 // Copyright 2013 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 CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_DRIVE_METADATA_STORE_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_DRIVE_METADATA_STORE_H_
7
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "base/callback_forward.h"
14 #include "base/files/file_path.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "chrome/browser/sync_file_system/sync_callbacks.h"
20 #include "chrome/browser/sync_file_system/sync_status_code.h"
21 #include "webkit/browser/fileapi/file_system_url.h"
22
23 namespace base {
24 class ListValue;
25 class SequencedTaskRunner;
26 }
27
28 namespace leveldb {
29 class DB;
30 class WriteBatch;
31 }
32
33 class GURL;
34
35 namespace sync_file_system {
36
37 class DriveMetadata;
38 struct DBContents;
39 struct FileMetadata;
40
41 // This class holds a snapshot of the server side metadata.
42 class DriveMetadataStore
43     : public base::NonThreadSafe,
44       public base::SupportsWeakPtr<DriveMetadataStore> {
45  public:
46   typedef std::map<GURL, std::string> ResourceIdByOrigin;
47   typedef std::map<std::string, GURL> OriginByResourceId;
48   typedef std::map<base::FilePath, DriveMetadata> PathToMetadata;
49   typedef std::map<GURL, PathToMetadata> MetadataMap;
50   typedef std::vector<std::pair<fileapi::FileSystemURL, DriveMetadata> >
51       URLAndDriveMetadataList;
52   typedef base::Callback<void(SyncStatusCode status, bool created)>
53       InitializationCallback;
54
55   static const base::FilePath::CharType kDatabaseName[];
56
57   DriveMetadataStore(const base::FilePath& base_dir,
58                      base::SequencedTaskRunner* file_task_runner);
59   ~DriveMetadataStore();
60
61   // Initializes the internal database and loads its content to memory.
62   // This function works asynchronously.
63   void Initialize(const InitializationCallback& callback);
64
65   void SetLargestChangeStamp(int64 largest_changestamp,
66                              const SyncStatusCallback& callback);
67   int64 GetLargestChangeStamp() const;
68
69   // Updates database entry. Invokes |callback|, upon completion.
70   void UpdateEntry(const fileapi::FileSystemURL& url,
71                    const DriveMetadata& metadata,
72                    const SyncStatusCallback& callback);
73
74   // Deletes database entry for |url|. Invokes |callback|, upon completion.
75   void DeleteEntry(const fileapi::FileSystemURL& url,
76                    const SyncStatusCallback& callback);
77
78   // Lookups and reads the database entry for |url|.
79   SyncStatusCode ReadEntry(const fileapi::FileSystemURL& url,
80                            DriveMetadata* metadata) const;
81
82   // Marks |origin| as incremental sync and associates it with the directory
83   // identified by |resource_id|.
84   // |origin| must not already be an incremental sync origin.
85   void AddIncrementalSyncOrigin(const GURL& origin,
86                                 const std::string& resource_id);
87
88   // Returns true if |origin| is an incremental sync or disabled origin.
89   bool IsKnownOrigin(const GURL& origin) const;
90
91   // Returns true if |origin| is an incremental sync origin, i.e. the origin's
92   // entire file list has been cached and is ready to apply changes
93   // incrementally.
94   bool IsIncrementalSyncOrigin(const GURL& origin) const;
95
96   // Returns true if |origin| is a disabled origin.
97   bool IsOriginDisabled(const GURL& origin) const;
98
99   void EnableOrigin(const GURL& origin,
100                     const SyncStatusCallback& callback);
101
102   void DisableOrigin(const GURL& origin,
103                      const SyncStatusCallback& callback);
104
105   void RemoveOrigin(const GURL& origin,
106                     const SyncStatusCallback& callback);
107
108   // Sets the directory identified by |resource_id| as the sync data directory.
109   // All data for the Sync FileSystem should be store into the directory.
110   void SetSyncRootDirectory(const std::string& resource_id);
111   void SetOriginRootDirectory(const GURL& origin,
112                               const std::string& resource_id);
113
114   // Returns a set of URLs for files in conflict.
115   SyncStatusCode GetConflictURLs(
116       fileapi::FileSystemURLSet* urls) const;
117
118   // Returns a set of URLs and Resource IDs for files to be fetched.
119   SyncStatusCode GetToBeFetchedFiles(URLAndDriveMetadataList* list) const;
120
121   // Returns resource id for |origin|.
122   // This may return an empty string if |origin| is not a incremental or
123   // disabled origin.
124   std::string GetResourceIdForOrigin(const GURL& origin) const;
125
126   const std::string& sync_root_directory() const {
127     DCHECK(CalledOnValidThread());
128     return sync_root_directory_resource_id_;
129   }
130
131   const ResourceIdByOrigin& incremental_sync_origins() const {
132     DCHECK(CalledOnValidThread());
133     return incremental_sync_origins_;
134   }
135
136   const ResourceIdByOrigin& disabled_origins() const {
137     DCHECK(CalledOnValidThread());
138     return disabled_origins_;
139   }
140
141   // Returns all tracked origins. i.e. incremental_sync_origins_ and
142   // disabled_origins_.
143   void GetAllOrigins(std::vector<GURL>* origins);
144
145   // Maps |resource_id| to corresponding |origin|.
146   // Returns true if the directory indicated by |resource_id| is not an origin
147   // root directory.
148   bool GetOriginByOriginRootDirectoryId(const std::string& resource_id,
149                                         GURL* origin);
150
151   // Returns all file metadata for the given origin.
152   scoped_ptr<base::ListValue> DumpFiles(const GURL& origin);
153
154  private:
155   friend class DriveMetadataStoreTest;
156
157   void WriteToDB(scoped_ptr<leveldb::WriteBatch> batch,
158                  const SyncStatusCallback& callback);
159
160   void UpdateDBStatus(SyncStatusCode status);
161   void UpdateDBStatusAndInvokeCallback(const SyncStatusCallback& callback,
162                                        const leveldb::Status& status);
163   void DidInitialize(const InitializationCallback& callback,
164                      scoped_ptr<DBContents> contents);
165   void DidUpdateOrigin(const SyncStatusCallback& callback,
166                        SyncStatusCode status);
167
168   leveldb::DB* GetDBInstanceForTesting();
169
170   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
171   base::FilePath base_dir_;
172   scoped_ptr<leveldb::DB> db_;
173   SyncStatusCode db_status_;
174
175   int64 largest_changestamp_;
176   MetadataMap metadata_map_;
177
178   std::string sync_root_directory_resource_id_;
179   ResourceIdByOrigin incremental_sync_origins_;
180   ResourceIdByOrigin disabled_origins_;
181
182   OriginByResourceId origin_by_resource_id_;
183
184   DISALLOW_COPY_AND_ASSIGN(DriveMetadataStore);
185 };
186
187 }  // namespace sync_file_system
188
189 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_V1_DRIVE_METADATA_STORE_H_