sync_engine.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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 "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"
18
19class ExtensionServiceInterface;
20class ProfileOAuth2TokenService;
21
22namespace base {
23class SequencedTaskRunner;
24}
25
26namespace drive {
27class DriveServiceInterface;
28class DriveNotificationManager;
29}
30
31namespace sync_file_system {
32namespace drive_backend {
33
34class LocalToRemoteSyncer;
35class MetadataDatabase;
36class RemoteToLocalSyncer;
37class SyncEngineInitializer;
38
39class SyncEngine : public RemoteFileSyncService,
40                   public LocalChangeProcessor,
41                   public SyncTaskManager::Client,
42                   public drive::DriveNotificationObserver,
43                   public drive::DriveServiceObserver,
44                   public net::NetworkChangeNotifier::NetworkChangeObserver,
45                   public SyncEngineContext {
46 public:
47  typedef Observer SyncServiceObserver;
48
49  static scoped_ptr<SyncEngine> CreateForBrowserContext(
50      content::BrowserContext* context);
51  static void AppendDependsOnFactories(
52      std::set<BrowserContextKeyedServiceFactory*>* factories);
53
54  virtual ~SyncEngine();
55
56  void Initialize();
57
58  // RemoteFileSyncService overrides.
59  virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE;
60  virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE;
61  virtual void RegisterOrigin(
62      const GURL& origin,
63      const SyncStatusCallback& callback) OVERRIDE;
64  virtual void EnableOrigin(
65      const GURL& origin,
66      const SyncStatusCallback& callback) OVERRIDE;
67  virtual void DisableOrigin(
68      const GURL& origin,
69      const SyncStatusCallback& callback) OVERRIDE;
70  virtual void UninstallOrigin(
71      const GURL& origin,
72      UninstallFlag flag,
73      const SyncStatusCallback& callback) OVERRIDE;
74  virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE;
75  virtual void SetRemoteChangeProcessor(
76      RemoteChangeProcessor* processor) OVERRIDE;
77  virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE;
78  virtual bool IsConflicting(const fileapi::FileSystemURL& url) OVERRIDE;
79  virtual RemoteServiceState GetCurrentState() const OVERRIDE;
80  virtual void GetOriginStatusMap(OriginStatusMap* status_map) OVERRIDE;
81  virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) OVERRIDE;
82  virtual scoped_ptr<base::ListValue> DumpDatabase() OVERRIDE;
83  virtual void SetSyncEnabled(bool enabled) OVERRIDE;
84  virtual SyncStatusCode SetConflictResolutionPolicy(
85      ConflictResolutionPolicy policy) OVERRIDE;
86  virtual ConflictResolutionPolicy GetConflictResolutionPolicy() const OVERRIDE;
87  virtual void GetRemoteVersions(
88      const fileapi::FileSystemURL& url,
89      const RemoteVersionsCallback& callback) OVERRIDE;
90  virtual void DownloadRemoteVersion(
91      const fileapi::FileSystemURL& url,
92      const std::string& version_id,
93      const DownloadVersionCallback& callback) OVERRIDE;
94
95  // LocalChangeProcessor overrides.
96  virtual void ApplyLocalChange(
97      const FileChange& local_change,
98      const base::FilePath& local_path,
99      const SyncFileMetadata& local_metadata,
100      const fileapi::FileSystemURL& url,
101      const SyncStatusCallback& callback) OVERRIDE;
102
103  // SyncTaskManager::Client overrides.
104  virtual void MaybeScheduleNextTask() OVERRIDE;
105  virtual void NotifyLastOperationStatus(SyncStatusCode sync_status,
106                                         bool used_network) OVERRIDE;
107
108  // drive::DriveNotificationObserver overrides.
109  virtual void OnNotificationReceived() OVERRIDE;
110  virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
111
112  // drive::DriveServiceObserver overrides.
113  virtual void OnReadyToSendRequests() OVERRIDE;
114  virtual void OnRefreshTokenInvalid() OVERRIDE;
115
116  // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
117  virtual void OnNetworkChanged(
118      net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
119
120  // SyncEngineContext overrides.
121  virtual drive::DriveServiceInterface* GetDriveService() OVERRIDE;
122  virtual drive::DriveUploaderInterface* GetDriveUploader() OVERRIDE;
123  virtual MetadataDatabase* GetMetadataDatabase() OVERRIDE;
124  virtual RemoteChangeProcessor* GetRemoteChangeProcessor() OVERRIDE;
125  virtual base::SequencedTaskRunner* GetBlockingTaskRunner() OVERRIDE;
126
127 private:
128  friend class SyncEngineTest;
129
130  SyncEngine(const base::FilePath& base_dir,
131             base::SequencedTaskRunner* task_runner,
132             scoped_ptr<drive::DriveServiceInterface> drive_service,
133             scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
134             drive::DriveNotificationManager* notification_manager,
135             ExtensionServiceInterface* extension_service,
136             ProfileOAuth2TokenService* auth_token_service);
137
138  void DoDisableApp(const std::string& app_id,
139                    const SyncStatusCallback& callback);
140  void DoEnableApp(const std::string& app_id,
141                   const SyncStatusCallback& callback);
142
143  void PostInitializeTask();
144  void DidInitialize(SyncEngineInitializer* initializer,
145                     SyncStatusCode status);
146  void DidProcessRemoteChange(RemoteToLocalSyncer* syncer,
147                              const SyncFileCallback& callback,
148                              SyncStatusCode status);
149  void DidApplyLocalChange(LocalToRemoteSyncer* syncer,
150                           const SyncStatusCallback& callback,
151                           SyncStatusCode status);
152
153  void MaybeStartFetchChanges();
154  void DidResolveConflict(SyncStatusCode status);
155  void DidFetchChanges(SyncStatusCode status);
156
157  void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state,
158                                            bool used_network);
159  void UpdateServiceState(RemoteServiceState state,
160                          const std::string& description);
161  void UpdateRegisteredApps();
162
163  base::FilePath base_dir_;
164  base::FilePath temporary_file_dir_;
165
166  scoped_refptr<base::SequencedTaskRunner> task_runner_;
167
168  scoped_ptr<drive::DriveServiceInterface> drive_service_;
169  scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
170  scoped_ptr<MetadataDatabase> metadata_database_;
171
172  // These external services are not owned by SyncEngine.
173  // The owner of the SyncEngine is responsible for their lifetime.
174  // I.e. the owner should declare the dependency explicitly by calling
175  // BrowserContextKeyedService::DependsOn().
176  drive::DriveNotificationManager* notification_manager_;
177  ExtensionServiceInterface* extension_service_;
178  ProfileOAuth2TokenService* auth_token_service_;
179
180  ObserverList<SyncServiceObserver> service_observers_;
181  ObserverList<FileStatusObserver> file_status_observers_;
182  RemoteChangeProcessor* remote_change_processor_;
183
184  RemoteServiceState service_state_;
185
186  bool should_check_conflict_;
187  bool should_check_remote_change_;
188  base::TimeTicks time_to_check_changes_;
189
190  bool sync_enabled_;
191  ConflictResolutionPolicy conflict_resolution_policy_;
192  bool network_available_;
193
194  scoped_ptr<SyncTaskManager> task_manager_;
195
196  base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
197
198  DISALLOW_COPY_AND_ASSIGN(SyncEngine);
199};
200
201}  // namespace drive_backend
202}  // namespace sync_file_system
203
204#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_
205