Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / remote_to_local_syncer.h
blob5609c17d6adcfc24803f91b847ce79ead6ee9479
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.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_TO_LOCAL_SYNCER_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_TO_LOCAL_SYNCER_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
13 #include "chrome/browser/sync_file_system/drive_backend/sync_task.h"
14 #include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h"
15 #include "chrome/browser/sync_file_system/remote_change_processor.h"
16 #include "chrome/browser/sync_file_system/sync_action.h"
17 #include "chrome/browser/sync_file_system/sync_callbacks.h"
18 #include "chrome/browser/sync_file_system/sync_file_metadata.h"
19 #include "google_apis/drive/drive_api_error_codes.h"
20 #include "storage/browser/fileapi/file_system_url.h"
22 namespace drive {
23 class DriveServiceInterface;
26 namespace google_apis {
27 class FileList;
28 class FileResource;
31 namespace storage {
32 class ScopedFile;
35 namespace sync_file_system {
36 namespace drive_backend {
38 class MetadataDatabase;
39 class SyncEngineContext;
41 class RemoteToLocalSyncer : public SyncTask {
42 public:
43 typedef SyncTaskManager::Continuation Continuation;
45 // Conflicting trackers will have low priority for RemoteToLocalSyncer so that
46 // it should be resolved by LocatToRemoteSyncer.
47 explicit RemoteToLocalSyncer(SyncEngineContext* sync_context);
48 ~RemoteToLocalSyncer() override;
50 void RunPreflight(scoped_ptr<SyncTaskToken> token) override;
51 void RunExclusive(scoped_ptr<SyncTaskToken> token);
53 const storage::FileSystemURL& url() const { return url_; }
54 SyncFileType file_type() const { return file_type_; }
55 SyncAction sync_action() const { return sync_action_; }
57 bool is_sync_root_deletion() const { return sync_root_deletion_; }
59 private:
60 typedef std::vector<std::string> FileIDList;
62 // TODO(tzik): Update documentation here.
64 // Dispatches remote change to handlers or to SyncCompleted() directly.
65 // This function uses information only in MetadataDatabase.
67 // If the tracker doesn't have remote metadata:
68 // # The file is listed in a folder right before this operation.
69 // - Dispatch to HandleMissingRemoteMetadata to fetch remote metadata.
70 // Else, if the tracker is not active or the dominating app-root is disabled:
71 // # Assume the file has remote metadata.
72 // - Update the tracker with |missing| flag and empty |md5|.
73 // Note: MetadataDatabase may activate the tracker if possible.
74 // Else, if the tracker doesn't have synced metadata:
75 // # Assume the tracker has remote metadata and the tracker is active.
76 // # The tracker is not yet synced ever.
77 // - If the file is remotely deleted, do nothing to local file and dispatch
78 // directly to SyncCompleted().
79 // - Else, if the file is a regular file, dispatch to HandleNewFile().
80 // - Else, if the file is a folder, dispatch to HandleFolderUpdate().
81 // - Else, the file should be an unsupported active file. This should not
82 // happen.
83 // Else, if the remote metadata is marked as deleted:
84 // # Most of the remote metadata is missing including title, kind and md5.
85 // - Dispatch to HandleDeletion().
86 // Else, if the tracker has different titles between its synced metadata and
87 // remote metadata:
88 // # Assume the tracker is active and has remote metetadata and synced
89 // metadata.
90 // # The file is remotely renamed.
91 // # Maybe, this can be decomposed to delete and update.
92 // - Dispatch to HandleRemoteRename().
93 // Else, if the tracker's parent is not a parent of the remote metadata:
94 // # The file has reorganized.
95 // # Maybe, this can be decomposed to delete and update.
96 // - Dispatch to HandreReorganize().
97 // Else, if the folder is a regular file and the md5 in remote metadata does
98 // not match the md5 in synced metadata:
99 // # The file is modified remotely.
100 // - Dispatch to HandleContentUpdate().
101 // Else, if the tracker is a folder and it has needs_folder_listing flag:
102 // - Dispatch to HandleFolderContentListing()
103 // Else, there should be no change to sync.
104 // - Dispatch to HandleOfflineSolvable()
105 void ResolveRemoteChange(scoped_ptr<SyncTaskToken> token);
107 void MoveToBackground(scoped_ptr<SyncTaskToken> token,
108 const Continuation& continuation);
109 void ContinueAsBackgroundTask(const Continuation& continuation,
110 scoped_ptr<SyncTaskToken> token);
112 // Handles missing remote metadata case.
113 // Fetches remote metadata and updates MetadataDatabase by that. The sync
114 // operation itself will be deferred to the next sync round.
115 // Note: if the file is not found, it should be handled as if deleted.
116 void HandleMissingRemoteMetadata(scoped_ptr<SyncTaskToken> token);
117 void DidGetRemoteMetadata(scoped_ptr<SyncTaskToken> token,
118 google_apis::DriveApiErrorCode error,
119 scoped_ptr<google_apis::FileResource> entry);
121 // This implements the body of the HandleNewFile and HandleContentUpdate.
122 // If the file doesn't have corresponding local file:
123 // - Dispatch to DownloadFile.
124 // Else, if the local file doesn't have local change:
125 // - Dispatch to DownloadFile if the local file is a regular file.
126 // - If the local file is a folder, handle this case as a conflict. Lower
127 // the priority of the tracker, and defer further handling to
128 // local-to-remote change.
129 // Else:
130 // # The file has local modification.
131 // - Handle this case as a conflict. Lower the priority of the tracker, and
132 // defer further handling to local-to-remote change.
133 void DidPrepareForAddOrUpdateFile(scoped_ptr<SyncTaskToken> token,
134 SyncStatusCode status);
136 // Handles remotely added folder. Needs Prepare() call.
137 // TODO(tzik): Write details and implement this.
138 void HandleFolderUpdate(scoped_ptr<SyncTaskToken> token);
139 void DidPrepareForFolderUpdate(scoped_ptr<SyncTaskToken> token,
140 SyncStatusCode status);
142 // Handles deleted remote file. Needs Prepare() call.
143 // If the deleted tracker is the sync-root:
144 // - TODO(tzik): Needs special handling.
145 // Else, if the deleted tracker is a app-root:
146 // - TODO(tzik): Needs special handling.
147 // Else, if the local file is already deleted:
148 // - Do nothing anymore to the local, call SyncCompleted().
149 // Else, if the local file is modified:
150 // - Do nothing to the local file, call SyncCompleted().
151 // Else, if the local file is not modified:
152 // - Delete local file.
153 // # Note: if the local file is a folder, delete recursively.
154 void HandleDeletion(scoped_ptr<SyncTaskToken> token);
155 void DidPrepareForDeletion(scoped_ptr<SyncTaskToken> token,
156 SyncStatusCode status);
158 void HandleFileMove(scoped_ptr<SyncTaskToken> token);
160 // Handles new file. Needs Prepare() call.
161 void HandleContentUpdate(scoped_ptr<SyncTaskToken> token);
163 void ListFolderContent(scoped_ptr<SyncTaskToken> token);
164 void DidListFolderContent(
165 scoped_ptr<SyncTaskToken> token,
166 scoped_ptr<FileIDList> children,
167 google_apis::DriveApiErrorCode error,
168 scoped_ptr<google_apis::FileList> file_list);
170 void SyncCompleted(scoped_ptr<SyncTaskToken> token, SyncStatusCode status);
171 void FinalizeSync(scoped_ptr<SyncTaskToken> token, SyncStatusCode status);
173 void Prepare(const SyncStatusCallback& callback);
174 void DidPrepare(const SyncStatusCallback& callback,
175 SyncStatusCode status,
176 const SyncFileMetadata& metadata,
177 const FileChangeList& changes);
179 void DeleteLocalFile(scoped_ptr<SyncTaskToken> token);
180 void DownloadFile(scoped_ptr<SyncTaskToken> token);
181 void DidDownloadFile(scoped_ptr<SyncTaskToken> token,
182 storage::ScopedFile file,
183 google_apis::DriveApiErrorCode error,
184 const base::FilePath&);
185 void DidApplyDownload(scoped_ptr<SyncTaskToken> token,
186 storage::ScopedFile,
187 SyncStatusCode status);
189 void CreateFolder(scoped_ptr<SyncTaskToken> token);
191 // TODO(tzik): After we convert all callbacks to token-passing style,
192 // drop this function.
193 SyncStatusCallback SyncCompletedCallback(scoped_ptr<SyncTaskToken> token);
195 drive::DriveServiceInterface* drive_service();
196 MetadataDatabase* metadata_database();
197 RemoteChangeProcessor* remote_change_processor();
199 SyncEngineContext* sync_context_; // Not owned.
201 scoped_ptr<FileTracker> dirty_tracker_;
202 scoped_ptr<FileMetadata> remote_metadata_;
204 storage::FileSystemURL url_;
205 SyncFileType file_type_;
206 SyncAction sync_action_;
208 bool prepared_;
209 bool sync_root_deletion_;
211 scoped_ptr<SyncFileMetadata> local_metadata_;
212 scoped_ptr<FileChangeList> local_changes_;
214 base::WeakPtrFactory<RemoteToLocalSyncer> weak_ptr_factory_;
216 DISALLOW_COPY_AND_ASSIGN(RemoteToLocalSyncer);
219 } // namespace drive_backend
220 } // namespace sync_file_system
222 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_REMOTE_TO_LOCAL_SYNCER_H_