Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / sync / entry_revert_performer.cc
blob221fd0b4abdce402efebb41b956278def4ba61b7
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/drive.pb.h"
9 #include "chrome/browser/chromeos/drive/file_change.h"
10 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
11 #include "chrome/browser/chromeos/drive/job_scheduler.h"
12 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
13 #include "chrome/browser/chromeos/drive/resource_metadata.h"
14 #include "chrome/browser/drive/drive_api_util.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "google_apis/drive/drive_api_parser.h"
18 using content::BrowserThread;
20 namespace drive {
21 namespace internal {
22 namespace {
24 FileError FinishRevert(ResourceMetadata* metadata,
25 const std::string& local_id,
26 google_apis::DriveApiErrorCode status,
27 scoped_ptr<google_apis::FileResource> file_resource,
28 FileChange* changed_files) {
29 ResourceEntry entry;
30 std::string parent_resource_id;
31 FileError error = GDataToFileError(status);
32 switch (error) {
33 case FILE_ERROR_OK:
34 if (!ConvertFileResourceToResourceEntry(*file_resource, &entry,
35 &parent_resource_id))
36 return FILE_ERROR_NOT_A_FILE;
37 break;
39 case FILE_ERROR_NOT_FOUND:
40 entry.set_deleted(true);
41 break;
43 default:
44 return error;
47 base::FilePath original_path;
48 error = metadata->GetFilePath(local_id, &original_path);
49 if (error != FILE_ERROR_OK)
50 return error;
52 if (entry.deleted()) {
53 error = metadata->RemoveEntry(local_id);
54 if (error != FILE_ERROR_OK)
55 return error;
57 changed_files->Update(
58 original_path,
59 FileChange::FILE_TYPE_UNKNOWN, // Undetermined type for deleted file.
60 FileChange::DELETE);
61 } else {
62 changed_files->Update(original_path, entry, FileChange::DELETE);
64 error = ChangeListProcessor::SetParentLocalIdOfEntry(metadata, &entry,
65 parent_resource_id);
66 if (error != FILE_ERROR_OK)
67 return error;
69 entry.set_local_id(local_id);
70 error = metadata->RefreshEntry(entry);
71 if (error != FILE_ERROR_OK)
72 return error;
74 base::FilePath new_path;
75 error = metadata->GetFilePath(local_id, &new_path);
76 if (error != FILE_ERROR_OK)
77 return error;
79 changed_files->Update(new_path, entry, FileChange::ADD_OR_UPDATE);
81 return FILE_ERROR_OK;
84 } // namespace
86 EntryRevertPerformer::EntryRevertPerformer(
87 base::SequencedTaskRunner* blocking_task_runner,
88 file_system::OperationDelegate* delegate,
89 JobScheduler* scheduler,
90 ResourceMetadata* metadata)
91 : blocking_task_runner_(blocking_task_runner),
92 delegate_(delegate),
93 scheduler_(scheduler),
94 metadata_(metadata),
95 weak_ptr_factory_(this) {
96 DCHECK_CURRENTLY_ON(BrowserThread::UI);
99 EntryRevertPerformer::~EntryRevertPerformer() {
100 DCHECK_CURRENTLY_ON(BrowserThread::UI);
103 void EntryRevertPerformer::RevertEntry(const std::string& local_id,
104 const ClientContext& context,
105 const FileOperationCallback& callback) {
106 DCHECK_CURRENTLY_ON(BrowserThread::UI);
107 DCHECK(!callback.is_null());
109 scoped_ptr<ResourceEntry> entry(new ResourceEntry);
110 ResourceEntry* entry_ptr = entry.get();
111 base::PostTaskAndReplyWithResult(
112 blocking_task_runner_.get(),
113 FROM_HERE,
114 base::Bind(&ResourceMetadata::GetResourceEntryById,
115 base::Unretained(metadata_), local_id, entry_ptr),
116 base::Bind(&EntryRevertPerformer::RevertEntryAfterPrepare,
117 weak_ptr_factory_.GetWeakPtr(), context, callback,
118 base::Passed(&entry)));
121 void EntryRevertPerformer::RevertEntryAfterPrepare(
122 const ClientContext& context,
123 const FileOperationCallback& callback,
124 scoped_ptr<ResourceEntry> entry,
125 FileError error) {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI);
127 DCHECK(!callback.is_null());
129 if (error == FILE_ERROR_OK && entry->resource_id().empty())
130 error = FILE_ERROR_INVALID_OPERATION;
132 if (error != FILE_ERROR_OK) {
133 callback.Run(error);
134 return;
137 scheduler_->GetFileResource(
138 entry->resource_id(),
139 context,
140 base::Bind(&EntryRevertPerformer::RevertEntryAfterGetFileResource,
141 weak_ptr_factory_.GetWeakPtr(), callback, entry->local_id()));
144 void EntryRevertPerformer::RevertEntryAfterGetFileResource(
145 const FileOperationCallback& callback,
146 const std::string& local_id,
147 google_apis::DriveApiErrorCode status,
148 scoped_ptr<google_apis::FileResource> entry) {
149 DCHECK_CURRENTLY_ON(BrowserThread::UI);
150 DCHECK(!callback.is_null());
152 FileChange* changed_files = new FileChange;
153 base::PostTaskAndReplyWithResult(
154 blocking_task_runner_.get(),
155 FROM_HERE,
156 base::Bind(&FinishRevert,
157 metadata_,
158 local_id,
159 status,
160 base::Passed(&entry),
161 changed_files),
162 base::Bind(&EntryRevertPerformer::RevertEntryAfterFinishRevert,
163 weak_ptr_factory_.GetWeakPtr(),
164 callback,
165 base::Owned(changed_files)));
168 void EntryRevertPerformer::RevertEntryAfterFinishRevert(
169 const FileOperationCallback& callback,
170 const FileChange* changed_files,
171 FileError error) {
172 DCHECK_CURRENTLY_ON(BrowserThread::UI);
173 DCHECK(!callback.is_null());
175 delegate_->OnFileChangedByOperation(*changed_files);
177 callback.Run(error);
180 } // namespace internal
181 } // namespace drive