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
;
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
) {
30 std::string parent_resource_id
;
31 FileError error
= GDataToFileError(status
);
34 if (!ConvertFileResourceToResourceEntry(*file_resource
, &entry
,
36 return FILE_ERROR_NOT_A_FILE
;
39 case FILE_ERROR_NOT_FOUND
:
40 entry
.set_deleted(true);
47 base::FilePath original_path
;
48 error
= metadata
->GetFilePath(local_id
, &original_path
);
49 if (error
!= FILE_ERROR_OK
)
52 if (entry
.deleted()) {
53 error
= metadata
->RemoveEntry(local_id
);
54 if (error
!= FILE_ERROR_OK
)
57 changed_files
->Update(
59 FileChange::FILE_TYPE_UNKNOWN
, // Undetermined type for deleted file.
62 changed_files
->Update(original_path
, entry
, FileChange::DELETE
);
64 error
= ChangeListProcessor::SetParentLocalIdOfEntry(metadata
, &entry
,
66 if (error
!= FILE_ERROR_OK
)
69 entry
.set_local_id(local_id
);
70 error
= metadata
->RefreshEntry(entry
);
71 if (error
!= FILE_ERROR_OK
)
74 base::FilePath new_path
;
75 error
= metadata
->GetFilePath(local_id
, &new_path
);
76 if (error
!= FILE_ERROR_OK
)
79 changed_files
->Update(new_path
, entry
, FileChange::ADD_OR_UPDATE
);
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
),
93 scheduler_(scheduler
),
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(),
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
,
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
) {
137 scheduler_
->GetFileResource(
138 entry
->resource_id(),
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(),
156 base::Bind(&FinishRevert
,
160 base::Passed(&entry
),
162 base::Bind(&EntryRevertPerformer::RevertEntryAfterFinishRevert
,
163 weak_ptr_factory_
.GetWeakPtr(),
165 base::Owned(changed_files
)));
168 void EntryRevertPerformer::RevertEntryAfterFinishRevert(
169 const FileOperationCallback
& callback
,
170 const FileChange
* changed_files
,
172 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
173 DCHECK(!callback
.is_null());
175 delegate_
->OnFileChangedByOperation(*changed_files
);
180 } // namespace internal