1 // Copyright (c) 2012 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/file_system/remove_operation.h"
7 #include "base/sequenced_task_runner.h"
8 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
9 #include "components/drive/drive.pb.h"
10 #include "components/drive/file_cache.h"
11 #include "components/drive/file_change.h"
12 #include "components/drive/file_system_core_util.h"
13 #include "components/drive/job_scheduler.h"
14 #include "components/drive/resource_metadata.h"
17 namespace file_system
{
21 // Removes cache file and moves the metadata entry to the trash.
22 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
23 internal::FileCache
* cache
,
24 const base::FilePath
& path
,
26 std::string
* local_id
,
28 base::FilePath
* changed_path
) {
29 FileError error
= metadata
->GetIdByPath(path
, local_id
);
30 if (error
!= FILE_ERROR_OK
)
33 error
= metadata
->GetResourceEntryById(*local_id
, entry
);
34 if (error
!= FILE_ERROR_OK
)
37 if (entry
->file_info().is_directory() && !is_recursive
) {
38 // Check emptiness of the directory.
39 ResourceEntryVector entries
;
40 error
= metadata
->ReadDirectoryByPath(path
, &entries
);
41 if (error
!= FILE_ERROR_OK
)
44 return FILE_ERROR_NOT_EMPTY
;
47 error
= cache
->Remove(*local_id
);
48 if (error
!= FILE_ERROR_OK
)
54 entry
->set_parent_local_id(util::kDriveTrashDirLocalId
);
55 return metadata
->RefreshEntry(*entry
);
60 RemoveOperation::RemoveOperation(
61 base::SequencedTaskRunner
* blocking_task_runner
,
62 OperationDelegate
* delegate
,
63 internal::ResourceMetadata
* metadata
,
64 internal::FileCache
* cache
)
65 : blocking_task_runner_(blocking_task_runner
),
69 weak_ptr_factory_(this) {
72 RemoveOperation::~RemoveOperation() {
73 DCHECK(thread_checker_
.CalledOnValidThread());
76 void RemoveOperation::Remove(const base::FilePath
& path
,
78 const FileOperationCallback
& callback
) {
79 DCHECK(thread_checker_
.CalledOnValidThread());
80 DCHECK(!callback
.is_null());
82 std::string
* local_id
= new std::string
;
83 base::FilePath
* changed_path
= new base::FilePath
;
84 ResourceEntry
* entry
= new ResourceEntry
;
85 base::PostTaskAndReplyWithResult(
86 blocking_task_runner_
.get(),
88 base::Bind(&UpdateLocalState
,
96 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState
,
97 weak_ptr_factory_
.GetWeakPtr(),
99 base::Owned(local_id
),
101 base::Owned(changed_path
)));
104 void RemoveOperation::RemoveAfterUpdateLocalState(
105 const FileOperationCallback
& callback
,
106 const std::string
* local_id
,
107 const ResourceEntry
* entry
,
108 const base::FilePath
* changed_path
,
110 DCHECK(thread_checker_
.CalledOnValidThread());
111 DCHECK(!callback
.is_null());
113 if (!changed_path
->empty()) {
114 FileChange changed_file
;
115 changed_file
.Update(*changed_path
, *entry
, FileChange::CHANGE_TYPE_DELETE
);
116 if (error
== FILE_ERROR_OK
) {
117 delegate_
->OnFileChangedByOperation(changed_file
);
118 delegate_
->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED
),
126 } // namespace file_system