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_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "chrome/browser/drive/drive_notification_observer.h"
15 #include "chrome/browser/drive/drive_service_interface.h"
16 #include "chrome/browser/sync_file_system/drive_backend/callback_tracker.h"
17 #include "chrome/browser/sync_file_system/local_change_processor.h"
18 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
19 #include "chrome/browser/sync_file_system/sync_action.h"
20 #include "chrome/browser/sync_file_system/sync_direction.h"
21 #include "components/signin/core/browser/signin_manager_base.h"
22 #include "net/base/network_change_notifier.h"
24 class ExtensionServiceInterface
;
25 class OAuth2TokenService
;
28 class SequencedTaskRunner
;
32 class DriveServiceInterface
;
33 class DriveNotificationManager
;
34 class DriveUploaderInterface
;
42 class URLRequestContextGetter
;
45 namespace sync_file_system
{
47 class RemoteChangeProcessor
;
48 class SyncFileSystemTest
;
50 namespace drive_backend
{
52 class DriveServiceWrapper
;
53 class DriveUploaderWrapper
;
54 class MetadataDatabase
;
55 class RemoteChangeProcessorOnWorker
;
56 class RemoteChangeProcessorWrapper
;
57 class SyncTaskManager
;
58 class SyncWorkerInterface
;
60 class SyncEngine
: public RemoteFileSyncService
,
61 public LocalChangeProcessor
,
62 public drive::DriveNotificationObserver
,
63 public drive::DriveServiceObserver
,
64 public net::NetworkChangeNotifier::NetworkChangeObserver
,
65 public SigninManagerBase::Observer
{
67 typedef RemoteFileSyncService::Observer SyncServiceObserver
;
69 class DriveServiceFactory
{
71 DriveServiceFactory() {}
72 virtual ~DriveServiceFactory() {}
73 virtual scoped_ptr
<drive::DriveServiceInterface
> CreateDriveService(
74 OAuth2TokenService
* oauth2_token_service
,
75 net::URLRequestContextGetter
* url_request_context_getter
,
76 base::SequencedTaskRunner
* blocking_task_runner
);
79 DISALLOW_COPY_AND_ASSIGN(DriveServiceFactory
);
82 static scoped_ptr
<SyncEngine
> CreateForBrowserContext(
83 content::BrowserContext
* context
,
84 TaskLogger
* task_logger
);
85 static void AppendDependsOnFactories(
86 std::set
<BrowserContextKeyedServiceFactory
*>* factories
);
88 ~SyncEngine() override
;
91 // Can be called more than once.
94 void InitializeForTesting(
95 scoped_ptr
<drive::DriveServiceInterface
> drive_service
,
96 scoped_ptr
<drive::DriveUploaderInterface
> drive_uploader
,
97 scoped_ptr
<SyncWorkerInterface
> sync_worker
);
98 void InitializeInternal(
99 scoped_ptr
<drive::DriveServiceInterface
> drive_service
,
100 scoped_ptr
<drive::DriveUploaderInterface
> drive_uploader
,
101 scoped_ptr
<SyncWorkerInterface
> sync_worker
);
103 // RemoteFileSyncService overrides.
104 void AddServiceObserver(SyncServiceObserver
* observer
) override
;
105 void AddFileStatusObserver(FileStatusObserver
* observer
) override
;
106 void RegisterOrigin(const GURL
& origin
,
107 const SyncStatusCallback
& callback
) override
;
108 void EnableOrigin(const GURL
& origin
,
109 const SyncStatusCallback
& callback
) override
;
110 void DisableOrigin(const GURL
& origin
,
111 const SyncStatusCallback
& callback
) override
;
112 void UninstallOrigin(const GURL
& origin
,
114 const SyncStatusCallback
& callback
) override
;
115 void ProcessRemoteChange(const SyncFileCallback
& callback
) override
;
116 void SetRemoteChangeProcessor(RemoteChangeProcessor
* processor
) override
;
117 LocalChangeProcessor
* GetLocalChangeProcessor() override
;
118 RemoteServiceState
GetCurrentState() const override
;
119 void GetOriginStatusMap(const StatusMapCallback
& callback
) override
;
120 void DumpFiles(const GURL
& origin
, const ListCallback
& callback
) override
;
121 void DumpDatabase(const ListCallback
& callback
) override
;
122 void SetSyncEnabled(bool enabled
) override
;
123 void PromoteDemotedChanges(const base::Closure
& callback
) override
;
125 // LocalChangeProcessor overrides.
126 void ApplyLocalChange(const FileChange
& local_change
,
127 const base::FilePath
& local_path
,
128 const SyncFileMetadata
& local_metadata
,
129 const storage::FileSystemURL
& url
,
130 const SyncStatusCallback
& callback
) override
;
132 // drive::DriveNotificationObserver overrides.
133 void OnNotificationReceived() override
;
134 void OnPushNotificationEnabled(bool enabled
) override
;
136 // drive::DriveServiceObserver overrides.
137 void OnReadyToSendRequests() override
;
138 void OnRefreshTokenInvalid() override
;
140 // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
141 void OnNetworkChanged(
142 net::NetworkChangeNotifier::ConnectionType type
) override
;
144 // SigninManagerBase::Observer overrides.
145 void GoogleSigninFailed(const GoogleServiceAuthError
& error
) override
;
146 void GoogleSigninSucceeded(const std::string
& account_id
,
147 const std::string
& username
,
148 const std::string
& password
) override
;
149 void GoogleSignedOut(const std::string
& account_id
,
150 const std::string
& username
) override
;
153 class WorkerObserver
;
155 friend class DriveBackendSyncTest
;
156 friend class SyncEngineTest
;
157 friend class sync_file_system::SyncFileSystemTest
;
159 SyncEngine(const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
,
160 const scoped_refptr
<base::SequencedTaskRunner
>& worker_task_runner
,
161 const scoped_refptr
<base::SequencedTaskRunner
>& drive_task_runner
,
162 const scoped_refptr
<base::SequencedWorkerPool
>& worker_pool
,
163 const base::FilePath
& sync_file_system_dir
,
164 TaskLogger
* task_logger
,
165 drive::DriveNotificationManager
* notification_manager
,
166 ExtensionServiceInterface
* extension_service
,
167 SigninManagerBase
* signin_manager
,
168 OAuth2TokenService
* token_service
,
169 net::URLRequestContextGetter
* request_context
,
170 scoped_ptr
<DriveServiceFactory
> drive_service_factory
,
171 leveldb::Env
* env_override
);
173 // Called by WorkerObserver.
174 void OnPendingFileListUpdated(int item_count
);
175 void OnFileStatusChanged(const storage::FileSystemURL
& url
,
176 SyncFileType file_type
,
177 SyncFileStatus file_status
,
178 SyncAction sync_action
,
179 SyncDirection direction
);
180 void UpdateServiceState(RemoteServiceState state
,
181 const std::string
& description
);
183 SyncStatusCallback
TrackCallback(const SyncStatusCallback
& callback
);
185 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
186 scoped_refptr
<base::SequencedTaskRunner
> worker_task_runner_
;
187 scoped_refptr
<base::SequencedTaskRunner
> drive_task_runner_
;
188 scoped_refptr
<base::SequencedWorkerPool
> worker_pool_
;
190 const base::FilePath sync_file_system_dir_
;
191 TaskLogger
* task_logger_
;
193 // These external services are not owned by SyncEngine.
194 // The owner of the SyncEngine is responsible for their lifetime.
195 // I.e. the owner should declare the dependency explicitly by calling
196 // KeyedService::DependsOn().
197 drive::DriveNotificationManager
* notification_manager_
;
198 ExtensionServiceInterface
* extension_service_
;
199 SigninManagerBase
* signin_manager_
;
200 OAuth2TokenService
* token_service_
;
202 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
204 scoped_ptr
<DriveServiceFactory
> drive_service_factory_
;
206 scoped_ptr
<drive::DriveServiceInterface
> drive_service_
;
207 scoped_ptr
<DriveServiceWrapper
> drive_service_wrapper_
;
208 scoped_ptr
<drive::DriveUploaderInterface
> drive_uploader_
;
209 scoped_ptr
<DriveUploaderWrapper
> drive_uploader_wrapper_
;
211 RemoteChangeProcessor
* remote_change_processor_
; // Not owned.
212 scoped_ptr
<RemoteChangeProcessorWrapper
> remote_change_processor_wrapper_
;
213 // Delete this on worker.
214 scoped_ptr
<RemoteChangeProcessorOnWorker
> remote_change_processor_on_worker_
;
216 RemoteServiceState service_state_
;
217 bool has_refresh_token_
;
218 bool network_available_
;
221 // Delete them on worker.
222 scoped_ptr
<WorkerObserver
> worker_observer_
;
223 scoped_ptr
<SyncWorkerInterface
> sync_worker_
;
225 ObserverList
<SyncServiceObserver
> service_observers_
;
226 ObserverList
<FileStatusObserver
> file_status_observers_
;
227 leveldb::Env
* env_override_
;
229 CallbackTracker callback_tracker_
;
231 base::WeakPtrFactory
<SyncEngine
> weak_ptr_factory_
;
232 DISALLOW_COPY_AND_ASSIGN(SyncEngine
);
235 } // namespace drive_backend
236 } // namespace sync_file_system
238 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_