| 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/chromeos/drive/sync/remove_performer.h" | 5 #include "chrome/browser/chromeos/drive/sync/remove_performer.h" |
| 6 | 6 |
| 7 #include "base/sequenced_task_runner.h" | 7 #include "base/sequenced_task_runner.h" |
| 8 #include "chrome/browser/chromeos/drive/drive.pb.h" | 8 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 9 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h" | 9 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h" |
| 10 #include "chrome/browser/chromeos/drive/file_system_util.h" | 10 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 ResourceMetadata* metadata) | 56 ResourceMetadata* metadata) |
| 57 : blocking_task_runner_(blocking_task_runner), | 57 : blocking_task_runner_(blocking_task_runner), |
| 58 delegate_(delegate), | 58 delegate_(delegate), |
| 59 scheduler_(scheduler), | 59 scheduler_(scheduler), |
| 60 metadata_(metadata), | 60 metadata_(metadata), |
| 61 entry_revert_performer_(new EntryRevertPerformer(blocking_task_runner, | 61 entry_revert_performer_(new EntryRevertPerformer(blocking_task_runner, |
| 62 delegate, | 62 delegate, |
| 63 scheduler, | 63 scheduler, |
| 64 metadata)), | 64 metadata)), |
| 65 weak_ptr_factory_(this) { | 65 weak_ptr_factory_(this) { |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 67 } | 67 } |
| 68 | 68 |
| 69 RemovePerformer::~RemovePerformer() { | 69 RemovePerformer::~RemovePerformer() { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Returns |entry| corresponding to |local_id|. | 73 // Returns |entry| corresponding to |local_id|. |
| 74 // Adding to that, removes the entry when it does not exist on the server. | 74 // Adding to that, removes the entry when it does not exist on the server. |
| 75 FileError TryToRemoveLocally(ResourceMetadata* metadata, | 75 FileError TryToRemoveLocally(ResourceMetadata* metadata, |
| 76 const std::string& local_id, | 76 const std::string& local_id, |
| 77 ResourceEntry* entry) { | 77 ResourceEntry* entry) { |
| 78 FileError error = metadata->GetResourceEntryById(local_id, entry); | 78 FileError error = metadata->GetResourceEntryById(local_id, entry); |
| 79 if (error != FILE_ERROR_OK || !entry->resource_id().empty()) | 79 if (error != FILE_ERROR_OK || !entry->resource_id().empty()) |
| 80 return error; | 80 return error; |
| 81 return metadata->RemoveEntry(local_id); | 81 return metadata->RemoveEntry(local_id); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void RemovePerformer::Remove(const std::string& local_id, | 84 void RemovePerformer::Remove(const std::string& local_id, |
| 85 const ClientContext& context, | 85 const ClientContext& context, |
| 86 const FileOperationCallback& callback) { | 86 const FileOperationCallback& callback) { |
| 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 88 DCHECK(!callback.is_null()); | 88 DCHECK(!callback.is_null()); |
| 89 | 89 |
| 90 ResourceEntry* entry = new ResourceEntry; | 90 ResourceEntry* entry = new ResourceEntry; |
| 91 base::PostTaskAndReplyWithResult( | 91 base::PostTaskAndReplyWithResult( |
| 92 blocking_task_runner_.get(), | 92 blocking_task_runner_.get(), |
| 93 FROM_HERE, | 93 FROM_HERE, |
| 94 base::Bind(&TryToRemoveLocally, metadata_, local_id, entry), | 94 base::Bind(&TryToRemoveLocally, metadata_, local_id, entry), |
| 95 base::Bind(&RemovePerformer::RemoveAfterGetResourceEntry, | 95 base::Bind(&RemovePerformer::RemoveAfterGetResourceEntry, |
| 96 weak_ptr_factory_.GetWeakPtr(), | 96 weak_ptr_factory_.GetWeakPtr(), |
| 97 context, | 97 context, |
| 98 callback, | 98 callback, |
| 99 base::Owned(entry))); | 99 base::Owned(entry))); |
| 100 } | 100 } |
| 101 | 101 |
| 102 void RemovePerformer::RemoveAfterGetResourceEntry( | 102 void RemovePerformer::RemoveAfterGetResourceEntry( |
| 103 const ClientContext& context, | 103 const ClientContext& context, |
| 104 const FileOperationCallback& callback, | 104 const FileOperationCallback& callback, |
| 105 const ResourceEntry* entry, | 105 const ResourceEntry* entry, |
| 106 FileError error) { | 106 FileError error) { |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 108 DCHECK(!callback.is_null()); | 108 DCHECK(!callback.is_null()); |
| 109 | 109 |
| 110 if (error != FILE_ERROR_OK || entry->resource_id().empty()) { | 110 if (error != FILE_ERROR_OK || entry->resource_id().empty()) { |
| 111 callback.Run(error); | 111 callback.Run(error); |
| 112 return; | 112 return; |
| 113 } | 113 } |
| 114 | 114 |
| 115 // To match with the behavior of drive.google.com: | 115 // To match with the behavior of drive.google.com: |
| 116 // Removal of shared entries under MyDrive is just removing from the parent. | 116 // Removal of shared entries under MyDrive is just removing from the parent. |
| 117 // The entry will stay in shared-with-me (in other words, in "drive/other".) | 117 // The entry will stay in shared-with-me (in other words, in "drive/other".) |
| 118 // | 118 // |
| 119 // TODO(kinaba): to be more precise, we might be better to branch by whether | 119 // TODO(kinaba): to be more precise, we might be better to branch by whether |
| 120 // or not the current account is an owner of the file. The code below is | 120 // or not the current account is an owner of the file. The code below is |
| 121 // written under the assumption that |shared_with_me| coincides with that. | 121 // written under the assumption that |shared_with_me| coincides with that. |
| 122 if (entry->shared_with_me()) { | 122 if (entry->shared_with_me()) { |
| 123 UnparentResource(context, callback, entry->resource_id(), | 123 UnparentResource(context, callback, entry->resource_id(), |
| 124 entry->local_id()); | 124 entry->local_id()); |
| 125 } else { | 125 } else { |
| 126 // Otherwise try sending the entry to trash. | 126 // Otherwise try sending the entry to trash. |
| 127 TrashResource(context, callback, entry->resource_id(), entry->local_id()); | 127 TrashResource(context, callback, entry->resource_id(), entry->local_id()); |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 void RemovePerformer::TrashResource(const ClientContext& context, | 131 void RemovePerformer::TrashResource(const ClientContext& context, |
| 132 const FileOperationCallback& callback, | 132 const FileOperationCallback& callback, |
| 133 const std::string& resource_id, | 133 const std::string& resource_id, |
| 134 const std::string& local_id) { | 134 const std::string& local_id) { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 135 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 136 DCHECK(!callback.is_null()); | 136 DCHECK(!callback.is_null()); |
| 137 | 137 |
| 138 scheduler_->TrashResource( | 138 scheduler_->TrashResource( |
| 139 resource_id, | 139 resource_id, |
| 140 context, | 140 context, |
| 141 base::Bind(&RemovePerformer::TrashResourceAfterUpdateRemoteState, | 141 base::Bind(&RemovePerformer::TrashResourceAfterUpdateRemoteState, |
| 142 weak_ptr_factory_.GetWeakPtr(), context, callback, local_id)); | 142 weak_ptr_factory_.GetWeakPtr(), context, callback, local_id)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void RemovePerformer::TrashResourceAfterUpdateRemoteState( | 145 void RemovePerformer::TrashResourceAfterUpdateRemoteState( |
| 146 const ClientContext& context, | 146 const ClientContext& context, |
| 147 const FileOperationCallback& callback, | 147 const FileOperationCallback& callback, |
| 148 const std::string& local_id, | 148 const std::string& local_id, |
| 149 google_apis::DriveApiErrorCode status) { | 149 google_apis::DriveApiErrorCode status) { |
| 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 150 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 151 DCHECK(!callback.is_null()); | 151 DCHECK(!callback.is_null()); |
| 152 | 152 |
| 153 if (status == google_apis::HTTP_FORBIDDEN) { | 153 if (status == google_apis::HTTP_FORBIDDEN) { |
| 154 // Editing this entry is not allowed, revert local changes. | 154 // Editing this entry is not allowed, revert local changes. |
| 155 entry_revert_performer_->RevertEntry(local_id, context, callback); | 155 entry_revert_performer_->RevertEntry(local_id, context, callback); |
| 156 delegate_->OnDriveSyncError( | 156 delegate_->OnDriveSyncError( |
| 157 file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION, local_id); | 157 file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION, local_id); |
| 158 return; | 158 return; |
| 159 } | 159 } |
| 160 | 160 |
| 161 FileError error = GDataToFileError(status); | 161 FileError error = GDataToFileError(status); |
| 162 if (error == FILE_ERROR_NOT_FOUND) { // Remove local entry when not found. | 162 if (error == FILE_ERROR_NOT_FOUND) { // Remove local entry when not found. |
| 163 RemoveEntryOnUIThread(blocking_task_runner_.get(), metadata_, local_id, | 163 RemoveEntryOnUIThread(blocking_task_runner_.get(), metadata_, local_id, |
| 164 callback); | 164 callback); |
| 165 return; | 165 return; |
| 166 } | 166 } |
| 167 | 167 |
| 168 // Now we're done. If the entry is trashed on the server, it'll be also | 168 // Now we're done. If the entry is trashed on the server, it'll be also |
| 169 // deleted locally on the next update. | 169 // deleted locally on the next update. |
| 170 callback.Run(error); | 170 callback.Run(error); |
| 171 } | 171 } |
| 172 | 172 |
| 173 void RemovePerformer::UnparentResource(const ClientContext& context, | 173 void RemovePerformer::UnparentResource(const ClientContext& context, |
| 174 const FileOperationCallback& callback, | 174 const FileOperationCallback& callback, |
| 175 const std::string& resource_id, | 175 const std::string& resource_id, |
| 176 const std::string& local_id) { | 176 const std::string& local_id) { |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 178 DCHECK(!callback.is_null()); | 178 DCHECK(!callback.is_null()); |
| 179 | 179 |
| 180 scheduler_->GetFileResource( | 180 scheduler_->GetFileResource( |
| 181 resource_id, | 181 resource_id, |
| 182 context, | 182 context, |
| 183 base::Bind(&RemovePerformer::UnparentResourceAfterGetFileResource, | 183 base::Bind(&RemovePerformer::UnparentResourceAfterGetFileResource, |
| 184 weak_ptr_factory_.GetWeakPtr(), context, callback, local_id)); | 184 weak_ptr_factory_.GetWeakPtr(), context, callback, local_id)); |
| 185 } | 185 } |
| 186 | 186 |
| 187 void RemovePerformer::UnparentResourceAfterGetFileResource( | 187 void RemovePerformer::UnparentResourceAfterGetFileResource( |
| 188 const ClientContext& context, | 188 const ClientContext& context, |
| 189 const FileOperationCallback& callback, | 189 const FileOperationCallback& callback, |
| 190 const std::string& local_id, | 190 const std::string& local_id, |
| 191 google_apis::DriveApiErrorCode status, | 191 google_apis::DriveApiErrorCode status, |
| 192 scoped_ptr<google_apis::FileResource> file_resource) { | 192 scoped_ptr<google_apis::FileResource> file_resource) { |
| 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 193 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 194 DCHECK(!callback.is_null()); | 194 DCHECK(!callback.is_null()); |
| 195 | 195 |
| 196 FileError error = GDataToFileError(status); | 196 FileError error = GDataToFileError(status); |
| 197 if (error == FILE_ERROR_NOT_FOUND) { // Remove local entry when not found. | 197 if (error == FILE_ERROR_NOT_FOUND) { // Remove local entry when not found. |
| 198 RemoveEntryOnUIThread(blocking_task_runner_.get(), metadata_, local_id, | 198 RemoveEntryOnUIThread(blocking_task_runner_.get(), metadata_, local_id, |
| 199 callback); | 199 callback); |
| 200 return; | 200 return; |
| 201 } | 201 } |
| 202 | 202 |
| 203 if (error != FILE_ERROR_OK) { | 203 if (error != FILE_ERROR_OK) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 232 entry.resource_id(), | 232 entry.resource_id(), |
| 233 context, | 233 context, |
| 234 base::Bind(&RemovePerformer::UnparentResourceAfterUpdateRemoteState, | 234 base::Bind(&RemovePerformer::UnparentResourceAfterUpdateRemoteState, |
| 235 weak_ptr_factory_.GetWeakPtr(), callback, local_id)); | 235 weak_ptr_factory_.GetWeakPtr(), callback, local_id)); |
| 236 } | 236 } |
| 237 | 237 |
| 238 void RemovePerformer::UnparentResourceAfterUpdateRemoteState( | 238 void RemovePerformer::UnparentResourceAfterUpdateRemoteState( |
| 239 const FileOperationCallback& callback, | 239 const FileOperationCallback& callback, |
| 240 const std::string& local_id, | 240 const std::string& local_id, |
| 241 google_apis::DriveApiErrorCode status) { | 241 google_apis::DriveApiErrorCode status) { |
| 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 242 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 243 DCHECK(!callback.is_null()); | 243 DCHECK(!callback.is_null()); |
| 244 | 244 |
| 245 FileError error = GDataToFileError(status); | 245 FileError error = GDataToFileError(status); |
| 246 if (error != FILE_ERROR_OK) { | 246 if (error != FILE_ERROR_OK) { |
| 247 callback.Run(error); | 247 callback.Run(error); |
| 248 return; | 248 return; |
| 249 } | 249 } |
| 250 | 250 |
| 251 base::PostTaskAndReplyWithResult( | 251 base::PostTaskAndReplyWithResult( |
| 252 blocking_task_runner_.get(), | 252 blocking_task_runner_.get(), |
| 253 FROM_HERE, | 253 FROM_HERE, |
| 254 base::Bind(&UpdateLocalStateAfterUnparent, metadata_, local_id), | 254 base::Bind(&UpdateLocalStateAfterUnparent, metadata_, local_id), |
| 255 callback); | 255 callback); |
| 256 } | 256 } |
| 257 | 257 |
| 258 } // namespace internal | 258 } // namespace internal |
| 259 } // namespace drive | 259 } // namespace drive |
| OLD | NEW |