[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 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 7 | #include <map> |
| 8 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 10 | #include "base/stl_util.h" |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 11 | #include "base/stringprintf.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 12 | #include "dbus/bus.h" |
| 13 | #include "dbus/message.h" |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 14 | #include "dbus/object_path.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 15 | #include "dbus/object_proxy.h" |
| 16 | #include "third_party/cros_system_api/dbus/service_constants.h" |
| 17 | |
| 18 | namespace chromeos { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | const char* kDefaultMountOptions[] = { |
| 23 | "rw", |
| 24 | "nodev", |
| 25 | "noexec", |
| 26 | "nosuid", |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | const char* kDefaultUnmountOptions[] = { |
| 30 | "force", |
| 31 | }; |
| 32 | |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 33 | const char kMountLabelOption[] = "mountlabel"; |
| 34 | |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 35 | // Checks if retrieved media type is in boundaries of DeviceMediaType. |
| 36 | bool IsValidMediaType(uint32 type) { |
| 37 | return type < static_cast<uint32>(cros_disks::DEVICE_MEDIA_NUM_VALUES); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | // Translates enum used in cros-disks to enum used in Chrome. |
| 42 | // Note that we could just do static_cast, but this is less sensitive to |
| 43 | // changes in cros-disks. |
| 44 | DeviceType DeviceMediaTypeToDeviceType(uint32 media_type_uint32) { |
| 45 | if (!IsValidMediaType(media_type_uint32)) |
| 46 | return DEVICE_TYPE_UNKNOWN; |
| 47 | |
| 48 | cros_disks::DeviceMediaType media_type = |
| 49 | cros_disks::DeviceMediaType(media_type_uint32); |
| 50 | |
| 51 | switch (media_type) { |
| 52 | case(cros_disks::DEVICE_MEDIA_UNKNOWN): |
| 53 | return DEVICE_TYPE_UNKNOWN; |
| 54 | case(cros_disks::DEVICE_MEDIA_USB): |
| 55 | return DEVICE_TYPE_USB; |
| 56 | case(cros_disks::DEVICE_MEDIA_SD): |
| 57 | return DEVICE_TYPE_SD; |
| 58 | case(cros_disks::DEVICE_MEDIA_OPTICAL_DISC): |
| 59 | return DEVICE_TYPE_OPTICAL_DISC; |
| 60 | case(cros_disks::DEVICE_MEDIA_MOBILE): |
| 61 | return DEVICE_TYPE_MOBILE; |
[email protected] | f4ae40ac | 2012-05-04 21:57:00 | [diff] [blame] | 62 | case(cros_disks::DEVICE_MEDIA_DVD): |
| 63 | return DEVICE_TYPE_DVD; |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 64 | default: |
| 65 | return DEVICE_TYPE_UNKNOWN; |
| 66 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Pops a bool value when |reader| is not NULL. |
| 70 | // Returns true when a value is popped, false otherwise. |
| 71 | bool MaybePopBool(dbus::MessageReader* reader, bool* value) { |
| 72 | if (!reader) |
| 73 | return false; |
| 74 | return reader->PopBool(value); |
| 75 | } |
| 76 | |
| 77 | // Pops a string value when |reader| is not NULL. |
| 78 | // Returns true when a value is popped, false otherwise. |
| 79 | bool MaybePopString(dbus::MessageReader* reader, std::string* value) { |
| 80 | if (!reader) |
| 81 | return false; |
| 82 | return reader->PopString(value); |
| 83 | } |
| 84 | |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 85 | // Pops a uint32 value when |reader| is not NULL. |
| 86 | // Returns true when a value is popped, false otherwise. |
| 87 | bool MaybePopUint32(dbus::MessageReader* reader, uint32* value) { |
| 88 | if (!reader) |
| 89 | return false; |
| 90 | |
| 91 | return reader->PopUint32(value); |
| 92 | } |
| 93 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 94 | // Pops a uint64 value when |reader| is not NULL. |
| 95 | // Returns true when a value is popped, false otherwise. |
| 96 | bool MaybePopUint64(dbus::MessageReader* reader, uint64* value) { |
| 97 | if (!reader) |
| 98 | return false; |
| 99 | return reader->PopUint64(value); |
| 100 | } |
| 101 | |
| 102 | // Pops an array of strings when |reader| is not NULL. |
| 103 | // Returns true when an array is popped, false otherwise. |
| 104 | bool MaybePopArrayOfStrings(dbus::MessageReader* reader, |
| 105 | std::vector<std::string>* value) { |
| 106 | if (!reader) |
| 107 | return false; |
| 108 | return reader->PopArrayOfStrings(value); |
| 109 | } |
| 110 | |
| 111 | // The CrosDisksClient implementation. |
| 112 | class CrosDisksClientImpl : public CrosDisksClient { |
| 113 | public: |
| 114 | explicit CrosDisksClientImpl(dbus::Bus* bus) |
[email protected] | 216ed0b | 2012-02-14 21:29:06 | [diff] [blame] | 115 | : proxy_(bus->GetObjectProxy( |
| 116 | cros_disks::kCrosDisksServiceName, |
| 117 | dbus::ObjectPath(cros_disks::kCrosDisksServicePath))), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 118 | weak_ptr_factory_(this) { |
| 119 | } |
| 120 | |
| 121 | // CrosDisksClient override. |
| 122 | virtual void Mount(const std::string& source_path, |
[email protected] | b9f22d1 | 2012-04-25 21:46:48 | [diff] [blame] | 123 | const std::string& source_format, |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 124 | const std::string& mount_label, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 125 | MountType type, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 126 | const MountCallback& callback, |
| 127 | const ErrorCallback& error_callback) OVERRIDE { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 128 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 129 | cros_disks::kMount); |
| 130 | dbus::MessageWriter writer(&method_call); |
| 131 | writer.AppendString(source_path); |
[email protected] | b9f22d1 | 2012-04-25 21:46:48 | [diff] [blame] | 132 | writer.AppendString(source_format); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 133 | std::vector<std::string> mount_options(kDefaultMountOptions, |
| 134 | kDefaultMountOptions + |
| 135 | arraysize(kDefaultMountOptions)); |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 136 | if (!mount_label.empty()) { |
| 137 | std::string mount_label_option = base::StringPrintf("%s=%s", |
| 138 | kMountLabelOption, |
| 139 | mount_label.c_str()); |
| 140 | mount_options.push_back(mount_label_option); |
| 141 | } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 142 | writer.AppendArrayOfStrings(mount_options); |
| 143 | proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 144 | base::Bind(&CrosDisksClientImpl::OnMount, |
| 145 | weak_ptr_factory_.GetWeakPtr(), |
| 146 | callback, |
| 147 | error_callback)); |
| 148 | } |
| 149 | |
| 150 | // CrosDisksClient override. |
| 151 | virtual void Unmount(const std::string& device_path, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 152 | const UnmountCallback& callback, |
| 153 | const ErrorCallback& error_callback) OVERRIDE { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 154 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 155 | cros_disks::kUnmount); |
| 156 | dbus::MessageWriter writer(&method_call); |
| 157 | writer.AppendString(device_path); |
| 158 | std::vector<std::string> unmount_options(kDefaultUnmountOptions, |
| 159 | kDefaultUnmountOptions + |
| 160 | arraysize(kDefaultUnmountOptions)); |
| 161 | writer.AppendArrayOfStrings(unmount_options); |
| 162 | proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 163 | base::Bind(&CrosDisksClientImpl::OnUnmount, |
| 164 | weak_ptr_factory_.GetWeakPtr(), |
| 165 | device_path, |
| 166 | callback, |
| 167 | error_callback)); |
| 168 | } |
| 169 | |
| 170 | // CrosDisksClient override. |
| 171 | virtual void EnumerateAutoMountableDevices( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 172 | const EnumerateAutoMountableDevicesCallback& callback, |
| 173 | const ErrorCallback& error_callback) OVERRIDE { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 174 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 175 | cros_disks::kEnumerateAutoMountableDevices); |
| 176 | proxy_->CallMethod( |
| 177 | &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 178 | base::Bind(&CrosDisksClientImpl::OnEnumerateAutoMountableDevices, |
| 179 | weak_ptr_factory_.GetWeakPtr(), |
| 180 | callback, |
| 181 | error_callback)); |
| 182 | } |
| 183 | |
| 184 | // CrosDisksClient override. |
| 185 | virtual void FormatDevice(const std::string& device_path, |
| 186 | const std::string& filesystem, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 187 | const FormatDeviceCallback& callback, |
| 188 | const ErrorCallback& error_callback) OVERRIDE { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 189 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 190 | cros_disks::kFormatDevice); |
| 191 | dbus::MessageWriter writer(&method_call); |
| 192 | writer.AppendString(device_path); |
| 193 | writer.AppendString(filesystem); |
| 194 | proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 195 | base::Bind(&CrosDisksClientImpl::OnFormatDevice, |
| 196 | weak_ptr_factory_.GetWeakPtr(), |
| 197 | device_path, |
| 198 | callback, |
| 199 | error_callback)); |
| 200 | } |
| 201 | |
| 202 | // CrosDisksClient override. |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 203 | virtual void GetDeviceProperties( |
| 204 | const std::string& device_path, |
| 205 | const GetDevicePropertiesCallback& callback, |
| 206 | const ErrorCallback& error_callback) OVERRIDE { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 207 | dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 208 | cros_disks::kGetDeviceProperties); |
| 209 | dbus::MessageWriter writer(&method_call); |
| 210 | writer.AppendString(device_path); |
| 211 | proxy_->CallMethod(&method_call, |
| 212 | dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 213 | base::Bind(&CrosDisksClientImpl::OnGetDeviceProperties, |
| 214 | weak_ptr_factory_.GetWeakPtr(), |
| 215 | device_path, |
| 216 | callback, |
| 217 | error_callback)); |
| 218 | } |
| 219 | |
| 220 | // CrosDisksClient override. |
| 221 | virtual void SetUpConnections( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 222 | const MountEventHandler& mount_event_handler, |
| 223 | const MountCompletedHandler& mount_completed_handler) OVERRIDE { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 224 | static const SignalEventTuple kSignalEventTuples[] = { |
[email protected] | b3e3f49 | 2011-11-18 18:46:00 | [diff] [blame] | 225 | { cros_disks::kDeviceAdded, DEVICE_ADDED }, |
| 226 | { cros_disks::kDeviceScanned, DEVICE_SCANNED }, |
| 227 | { cros_disks::kDeviceRemoved, DEVICE_REMOVED }, |
| 228 | { cros_disks::kDiskAdded, DISK_ADDED }, |
| 229 | { cros_disks::kDiskChanged, DISK_CHANGED }, |
| 230 | { cros_disks::kDiskRemoved, DISK_REMOVED }, |
| 231 | { cros_disks::kFormattingFinished, FORMATTING_FINISHED }, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 232 | }; |
| 233 | const size_t kNumSignalEventTuples = arraysize(kSignalEventTuples); |
| 234 | |
| 235 | for (size_t i = 0; i < kNumSignalEventTuples; ++i) { |
| 236 | proxy_->ConnectToSignal( |
| 237 | cros_disks::kCrosDisksInterface, |
| 238 | kSignalEventTuples[i].signal_name, |
| 239 | base::Bind(&CrosDisksClientImpl::OnMountEvent, |
| 240 | weak_ptr_factory_.GetWeakPtr(), |
| 241 | kSignalEventTuples[i].event_type, |
| 242 | mount_event_handler), |
| 243 | base::Bind(&CrosDisksClientImpl::OnSignalConnected, |
| 244 | weak_ptr_factory_.GetWeakPtr())); |
| 245 | } |
| 246 | proxy_->ConnectToSignal( |
| 247 | cros_disks::kCrosDisksInterface, |
[email protected] | b3e3f49 | 2011-11-18 18:46:00 | [diff] [blame] | 248 | cros_disks::kMountCompleted, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 249 | base::Bind(&CrosDisksClientImpl::OnMountCompleted, |
| 250 | weak_ptr_factory_.GetWeakPtr(), |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 251 | mount_completed_handler), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 252 | base::Bind(&CrosDisksClientImpl::OnSignalConnected, |
| 253 | weak_ptr_factory_.GetWeakPtr())); |
| 254 | } |
| 255 | |
| 256 | private: |
| 257 | // A struct to contain a pair of signal name and mount event type. |
| 258 | // Used by SetUpConnections. |
| 259 | struct SignalEventTuple { |
| 260 | const char *signal_name; |
| 261 | MountEventType event_type; |
| 262 | }; |
| 263 | |
| 264 | // Handles the result of Mount and calls |callback| or |error_callback|. |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 265 | void OnMount(const MountCallback& callback, |
| 266 | const ErrorCallback& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 267 | dbus::Response* response) { |
| 268 | if (!response) { |
| 269 | error_callback.Run(); |
| 270 | return; |
| 271 | } |
| 272 | callback.Run(); |
| 273 | } |
| 274 | |
| 275 | // Handles the result of Unount and calls |callback| or |error_callback|. |
| 276 | void OnUnmount(const std::string& device_path, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 277 | const UnmountCallback& callback, |
| 278 | const ErrorCallback& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 279 | dbus::Response* response) { |
| 280 | if (!response) { |
| 281 | error_callback.Run(); |
| 282 | return; |
| 283 | } |
| 284 | callback.Run(device_path); |
| 285 | } |
| 286 | |
| 287 | // Handles the result of EnumerateAutoMountableDevices and calls |callback| or |
| 288 | // |error_callback|. |
| 289 | void OnEnumerateAutoMountableDevices( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 290 | const EnumerateAutoMountableDevicesCallback& callback, |
| 291 | const ErrorCallback& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 292 | dbus::Response* response) { |
| 293 | if (!response) { |
| 294 | error_callback.Run(); |
| 295 | return; |
| 296 | } |
| 297 | dbus::MessageReader reader(response); |
| 298 | std::vector<std::string> device_paths; |
| 299 | if (!reader.PopArrayOfStrings(&device_paths)) { |
| 300 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 301 | error_callback.Run(); |
| 302 | return; |
| 303 | } |
| 304 | callback.Run(device_paths); |
| 305 | } |
| 306 | |
| 307 | // Handles the result of FormatDevice and calls |callback| or |
| 308 | // |error_callback|. |
| 309 | void OnFormatDevice(const std::string& device_path, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 310 | const FormatDeviceCallback& callback, |
| 311 | const ErrorCallback& error_callback, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 312 | dbus::Response* response) { |
| 313 | if (!response) { |
| 314 | error_callback.Run(); |
| 315 | return; |
| 316 | } |
| 317 | dbus::MessageReader reader(response); |
| 318 | bool success = false; |
| 319 | if (!reader.PopBool(&success)) { |
| 320 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 321 | error_callback.Run(); |
| 322 | return; |
| 323 | } |
| 324 | callback.Run(device_path, success); |
| 325 | } |
| 326 | |
| 327 | // Handles the result of GetDeviceProperties and calls |callback| or |
| 328 | // |error_callback|. |
| 329 | void OnGetDeviceProperties(const std::string& device_path, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 330 | const GetDevicePropertiesCallback& callback, |
| 331 | const ErrorCallback& 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 | DiskInfo disk(device_path, response); |
| 338 | callback.Run(disk); |
| 339 | } |
| 340 | |
| 341 | // Handles mount event signals and calls |handler|. |
| 342 | void OnMountEvent(MountEventType event_type, |
| 343 | MountEventHandler handler, |
| 344 | dbus::Signal* signal) { |
| 345 | dbus::MessageReader reader(signal); |
| 346 | std::string device; |
| 347 | if (!reader.PopString(&device)) { |
| 348 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 349 | return; |
| 350 | } |
| 351 | handler.Run(event_type, device); |
| 352 | } |
| 353 | |
| 354 | // Handles MountCompleted signal and calls |handler|. |
| 355 | void OnMountCompleted(MountCompletedHandler handler, dbus::Signal* signal) { |
| 356 | dbus::MessageReader reader(signal); |
| 357 | unsigned int error_code = 0; |
| 358 | std::string source_path; |
| 359 | unsigned int mount_type = 0; |
| 360 | std::string mount_path; |
| 361 | if (!reader.PopUint32(&error_code) || |
| 362 | !reader.PopString(&source_path) || |
| 363 | !reader.PopUint32(&mount_type) || |
| 364 | !reader.PopString(&mount_path)) { |
| 365 | LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
| 366 | return; |
| 367 | } |
| 368 | handler.Run(static_cast<MountError>(error_code), source_path, |
| 369 | static_cast<MountType>(mount_type), mount_path); |
| 370 | } |
| 371 | |
| 372 | // Handles the result of signal connection setup. |
| 373 | void OnSignalConnected(const std::string& interface, |
| 374 | const std::string& signal, |
| 375 | bool successed) { |
| 376 | LOG_IF(ERROR, !successed) << "Connect to " << interface << " " << |
| 377 | signal << " failed."; |
| 378 | } |
| 379 | |
| 380 | dbus::ObjectProxy* proxy_; |
[email protected] | 926957b | 2012-09-07 05:34:16 | [diff] [blame] | 381 | |
| 382 | // Note: This should remain the last member so it'll be destroyed and |
| 383 | // invalidate its weak pointers before any other members are destroyed. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 384 | base::WeakPtrFactory<CrosDisksClientImpl> weak_ptr_factory_; |
| 385 | |
| 386 | DISALLOW_COPY_AND_ASSIGN(CrosDisksClientImpl); |
| 387 | }; |
| 388 | |
| 389 | // A stub implementaion of CrosDisksClient. |
| 390 | class CrosDisksClientStubImpl : public CrosDisksClient { |
| 391 | public: |
| 392 | CrosDisksClientStubImpl() {} |
| 393 | virtual ~CrosDisksClientStubImpl() {} |
| 394 | |
| 395 | virtual void Mount(const std::string& source_path, |
[email protected] | b9f22d1 | 2012-04-25 21:46:48 | [diff] [blame] | 396 | const std::string& source_format, |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 397 | const std::string& mount_label, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 398 | MountType type, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 399 | const MountCallback& callback, |
| 400 | const ErrorCallback& error_callback) OVERRIDE {} |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 401 | virtual void Unmount(const std::string& device_path, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 402 | const UnmountCallback& callback, |
| 403 | const ErrorCallback& error_callback) OVERRIDE {} |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 404 | virtual void EnumerateAutoMountableDevices( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 405 | const EnumerateAutoMountableDevicesCallback& callback, |
| 406 | const ErrorCallback& error_callback) OVERRIDE {} |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 407 | virtual void FormatDevice(const std::string& device_path, |
| 408 | const std::string& filesystem, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 409 | const FormatDeviceCallback& callback, |
| 410 | const ErrorCallback& error_callback) OVERRIDE {} |
| 411 | virtual void GetDeviceProperties( |
| 412 | const std::string& device_path, |
| 413 | const GetDevicePropertiesCallback& callback, |
| 414 | const ErrorCallback& error_callback) OVERRIDE {} |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 415 | virtual void SetUpConnections( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 416 | const MountEventHandler& mount_event_handler, |
| 417 | const MountCompletedHandler& mount_completed_handler) OVERRIDE {} |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 418 | |
| 419 | private: |
| 420 | DISALLOW_COPY_AND_ASSIGN(CrosDisksClientStubImpl); |
| 421 | }; |
| 422 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 423 | } // namespace |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 424 | |
| 425 | //////////////////////////////////////////////////////////////////////////////// |
| 426 | // DiskInfo |
| 427 | |
| 428 | DiskInfo::DiskInfo(const std::string& device_path, dbus::Response* response) |
| 429 | : device_path_(device_path), |
| 430 | is_drive_(false), |
| 431 | has_media_(false), |
| 432 | on_boot_device_(false), |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 433 | device_type_(DEVICE_TYPE_UNKNOWN), |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 434 | total_size_in_bytes_(0), |
| 435 | is_read_only_(false), |
| 436 | is_hidden_(true) { |
| 437 | InitializeFromResponse(response); |
| 438 | } |
| 439 | |
| 440 | DiskInfo::~DiskInfo() { |
| 441 | } |
| 442 | |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 443 | // Initializes |this| from |response| given by the cros-disks service. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 444 | // Below is an example of |response|'s raw message (long string is ellipsized). |
| 445 | // |
| 446 | // |
| 447 | // message_type: MESSAGE_METHOD_RETURN |
| 448 | // destination: :1.8 |
| 449 | // sender: :1.16 |
| 450 | // signature: a{sv} |
| 451 | // serial: 96 |
| 452 | // reply_serial: 267 |
| 453 | // |
| 454 | // array [ |
| 455 | // dict entry { |
| 456 | // string "DeviceFile" |
| 457 | // variant string "/dev/sdb" |
| 458 | // } |
| 459 | // dict entry { |
| 460 | // string "DeviceIsDrive" |
| 461 | // variant bool true |
| 462 | // } |
| 463 | // dict entry { |
| 464 | // string "DeviceIsMediaAvailable" |
| 465 | // variant bool true |
| 466 | // } |
| 467 | // dict entry { |
| 468 | // string "DeviceIsMounted" |
| 469 | // variant bool false |
| 470 | // } |
| 471 | // dict entry { |
| 472 | // string "DeviceIsOnBootDevice" |
| 473 | // variant bool false |
| 474 | // } |
| 475 | // dict entry { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 476 | // string "DeviceIsReadOnly" |
| 477 | // variant bool false |
| 478 | // } |
| 479 | // dict entry { |
| 480 | // string "DeviceIsVirtual" |
| 481 | // variant bool false |
| 482 | // } |
| 483 | // dict entry { |
| 484 | // string "DeviceMediaType" |
| 485 | // variant uint32 1 |
| 486 | // } |
| 487 | // dict entry { |
| 488 | // string "DeviceMountPaths" |
| 489 | // variant array [ |
| 490 | // ] |
| 491 | // } |
| 492 | // dict entry { |
| 493 | // string "DevicePresentationHide" |
| 494 | // variant bool true |
| 495 | // } |
| 496 | // dict entry { |
| 497 | // string "DeviceSize" |
| 498 | // variant uint64 7998537728 |
| 499 | // } |
| 500 | // dict entry { |
| 501 | // string "DriveIsRotational" |
| 502 | // variant bool false |
| 503 | // } |
| 504 | // dict entry { |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame^] | 505 | // string "VendorId" |
| 506 | // variant string "18d1" |
| 507 | // } |
| 508 | // dict entry { |
| 509 | // string "VendorName" |
| 510 | // variant string "Google Inc." |
| 511 | // } |
| 512 | // dict entry { |
| 513 | // string "ProductId" |
| 514 | // variant string "4e11" |
| 515 | // } |
| 516 | // dict entry { |
| 517 | // string "ProductName" |
| 518 | // variant string "Nexus One" |
| 519 | // } |
| 520 | // dict entry { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 521 | // string "DriveModel" |
| 522 | // variant string "TransMemory" |
| 523 | // } |
| 524 | // dict entry { |
| 525 | // string "IdLabel" |
| 526 | // variant string "" |
| 527 | // } |
| 528 | // dict entry { |
| 529 | // string "IdUuid" |
| 530 | // variant string "" |
| 531 | // } |
| 532 | // dict entry { |
| 533 | // string "NativePath" |
| 534 | // variant string "/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-4/... |
| 535 | // } |
| 536 | // ] |
| 537 | void DiskInfo::InitializeFromResponse(dbus::Response* response) { |
| 538 | dbus::MessageReader response_reader(response); |
| 539 | dbus::MessageReader array_reader(response); |
| 540 | if (!response_reader.PopArray(&array_reader)) { |
| 541 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 542 | return; |
| 543 | } |
| 544 | // TODO(satorux): Rework this code using Protocol Buffers. crosbug.com/22626 |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 545 | typedef std::map<std::string, dbus::MessageReader*> PropertiesMap; |
| 546 | PropertiesMap properties; |
| 547 | STLValueDeleter<PropertiesMap> properties_value_deleter(&properties); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 548 | while (array_reader.HasMoreData()) { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 549 | dbus::MessageReader* value_reader = new dbus::MessageReader(response); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 550 | dbus::MessageReader dict_entry_reader(response); |
| 551 | std::string key; |
| 552 | if (!array_reader.PopDictEntry(&dict_entry_reader) || |
| 553 | !dict_entry_reader.PopString(&key) || |
| 554 | !dict_entry_reader.PopVariant(value_reader)) { |
| 555 | LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 556 | return; |
| 557 | } |
| 558 | properties[key] = value_reader; |
| 559 | } |
| 560 | MaybePopBool(properties[cros_disks::kDeviceIsDrive], &is_drive_); |
| 561 | MaybePopBool(properties[cros_disks::kDeviceIsReadOnly], &is_read_only_); |
| 562 | MaybePopBool(properties[cros_disks::kDevicePresentationHide], &is_hidden_); |
| 563 | MaybePopBool(properties[cros_disks::kDeviceIsMediaAvailable], &has_media_); |
| 564 | MaybePopBool(properties[cros_disks::kDeviceIsOnBootDevice], |
| 565 | &on_boot_device_); |
| 566 | MaybePopString(properties[cros_disks::kNativePath], &system_path_); |
| 567 | MaybePopString(properties[cros_disks::kDeviceFile], &file_path_); |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame^] | 568 | MaybePopString(properties[cros_disks::kVendorId], &vendor_id_); |
| 569 | MaybePopString(properties[cros_disks::kVendorName], &vendor_name_); |
| 570 | MaybePopString(properties[cros_disks::kProductId], &product_id_); |
| 571 | MaybePopString(properties[cros_disks::kProductName], &product_name_); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 572 | MaybePopString(properties[cros_disks::kDriveModel], &drive_model_); |
| 573 | MaybePopString(properties[cros_disks::kIdLabel], &label_); |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 574 | MaybePopString(properties[cros_disks::kIdUuid], &uuid_); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 575 | MaybePopUint64(properties[cros_disks::kDeviceSize], &total_size_in_bytes_); |
| 576 | |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 577 | uint32 media_type_uint32 = 0; |
| 578 | if (MaybePopUint32(properties[cros_disks::kDeviceMediaType], |
| 579 | &media_type_uint32)) { |
| 580 | device_type_ = DeviceMediaTypeToDeviceType(media_type_uint32); |
| 581 | } |
| 582 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 583 | std::vector<std::string> mount_paths; |
| 584 | if (MaybePopArrayOfStrings(properties[cros_disks::kDeviceMountPaths], |
| 585 | &mount_paths) && !mount_paths.empty()) |
| 586 | mount_path_ = mount_paths[0]; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | //////////////////////////////////////////////////////////////////////////////// |
| 590 | // CrosDisksClient |
| 591 | |
| 592 | CrosDisksClient::CrosDisksClient() {} |
| 593 | |
| 594 | CrosDisksClient::~CrosDisksClient() {} |
| 595 | |
| 596 | // static |
[email protected] | e8db03d6 | 2012-03-31 04:08:38 | [diff] [blame] | 597 | CrosDisksClient* CrosDisksClient::Create(DBusClientImplementationType type, |
| 598 | dbus::Bus* bus) { |
| 599 | if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 600 | return new CrosDisksClientImpl(bus); |
[email protected] | e8db03d6 | 2012-03-31 04:08:38 | [diff] [blame] | 601 | DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 602 | return new CrosDisksClientStubImpl(); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | } // namespace chromeos |