sync_engine.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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_H_ 6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_ 7 8#include <set> 9#include <string> 10 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" 23 24class ExtensionServiceInterface; 25class ProfileOAuth2TokenService; 26 27namespace base { 28class SequencedTaskRunner; 29} 30 31namespace drive { 32class DriveServiceInterface; 33class DriveNotificationManager; 34class DriveUploaderInterface; 35} 36 37namespace leveldb { 38class Env; 39} 40 41namespace net { 42class URLRequestContextGetter; 43} 44 45namespace sync_file_system { 46 47class RemoteChangeProcessor; 48 49namespace drive_backend { 50 51class DriveServiceWrapper; 52class DriveUploaderWrapper; 53class MetadataDatabase; 54class RemoteChangeProcessorOnWorker; 55class RemoteChangeProcessorWrapper; 56class SyncTaskManager; 57class SyncWorkerInterface; 58 59class SyncEngine : public RemoteFileSyncService, 60 public LocalChangeProcessor, 61 public drive::DriveNotificationObserver, 62 public drive::DriveServiceObserver, 63 public net::NetworkChangeNotifier::NetworkChangeObserver, 64 public SigninManagerBase::Observer { 65 public: 66 typedef RemoteFileSyncService::Observer SyncServiceObserver; 67 68 static scoped_ptr<SyncEngine> CreateForBrowserContext( 69 content::BrowserContext* context, 70 TaskLogger* task_logger); 71 static void AppendDependsOnFactories( 72 std::set<BrowserContextKeyedServiceFactory*>* factories); 73 74 virtual ~SyncEngine(); 75 void Reset(); 76 77 // Can be called more than once. 78 void Initialize(); 79 80 void InitializeForTesting( 81 scoped_ptr<drive::DriveServiceInterface> drive_service, 82 scoped_ptr<drive::DriveUploaderInterface> drive_uploader, 83 scoped_ptr<SyncWorkerInterface> sync_worker); 84 void InitializeInternal( 85 scoped_ptr<drive::DriveServiceInterface> drive_service, 86 scoped_ptr<drive::DriveUploaderInterface> drive_uploader, 87 scoped_ptr<SyncWorkerInterface> sync_worker); 88 89 // RemoteFileSyncService overrides. 90 virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE; 91 virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE; 92 virtual void RegisterOrigin( 93 const GURL& origin, 94 const SyncStatusCallback& callback) OVERRIDE; 95 virtual void EnableOrigin( 96 const GURL& origin, 97 const SyncStatusCallback& callback) OVERRIDE; 98 virtual void DisableOrigin( 99 const GURL& origin, 100 const SyncStatusCallback& callback) OVERRIDE; 101 virtual void UninstallOrigin( 102 const GURL& origin, 103 UninstallFlag flag, 104 const SyncStatusCallback& callback) OVERRIDE; 105 virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE; 106 virtual void SetRemoteChangeProcessor( 107 RemoteChangeProcessor* processor) OVERRIDE; 108 virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE; 109 virtual RemoteServiceState GetCurrentState() const OVERRIDE; 110 virtual void GetOriginStatusMap(const StatusMapCallback& callback) OVERRIDE; 111 virtual void DumpFiles(const GURL& origin, 112 const ListCallback& callback) OVERRIDE; 113 virtual void DumpDatabase(const ListCallback& callback) OVERRIDE; 114 virtual void SetSyncEnabled(bool enabled) OVERRIDE; 115 virtual void PromoteDemotedChanges(const base::Closure& callback) OVERRIDE; 116 117 // LocalChangeProcessor overrides. 118 virtual void ApplyLocalChange( 119 const FileChange& local_change, 120 const base::FilePath& local_path, 121 const SyncFileMetadata& local_metadata, 122 const fileapi::FileSystemURL& url, 123 const SyncStatusCallback& callback) OVERRIDE; 124 125 // drive::DriveNotificationObserver overrides. 126 virtual void OnNotificationReceived() OVERRIDE; 127 virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE; 128 129 // drive::DriveServiceObserver overrides. 130 virtual void OnReadyToSendRequests() OVERRIDE; 131 virtual void OnRefreshTokenInvalid() OVERRIDE; 132 133 // net::NetworkChangeNotifier::NetworkChangeObserver overrides. 134 virtual void OnNetworkChanged( 135 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; 136 137 // SigninManagerBase::Observer overrides. 138 virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) OVERRIDE; 139 virtual void GoogleSigninSucceeded(const std::string& username, 140 const std::string& password) OVERRIDE; 141 virtual void GoogleSignedOut(const std::string& username) OVERRIDE; 142 143 private: 144 class WorkerObserver; 145 146 friend class DriveBackendSyncTest; 147 friend class SyncEngineTest; 148 149 SyncEngine(base::SingleThreadTaskRunner* ui_task_runner, 150 base::SequencedTaskRunner* worker_task_runner, 151 base::SequencedTaskRunner* drive_task_runner, 152 const base::FilePath& sync_file_system_dir, 153 TaskLogger* task_logger, 154 drive::DriveNotificationManager* notification_manager, 155 ExtensionServiceInterface* extension_service, 156 SigninManagerBase* signin_manager, 157 ProfileOAuth2TokenService* token_service, 158 net::URLRequestContextGetter* request_context, 159 leveldb::Env* env_override); 160 161 // Called by WorkerObserver. 162 void OnPendingFileListUpdated(int item_count); 163 void OnFileStatusChanged(const fileapi::FileSystemURL& url, 164 SyncFileStatus file_status, 165 SyncAction sync_action, 166 SyncDirection direction); 167 void UpdateServiceState(RemoteServiceState state, 168 const std::string& description); 169 170 SyncStatusCallback TrackCallback(const SyncStatusCallback& callback); 171 172 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 173 scoped_refptr<base::SequencedTaskRunner> worker_task_runner_; 174 scoped_refptr<base::SequencedTaskRunner> drive_task_runner_; 175 176 const base::FilePath sync_file_system_dir_; 177 TaskLogger* task_logger_; 178 179 // These external services are not owned by SyncEngine. 180 // The owner of the SyncEngine is responsible for their lifetime. 181 // I.e. the owner should declare the dependency explicitly by calling 182 // KeyedService::DependsOn(). 183 drive::DriveNotificationManager* notification_manager_; 184 ExtensionServiceInterface* extension_service_; 185 SigninManagerBase* signin_manager_; 186 ProfileOAuth2TokenService* token_service_; 187 188 scoped_refptr<net::URLRequestContextGetter> request_context_; 189 190 scoped_ptr<drive::DriveServiceInterface> drive_service_; 191 scoped_ptr<DriveServiceWrapper> drive_service_wrapper_; 192 scoped_ptr<drive::DriveUploaderInterface> drive_uploader_; 193 scoped_ptr<DriveUploaderWrapper> drive_uploader_wrapper_; 194 195 RemoteChangeProcessor* remote_change_processor_; // Not owned. 196 scoped_ptr<RemoteChangeProcessorWrapper> remote_change_processor_wrapper_; 197 // Delete this on worker. 198 scoped_ptr<RemoteChangeProcessorOnWorker> remote_change_processor_on_worker_; 199 200 RemoteServiceState service_state_; 201 bool has_refresh_token_; 202 bool network_available_; 203 bool sync_enabled_; 204 205 // Delete them on worker. 206 scoped_ptr<WorkerObserver> worker_observer_; 207 scoped_ptr<SyncWorkerInterface> sync_worker_; 208 209 ObserverList<SyncServiceObserver> service_observers_; 210 ObserverList<FileStatusObserver> file_status_observers_; 211 leveldb::Env* env_override_; 212 213 CallbackTracker callback_tracker_; 214 215 base::WeakPtrFactory<SyncEngine> weak_ptr_factory_; 216 DISALLOW_COPY_AND_ASSIGN(SyncEngine); 217}; 218 219} // namespace drive_backend 220} // namespace sync_file_system 221 222#endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_ 223