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