Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system / remove_operation.cc
blobc3f7a1ed80b5fb43670091432f20da9ddfc686bf
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;
19 namespace drive {
20 namespace file_system {
22 namespace {
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,
28 bool is_recursive,
29 std::string* local_id,
30 ResourceEntry* entry,
31 base::FilePath* changed_path) {
32 FileError error = metadata->GetIdByPath(path, local_id);
33 if (error != FILE_ERROR_OK)
34 return error;
36 error = metadata->GetResourceEntryById(*local_id, entry);
37 if (error != FILE_ERROR_OK)
38 return error;
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)
45 return error;
46 if (!entries.empty())
47 return FILE_ERROR_NOT_EMPTY;
50 error = cache->Remove(*local_id);
51 if (error != FILE_ERROR_OK)
52 return error;
54 *changed_path = path;
56 // Move to the trash.
57 entry->set_parent_local_id(util::kDriveTrashDirLocalId);
58 return metadata->RefreshEntry(*entry);
61 } // namespace
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),
69 delegate_(delegate),
70 metadata_(metadata),
71 cache_(cache),
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,
81 bool is_recursive,
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(),
91 FROM_HERE,
92 base::Bind(&UpdateLocalState,
93 metadata_,
94 cache_,
95 path,
96 is_recursive,
97 local_id,
98 entry,
99 changed_path),
100 base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState,
101 weak_ptr_factory_.GetWeakPtr(),
102 callback,
103 base::Owned(local_id),
104 base::Owned(entry),
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,
113 FileError error) {
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),
123 *local_id);
127 callback.Run(error);
130 } // namespace file_system
131 } // namespace drive