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"
8 #include "base/files/file_util.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/task_runner_util.h"
12 #include "base/threading/worker_pool.h"
18 // Performs fake mounting by creating a directory with a dummy file.
19 MountError
PerformFakeMount(const std::string
& source_path
,
20 const base::FilePath
& mounted_path
) {
21 // Just create an empty directory and shows it as the mounted directory.
22 if (!base::CreateDirectory(mounted_path
)) {
23 DLOG(ERROR
) << "Failed to create directory at " << mounted_path
.value();
24 return MOUNT_ERROR_DIRECTORY_CREATION_FAILED
;
28 const base::FilePath dummy_file_path
=
29 mounted_path
.Append("SUCCESSFULLY_PERFORMED_FAKE_MOUNT.txt");
30 const std::string dummy_file_content
= "This is a dummy file.";
31 const int write_result
= base::WriteFile(
32 dummy_file_path
, dummy_file_content
.data(), dummy_file_content
.size());
33 if (write_result
!= static_cast<int>(dummy_file_content
.size())) {
34 DLOG(ERROR
) << "Failed to put a dummy file at "
35 << dummy_file_path
.value();
36 return MOUNT_ERROR_MOUNT_PROGRAM_FAILED
;
39 return MOUNT_ERROR_NONE
;
42 // Continuation of Mount().
43 void DidMount(const CrosDisksClient::MountCompletedHandler
&
44 mount_completed_handler
,
45 const std::string
& source_path
,
47 const base::Closure
& callback
,
48 const base::FilePath
& mounted_path
,
49 MountError mount_error
) {
50 // Tell the caller of Mount() that the mount request was accepted.
51 base::MessageLoopProxy::current()->PostTask(FROM_HERE
, callback
);
53 // Tell the caller of Mount() that the mount completed.
54 if (!mount_completed_handler
.is_null()) {
55 base::MessageLoopProxy::current()->PostTask(
57 base::Bind(mount_completed_handler
,
58 MountEntry(mount_error
, source_path
, type
,
59 mounted_path
.AsUTF8Unsafe())));
65 FakeCrosDisksClient::FakeCrosDisksClient()
66 : unmount_call_count_(0),
67 unmount_success_(true),
68 format_call_count_(0),
69 format_success_(true) {
72 FakeCrosDisksClient::~FakeCrosDisksClient() {
75 void FakeCrosDisksClient::Init(dbus::Bus
* bus
) {
78 void FakeCrosDisksClient::Mount(const std::string
& source_path
,
79 const std::string
& source_format
,
80 const std::string
& mount_label
,
81 const base::Closure
& callback
,
82 const base::Closure
& error_callback
) {
83 // This fake implementation only accepts archive mount requests.
84 const MountType type
= MOUNT_TYPE_ARCHIVE
;
86 const base::FilePath mounted_path
= GetArchiveMountPoint().Append(
87 base::FilePath::FromUTF8Unsafe(mount_label
));
88 mounted_paths_
.insert(mounted_path
);
90 base::PostTaskAndReplyWithResult(
91 base::WorkerPool::GetTaskRunner(true /* task_is_slow */).get(),
93 base::Bind(&PerformFakeMount
, source_path
, mounted_path
),
95 mount_completed_handler_
,
102 void FakeCrosDisksClient::Unmount(const std::string
& device_path
,
103 UnmountOptions options
,
104 const base::Closure
& callback
,
105 const base::Closure
& error_callback
) {
106 DCHECK(!callback
.is_null());
107 DCHECK(!error_callback
.is_null());
109 // Remove the dummy mounted directory if it exists.
110 if (mounted_paths_
.count(base::FilePath::FromUTF8Unsafe(device_path
)) > 0) {
111 mounted_paths_
.erase(base::FilePath::FromUTF8Unsafe(device_path
));
112 base::WorkerPool::PostTaskAndReply(
114 base::Bind(base::IgnoreResult(&base::DeleteFile
),
115 base::FilePath::FromUTF8Unsafe(device_path
),
116 true /* recursive */),
118 true /* task_is_slow */);
121 unmount_call_count_
++;
122 last_unmount_device_path_
= device_path
;
123 last_unmount_options_
= options
;
124 base::MessageLoopProxy::current()->PostTask(
125 FROM_HERE
, unmount_success_
? callback
: error_callback
);
126 if (!unmount_listener_
.is_null())
127 unmount_listener_
.Run();
130 void FakeCrosDisksClient::EnumerateAutoMountableDevices(
131 const EnumerateAutoMountableDevicesCallback
& callback
,
132 const base::Closure
& error_callback
) {
135 void FakeCrosDisksClient::EnumerateMountEntries(
136 const EnumerateMountEntriesCallback
& callback
,
137 const base::Closure
& error_callback
) {
140 void FakeCrosDisksClient::Format(const std::string
& device_path
,
141 const std::string
& filesystem
,
142 const base::Closure
& callback
,
143 const base::Closure
& error_callback
) {
144 DCHECK(!callback
.is_null());
145 DCHECK(!error_callback
.is_null());
147 format_call_count_
++;
148 last_format_device_path_
= device_path
;
149 last_format_filesystem_
= filesystem
;
150 if (format_success_
) {
151 base::MessageLoopProxy::current()->PostTask(FROM_HERE
, callback
);
153 base::MessageLoopProxy::current()->PostTask(FROM_HERE
, error_callback
);
157 void FakeCrosDisksClient::GetDeviceProperties(
158 const std::string
& device_path
,
159 const GetDevicePropertiesCallback
& callback
,
160 const base::Closure
& error_callback
) {
163 void FakeCrosDisksClient::SetMountEventHandler(
164 const MountEventHandler
& mount_event_handler
) {
165 mount_event_handler_
= mount_event_handler
;
168 void FakeCrosDisksClient::SetMountCompletedHandler(
169 const MountCompletedHandler
& mount_completed_handler
) {
170 mount_completed_handler_
= mount_completed_handler
;
173 void FakeCrosDisksClient::SetFormatCompletedHandler(
174 const FormatCompletedHandler
& format_completed_handler
) {
175 format_completed_handler_
= format_completed_handler
;
178 bool FakeCrosDisksClient::SendMountEvent(MountEventType event
,
179 const std::string
& path
) {
180 if (mount_event_handler_
.is_null())
182 mount_event_handler_
.Run(event
, path
);
186 bool FakeCrosDisksClient::SendMountCompletedEvent(
187 MountError error_code
,
188 const std::string
& source_path
,
189 MountType mount_type
,
190 const std::string
& mount_path
) {
191 if (mount_completed_handler_
.is_null())
193 mount_completed_handler_
.Run(
194 MountEntry(error_code
, source_path
, mount_type
, mount_path
));
198 bool FakeCrosDisksClient::SendFormatCompletedEvent(
199 FormatError error_code
,
200 const std::string
& device_path
) {
201 if (format_completed_handler_
.is_null())
203 format_completed_handler_
.Run(error_code
, device_path
);
207 } // namespace chromeos