OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/cros_disks_client.h" | 5 #include "chromeos/dbus/cros_disks_client.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 return DEVICE_TYPE_OPTICAL_DISC; | 69 return DEVICE_TYPE_OPTICAL_DISC; |
70 case(cros_disks::DEVICE_MEDIA_MOBILE): | 70 case(cros_disks::DEVICE_MEDIA_MOBILE): |
71 return DEVICE_TYPE_MOBILE; | 71 return DEVICE_TYPE_MOBILE; |
72 case(cros_disks::DEVICE_MEDIA_DVD): | 72 case(cros_disks::DEVICE_MEDIA_DVD): |
73 return DEVICE_TYPE_DVD; | 73 return DEVICE_TYPE_DVD; |
74 default: | 74 default: |
75 return DEVICE_TYPE_UNKNOWN; | 75 return DEVICE_TYPE_UNKNOWN; |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
| 79 bool ReadMountEntryFromDbus(dbus::MessageReader* reader, MountEntry* entry) { |
| 80 uint32 error_code = 0; |
| 81 std::string source_path; |
| 82 uint32 mount_type = 0; |
| 83 std::string mount_path; |
| 84 if (!reader->PopUint32(&error_code) || |
| 85 !reader->PopString(&source_path) || |
| 86 !reader->PopUint32(&mount_type) || |
| 87 !reader->PopString(&mount_path)) { |
| 88 return false; |
| 89 } |
| 90 *entry = MountEntry(static_cast<MountError>(error_code), source_path, |
| 91 static_cast<MountType>(mount_type), mount_path); |
| 92 return true; |
| 93 } |
| 94 |
79 // The CrosDisksClient implementation. | 95 // The CrosDisksClient implementation. |
80 class CrosDisksClientImpl : public CrosDisksClient { | 96 class CrosDisksClientImpl : public CrosDisksClient { |
81 public: | 97 public: |
82 CrosDisksClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {} | 98 CrosDisksClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {} |
83 | 99 |
84 // CrosDisksClient override. | 100 // CrosDisksClient override. |
85 virtual void Mount(const std::string& source_path, | 101 virtual void Mount(const std::string& source_path, |
86 const std::string& source_format, | 102 const std::string& source_format, |
87 const std::string& mount_label, | 103 const std::string& mount_label, |
88 const base::Closure& callback, | 104 const base::Closure& callback, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 cros_disks::kEnumerateAutoMountableDevices); | 157 cros_disks::kEnumerateAutoMountableDevices); |
142 proxy_->CallMethod( | 158 proxy_->CallMethod( |
143 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 159 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
144 base::Bind(&CrosDisksClientImpl::OnEnumerateAutoMountableDevices, | 160 base::Bind(&CrosDisksClientImpl::OnEnumerateAutoMountableDevices, |
145 weak_ptr_factory_.GetWeakPtr(), | 161 weak_ptr_factory_.GetWeakPtr(), |
146 callback, | 162 callback, |
147 error_callback)); | 163 error_callback)); |
148 } | 164 } |
149 | 165 |
150 // CrosDisksClient override. | 166 // CrosDisksClient override. |
| 167 virtual void EnumerateMountEntries( |
| 168 const EnumerateMountEntriesCallback& callback, |
| 169 const base::Closure& error_callback) OVERRIDE { |
| 170 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
| 171 cros_disks::kEnumerateMountEntries); |
| 172 proxy_->CallMethod( |
| 173 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 174 base::Bind(&CrosDisksClientImpl::OnEnumerateMountEntries, |
| 175 weak_ptr_factory_.GetWeakPtr(), |
| 176 callback, |
| 177 error_callback)); |
| 178 } |
| 179 |
| 180 // CrosDisksClient override. |
151 virtual void Format(const std::string& device_path, | 181 virtual void Format(const std::string& device_path, |
152 const std::string& filesystem, | 182 const std::string& filesystem, |
153 const base::Closure& callback, | 183 const base::Closure& callback, |
154 const base::Closure& error_callback) OVERRIDE { | 184 const base::Closure& error_callback) OVERRIDE { |
155 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, | 185 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, |
156 cros_disks::kFormat); | 186 cros_disks::kFormat); |
157 dbus::MessageWriter writer(&method_call); | 187 dbus::MessageWriter writer(&method_call); |
158 writer.AppendString(device_path); | 188 writer.AppendString(device_path); |
159 writer.AppendString(filesystem); | 189 writer.AppendString(filesystem); |
160 // No format option is currently specified, but we can later use this | 190 // No format option is currently specified, but we can later use this |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 void OnMount(const base::Closure& callback, | 287 void OnMount(const base::Closure& callback, |
258 const base::Closure& error_callback, | 288 const base::Closure& error_callback, |
259 dbus::Response* response) { | 289 dbus::Response* response) { |
260 if (!response) { | 290 if (!response) { |
261 error_callback.Run(); | 291 error_callback.Run(); |
262 return; | 292 return; |
263 } | 293 } |
264 callback.Run(); | 294 callback.Run(); |
265 } | 295 } |
266 | 296 |
267 // Handles the result of Unount and calls |callback| or |error_callback|. | 297 // Handles the result of Unmount and calls |callback| or |error_callback|. |
268 void OnUnmount(const base::Closure& callback, | 298 void OnUnmount(const base::Closure& callback, |
269 const base::Closure& error_callback, | 299 const base::Closure& error_callback, |
270 dbus::Response* response) { | 300 dbus::Response* response) { |
271 if (!response) { | 301 if (!response) { |
272 error_callback.Run(); | 302 error_callback.Run(); |
273 return; | 303 return; |
274 } | 304 } |
275 | 305 |
276 // Temporarly allow Unmount method to report failure both by setting dbus | 306 // Temporarly allow Unmount method to report failure both by setting dbus |
277 // error (in which case response is not set) and by returning mount error | 307 // error (in which case response is not set) and by returning mount error |
(...skipping 27 matching lines...) Expand all Loading... |
305 dbus::MessageReader reader(response); | 335 dbus::MessageReader reader(response); |
306 std::vector<std::string> device_paths; | 336 std::vector<std::string> device_paths; |
307 if (!reader.PopArrayOfStrings(&device_paths)) { | 337 if (!reader.PopArrayOfStrings(&device_paths)) { |
308 LOG(ERROR) << "Invalid response: " << response->ToString(); | 338 LOG(ERROR) << "Invalid response: " << response->ToString(); |
309 error_callback.Run(); | 339 error_callback.Run(); |
310 return; | 340 return; |
311 } | 341 } |
312 callback.Run(device_paths); | 342 callback.Run(device_paths); |
313 } | 343 } |
314 | 344 |
| 345 // Handles the result of EnumerateMountEntries and calls |callback| or |
| 346 // |error_callback|. |
| 347 void OnEnumerateMountEntries( |
| 348 const EnumerateMountEntriesCallback& callback, |
| 349 const base::Closure& error_callback, |
| 350 dbus::Response* response) { |
| 351 if (!response) { |
| 352 error_callback.Run(); |
| 353 return; |
| 354 } |
| 355 |
| 356 dbus::MessageReader reader(response); |
| 357 dbus::MessageReader array_reader(NULL); |
| 358 if (!reader.PopArray(&array_reader)) { |
| 359 LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 360 error_callback.Run(); |
| 361 return; |
| 362 } |
| 363 |
| 364 std::vector<MountEntry> entries; |
| 365 while (array_reader.HasMoreData()) { |
| 366 MountEntry entry; |
| 367 dbus::MessageReader sub_reader(NULL); |
| 368 if (!array_reader.PopStruct(&sub_reader) || |
| 369 !ReadMountEntryFromDbus(&sub_reader, &entry)) { |
| 370 LOG(ERROR) << "Invalid response: " << response->ToString(); |
| 371 error_callback.Run(); |
| 372 return; |
| 373 } |
| 374 entries.push_back(entry); |
| 375 } |
| 376 callback.Run(entries); |
| 377 } |
| 378 |
315 // Handles the result of Format and calls |callback| or |error_callback|. | 379 // Handles the result of Format and calls |callback| or |error_callback|. |
316 void OnFormat(const base::Closure& callback, | 380 void OnFormat(const base::Closure& callback, |
317 const base::Closure& error_callback, | 381 const base::Closure& error_callback, |
318 dbus::Response* response) { | 382 dbus::Response* response) { |
319 if (!response) { | 383 if (!response) { |
320 error_callback.Run(); | 384 error_callback.Run(); |
321 return; | 385 return; |
322 } | 386 } |
323 callback.Run(); | 387 callback.Run(); |
324 } | 388 } |
(...skipping 21 matching lines...) Expand all Loading... |
346 if (!reader.PopString(&device)) { | 410 if (!reader.PopString(&device)) { |
347 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | 411 LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
348 return; | 412 return; |
349 } | 413 } |
350 handler.Run(event_type, device); | 414 handler.Run(event_type, device); |
351 } | 415 } |
352 | 416 |
353 // Handles MountCompleted signal and calls |handler|. | 417 // Handles MountCompleted signal and calls |handler|. |
354 void OnMountCompleted(MountCompletedHandler handler, dbus::Signal* signal) { | 418 void OnMountCompleted(MountCompletedHandler handler, dbus::Signal* signal) { |
355 dbus::MessageReader reader(signal); | 419 dbus::MessageReader reader(signal); |
356 uint32 error_code = 0; | 420 MountEntry entry; |
357 std::string source_path; | 421 if (!ReadMountEntryFromDbus(&reader, &entry)) { |
358 uint32 mount_type = 0; | |
359 std::string mount_path; | |
360 if (!reader.PopUint32(&error_code) || | |
361 !reader.PopString(&source_path) || | |
362 !reader.PopUint32(&mount_type) || | |
363 !reader.PopString(&mount_path)) { | |
364 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | 422 LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
365 return; | 423 return; |
366 } | 424 } |
367 handler.Run(static_cast<MountError>(error_code), source_path, | 425 handler.Run(entry); |
368 static_cast<MountType>(mount_type), mount_path); | |
369 } | 426 } |
370 | 427 |
371 // Handles FormatCompleted signal and calls |handler|. | 428 // Handles FormatCompleted signal and calls |handler|. |
372 void OnFormatCompleted(FormatCompletedHandler handler, dbus::Signal* signal) { | 429 void OnFormatCompleted(FormatCompletedHandler handler, dbus::Signal* signal) { |
373 dbus::MessageReader reader(signal); | 430 dbus::MessageReader reader(signal); |
374 uint32 error_code = 0; | 431 uint32 error_code = 0; |
375 std::string device_path; | 432 std::string device_path; |
376 if (!reader.PopUint32(&error_code) || !reader.PopString(&device_path)) { | 433 if (!reader.PopUint32(&error_code) || !reader.PopString(&device_path)) { |
377 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | 434 LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
378 return; | 435 return; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 } | 518 } |
462 | 519 |
463 virtual void EnumerateAutoMountableDevices( | 520 virtual void EnumerateAutoMountableDevices( |
464 const EnumerateAutoMountableDevicesCallback& callback, | 521 const EnumerateAutoMountableDevicesCallback& callback, |
465 const base::Closure& error_callback) OVERRIDE { | 522 const base::Closure& error_callback) OVERRIDE { |
466 std::vector<std::string> device_paths; | 523 std::vector<std::string> device_paths; |
467 base::MessageLoopProxy::current()->PostTask( | 524 base::MessageLoopProxy::current()->PostTask( |
468 FROM_HERE, base::Bind(callback, device_paths)); | 525 FROM_HERE, base::Bind(callback, device_paths)); |
469 } | 526 } |
470 | 527 |
| 528 virtual void EnumerateMountEntries( |
| 529 const EnumerateMountEntriesCallback& callback, |
| 530 const base::Closure& error_callback) OVERRIDE { |
| 531 std::vector<MountEntry> entries; |
| 532 base::MessageLoopProxy::current()->PostTask( |
| 533 FROM_HERE, base::Bind(callback, entries)); |
| 534 } |
| 535 |
471 virtual void Format(const std::string& device_path, | 536 virtual void Format(const std::string& device_path, |
472 const std::string& filesystem, | 537 const std::string& filesystem, |
473 const base::Closure& callback, | 538 const base::Closure& callback, |
474 const base::Closure& error_callback) OVERRIDE { | 539 const base::Closure& error_callback) OVERRIDE { |
475 base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback); | 540 base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback); |
476 } | 541 } |
477 | 542 |
478 virtual void GetDeviceProperties( | 543 virtual void GetDeviceProperties( |
479 const std::string& device_path, | 544 const std::string& device_path, |
480 const GetDevicePropertiesCallback& callback, | 545 const GetDevicePropertiesCallback& callback, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 void FinishMount(MountError error, | 613 void FinishMount(MountError error, |
549 const std::string& source_path, | 614 const std::string& source_path, |
550 MountType type, | 615 MountType type, |
551 const std::string& mounted_path, | 616 const std::string& mounted_path, |
552 const base::Closure& callback) { | 617 const base::Closure& callback) { |
553 base::MessageLoopProxy::current()->PostTask(FROM_HERE, callback); | 618 base::MessageLoopProxy::current()->PostTask(FROM_HERE, callback); |
554 if (!mount_completed_handler_.is_null()) { | 619 if (!mount_completed_handler_.is_null()) { |
555 base::MessageLoopProxy::current()->PostTask( | 620 base::MessageLoopProxy::current()->PostTask( |
556 FROM_HERE, | 621 FROM_HERE, |
557 base::Bind(mount_completed_handler_, | 622 base::Bind(mount_completed_handler_, |
558 error, source_path, type, mounted_path)); | 623 MountEntry(error, source_path, type, mounted_path))); |
559 } | 624 } |
560 } | 625 } |
561 | 626 |
562 // Mounted path to source path map. | 627 // Mounted path to source path map. |
563 std::map<std::string, std::string> mounted_to_source_path_map_; | 628 std::map<std::string, std::string> mounted_to_source_path_map_; |
564 | 629 |
565 MountEventHandler mount_event_handler_; | 630 MountEventHandler mount_event_handler_; |
566 MountCompletedHandler mount_completed_handler_; | 631 MountCompletedHandler mount_completed_handler_; |
567 FormatCompletedHandler format_completed_handler_; | 632 FormatCompletedHandler format_completed_handler_; |
568 | 633 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 } | 826 } |
762 | 827 |
763 // static | 828 // static |
764 base::FilePath CrosDisksClient::GetRemovableDiskMountPoint() { | 829 base::FilePath CrosDisksClient::GetRemovableDiskMountPoint() { |
765 return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? | 830 return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? |
766 FILE_PATH_LITERAL("/media/removable") : | 831 FILE_PATH_LITERAL("/media/removable") : |
767 FILE_PATH_LITERAL("/tmp/chromeos/media/removable")); | 832 FILE_PATH_LITERAL("/tmp/chromeos/media/removable")); |
768 } | 833 } |
769 | 834 |
770 } // namespace chromeos | 835 } // namespace chromeos |
OLD | NEW |