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