| 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 if (!GetClientInformation(&client_info)) | 151 if (!GetClientInformation(&client_info)) |
| 152 return false; | 152 return false; |
| 153 | 153 |
| 154 HRESULT hr = device->CreateInstance(__uuidof(PortableDevice), NULL, | 154 HRESULT hr = device->CreateInstance(__uuidof(PortableDevice), NULL, |
| 155 CLSCTX_INPROC_SERVER); | 155 CLSCTX_INPROC_SERVER); |
| 156 if (FAILED(hr)) { | 156 if (FAILED(hr)) { |
| 157 DPLOG(ERROR) << "Failed to create an instance of IPortableDevice"; | 157 DPLOG(ERROR) << "Failed to create an instance of IPortableDevice"; |
| 158 return false; | 158 return false; |
| 159 } | 159 } |
| 160 | 160 |
| 161 hr = (*device)->Open(pnp_device_id.c_str(), client_info.get()); | 161 hr = (*device)->Open(pnp_device_id.c_str(), client_info.Get()); |
| 162 if (SUCCEEDED(hr)) | 162 if (SUCCEEDED(hr)) |
| 163 return true; | 163 return true; |
| 164 | 164 |
| 165 if (hr == E_ACCESSDENIED) | 165 if (hr == E_ACCESSDENIED) |
| 166 DPLOG(ERROR) << "Access denied to open the device"; | 166 DPLOG(ERROR) << "Access denied to open the device"; |
| 167 return false; | 167 return false; |
| 168 } | 168 } |
| 169 | 169 |
| 170 // Returns the unique id property key of the object specified by the | 170 // Returns the unique id property key of the object specified by the |
| 171 // |object_id|. | 171 // |object_id|. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 DPLOG(ERROR) << "Failed to get IPortableDeviceProperties interface"; | 224 DPLOG(ERROR) << "Failed to get IPortableDeviceProperties interface"; |
| 225 return false; | 225 return false; |
| 226 } | 226 } |
| 227 | 227 |
| 228 base::win::ScopedComPtr<IPortableDeviceKeyCollection> properties_to_read; | 228 base::win::ScopedComPtr<IPortableDeviceKeyCollection> properties_to_read; |
| 229 if (!PopulatePropertyKeyCollection(object_id, &properties_to_read)) | 229 if (!PopulatePropertyKeyCollection(object_id, &properties_to_read)) |
| 230 return false; | 230 return false; |
| 231 | 231 |
| 232 base::win::ScopedComPtr<IPortableDeviceValues> properties_values; | 232 base::win::ScopedComPtr<IPortableDeviceValues> properties_values; |
| 233 if (FAILED(properties->GetValues(object_id.c_str(), | 233 if (FAILED(properties->GetValues(object_id.c_str(), |
| 234 properties_to_read.get(), | 234 properties_to_read.Get(), |
| 235 properties_values.Receive()))) { | 235 properties_values.Receive()))) { |
| 236 return false; | 236 return false; |
| 237 } | 237 } |
| 238 | 238 |
| 239 REFPROPERTYKEY key = GetUniqueIdPropertyKey(object_id); | 239 REFPROPERTYKEY key = GetUniqueIdPropertyKey(object_id); |
| 240 return GetStringPropertyValue(properties_values.get(), key, unique_id); | 240 return GetStringPropertyValue(properties_values.Get(), key, unique_id); |
| 241 } | 241 } |
| 242 | 242 |
| 243 // Constructs the device storage unique identifier using |device_serial_num| and | 243 // Constructs the device storage unique identifier using |device_serial_num| and |
| 244 // |storage_id|. On success, returns true and fills in |device_storage_id|. | 244 // |storage_id|. On success, returns true and fills in |device_storage_id|. |
| 245 bool ConstructDeviceStorageUniqueId(const base::string16& device_serial_num, | 245 bool ConstructDeviceStorageUniqueId(const base::string16& device_serial_num, |
| 246 const base::string16& storage_id, | 246 const base::string16& storage_id, |
| 247 std::string* device_storage_id) { | 247 std::string* device_storage_id) { |
| 248 if (device_serial_num.empty() && storage_id.empty()) | 248 if (device_serial_num.empty() && storage_id.empty()) |
| 249 return false; | 249 return false; |
| 250 | 250 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 bool GetDeviceStorageObjectsOnBlockingThread( | 329 bool GetDeviceStorageObjectsOnBlockingThread( |
| 330 const base::string16& pnp_device_id, | 330 const base::string16& pnp_device_id, |
| 331 PortableDeviceWatcherWin::StorageObjects* storage_objects) { | 331 PortableDeviceWatcherWin::StorageObjects* storage_objects) { |
| 332 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 332 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 333 DCHECK(storage_objects); | 333 DCHECK(storage_objects); |
| 334 base::win::ScopedComPtr<IPortableDevice> device; | 334 base::win::ScopedComPtr<IPortableDevice> device; |
| 335 if (!SetUp(pnp_device_id, &device)) | 335 if (!SetUp(pnp_device_id, &device)) |
| 336 return false; | 336 return false; |
| 337 | 337 |
| 338 base::string16 device_serial_num; | 338 base::string16 device_serial_num; |
| 339 if (!GetObjectUniqueId(device.get(), WPD_DEVICE_OBJECT_ID, | 339 if (!GetObjectUniqueId(device.Get(), WPD_DEVICE_OBJECT_ID, |
| 340 &device_serial_num)) { | 340 &device_serial_num)) { |
| 341 return false; | 341 return false; |
| 342 } | 342 } |
| 343 | 343 |
| 344 PortableDeviceWatcherWin::StorageObjectIDs storage_obj_ids; | 344 PortableDeviceWatcherWin::StorageObjectIDs storage_obj_ids; |
| 345 if (!GetRemovableStorageObjectIds(device.get(), &storage_obj_ids)) | 345 if (!GetRemovableStorageObjectIds(device.Get(), &storage_obj_ids)) |
| 346 return false; | 346 return false; |
| 347 for (PortableDeviceWatcherWin::StorageObjectIDs::const_iterator id_iter = | 347 for (PortableDeviceWatcherWin::StorageObjectIDs::const_iterator id_iter = |
| 348 storage_obj_ids.begin(); id_iter != storage_obj_ids.end(); ++id_iter) { | 348 storage_obj_ids.begin(); id_iter != storage_obj_ids.end(); ++id_iter) { |
| 349 base::string16 storage_persistent_id; | 349 base::string16 storage_persistent_id; |
| 350 if (!GetObjectUniqueId(device.get(), *id_iter, &storage_persistent_id)) | 350 if (!GetObjectUniqueId(device.Get(), *id_iter, &storage_persistent_id)) |
| 351 continue; | 351 continue; |
| 352 | 352 |
| 353 std::string device_storage_id; | 353 std::string device_storage_id; |
| 354 if (ConstructDeviceStorageUniqueId(device_serial_num, storage_persistent_id, | 354 if (ConstructDeviceStorageUniqueId(device_serial_num, storage_persistent_id, |
| 355 &device_storage_id)) { | 355 &device_storage_id)) { |
| 356 storage_objects->push_back(PortableDeviceWatcherWin::DeviceStorageObject( | 356 storage_objects->push_back(PortableDeviceWatcherWin::DeviceStorageObject( |
| 357 *id_iter, device_storage_id)); | 357 *id_iter, device_storage_id)); |
| 358 } | 358 } |
| 359 } | 359 } |
| 360 return true; | 360 return true; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 return false; | 416 return false; |
| 417 | 417 |
| 418 std::unique_ptr<base::char16*[]> pnp_device_ids( | 418 std::unique_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(portable_device_mgr.get(), | 426 if (GetDeviceInfoOnBlockingThread(portable_device_mgr.Get(), |
| 427 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.get(), 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 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 if (storage_notifications_) { | 670 if (storage_notifications_) { |
| 671 storage_notifications_->ProcessDetach( | 671 storage_notifications_->ProcessDetach( |
| 672 storage_map_iter->second.device_id()); | 672 storage_map_iter->second.device_id()); |
| 673 } | 673 } |
| 674 storage_map_.erase(storage_map_iter); | 674 storage_map_.erase(storage_map_iter); |
| 675 } | 675 } |
| 676 device_map_.erase(device_iter); | 676 device_map_.erase(device_iter); |
| 677 } | 677 } |
| 678 | 678 |
| 679 } // namespace storage_monitor | 679 } // namespace storage_monitor |
| OLD | NEW |