Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / dbus / fake_cros_disks_client.cc
blobdd33f802bb26545f6eae008792fd38f9edc0ff02
1 // Copyright (c) 2013 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 "chromeos/dbus/fake_cros_disks_client.h"
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/task_runner_util.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/worker_pool.h"
16 namespace chromeos {
18 namespace {
20 // Performs fake mounting by creating a directory with a dummy file.
21 MountError PerformFakeMount(const std::string& source_path,
22 const base::FilePath& mounted_path) {
23 // Just create an empty directory and shows it as the mounted directory.
24 if (!base::CreateDirectory(mounted_path)) {
25 DLOG(ERROR) << "Failed to create directory at " << mounted_path.value();
26 return MOUNT_ERROR_DIRECTORY_CREATION_FAILED;
29 // Put a dummy file.
30 const base::FilePath dummy_file_path =
31 mounted_path.Append("SUCCESSFULLY_PERFORMED_FAKE_MOUNT.txt");
32 const std::string dummy_file_content = "This is a dummy file.";
33 const int write_result = base::WriteFile(
34 dummy_file_path, dummy_file_content.data(), dummy_file_content.size());
35 if (write_result != static_cast<int>(dummy_file_content.size())) {
36 DLOG(ERROR) << "Failed to put a dummy file at "
37 << dummy_file_path.value();
38 return MOUNT_ERROR_MOUNT_PROGRAM_FAILED;
41 return MOUNT_ERROR_NONE;
44 // Continuation of Mount().
45 void DidMount(const CrosDisksClient::MountCompletedHandler&
46 mount_completed_handler,
47 const std::string& source_path,
48 MountType type,
49 const base::Closure& callback,
50 const base::FilePath& mounted_path,
51 MountError mount_error) {
52 // Tell the caller of Mount() that the mount request was accepted.
53 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
55 // Tell the caller of Mount() that the mount completed.
56 if (!mount_completed_handler.is_null()) {
57 base::ThreadTaskRunnerHandle::Get()->PostTask(
58 FROM_HERE, base::Bind(mount_completed_handler,
59 MountEntry(mount_error, source_path, type,
60 mounted_path.AsUTF8Unsafe())));
64 } // namespace
66 FakeCrosDisksClient::FakeCrosDisksClient()
67 : unmount_call_count_(0),
68 unmount_success_(true),
69 format_call_count_(0),
70 format_success_(true) {
73 FakeCrosDisksClient::~FakeCrosDisksClient() {
76 void FakeCrosDisksClient::Init(dbus::Bus* bus) {
79 void FakeCrosDisksClient::Mount(const std::string& source_path,
80 const std::string& source_format,
81 const std::string& mount_label,
82 const base::Closure& callback,
83 const base::Closure& error_callback) {
84 // This fake implementation only accepts archive mount requests.
85 const MountType type = MOUNT_TYPE_ARCHIVE;
87 const base::FilePath mounted_path = GetArchiveMountPoint().Append(
88 base::FilePath::FromUTF8Unsafe(mount_label));
89 mounted_paths_.insert(mounted_path);
91 base::PostTaskAndReplyWithResult(
92 base::WorkerPool::GetTaskRunner(true /* task_is_slow */).get(),
93 FROM_HERE,
94 base::Bind(&PerformFakeMount, source_path, mounted_path),
95 base::Bind(&DidMount,
96 mount_completed_handler_,
97 source_path,
98 type,
99 callback,
100 mounted_path));
103 void FakeCrosDisksClient::Unmount(const std::string& device_path,
104 UnmountOptions options,
105 const base::Closure& callback,
106 const base::Closure& error_callback) {
107 DCHECK(!callback.is_null());
108 DCHECK(!error_callback.is_null());
110 // Remove the dummy mounted directory if it exists.
111 if (mounted_paths_.count(base::FilePath::FromUTF8Unsafe(device_path)) > 0) {
112 mounted_paths_.erase(base::FilePath::FromUTF8Unsafe(device_path));
113 base::WorkerPool::PostTaskAndReply(
114 FROM_HERE,
115 base::Bind(base::IgnoreResult(&base::DeleteFile),
116 base::FilePath::FromUTF8Unsafe(device_path),
117 true /* recursive */),
118 callback,
119 true /* task_is_slow */);
122 unmount_call_count_++;
123 last_unmount_device_path_ = device_path;
124 last_unmount_options_ = options;
125 base::ThreadTaskRunnerHandle::Get()->PostTask(
126 FROM_HERE, unmount_success_ ? callback : error_callback);
127 if (!unmount_listener_.is_null())
128 unmount_listener_.Run();
131 void FakeCrosDisksClient::EnumerateAutoMountableDevices(
132 const EnumerateAutoMountableDevicesCallback& callback,
133 const base::Closure& error_callback) {
136 void FakeCrosDisksClient::EnumerateMountEntries(
137 const EnumerateMountEntriesCallback& callback,
138 const base::Closure& error_callback) {
141 void FakeCrosDisksClient::Format(const std::string& device_path,
142 const std::string& filesystem,
143 const base::Closure& callback,
144 const base::Closure& error_callback) {
145 DCHECK(!callback.is_null());
146 DCHECK(!error_callback.is_null());
148 format_call_count_++;
149 last_format_device_path_ = device_path;
150 last_format_filesystem_ = filesystem;
151 if (format_success_) {
152 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
153 } else {
154 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, error_callback);
158 void FakeCrosDisksClient::GetDeviceProperties(
159 const std::string& device_path,
160 const GetDevicePropertiesCallback& callback,
161 const base::Closure& error_callback) {
164 void FakeCrosDisksClient::SetMountEventHandler(
165 const MountEventHandler& mount_event_handler) {
166 mount_event_handler_ = mount_event_handler;
169 void FakeCrosDisksClient::SetMountCompletedHandler(
170 const MountCompletedHandler& mount_completed_handler) {
171 mount_completed_handler_ = mount_completed_handler;
174 void FakeCrosDisksClient::SetFormatCompletedHandler(
175 const FormatCompletedHandler& format_completed_handler) {
176 format_completed_handler_ = format_completed_handler;
179 bool FakeCrosDisksClient::SendMountEvent(MountEventType event,
180 const std::string& path) {
181 if (mount_event_handler_.is_null())
182 return false;
183 mount_event_handler_.Run(event, path);
184 return true;
187 bool FakeCrosDisksClient::SendMountCompletedEvent(
188 MountError error_code,
189 const std::string& source_path,
190 MountType mount_type,
191 const std::string& mount_path) {
192 if (mount_completed_handler_.is_null())
193 return false;
194 mount_completed_handler_.Run(
195 MountEntry(error_code, source_path, mount_type, mount_path));
196 return true;
199 bool FakeCrosDisksClient::SendFormatCompletedEvent(
200 FormatError error_code,
201 const std::string& device_path) {
202 if (format_completed_handler_.is_null())
203 return false;
204 format_completed_handler_.Run(error_code, device_path);
205 return true;
208 } // namespace chromeos