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/drive.pb.h"
9 #include "chrome/browser/chromeos/drive/file_cache.h"
10 #include "chrome/browser/chromeos/drive/file_change.h"
11 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
12 #include "chrome/browser/chromeos/drive/file_system_util.h"
13 #include "chrome/browser/chromeos/drive/job_scheduler.h"
14 #include "chrome/browser/chromeos/drive/resource_metadata.h"
15 #include "content/public/browser/browser_thread.h"
17 using content::BrowserThread
;
20 namespace file_system
{
24 // Removes cache file and moves the metadata entry to the trash.
25 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
26 internal::FileCache
* cache
,
27 const base::FilePath
& path
,
29 std::string
* local_id
,
31 base::FilePath
* changed_path
) {
32 FileError error
= metadata
->GetIdByPath(path
, local_id
);
33 if (error
!= FILE_ERROR_OK
)
36 error
= metadata
->GetResourceEntryById(*local_id
, entry
);
37 if (error
!= FILE_ERROR_OK
)
40 if (entry
->file_info().is_directory() && !is_recursive
) {
41 // Check emptiness of the directory.
42 ResourceEntryVector entries
;
43 error
= metadata
->ReadDirectoryByPath(path
, &entries
);
44 if (error
!= FILE_ERROR_OK
)
47 return FILE_ERROR_NOT_EMPTY
;
50 error
= cache
->Remove(*local_id
);
51 if (error
!= FILE_ERROR_OK
)
57 entry
->set_parent_local_id(util::kDriveTrashDirLocalId
);
58 return metadata
->RefreshEntry(*entry
);
63 RemoveOperation::RemoveOperation(
64 base::SequencedTaskRunner
* blocking_task_runner
,
65 OperationDelegate
* delegate
,
66 internal::ResourceMetadata
* metadata
,
67 internal::FileCache
* cache
)
68 : blocking_task_runner_(blocking_task_runner
),
72 weak_ptr_factory_(this) {
73 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
76 RemoveOperation::~RemoveOperation() {
77 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
80 void RemoveOperation::Remove(const base::FilePath
& path
,
82 const FileOperationCallback
& callback
) {
83 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
84 DCHECK(!callback
.is_null());
86 std::string
* local_id
= new std::string
;
87 base::FilePath
* changed_path
= new base::FilePath
;
88 ResourceEntry
* entry
= new ResourceEntry
;
89 base::PostTaskAndReplyWithResult(
90 blocking_task_runner_
.get(),
92 base::Bind(&UpdateLocalState
,
100 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState
,
101 weak_ptr_factory_
.GetWeakPtr(),
103 base::Owned(local_id
),
105 base::Owned(changed_path
)));
108 void RemoveOperation::RemoveAfterUpdateLocalState(
109 const FileOperationCallback
& callback
,
110 const std::string
* local_id
,
111 const ResourceEntry
* entry
,
112 const base::FilePath
* changed_path
,
114 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
115 DCHECK(!callback
.is_null());
117 if (!changed_path
->empty()) {
118 FileChange changed_file
;
119 changed_file
.Update(*changed_path
, *entry
, FileChange::DELETE
);
120 if (error
== FILE_ERROR_OK
) {
121 delegate_
->OnFileChangedByOperation(changed_file
);
122 delegate_
->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED
),
130 } // namespace file_system