Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / sync / entry_revert_performer.cc
blobe4f895fc8e855c6e1681a0b27aca2c5d3dd56c26
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_system/operation_observer.h"
10 #include "chrome/browser/chromeos/drive/job_scheduler.h"
11 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
12 #include "chrome/browser/chromeos/drive/resource_metadata.h"
13 #include "content/public/browser/browser_thread.h"
15 using content::BrowserThread;
17 namespace drive {
18 namespace internal {
19 namespace {
21 FileError FinishRevert(ResourceMetadata* metadata,
22 const std::string& local_id,
23 google_apis::GDataErrorCode status,
24 scoped_ptr<google_apis::ResourceEntry> resource_entry,
25 std::set<base::FilePath>* changed_directories) {
26 ResourceEntry entry;
27 std::string parent_resource_id;
28 FileError error = GDataToFileError(status);
29 switch (error) {
30 case FILE_ERROR_OK:
31 if (!ConvertToResourceEntry(*resource_entry, &entry, &parent_resource_id))
32 return FILE_ERROR_NOT_A_FILE;
33 break;
35 case FILE_ERROR_NOT_FOUND:
36 entry.set_deleted(true);
37 break;
39 default:
40 return error;
43 const base::FilePath original_path = metadata->GetFilePath(local_id);
45 if (entry.deleted()) {
46 error = metadata->RemoveEntry(local_id);
47 if (error != FILE_ERROR_OK)
48 return error;
50 changed_directories->insert(original_path.DirName());
51 } else {
52 error = ChangeListProcessor::SetParentLocalIdOfEntry(metadata, &entry,
53 parent_resource_id);
54 if (error != FILE_ERROR_OK)
55 return error;
57 entry.set_local_id(local_id);
58 error = metadata->RefreshEntry(entry);
59 if (error != FILE_ERROR_OK)
60 return error;
62 changed_directories->insert(metadata->GetFilePath(entry.parent_local_id()));
63 changed_directories->insert(original_path.DirName());
65 return FILE_ERROR_OK;
68 } // namespace
70 EntryRevertPerformer::EntryRevertPerformer(
71 base::SequencedTaskRunner* blocking_task_runner,
72 file_system::OperationObserver* observer,
73 JobScheduler* scheduler,
74 ResourceMetadata* metadata)
75 : blocking_task_runner_(blocking_task_runner),
76 observer_(observer),
77 scheduler_(scheduler),
78 metadata_(metadata),
79 weak_ptr_factory_(this) {
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 EntryRevertPerformer::~EntryRevertPerformer() {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
87 void EntryRevertPerformer::RevertEntry(const std::string& local_id,
88 const FileOperationCallback& callback) {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90 DCHECK(!callback.is_null());
92 scoped_ptr<ResourceEntry> entry(new ResourceEntry);
93 ResourceEntry* entry_ptr = entry.get();
94 base::PostTaskAndReplyWithResult(
95 blocking_task_runner_.get(),
96 FROM_HERE,
97 base::Bind(&ResourceMetadata::GetResourceEntryById,
98 base::Unretained(metadata_), local_id, entry_ptr),
99 base::Bind(&EntryRevertPerformer::RevertEntryAfterPrepare,
100 weak_ptr_factory_.GetWeakPtr(), callback,
101 base::Passed(&entry)));
104 void EntryRevertPerformer::RevertEntryAfterPrepare(
105 const FileOperationCallback& callback,
106 scoped_ptr<ResourceEntry> entry,
107 FileError error) {
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
109 DCHECK(!callback.is_null());
111 if (error != FILE_ERROR_OK) {
112 callback.Run(error);
113 return;
116 scheduler_->GetResourceEntry(
117 entry->resource_id(),
118 ClientContext(BACKGROUND),
119 base::Bind(&EntryRevertPerformer::RevertEntryAfterGetResourceEntry,
120 weak_ptr_factory_.GetWeakPtr(), callback, entry->local_id()));
123 void EntryRevertPerformer::RevertEntryAfterGetResourceEntry(
124 const FileOperationCallback& callback,
125 const std::string& local_id,
126 google_apis::GDataErrorCode status,
127 scoped_ptr<google_apis::ResourceEntry> resource_entry) {
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
129 DCHECK(!callback.is_null());
131 std::set<base::FilePath>* changed_directories = new std::set<base::FilePath>;
132 base::PostTaskAndReplyWithResult(
133 blocking_task_runner_.get(),
134 FROM_HERE,
135 base::Bind(&FinishRevert, metadata_, local_id, status,
136 base::Passed(&resource_entry), changed_directories),
137 base::Bind(&EntryRevertPerformer::RevertEntryAfterFinishRevert,
138 weak_ptr_factory_.GetWeakPtr(), callback,
139 base::Owned(changed_directories)));
142 void EntryRevertPerformer::RevertEntryAfterFinishRevert(
143 const FileOperationCallback& callback,
144 const std::set<base::FilePath>* changed_directories,
145 FileError error) {
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
147 DCHECK(!callback.is_null());
149 for (std::set<base::FilePath>::const_iterator it =
150 changed_directories->begin(); it != changed_directories->end(); ++it)
151 observer_->OnDirectoryChangedByOperation(*it);
153 callback.Run(error);
156 } // namespace internal
157 } // namespace drive