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/local_change_processor.h"
17 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
18 #include "chrome/browser/sync_file_system/sync_action.h"
19 #include "chrome/browser/sync_file_system/sync_direction.h"
20 #include "net/base/network_change_notifier.h"
22 class ExtensionServiceInterface;
23 class SigninManagerBase;
26 class SequencedTaskRunner;
30 class DriveServiceInterface;
31 class DriveNotificationManager;
32 class DriveUploaderInterface;
39 namespace sync_file_system {
41 class RemoteChangeProcessor;
43 namespace drive_backend {
45 class DriveServiceWrapper;
46 class DriveUploaderWrapper;
47 class LocalToRemoteSyncer;
48 class MetadataDatabase;
49 class RemoteChangeProcessorOnWorker;
50 class RemoteChangeProcessorWrapper;
51 class RemoteToLocalSyncer;
52 class SyncEngineInitializer;
53 class SyncTaskManager;
56 class SyncEngine : public RemoteFileSyncService,
57 public LocalChangeProcessor,
58 public drive::DriveNotificationObserver,
59 public drive::DriveServiceObserver,
60 public net::NetworkChangeNotifier::NetworkChangeObserver {
62 typedef Observer SyncServiceObserver;
64 static scoped_ptr<SyncEngine> CreateForBrowserContext(
65 content::BrowserContext* context);
66 static void AppendDependsOnFactories(
67 std::set<BrowserContextKeyedServiceFactory*>* factories);
69 virtual ~SyncEngine();
71 void Initialize(const base::FilePath& base_dir,
72 base::SequencedTaskRunner* file_task_runner,
73 leveldb::Env* env_override);
75 // RemoteFileSyncService overrides.
76 virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE;
77 virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE;
78 virtual void RegisterOrigin(
80 const SyncStatusCallback& callback) OVERRIDE;
81 virtual void EnableOrigin(
83 const SyncStatusCallback& callback) OVERRIDE;
84 virtual void DisableOrigin(
86 const SyncStatusCallback& callback) OVERRIDE;
87 virtual void UninstallOrigin(
90 const SyncStatusCallback& callback) OVERRIDE;
91 virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE;
92 virtual void SetRemoteChangeProcessor(
93 RemoteChangeProcessor* processor) OVERRIDE;
94 virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE;
95 virtual bool IsConflicting(const fileapi::FileSystemURL& url) OVERRIDE;
96 virtual RemoteServiceState GetCurrentState() const OVERRIDE;
97 virtual void GetOriginStatusMap(OriginStatusMap* status_map) OVERRIDE;
98 virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) OVERRIDE;
99 virtual scoped_ptr<base::ListValue> DumpDatabase() OVERRIDE;
100 virtual void SetSyncEnabled(bool enabled) OVERRIDE;
101 virtual SyncStatusCode SetDefaultConflictResolutionPolicy(
102 ConflictResolutionPolicy policy) OVERRIDE;
103 virtual SyncStatusCode SetConflictResolutionPolicy(
105 ConflictResolutionPolicy policy) OVERRIDE;
106 virtual ConflictResolutionPolicy GetDefaultConflictResolutionPolicy()
108 virtual ConflictResolutionPolicy GetConflictResolutionPolicy(
109 const GURL& origin) const OVERRIDE;
110 virtual void GetRemoteVersions(
111 const fileapi::FileSystemURL& url,
112 const RemoteVersionsCallback& callback) OVERRIDE;
113 virtual void DownloadRemoteVersion(
114 const fileapi::FileSystemURL& url,
115 const std::string& version_id,
116 const DownloadVersionCallback& callback) OVERRIDE;
117 virtual void PromoteDemotedChanges() OVERRIDE;
119 // LocalChangeProcessor overrides.
120 virtual void ApplyLocalChange(
121 const FileChange& local_change,
122 const base::FilePath& local_path,
123 const SyncFileMetadata& local_metadata,
124 const fileapi::FileSystemURL& url,
125 const SyncStatusCallback& callback) OVERRIDE;
127 // drive::DriveNotificationObserver overrides.
128 virtual void OnNotificationReceived() OVERRIDE;
129 virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
131 // drive::DriveServiceObserver overrides.
132 virtual void OnReadyToSendRequests() OVERRIDE;
133 virtual void OnRefreshTokenInvalid() OVERRIDE;
135 // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
136 virtual void OnNetworkChanged(
137 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
139 drive::DriveServiceInterface* GetDriveService();
140 drive::DriveUploaderInterface* GetDriveUploader();
141 MetadataDatabase* GetMetadataDatabase();
142 SyncTaskManager* GetSyncTaskManagerForTesting();
144 // Notifies update of sync status to each observer.
145 void UpdateSyncEnabled(bool enabled);
147 void OnPendingFileListUpdated(int item_count);
148 void OnFileStatusChanged(const fileapi::FileSystemURL& url,
149 SyncFileStatus file_status,
150 SyncAction sync_action,
151 SyncDirection direction);
152 void UpdateServiceState(RemoteServiceState state,
153 const std::string& description);
156 class WorkerObserver;
158 friend class DriveBackendSyncTest;
159 friend class SyncEngineTest;
161 SyncEngine(scoped_ptr<drive::DriveServiceInterface> drive_service,
162 scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
163 base::SequencedTaskRunner* worker_task_runner,
164 drive::DriveNotificationManager* notification_manager,
165 ExtensionServiceInterface* extension_service,
166 SigninManagerBase* signin_manager);
168 void UpdateRegisteredApps();
170 scoped_ptr<drive::DriveServiceInterface> drive_service_;
171 scoped_ptr<DriveServiceWrapper> drive_service_wrapper_;
172 scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
173 scoped_ptr<DriveUploaderWrapper> drive_uploader_wrapper_;
174 RemoteChangeProcessor* remote_change_processor_;
175 scoped_ptr<RemoteChangeProcessorWrapper> remote_change_processor_wrapper_;
177 scoped_ptr<RemoteChangeProcessorOnWorker> remote_change_processor_on_worker_;
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_;
187 ObserverList<SyncServiceObserver> service_observers_;
188 ObserverList<FileStatusObserver> file_status_observers_;
190 scoped_ptr<WorkerObserver> worker_observer_;
191 scoped_ptr<SyncWorker> sync_worker_;
192 scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
194 base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
195 DISALLOW_COPY_AND_ASSIGN(SyncEngine);
198 } // namespace drive_backend
199 } // namespace sync_file_system
201 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_