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