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_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list.h"
11 #include "chrome/browser/drive/drive_notification_observer.h"
12 #include "chrome/browser/drive/drive_service_interface.h"
13 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
14 #include "chrome/browser/sync_file_system/local_change_processor.h"
15 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
16 #include "chrome/browser/sync_file_system/sync_task_manager.h"
17 #include "net/base/network_change_notifier.h"
19 class ExtensionServiceInterface;
20 class ProfileOAuth2TokenService;
23 class SequencedTaskRunner;
27 class DriveServiceInterface;
28 class DriveNotificationManager;
35 namespace sync_file_system {
36 namespace drive_backend {
38 class LocalToRemoteSyncer;
39 class MetadataDatabase;
40 class RemoteToLocalSyncer;
41 class SyncEngineInitializer;
43 class SyncEngine : public RemoteFileSyncService,
44 public LocalChangeProcessor,
45 public SyncTaskManager::Client,
46 public drive::DriveNotificationObserver,
47 public drive::DriveServiceObserver,
48 public net::NetworkChangeNotifier::NetworkChangeObserver,
49 public SyncEngineContext {
51 typedef Observer SyncServiceObserver;
53 static scoped_ptr<SyncEngine> CreateForBrowserContext(
54 content::BrowserContext* context);
55 static void AppendDependsOnFactories(
56 std::set<BrowserContextKeyedServiceFactory*>* factories);
58 virtual ~SyncEngine();
62 // RemoteFileSyncService overrides.
63 virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE;
64 virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE;
65 virtual void RegisterOrigin(
67 const SyncStatusCallback& callback) OVERRIDE;
68 virtual void EnableOrigin(
70 const SyncStatusCallback& callback) OVERRIDE;
71 virtual void DisableOrigin(
73 const SyncStatusCallback& callback) OVERRIDE;
74 virtual void UninstallOrigin(
77 const SyncStatusCallback& callback) OVERRIDE;
78 virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE;
79 virtual void SetRemoteChangeProcessor(
80 RemoteChangeProcessor* processor) OVERRIDE;
81 virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE;
82 virtual bool IsConflicting(const fileapi::FileSystemURL& url) OVERRIDE;
83 virtual RemoteServiceState GetCurrentState() const OVERRIDE;
84 virtual void GetOriginStatusMap(OriginStatusMap* status_map) OVERRIDE;
85 virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) OVERRIDE;
86 virtual scoped_ptr<base::ListValue> DumpDatabase() OVERRIDE;
87 virtual void SetSyncEnabled(bool enabled) OVERRIDE;
88 virtual SyncStatusCode SetConflictResolutionPolicy(
89 ConflictResolutionPolicy policy) OVERRIDE;
90 virtual ConflictResolutionPolicy GetConflictResolutionPolicy() const OVERRIDE;
91 virtual void GetRemoteVersions(
92 const fileapi::FileSystemURL& url,
93 const RemoteVersionsCallback& callback) OVERRIDE;
94 virtual void DownloadRemoteVersion(
95 const fileapi::FileSystemURL& url,
96 const std::string& version_id,
97 const DownloadVersionCallback& callback) OVERRIDE;
98 virtual void PromoteDemotedChanges() OVERRIDE;
100 // LocalChangeProcessor overrides.
101 virtual void ApplyLocalChange(
102 const FileChange& local_change,
103 const base::FilePath& local_path,
104 const SyncFileMetadata& local_metadata,
105 const fileapi::FileSystemURL& url,
106 const SyncStatusCallback& callback) OVERRIDE;
108 // SyncTaskManager::Client overrides.
109 virtual void MaybeScheduleNextTask() OVERRIDE;
110 virtual void NotifyLastOperationStatus(SyncStatusCode sync_status,
111 bool used_network) OVERRIDE;
113 // drive::DriveNotificationObserver overrides.
114 virtual void OnNotificationReceived() OVERRIDE;
115 virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
117 // drive::DriveServiceObserver overrides.
118 virtual void OnReadyToSendRequests() OVERRIDE;
119 virtual void OnRefreshTokenInvalid() OVERRIDE;
121 // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
122 virtual void OnNetworkChanged(
123 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
125 // SyncEngineContext overrides.
126 virtual drive::DriveServiceInterface* GetDriveService() OVERRIDE;
127 virtual drive::DriveUploaderInterface* GetDriveUploader() OVERRIDE;
128 virtual MetadataDatabase* GetMetadataDatabase() OVERRIDE;
129 virtual RemoteChangeProcessor* GetRemoteChangeProcessor() OVERRIDE;
130 virtual base::SequencedTaskRunner* GetBlockingTaskRunner() OVERRIDE;
133 friend class DriveBackendSyncTest;
134 friend class SyncEngineTest;
136 SyncEngine(const base::FilePath& base_dir,
137 base::SequencedTaskRunner* task_runner,
138 scoped_ptr<drive::DriveServiceInterface> drive_service,
139 scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
140 drive::DriveNotificationManager* notification_manager,
141 ExtensionServiceInterface* extension_service,
142 ProfileOAuth2TokenService* auth_token_service,
143 leveldb::Env* env_override);
145 void DoDisableApp(const std::string& app_id,
146 const SyncStatusCallback& callback);
147 void DoEnableApp(const std::string& app_id,
148 const SyncStatusCallback& callback);
150 void PostInitializeTask();
151 void DidInitialize(SyncEngineInitializer* initializer,
152 SyncStatusCode status);
153 void DidProcessRemoteChange(RemoteToLocalSyncer* syncer,
154 const SyncFileCallback& callback,
155 SyncStatusCode status);
156 void DidApplyLocalChange(LocalToRemoteSyncer* syncer,
157 const SyncStatusCallback& callback,
158 SyncStatusCode status);
160 void MaybeStartFetchChanges();
161 void DidResolveConflict(SyncStatusCode status);
162 void DidFetchChanges(SyncStatusCode status);
164 void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state,
166 void UpdateServiceState(RemoteServiceState state,
167 const std::string& description);
168 void UpdateRegisteredApps();
170 base::FilePath base_dir_;
171 base::FilePath temporary_file_dir_;
173 scoped_refptr<base::SequencedTaskRunner> task_runner_;
174 leveldb::Env* env_override_;
176 scoped_ptr<drive::DriveServiceInterface> drive_service_;
177 scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
178 scoped_ptr<MetadataDatabase> metadata_database_;
180 // These external services are not owned by SyncEngine.
181 // The owner of the SyncEngine is responsible for their lifetime.
182 // I.e. the owner should declare the dependency explicitly by calling
183 // BrowserContextKeyedService::DependsOn().
184 drive::DriveNotificationManager* notification_manager_;
185 ExtensionServiceInterface* extension_service_;
186 ProfileOAuth2TokenService* auth_token_service_;
188 ObserverList<SyncServiceObserver> service_observers_;
189 ObserverList<FileStatusObserver> file_status_observers_;
190 RemoteChangeProcessor* remote_change_processor_;
192 RemoteServiceState service_state_;
194 bool should_check_conflict_;
195 bool should_check_remote_change_;
196 bool listing_remote_changes_;
197 base::TimeTicks time_to_check_changes_;
200 ConflictResolutionPolicy conflict_resolution_policy_;
201 bool network_available_;
203 scoped_ptr<SyncTaskManager> task_manager_;
205 base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
207 DISALLOW_COPY_AND_ASSIGN(SyncEngine);
210 } // namespace drive_backend
211 } // namespace sync_file_system
213 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_