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