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