| 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 #include "chrome/browser/chromeos/drive/sync/entry_revert_performer.h" | |
| 6 | |
| 7 #include "components/drive/change_list_processor.h" | |
| 8 #include "components/drive/drive.pb.h" | |
| 9 #include "components/drive/drive_api_util.h" | |
| 10 #include "components/drive/file_change.h" | |
| 11 #include "components/drive/file_system/operation_delegate.h" | |
| 12 #include "components/drive/job_scheduler.h" | |
| 13 #include "components/drive/resource_entry_conversion.h" | |
| 14 #include "components/drive/resource_metadata.h" | |
| 15 #include "google_apis/drive/drive_api_parser.h" | |
| 16 | |
| 17 namespace drive { | |
| 18 namespace internal { | |
| 19 namespace { | |
| 20 | |
| 21 FileError FinishRevert(ResourceMetadata* metadata, | |
| 22 const std::string& local_id, | |
| 23 google_apis::DriveApiErrorCode status, | |
| 24 scoped_ptr<google_apis::FileResource> file_resource, | |
| 25 FileChange* changed_files) { | |
| 26 ResourceEntry entry; | |
| 27 std::string parent_resource_id; | |
| 28 FileError error = GDataToFileError(status); | |
| 29 switch (error) { | |
| 30 case FILE_ERROR_OK: | |
| 31 if (!ConvertFileResourceToResourceEntry(*file_resource, &entry, | |
| 32 &parent_resource_id)) | |
| 33 return FILE_ERROR_NOT_A_FILE; | |
| 34 break; | |
| 35 | |
| 36 case FILE_ERROR_NOT_FOUND: | |
| 37 entry.set_deleted(true); | |
| 38 break; | |
| 39 | |
| 40 default: | |
| 41 return error; | |
| 42 } | |
| 43 | |
| 44 base::FilePath original_path; | |
| 45 error = metadata->GetFilePath(local_id, &original_path); | |
| 46 if (error != FILE_ERROR_OK) | |
| 47 return error; | |
| 48 | |
| 49 if (entry.deleted()) { | |
| 50 error = metadata->RemoveEntry(local_id); | |
| 51 if (error != FILE_ERROR_OK) | |
| 52 return error; | |
| 53 | |
| 54 changed_files->Update( | |
| 55 original_path, | |
| 56 FileChange::FILE_TYPE_NO_INFO, // Undetermined type for deleted file. | |
| 57 FileChange::CHANGE_TYPE_DELETE); | |
| 58 } else { | |
| 59 changed_files->Update(original_path, entry, FileChange::CHANGE_TYPE_DELETE); | |
| 60 | |
| 61 error = ChangeListProcessor::SetParentLocalIdOfEntry(metadata, &entry, | |
| 62 parent_resource_id); | |
| 63 if (error != FILE_ERROR_OK) | |
| 64 return error; | |
| 65 | |
| 66 entry.set_local_id(local_id); | |
| 67 error = metadata->RefreshEntry(entry); | |
| 68 if (error != FILE_ERROR_OK) | |
| 69 return error; | |
| 70 | |
| 71 base::FilePath new_path; | |
| 72 error = metadata->GetFilePath(local_id, &new_path); | |
| 73 if (error != FILE_ERROR_OK) | |
| 74 return error; | |
| 75 | |
| 76 changed_files->Update(new_path, entry, | |
| 77 FileChange::CHANGE_TYPE_ADD_OR_UPDATE); | |
| 78 } | |
| 79 return FILE_ERROR_OK; | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 EntryRevertPerformer::EntryRevertPerformer( | |
| 85 base::SequencedTaskRunner* blocking_task_runner, | |
| 86 file_system::OperationDelegate* delegate, | |
| 87 JobScheduler* scheduler, | |
| 88 ResourceMetadata* metadata) | |
| 89 : blocking_task_runner_(blocking_task_runner), | |
| 90 delegate_(delegate), | |
| 91 scheduler_(scheduler), | |
| 92 metadata_(metadata), | |
| 93 weak_ptr_factory_(this) { | |
| 94 } | |
| 95 | |
| 96 EntryRevertPerformer::~EntryRevertPerformer() { | |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 98 } | |
| 99 | |
| 100 void EntryRevertPerformer::RevertEntry(const std::string& local_id, | |
| 101 const ClientContext& context, | |
| 102 const FileOperationCallback& callback) { | |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 104 DCHECK(!callback.is_null()); | |
| 105 | |
| 106 scoped_ptr<ResourceEntry> entry(new ResourceEntry); | |
| 107 ResourceEntry* entry_ptr = entry.get(); | |
| 108 base::PostTaskAndReplyWithResult( | |
| 109 blocking_task_runner_.get(), | |
| 110 FROM_HERE, | |
| 111 base::Bind(&ResourceMetadata::GetResourceEntryById, | |
| 112 base::Unretained(metadata_), local_id, entry_ptr), | |
| 113 base::Bind(&EntryRevertPerformer::RevertEntryAfterPrepare, | |
| 114 weak_ptr_factory_.GetWeakPtr(), context, callback, | |
| 115 base::Passed(&entry))); | |
| 116 } | |
| 117 | |
| 118 void EntryRevertPerformer::RevertEntryAfterPrepare( | |
| 119 const ClientContext& context, | |
| 120 const FileOperationCallback& callback, | |
| 121 scoped_ptr<ResourceEntry> entry, | |
| 122 FileError error) { | |
| 123 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 124 DCHECK(!callback.is_null()); | |
| 125 | |
| 126 if (error == FILE_ERROR_OK && entry->resource_id().empty()) | |
| 127 error = FILE_ERROR_INVALID_OPERATION; | |
| 128 | |
| 129 if (error != FILE_ERROR_OK) { | |
| 130 callback.Run(error); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 scheduler_->GetFileResource( | |
| 135 entry->resource_id(), | |
| 136 context, | |
| 137 base::Bind(&EntryRevertPerformer::RevertEntryAfterGetFileResource, | |
| 138 weak_ptr_factory_.GetWeakPtr(), callback, entry->local_id())); | |
| 139 } | |
| 140 | |
| 141 void EntryRevertPerformer::RevertEntryAfterGetFileResource( | |
| 142 const FileOperationCallback& callback, | |
| 143 const std::string& local_id, | |
| 144 google_apis::DriveApiErrorCode status, | |
| 145 scoped_ptr<google_apis::FileResource> entry) { | |
| 146 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 147 DCHECK(!callback.is_null()); | |
| 148 | |
| 149 FileChange* changed_files = new FileChange; | |
| 150 base::PostTaskAndReplyWithResult( | |
| 151 blocking_task_runner_.get(), | |
| 152 FROM_HERE, | |
| 153 base::Bind(&FinishRevert, | |
| 154 metadata_, | |
| 155 local_id, | |
| 156 status, | |
| 157 base::Passed(&entry), | |
| 158 changed_files), | |
| 159 base::Bind(&EntryRevertPerformer::RevertEntryAfterFinishRevert, | |
| 160 weak_ptr_factory_.GetWeakPtr(), | |
| 161 callback, | |
| 162 base::Owned(changed_files))); | |
| 163 } | |
| 164 | |
| 165 void EntryRevertPerformer::RevertEntryAfterFinishRevert( | |
| 166 const FileOperationCallback& callback, | |
| 167 const FileChange* changed_files, | |
| 168 FileError error) { | |
| 169 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 170 DCHECK(!callback.is_null()); | |
| 171 | |
| 172 delegate_->OnFileChangedByOperation(*changed_files); | |
| 173 | |
| 174 callback.Run(error); | |
| 175 } | |
| 176 | |
| 177 } // namespace internal | |
| 178 } // namespace drive | |
| OLD | NEW |