OLD | NEW |
| (Empty) |
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_DRIVE_FILE_SYNC_SERVICE_H_ | |
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_DRIVE_FILE_SYNC_SERVICE_H_ | |
7 | |
8 #include <deque> | |
9 #include <map> | |
10 #include <set> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/callback.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/observer_list.h" | |
18 #include "base/threading/non_thread_safe.h" | |
19 #include "chrome/browser/drive/drive_notification_observer.h" | |
20 #include "chrome/browser/sync_file_system/conflict_resolution_resolver.h" | |
21 #include "chrome/browser/sync_file_system/drive_backend/api_util_interface.h" | |
22 #include "chrome/browser/sync_file_system/drive_backend/drive_metadata_store.h" | |
23 #include "chrome/browser/sync_file_system/drive_backend/local_sync_operation_res
olver.h" | |
24 #include "chrome/browser/sync_file_system/drive_backend/remote_change_handler.h" | |
25 #include "chrome/browser/sync_file_system/file_change.h" | |
26 #include "chrome/browser/sync_file_system/local_change_processor.h" | |
27 #include "chrome/browser/sync_file_system/remote_file_sync_service.h" | |
28 #include "chrome/browser/sync_file_system/sync_action.h" | |
29 #include "chrome/browser/sync_file_system/sync_callbacks.h" | |
30 #include "chrome/browser/sync_file_system/sync_direction.h" | |
31 #include "chrome/browser/sync_file_system/sync_file_system.pb.h" | |
32 #include "chrome/browser/sync_file_system/sync_status_code.h" | |
33 #include "chrome/browser/sync_file_system/sync_task_manager.h" | |
34 | |
35 class ExtensionService; | |
36 | |
37 namespace google_apis { | |
38 class ResourceList; | |
39 } | |
40 | |
41 namespace tracked_objects { | |
42 class Location; | |
43 } | |
44 | |
45 namespace sync_file_system { | |
46 | |
47 namespace drive_backend { | |
48 class LocalSyncDelegate; | |
49 class RemoteSyncDelegate; | |
50 } | |
51 | |
52 class SyncTaskManager; | |
53 | |
54 // Maintains remote file changes. | |
55 // Owned by SyncFileSystemService (which is a per-profile object). | |
56 class DriveFileSyncService : public RemoteFileSyncService, | |
57 public LocalChangeProcessor, | |
58 public drive_backend::APIUtilObserver, | |
59 public SyncTaskManager::Client, | |
60 public base::NonThreadSafe, | |
61 public base::SupportsWeakPtr<DriveFileSyncService>, | |
62 public drive::DriveNotificationObserver { | |
63 public: | |
64 typedef base::Callback<void(const SyncStatusCallback& callback)> Task; | |
65 | |
66 static ConflictResolutionPolicy kDefaultPolicy; | |
67 | |
68 virtual ~DriveFileSyncService(); | |
69 | |
70 // Creates DriveFileSyncService. | |
71 static scoped_ptr<DriveFileSyncService> Create(Profile* profile); | |
72 | |
73 // Creates DriveFileSyncService instance for testing. | |
74 // |metadata_store| must be initialized beforehand. | |
75 static scoped_ptr<DriveFileSyncService> CreateForTesting( | |
76 Profile* profile, | |
77 const base::FilePath& base_dir, | |
78 scoped_ptr<drive_backend::APIUtilInterface> api_util, | |
79 scoped_ptr<DriveMetadataStore> metadata_store); | |
80 | |
81 // Destroys |sync_service| and passes the ownership of |sync_client| to caller | |
82 // for testing. | |
83 static scoped_ptr<drive_backend::APIUtilInterface> | |
84 DestroyAndPassAPIUtilForTesting( | |
85 scoped_ptr<DriveFileSyncService> sync_service); | |
86 | |
87 // RemoteFileSyncService overrides. | |
88 virtual void AddServiceObserver(Observer* observer) OVERRIDE; | |
89 virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE; | |
90 virtual void RegisterOriginForTrackingChanges( | |
91 const GURL& origin, | |
92 const SyncStatusCallback& callback) OVERRIDE; | |
93 virtual void UnregisterOriginForTrackingChanges( | |
94 const GURL& origin, | |
95 const SyncStatusCallback& callback) OVERRIDE; | |
96 virtual void EnableOriginForTrackingChanges( | |
97 const GURL& origin, | |
98 const SyncStatusCallback& callback) OVERRIDE; | |
99 virtual void DisableOriginForTrackingChanges( | |
100 const GURL& origin, | |
101 const SyncStatusCallback& callback) OVERRIDE; | |
102 virtual void UninstallOrigin( | |
103 const GURL& origin, | |
104 const SyncStatusCallback& callback) OVERRIDE; | |
105 virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE; | |
106 virtual void SetRemoteChangeProcessor( | |
107 RemoteChangeProcessor* processor) OVERRIDE; | |
108 virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE; | |
109 virtual bool IsConflicting(const fileapi::FileSystemURL& url) OVERRIDE; | |
110 virtual RemoteServiceState GetCurrentState() const OVERRIDE; | |
111 virtual void GetOriginStatusMap(OriginStatusMap* status_map) OVERRIDE; | |
112 virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) OVERRIDE; | |
113 virtual void SetSyncEnabled(bool enabled) OVERRIDE; | |
114 virtual SyncStatusCode SetConflictResolutionPolicy( | |
115 ConflictResolutionPolicy policy) OVERRIDE; | |
116 virtual ConflictResolutionPolicy GetConflictResolutionPolicy() const OVERRIDE; | |
117 virtual void GetRemoteVersions( | |
118 const fileapi::FileSystemURL& url, | |
119 const RemoteVersionsCallback& callback) OVERRIDE; | |
120 virtual void DownloadRemoteVersion( | |
121 const fileapi::FileSystemURL& url, | |
122 const std::string& version_id, | |
123 const DownloadVersionCallback& callback) OVERRIDE; | |
124 | |
125 // LocalChangeProcessor overrides. | |
126 virtual void ApplyLocalChange( | |
127 const FileChange& change, | |
128 const base::FilePath& local_file_path, | |
129 const SyncFileMetadata& local_file_metadata, | |
130 const fileapi::FileSystemURL& url, | |
131 const SyncStatusCallback& callback) OVERRIDE; | |
132 | |
133 // DriveFileSyncClientObserver overrides. | |
134 virtual void OnAuthenticated() OVERRIDE; | |
135 virtual void OnNetworkConnected() OVERRIDE; | |
136 | |
137 // drive::DriveNotificationObserver implementation. | |
138 virtual void OnNotificationReceived() OVERRIDE; | |
139 virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE; | |
140 | |
141 // SyncTaskManager::Client overrides. | |
142 virtual void MaybeScheduleNextTask() OVERRIDE; | |
143 virtual void NotifyLastOperationStatus(SyncStatusCode sync_status) OVERRIDE; | |
144 | |
145 static std::string PathToTitle(const base::FilePath& path); | |
146 static base::FilePath TitleToPath(const std::string& title); | |
147 static DriveMetadata::ResourceType SyncFileTypeToDriveMetadataResourceType( | |
148 SyncFileType file_type); | |
149 static SyncFileType DriveMetadataResourceTypeToSyncFileType( | |
150 DriveMetadata::ResourceType resource_type); | |
151 | |
152 private: | |
153 friend class SyncTaskManager; | |
154 friend class drive_backend::LocalSyncDelegate; | |
155 friend class drive_backend::RemoteSyncDelegate; | |
156 | |
157 friend class DriveFileSyncServiceFakeTest; | |
158 friend class DriveFileSyncServiceSyncTest; | |
159 friend class DriveFileSyncServiceTest; | |
160 struct ApplyLocalChangeParam; | |
161 struct ProcessRemoteChangeParam; | |
162 | |
163 typedef base::Callback< | |
164 void(SyncStatusCode status, | |
165 const std::string& resource_id)> ResourceIdCallback; | |
166 | |
167 explicit DriveFileSyncService(Profile* profile); | |
168 | |
169 void Initialize(scoped_ptr<SyncTaskManager> task_manager, | |
170 const SyncStatusCallback& callback); | |
171 void InitializeForTesting( | |
172 scoped_ptr<SyncTaskManager> task_manager, | |
173 const base::FilePath& base_dir, | |
174 scoped_ptr<drive_backend::APIUtilInterface> sync_client, | |
175 scoped_ptr<DriveMetadataStore> metadata_store, | |
176 const SyncStatusCallback& callback); | |
177 | |
178 void DidInitializeMetadataStore(const SyncStatusCallback& callback, | |
179 SyncStatusCode status, | |
180 bool created); | |
181 void DidGetDriveRootResourceId(const SyncStatusCallback& callback, | |
182 google_apis::GDataErrorCode error, | |
183 const std::string& root_resource_id); | |
184 | |
185 void UpdateServiceStateFromLastOperationStatus( | |
186 SyncStatusCode sync_status, | |
187 google_apis::GDataErrorCode gdata_error); | |
188 | |
189 // Updates the service state. Also this may notify observers if the | |
190 // service state has been changed from the original value. | |
191 void UpdateServiceState(RemoteServiceState state, | |
192 const std::string& description); | |
193 | |
194 void DoRegisterOriginForTrackingChanges( | |
195 const GURL& origin, | |
196 const SyncStatusCallback& callback); | |
197 void DoUnregisterOriginForTrackingChanges( | |
198 const GURL& origin, | |
199 const SyncStatusCallback& callback); | |
200 void DoEnableOriginForTrackingChanges( | |
201 const GURL& origin, | |
202 const SyncStatusCallback& callback); | |
203 void DoDisableOriginForTrackingChanges( | |
204 const GURL& origin, | |
205 const SyncStatusCallback& callback); | |
206 void DoUninstallOrigin( | |
207 const GURL& origin, | |
208 const SyncStatusCallback& callback); | |
209 void DoProcessRemoteChange( | |
210 const SyncFileCallback& sync_callback, | |
211 const SyncStatusCallback& completion_callback); | |
212 void DoApplyLocalChange( | |
213 const FileChange& change, | |
214 const base::FilePath& local_file_path, | |
215 const SyncFileMetadata& local_file_metadata, | |
216 const fileapi::FileSystemURL& url, | |
217 const SyncStatusCallback& callback); | |
218 | |
219 void DoGetRemoteVersions( | |
220 const fileapi::FileSystemURL& url, | |
221 const RemoteVersionsCallback& callback, | |
222 const SyncStatusCallback& completion_callback); | |
223 void DidGetEntryForRemoteVersions( | |
224 const RemoteVersionsCallback& callback, | |
225 google_apis::GDataErrorCode error, | |
226 scoped_ptr<google_apis::ResourceEntry> entry); | |
227 | |
228 void DoDownloadRemoteVersion( | |
229 const fileapi::FileSystemURL& url, | |
230 const std::string& version_id, | |
231 const DownloadVersionCallback& callback, | |
232 const SyncStatusCallback& completion_callback); | |
233 void DidDownloadVersion( | |
234 const DownloadVersionCallback& download_callback, | |
235 google_apis::GDataErrorCode error, | |
236 const std::string& file_md5, | |
237 int64 file_size, | |
238 const base::Time& last_updated, | |
239 scoped_ptr<webkit_blob::ScopedFile> downloaded); | |
240 | |
241 void UpdateRegisteredOrigins(); | |
242 | |
243 void StartBatchSync(const SyncStatusCallback& callback); | |
244 void GetDriveDirectoryForOrigin(const GURL& origin, | |
245 const SyncStatusCallback& callback, | |
246 const std::string& sync_root_resource_id); | |
247 void DidGetDriveDirectoryForOrigin(const GURL& origin, | |
248 const SyncStatusCallback& callback, | |
249 SyncStatusCode status, | |
250 const std::string& resource_id); | |
251 void DidUninstallOrigin(const GURL& origin, | |
252 const SyncStatusCallback& callback, | |
253 google_apis::GDataErrorCode error); | |
254 void DidGetLargestChangeStampForBatchSync( | |
255 const SyncStatusCallback& callback, | |
256 const GURL& origin, | |
257 const std::string& resource_id, | |
258 google_apis::GDataErrorCode error, | |
259 int64 largest_changestamp); | |
260 void DidGetDirectoryContentForBatchSync( | |
261 const SyncStatusCallback& callback, | |
262 const GURL& origin, | |
263 const std::string& resource_id, | |
264 int64 largest_changestamp, | |
265 google_apis::GDataErrorCode error, | |
266 scoped_ptr<google_apis::ResourceList> feed); | |
267 | |
268 void DidProcessRemoteChange(const SyncFileCallback& sync_callback, | |
269 const SyncStatusCallback& completion_callback, | |
270 SyncStatusCode status); | |
271 void DidApplyLocalChange(const SyncStatusCallback& callback, | |
272 SyncStatusCode status); | |
273 | |
274 // Returns true if |pending_changes_| was updated. | |
275 bool AppendRemoteChange( | |
276 const GURL& origin, | |
277 const google_apis::ResourceEntry& entry, | |
278 int64 changestamp); | |
279 bool AppendFetchChange( | |
280 const GURL& origin, | |
281 const base::FilePath& path, | |
282 const std::string& resource_id, | |
283 SyncFileType file_type); | |
284 bool AppendRemoteChangeInternal( | |
285 const GURL& origin, | |
286 const base::FilePath& path, | |
287 bool is_deleted, | |
288 const std::string& resource_id, | |
289 int64 changestamp, | |
290 const std::string& remote_file_md5, | |
291 const base::Time& updated_time, | |
292 SyncFileType file_type); | |
293 void RemoveRemoteChange(const fileapi::FileSystemURL& url); | |
294 void MaybeMarkAsIncrementalSyncOrigin(const GURL& origin); | |
295 | |
296 // TODO(kinuko,tzik): Move this out of DriveFileSyncService. | |
297 void MarkConflict( | |
298 const fileapi::FileSystemURL& url, | |
299 DriveMetadata* drive_metadata, | |
300 const SyncStatusCallback& callback); | |
301 void NotifyConflict( | |
302 const fileapi::FileSystemURL& url, | |
303 const SyncStatusCallback& callback, | |
304 SyncStatusCode status); | |
305 | |
306 // A wrapper implementation to GDataErrorCodeToSyncStatusCode which returns | |
307 // authentication error if the user is not signed in. | |
308 SyncStatusCode GDataErrorCodeToSyncStatusCodeWrapper( | |
309 google_apis::GDataErrorCode error); | |
310 | |
311 base::FilePath temporary_file_dir_; | |
312 | |
313 // May start batch sync or incremental sync. | |
314 // This posts either one of following tasks: | |
315 // - StartBatchSyncForOrigin() if it has any pending batch sync origins, or | |
316 // - FetchChangesForIncrementalSync() otherwise. | |
317 // | |
318 // These two methods are called only from this method. | |
319 void MaybeStartFetchChanges(); | |
320 | |
321 void FetchChangesForIncrementalSync(const SyncStatusCallback& callback); | |
322 void DidFetchChangesForIncrementalSync( | |
323 const SyncStatusCallback& callback, | |
324 bool has_new_changes, | |
325 google_apis::GDataErrorCode error, | |
326 scoped_ptr<google_apis::ResourceList> changes); | |
327 bool GetOriginForEntry(const google_apis::ResourceEntry& entry, GURL* origin); | |
328 void NotifyObserversFileStatusChanged(const fileapi::FileSystemURL& url, | |
329 SyncFileStatus sync_status, | |
330 SyncAction action_taken, | |
331 SyncDirection direction); | |
332 | |
333 void HandleSyncRootDirectoryChange(const google_apis::ResourceEntry& entry); | |
334 void HandleOriginRootDirectoryChange(const google_apis::ResourceEntry& entry); | |
335 | |
336 void EnsureSyncRootDirectory(const ResourceIdCallback& callback); | |
337 void DidEnsureSyncRoot(const ResourceIdCallback& callback, | |
338 google_apis::GDataErrorCode error, | |
339 const std::string& sync_root_resource_id); | |
340 void EnsureOriginRootDirectory(const GURL& origin, | |
341 const ResourceIdCallback& callback); | |
342 void DidEnsureSyncRootForOriginRoot(const GURL& origin, | |
343 const ResourceIdCallback& callback, | |
344 SyncStatusCode status, | |
345 const std::string& sync_root_resource_id); | |
346 void DidEnsureOriginRoot(const GURL& origin, | |
347 const ResourceIdCallback& callback, | |
348 google_apis::GDataErrorCode error, | |
349 const std::string& resource_id); | |
350 | |
351 // This function returns Resouce ID for the sync root directory if available. | |
352 // Returns an empty string 1) when the resource ID has not been initialized | |
353 // yet, and 2) after the service has detected the remote sync root folder was | |
354 // removed. | |
355 std::string sync_root_resource_id(); | |
356 | |
357 scoped_ptr<DriveMetadataStore> metadata_store_; | |
358 scoped_ptr<drive_backend::APIUtilInterface> api_util_; | |
359 | |
360 Profile* profile_; | |
361 | |
362 scoped_ptr<SyncTaskManager> task_manager_; | |
363 | |
364 scoped_ptr<drive_backend::LocalSyncDelegate> running_local_sync_task_; | |
365 scoped_ptr<drive_backend::RemoteSyncDelegate> running_remote_sync_task_; | |
366 | |
367 // The current remote service state. This does NOT reflect the | |
368 // sync_enabled_ flag, while GetCurrentState() DOES reflect the flag | |
369 // value (i.e. it returns REMOTE_SERVICE_DISABLED when sync_enabled_ | |
370 // is false even if state_ is REMOTE_SERVICE_OK). | |
371 RemoteServiceState state_; | |
372 | |
373 // Indicates if sync is enabled or not. This flag can be turned on or | |
374 // off by SetSyncEnabled() method. To start synchronization | |
375 // this needs to be true and state_ needs to be REMOTE_SERVICE_OK. | |
376 bool sync_enabled_; | |
377 | |
378 int64 largest_fetched_changestamp_; | |
379 | |
380 std::map<GURL, std::string> pending_batch_sync_origins_; | |
381 | |
382 // Is set to true when there's a fair possibility that we have some | |
383 // remote changes that haven't been fetched yet. | |
384 // | |
385 // This flag is set when: | |
386 // - This gets invalidation notification, | |
387 // - The service is authenticated or becomes online, and | |
388 // - The polling timer is fired. | |
389 // | |
390 // This flag is cleared when: | |
391 // - A batch or incremental sync has been started, and | |
392 // - When all pending batch sync tasks have been finished. | |
393 bool may_have_unfetched_changes_; | |
394 | |
395 ObserverList<Observer> service_observers_; | |
396 ObserverList<FileStatusObserver> file_status_observers_; | |
397 | |
398 RemoteChangeHandler remote_change_handler_; | |
399 RemoteChangeProcessor* remote_change_processor_; | |
400 | |
401 google_apis::GDataErrorCode last_gdata_error_; | |
402 | |
403 ConflictResolutionResolver conflict_resolution_resolver_; | |
404 | |
405 DISALLOW_COPY_AND_ASSIGN(DriveFileSyncService); | |
406 }; | |
407 | |
408 } // namespace sync_file_system | |
409 | |
410 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_DRIVE_FILE_SYNC_SERVICE
_H_ | |
OLD | NEW |