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