Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / sync / entry_revert_performer.cc
blob478ddfd19b69b1a15d1fd7dbd465b92d03f0eb63
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 #include "chrome/browser/chromeos/drive/sync/entry_revert_performer.h"
7 #include "chrome/browser/chromeos/drive/change_list_processor.h"
8 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
9 #include "components/drive/drive.pb.h"
10 #include "components/drive/drive_api_util.h"
11 #include "components/drive/file_change.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"
17 namespace drive {
18 namespace internal {
19 namespace {
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;
36 case FILE_ERROR_NOT_FOUND:
37 entry.set_deleted(true);
38 break;
40 default:
41 return error;
44 base::FilePath original_path;
45 error = metadata->GetFilePath(local_id, &original_path);
46 if (error != FILE_ERROR_OK)
47 return error;
49 if (entry.deleted()) {
50 error = metadata->RemoveEntry(local_id);
51 if (error != FILE_ERROR_OK)
52 return error;
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);
61 error = ChangeListProcessor::SetParentLocalIdOfEntry(metadata, &entry,
62 parent_resource_id);
63 if (error != FILE_ERROR_OK)
64 return error;
66 entry.set_local_id(local_id);
67 error = metadata->RefreshEntry(entry);
68 if (error != FILE_ERROR_OK)
69 return error;
71 base::FilePath new_path;
72 error = metadata->GetFilePath(local_id, &new_path);
73 if (error != FILE_ERROR_OK)
74 return error;
76 changed_files->Update(new_path, entry,
77 FileChange::CHANGE_TYPE_ADD_OR_UPDATE);
79 return FILE_ERROR_OK;
82 } // namespace
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) {
96 EntryRevertPerformer::~EntryRevertPerformer() {
97 DCHECK(thread_checker_.CalledOnValidThread());
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());
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)));
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());
126 if (error == FILE_ERROR_OK && entry->resource_id().empty())
127 error = FILE_ERROR_INVALID_OPERATION;
129 if (error != FILE_ERROR_OK) {
130 callback.Run(error);
131 return;
134 scheduler_->GetFileResource(
135 entry->resource_id(),
136 context,
137 base::Bind(&EntryRevertPerformer::RevertEntryAfterGetFileResource,
138 weak_ptr_factory_.GetWeakPtr(), callback, entry->local_id()));
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());
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)));
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());
172 delegate_->OnFileChangedByOperation(*changed_files);
174 callback.Run(error);
177 } // namespace internal
178 } // namespace drive