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
;
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
) {
27 std::string parent_resource_id
;
28 FileError error
= GDataToFileError(status
);
31 if (!ConvertToResourceEntry(*resource_entry
, &entry
, &parent_resource_id
))
32 return FILE_ERROR_NOT_A_FILE
;
35 case FILE_ERROR_NOT_FOUND
:
36 entry
.set_deleted(true);
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
)
50 changed_directories
->insert(original_path
.DirName());
52 error
= ChangeListProcessor::SetParentLocalIdOfEntry(metadata
, &entry
,
54 if (error
!= FILE_ERROR_OK
)
57 entry
.set_local_id(local_id
);
58 error
= metadata
->RefreshEntry(entry
);
59 if (error
!= FILE_ERROR_OK
)
62 changed_directories
->insert(metadata
->GetFilePath(entry
.parent_local_id()));
63 changed_directories
->insert(original_path
.DirName());
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
),
77 scheduler_(scheduler
),
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(),
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
,
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
109 DCHECK(!callback
.is_null());
111 if (error
!= FILE_ERROR_OK
) {
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(),
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
,
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
);
156 } // namespace internal