OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // Any tasks that communicates with the portable device may take >100ms to | 5 // Any tasks that communicates with the portable device may take >100ms to |
6 // complete. Those tasks should be run on an blocking thread instead of the | 6 // complete. Those tasks should be run on an blocking thread instead of the |
7 // UI thread. | 7 // UI thread. |
8 | 8 |
9 #include "components/storage_monitor/portable_device_watcher_win.h" | 9 #include "components/storage_monitor/portable_device_watcher_win.h" |
10 | 10 |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 : notifications_(NULL), | 482 : notifications_(NULL), |
483 storage_notifications_(NULL), | 483 storage_notifications_(NULL), |
484 weak_ptr_factory_(this) { | 484 weak_ptr_factory_(this) { |
485 } | 485 } |
486 | 486 |
487 PortableDeviceWatcherWin::~PortableDeviceWatcherWin() { | 487 PortableDeviceWatcherWin::~PortableDeviceWatcherWin() { |
488 UnregisterDeviceNotification(notifications_); | 488 UnregisterDeviceNotification(notifications_); |
489 } | 489 } |
490 | 490 |
491 void PortableDeviceWatcherWin::Init(HWND hwnd) { | 491 void PortableDeviceWatcherWin::Init(HWND hwnd) { |
492 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 492 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
493 notifications_ = RegisterPortableDeviceNotification(hwnd); | 493 notifications_ = RegisterPortableDeviceNotification(hwnd); |
494 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); | 494 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); |
495 media_task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | 495 media_task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
496 pool->GetNamedSequenceToken(kMediaTaskRunnerName), | 496 pool->GetNamedSequenceToken(kMediaTaskRunnerName), |
497 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | 497 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
498 EnumerateAttachedDevices(); | 498 EnumerateAttachedDevices(); |
499 } | 499 } |
500 | 500 |
501 void PortableDeviceWatcherWin::OnWindowMessage(UINT event_type, LPARAM data) { | 501 void PortableDeviceWatcherWin::OnWindowMessage(UINT event_type, LPARAM data) { |
502 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 502 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
503 if (!IsPortableDeviceStructure(data)) | 503 if (!IsPortableDeviceStructure(data)) |
504 return; | 504 return; |
505 | 505 |
506 base::string16 device_id = GetPnpDeviceId(data); | 506 base::string16 device_id = GetPnpDeviceId(data); |
507 if (event_type == DBT_DEVICEARRIVAL) | 507 if (event_type == DBT_DEVICEARRIVAL) |
508 HandleDeviceAttachEvent(device_id); | 508 HandleDeviceAttachEvent(device_id); |
509 else if (event_type == DBT_DEVICEREMOVECOMPLETE) | 509 else if (event_type == DBT_DEVICEREMOVECOMPLETE) |
510 HandleDeviceDetachEvent(device_id); | 510 HandleDeviceDetachEvent(device_id); |
511 } | 511 } |
512 | 512 |
513 bool PortableDeviceWatcherWin::GetMTPStorageInfoFromDeviceId( | 513 bool PortableDeviceWatcherWin::GetMTPStorageInfoFromDeviceId( |
514 const std::string& storage_device_id, | 514 const std::string& storage_device_id, |
515 base::string16* device_location, | 515 base::string16* device_location, |
516 base::string16* storage_object_id) const { | 516 base::string16* storage_object_id) const { |
517 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 517 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
518 DCHECK(device_location); | 518 DCHECK(device_location); |
519 DCHECK(storage_object_id); | 519 DCHECK(storage_object_id); |
520 MTPStorageMap::const_iterator storage_map_iter = | 520 MTPStorageMap::const_iterator storage_map_iter = |
521 storage_map_.find(storage_device_id); | 521 storage_map_.find(storage_device_id); |
522 if (storage_map_iter == storage_map_.end()) | 522 if (storage_map_iter == storage_map_.end()) |
523 return false; | 523 return false; |
524 | 524 |
525 MTPDeviceMap::const_iterator device_iter = | 525 MTPDeviceMap::const_iterator device_iter = |
526 device_map_.find(storage_map_iter->second.location()); | 526 device_map_.find(storage_map_iter->second.location()); |
527 if (device_iter == device_map_.end()) | 527 if (device_iter == device_map_.end()) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 callback.Run(StorageMonitor::EJECT_NO_SUCH_DEVICE); | 566 callback.Run(StorageMonitor::EJECT_NO_SUCH_DEVICE); |
567 return; | 567 return; |
568 } | 568 } |
569 HandleDeviceDetachEvent(device_location); | 569 HandleDeviceDetachEvent(device_location); |
570 | 570 |
571 callback.Run(StorageMonitor::EJECT_OK); | 571 callback.Run(StorageMonitor::EJECT_OK); |
572 } | 572 } |
573 | 573 |
574 void PortableDeviceWatcherWin::EnumerateAttachedDevices() { | 574 void PortableDeviceWatcherWin::EnumerateAttachedDevices() { |
575 DCHECK(media_task_runner_.get()); | 575 DCHECK(media_task_runner_.get()); |
576 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 576 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
577 Devices* devices = new Devices; | 577 Devices* devices = new Devices; |
578 base::PostTaskAndReplyWithResult( | 578 base::PostTaskAndReplyWithResult( |
579 media_task_runner_.get(), FROM_HERE, | 579 media_task_runner_.get(), FROM_HERE, |
580 base::Bind(&EnumerateAttachedDevicesOnBlockingThread, devices), | 580 base::Bind(&EnumerateAttachedDevicesOnBlockingThread, devices), |
581 base::Bind(&PortableDeviceWatcherWin::OnDidEnumerateAttachedDevices, | 581 base::Bind(&PortableDeviceWatcherWin::OnDidEnumerateAttachedDevices, |
582 weak_ptr_factory_.GetWeakPtr(), base::Owned(devices))); | 582 weak_ptr_factory_.GetWeakPtr(), base::Owned(devices))); |
583 } | 583 } |
584 | 584 |
585 void PortableDeviceWatcherWin::OnDidEnumerateAttachedDevices( | 585 void PortableDeviceWatcherWin::OnDidEnumerateAttachedDevices( |
586 const Devices* devices, const bool result) { | 586 const Devices* devices, const bool result) { |
587 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 587 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
588 DCHECK(devices); | 588 DCHECK(devices); |
589 if (!result) | 589 if (!result) |
590 return; | 590 return; |
591 for (Devices::const_iterator device_iter = devices->begin(); | 591 for (Devices::const_iterator device_iter = devices->begin(); |
592 device_iter != devices->end(); ++device_iter) { | 592 device_iter != devices->end(); ++device_iter) { |
593 OnDidHandleDeviceAttachEvent(&(*device_iter), result); | 593 OnDidHandleDeviceAttachEvent(&(*device_iter), result); |
594 } | 594 } |
595 } | 595 } |
596 | 596 |
597 void PortableDeviceWatcherWin::HandleDeviceAttachEvent( | 597 void PortableDeviceWatcherWin::HandleDeviceAttachEvent( |
598 const base::string16& pnp_device_id) { | 598 const base::string16& pnp_device_id) { |
599 DCHECK(media_task_runner_.get()); | 599 DCHECK(media_task_runner_.get()); |
600 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 600 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
601 DeviceDetails* device_details = new DeviceDetails; | 601 DeviceDetails* device_details = new DeviceDetails; |
602 base::PostTaskAndReplyWithResult( | 602 base::PostTaskAndReplyWithResult( |
603 media_task_runner_.get(), FROM_HERE, | 603 media_task_runner_.get(), FROM_HERE, |
604 base::Bind(&HandleDeviceAttachedEventOnBlockingThread, pnp_device_id, | 604 base::Bind(&HandleDeviceAttachedEventOnBlockingThread, pnp_device_id, |
605 device_details), | 605 device_details), |
606 base::Bind(&PortableDeviceWatcherWin::OnDidHandleDeviceAttachEvent, | 606 base::Bind(&PortableDeviceWatcherWin::OnDidHandleDeviceAttachEvent, |
607 weak_ptr_factory_.GetWeakPtr(), base::Owned(device_details))); | 607 weak_ptr_factory_.GetWeakPtr(), base::Owned(device_details))); |
608 } | 608 } |
609 | 609 |
610 void PortableDeviceWatcherWin::OnDidHandleDeviceAttachEvent( | 610 void PortableDeviceWatcherWin::OnDidHandleDeviceAttachEvent( |
611 const DeviceDetails* device_details, const bool result) { | 611 const DeviceDetails* device_details, const bool result) { |
612 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 612 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
613 DCHECK(device_details); | 613 DCHECK(device_details); |
614 if (!result) | 614 if (!result) |
615 return; | 615 return; |
616 | 616 |
617 const StorageObjects& storage_objects = device_details->storage_objects; | 617 const StorageObjects& storage_objects = device_details->storage_objects; |
618 const base::string16& name = device_details->name; | 618 const base::string16& name = device_details->name; |
619 const base::string16& location = device_details->location; | 619 const base::string16& location = device_details->location; |
620 DCHECK(!ContainsKey(device_map_, location)); | 620 DCHECK(!ContainsKey(device_map_, location)); |
621 for (StorageObjects::const_iterator storage_iter = storage_objects.begin(); | 621 for (StorageObjects::const_iterator storage_iter = storage_objects.begin(); |
622 storage_iter != storage_objects.end(); ++storage_iter) { | 622 storage_iter != storage_objects.end(); ++storage_iter) { |
(...skipping 16 matching lines...) Expand all Loading... |
639 if (storage_notifications_) { | 639 if (storage_notifications_) { |
640 info.set_location(GetStoragePathFromStorageId(storage_id)); | 640 info.set_location(GetStoragePathFromStorageId(storage_id)); |
641 storage_notifications_->ProcessAttach(info); | 641 storage_notifications_->ProcessAttach(info); |
642 } | 642 } |
643 } | 643 } |
644 device_map_[location] = storage_objects; | 644 device_map_[location] = storage_objects; |
645 } | 645 } |
646 | 646 |
647 void PortableDeviceWatcherWin::HandleDeviceDetachEvent( | 647 void PortableDeviceWatcherWin::HandleDeviceDetachEvent( |
648 const base::string16& pnp_device_id) { | 648 const base::string16& pnp_device_id) { |
649 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 649 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
650 MTPDeviceMap::iterator device_iter = device_map_.find(pnp_device_id); | 650 MTPDeviceMap::iterator device_iter = device_map_.find(pnp_device_id); |
651 if (device_iter == device_map_.end()) | 651 if (device_iter == device_map_.end()) |
652 return; | 652 return; |
653 | 653 |
654 const StorageObjects& storage_objects = device_iter->second; | 654 const StorageObjects& storage_objects = device_iter->second; |
655 for (StorageObjects::const_iterator storage_object_iter = | 655 for (StorageObjects::const_iterator storage_object_iter = |
656 storage_objects.begin(); storage_object_iter != storage_objects.end(); | 656 storage_objects.begin(); storage_object_iter != storage_objects.end(); |
657 ++storage_object_iter) { | 657 ++storage_object_iter) { |
658 std::string storage_id = storage_object_iter->object_persistent_id; | 658 std::string storage_id = storage_object_iter->object_persistent_id; |
659 MTPStorageMap::iterator storage_map_iter = storage_map_.find(storage_id); | 659 MTPStorageMap::iterator storage_map_iter = storage_map_.find(storage_id); |
660 DCHECK(storage_map_iter != storage_map_.end()); | 660 DCHECK(storage_map_iter != storage_map_.end()); |
661 if (storage_notifications_) { | 661 if (storage_notifications_) { |
662 storage_notifications_->ProcessDetach( | 662 storage_notifications_->ProcessDetach( |
663 storage_map_iter->second.device_id()); | 663 storage_map_iter->second.device_id()); |
664 } | 664 } |
665 storage_map_.erase(storage_map_iter); | 665 storage_map_.erase(storage_map_iter); |
666 } | 666 } |
667 device_map_.erase(device_iter); | 667 device_map_.erase(device_iter); |
668 } | 668 } |
669 | 669 |
670 } // namespace storage_monitor | 670 } // namespace storage_monitor |
OLD | NEW |