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"
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
) {
27 std::string parent_resource_id
;
28 FileError error
= GDataToFileError(status
);
31 if (!ConvertFileResourceToResourceEntry(*file_resource
, &entry
,
33 return FILE_ERROR_NOT_A_FILE
;
36 case FILE_ERROR_NOT_FOUND
:
37 entry
.set_deleted(true);
44 base::FilePath original_path
;
45 error
= metadata
->GetFilePath(local_id
, &original_path
);
46 if (error
!= FILE_ERROR_OK
)
49 if (entry
.deleted()) {
50 error
= metadata
->RemoveEntry(local_id
);
51 if (error
!= FILE_ERROR_OK
)
54 changed_files
->Update(
56 FileChange::FILE_TYPE_NO_INFO
, // Undetermined type for deleted file.
57 FileChange::CHANGE_TYPE_DELETE
);
59 changed_files
->Update(original_path
, entry
, FileChange::CHANGE_TYPE_DELETE
);
61 error
= ChangeListProcessor::SetParentLocalIdOfEntry(metadata
, &entry
,
63 if (error
!= FILE_ERROR_OK
)
66 entry
.set_local_id(local_id
);
67 error
= metadata
->RefreshEntry(entry
);
68 if (error
!= FILE_ERROR_OK
)
71 base::FilePath new_path
;
72 error
= metadata
->GetFilePath(local_id
, &new_path
);
73 if (error
!= FILE_ERROR_OK
)
76 changed_files
->Update(new_path
, entry
,
77 FileChange::CHANGE_TYPE_ADD_OR_UPDATE
);
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
),
91 scheduler_(scheduler
),
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(),
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
,
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
) {
134 scheduler_
->GetFileResource(
135 entry
->resource_id(),
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(),
153 base::Bind(&FinishRevert
,
157 base::Passed(&entry
),
159 base::Bind(&EntryRevertPerformer::RevertEntryAfterFinishRevert
,
160 weak_ptr_factory_
.GetWeakPtr(),
162 base::Owned(changed_files
)));
165 void EntryRevertPerformer::RevertEntryAfterFinishRevert(
166 const FileOperationCallback
& callback
,
167 const FileChange
* changed_files
,
169 DCHECK(thread_checker_
.CalledOnValidThread());
170 DCHECK(!callback
.is_null());
172 delegate_
->OnFileChangedByOperation(*changed_files
);
177 } // namespace internal