| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/file_system/remove_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/remove_operation.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_cache.h" | 9 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 10 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" | 10 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
| 11 #include "chrome/browser/chromeos/drive/file_system_util.h" | 11 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 12 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 12 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 | 14 |
| 15 using content::BrowserThread; | 15 using content::BrowserThread; |
| 16 | 16 |
| 17 namespace drive { | 17 namespace drive { |
| 18 namespace file_system { | 18 namespace file_system { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Checks local metadata state before requesting remote delete. | 22 // Checks local metadata state before requesting remote delete. |
| 23 // |parent_resource_id| is set to the resource ID of the parent directory of | 23 // |parent_resource_id| is set to the resource ID of the parent directory of |
| 24 // |path|. If it is a special folder like drive/other, empty string is set. | 24 // |path|. If it is a special folder like drive/other, empty string is set. |
| 25 // |local_id| is set to the local ID of the entry located at |path|. | |
| 26 // |entry| is the resource entry for the |path|. | 25 // |entry| is the resource entry for the |path|. |
| 27 FileError CheckLocalState(internal::ResourceMetadata* metadata, | 26 FileError CheckLocalState(internal::ResourceMetadata* metadata, |
| 28 const base::FilePath& path, | 27 const base::FilePath& path, |
| 29 bool is_recursive, | 28 bool is_recursive, |
| 30 std::string* parent_resource_id, | 29 std::string* parent_resource_id, |
| 31 std::string* local_id, | |
| 32 ResourceEntry* entry) { | 30 ResourceEntry* entry) { |
| 33 FileError error = metadata->GetIdByPath(path, local_id); | 31 std::string local_id; |
| 32 FileError error = metadata->GetIdByPath(path, &local_id); |
| 34 if (error != FILE_ERROR_OK) | 33 if (error != FILE_ERROR_OK) |
| 35 return error; | 34 return error; |
| 36 | 35 |
| 37 error = metadata->GetResourceEntryById(*local_id, entry); | 36 error = metadata->GetResourceEntryById(local_id, entry); |
| 38 if (error != FILE_ERROR_OK) | 37 if (error != FILE_ERROR_OK) |
| 39 return error; | 38 return error; |
| 40 | 39 |
| 41 if (entry->file_info().is_directory() && !is_recursive) { | 40 if (entry->file_info().is_directory() && !is_recursive) { |
| 42 // Check emptiness of the directory. | 41 // Check emptiness of the directory. |
| 43 ResourceEntryVector entries; | 42 ResourceEntryVector entries; |
| 44 error = metadata->ReadDirectoryByPath(path, &entries); | 43 error = metadata->ReadDirectoryByPath(path, &entries); |
| 45 if (error != FILE_ERROR_OK) | 44 if (error != FILE_ERROR_OK) |
| 46 return error; | 45 return error; |
| 47 if (!entries.empty()) | 46 if (!entries.empty()) |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 117 } | 116 } |
| 118 | 117 |
| 119 void RemoveOperation::Remove(const base::FilePath& path, | 118 void RemoveOperation::Remove(const base::FilePath& path, |
| 120 bool is_recursive, | 119 bool is_recursive, |
| 121 const FileOperationCallback& callback) { | 120 const FileOperationCallback& callback) { |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 123 DCHECK(!callback.is_null()); | 122 DCHECK(!callback.is_null()); |
| 124 | 123 |
| 125 std::string* parent_resource_id = new std::string; | 124 std::string* parent_resource_id = new std::string; |
| 126 std::string* local_id = new std::string; | |
| 127 ResourceEntry* entry = new ResourceEntry; | 125 ResourceEntry* entry = new ResourceEntry; |
| 128 base::PostTaskAndReplyWithResult( | 126 base::PostTaskAndReplyWithResult( |
| 129 blocking_task_runner_.get(), | 127 blocking_task_runner_.get(), |
| 130 FROM_HERE, | 128 FROM_HERE, |
| 131 base::Bind(&CheckLocalState, | 129 base::Bind(&CheckLocalState, |
| 132 metadata_, | 130 metadata_, |
| 133 path, | 131 path, |
| 134 is_recursive, | 132 is_recursive, |
| 135 parent_resource_id, | 133 parent_resource_id, |
| 136 local_id, | |
| 137 entry), | 134 entry), |
| 138 base::Bind(&RemoveOperation::RemoveAfterCheckLocalState, | 135 base::Bind(&RemoveOperation::RemoveAfterCheckLocalState, |
| 139 weak_ptr_factory_.GetWeakPtr(), | 136 weak_ptr_factory_.GetWeakPtr(), |
| 140 callback, | 137 callback, |
| 141 base::Owned(parent_resource_id), | 138 base::Owned(parent_resource_id), |
| 142 base::Owned(local_id), | |
| 143 base::Owned(entry))); | 139 base::Owned(entry))); |
| 144 } | 140 } |
| 145 | 141 |
| 146 void RemoveOperation::RemoveAfterCheckLocalState( | 142 void RemoveOperation::RemoveAfterCheckLocalState( |
| 147 const FileOperationCallback& callback, | 143 const FileOperationCallback& callback, |
| 148 const std::string* parent_resource_id, | 144 const std::string* parent_resource_id, |
| 149 const std::string* local_id, | |
| 150 const ResourceEntry* entry, | 145 const ResourceEntry* entry, |
| 151 FileError error) { | 146 FileError error) { |
| 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 153 DCHECK(!callback.is_null()); | 148 DCHECK(!callback.is_null()); |
| 154 | 149 |
| 155 if (error != FILE_ERROR_OK) { | 150 if (error != FILE_ERROR_OK) { |
| 156 callback.Run(error); | 151 callback.Run(error); |
| 157 return; | 152 return; |
| 158 } | 153 } |
| 159 | 154 |
| 160 // To match with the behavior of drive.google.com: | 155 // To match with the behavior of drive.google.com: |
| 161 // Removal of shared entries under MyDrive is just removing from the parent. | 156 // Removal of shared entries under MyDrive is just removing from the parent. |
| 162 // The entry will stay in shared-with-me (in other words, in "drive/other".) | 157 // The entry will stay in shared-with-me (in other words, in "drive/other".) |
| 163 // | 158 // |
| 164 // TODO(kinaba): to be more precise, we might be better to branch by whether | 159 // TODO(kinaba): to be more precise, we might be better to branch by whether |
| 165 // or not the current account is an owner of the file. The code below is | 160 // or not the current account is an owner of the file. The code below is |
| 166 // written under the assumption that |shared_with_me| coincides with that. | 161 // written under the assumption that |shared_with_me| coincides with that. |
| 167 if (entry->shared_with_me() && !parent_resource_id->empty()) { | 162 if (entry->shared_with_me() && !parent_resource_id->empty()) { |
| 168 scheduler_->RemoveResourceFromDirectory( | 163 scheduler_->RemoveResourceFromDirectory( |
| 169 *parent_resource_id, | 164 *parent_resource_id, |
| 170 entry->resource_id(), | 165 entry->resource_id(), |
| 171 base::Bind(&RemoveOperation::RemoveAfterUpdateRemoteState, | 166 base::Bind(&RemoveOperation::RemoveAfterUpdateRemoteState, |
| 172 weak_ptr_factory_.GetWeakPtr(), | 167 weak_ptr_factory_.GetWeakPtr(), |
| 173 callback, | 168 callback, |
| 174 base::Bind(&UpdateLocalStateAfterUnparent, | 169 base::Bind(&UpdateLocalStateAfterUnparent, |
| 175 metadata_, | 170 metadata_, |
| 176 *local_id))); | 171 entry->local_id()))); |
| 177 } else { | 172 } else { |
| 178 // Otherwise try sending the entry to trash. | 173 // Otherwise try sending the entry to trash. |
| 179 scheduler_->DeleteResource( | 174 scheduler_->DeleteResource( |
| 180 entry->resource_id(), | 175 entry->resource_id(), |
| 181 base::Bind(&RemoveOperation::RemoveAfterUpdateRemoteState, | 176 base::Bind(&RemoveOperation::RemoveAfterUpdateRemoteState, |
| 182 weak_ptr_factory_.GetWeakPtr(), | 177 weak_ptr_factory_.GetWeakPtr(), |
| 183 callback, | 178 callback, |
| 184 base::Bind(&UpdateLocalStateAfterDelete, | 179 base::Bind(&UpdateLocalStateAfterDelete, |
| 185 metadata_, | 180 metadata_, |
| 186 cache_, | 181 cache_, |
| 187 *local_id))); | 182 entry->local_id()))); |
| 188 } | 183 } |
| 189 } | 184 } |
| 190 | 185 |
| 191 void RemoveOperation::RemoveAfterUpdateRemoteState( | 186 void RemoveOperation::RemoveAfterUpdateRemoteState( |
| 192 const FileOperationCallback& callback, | 187 const FileOperationCallback& callback, |
| 193 const base::Callback<FileError(base::FilePath*)>& local_update_task, | 188 const base::Callback<FileError(base::FilePath*)>& local_update_task, |
| 194 google_apis::GDataErrorCode status) { | 189 google_apis::GDataErrorCode status) { |
| 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 196 DCHECK(!callback.is_null()); | 191 DCHECK(!callback.is_null()); |
| 197 | 192 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 220 DCHECK(!callback.is_null()); | 215 DCHECK(!callback.is_null()); |
| 221 | 216 |
| 222 if (error == FILE_ERROR_OK) | 217 if (error == FILE_ERROR_OK) |
| 223 observer_->OnDirectoryChangedByOperation(*changed_directory_path); | 218 observer_->OnDirectoryChangedByOperation(*changed_directory_path); |
| 224 | 219 |
| 225 callback.Run(error); | 220 callback.Run(error); |
| 226 } | 221 } |
| 227 | 222 |
| 228 } // namespace file_system | 223 } // namespace file_system |
| 229 } // namespace drive | 224 } // namespace drive |
| OLD | NEW |