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/files/file_util.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/task_runner_util.h" | 12 #include "base/task_scheduler/post_task.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
14 #include "base/threading/worker_pool.h" | |
15 | 14 |
16 namespace chromeos { | 15 namespace chromeos { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 // Performs fake mounting by creating a directory with a dummy file. | 19 // Performs fake mounting by creating a directory with a dummy file. |
21 MountError PerformFakeMount(const std::string& source_path, | 20 MountError PerformFakeMount(const std::string& source_path, |
22 const base::FilePath& mounted_path) { | 21 const base::FilePath& mounted_path) { |
23 // Just create an empty directory and shows it as the mounted directory. | 22 // Just create an empty directory and shows it as the mounted directory. |
24 if (!base::CreateDirectory(mounted_path)) { | 23 if (!base::CreateDirectory(mounted_path)) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 case MOUNT_TYPE_DEVICE: | 96 case MOUNT_TYPE_DEVICE: |
98 mounted_path = GetRemovableDiskMountPoint().Append( | 97 mounted_path = GetRemovableDiskMountPoint().Append( |
99 base::FilePath::FromUTF8Unsafe(mount_label)); | 98 base::FilePath::FromUTF8Unsafe(mount_label)); |
100 break; | 99 break; |
101 case MOUNT_TYPE_INVALID: | 100 case MOUNT_TYPE_INVALID: |
102 // Unreachable | 101 // Unreachable |
103 return; | 102 return; |
104 } | 103 } |
105 mounted_paths_.insert(mounted_path); | 104 mounted_paths_.insert(mounted_path); |
106 | 105 |
107 base::PostTaskAndReplyWithResult( | 106 base::PostTaskWithTraitsAndReplyWithResult( |
108 base::WorkerPool::GetTaskRunner(true /* task_is_slow */).get(), | 107 FROM_HERE, base::TaskTraits() |
109 FROM_HERE, | 108 .WithShutdownBehavior( |
| 109 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 110 .MayBlock(), |
110 base::Bind(&PerformFakeMount, source_path, mounted_path), | 111 base::Bind(&PerformFakeMount, source_path, mounted_path), |
111 base::Bind(&DidMount, | 112 base::Bind(&DidMount, mount_completed_handler_, source_path, type, |
112 mount_completed_handler_, | 113 callback, mounted_path)); |
113 source_path, | |
114 type, | |
115 callback, | |
116 mounted_path)); | |
117 } | 114 } |
118 | 115 |
119 void FakeCrosDisksClient::Unmount(const std::string& device_path, | 116 void FakeCrosDisksClient::Unmount(const std::string& device_path, |
120 UnmountOptions options, | 117 UnmountOptions options, |
121 const base::Closure& callback, | 118 const base::Closure& callback, |
122 const base::Closure& error_callback) { | 119 const base::Closure& error_callback) { |
123 DCHECK(!callback.is_null()); | 120 DCHECK(!callback.is_null()); |
124 DCHECK(!error_callback.is_null()); | 121 DCHECK(!error_callback.is_null()); |
125 | 122 |
126 // Remove the dummy mounted directory if it exists. | 123 // Remove the dummy mounted directory if it exists. |
127 if (mounted_paths_.count(base::FilePath::FromUTF8Unsafe(device_path)) > 0) { | 124 if (mounted_paths_.count(base::FilePath::FromUTF8Unsafe(device_path)) > 0) { |
128 mounted_paths_.erase(base::FilePath::FromUTF8Unsafe(device_path)); | 125 mounted_paths_.erase(base::FilePath::FromUTF8Unsafe(device_path)); |
129 base::WorkerPool::PostTaskAndReply( | 126 base::PostTaskWithTraitsAndReply( |
130 FROM_HERE, | 127 FROM_HERE, base::TaskTraits() |
| 128 .WithShutdownBehavior( |
| 129 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 130 .WithPriority(base::TaskPriority::BACKGROUND) |
| 131 .MayBlock(), |
131 base::Bind(base::IgnoreResult(&base::DeleteFile), | 132 base::Bind(base::IgnoreResult(&base::DeleteFile), |
132 base::FilePath::FromUTF8Unsafe(device_path), | 133 base::FilePath::FromUTF8Unsafe(device_path), |
133 true /* recursive */), | 134 true /* recursive */), |
134 callback, | 135 callback); |
135 true /* task_is_slow */); | |
136 } | 136 } |
137 | 137 |
138 unmount_call_count_++; | 138 unmount_call_count_++; |
139 last_unmount_device_path_ = device_path; | 139 last_unmount_device_path_ = device_path; |
140 last_unmount_options_ = options; | 140 last_unmount_options_ = options; |
141 base::ThreadTaskRunnerHandle::Get()->PostTask( | 141 base::ThreadTaskRunnerHandle::Get()->PostTask( |
142 FROM_HERE, unmount_success_ ? callback : error_callback); | 142 FROM_HERE, unmount_success_ ? callback : error_callback); |
143 if (!unmount_listener_.is_null()) | 143 if (!unmount_listener_.is_null()) |
144 unmount_listener_.Run(); | 144 unmount_listener_.Run(); |
145 } | 145 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 bool FakeCrosDisksClient::SendFormatCompletedEvent( | 215 bool FakeCrosDisksClient::SendFormatCompletedEvent( |
216 FormatError error_code, | 216 FormatError error_code, |
217 const std::string& device_path) { | 217 const std::string& device_path) { |
218 if (format_completed_handler_.is_null()) | 218 if (format_completed_handler_.is_null()) |
219 return false; | 219 return false; |
220 format_completed_handler_.Run(error_code, device_path); | 220 format_completed_handler_.Run(error_code, device_path); |
221 return true; | 221 return true; |
222 } | 222 } |
223 | 223 |
224 } // namespace chromeos | 224 } // namespace chromeos |
OLD | NEW |