sync_engine.h revision f2477e01787aa58f445919b809d89e252beef54f
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;
20
21namespace base {
22class SequencedTaskRunner;
23}
24
25namespace drive {
26class DriveServiceInterface;
27class DriveNotificationManager;
28}
29
30namespace sync_file_system {
31namespace drive_backend {
32
33class LocalToRemoteSyncer;
34class MetadataDatabase;
35class RemoteToLocalSyncer;
36class SyncEngineInitializer;
37
38class SyncEngine : public RemoteFileSyncService,
39                   public LocalChangeProcessor,
40                   public SyncTaskManager::Client,
41                   public drive::DriveNotificationObserver,
42                   public drive::DriveServiceObserver,
43                   public net::NetworkChangeNotifier::NetworkChangeObserver,
44                   public SyncEngineContext {
45 public:
46  typedef Observer SyncServiceObserver;
47
48  SyncEngine(const base::FilePath& base_dir,
49             base::SequencedTaskRunner* task_runner,
50             scoped_ptr<drive::DriveServiceInterface> drive_service,
51             scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
52             drive::DriveNotificationManager* notification_manager,
53             ExtensionServiceInterface* extension_service);
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 void SetSyncEnabled(bool enabled) OVERRIDE;
83  virtual SyncStatusCode SetConflictResolutionPolicy(
84      ConflictResolutionPolicy policy) OVERRIDE;
85  virtual ConflictResolutionPolicy GetConflictResolutionPolicy() const OVERRIDE;
86  virtual void GetRemoteVersions(
87      const fileapi::FileSystemURL& url,
88      const RemoteVersionsCallback& callback) OVERRIDE;
89  virtual void DownloadRemoteVersion(
90      const fileapi::FileSystemURL& url,
91      const std::string& version_id,
92      const DownloadVersionCallback& callback) OVERRIDE;
93
94  // LocalChangeProcessor overrides.
95  virtual void ApplyLocalChange(
96      const FileChange& local_change,
97      const base::FilePath& local_path,
98      const SyncFileMetadata& local_metadata,
99      const fileapi::FileSystemURL& url,
100      const SyncStatusCallback& callback) OVERRIDE;
101
102  // SyncTaskManager::Client overrides.
103  virtual void MaybeScheduleNextTask() OVERRIDE;
104  virtual void NotifyLastOperationStatus(SyncStatusCode sync_status,
105                                         bool used_network) OVERRIDE;
106
107  // drive::DriveNotificationObserver overrides.
108  virtual void OnNotificationReceived() OVERRIDE;
109  virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
110
111  // drive::DriveServiceObserver overrides.
112  virtual void OnReadyToSendRequests() OVERRIDE;
113  virtual void OnRefreshTokenInvalid() OVERRIDE;
114
115  // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
116  virtual void OnNetworkChanged(
117      net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
118
119  // SyncEngineContext overrides.
120  virtual drive::DriveServiceInterface* GetDriveService() OVERRIDE;
121  virtual drive::DriveUploaderInterface* GetDriveUploader() OVERRIDE;
122  virtual MetadataDatabase* GetMetadataDatabase() OVERRIDE;
123  virtual RemoteChangeProcessor* GetRemoteChangeProcessor() OVERRIDE;
124  virtual base::SequencedTaskRunner* GetBlockingTaskRunner() OVERRIDE;
125
126 private:
127  friend class SyncEngineTest;
128  void DoDisableApp(const std::string& app_id,
129                    const SyncStatusCallback& callback);
130  void DoEnableApp(const std::string& app_id,
131                   const SyncStatusCallback& callback);
132
133  void DidInitialize(SyncEngineInitializer* initializer,
134                     SyncStatusCode status);
135  void DidProcessRemoteChange(RemoteToLocalSyncer* syncer,
136                              const SyncFileCallback& callback,
137                              SyncStatusCode status);
138  void DidApplyLocalChange(LocalToRemoteSyncer* syncer,
139                           const SyncStatusCallback& callback,
140                           SyncStatusCode status);
141  void DidFetchChangeList(SyncStatusCallback& callback);
142
143  void MaybeStartFetchChanges();
144  void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state,
145                                            bool used_network);
146  void UpdateServiceState(RemoteServiceState state,
147                          const std::string& description);
148  void UpdateRegisteredApps();
149
150  base::FilePath base_dir_;
151  base::FilePath temporary_file_dir_;
152
153  scoped_refptr<base::SequencedTaskRunner> task_runner_;
154
155  scoped_ptr<drive::DriveServiceInterface> drive_service_;
156  scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
157  scoped_ptr<MetadataDatabase> metadata_database_;
158
159  // These external services are not owned by SyncEngine.
160  // The owner of the SyncEngine is responsible for their lifetime.
161  // I.e. the owner should declare the dependency explicitly by calling
162  // BrowserContextKeyedService::DependsOn().
163  drive::DriveNotificationManager* notification_manager_;
164  ExtensionServiceInterface* extension_service_;
165
166  ObserverList<SyncServiceObserver> service_observers_;
167  ObserverList<FileStatusObserver> file_status_observers_;
168  RemoteChangeProcessor* remote_change_processor_;
169
170  RemoteServiceState service_state_;
171
172  bool should_check_remote_change_;
173  base::TimeTicks time_to_check_changes_;
174
175  bool sync_enabled_;
176  ConflictResolutionPolicy conflict_resolution_policy_;
177  bool network_available_;
178
179  scoped_ptr<SyncTaskManager> task_manager_;
180
181  base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
182
183  DISALLOW_COPY_AND_ASSIGN(SyncEngine);
184};
185
186}  // namespace drive_backend
187}  // namespace sync_file_system
188
189#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_
190