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/resource_metadata.h"
14 #include "content/public/browser/browser_thread.h"
16 using content::BrowserThread;
19 namespace file_system {
23 // Removes cache file and moves the metadata entry to the trash.
24 FileError UpdateLocalState(internal::ResourceMetadata* metadata,
25 internal::FileCache* cache,
26 const base::FilePath& path,
28 std::string* local_id,
30 base::FilePath* changed_path) {
31 FileError error = metadata->GetIdByPath(path, local_id);
32 if (error != FILE_ERROR_OK)
35 error = metadata->GetResourceEntryById(*local_id, entry);
36 if (error != FILE_ERROR_OK)
39 if (entry->file_info().is_directory() && !is_recursive) {
40 // Check emptiness of the directory.
41 ResourceEntryVector entries;
42 error = metadata->ReadDirectoryByPath(path, &entries);
43 if (error != FILE_ERROR_OK)
46 return FILE_ERROR_NOT_EMPTY;
49 error = cache->Remove(*local_id);
50 if (error != FILE_ERROR_OK)
56 entry->set_parent_local_id(util::kDriveTrashDirLocalId);
57 return metadata->RefreshEntry(*entry);
62 RemoveOperation::RemoveOperation(
63 base::SequencedTaskRunner* blocking_task_runner,
64 OperationDelegate* delegate,
65 internal::ResourceMetadata* metadata,
66 internal::FileCache* cache)
67 : blocking_task_runner_(blocking_task_runner),
71 weak_ptr_factory_(this) {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75 RemoveOperation::~RemoveOperation() {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
79 void RemoveOperation::Remove(const base::FilePath& path,
81 const FileOperationCallback& callback) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 DCHECK(!callback.is_null());
85 std::string* local_id = new std::string;
86 base::FilePath* changed_path = new base::FilePath;
87 ResourceEntry* entry = new ResourceEntry;
88 base::PostTaskAndReplyWithResult(
89 blocking_task_runner_.get(),
91 base::Bind(&UpdateLocalState,
99 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState,
100 weak_ptr_factory_.GetWeakPtr(),
102 base::Owned(local_id),
104 base::Owned(changed_path)));
107 void RemoveOperation::RemoveAfterUpdateLocalState(
108 const FileOperationCallback& callback,
109 const std::string* local_id,
110 const ResourceEntry* entry,
111 const base::FilePath* changed_path,
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
114 DCHECK(!callback.is_null());
116 if (!changed_path->empty()) {
117 FileChange changed_file;
118 changed_file.Update(*changed_path, *entry, FileChange::DELETE);
119 if (error == FILE_ERROR_OK) {
120 delegate_->OnFileChangedByOperation(changed_file);
121 delegate_->OnEntryUpdatedByOperation(*local_id);
128 } // namespace file_system