Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / sync_worker.h
blobdeb0ee56e488ebf54fbb1d6d3e988f0215a81cad
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.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_WORKER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_WORKER_H_
8 #include <string>
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h"
15 #include "chrome/browser/sync_file_system/drive_backend/sync_worker_interface.h"
16 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
17 #include "chrome/browser/sync_file_system/sync_callbacks.h"
18 #include "chrome/browser/sync_file_system/task_logger.h"
20 class ExtensionServiceInterface;
21 class GURL;
23 namespace base {
24 class ListValue;
27 namespace drive {
28 class DriveServiceInterface;
29 class DriveUploaderInterface;
32 namespace storage {
33 class FileSystemURL;
36 namespace leveldb {
37 class Env;
40 namespace sync_file_system {
42 class FileChange;
43 class SyncFileMetadata;
45 namespace drive_backend {
47 class LocalToRemoteSyncer;
48 class MetadataDatabase;
49 class RemoteChangeProcessorOnWorker;
50 class RemoteToLocalSyncer;
51 class SyncEngineContext;
52 class SyncEngineInitializer;
54 class SyncWorker : public SyncWorkerInterface,
55 public SyncTaskManager::Client {
56 public:
57 SyncWorker(const base::FilePath& base_dir,
58 const base::WeakPtr<ExtensionServiceInterface>& extension_service,
59 leveldb::Env* env_override);
61 virtual ~SyncWorker();
63 virtual void Initialize(scoped_ptr<SyncEngineContext> context) OVERRIDE;
65 // SyncTaskManager::Client overrides
66 virtual void MaybeScheduleNextTask() OVERRIDE;
67 virtual void NotifyLastOperationStatus(
68 SyncStatusCode sync_status, bool used_network) OVERRIDE;
69 virtual void RecordTaskLog(scoped_ptr<TaskLogger::TaskLog> task_log) OVERRIDE;
71 // SyncWorkerInterface overrides
72 virtual void RegisterOrigin(const GURL& origin,
73 const SyncStatusCallback& callback) OVERRIDE;
74 virtual void EnableOrigin(const GURL& origin,
75 const SyncStatusCallback& callback) OVERRIDE;
76 virtual void DisableOrigin(const GURL& origin,
77 const SyncStatusCallback& callback) OVERRIDE;
78 virtual void UninstallOrigin(const GURL& origin,
79 RemoteFileSyncService::UninstallFlag flag,
80 const SyncStatusCallback& callback) OVERRIDE;
81 virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE;
82 virtual void SetRemoteChangeProcessor(
83 RemoteChangeProcessorOnWorker* remote_change_processor_on_worker)
84 OVERRIDE;
85 virtual RemoteServiceState GetCurrentState() const OVERRIDE;
86 virtual void GetOriginStatusMap(
87 const RemoteFileSyncService::StatusMapCallback& callback) OVERRIDE;
88 virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) OVERRIDE;
89 virtual scoped_ptr<base::ListValue> DumpDatabase() OVERRIDE;
90 virtual void SetSyncEnabled(bool enabled) OVERRIDE;
91 virtual void PromoteDemotedChanges(const base::Closure& callback) OVERRIDE;
92 virtual void ApplyLocalChange(const FileChange& local_change,
93 const base::FilePath& local_path,
94 const SyncFileMetadata& local_metadata,
95 const storage::FileSystemURL& url,
96 const SyncStatusCallback& callback) OVERRIDE;
97 virtual void ActivateService(RemoteServiceState service_state,
98 const std::string& description) OVERRIDE;
99 virtual void DeactivateService(const std::string& description) OVERRIDE;
100 virtual void DetachFromSequence() OVERRIDE;
101 virtual void AddObserver(Observer* observer) OVERRIDE;
103 private:
104 friend class DriveBackendSyncTest;
105 friend class SyncWorkerTest;
107 enum AppStatus {
108 APP_STATUS_ENABLED,
109 APP_STATUS_DISABLED,
110 APP_STATUS_UNINSTALLED,
113 typedef base::hash_map<std::string, AppStatus> AppStatusMap;
115 void DoDisableApp(const std::string& app_id,
116 const SyncStatusCallback& callback);
117 void DoEnableApp(const std::string& app_id,
118 const SyncStatusCallback& callback);
120 void PostInitializeTask();
121 void DidInitialize(SyncEngineInitializer* initializer,
122 SyncStatusCode status);
123 void UpdateRegisteredApps();
124 static void QueryAppStatusOnUIThread(
125 const base::WeakPtr<ExtensionServiceInterface>& extension_service_ptr,
126 const std::vector<std::string>* app_ids,
127 AppStatusMap* status,
128 const base::Closure& callback);
129 void DidQueryAppStatus(const AppStatusMap* app_status);
130 void DidProcessRemoteChange(RemoteToLocalSyncer* syncer,
131 const SyncFileCallback& callback,
132 SyncStatusCode status);
133 void DidApplyLocalChange(LocalToRemoteSyncer* syncer,
134 const SyncStatusCallback& callback,
135 SyncStatusCode status);
137 // Returns true if a FetchChanges task is scheduled.
138 bool MaybeStartFetchChanges();
139 void DidResolveConflict(SyncStatusCode status);
140 void DidFetchChanges(SyncStatusCode status);
142 void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state,
143 bool used_network);
144 void UpdateServiceState(RemoteServiceState state,
145 const std::string& description);
147 void CallOnIdleForTesting(const base::Closure& callback);
149 drive::DriveServiceInterface* GetDriveService();
150 drive::DriveUploaderInterface* GetDriveUploader();
151 MetadataDatabase* GetMetadataDatabase();
153 base::FilePath base_dir_;
155 leveldb::Env* env_override_;
157 // Sync with SyncEngine.
158 RemoteServiceState service_state_;
160 bool should_check_conflict_;
161 bool should_check_remote_change_;
162 bool listing_remote_changes_;
163 base::TimeTicks time_to_check_changes_;
165 bool sync_enabled_;
166 base::Closure call_on_idle_callback_;
168 scoped_ptr<SyncTaskManager> task_manager_;
170 base::WeakPtr<ExtensionServiceInterface> extension_service_;
172 scoped_ptr<SyncEngineContext> context_;
173 ObserverList<Observer> observers_;
175 base::SequenceChecker sequence_checker_;
177 base::WeakPtrFactory<SyncWorker> weak_ptr_factory_;
178 DISALLOW_COPY_AND_ASSIGN(SyncWorker);
181 } // namespace drive_backend
182 } // namespace sync_file_system
184 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_WORKER_H_