Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system / remove_operation.cc
blob348ca895ccb8b419866798f9fed4af606a5e06d5
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;
17 namespace drive {
18 namespace file_system {
20 namespace {
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,
26 bool is_recursive,
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)
31 return error;
33 ResourceEntry entry;
34 error = metadata->GetResourceEntryById(*local_id, &entry);
35 if (error != FILE_ERROR_OK)
36 return error;
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)
43 return error;
44 if (!entries.empty())
45 return FILE_ERROR_NOT_EMPTY;
48 *changed_directory_path = metadata->GetFilePath(*local_id).DirName();
50 // Move to the trash.
51 entry.set_parent_local_id(util::kDriveTrashDirLocalId);
52 error = metadata->RefreshEntry(entry);
53 if (error != FILE_ERROR_OK)
54 return error;
56 return cache->Remove(*local_id);
59 } // namespace
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),
67 observer_(observer),
68 metadata_(metadata),
69 cache_(cache),
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,
79 bool is_recursive,
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(),
88 FROM_HERE,
89 base::Bind(&UpdateLocalState,
90 metadata_,
91 cache_,
92 path,
93 is_recursive,
94 local_id,
95 changed_directory_path),
96 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState,
97 weak_ptr_factory_.GetWeakPtr(),
98 callback,
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,
107 FileError error) {
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);
116 callback.Run(error);
119 } // namespace file_system
120 } // namespace drive