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_system/operation_observer.h"
11 #include "chrome/browser/chromeos/drive/file_system_util.h"
12 #include "chrome/browser/chromeos/drive/resource_metadata.h"
13 #include "content/public/browser/browser_thread.h"
15 using content::BrowserThread
;
18 namespace file_system
{
22 // Removes cache file and moves the metadata entry to the trash.
23 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
24 internal::FileCache
* cache
,
25 const base::FilePath
& path
,
27 std::string
* local_id
,
28 base::FilePath
* changed_directory_path
) {
29 FileError error
= metadata
->GetIdByPath(path
, local_id
);
30 if (error
!= FILE_ERROR_OK
)
34 error
= metadata
->GetResourceEntryById(*local_id
, &entry
);
35 if (error
!= FILE_ERROR_OK
)
38 if (entry
.file_info().is_directory() && !is_recursive
) {
39 // Check emptiness of the directory.
40 ResourceEntryVector entries
;
41 error
= metadata
->ReadDirectoryByPath(path
, &entries
);
42 if (error
!= FILE_ERROR_OK
)
45 return FILE_ERROR_NOT_EMPTY
;
48 *changed_directory_path
= metadata
->GetFilePath(*local_id
).DirName();
51 entry
.set_parent_local_id(util::kDriveTrashDirLocalId
);
52 error
= metadata
->RefreshEntry(entry
);
53 if (error
!= FILE_ERROR_OK
)
56 return cache
->Remove(*local_id
);
61 RemoveOperation::RemoveOperation(
62 base::SequencedTaskRunner
* blocking_task_runner
,
63 OperationObserver
* observer
,
64 internal::ResourceMetadata
* metadata
,
65 internal::FileCache
* cache
)
66 : blocking_task_runner_(blocking_task_runner
),
70 weak_ptr_factory_(this) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
74 RemoveOperation::~RemoveOperation() {
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
78 void RemoveOperation::Remove(const base::FilePath
& path
,
80 const FileOperationCallback
& callback
) {
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
82 DCHECK(!callback
.is_null());
84 std::string
* local_id
= new std::string
;
85 base::FilePath
* changed_directory_path
= new base::FilePath
;
86 base::PostTaskAndReplyWithResult(
87 blocking_task_runner_
.get(),
89 base::Bind(&UpdateLocalState
,
95 changed_directory_path
),
96 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState
,
97 weak_ptr_factory_
.GetWeakPtr(),
99 base::Owned(local_id
),
100 base::Owned(changed_directory_path
)));
103 void RemoveOperation::RemoveAfterUpdateLocalState(
104 const FileOperationCallback
& callback
,
105 const std::string
* local_id
,
106 const base::FilePath
* changed_directory_path
,
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
109 DCHECK(!callback
.is_null());
111 if (error
== FILE_ERROR_OK
) {
112 observer_
->OnDirectoryChangedByOperation(*changed_directory_path
);
113 observer_
->OnEntryUpdatedByOperation(*local_id
);
119 } // namespace file_system