sync_worker.h revision 010d83a9304c5a91596085d917d248abff47903a
1// Copyright 2014 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_WORKER_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_WORKER_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/sync_task_manager.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 "net/base/network_change_notifier.h"
22
23class ExtensionServiceInterface;
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 LocalToRemoteSyncer;
46class MetadataDatabase;
47class RemoteChangeProcessorOnWorker;
48class RemoteToLocalSyncer;
49class SyncEngineContext;
50class SyncEngineInitializer;
51
52class SyncWorker : public SyncTaskManager::Client {
53 public:
54  enum AppStatus {
55    APP_STATUS_ENABLED,
56    APP_STATUS_DISABLED,
57    APP_STATUS_UNINSTALLED,
58  };
59
60  typedef base::hash_map<std::string, AppStatus> AppStatusMap;
61
62  class Observer {
63   public:
64    virtual void OnPendingFileListUpdated(int item_count) = 0;
65    virtual void OnFileStatusChanged(const fileapi::FileSystemURL& url,
66                                     SyncFileStatus file_status,
67                                     SyncAction sync_action,
68                                     SyncDirection direction) = 0;
69    virtual void UpdateServiceState(RemoteServiceState state,
70                                    const std::string& description) = 0;
71
72   protected:
73    virtual ~Observer() {}
74  };
75
76  static scoped_ptr<SyncWorker> CreateOnWorker(
77      const base::FilePath& base_dir,
78      Observer* observer,
79      const base::WeakPtr<ExtensionServiceInterface>& extension_service,
80      scoped_ptr<SyncEngineContext> sync_engine_context,
81      leveldb::Env* env_override);
82
83  virtual ~SyncWorker();
84
85  void Initialize();
86
87  // SyncTaskManager::Client overrides
88  virtual void MaybeScheduleNextTask() OVERRIDE;
89  virtual void NotifyLastOperationStatus(
90      SyncStatusCode sync_status, bool used_network) OVERRIDE;
91
92  void RegisterOrigin(const GURL& origin, const SyncStatusCallback& callback);
93  void EnableOrigin(const GURL& origin, const SyncStatusCallback& callback);
94  void DisableOrigin(const GURL& origin, const SyncStatusCallback& callback);
95  void UninstallOrigin(
96      const GURL& origin,
97      RemoteFileSyncService::UninstallFlag flag,
98      const SyncStatusCallback& callback);
99  void ProcessRemoteChange(const SyncFileCallback& callback);
100  void SetRemoteChangeProcessor(
101      RemoteChangeProcessorOnWorker* remote_change_processor_on_worker);
102  RemoteServiceState GetCurrentState() const;
103  void GetOriginStatusMap(RemoteFileSyncService::OriginStatusMap* status_map);
104  scoped_ptr<base::ListValue> DumpFiles(const GURL& origin);
105  scoped_ptr<base::ListValue> DumpDatabase();
106  void SetSyncEnabled(bool enabled);
107  SyncStatusCode SetDefaultConflictResolutionPolicy(
108      ConflictResolutionPolicy policy);
109  SyncStatusCode SetConflictResolutionPolicy(
110      const GURL& origin,
111      ConflictResolutionPolicy policy);
112  ConflictResolutionPolicy GetDefaultConflictResolutionPolicy()
113      const;
114  ConflictResolutionPolicy GetConflictResolutionPolicy(
115      const GURL& origin) const;
116
117  void ApplyLocalChange(
118      const FileChange& local_change,
119      const base::FilePath& local_path,
120      const SyncFileMetadata& local_metadata,
121      const fileapi::FileSystemURL& url,
122      const SyncStatusCallback& callback);
123
124  void OnNotificationReceived();
125
126  void OnReadyToSendRequests(const std::string& account_id);
127  void OnRefreshTokenInvalid();
128
129  void OnNetworkChanged(net::NetworkChangeNotifier::ConnectionType type);
130
131  drive::DriveServiceInterface* GetDriveService();
132  drive::DriveUploaderInterface* GetDriveUploader();
133  MetadataDatabase* GetMetadataDatabase();
134  SyncTaskManager* GetSyncTaskManager();
135
136  void AddObserver(Observer* observer);
137
138 private:
139  friend class DriveBackendSyncTest;
140  friend class SyncEngineTest;
141
142  SyncWorker(const base::FilePath& base_dir,
143             const base::WeakPtr<ExtensionServiceInterface>& extension_service,
144             scoped_ptr<SyncEngineContext> sync_engine_context,
145             leveldb::Env* env_override);
146
147  void DoDisableApp(const std::string& app_id,
148                    const SyncStatusCallback& callback);
149  void DoEnableApp(const std::string& app_id,
150                   const SyncStatusCallback& callback);
151
152  void PostInitializeTask();
153  void DidInitialize(SyncEngineInitializer* initializer,
154                     SyncStatusCode status);
155  void UpdateRegisteredApp();
156  void DidQueryAppStatus(const AppStatusMap* app_status);
157  void DidProcessRemoteChange(RemoteToLocalSyncer* syncer,
158                              const SyncFileCallback& callback,
159                              SyncStatusCode status);
160  void DidApplyLocalChange(LocalToRemoteSyncer* syncer,
161                           const SyncStatusCallback& callback,
162                           SyncStatusCode status);
163
164  void MaybeStartFetchChanges();
165  void DidResolveConflict(SyncStatusCode status);
166  void DidFetchChanges(SyncStatusCode status);
167
168  void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state,
169                                            bool used_network);
170  void UpdateServiceState(RemoteServiceState state,
171                          const std::string& description);
172  void UpdateRegisteredApps();
173
174  base::FilePath base_dir_;
175
176  leveldb::Env* env_override_;
177
178  // Sync with SyncEngine.
179  RemoteServiceState service_state_;
180
181  bool should_check_conflict_;
182  bool should_check_remote_change_;
183  bool listing_remote_changes_;
184  base::TimeTicks time_to_check_changes_;
185
186  bool sync_enabled_;
187  ConflictResolutionPolicy default_conflict_resolution_policy_;
188  bool network_available_;
189
190  scoped_ptr<SyncTaskManager> task_manager_;
191
192  base::WeakPtr<ExtensionServiceInterface> extension_service_;
193
194  scoped_ptr<SyncEngineContext> context_;
195  ObserverList<Observer> observers_;
196
197  base::WeakPtrFactory<SyncWorker> weak_ptr_factory_;
198  DISALLOW_COPY_AND_ASSIGN(SyncWorker);
199};
200
201}  // namespace drive_backend
202}  // namespace sync_file_system
203
204#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_WORKER_H_
205