| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync_file_system/drive_backend/local_to_remote_syncer.h
" | 5 #include "chrome/browser/sync_file_system/drive_backend/local_to_remote_syncer.h
" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 16 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
| 17 #include "chrome/browser/sync_file_system/drive_backend/callback_helper.h" | 18 #include "chrome/browser/sync_file_system/drive_backend/callback_helper.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 33 | 34 |
| 34 namespace sync_file_system { | 35 namespace sync_file_system { |
| 35 namespace drive_backend { | 36 namespace drive_backend { |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 scoped_ptr<FileTracker> FindTrackerByID(MetadataDatabase* metadata_database, | 40 scoped_ptr<FileTracker> FindTrackerByID(MetadataDatabase* metadata_database, |
| 40 int64_t tracker_id) { | 41 int64_t tracker_id) { |
| 41 scoped_ptr<FileTracker> tracker(new FileTracker); | 42 scoped_ptr<FileTracker> tracker(new FileTracker); |
| 42 if (metadata_database->FindTrackerByTrackerID(tracker_id, tracker.get())) | 43 if (metadata_database->FindTrackerByTrackerID(tracker_id, tracker.get())) |
| 43 return tracker.Pass(); | 44 return tracker; |
| 44 return scoped_ptr<FileTracker>(); | 45 return scoped_ptr<FileTracker>(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 bool GetKnownChangeID(MetadataDatabase* metadata_database, | 48 bool GetKnownChangeID(MetadataDatabase* metadata_database, |
| 48 const std::string& file_id, | 49 const std::string& file_id, |
| 49 int64_t* change_id) { | 50 int64_t* change_id) { |
| 50 FileMetadata remote_file_metadata; | 51 FileMetadata remote_file_metadata; |
| 51 if (!metadata_database->FindFileByFileID(file_id, &remote_file_metadata)) | 52 if (!metadata_database->FindFileByFileID(file_id, &remote_file_metadata)) |
| 52 return false; | 53 return false; |
| 53 *change_id = remote_file_metadata.details().change_id(); | 54 *change_id = remote_file_metadata.details().change_id(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 94 } |
| 94 | 95 |
| 95 LocalToRemoteSyncer::~LocalToRemoteSyncer() { | 96 LocalToRemoteSyncer::~LocalToRemoteSyncer() { |
| 96 } | 97 } |
| 97 | 98 |
| 98 void LocalToRemoteSyncer::RunPreflight(scoped_ptr<SyncTaskToken> token) { | 99 void LocalToRemoteSyncer::RunPreflight(scoped_ptr<SyncTaskToken> token) { |
| 99 token->InitializeTaskLog("Local -> Remote"); | 100 token->InitializeTaskLog("Local -> Remote"); |
| 100 | 101 |
| 101 if (!IsContextReady()) { | 102 if (!IsContextReady()) { |
| 102 token->RecordLog("Context not ready."); | 103 token->RecordLog("Context not ready."); |
| 103 SyncTaskManager::NotifyTaskDone(token.Pass(), SYNC_STATUS_FAILED); | 104 SyncTaskManager::NotifyTaskDone(std::move(token), SYNC_STATUS_FAILED); |
| 104 return; | 105 return; |
| 105 } | 106 } |
| 106 | 107 |
| 107 token->RecordLog(base::StringPrintf( | 108 token->RecordLog(base::StringPrintf( |
| 108 "Start: %s on %s@%s %s", | 109 "Start: %s on %s@%s %s", |
| 109 local_change_.DebugString().c_str(), | 110 local_change_.DebugString().c_str(), |
| 110 url_.path().AsUTF8Unsafe().c_str(), | 111 url_.path().AsUTF8Unsafe().c_str(), |
| 111 url_.origin().host().c_str(), | 112 url_.origin().host().c_str(), |
| 112 local_is_missing_ ? "(missing)" : "")); | 113 local_is_missing_ ? "(missing)" : "")); |
| 113 | 114 |
| 114 if (local_is_missing_ && !local_change_.IsDelete()) { | 115 if (local_is_missing_ && !local_change_.IsDelete()) { |
| 115 // Stray file, we can just return. | 116 // Stray file, we can just return. |
| 116 token->RecordLog("Missing file for non-delete change."); | 117 token->RecordLog("Missing file for non-delete change."); |
| 117 SyncTaskManager::NotifyTaskDone(token.Pass(), SYNC_STATUS_OK); | 118 SyncTaskManager::NotifyTaskDone(std::move(token), SYNC_STATUS_OK); |
| 118 return; | 119 return; |
| 119 } | 120 } |
| 120 | 121 |
| 121 std::string app_id = url_.origin().host(); | 122 std::string app_id = url_.origin().host(); |
| 122 base::FilePath path = url_.path(); | 123 base::FilePath path = url_.path(); |
| 123 | 124 |
| 124 scoped_ptr<FileTracker> active_ancestor_tracker(new FileTracker); | 125 scoped_ptr<FileTracker> active_ancestor_tracker(new FileTracker); |
| 125 base::FilePath active_ancestor_path; | 126 base::FilePath active_ancestor_path; |
| 126 if (!metadata_database()->FindNearestActiveAncestor( | 127 if (!metadata_database()->FindNearestActiveAncestor( |
| 127 app_id, path, | 128 app_id, path, |
| 128 active_ancestor_tracker.get(), &active_ancestor_path)) { | 129 active_ancestor_tracker.get(), &active_ancestor_path)) { |
| 129 // The app is disabled or not registered. | 130 // The app is disabled or not registered. |
| 130 token->RecordLog("App is disabled or not registered"); | 131 token->RecordLog("App is disabled or not registered"); |
| 131 SyncTaskManager::NotifyTaskDone(token.Pass(), SYNC_STATUS_UNKNOWN_ORIGIN); | 132 SyncTaskManager::NotifyTaskDone(std::move(token), |
| 133 SYNC_STATUS_UNKNOWN_ORIGIN); |
| 132 return; | 134 return; |
| 133 } | 135 } |
| 134 DCHECK(active_ancestor_tracker->active()); | 136 DCHECK(active_ancestor_tracker->active()); |
| 135 DCHECK(active_ancestor_tracker->has_synced_details()); | 137 DCHECK(active_ancestor_tracker->has_synced_details()); |
| 136 const FileDetails& active_ancestor_details = | 138 const FileDetails& active_ancestor_details = |
| 137 active_ancestor_tracker->synced_details(); | 139 active_ancestor_tracker->synced_details(); |
| 138 | 140 |
| 139 // TODO(tzik): Consider handling | 141 // TODO(tzik): Consider handling |
| 140 // active_ancestor_tracker->synced_details().missing() case. | 142 // active_ancestor_tracker->synced_details().missing() case. |
| 141 | 143 |
| 142 DCHECK(active_ancestor_details.file_kind() == FILE_KIND_FILE || | 144 DCHECK(active_ancestor_details.file_kind() == FILE_KIND_FILE || |
| 143 active_ancestor_details.file_kind() == FILE_KIND_FOLDER); | 145 active_ancestor_details.file_kind() == FILE_KIND_FOLDER); |
| 144 | 146 |
| 145 base::FilePath missing_entries; | 147 base::FilePath missing_entries; |
| 146 if (active_ancestor_path.empty()) { | 148 if (active_ancestor_path.empty()) { |
| 147 missing_entries = path; | 149 missing_entries = path; |
| 148 } else if (active_ancestor_path != path) { | 150 } else if (active_ancestor_path != path) { |
| 149 if (!active_ancestor_path.AppendRelativePath(path, &missing_entries)) { | 151 if (!active_ancestor_path.AppendRelativePath(path, &missing_entries)) { |
| 150 NOTREACHED(); | 152 NOTREACHED(); |
| 151 token->RecordLog( | 153 token->RecordLog( |
| 152 base::StringPrintf("Detected invalid ancestor: %" PRIsFP, | 154 base::StringPrintf("Detected invalid ancestor: %" PRIsFP, |
| 153 active_ancestor_path.value().c_str())); | 155 active_ancestor_path.value().c_str())); |
| 154 SyncTaskManager::NotifyTaskDone(token.Pass(), SYNC_STATUS_FAILED); | 156 SyncTaskManager::NotifyTaskDone(std::move(token), SYNC_STATUS_FAILED); |
| 155 return; | 157 return; |
| 156 } | 158 } |
| 157 } | 159 } |
| 158 | 160 |
| 159 std::vector<base::FilePath::StringType> missing_components; | 161 std::vector<base::FilePath::StringType> missing_components; |
| 160 storage::VirtualPath::GetComponents(missing_entries, &missing_components); | 162 storage::VirtualPath::GetComponents(missing_entries, &missing_components); |
| 161 | 163 |
| 162 if (!missing_components.empty()) { | 164 if (!missing_components.empty()) { |
| 163 if (local_is_missing_) { | 165 if (local_is_missing_) { |
| 164 token->RecordLog("Both local and remote are marked missing"); | 166 token->RecordLog("Both local and remote are marked missing"); |
| 165 // !IsDelete() but SYNC_FILE_TYPE_UNKNOWN could happen when a file is | 167 // !IsDelete() but SYNC_FILE_TYPE_UNKNOWN could happen when a file is |
| 166 // deleted by recursive deletion (which is not recorded by tracker) | 168 // deleted by recursive deletion (which is not recorded by tracker) |
| 167 // but there're remaining changes for the same file in the tracker. | 169 // but there're remaining changes for the same file in the tracker. |
| 168 | 170 |
| 169 // Local file is deleted and remote file is missing, already deleted or | 171 // Local file is deleted and remote file is missing, already deleted or |
| 170 // not yet synced. There is nothing to do for the file. | 172 // not yet synced. There is nothing to do for the file. |
| 171 SyncTaskManager::NotifyTaskDone(token.Pass(), SYNC_STATUS_OK); | 173 SyncTaskManager::NotifyTaskDone(std::move(token), SYNC_STATUS_OK); |
| 172 return; | 174 return; |
| 173 } | 175 } |
| 174 } | 176 } |
| 175 | 177 |
| 176 if (missing_components.size() > 1) { | 178 if (missing_components.size() > 1) { |
| 177 // The original target doesn't have remote file and parent. | 179 // The original target doesn't have remote file and parent. |
| 178 // Try creating the parent first. | 180 // Try creating the parent first. |
| 179 if (active_ancestor_details.file_kind() == FILE_KIND_FOLDER) { | 181 if (active_ancestor_details.file_kind() == FILE_KIND_FOLDER) { |
| 180 remote_parent_folder_tracker_ = active_ancestor_tracker.Pass(); | 182 remote_parent_folder_tracker_ = std::move(active_ancestor_tracker); |
| 181 target_path_ = active_ancestor_path.Append(missing_components[0]); | 183 target_path_ = active_ancestor_path.Append(missing_components[0]); |
| 182 token->RecordLog("Detected missing parent folder."); | 184 token->RecordLog("Detected missing parent folder."); |
| 183 | 185 |
| 184 retry_on_success_ = true; | 186 retry_on_success_ = true; |
| 185 MoveToBackground(base::Bind(&LocalToRemoteSyncer::CreateRemoteFolder, | 187 MoveToBackground(base::Bind(&LocalToRemoteSyncer::CreateRemoteFolder, |
| 186 weak_ptr_factory_.GetWeakPtr()), | 188 weak_ptr_factory_.GetWeakPtr()), |
| 187 token.Pass()); | 189 std::move(token)); |
| 188 return; | 190 return; |
| 189 } | 191 } |
| 190 | 192 |
| 191 DCHECK(active_ancestor_details.file_kind() == FILE_KIND_FILE); | 193 DCHECK(active_ancestor_details.file_kind() == FILE_KIND_FILE); |
| 192 remote_parent_folder_tracker_ = | 194 remote_parent_folder_tracker_ = |
| 193 FindTrackerByID(metadata_database(), | 195 FindTrackerByID(metadata_database(), |
| 194 active_ancestor_tracker->parent_tracker_id()); | 196 active_ancestor_tracker->parent_tracker_id()); |
| 195 remote_file_tracker_ = active_ancestor_tracker.Pass(); | 197 remote_file_tracker_ = std::move(active_ancestor_tracker); |
| 196 target_path_ = active_ancestor_path; | 198 target_path_ = active_ancestor_path; |
| 197 token->RecordLog("Detected non-folder file in its path."); | 199 token->RecordLog("Detected non-folder file in its path."); |
| 198 | 200 |
| 199 retry_on_success_ = true; | 201 retry_on_success_ = true; |
| 200 MoveToBackground(base::Bind(&LocalToRemoteSyncer::DeleteRemoteFile, | 202 MoveToBackground(base::Bind(&LocalToRemoteSyncer::DeleteRemoteFile, |
| 201 weak_ptr_factory_.GetWeakPtr()), | 203 weak_ptr_factory_.GetWeakPtr()), |
| 202 token.Pass()); | 204 std::move(token)); |
| 203 return; | 205 return; |
| 204 } | 206 } |
| 205 | 207 |
| 206 if (missing_components.empty()) { | 208 if (missing_components.empty()) { |
| 207 // The original target has remote active file/folder. | 209 // The original target has remote active file/folder. |
| 208 remote_parent_folder_tracker_ = | 210 remote_parent_folder_tracker_ = |
| 209 FindTrackerByID(metadata_database(), | 211 FindTrackerByID(metadata_database(), |
| 210 active_ancestor_tracker->parent_tracker_id()); | 212 active_ancestor_tracker->parent_tracker_id()); |
| 211 remote_file_tracker_ = active_ancestor_tracker.Pass(); | 213 remote_file_tracker_ = std::move(active_ancestor_tracker); |
| 212 target_path_ = url_.path(); | 214 target_path_ = url_.path(); |
| 213 DCHECK(target_path_ == active_ancestor_path); | 215 DCHECK(target_path_ == active_ancestor_path); |
| 214 | 216 |
| 215 if (remote_file_tracker_->dirty()) { | 217 if (remote_file_tracker_->dirty()) { |
| 216 token->RecordLog(base::StringPrintf( | 218 token->RecordLog(base::StringPrintf( |
| 217 "Detected conflicting dirty tracker:%" PRId64, | 219 "Detected conflicting dirty tracker:%" PRId64, |
| 218 remote_file_tracker_->tracker_id())); | 220 remote_file_tracker_->tracker_id())); |
| 219 // Both local and remote file has pending modification. | 221 // Both local and remote file has pending modification. |
| 220 HandleConflict(token.Pass()); | 222 HandleConflict(std::move(token)); |
| 221 return; | 223 return; |
| 222 } | 224 } |
| 223 | 225 |
| 224 // Non-conflicting file/folder update case. | 226 // Non-conflicting file/folder update case. |
| 225 HandleExistingRemoteFile(token.Pass()); | 227 HandleExistingRemoteFile(std::move(token)); |
| 226 return; | 228 return; |
| 227 } | 229 } |
| 228 | 230 |
| 229 DCHECK(local_change_.IsAddOrUpdate()); | 231 DCHECK(local_change_.IsAddOrUpdate()); |
| 230 DCHECK_EQ(1u, missing_components.size()); | 232 DCHECK_EQ(1u, missing_components.size()); |
| 231 // The original target has remote parent folder and doesn't have remote active | 233 // The original target has remote parent folder and doesn't have remote active |
| 232 // file. | 234 // file. |
| 233 // Upload the file as a new file or create a folder. | 235 // Upload the file as a new file or create a folder. |
| 234 remote_parent_folder_tracker_ = active_ancestor_tracker.Pass(); | 236 remote_parent_folder_tracker_ = std::move(active_ancestor_tracker); |
| 235 target_path_ = url_.path(); | 237 target_path_ = url_.path(); |
| 236 DCHECK(target_path_ == active_ancestor_path.Append(missing_components[0])); | 238 DCHECK(target_path_ == active_ancestor_path.Append(missing_components[0])); |
| 237 if (local_change_.file_type() == SYNC_FILE_TYPE_FILE) { | 239 if (local_change_.file_type() == SYNC_FILE_TYPE_FILE) { |
| 238 token->RecordLog("Detected a new file."); | 240 token->RecordLog("Detected a new file."); |
| 239 MoveToBackground(base::Bind(&LocalToRemoteSyncer::UploadNewFile, | 241 MoveToBackground(base::Bind(&LocalToRemoteSyncer::UploadNewFile, |
| 240 weak_ptr_factory_.GetWeakPtr()), | 242 weak_ptr_factory_.GetWeakPtr()), |
| 241 token.Pass()); | 243 std::move(token)); |
| 242 return; | 244 return; |
| 243 } | 245 } |
| 244 | 246 |
| 245 token->RecordLog("Detected a new folder."); | 247 token->RecordLog("Detected a new folder."); |
| 246 MoveToBackground(base::Bind(&LocalToRemoteSyncer::CreateRemoteFolder, | 248 MoveToBackground(base::Bind(&LocalToRemoteSyncer::CreateRemoteFolder, |
| 247 weak_ptr_factory_.GetWeakPtr()), | 249 weak_ptr_factory_.GetWeakPtr()), |
| 248 token.Pass()); | 250 std::move(token)); |
| 249 } | 251 } |
| 250 | 252 |
| 251 void LocalToRemoteSyncer::MoveToBackground(const Continuation& continuation, | 253 void LocalToRemoteSyncer::MoveToBackground(const Continuation& continuation, |
| 252 scoped_ptr<SyncTaskToken> token) { | 254 scoped_ptr<SyncTaskToken> token) { |
| 253 scoped_ptr<TaskBlocker> blocker(new TaskBlocker); | 255 scoped_ptr<TaskBlocker> blocker(new TaskBlocker); |
| 254 blocker->app_id = url_.origin().host(); | 256 blocker->app_id = url_.origin().host(); |
| 255 blocker->paths.push_back(target_path_); | 257 blocker->paths.push_back(target_path_); |
| 256 | 258 |
| 257 if (remote_file_tracker_) { | 259 if (remote_file_tracker_) { |
| 258 if (!GetKnownChangeID(metadata_database(), | 260 if (!GetKnownChangeID(metadata_database(), |
| 259 remote_file_tracker_->file_id(), | 261 remote_file_tracker_->file_id(), |
| 260 &remote_file_change_id_)) { | 262 &remote_file_change_id_)) { |
| 261 NOTREACHED(); | 263 NOTREACHED(); |
| 262 SyncCompleted(token.Pass(), SYNC_STATUS_FAILED); | 264 SyncCompleted(std::move(token), SYNC_STATUS_FAILED); |
| 263 return; | 265 return; |
| 264 } | 266 } |
| 265 | 267 |
| 266 blocker->tracker_ids.push_back(remote_file_tracker_->tracker_id()); | 268 blocker->tracker_ids.push_back(remote_file_tracker_->tracker_id()); |
| 267 blocker->file_ids.push_back(remote_file_tracker_->file_id()); | 269 blocker->file_ids.push_back(remote_file_tracker_->file_id()); |
| 268 } | 270 } |
| 269 | 271 |
| 270 // Run current task as a background task with |blocker|. | 272 // Run current task as a background task with |blocker|. |
| 271 // After the invocation of ContinueAsBackgroundTask | 273 // After the invocation of ContinueAsBackgroundTask |
| 272 SyncTaskManager::UpdateTaskBlocker( | 274 SyncTaskManager::UpdateTaskBlocker( |
| 273 token.Pass(), blocker.Pass(), | 275 std::move(token), std::move(blocker), |
| 274 base::Bind(&LocalToRemoteSyncer::ContinueAsBackgroundTask, | 276 base::Bind(&LocalToRemoteSyncer::ContinueAsBackgroundTask, |
| 275 weak_ptr_factory_.GetWeakPtr(), | 277 weak_ptr_factory_.GetWeakPtr(), continuation)); |
| 276 continuation)); | |
| 277 } | 278 } |
| 278 | 279 |
| 279 void LocalToRemoteSyncer::ContinueAsBackgroundTask( | 280 void LocalToRemoteSyncer::ContinueAsBackgroundTask( |
| 280 const Continuation& continuation, | 281 const Continuation& continuation, |
| 281 scoped_ptr<SyncTaskToken> token) { | 282 scoped_ptr<SyncTaskToken> token) { |
| 282 // The SyncTask runs as a background task beyond this point. | 283 // The SyncTask runs as a background task beyond this point. |
| 283 // Note that any task can run between MoveToBackground() and | 284 // Note that any task can run between MoveToBackground() and |
| 284 // ContinueAsBackgroundTask(), so we need to make sure other tasks didn't | 285 // ContinueAsBackgroundTask(), so we need to make sure other tasks didn't |
| 285 // affect to the current LocalToRemoteSyncer task. | 286 // affect to the current LocalToRemoteSyncer task. |
| 286 // | 287 // |
| 287 // - For RemoteToLocalSyncer, it doesn't actually run beyond | 288 // - For RemoteToLocalSyncer, it doesn't actually run beyond |
| 288 // PrepareForProcessRemoteChange() since it should be blocked in | 289 // PrepareForProcessRemoteChange() since it should be blocked in |
| 289 // LocalFileSyncService. | 290 // LocalFileSyncService. |
| 290 // - For ListChangesTask, it may update FileMetatada together with |change_id| | 291 // - For ListChangesTask, it may update FileMetatada together with |change_id| |
| 291 // and may delete FileTracker. So, ensure |change_id| is not changed and | 292 // and may delete FileTracker. So, ensure |change_id| is not changed and |
| 292 // check if FileTracker still exists. | 293 // check if FileTracker still exists. |
| 293 // - For UninstallAppTask, it may also delete FileMetadata and FileTracker. | 294 // - For UninstallAppTask, it may also delete FileMetadata and FileTracker. |
| 294 // Check if FileTracker still exists. | 295 // Check if FileTracker still exists. |
| 295 // - Others, SyncEngineInitializer and RegisterAppTask doesn't affect to | 296 // - Others, SyncEngineInitializer and RegisterAppTask doesn't affect to |
| 296 // LocalToRemoteSyncer. | 297 // LocalToRemoteSyncer. |
| 297 if (remote_file_tracker_) { | 298 if (remote_file_tracker_) { |
| 298 int64_t latest_change_id = 0; | 299 int64_t latest_change_id = 0; |
| 299 if (!GetKnownChangeID(metadata_database(), | 300 if (!GetKnownChangeID(metadata_database(), |
| 300 remote_file_tracker_->file_id(), | 301 remote_file_tracker_->file_id(), |
| 301 &latest_change_id) || | 302 &latest_change_id) || |
| 302 latest_change_id > remote_file_change_id_) { | 303 latest_change_id > remote_file_change_id_) { |
| 303 SyncCompleted(token.Pass(), SYNC_STATUS_RETRY); | 304 SyncCompleted(std::move(token), SYNC_STATUS_RETRY); |
| 304 return; | 305 return; |
| 305 } | 306 } |
| 306 | 307 |
| 307 if (!metadata_database()->FindTrackerByTrackerID( | 308 if (!metadata_database()->FindTrackerByTrackerID( |
| 308 remote_file_tracker_->tracker_id(), nullptr)) { | 309 remote_file_tracker_->tracker_id(), nullptr)) { |
| 309 SyncCompleted(token.Pass(), SYNC_STATUS_RETRY); | 310 SyncCompleted(std::move(token), SYNC_STATUS_RETRY); |
| 310 return; | 311 return; |
| 311 } | 312 } |
| 312 } | 313 } |
| 313 | 314 |
| 314 continuation.Run(token.Pass()); | 315 continuation.Run(std::move(token)); |
| 315 } | 316 } |
| 316 | 317 |
| 317 void LocalToRemoteSyncer::SyncCompleted(scoped_ptr<SyncTaskToken> token, | 318 void LocalToRemoteSyncer::SyncCompleted(scoped_ptr<SyncTaskToken> token, |
| 318 SyncStatusCode status) { | 319 SyncStatusCode status) { |
| 319 if (status == SYNC_STATUS_OK && retry_on_success_) | 320 if (status == SYNC_STATUS_OK && retry_on_success_) |
| 320 status = SYNC_STATUS_RETRY; | 321 status = SYNC_STATUS_RETRY; |
| 321 | 322 |
| 322 if (needs_remote_change_listing_) | 323 if (needs_remote_change_listing_) |
| 323 status = SYNC_STATUS_FILE_BUSY; | 324 status = SYNC_STATUS_FILE_BUSY; |
| 324 | 325 |
| 325 token->RecordLog(base::StringPrintf( | 326 token->RecordLog(base::StringPrintf( |
| 326 "Finished: action=%s, status=%s for %s@%s", | 327 "Finished: action=%s, status=%s for %s@%s", |
| 327 SyncActionToString(sync_action_), | 328 SyncActionToString(sync_action_), |
| 328 SyncStatusCodeToString(status), | 329 SyncStatusCodeToString(status), |
| 329 target_path_.AsUTF8Unsafe().c_str(), | 330 target_path_.AsUTF8Unsafe().c_str(), |
| 330 url_.origin().host().c_str())); | 331 url_.origin().host().c_str())); |
| 331 | 332 |
| 332 SyncTaskManager::NotifyTaskDone(token.Pass(), status); | 333 SyncTaskManager::NotifyTaskDone(std::move(token), status); |
| 333 } | 334 } |
| 334 | 335 |
| 335 void LocalToRemoteSyncer::HandleConflict(scoped_ptr<SyncTaskToken> token) { | 336 void LocalToRemoteSyncer::HandleConflict(scoped_ptr<SyncTaskToken> token) { |
| 336 DCHECK(remote_file_tracker_); | 337 DCHECK(remote_file_tracker_); |
| 337 DCHECK(remote_file_tracker_->has_synced_details()); | 338 DCHECK(remote_file_tracker_->has_synced_details()); |
| 338 DCHECK(remote_file_tracker_->active()); | 339 DCHECK(remote_file_tracker_->active()); |
| 339 DCHECK(remote_file_tracker_->dirty()); | 340 DCHECK(remote_file_tracker_->dirty()); |
| 340 | 341 |
| 341 if (local_is_missing_) { | 342 if (local_is_missing_) { |
| 342 SyncCompleted(token.Pass(), SYNC_STATUS_OK); | 343 SyncCompleted(std::move(token), SYNC_STATUS_OK); |
| 343 return; | 344 return; |
| 344 } | 345 } |
| 345 | 346 |
| 346 if (local_change_.IsFile()) { | 347 if (local_change_.IsFile()) { |
| 347 // Upload the conflicting file as a new file and let ConflictResolver | 348 // Upload the conflicting file as a new file and let ConflictResolver |
| 348 // resolve it. | 349 // resolve it. |
| 349 MoveToBackground(base::Bind(&LocalToRemoteSyncer::UploadNewFile, | 350 MoveToBackground(base::Bind(&LocalToRemoteSyncer::UploadNewFile, |
| 350 weak_ptr_factory_.GetWeakPtr()), | 351 weak_ptr_factory_.GetWeakPtr()), |
| 351 token.Pass()); | 352 std::move(token)); |
| 352 return; | 353 return; |
| 353 } | 354 } |
| 354 | 355 |
| 355 DCHECK(local_change_.IsDirectory()); | 356 DCHECK(local_change_.IsDirectory()); |
| 356 // Check if we can reuse the remote folder. | 357 // Check if we can reuse the remote folder. |
| 357 FileMetadata remote_file_metadata; | 358 FileMetadata remote_file_metadata; |
| 358 if (!metadata_database()->FindFileByFileID( | 359 if (!metadata_database()->FindFileByFileID( |
| 359 remote_file_tracker_->file_id(), &remote_file_metadata)) { | 360 remote_file_tracker_->file_id(), &remote_file_metadata)) { |
| 360 NOTREACHED(); | 361 NOTREACHED(); |
| 361 MoveToBackground(base::Bind(&LocalToRemoteSyncer::CreateRemoteFolder, | 362 MoveToBackground(base::Bind(&LocalToRemoteSyncer::CreateRemoteFolder, |
| 362 weak_ptr_factory_.GetWeakPtr()), | 363 weak_ptr_factory_.GetWeakPtr()), |
| 363 token.Pass()); | 364 std::move(token)); |
| 364 return; | 365 return; |
| 365 } | 366 } |
| 366 | 367 |
| 367 const FileDetails& remote_details = remote_file_metadata.details(); | 368 const FileDetails& remote_details = remote_file_metadata.details(); |
| 368 base::FilePath title = storage::VirtualPath::BaseName(target_path_); | 369 base::FilePath title = storage::VirtualPath::BaseName(target_path_); |
| 369 if (!remote_details.missing() && | 370 if (!remote_details.missing() && |
| 370 remote_details.file_kind() == FILE_KIND_FOLDER && | 371 remote_details.file_kind() == FILE_KIND_FOLDER && |
| 371 remote_details.title() == title.AsUTF8Unsafe() && | 372 remote_details.title() == title.AsUTF8Unsafe() && |
| 372 HasFileAsParent(remote_details, | 373 HasFileAsParent(remote_details, |
| 373 remote_parent_folder_tracker_->file_id())) { | 374 remote_parent_folder_tracker_->file_id())) { |
| 374 MoveToBackground( | 375 MoveToBackground( |
| 375 base::Bind(&LocalToRemoteSyncer::UpdateTrackerForReusedFolder, | 376 base::Bind(&LocalToRemoteSyncer::UpdateTrackerForReusedFolder, |
| 376 weak_ptr_factory_.GetWeakPtr(), | 377 weak_ptr_factory_.GetWeakPtr(), remote_details), |
| 377 remote_details), | 378 std::move(token)); |
| 378 token.Pass()); | |
| 379 return; | 379 return; |
| 380 } | 380 } |
| 381 | 381 |
| 382 MoveToBackground(base::Bind(&LocalToRemoteSyncer::CreateRemoteFolder, | 382 MoveToBackground(base::Bind(&LocalToRemoteSyncer::CreateRemoteFolder, |
| 383 weak_ptr_factory_.GetWeakPtr()), | 383 weak_ptr_factory_.GetWeakPtr()), |
| 384 token.Pass()); | 384 std::move(token)); |
| 385 } | 385 } |
| 386 | 386 |
| 387 void LocalToRemoteSyncer::UpdateTrackerForReusedFolder( | 387 void LocalToRemoteSyncer::UpdateTrackerForReusedFolder( |
| 388 const FileDetails& details, | 388 const FileDetails& details, |
| 389 scoped_ptr<SyncTaskToken> token) { | 389 scoped_ptr<SyncTaskToken> token) { |
| 390 SyncStatusCode status = metadata_database()->UpdateTracker( | 390 SyncStatusCode status = metadata_database()->UpdateTracker( |
| 391 remote_file_tracker_->tracker_id(), details); | 391 remote_file_tracker_->tracker_id(), details); |
| 392 SyncCompleted(token.Pass(), status); | 392 SyncCompleted(std::move(token), status); |
| 393 } | 393 } |
| 394 | 394 |
| 395 void LocalToRemoteSyncer::HandleExistingRemoteFile( | 395 void LocalToRemoteSyncer::HandleExistingRemoteFile( |
| 396 scoped_ptr<SyncTaskToken> token) { | 396 scoped_ptr<SyncTaskToken> token) { |
| 397 DCHECK(remote_file_tracker_); | 397 DCHECK(remote_file_tracker_); |
| 398 DCHECK(!remote_file_tracker_->dirty()); | 398 DCHECK(!remote_file_tracker_->dirty()); |
| 399 DCHECK(remote_file_tracker_->active()); | 399 DCHECK(remote_file_tracker_->active()); |
| 400 DCHECK(remote_file_tracker_->has_synced_details()); | 400 DCHECK(remote_file_tracker_->has_synced_details()); |
| 401 | 401 |
| 402 if (local_is_missing_) { | 402 if (local_is_missing_) { |
| 403 // Local file deletion for existing remote file. | 403 // Local file deletion for existing remote file. |
| 404 MoveToBackground(base::Bind(&LocalToRemoteSyncer::DeleteRemoteFile, | 404 MoveToBackground(base::Bind(&LocalToRemoteSyncer::DeleteRemoteFile, |
| 405 weak_ptr_factory_.GetWeakPtr()), | 405 weak_ptr_factory_.GetWeakPtr()), |
| 406 token.Pass()); | 406 std::move(token)); |
| 407 return; | 407 return; |
| 408 } | 408 } |
| 409 | 409 |
| 410 DCHECK(local_change_.IsAddOrUpdate()); | 410 DCHECK(local_change_.IsAddOrUpdate()); |
| 411 DCHECK(local_change_.IsFile() || local_change_.IsDirectory()); | 411 DCHECK(local_change_.IsFile() || local_change_.IsDirectory()); |
| 412 | 412 |
| 413 const FileDetails& synced_details = remote_file_tracker_->synced_details(); | 413 const FileDetails& synced_details = remote_file_tracker_->synced_details(); |
| 414 DCHECK(synced_details.file_kind() == FILE_KIND_FILE || | 414 DCHECK(synced_details.file_kind() == FILE_KIND_FILE || |
| 415 synced_details.file_kind() == FILE_KIND_FOLDER); | 415 synced_details.file_kind() == FILE_KIND_FOLDER); |
| 416 if (local_change_.IsFile()) { | 416 if (local_change_.IsFile()) { |
| 417 if (synced_details.file_kind() == FILE_KIND_FILE) { | 417 if (synced_details.file_kind() == FILE_KIND_FILE) { |
| 418 // Non-conflicting local file update to existing remote regular file. | 418 // Non-conflicting local file update to existing remote regular file. |
| 419 MoveToBackground(base::Bind(&LocalToRemoteSyncer::UploadExistingFile, | 419 MoveToBackground(base::Bind(&LocalToRemoteSyncer::UploadExistingFile, |
| 420 weak_ptr_factory_.GetWeakPtr()), | 420 weak_ptr_factory_.GetWeakPtr()), |
| 421 token.Pass()); | 421 std::move(token)); |
| 422 return; | 422 return; |
| 423 } | 423 } |
| 424 | 424 |
| 425 DCHECK_EQ(FILE_KIND_FOLDER, synced_details.file_kind()); | 425 DCHECK_EQ(FILE_KIND_FOLDER, synced_details.file_kind()); |
| 426 // Non-conflicting local file update to existing remote *folder*. | 426 // Non-conflicting local file update to existing remote *folder*. |
| 427 // Assuming this case as local folder deletion + local file creation, delete | 427 // Assuming this case as local folder deletion + local file creation, delete |
| 428 // the remote folder and upload the file. | 428 // the remote folder and upload the file. |
| 429 retry_on_success_ = true; | 429 retry_on_success_ = true; |
| 430 MoveToBackground(base::Bind(&LocalToRemoteSyncer::DeleteRemoteFile, | 430 MoveToBackground(base::Bind(&LocalToRemoteSyncer::DeleteRemoteFile, |
| 431 weak_ptr_factory_.GetWeakPtr()), | 431 weak_ptr_factory_.GetWeakPtr()), |
| 432 token.Pass()); | 432 std::move(token)); |
| 433 return; | 433 return; |
| 434 } | 434 } |
| 435 | 435 |
| 436 DCHECK(local_change_.IsDirectory()); | 436 DCHECK(local_change_.IsDirectory()); |
| 437 if (synced_details.file_kind() == FILE_KIND_FILE) { | 437 if (synced_details.file_kind() == FILE_KIND_FILE) { |
| 438 // Non-conflicting local folder creation to existing remote *file*. | 438 // Non-conflicting local folder creation to existing remote *file*. |
| 439 // Assuming this case as local file deletion + local folder creation, delete | 439 // Assuming this case as local file deletion + local folder creation, delete |
| 440 // the remote file and create a remote folder. | 440 // the remote file and create a remote folder. |
| 441 retry_on_success_ = true; | 441 retry_on_success_ = true; |
| 442 MoveToBackground(base::Bind(&LocalToRemoteSyncer::DeleteRemoteFile, | 442 MoveToBackground(base::Bind(&LocalToRemoteSyncer::DeleteRemoteFile, |
| 443 weak_ptr_factory_.GetWeakPtr()), | 443 weak_ptr_factory_.GetWeakPtr()), |
| 444 token.Pass()); | 444 std::move(token)); |
| 445 return; | 445 return; |
| 446 } | 446 } |
| 447 | 447 |
| 448 // Non-conflicting local folder creation to existing remote folder. | 448 // Non-conflicting local folder creation to existing remote folder. |
| 449 DCHECK_EQ(FILE_KIND_FOLDER, synced_details.file_kind()); | 449 DCHECK_EQ(FILE_KIND_FOLDER, synced_details.file_kind()); |
| 450 SyncCompleted(token.Pass(), SYNC_STATUS_OK); | 450 SyncCompleted(std::move(token), SYNC_STATUS_OK); |
| 451 } | 451 } |
| 452 | 452 |
| 453 void LocalToRemoteSyncer::DeleteRemoteFile(scoped_ptr<SyncTaskToken> token) { | 453 void LocalToRemoteSyncer::DeleteRemoteFile(scoped_ptr<SyncTaskToken> token) { |
| 454 DCHECK(remote_file_tracker_); | 454 DCHECK(remote_file_tracker_); |
| 455 DCHECK(remote_file_tracker_->has_synced_details()); | 455 DCHECK(remote_file_tracker_->has_synced_details()); |
| 456 | 456 |
| 457 switch (remote_file_tracker_->synced_details().file_kind()) { | 457 switch (remote_file_tracker_->synced_details().file_kind()) { |
| 458 case FILE_KIND_UNSUPPORTED: | 458 case FILE_KIND_UNSUPPORTED: |
| 459 NOTREACHED(); | 459 NOTREACHED(); |
| 460 file_type_ = SYNC_FILE_TYPE_UNKNOWN; | 460 file_type_ = SYNC_FILE_TYPE_UNKNOWN; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 475 } | 475 } |
| 476 | 476 |
| 477 void LocalToRemoteSyncer::DidDeleteRemoteFile( | 477 void LocalToRemoteSyncer::DidDeleteRemoteFile( |
| 478 scoped_ptr<SyncTaskToken> token, | 478 scoped_ptr<SyncTaskToken> token, |
| 479 google_apis::DriveApiErrorCode error) { | 479 google_apis::DriveApiErrorCode error) { |
| 480 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); | 480 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); |
| 481 if (status != SYNC_STATUS_OK && | 481 if (status != SYNC_STATUS_OK && |
| 482 error != google_apis::HTTP_NOT_FOUND && | 482 error != google_apis::HTTP_NOT_FOUND && |
| 483 error != google_apis::HTTP_PRECONDITION && | 483 error != google_apis::HTTP_PRECONDITION && |
| 484 error != google_apis::HTTP_CONFLICT) { | 484 error != google_apis::HTTP_CONFLICT) { |
| 485 SyncCompleted(token.Pass(), status); | 485 SyncCompleted(std::move(token), status); |
| 486 return; | 486 return; |
| 487 } | 487 } |
| 488 | 488 |
| 489 // Handle NOT_FOUND case as SUCCESS case. | 489 // Handle NOT_FOUND case as SUCCESS case. |
| 490 // For PRECONDITION / CONFLICT case, the remote file is modified since the | 490 // For PRECONDITION / CONFLICT case, the remote file is modified since the |
| 491 // last sync completed. As our policy for deletion-modification conflict | 491 // last sync completed. As our policy for deletion-modification conflict |
| 492 // resolution, ignore the local deletion. | 492 // resolution, ignore the local deletion. |
| 493 if (status == SYNC_STATUS_OK || | 493 if (status == SYNC_STATUS_OK || |
| 494 error == google_apis::HTTP_NOT_FOUND) { | 494 error == google_apis::HTTP_NOT_FOUND) { |
| 495 SyncStatusCode status = metadata_database()->UpdateByDeletedRemoteFile( | 495 SyncStatusCode status = metadata_database()->UpdateByDeletedRemoteFile( |
| 496 remote_file_tracker_->file_id()); | 496 remote_file_tracker_->file_id()); |
| 497 SyncCompleted(token.Pass(), status); | 497 SyncCompleted(std::move(token), status); |
| 498 return; | 498 return; |
| 499 } | 499 } |
| 500 | 500 |
| 501 SyncCompleted(token.Pass(), SYNC_STATUS_OK); | 501 SyncCompleted(std::move(token), SYNC_STATUS_OK); |
| 502 } | 502 } |
| 503 | 503 |
| 504 void LocalToRemoteSyncer::UploadExistingFile(scoped_ptr<SyncTaskToken> token) { | 504 void LocalToRemoteSyncer::UploadExistingFile(scoped_ptr<SyncTaskToken> token) { |
| 505 DCHECK(remote_file_tracker_); | 505 DCHECK(remote_file_tracker_); |
| 506 DCHECK(remote_file_tracker_->has_synced_details()); | 506 DCHECK(remote_file_tracker_->has_synced_details()); |
| 507 DCHECK(sync_context_->GetWorkerTaskRunner()->RunsTasksOnCurrentThread()); | 507 DCHECK(sync_context_->GetWorkerTaskRunner()->RunsTasksOnCurrentThread()); |
| 508 | 508 |
| 509 const std::string local_file_md5 = drive::util::GetMd5Digest(local_path_, | 509 const std::string local_file_md5 = drive::util::GetMd5Digest(local_path_, |
| 510 nullptr); | 510 nullptr); |
| 511 if (local_file_md5 == remote_file_tracker_->synced_details().md5()) { | 511 if (local_file_md5 == remote_file_tracker_->synced_details().md5()) { |
| 512 // Local file is not changed. | 512 // Local file is not changed. |
| 513 SyncCompleted(token.Pass(), SYNC_STATUS_OK); | 513 SyncCompleted(std::move(token), SYNC_STATUS_OK); |
| 514 return; | 514 return; |
| 515 } | 515 } |
| 516 | 516 |
| 517 file_type_ = SYNC_FILE_TYPE_FILE; | 517 file_type_ = SYNC_FILE_TYPE_FILE; |
| 518 sync_action_ = SYNC_ACTION_UPDATED; | 518 sync_action_ = SYNC_ACTION_UPDATED; |
| 519 | 519 |
| 520 drive::UploadExistingFileOptions options; | 520 drive::UploadExistingFileOptions options; |
| 521 options.etag = remote_file_tracker_->synced_details().etag(); | 521 options.etag = remote_file_tracker_->synced_details().etag(); |
| 522 drive_uploader()->UploadExistingFile( | 522 drive_uploader()->UploadExistingFile( |
| 523 remote_file_tracker_->file_id(), | 523 remote_file_tracker_->file_id(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 536 scoped_ptr<google_apis::FileResource> entry) { | 536 scoped_ptr<google_apis::FileResource> entry) { |
| 537 if (error == google_apis::HTTP_PRECONDITION || | 537 if (error == google_apis::HTTP_PRECONDITION || |
| 538 error == google_apis::HTTP_CONFLICT || | 538 error == google_apis::HTTP_CONFLICT || |
| 539 error == google_apis::HTTP_NOT_FOUND) { | 539 error == google_apis::HTTP_NOT_FOUND) { |
| 540 // The remote file has unfetched remote change. Fetch latest metadata and | 540 // The remote file has unfetched remote change. Fetch latest metadata and |
| 541 // update database with it. | 541 // update database with it. |
| 542 // TODO(tzik): Consider adding local side low-priority dirtiness handling to | 542 // TODO(tzik): Consider adding local side low-priority dirtiness handling to |
| 543 // handle this as ListChangesTask. | 543 // handle this as ListChangesTask. |
| 544 | 544 |
| 545 needs_remote_change_listing_ = true; | 545 needs_remote_change_listing_ = true; |
| 546 UpdateRemoteMetadata( | 546 UpdateRemoteMetadata(remote_file_tracker_->file_id(), std::move(token)); |
| 547 remote_file_tracker_->file_id(), | |
| 548 token.Pass()); | |
| 549 return; | 547 return; |
| 550 } | 548 } |
| 551 | 549 |
| 552 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); | 550 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); |
| 553 if (status != SYNC_STATUS_OK) { | 551 if (status != SYNC_STATUS_OK) { |
| 554 SyncCompleted(token.Pass(), status); | 552 SyncCompleted(std::move(token), status); |
| 555 return; | 553 return; |
| 556 } | 554 } |
| 557 | 555 |
| 558 if (!entry) { | 556 if (!entry) { |
| 559 NOTREACHED(); | 557 NOTREACHED(); |
| 560 SyncCompleted(token.Pass(), SYNC_STATUS_FAILED); | 558 SyncCompleted(std::move(token), SYNC_STATUS_FAILED); |
| 561 return; | 559 return; |
| 562 } | 560 } |
| 563 | 561 |
| 564 DCHECK(entry); | 562 DCHECK(entry); |
| 565 status = metadata_database()->UpdateByFileResource(*entry); | 563 status = metadata_database()->UpdateByFileResource(*entry); |
| 566 if (status != SYNC_STATUS_OK) { | 564 if (status != SYNC_STATUS_OK) { |
| 567 SyncCompleted(token.Pass(), status); | 565 SyncCompleted(std::move(token), status); |
| 568 return; | 566 return; |
| 569 } | 567 } |
| 570 | 568 |
| 571 FileMetadata file; | 569 FileMetadata file; |
| 572 if (!metadata_database()->FindFileByFileID( | 570 if (!metadata_database()->FindFileByFileID( |
| 573 remote_file_tracker_->file_id(), &file)) { | 571 remote_file_tracker_->file_id(), &file)) { |
| 574 NOTREACHED(); | 572 NOTREACHED(); |
| 575 SyncCompleted(token.Pass(), SYNC_STATUS_FAILED); | 573 SyncCompleted(std::move(token), SYNC_STATUS_FAILED); |
| 576 return; | 574 return; |
| 577 } | 575 } |
| 578 | 576 |
| 579 const FileDetails& details = file.details(); | 577 const FileDetails& details = file.details(); |
| 580 base::FilePath title = storage::VirtualPath::BaseName(target_path_); | 578 base::FilePath title = storage::VirtualPath::BaseName(target_path_); |
| 581 if (!details.missing() && | 579 if (!details.missing() && |
| 582 details.file_kind() == FILE_KIND_FILE && | 580 details.file_kind() == FILE_KIND_FILE && |
| 583 details.title() == title.AsUTF8Unsafe() && | 581 details.title() == title.AsUTF8Unsafe() && |
| 584 HasFileAsParent(details, | 582 HasFileAsParent(details, |
| 585 remote_parent_folder_tracker_->file_id())) { | 583 remote_parent_folder_tracker_->file_id())) { |
| 586 SyncStatusCode status = metadata_database()->UpdateTracker( | 584 SyncStatusCode status = metadata_database()->UpdateTracker( |
| 587 remote_file_tracker_->tracker_id(), file.details()); | 585 remote_file_tracker_->tracker_id(), file.details()); |
| 588 SyncCompleted(token.Pass(), status); | 586 SyncCompleted(std::move(token), status); |
| 589 return; | 587 return; |
| 590 } | 588 } |
| 591 | 589 |
| 592 SyncCompleted(token.Pass(), SYNC_STATUS_RETRY); | 590 SyncCompleted(std::move(token), SYNC_STATUS_RETRY); |
| 593 } | 591 } |
| 594 | 592 |
| 595 void LocalToRemoteSyncer::UpdateRemoteMetadata( | 593 void LocalToRemoteSyncer::UpdateRemoteMetadata( |
| 596 const std::string& file_id, | 594 const std::string& file_id, |
| 597 scoped_ptr<SyncTaskToken> token) { | 595 scoped_ptr<SyncTaskToken> token) { |
| 598 DCHECK(remote_file_tracker_); | 596 DCHECK(remote_file_tracker_); |
| 599 | 597 |
| 600 drive_service()->GetFileResource( | 598 drive_service()->GetFileResource( |
| 601 file_id, | 599 file_id, |
| 602 base::Bind(&LocalToRemoteSyncer::DidGetRemoteMetadata, | 600 base::Bind(&LocalToRemoteSyncer::DidGetRemoteMetadata, |
| 603 weak_ptr_factory_.GetWeakPtr(), | 601 weak_ptr_factory_.GetWeakPtr(), |
| 604 file_id, base::Passed(&token))); | 602 file_id, base::Passed(&token))); |
| 605 } | 603 } |
| 606 | 604 |
| 607 void LocalToRemoteSyncer::DidGetRemoteMetadata( | 605 void LocalToRemoteSyncer::DidGetRemoteMetadata( |
| 608 const std::string& file_id, | 606 const std::string& file_id, |
| 609 scoped_ptr<SyncTaskToken> token, | 607 scoped_ptr<SyncTaskToken> token, |
| 610 google_apis::DriveApiErrorCode error, | 608 google_apis::DriveApiErrorCode error, |
| 611 scoped_ptr<google_apis::FileResource> entry) { | 609 scoped_ptr<google_apis::FileResource> entry) { |
| 612 DCHECK(sync_context_->GetWorkerTaskRunner()->RunsTasksOnCurrentThread()); | 610 DCHECK(sync_context_->GetWorkerTaskRunner()->RunsTasksOnCurrentThread()); |
| 613 | 611 |
| 614 if (error == google_apis::HTTP_NOT_FOUND) { | 612 if (error == google_apis::HTTP_NOT_FOUND) { |
| 615 retry_on_success_ = true; | 613 retry_on_success_ = true; |
| 616 SyncStatusCode status = | 614 SyncStatusCode status = |
| 617 metadata_database()->UpdateByDeletedRemoteFile(file_id); | 615 metadata_database()->UpdateByDeletedRemoteFile(file_id); |
| 618 SyncCompleted(token.Pass(), status); | 616 SyncCompleted(std::move(token), status); |
| 619 return; | 617 return; |
| 620 } | 618 } |
| 621 | 619 |
| 622 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); | 620 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); |
| 623 if (status != SYNC_STATUS_OK) { | 621 if (status != SYNC_STATUS_OK) { |
| 624 SyncCompleted(token.Pass(), status); | 622 SyncCompleted(std::move(token), status); |
| 625 return; | 623 return; |
| 626 } | 624 } |
| 627 | 625 |
| 628 if (!entry) { | 626 if (!entry) { |
| 629 NOTREACHED(); | 627 NOTREACHED(); |
| 630 SyncCompleted(token.Pass(), SYNC_STATUS_FAILED); | 628 SyncCompleted(std::move(token), SYNC_STATUS_FAILED); |
| 631 return; | 629 return; |
| 632 } | 630 } |
| 633 | 631 |
| 634 retry_on_success_ = true; | 632 retry_on_success_ = true; |
| 635 status = metadata_database()->UpdateByFileResource(*entry); | 633 status = metadata_database()->UpdateByFileResource(*entry); |
| 636 SyncCompleted(token.Pass(), status); | 634 SyncCompleted(std::move(token), status); |
| 637 } | 635 } |
| 638 | 636 |
| 639 void LocalToRemoteSyncer::UploadNewFile(scoped_ptr<SyncTaskToken> token) { | 637 void LocalToRemoteSyncer::UploadNewFile(scoped_ptr<SyncTaskToken> token) { |
| 640 DCHECK(remote_parent_folder_tracker_); | 638 DCHECK(remote_parent_folder_tracker_); |
| 641 | 639 |
| 642 file_type_ = SYNC_FILE_TYPE_FILE; | 640 file_type_ = SYNC_FILE_TYPE_FILE; |
| 643 sync_action_ = SYNC_ACTION_ADDED; | 641 sync_action_ = SYNC_ACTION_ADDED; |
| 644 base::FilePath title = storage::VirtualPath::BaseName(target_path_); | 642 base::FilePath title = storage::VirtualPath::BaseName(target_path_); |
| 645 drive_uploader()->UploadNewFile( | 643 drive_uploader()->UploadNewFile( |
| 646 remote_parent_folder_tracker_->file_id(), local_path_, | 644 remote_parent_folder_tracker_->file_id(), local_path_, |
| 647 title.AsUTF8Unsafe(), GetMimeTypeFromTitle(title), | 645 title.AsUTF8Unsafe(), GetMimeTypeFromTitle(title), |
| 648 drive::UploadNewFileOptions(), | 646 drive::UploadNewFileOptions(), |
| 649 base::Bind(&LocalToRemoteSyncer::DidUploadNewFile, | 647 base::Bind(&LocalToRemoteSyncer::DidUploadNewFile, |
| 650 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token)), | 648 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token)), |
| 651 google_apis::ProgressCallback()); | 649 google_apis::ProgressCallback()); |
| 652 } | 650 } |
| 653 | 651 |
| 654 void LocalToRemoteSyncer::DidUploadNewFile( | 652 void LocalToRemoteSyncer::DidUploadNewFile( |
| 655 scoped_ptr<SyncTaskToken> token, | 653 scoped_ptr<SyncTaskToken> token, |
| 656 google_apis::DriveApiErrorCode error, | 654 google_apis::DriveApiErrorCode error, |
| 657 const GURL& upload_location, | 655 const GURL& upload_location, |
| 658 scoped_ptr<google_apis::FileResource> entry) { | 656 scoped_ptr<google_apis::FileResource> entry) { |
| 659 if (error == google_apis::HTTP_NOT_FOUND) | 657 if (error == google_apis::HTTP_NOT_FOUND) |
| 660 needs_remote_change_listing_ = true; | 658 needs_remote_change_listing_ = true; |
| 661 | 659 |
| 662 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); | 660 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); |
| 663 if (status != SYNC_STATUS_OK) { | 661 if (status != SYNC_STATUS_OK) { |
| 664 SyncCompleted(token.Pass(), status); | 662 SyncCompleted(std::move(token), status); |
| 665 return; | 663 return; |
| 666 } | 664 } |
| 667 | 665 |
| 668 if (!entry) { | 666 if (!entry) { |
| 669 NOTREACHED(); | 667 NOTREACHED(); |
| 670 SyncCompleted(token.Pass(), SYNC_STATUS_FAILED); | 668 SyncCompleted(std::move(token), SYNC_STATUS_FAILED); |
| 671 return; | 669 return; |
| 672 } | 670 } |
| 673 | 671 |
| 674 status = metadata_database()->ReplaceActiveTrackerWithNewResource( | 672 status = metadata_database()->ReplaceActiveTrackerWithNewResource( |
| 675 remote_parent_folder_tracker_->tracker_id(), *entry); | 673 remote_parent_folder_tracker_->tracker_id(), *entry); |
| 676 SyncCompleted(token.Pass(), status); | 674 SyncCompleted(std::move(token), status); |
| 677 } | 675 } |
| 678 | 676 |
| 679 void LocalToRemoteSyncer::CreateRemoteFolder(scoped_ptr<SyncTaskToken> token) { | 677 void LocalToRemoteSyncer::CreateRemoteFolder(scoped_ptr<SyncTaskToken> token) { |
| 680 DCHECK(remote_parent_folder_tracker_); | 678 DCHECK(remote_parent_folder_tracker_); |
| 681 | 679 |
| 682 base::FilePath title = storage::VirtualPath::BaseName(target_path_); | 680 base::FilePath title = storage::VirtualPath::BaseName(target_path_); |
| 683 file_type_ = SYNC_FILE_TYPE_DIRECTORY; | 681 file_type_ = SYNC_FILE_TYPE_DIRECTORY; |
| 684 sync_action_ = SYNC_ACTION_ADDED; | 682 sync_action_ = SYNC_ACTION_ADDED; |
| 685 | 683 |
| 686 DCHECK(!folder_creator_); | 684 DCHECK(!folder_creator_); |
| 687 folder_creator_.reset(new FolderCreator( | 685 folder_creator_.reset(new FolderCreator( |
| 688 drive_service(), metadata_database(), | 686 drive_service(), metadata_database(), |
| 689 remote_parent_folder_tracker_->file_id(), | 687 remote_parent_folder_tracker_->file_id(), |
| 690 title.AsUTF8Unsafe())); | 688 title.AsUTF8Unsafe())); |
| 691 folder_creator_->Run(base::Bind( | 689 folder_creator_->Run(base::Bind( |
| 692 &LocalToRemoteSyncer::DidCreateRemoteFolder, | 690 &LocalToRemoteSyncer::DidCreateRemoteFolder, |
| 693 weak_ptr_factory_.GetWeakPtr(), | 691 weak_ptr_factory_.GetWeakPtr(), |
| 694 base::Passed(&token))); | 692 base::Passed(&token))); |
| 695 } | 693 } |
| 696 | 694 |
| 697 void LocalToRemoteSyncer::DidCreateRemoteFolder( | 695 void LocalToRemoteSyncer::DidCreateRemoteFolder( |
| 698 scoped_ptr<SyncTaskToken> token, | 696 scoped_ptr<SyncTaskToken> token, |
| 699 const std::string& file_id, | 697 const std::string& file_id, |
| 700 SyncStatusCode status) { | 698 SyncStatusCode status) { |
| 701 if (status == SYNC_FILE_ERROR_NOT_FOUND) | 699 if (status == SYNC_FILE_ERROR_NOT_FOUND) |
| 702 needs_remote_change_listing_ = true; | 700 needs_remote_change_listing_ = true; |
| 703 | 701 |
| 704 scoped_ptr<FolderCreator> deleter = folder_creator_.Pass(); | 702 scoped_ptr<FolderCreator> deleter = std::move(folder_creator_); |
| 705 if (status != SYNC_STATUS_OK) { | 703 if (status != SYNC_STATUS_OK) { |
| 706 SyncCompleted(token.Pass(), status); | 704 SyncCompleted(std::move(token), status); |
| 707 return; | 705 return; |
| 708 } | 706 } |
| 709 | 707 |
| 710 status = SYNC_STATUS_FAILED; | 708 status = SYNC_STATUS_FAILED; |
| 711 MetadataDatabase::ActivationStatus activation_status = | 709 MetadataDatabase::ActivationStatus activation_status = |
| 712 metadata_database()->TryActivateTracker( | 710 metadata_database()->TryActivateTracker( |
| 713 remote_parent_folder_tracker_->tracker_id(), | 711 remote_parent_folder_tracker_->tracker_id(), |
| 714 file_id, &status); | 712 file_id, &status); |
| 715 switch (activation_status) { | 713 switch (activation_status) { |
| 716 case MetadataDatabase::ACTIVATION_PENDING: | 714 case MetadataDatabase::ACTIVATION_PENDING: |
| 717 SyncCompleted(token.Pass(), status); | 715 SyncCompleted(std::move(token), status); |
| 718 return; | 716 return; |
| 719 case MetadataDatabase::ACTIVATION_FAILED_ANOTHER_ACTIVE_TRACKER: | 717 case MetadataDatabase::ACTIVATION_FAILED_ANOTHER_ACTIVE_TRACKER: |
| 720 // The activation failed due to another tracker that has another parent. | 718 // The activation failed due to another tracker that has another parent. |
| 721 // Detach the folder from the current parent to avoid using this folder as | 719 // Detach the folder from the current parent to avoid using this folder as |
| 722 // active folder. | 720 // active folder. |
| 723 drive_service()->RemoveResourceFromDirectory( | 721 drive_service()->RemoveResourceFromDirectory( |
| 724 remote_parent_folder_tracker_->file_id(), file_id, | 722 remote_parent_folder_tracker_->file_id(), file_id, |
| 725 base::Bind(&LocalToRemoteSyncer::DidDetachResourceForCreationConflict, | 723 base::Bind(&LocalToRemoteSyncer::DidDetachResourceForCreationConflict, |
| 726 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token))); | 724 weak_ptr_factory_.GetWeakPtr(), base::Passed(&token))); |
| 727 return; | 725 return; |
| 728 } | 726 } |
| 729 | 727 |
| 730 NOTREACHED(); | 728 NOTREACHED(); |
| 731 SyncCompleted(token.Pass(), SYNC_STATUS_FAILED); | 729 SyncCompleted(std::move(token), SYNC_STATUS_FAILED); |
| 732 return; | 730 return; |
| 733 } | 731 } |
| 734 | 732 |
| 735 void LocalToRemoteSyncer::DidDetachResourceForCreationConflict( | 733 void LocalToRemoteSyncer::DidDetachResourceForCreationConflict( |
| 736 scoped_ptr<SyncTaskToken> token, | 734 scoped_ptr<SyncTaskToken> token, |
| 737 google_apis::DriveApiErrorCode error) { | 735 google_apis::DriveApiErrorCode error) { |
| 738 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); | 736 SyncStatusCode status = DriveApiErrorCodeToSyncStatusCode(error); |
| 739 if (status != SYNC_STATUS_OK) { | 737 if (status != SYNC_STATUS_OK) { |
| 740 SyncCompleted(token.Pass(), status); | 738 SyncCompleted(std::move(token), status); |
| 741 return; | 739 return; |
| 742 } | 740 } |
| 743 | 741 |
| 744 SyncCompleted(token.Pass(), SYNC_STATUS_RETRY); | 742 SyncCompleted(std::move(token), SYNC_STATUS_RETRY); |
| 745 } | 743 } |
| 746 | 744 |
| 747 bool LocalToRemoteSyncer::IsContextReady() { | 745 bool LocalToRemoteSyncer::IsContextReady() { |
| 748 return sync_context_->GetDriveService() && | 746 return sync_context_->GetDriveService() && |
| 749 sync_context_->GetDriveUploader() && | 747 sync_context_->GetDriveUploader() && |
| 750 sync_context_->GetMetadataDatabase(); | 748 sync_context_->GetMetadataDatabase(); |
| 751 } | 749 } |
| 752 | 750 |
| 753 drive::DriveServiceInterface* LocalToRemoteSyncer::drive_service() { | 751 drive::DriveServiceInterface* LocalToRemoteSyncer::drive_service() { |
| 754 set_used_network(true); | 752 set_used_network(true); |
| 755 return sync_context_->GetDriveService(); | 753 return sync_context_->GetDriveService(); |
| 756 } | 754 } |
| 757 | 755 |
| 758 drive::DriveUploaderInterface* LocalToRemoteSyncer::drive_uploader() { | 756 drive::DriveUploaderInterface* LocalToRemoteSyncer::drive_uploader() { |
| 759 set_used_network(true); | 757 set_used_network(true); |
| 760 return sync_context_->GetDriveUploader(); | 758 return sync_context_->GetDriveUploader(); |
| 761 } | 759 } |
| 762 | 760 |
| 763 MetadataDatabase* LocalToRemoteSyncer::metadata_database() { | 761 MetadataDatabase* LocalToRemoteSyncer::metadata_database() { |
| 764 return sync_context_->GetMetadataDatabase(); | 762 return sync_context_->GetMetadataDatabase(); |
| 765 } | 763 } |
| 766 | 764 |
| 767 } // namespace drive_backend | 765 } // namespace drive_backend |
| 768 } // namespace sync_file_system | 766 } // namespace sync_file_system |
| OLD | NEW |