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