sync_engine.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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/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"
21
22class ExtensionServiceInterface;
23class SigninManagerBase;
24
25namespace base {
26class SequencedTaskRunner;
27}
28
29namespace drive {
30class DriveServiceInterface;
31class DriveNotificationManager;
32class DriveUploaderInterface;
33}
34
35namespace leveldb {
36class Env;
37}
38
39namespace sync_file_system {
40
41class RemoteChangeProcessor;
42
43namespace drive_backend {
44
45class DriveServiceWrapper;
46class DriveUploaderWrapper;
47class MetadataDatabase;
48class RemoteChangeProcessorOnWorker;
49class RemoteChangeProcessorWrapper;
50class SyncTaskManager;
51class SyncWorker;
52
53class SyncEngine : public RemoteFileSyncService,
54                   public LocalChangeProcessor,
55                   public drive::DriveNotificationObserver,
56                   public drive::DriveServiceObserver,
57                   public net::NetworkChangeNotifier::NetworkChangeObserver {
58 public:
59  typedef Observer SyncServiceObserver;
60
61  static scoped_ptr<SyncEngine> CreateForBrowserContext(
62      content::BrowserContext* context,
63      TaskLogger* task_logger);
64  static void AppendDependsOnFactories(
65      std::set<BrowserContextKeyedServiceFactory*>* factories);
66
67  virtual ~SyncEngine();
68
69  void Initialize(const base::FilePath& base_dir,
70                  TaskLogger* task_logger,
71                  base::SequencedTaskRunner* file_task_runner,
72                  leveldb::Env* env_override);
73
74  // RemoteFileSyncService overrides.
75  virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE;
76  virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE;
77  virtual void RegisterOrigin(
78      const GURL& origin,
79      const SyncStatusCallback& callback) OVERRIDE;
80  virtual void EnableOrigin(
81      const GURL& origin,
82      const SyncStatusCallback& callback) OVERRIDE;
83  virtual void DisableOrigin(
84      const GURL& origin,
85      const SyncStatusCallback& callback) OVERRIDE;
86  virtual void UninstallOrigin(
87      const GURL& origin,
88      UninstallFlag flag,
89      const SyncStatusCallback& callback) OVERRIDE;
90  virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE;
91  virtual void SetRemoteChangeProcessor(
92      RemoteChangeProcessor* processor) OVERRIDE;
93  virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE;
94  virtual RemoteServiceState GetCurrentState() const OVERRIDE;
95  virtual void GetOriginStatusMap(const StatusMapCallback& callback) OVERRIDE;
96  virtual void DumpFiles(const GURL& origin,
97                         const ListCallback& callback) OVERRIDE;
98  virtual void DumpDatabase(const ListCallback& callback) OVERRIDE;
99  virtual void SetSyncEnabled(bool enabled) OVERRIDE;
100  virtual void PromoteDemotedChanges() OVERRIDE;
101
102  // LocalChangeProcessor overrides.
103  virtual void ApplyLocalChange(
104      const FileChange& local_change,
105      const base::FilePath& local_path,
106      const SyncFileMetadata& local_metadata,
107      const fileapi::FileSystemURL& url,
108      const SyncStatusCallback& callback) OVERRIDE;
109
110  // drive::DriveNotificationObserver overrides.
111  virtual void OnNotificationReceived() OVERRIDE;
112  virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
113
114  // drive::DriveServiceObserver overrides.
115  virtual void OnReadyToSendRequests() OVERRIDE;
116  virtual void OnRefreshTokenInvalid() OVERRIDE;
117
118  // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
119  virtual void OnNetworkChanged(
120      net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
121
122  drive::DriveServiceInterface* GetDriveService();
123  drive::DriveUploaderInterface* GetDriveUploader();
124  MetadataDatabase* GetMetadataDatabase();
125  SyncTaskManager* GetSyncTaskManagerForTesting();
126
127  void OnPendingFileListUpdated(int item_count);
128  void OnFileStatusChanged(const fileapi::FileSystemURL& url,
129                           SyncFileStatus file_status,
130                           SyncAction sync_action,
131                           SyncDirection direction);
132  void UpdateServiceState(RemoteServiceState state,
133                          const std::string& description);
134
135 private:
136  class WorkerObserver;
137
138  friend class DriveBackendSyncTest;
139  friend class SyncEngineTest;
140
141  SyncEngine(scoped_ptr<drive::DriveServiceInterface> drive_service,
142             scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
143             base::SequencedTaskRunner* worker_task_runner,
144             drive::DriveNotificationManager* notification_manager,
145             ExtensionServiceInterface* extension_service,
146             SigninManagerBase* signin_manager);
147
148  void UpdateRegisteredApps();
149
150  scoped_ptr<drive::DriveServiceInterface> drive_service_;
151  scoped_ptr<DriveServiceWrapper> drive_service_wrapper_;
152  scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
153  scoped_ptr<DriveUploaderWrapper> drive_uploader_wrapper_;
154  RemoteChangeProcessor* remote_change_processor_;
155  scoped_ptr<RemoteChangeProcessorWrapper> remote_change_processor_wrapper_;
156
157  scoped_ptr<RemoteChangeProcessorOnWorker> remote_change_processor_on_worker_;
158
159  RemoteServiceState service_state_;
160
161  // These external services are not owned by SyncEngine.
162  // The owner of the SyncEngine is responsible for their lifetime.
163  // I.e. the owner should declare the dependency explicitly by calling
164  // KeyedService::DependsOn().
165  drive::DriveNotificationManager* notification_manager_;
166  ExtensionServiceInterface* extension_service_;
167  SigninManagerBase* signin_manager_;
168
169  ObserverList<SyncServiceObserver> service_observers_;
170  ObserverList<FileStatusObserver> file_status_observers_;
171
172  scoped_ptr<WorkerObserver> worker_observer_;
173  scoped_ptr<SyncWorker> sync_worker_;
174  scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
175
176  base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
177  DISALLOW_COPY_AND_ASSIGN(SyncEngine);
178};
179
180}  // namespace drive_backend
181}  // namespace sync_file_system
182
183#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_
184