| 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 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 return false; | 416 return false; |
| 417 | 417 |
| 418 scoped_ptr<base::char16*[]> pnp_device_ids( | 418 scoped_ptr<base::char16*[]> pnp_device_ids( |
| 419 new base::char16*[pnp_device_count]); | 419 new base::char16*[pnp_device_count]); |
| 420 hr = portable_device_mgr->GetDevices(pnp_device_ids.get(), &pnp_device_count); | 420 hr = portable_device_mgr->GetDevices(pnp_device_ids.get(), &pnp_device_count); |
| 421 if (FAILED(hr)) | 421 if (FAILED(hr)) |
| 422 return false; | 422 return false; |
| 423 | 423 |
| 424 for (DWORD index = 0; index < pnp_device_count; ++index) { | 424 for (DWORD index = 0; index < pnp_device_count; ++index) { |
| 425 PortableDeviceWatcherWin::DeviceDetails device_details; | 425 PortableDeviceWatcherWin::DeviceDetails device_details; |
| 426 if (GetDeviceInfoOnBlockingThread( | 426 if (GetDeviceInfoOnBlockingThread(portable_device_mgr.get(), |
| 427 portable_device_mgr, pnp_device_ids[index], &device_details)) | 427 pnp_device_ids[index], &device_details)) |
| 428 devices->push_back(device_details); | 428 devices->push_back(device_details); |
| 429 CoTaskMemFree(pnp_device_ids[index]); | 429 CoTaskMemFree(pnp_device_ids[index]); |
| 430 } | 430 } |
| 431 return !devices->empty(); | 431 return !devices->empty(); |
| 432 } | 432 } |
| 433 | 433 |
| 434 // Handles the device attach event message on a media task runner. | 434 // Handles the device attach event message on a media task runner. |
| 435 // |pnp_device_id| specifies the attached plug and play device ID string. On | 435 // |pnp_device_id| specifies the attached plug and play device ID string. On |
| 436 // success, returns true and populates |device_details| with device information. | 436 // success, returns true and populates |device_details| with device information. |
| 437 // On failure, returns false. | 437 // On failure, returns false. |
| 438 bool HandleDeviceAttachedEventOnBlockingThread( | 438 bool HandleDeviceAttachedEventOnBlockingThread( |
| 439 const base::string16& pnp_device_id, | 439 const base::string16& pnp_device_id, |
| 440 PortableDeviceWatcherWin::DeviceDetails* device_details) { | 440 PortableDeviceWatcherWin::DeviceDetails* device_details) { |
| 441 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 441 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 442 DCHECK(device_details); | 442 DCHECK(device_details); |
| 443 base::win::ScopedComPtr<IPortableDeviceManager> portable_device_mgr; | 443 base::win::ScopedComPtr<IPortableDeviceManager> portable_device_mgr; |
| 444 if (!GetPortableDeviceManager(&portable_device_mgr)) | 444 if (!GetPortableDeviceManager(&portable_device_mgr)) |
| 445 return false; | 445 return false; |
| 446 // Sometimes, portable device manager doesn't have the new device details. | 446 // Sometimes, portable device manager doesn't have the new device details. |
| 447 // Refresh the manager device list to update its details. | 447 // Refresh the manager device list to update its details. |
| 448 portable_device_mgr->RefreshDeviceList(); | 448 portable_device_mgr->RefreshDeviceList(); |
| 449 return GetDeviceInfoOnBlockingThread(portable_device_mgr, pnp_device_id, | 449 return GetDeviceInfoOnBlockingThread(portable_device_mgr.get(), pnp_device_id, |
| 450 device_details); | 450 device_details); |
| 451 } | 451 } |
| 452 | 452 |
| 453 // Registers |hwnd| to receive portable device notification details. On success, | 453 // Registers |hwnd| to receive portable device notification details. On success, |
| 454 // returns the device notifications handle else returns NULL. | 454 // returns the device notifications handle else returns NULL. |
| 455 HDEVNOTIFY RegisterPortableDeviceNotification(HWND hwnd) { | 455 HDEVNOTIFY RegisterPortableDeviceNotification(HWND hwnd) { |
| 456 GUID dev_interface_guid = GUID_NULL; | 456 GUID dev_interface_guid = GUID_NULL; |
| 457 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &dev_interface_guid); | 457 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &dev_interface_guid); |
| 458 if (FAILED(hr)) | 458 if (FAILED(hr)) |
| 459 return NULL; | 459 return NULL; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 577 Devices* devices = new Devices; | 577 Devices* devices = new Devices; |
| 578 base::PostTaskAndReplyWithResult( | 578 base::PostTaskAndReplyWithResult( |
| 579 media_task_runner_, | 579 media_task_runner_.get(), FROM_HERE, |
| 580 FROM_HERE, | |
| 581 base::Bind(&EnumerateAttachedDevicesOnBlockingThread, devices), | 580 base::Bind(&EnumerateAttachedDevicesOnBlockingThread, devices), |
| 582 base::Bind(&PortableDeviceWatcherWin::OnDidEnumerateAttachedDevices, | 581 base::Bind(&PortableDeviceWatcherWin::OnDidEnumerateAttachedDevices, |
| 583 weak_ptr_factory_.GetWeakPtr(), base::Owned(devices))); | 582 weak_ptr_factory_.GetWeakPtr(), base::Owned(devices))); |
| 584 } | 583 } |
| 585 | 584 |
| 586 void PortableDeviceWatcherWin::OnDidEnumerateAttachedDevices( | 585 void PortableDeviceWatcherWin::OnDidEnumerateAttachedDevices( |
| 587 const Devices* devices, const bool result) { | 586 const Devices* devices, const bool result) { |
| 588 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 587 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 589 DCHECK(devices); | 588 DCHECK(devices); |
| 590 if (!result) | 589 if (!result) |
| 591 return; | 590 return; |
| 592 for (Devices::const_iterator device_iter = devices->begin(); | 591 for (Devices::const_iterator device_iter = devices->begin(); |
| 593 device_iter != devices->end(); ++device_iter) { | 592 device_iter != devices->end(); ++device_iter) { |
| 594 OnDidHandleDeviceAttachEvent(&(*device_iter), result); | 593 OnDidHandleDeviceAttachEvent(&(*device_iter), result); |
| 595 } | 594 } |
| 596 } | 595 } |
| 597 | 596 |
| 598 void PortableDeviceWatcherWin::HandleDeviceAttachEvent( | 597 void PortableDeviceWatcherWin::HandleDeviceAttachEvent( |
| 599 const base::string16& pnp_device_id) { | 598 const base::string16& pnp_device_id) { |
| 600 DCHECK(media_task_runner_.get()); | 599 DCHECK(media_task_runner_.get()); |
| 601 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 600 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 602 DeviceDetails* device_details = new DeviceDetails; | 601 DeviceDetails* device_details = new DeviceDetails; |
| 603 base::PostTaskAndReplyWithResult( | 602 base::PostTaskAndReplyWithResult( |
| 604 media_task_runner_, | 603 media_task_runner_.get(), FROM_HERE, |
| 605 FROM_HERE, | |
| 606 base::Bind(&HandleDeviceAttachedEventOnBlockingThread, pnp_device_id, | 604 base::Bind(&HandleDeviceAttachedEventOnBlockingThread, pnp_device_id, |
| 607 device_details), | 605 device_details), |
| 608 base::Bind(&PortableDeviceWatcherWin::OnDidHandleDeviceAttachEvent, | 606 base::Bind(&PortableDeviceWatcherWin::OnDidHandleDeviceAttachEvent, |
| 609 weak_ptr_factory_.GetWeakPtr(), base::Owned(device_details))); | 607 weak_ptr_factory_.GetWeakPtr(), base::Owned(device_details))); |
| 610 } | 608 } |
| 611 | 609 |
| 612 void PortableDeviceWatcherWin::OnDidHandleDeviceAttachEvent( | 610 void PortableDeviceWatcherWin::OnDidHandleDeviceAttachEvent( |
| 613 const DeviceDetails* device_details, const bool result) { | 611 const DeviceDetails* device_details, const bool result) { |
| 614 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 612 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 615 DCHECK(device_details); | 613 DCHECK(device_details); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 if (storage_notifications_) { | 661 if (storage_notifications_) { |
| 664 storage_notifications_->ProcessDetach( | 662 storage_notifications_->ProcessDetach( |
| 665 storage_map_iter->second.device_id()); | 663 storage_map_iter->second.device_id()); |
| 666 } | 664 } |
| 667 storage_map_.erase(storage_map_iter); | 665 storage_map_.erase(storage_map_iter); |
| 668 } | 666 } |
| 669 device_map_.erase(device_iter); | 667 device_map_.erase(device_iter); |
| 670 } | 668 } |
| 671 | 669 |
| 672 } // namespace storage_monitor | 670 } // namespace storage_monitor |
| OLD | NEW |