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