Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend / sync_engine_initializer.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_SYNC_ENGINE_INITIALIZER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_INITIALIZER_H_
7
8 #include <string>
9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/sequenced_task_runner.h"
15 #include "chrome/browser/sync_file_system/drive_backend/sync_task.h"
16 #include "chrome/browser/sync_file_system/sync_callbacks.h"
17 #include "google_apis/drive/drive_common_callbacks.h"
18 #include "google_apis/drive/gdata_errorcode.h"
19
20 namespace drive {
21 class DriveServiceInterface;
22 }
23
24 namespace google_apis {
25 class AboutResource;
26 class ResourceEntry;
27 class ResourceList;
28 }
29
30 namespace leveldb {
31 class Env;
32 }
33
34 namespace sync_file_system {
35 namespace drive_backend {
36
37 class MetadataDatabase;
38 class SyncEngineContext;
39
40 // This class performs initializion sequence of SyncEngine.
41 //
42 // After initialize sequence completed, the Database must have
43 //  - Largest change ID,
44 //  - Sync-root folder and its tracker,
45 //  - All children of sync-root folder that have inactive and non-dirty
46 //    trackers.
47 //
48 // The initialization sequence is:
49 //  - Open database and load its contents,
50 //  - If the database is already populated, complete the sequence.
51 //  - Get AboutResource to get the largest change ID and the Drive root folder
52 //    ID.
53 //  - Find the remote sync-root folder, whose title is
54 //    "Chrome Syncable FileSystem" and has no parent.
55 //    Note that if the initialization is interrupted by the browser restart or
56 //    an error, the sequence leaves the folder in the Drive root folder.  So, if
57 //    we find the folder in the Drive root folder, handle it as the sync-root
58 //    folder.
59 //  - Create the remote sync-root folder if we don't have.
60 //  - Detach the remote sync-root folder from its parent if it has.
61 //  - Fetch the folder contents of the remote sync-root folder.
62 //    The contents are likely registered as app-root folders, but handle them
63 //    as regular inactive folders until they are registered explicitly.
64 //  - Populate database with the largest change ID, the sync-root folder and
65 //    its contents.
66 //
67 class SyncEngineInitializer : public SyncTask {
68  public:
69   SyncEngineInitializer(SyncEngineContext* sync_context,
70                         base::SequencedTaskRunner* task_runner,
71                         drive::DriveServiceInterface* drive_service,
72                         const base::FilePath& database_path,
73                         leveldb::Env* env_override);
74   virtual ~SyncEngineInitializer();
75   virtual void Run(scoped_ptr<SyncTaskToken> token) OVERRIDE;
76
77   scoped_ptr<MetadataDatabase> PassMetadataDatabase();
78
79  private:
80   typedef base::Callback<void(const SyncStatusCallback& callback)> Task;
81
82   void DidCreateMetadataDatabase(scoped_ptr<SyncTaskToken> token,
83                                  SyncStatusCode status,
84                                  scoped_ptr<MetadataDatabase> instance);
85
86   void GetAboutResource(scoped_ptr<SyncTaskToken> token);
87   void DidGetAboutResource(
88       scoped_ptr<SyncTaskToken> token,
89       google_apis::GDataErrorCode error,
90       scoped_ptr<google_apis::AboutResource> about_resource);
91   void FindSyncRoot(scoped_ptr<SyncTaskToken> token);
92   void DidFindSyncRoot(scoped_ptr<SyncTaskToken> token,
93                        google_apis::GDataErrorCode error,
94                        scoped_ptr<google_apis::ResourceList> resource_list);
95   void CreateSyncRoot(scoped_ptr<SyncTaskToken> token);
96   void DidCreateSyncRoot(scoped_ptr<SyncTaskToken> token,
97                          google_apis::GDataErrorCode error,
98                          scoped_ptr<google_apis::ResourceEntry> entry);
99   void DetachSyncRoot(scoped_ptr<SyncTaskToken> token);
100   void DidDetachSyncRoot(scoped_ptr<SyncTaskToken> token,
101                          google_apis::GDataErrorCode error);
102   void ListAppRootFolders(scoped_ptr<SyncTaskToken> token);
103   void DidListAppRootFolders(
104       scoped_ptr<SyncTaskToken> token,
105       google_apis::GDataErrorCode error,
106       scoped_ptr<google_apis::ResourceList> resource_list);
107   void PopulateDatabase(scoped_ptr<SyncTaskToken> token);
108   void DidPopulateDatabase(scoped_ptr<SyncTaskToken> token,
109                            SyncStatusCode status);
110
111   SyncEngineContext* sync_context_;  // Not owned.
112   leveldb::Env* env_override_;
113
114   scoped_refptr<base::SequencedTaskRunner> task_runner_;
115   drive::DriveServiceInterface* drive_service_;
116   google_apis::CancelCallback cancel_callback_;
117   base::FilePath database_path_;
118
119   int find_sync_root_retry_count_;
120
121   scoped_ptr<MetadataDatabase> metadata_database_;
122   ScopedVector<google_apis::ResourceEntry> app_root_folders_;
123
124   int64 largest_change_id_;
125   std::string root_folder_id_;
126
127   scoped_ptr<google_apis::ResourceEntry> sync_root_folder_;
128
129   base::WeakPtrFactory<SyncEngineInitializer> weak_ptr_factory_;
130
131   DISALLOW_COPY_AND_ASSIGN(SyncEngineInitializer);
132 };
133
134 }  // namespace drive_backend
135 }  // namespace sync_file_system
136
137 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_INITIALIZER_H_