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_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list.h"
11 #include "chrome/browser/drive/drive_notification_observer.h"
12 #include "chrome/browser/drive/drive_service_interface.h"
13 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
14 #include "chrome/browser/sync_file_system/local_change_processor.h"
15 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
16 #include "chrome/browser/sync_file_system/sync_task_manager.h"
17 #include "net/base/network_change_notifier.h"
19 class ExtensionServiceInterface
;
20 class ProfileOAuth2TokenService
;
23 class SequencedTaskRunner
;
27 class DriveServiceInterface
;
28 class DriveNotificationManager
;
31 namespace sync_file_system
{
32 namespace drive_backend
{
34 class LocalToRemoteSyncer
;
35 class MetadataDatabase
;
36 class RemoteToLocalSyncer
;
37 class SyncEngineInitializer
;
39 class SyncEngine
: public RemoteFileSyncService
,
40 public LocalChangeProcessor
,
41 public SyncTaskManager::Client
,
42 public drive::DriveNotificationObserver
,
43 public drive::DriveServiceObserver
,
44 public net::NetworkChangeNotifier::NetworkChangeObserver
,
45 public SyncEngineContext
{
47 typedef Observer SyncServiceObserver
;
49 static scoped_ptr
<SyncEngine
> CreateForBrowserContext(
50 content::BrowserContext
* context
);
51 static void AppendDependsOnFactories(
52 std::set
<BrowserContextKeyedServiceFactory
*>* factories
);
54 virtual ~SyncEngine();
58 // RemoteFileSyncService overrides.
59 virtual void AddServiceObserver(SyncServiceObserver
* observer
) OVERRIDE
;
60 virtual void AddFileStatusObserver(FileStatusObserver
* observer
) OVERRIDE
;
61 virtual void RegisterOrigin(
63 const SyncStatusCallback
& callback
) OVERRIDE
;
64 virtual void EnableOrigin(
66 const SyncStatusCallback
& callback
) OVERRIDE
;
67 virtual void DisableOrigin(
69 const SyncStatusCallback
& callback
) OVERRIDE
;
70 virtual void UninstallOrigin(
73 const SyncStatusCallback
& callback
) OVERRIDE
;
74 virtual void ProcessRemoteChange(const SyncFileCallback
& callback
) OVERRIDE
;
75 virtual void SetRemoteChangeProcessor(
76 RemoteChangeProcessor
* processor
) OVERRIDE
;
77 virtual LocalChangeProcessor
* GetLocalChangeProcessor() OVERRIDE
;
78 virtual bool IsConflicting(const fileapi::FileSystemURL
& url
) OVERRIDE
;
79 virtual RemoteServiceState
GetCurrentState() const OVERRIDE
;
80 virtual void GetOriginStatusMap(OriginStatusMap
* status_map
) OVERRIDE
;
81 virtual scoped_ptr
<base::ListValue
> DumpFiles(const GURL
& origin
) OVERRIDE
;
82 virtual scoped_ptr
<base::ListValue
> DumpDatabase() OVERRIDE
;
83 virtual void SetSyncEnabled(bool enabled
) OVERRIDE
;
84 virtual SyncStatusCode
SetConflictResolutionPolicy(
85 ConflictResolutionPolicy policy
) OVERRIDE
;
86 virtual ConflictResolutionPolicy
GetConflictResolutionPolicy() const OVERRIDE
;
87 virtual void GetRemoteVersions(
88 const fileapi::FileSystemURL
& url
,
89 const RemoteVersionsCallback
& callback
) OVERRIDE
;
90 virtual void DownloadRemoteVersion(
91 const fileapi::FileSystemURL
& url
,
92 const std::string
& version_id
,
93 const DownloadVersionCallback
& callback
) OVERRIDE
;
94 virtual void PromoteDemotedChanges() OVERRIDE
;
96 // LocalChangeProcessor overrides.
97 virtual void ApplyLocalChange(
98 const FileChange
& local_change
,
99 const base::FilePath
& local_path
,
100 const SyncFileMetadata
& local_metadata
,
101 const fileapi::FileSystemURL
& url
,
102 const SyncStatusCallback
& callback
) OVERRIDE
;
104 // SyncTaskManager::Client overrides.
105 virtual void MaybeScheduleNextTask() OVERRIDE
;
106 virtual void NotifyLastOperationStatus(SyncStatusCode sync_status
,
107 bool used_network
) OVERRIDE
;
109 // drive::DriveNotificationObserver overrides.
110 virtual void OnNotificationReceived() OVERRIDE
;
111 virtual void OnPushNotificationEnabled(bool enabled
) OVERRIDE
;
113 // drive::DriveServiceObserver overrides.
114 virtual void OnReadyToSendRequests() OVERRIDE
;
115 virtual void OnRefreshTokenInvalid() OVERRIDE
;
117 // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
118 virtual void OnNetworkChanged(
119 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
;
121 // SyncEngineContext overrides.
122 virtual drive::DriveServiceInterface
* GetDriveService() OVERRIDE
;
123 virtual drive::DriveUploaderInterface
* GetDriveUploader() OVERRIDE
;
124 virtual MetadataDatabase
* GetMetadataDatabase() OVERRIDE
;
125 virtual RemoteChangeProcessor
* GetRemoteChangeProcessor() OVERRIDE
;
126 virtual base::SequencedTaskRunner
* GetBlockingTaskRunner() OVERRIDE
;
129 friend class DriveBackendSyncTest
;
130 friend class SyncEngineTest
;
132 SyncEngine(const base::FilePath
& base_dir
,
133 base::SequencedTaskRunner
* task_runner
,
134 scoped_ptr
<drive::DriveServiceInterface
> drive_service
,
135 scoped_ptr
<drive::DriveUploaderInterface
> drive_uploader
,
136 drive::DriveNotificationManager
* notification_manager
,
137 ExtensionServiceInterface
* extension_service
,
138 ProfileOAuth2TokenService
* auth_token_service
);
140 void DoDisableApp(const std::string
& app_id
,
141 const SyncStatusCallback
& callback
);
142 void DoEnableApp(const std::string
& app_id
,
143 const SyncStatusCallback
& callback
);
145 void PostInitializeTask();
146 void DidInitialize(SyncEngineInitializer
* initializer
,
147 SyncStatusCode status
);
148 void DidProcessRemoteChange(RemoteToLocalSyncer
* syncer
,
149 const SyncFileCallback
& callback
,
150 SyncStatusCode status
);
151 void DidApplyLocalChange(LocalToRemoteSyncer
* syncer
,
152 const SyncStatusCallback
& callback
,
153 SyncStatusCode status
);
155 void MaybeStartFetchChanges();
156 void DidResolveConflict(SyncStatusCode status
);
157 void DidFetchChanges(SyncStatusCode status
);
159 void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state
,
161 void UpdateServiceState(RemoteServiceState state
,
162 const std::string
& description
);
163 void UpdateRegisteredApps();
165 base::FilePath base_dir_
;
166 base::FilePath temporary_file_dir_
;
168 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
170 scoped_ptr
<drive::DriveServiceInterface
> drive_service_
;
171 scoped_ptr
<drive::DriveUploaderInterface
> drive_uploader_
;
172 scoped_ptr
<MetadataDatabase
> metadata_database_
;
174 // These external services are not owned by SyncEngine.
175 // The owner of the SyncEngine is responsible for their lifetime.
176 // I.e. the owner should declare the dependency explicitly by calling
177 // BrowserContextKeyedService::DependsOn().
178 drive::DriveNotificationManager
* notification_manager_
;
179 ExtensionServiceInterface
* extension_service_
;
180 ProfileOAuth2TokenService
* auth_token_service_
;
182 ObserverList
<SyncServiceObserver
> service_observers_
;
183 ObserverList
<FileStatusObserver
> file_status_observers_
;
184 RemoteChangeProcessor
* remote_change_processor_
;
186 RemoteServiceState service_state_
;
188 bool should_check_conflict_
;
189 bool should_check_remote_change_
;
190 bool listing_remote_changes_
;
191 base::TimeTicks time_to_check_changes_
;
194 ConflictResolutionPolicy conflict_resolution_policy_
;
195 bool network_available_
;
197 scoped_ptr
<SyncTaskManager
> task_manager_
;
199 base::WeakPtrFactory
<SyncEngine
> weak_ptr_factory_
;
201 DISALLOW_COPY_AND_ASSIGN(SyncEngine
);
204 } // namespace drive_backend
205 } // namespace sync_file_system
207 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_