sync_engine_initializer.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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 FileList;
26class FileResource;
27class ResourceEntry;
28}
29
30namespace leveldb {
31class Env;
32}
33
34namespace sync_file_system {
35namespace drive_backend {
36
37class MetadataDatabase;
38class 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//
67class SyncEngineInitializer : public SyncTask {
68 public:
69  SyncEngineInitializer(SyncEngineContext* sync_context,
70                        const base::FilePath& database_path,
71                        leveldb::Env* env_override);
72  virtual ~SyncEngineInitializer();
73  virtual void RunPreflight(scoped_ptr<SyncTaskToken> token) OVERRIDE;
74
75  scoped_ptr<MetadataDatabase> PassMetadataDatabase();
76
77 private:
78  typedef base::Callback<void(const SyncStatusCallback& callback)> Task;
79
80  void DidCreateMetadataDatabase(scoped_ptr<SyncTaskToken> token,
81                                 SyncStatusCode status,
82                                 scoped_ptr<MetadataDatabase> instance);
83
84  void GetAboutResource(scoped_ptr<SyncTaskToken> token);
85  void DidGetAboutResource(
86      scoped_ptr<SyncTaskToken> token,
87      google_apis::GDataErrorCode error,
88      scoped_ptr<google_apis::AboutResource> about_resource);
89  void FindSyncRoot(scoped_ptr<SyncTaskToken> token);
90  void DidFindSyncRoot(scoped_ptr<SyncTaskToken> token,
91                       google_apis::GDataErrorCode error,
92                       scoped_ptr<google_apis::FileList> file_list);
93  void CreateSyncRoot(scoped_ptr<SyncTaskToken> token);
94  void DidCreateSyncRoot(scoped_ptr<SyncTaskToken> token,
95                         google_apis::GDataErrorCode error,
96                         scoped_ptr<google_apis::FileResource> entry);
97  void DetachSyncRoot(scoped_ptr<SyncTaskToken> token);
98  void DidDetachSyncRoot(scoped_ptr<SyncTaskToken> token,
99                         google_apis::GDataErrorCode error);
100  void ListAppRootFolders(scoped_ptr<SyncTaskToken> token);
101  void DidListAppRootFolders(
102      scoped_ptr<SyncTaskToken> token,
103      google_apis::GDataErrorCode error,
104      scoped_ptr<google_apis::FileList> file_list);
105  void PopulateDatabase(scoped_ptr<SyncTaskToken> token);
106  void DidPopulateDatabase(scoped_ptr<SyncTaskToken> token,
107                           SyncStatusCode status);
108
109  SyncEngineContext* sync_context_;  // Not owned.
110  leveldb::Env* env_override_;
111
112  google_apis::CancelCallback cancel_callback_;
113  base::FilePath database_path_;
114
115  int find_sync_root_retry_count_;
116
117  scoped_ptr<MetadataDatabase> metadata_database_;
118  ScopedVector<google_apis::FileResource> app_root_folders_;
119
120  int64 largest_change_id_;
121  std::string root_folder_id_;
122
123  scoped_ptr<google_apis::FileResource> sync_root_folder_;
124
125  base::WeakPtrFactory<SyncEngineInitializer> weak_ptr_factory_;
126
127  DISALLOW_COPY_AND_ASSIGN(SyncEngineInitializer);
128};
129
130}  // namespace drive_backend
131}  // namespace sync_file_system
132
133#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_INITIALIZER_H_
134