[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 64e19925 | 2012-04-06 01:54:36 | [diff] [blame] | 5 | #include "chromeos/dbus/cros_disks_client.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 6 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 10 | #include <map> |
| 11 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
thestig | b44bd35 | 2014-09-10 01:47:06 | [diff] [blame] | 14 | #include "base/files/file_util.h" |
[email protected] | 593cf3b | 2013-03-05 04:16:52 | [diff] [blame] | 15 | #include "base/location.h" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 17 | #include "base/stl_util.h" |
[email protected] | afa339d7 | 2013-06-11 06:32:51 | [diff] [blame] | 18 | #include "base/strings/stringprintf.h" |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 19 | #include "base/sys_info.h" |
[email protected] | 593cf3b | 2013-03-05 04:16:52 | [diff] [blame] | 20 | #include "base/task_runner_util.h" |
| 21 | #include "base/threading/worker_pool.h" |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 22 | #include "base/values.h" |
satorux | 8fd29380 | 2014-10-30 08:23:12 | [diff] [blame] | 23 | #include "chromeos/dbus/fake_cros_disks_client.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 24 | #include "dbus/bus.h" |
| 25 | #include "dbus/message.h" |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 26 | #include "dbus/object_path.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 27 | #include "dbus/object_proxy.h" |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 28 | #include "dbus/values_util.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 29 | #include "third_party/cros_system_api/dbus/service_constants.h" |
| 30 | |
| 31 | namespace chromeos { |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | const char* kDefaultMountOptions[] = { |
| 36 | "rw", |
| 37 | "nodev", |
| 38 | "noexec", |
| 39 | "nosuid", |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | const char* kDefaultUnmountOptions[] = { |
| 43 | "force", |
| 44 | }; |
| 45 | |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 46 | const char kLazyUnmountOption[] = "lazy"; |
| 47 | |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 48 | const char kMountLabelOption[] = "mountlabel"; |
| 49 | |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 50 | // Checks if retrieved media type is in boundaries of DeviceMediaType. |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 51 | bool IsValidMediaType(uint32_t type) { |
| 52 | return type < static_cast<uint32_t>(cros_disks::DEVICE_MEDIA_NUM_VALUES); |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 55 | // Translates enum used in cros-disks to enum used in Chrome. |
| 56 | // Note that we could just do static_cast, but this is less sensitive to |
| 57 | // changes in cros-disks. |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 58 | DeviceType DeviceMediaTypeToDeviceType(uint32_t media_type_uint32) { |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 59 | if (!IsValidMediaType(media_type_uint32)) |
| 60 | return DEVICE_TYPE_UNKNOWN; |
| 61 | |
| 62 | cros_disks::DeviceMediaType media_type = |
| 63 | cros_disks::DeviceMediaType(media_type_uint32); |
| 64 | |
| 65 | switch (media_type) { |
| 66 | case(cros_disks::DEVICE_MEDIA_UNKNOWN): |
| 67 | return DEVICE_TYPE_UNKNOWN; |
| 68 | case(cros_disks::DEVICE_MEDIA_USB): |
| 69 | return DEVICE_TYPE_USB; |
| 70 | case(cros_disks::DEVICE_MEDIA_SD): |
| 71 | return DEVICE_TYPE_SD; |
| 72 | case(cros_disks::DEVICE_MEDIA_OPTICAL_DISC): |
| 73 | return DEVICE_TYPE_OPTICAL_DISC; |
| 74 | case(cros_disks::DEVICE_MEDIA_MOBILE): |
| 75 | return DEVICE_TYPE_MOBILE; |
[email protected] | f4ae40ac | 2012-05-04 21:57:00 | [diff] [blame] | 76 | case(cros_disks::DEVICE_MEDIA_DVD): |
| 77 | return DEVICE_TYPE_DVD; |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 78 | default: |
| 79 | return DEVICE_TYPE_UNKNOWN; |
| 80 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 83 | bool ReadMountEntryFromDbus(dbus::MessageReader* reader, MountEntry* entry) { |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 84 | uint32_t error_code = 0; |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 85 | std::string source_path; |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 86 | uint32_t mount_type = 0; |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 87 | std::string mount_path; |
| 88 | if (!reader->PopUint32(&error_code) || |
| 89 | !reader->PopString(&source_path) || |
| 90 | !reader->PopUint32(&mount_type) || |
| 91 | !reader->PopString(&mount_path)) { |
| 92 | return false; |
| 93 | } |
| 94 | *entry = MountEntry(static_cast<MountError>(error_code), source_path, |
| 95 | static_cast<MountType>(mount_type), mount_path); |
| 96 | return true; |
| 97 | } |
| 98 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 99 | // The CrosDisksClient implementation. |
| 100 | class CrosDisksClientImpl : public CrosDisksClient { |
| 101 | public: |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 102 | CrosDisksClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {} |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 103 | |
| 104 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 105 | void Mount(const std::string& source_path, |
| 106 | const std::string& source_format, |
| 107 | const std::string& mount_label, |
| 108 | const base::Closure& callback, |
| 109 | const base::Closure& error_callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 110 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 111 | cros_disks::kMount); |
| 112 | dbus::MessageWriter writer(&method_call); |
| 113 | writer.AppendString(source_path); |
[email protected] | b9f22d1 | 2012-04-25 21:46:48 | [diff] [blame] | 114 | writer.AppendString(source_format); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 115 | std::vector<std::string> mount_options(kDefaultMountOptions, |
| 116 | kDefaultMountOptions + |
| 117 | arraysize(kDefaultMountOptions)); |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 118 | if (!mount_label.empty()) { |
| 119 | std::string mount_label_option = base::StringPrintf("%s=%s", |
| 120 | kMountLabelOption, |
| 121 | mount_label.c_str()); |
| 122 | mount_options.push_back(mount_label_option); |
| 123 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 124 | writer.AppendArrayOfStrings(mount_options); |
| 125 | proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 126 | base::Bind(&CrosDisksClientImpl::OnMount, |
| 127 | weak_ptr_factory_.GetWeakPtr(), |
| 128 | callback, |
| 129 | error_callback)); |
| 130 | } |
| 131 | |
| 132 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 133 | void Unmount(const std::string& device_path, |
| 134 | UnmountOptions options, |
| 135 | const base::Closure& callback, |
| 136 | const base::Closure& error_callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 137 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 138 | cros_disks::kUnmount); |
| 139 | dbus::MessageWriter writer(&method_call); |
| 140 | writer.AppendString(device_path); |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 141 | |
| 142 | std::vector<std::string> unmount_options( |
| 143 | kDefaultUnmountOptions, |
| 144 | kDefaultUnmountOptions + arraysize(kDefaultUnmountOptions)); |
| 145 | if (options == UNMOUNT_OPTIONS_LAZY) |
| 146 | unmount_options.push_back(kLazyUnmountOption); |
| 147 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 148 | writer.AppendArrayOfStrings(unmount_options); |
| 149 | proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 150 | base::Bind(&CrosDisksClientImpl::OnUnmount, |
| 151 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 152 | callback, |
| 153 | error_callback)); |
| 154 | } |
| 155 | |
| 156 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 157 | void EnumerateAutoMountableDevices( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 158 | const EnumerateAutoMountableDevicesCallback& callback, |
mostynb | 4f4cf14 | 2014-10-06 13:57:52 | [diff] [blame] | 159 | const base::Closure& error_callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 160 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 161 | cros_disks::kEnumerateAutoMountableDevices); |
| 162 | proxy_->CallMethod( |
| 163 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 164 | base::Bind(&CrosDisksClientImpl::OnEnumerateAutoMountableDevices, |
| 165 | weak_ptr_factory_.GetWeakPtr(), |
| 166 | callback, |
| 167 | error_callback)); |
| 168 | } |
| 169 | |
| 170 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 171 | void EnumerateMountEntries(const EnumerateMountEntriesCallback& callback, |
| 172 | const base::Closure& error_callback) override { |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 173 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 174 | cros_disks::kEnumerateMountEntries); |
| 175 | proxy_->CallMethod( |
| 176 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 177 | base::Bind(&CrosDisksClientImpl::OnEnumerateMountEntries, |
| 178 | weak_ptr_factory_.GetWeakPtr(), |
| 179 | callback, |
| 180 | error_callback)); |
| 181 | } |
| 182 | |
| 183 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 184 | void Format(const std::string& device_path, |
| 185 | const std::string& filesystem, |
| 186 | const base::Closure& callback, |
| 187 | const base::Closure& error_callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 188 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 189 | cros_disks::kFormat); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 190 | dbus::MessageWriter writer(&method_call); |
| 191 | writer.AppendString(device_path); |
| 192 | writer.AppendString(filesystem); |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 193 | // No format option is currently specified, but we can later use this |
| 194 | // argument to specify options for the format operation. |
| 195 | std::vector<std::string> format_options; |
| 196 | writer.AppendArrayOfStrings(format_options); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 197 | proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 198 | base::Bind(&CrosDisksClientImpl::OnFormat, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 199 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 200 | callback, |
| 201 | error_callback)); |
| 202 | } |
| 203 | |
| 204 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 205 | void GetDeviceProperties(const std::string& device_path, |
| 206 | const GetDevicePropertiesCallback& callback, |
| 207 | const base::Closure& error_callback) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 208 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 209 | cros_disks::kGetDeviceProperties); |
| 210 | dbus::MessageWriter writer(&method_call); |
| 211 | writer.AppendString(device_path); |
| 212 | proxy_->CallMethod(&method_call, |
| 213 | dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 214 | base::Bind(&CrosDisksClientImpl::OnGetDeviceProperties, |
| 215 | weak_ptr_factory_.GetWeakPtr(), |
| 216 | device_path, |
| 217 | callback, |
| 218 | error_callback)); |
| 219 | } |
| 220 | |
| 221 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 222 | void SetMountEventHandler( |
mostynb | 4f4cf14 | 2014-10-06 13:57:52 | [diff] [blame] | 223 | const MountEventHandler& mount_event_handler) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 224 | static const SignalEventTuple kSignalEventTuples[] = { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 225 | { cros_disks::kDeviceAdded, CROS_DISKS_DEVICE_ADDED }, |
| 226 | { cros_disks::kDeviceScanned, CROS_DISKS_DEVICE_SCANNED }, |
| 227 | { cros_disks::kDeviceRemoved, CROS_DISKS_DEVICE_REMOVED }, |
| 228 | { cros_disks::kDiskAdded, CROS_DISKS_DISK_ADDED }, |
| 229 | { cros_disks::kDiskChanged, CROS_DISKS_DISK_CHANGED }, |
| 230 | { cros_disks::kDiskRemoved, CROS_DISKS_DISK_REMOVED }, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 231 | }; |
| 232 | const size_t kNumSignalEventTuples = arraysize(kSignalEventTuples); |
| 233 | |
| 234 | for (size_t i = 0; i < kNumSignalEventTuples; ++i) { |
| 235 | proxy_->ConnectToSignal( |
| 236 | cros_disks::kCrosDisksInterface, |
| 237 | kSignalEventTuples[i].signal_name, |
| 238 | base::Bind(&CrosDisksClientImpl::OnMountEvent, |
| 239 | weak_ptr_factory_.GetWeakPtr(), |
| 240 | kSignalEventTuples[i].event_type, |
| 241 | mount_event_handler), |
| 242 | base::Bind(&CrosDisksClientImpl::OnSignalConnected, |
| 243 | weak_ptr_factory_.GetWeakPtr())); |
| 244 | } |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 248 | void SetMountCompletedHandler( |
mostynb | 4f4cf14 | 2014-10-06 13:57:52 | [diff] [blame] | 249 | const MountCompletedHandler& mount_completed_handler) override { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 250 | proxy_->ConnectToSignal( |
| 251 | cros_disks::kCrosDisksInterface, |
[email protected] | b3e3f49 | 2011-11-18 18:46:00 | [diff] [blame] | 252 | cros_disks::kMountCompleted, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 253 | base::Bind(&CrosDisksClientImpl::OnMountCompleted, |
| 254 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 255 | mount_completed_handler), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 256 | base::Bind(&CrosDisksClientImpl::OnSignalConnected, |
| 257 | weak_ptr_factory_.GetWeakPtr())); |
| 258 | } |
| 259 | |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 260 | // CrosDisksClient override. |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 261 | void SetFormatCompletedHandler( |
mostynb | 4f4cf14 | 2014-10-06 13:57:52 | [diff] [blame] | 262 | const FormatCompletedHandler& format_completed_handler) override { |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 263 | proxy_->ConnectToSignal( |
| 264 | cros_disks::kCrosDisksInterface, |
| 265 | cros_disks::kFormatCompleted, |
| 266 | base::Bind(&CrosDisksClientImpl::OnFormatCompleted, |
| 267 | weak_ptr_factory_.GetWeakPtr(), |
| 268 | format_completed_handler), |
| 269 | base::Bind(&CrosDisksClientImpl::OnSignalConnected, |
| 270 | weak_ptr_factory_.GetWeakPtr())); |
| 271 | } |
| 272 | |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 273 | protected: |
dcheng | 0280cb6 | 2015-01-16 07:37:50 | [diff] [blame] | 274 | void Init(dbus::Bus* bus) override { |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 275 | proxy_ = bus->GetObjectProxy( |
| 276 | cros_disks::kCrosDisksServiceName, |
| 277 | dbus::ObjectPath(cros_disks::kCrosDisksServicePath)); |
| 278 | } |
| 279 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 280 | private: |
| 281 | // A struct to contain a pair of signal name and mount event type. |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 282 | // Used by SetMountEventHandler. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 283 | struct SignalEventTuple { |
| 284 | const char *signal_name; |
| 285 | MountEventType event_type; |
| 286 | }; |
| 287 | |
| 288 | // Handles the result of Mount and calls |callback| or |error_callback|. |
[email protected] | 5624a25 | 2013-07-04 03:17:53 | [diff] [blame] | 289 | void OnMount(const base::Closure& callback, |
| 290 | const base::Closure& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 291 | dbus::Response* response) { |
| 292 | if (!response) { |
| 293 | error_callback.Run(); |
| 294 | return; |
| 295 | } |
| 296 | callback.Run(); |
| 297 | } |
| 298 | |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 299 | // Handles the result of Unmount and calls |callback| or |error_callback|. |
[email protected] | ffdcc7a9c | 2013-07-02 06:59:39 | [diff] [blame] | 300 | void OnUnmount(const base::Closure& callback, |
| 301 | const base::Closure& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 302 | dbus::Response* response) { |
| 303 | if (!response) { |
[email protected] | ffdcc7a9c | 2013-07-02 06:59:39 | [diff] [blame] | 304 | error_callback.Run(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 305 | return; |
| 306 | } |
[email protected] | ddcb18e | 2013-09-19 07:17:28 | [diff] [blame] | 307 | |
| 308 | // Temporarly allow Unmount method to report failure both by setting dbus |
| 309 | // error (in which case response is not set) and by returning mount error |
| 310 | // different from MOUNT_ERROR_NONE. This is done so we can change Unmount |
| 311 | // method to return mount error (http://crbug.com/288974) without breaking |
| 312 | // Chrome. |
| 313 | // TODO(tbarzic): When Unmount implementation is changed on cros disks side, |
| 314 | // make this fail if reader is not able to read the error code value from |
| 315 | // the response. |
| 316 | dbus::MessageReader reader(response); |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 317 | uint32_t error_code = 0; |
[email protected] | ddcb18e | 2013-09-19 07:17:28 | [diff] [blame] | 318 | if (reader.PopUint32(&error_code) && |
| 319 | static_cast<MountError>(error_code) != MOUNT_ERROR_NONE) { |
| 320 | error_callback.Run(); |
| 321 | return; |
| 322 | } |
| 323 | |
[email protected] | ffdcc7a9c | 2013-07-02 06:59:39 | [diff] [blame] | 324 | callback.Run(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // Handles the result of EnumerateAutoMountableDevices and calls |callback| or |
| 328 | // |error_callback|. |
| 329 | void OnEnumerateAutoMountableDevices( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 330 | const EnumerateAutoMountableDevicesCallback& callback, |
[email protected] | 5624a25 | 2013-07-04 03:17:53 | [diff] [blame] | 331 | const base::Closure& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 332 | dbus::Response* response) { |
| 333 | if (!response) { |
| 334 | error_callback.Run(); |
| 335 | return; |
| 336 | } |
| 337 | dbus::MessageReader reader(response); |
| 338 | std::vector<std::string> device_paths; |
| 339 | if (!reader.PopArrayOfStrings(&device_paths)) { |
| 340 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 341 | error_callback.Run(); |
| 342 | return; |
| 343 | } |
| 344 | callback.Run(device_paths); |
| 345 | } |
| 346 | |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 347 | // Handles the result of EnumerateMountEntries and calls |callback| or |
| 348 | // |error_callback|. |
| 349 | void OnEnumerateMountEntries( |
| 350 | const EnumerateMountEntriesCallback& callback, |
| 351 | const base::Closure& error_callback, |
| 352 | dbus::Response* response) { |
| 353 | if (!response) { |
| 354 | error_callback.Run(); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | dbus::MessageReader reader(response); |
| 359 | dbus::MessageReader array_reader(NULL); |
| 360 | if (!reader.PopArray(&array_reader)) { |
| 361 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 362 | error_callback.Run(); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | std::vector<MountEntry> entries; |
| 367 | while (array_reader.HasMoreData()) { |
| 368 | MountEntry entry; |
| 369 | dbus::MessageReader sub_reader(NULL); |
| 370 | if (!array_reader.PopStruct(&sub_reader) || |
| 371 | !ReadMountEntryFromDbus(&sub_reader, &entry)) { |
| 372 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 373 | error_callback.Run(); |
| 374 | return; |
| 375 | } |
| 376 | entries.push_back(entry); |
| 377 | } |
| 378 | callback.Run(entries); |
| 379 | } |
| 380 | |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 381 | // Handles the result of Format and calls |callback| or |error_callback|. |
| 382 | void OnFormat(const base::Closure& callback, |
| 383 | const base::Closure& error_callback, |
| 384 | dbus::Response* response) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 385 | if (!response) { |
| 386 | error_callback.Run(); |
| 387 | return; |
| 388 | } |
[email protected] | f026c0f | 2014-05-06 21:52:35 | [diff] [blame] | 389 | callback.Run(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Handles the result of GetDeviceProperties and calls |callback| or |
| 393 | // |error_callback|. |
| 394 | void OnGetDeviceProperties(const std::string& device_path, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 395 | const GetDevicePropertiesCallback& callback, |
[email protected] | 5624a25 | 2013-07-04 03:17:53 | [diff] [blame] | 396 | const base::Closure& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 397 | dbus::Response* response) { |
| 398 | if (!response) { |
| 399 | error_callback.Run(); |
| 400 | return; |
| 401 | } |
| 402 | DiskInfo disk(device_path, response); |
| 403 | callback.Run(disk); |
| 404 | } |
| 405 | |
| 406 | // Handles mount event signals and calls |handler|. |
| 407 | void OnMountEvent(MountEventType event_type, |
| 408 | MountEventHandler handler, |
| 409 | dbus::Signal* signal) { |
| 410 | dbus::MessageReader reader(signal); |
| 411 | std::string device; |
| 412 | if (!reader.PopString(&device)) { |
| 413 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 414 | return; |
| 415 | } |
| 416 | handler.Run(event_type, device); |
| 417 | } |
| 418 | |
| 419 | // Handles MountCompleted signal and calls |handler|. |
| 420 | void OnMountCompleted(MountCompletedHandler handler, dbus::Signal* signal) { |
| 421 | dbus::MessageReader reader(signal); |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 422 | MountEntry entry; |
| 423 | if (!ReadMountEntryFromDbus(&reader, &entry)) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 424 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 425 | return; |
| 426 | } |
[email protected] | d776059 | 2014-05-16 07:57:52 | [diff] [blame] | 427 | handler.Run(entry); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 428 | } |
| 429 | |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 430 | // Handles FormatCompleted signal and calls |handler|. |
| 431 | void OnFormatCompleted(FormatCompletedHandler handler, dbus::Signal* signal) { |
| 432 | dbus::MessageReader reader(signal); |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 433 | uint32_t error_code = 0; |
[email protected] | a0278d5 | 2014-05-06 03:36:15 | [diff] [blame] | 434 | std::string device_path; |
| 435 | if (!reader.PopUint32(&error_code) || !reader.PopString(&device_path)) { |
| 436 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 437 | return; |
| 438 | } |
| 439 | handler.Run(static_cast<FormatError>(error_code), device_path); |
| 440 | } |
| 441 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 442 | // Handles the result of signal connection setup. |
| 443 | void OnSignalConnected(const std::string& interface, |
| 444 | const std::string& signal, |
[email protected] | d6311dcb | 2012-10-22 03:40:43 | [diff] [blame] | 445 | bool succeeded) { |
| 446 | LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " << |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 447 | signal << " failed."; |
| 448 | } |
| 449 | |
| 450 | dbus::ObjectProxy* proxy_; |
[email protected] | 926957b | 2012-09-07 05:34:16 | [diff] [blame] | 451 | |
| 452 | // Note: This should remain the last member so it'll be destroyed and |
| 453 | // invalidate its weak pointers before any other members are destroyed. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 454 | base::WeakPtrFactory<CrosDisksClientImpl> weak_ptr_factory_; |
| 455 | |
| 456 | DISALLOW_COPY_AND_ASSIGN(CrosDisksClientImpl); |
| 457 | }; |
| 458 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 459 | } // namespace |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 460 | |
| 461 | //////////////////////////////////////////////////////////////////////////////// |
| 462 | // DiskInfo |
| 463 | |
| 464 | DiskInfo::DiskInfo(const std::string& device_path, dbus::Response* response) |
| 465 | : device_path_(device_path), |
| 466 | is_drive_(false), |
| 467 | has_media_(false), |
| 468 | on_boot_device_(false), |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 469 | on_removable_device_(false), |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 470 | device_type_(DEVICE_TYPE_UNKNOWN), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 471 | total_size_in_bytes_(0), |
| 472 | is_read_only_(false), |
| 473 | is_hidden_(true) { |
| 474 | InitializeFromResponse(response); |
| 475 | } |
| 476 | |
| 477 | DiskInfo::~DiskInfo() { |
| 478 | } |
| 479 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 480 | // Initializes |this| from |response| given by the cros-disks service. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 481 | // Below is an example of |response|'s raw message (long string is ellipsized). |
| 482 | // |
| 483 | // |
| 484 | // message_type: MESSAGE_METHOD_RETURN |
| 485 | // destination: :1.8 |
| 486 | // sender: :1.16 |
| 487 | // signature: a{sv} |
| 488 | // serial: 96 |
| 489 | // reply_serial: 267 |
| 490 | // |
| 491 | // array [ |
| 492 | // dict entry { |
| 493 | // string "DeviceFile" |
| 494 | // variant string "/dev/sdb" |
| 495 | // } |
| 496 | // dict entry { |
| 497 | // string "DeviceIsDrive" |
| 498 | // variant bool true |
| 499 | // } |
| 500 | // dict entry { |
| 501 | // string "DeviceIsMediaAvailable" |
| 502 | // variant bool true |
| 503 | // } |
| 504 | // dict entry { |
| 505 | // string "DeviceIsMounted" |
| 506 | // variant bool false |
| 507 | // } |
| 508 | // dict entry { |
| 509 | // string "DeviceIsOnBootDevice" |
| 510 | // variant bool false |
| 511 | // } |
| 512 | // dict entry { |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 513 | // string "DeviceIsOnRemovableDevice" |
| 514 | // variant bool true |
| 515 | // } |
| 516 | // dict entry { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 517 | // string "DeviceIsReadOnly" |
| 518 | // variant bool false |
| 519 | // } |
| 520 | // dict entry { |
| 521 | // string "DeviceIsVirtual" |
| 522 | // variant bool false |
| 523 | // } |
| 524 | // dict entry { |
| 525 | // string "DeviceMediaType" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 526 | // variant uint32_t 1 |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 527 | // } |
| 528 | // dict entry { |
| 529 | // string "DeviceMountPaths" |
| 530 | // variant array [ |
| 531 | // ] |
| 532 | // } |
| 533 | // dict entry { |
| 534 | // string "DevicePresentationHide" |
| 535 | // variant bool true |
| 536 | // } |
| 537 | // dict entry { |
| 538 | // string "DeviceSize" |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 539 | // variant uint64_t 7998537728 |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 540 | // } |
| 541 | // dict entry { |
| 542 | // string "DriveIsRotational" |
| 543 | // variant bool false |
| 544 | // } |
| 545 | // dict entry { |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 546 | // string "VendorId" |
| 547 | // variant string "18d1" |
| 548 | // } |
| 549 | // dict entry { |
| 550 | // string "VendorName" |
| 551 | // variant string "Google Inc." |
| 552 | // } |
| 553 | // dict entry { |
| 554 | // string "ProductId" |
| 555 | // variant string "4e11" |
| 556 | // } |
| 557 | // dict entry { |
| 558 | // string "ProductName" |
| 559 | // variant string "Nexus One" |
| 560 | // } |
| 561 | // dict entry { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 562 | // string "DriveModel" |
| 563 | // variant string "TransMemory" |
| 564 | // } |
| 565 | // dict entry { |
| 566 | // string "IdLabel" |
| 567 | // variant string "" |
| 568 | // } |
| 569 | // dict entry { |
| 570 | // string "IdUuid" |
| 571 | // variant string "" |
| 572 | // } |
| 573 | // dict entry { |
| 574 | // string "NativePath" |
| 575 | // variant string "/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-4/... |
| 576 | // } |
| 577 | // ] |
| 578 | void DiskInfo::InitializeFromResponse(dbus::Response* response) { |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 579 | dbus::MessageReader reader(response); |
dcheng | 0a6e80c | 2016-04-08 18:37:38 | [diff] [blame^] | 580 | std::unique_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 581 | base::DictionaryValue* properties = NULL; |
| 582 | if (!value || !value->GetAsDictionary(&properties)) |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 583 | return; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 584 | |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 585 | properties->GetBooleanWithoutPathExpansion( |
| 586 | cros_disks::kDeviceIsDrive, &is_drive_); |
| 587 | properties->GetBooleanWithoutPathExpansion( |
| 588 | cros_disks::kDeviceIsReadOnly, &is_read_only_); |
| 589 | properties->GetBooleanWithoutPathExpansion( |
| 590 | cros_disks::kDevicePresentationHide, &is_hidden_); |
| 591 | properties->GetBooleanWithoutPathExpansion( |
| 592 | cros_disks::kDeviceIsMediaAvailable, &has_media_); |
| 593 | properties->GetBooleanWithoutPathExpansion( |
| 594 | cros_disks::kDeviceIsOnBootDevice, &on_boot_device_); |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 595 | properties->GetBooleanWithoutPathExpansion( |
| 596 | cros_disks::kDeviceIsOnRemovableDevice, &on_removable_device_); |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 597 | properties->GetStringWithoutPathExpansion( |
| 598 | cros_disks::kNativePath, &system_path_); |
| 599 | properties->GetStringWithoutPathExpansion( |
| 600 | cros_disks::kDeviceFile, &file_path_); |
| 601 | properties->GetStringWithoutPathExpansion(cros_disks::kVendorId, &vendor_id_); |
| 602 | properties->GetStringWithoutPathExpansion( |
| 603 | cros_disks::kVendorName, &vendor_name_); |
| 604 | properties->GetStringWithoutPathExpansion( |
| 605 | cros_disks::kProductId, &product_id_); |
| 606 | properties->GetStringWithoutPathExpansion( |
| 607 | cros_disks::kProductName, &product_name_); |
| 608 | properties->GetStringWithoutPathExpansion( |
| 609 | cros_disks::kDriveModel, &drive_model_); |
| 610 | properties->GetStringWithoutPathExpansion(cros_disks::kIdLabel, &label_); |
| 611 | properties->GetStringWithoutPathExpansion(cros_disks::kIdUuid, &uuid_); |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 612 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 613 | // dbus::PopDataAsValue() pops uint64_t as double. |
| 614 | // The top 11 bits of uint64_t are dropped by the use of double. But, this |
| 615 | // works |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 616 | // unless the size exceeds 8 PB. |
| 617 | double device_size_double = 0; |
| 618 | if (properties->GetDoubleWithoutPathExpansion(cros_disks::kDeviceSize, |
| 619 | &device_size_double)) |
| 620 | total_size_in_bytes_ = device_size_double; |
| 621 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 622 | // dbus::PopDataAsValue() pops uint32_t as double. |
[email protected] | 81836aed | 2013-07-09 23:41:12 | [diff] [blame] | 623 | double media_type_double = 0; |
| 624 | if (properties->GetDoubleWithoutPathExpansion(cros_disks::kDeviceMediaType, |
| 625 | &media_type_double)) |
| 626 | device_type_ = DeviceMediaTypeToDeviceType(media_type_double); |
| 627 | |
| 628 | base::ListValue* mount_paths = NULL; |
| 629 | if (properties->GetListWithoutPathExpansion(cros_disks::kDeviceMountPaths, |
| 630 | &mount_paths)) |
| 631 | mount_paths->GetString(0, &mount_path_); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | //////////////////////////////////////////////////////////////////////////////// |
| 635 | // CrosDisksClient |
| 636 | |
| 637 | CrosDisksClient::CrosDisksClient() {} |
| 638 | |
| 639 | CrosDisksClient::~CrosDisksClient() {} |
| 640 | |
| 641 | // static |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 642 | CrosDisksClient* CrosDisksClient::Create(DBusClientImplementationType type) { |
[email protected] | e8db03d6 | 2012-03-31 04:08:38 | [diff] [blame] | 643 | if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
[email protected] | c5fd536 | 2013-08-27 12:23:04 | [diff] [blame] | 644 | return new CrosDisksClientImpl(); |
[email protected] | e8db03d6 | 2012-03-31 04:08:38 | [diff] [blame] | 645 | DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
satorux | 8fd29380 | 2014-10-30 08:23:12 | [diff] [blame] | 646 | return new FakeCrosDisksClient(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 647 | } |
| 648 | |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame] | 649 | // static |
| 650 | base::FilePath CrosDisksClient::GetArchiveMountPoint() { |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 651 | return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame] | 652 | FILE_PATH_LITERAL("/media/archive") : |
| 653 | FILE_PATH_LITERAL("/tmp/chromeos/media/archive")); |
| 654 | } |
| 655 | |
| 656 | // static |
| 657 | base::FilePath CrosDisksClient::GetRemovableDiskMountPoint() { |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 658 | return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame] | 659 | FILE_PATH_LITERAL("/media/removable") : |
| 660 | FILE_PATH_LITERAL("/tmp/chromeos/media/removable")); |
| 661 | } |
| 662 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 663 | } // namespace chromeos |