OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/fake_cros_disks_client.h" | 5 #include "chromeos/dbus/fake_cros_disks_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/task_runner_util.h" | |
12 #include "base/threading/worker_pool.h" | |
10 | 13 |
11 namespace chromeos { | 14 namespace chromeos { |
12 | 15 |
16 namespace { | |
17 | |
18 // Performs fake mounting by creating a directory with a dummy file. | |
19 static MountError PerformFakeMount(const std::string& source_path, | |
hashimoto
2014/10/29 06:43:53
nit: "static" unneeded.
satorux1
2014/10/29 06:47:02
Done.
| |
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; | |
25 } | |
26 | |
27 // Put a dummy file. | |
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; | |
37 } | |
38 | |
39 return MOUNT_ERROR_NONE; | |
40 } | |
41 | |
42 // Continuation of Mount(). | |
43 void DidMount(const CrosDisksClient::MountCompletedHandler& | |
44 mount_completed_handler, | |
45 const std::string& source_path, | |
46 MountType type, | |
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); | |
52 | |
53 // Tell the caller of Mount() that the mount completed. | |
54 if (!mount_completed_handler.is_null()) { | |
55 base::MessageLoopProxy::current()->PostTask( | |
56 FROM_HERE, | |
57 base::Bind(mount_completed_handler, | |
58 MountEntry(mount_error, source_path, type, | |
59 mounted_path.AsUTF8Unsafe()))); | |
60 } | |
61 } | |
62 | |
63 } // namespace | |
64 | |
13 FakeCrosDisksClient::FakeCrosDisksClient() | 65 FakeCrosDisksClient::FakeCrosDisksClient() |
14 : unmount_call_count_(0), | 66 : unmount_call_count_(0), |
15 unmount_success_(true), | 67 unmount_success_(true), |
16 format_call_count_(0), | 68 format_call_count_(0), |
17 format_success_(true) { | 69 format_success_(true) { |
18 } | 70 } |
19 | 71 |
20 FakeCrosDisksClient::~FakeCrosDisksClient() { | 72 FakeCrosDisksClient::~FakeCrosDisksClient() { |
21 } | 73 } |
22 | 74 |
23 void FakeCrosDisksClient::Init(dbus::Bus* bus) { | 75 void FakeCrosDisksClient::Init(dbus::Bus* bus) { |
24 } | 76 } |
25 | 77 |
26 void FakeCrosDisksClient::Mount(const std::string& source_path, | 78 void FakeCrosDisksClient::Mount(const std::string& source_path, |
27 const std::string& source_format, | 79 const std::string& source_format, |
28 const std::string& mount_label, | 80 const std::string& mount_label, |
29 const base::Closure& callback, | 81 const base::Closure& callback, |
30 const base::Closure& error_callback) { | 82 const base::Closure& error_callback) { |
83 // This fake implementation only accepts archive mount requests. | |
84 const MountType type = MOUNT_TYPE_ARCHIVE; | |
85 | |
86 const base::FilePath mounted_path = GetArchiveMountPoint().Append( | |
87 base::FilePath::FromUTF8Unsafe(mount_label)); | |
88 mounted_paths_.insert(mounted_path); | |
89 | |
90 base::PostTaskAndReplyWithResult( | |
91 base::WorkerPool::GetTaskRunner(true /* task_is_slow */).get(), | |
92 FROM_HERE, | |
93 base::Bind(&PerformFakeMount, source_path, mounted_path), | |
94 base::Bind(&DidMount, | |
95 mount_completed_handler_, | |
96 source_path, | |
97 type, | |
98 callback, | |
99 mounted_path)); | |
31 } | 100 } |
32 | 101 |
33 void FakeCrosDisksClient::Unmount(const std::string& device_path, | 102 void FakeCrosDisksClient::Unmount(const std::string& device_path, |
34 UnmountOptions options, | 103 UnmountOptions options, |
35 const base::Closure& callback, | 104 const base::Closure& callback, |
36 const base::Closure& error_callback) { | 105 const base::Closure& error_callback) { |
37 DCHECK(!callback.is_null()); | 106 DCHECK(!callback.is_null()); |
38 DCHECK(!error_callback.is_null()); | 107 DCHECK(!error_callback.is_null()); |
39 | 108 |
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( | |
113 FROM_HERE, | |
114 base::Bind(base::IgnoreResult(&base::DeleteFile), | |
115 base::FilePath::FromUTF8Unsafe(device_path), | |
116 true /* recursive */), | |
117 callback, | |
118 true /* task_is_slow */); | |
119 } | |
120 | |
40 unmount_call_count_++; | 121 unmount_call_count_++; |
41 last_unmount_device_path_ = device_path; | 122 last_unmount_device_path_ = device_path; |
42 last_unmount_options_ = options; | 123 last_unmount_options_ = options; |
43 base::MessageLoopProxy::current()->PostTask( | 124 base::MessageLoopProxy::current()->PostTask( |
44 FROM_HERE, unmount_success_ ? callback : error_callback); | 125 FROM_HERE, unmount_success_ ? callback : error_callback); |
45 if (!unmount_listener_.is_null()) | 126 if (!unmount_listener_.is_null()) |
46 unmount_listener_.Run(); | 127 unmount_listener_.Run(); |
47 } | 128 } |
48 | 129 |
49 void FakeCrosDisksClient::EnumerateAutoMountableDevices( | 130 void FakeCrosDisksClient::EnumerateAutoMountableDevices( |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 bool FakeCrosDisksClient::SendFormatCompletedEvent( | 198 bool FakeCrosDisksClient::SendFormatCompletedEvent( |
118 FormatError error_code, | 199 FormatError error_code, |
119 const std::string& device_path) { | 200 const std::string& device_path) { |
120 if (format_completed_handler_.is_null()) | 201 if (format_completed_handler_.is_null()) |
121 return false; | 202 return false; |
122 format_completed_handler_.Run(error_code, device_path); | 203 format_completed_handler_.Run(error_code, device_path); |
123 return true; | 204 return true; |
124 } | 205 } |
125 | 206 |
126 } // namespace chromeos | 207 } // namespace chromeos |
OLD | NEW |