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