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 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
306 // the volume name. | 306 // the volume name. |
307 return ((device_name.length() >= 2) && (device_name[1] == L':') && | 307 return ((device_name.length() >= 2) && (device_name[1] == L':') && |
308 (((device_name[0] >= L'A') && (device_name[0] <= L'Z')) || | 308 (((device_name[0] >= L'A') && (device_name[0] <= L'Z')) || |
309 ((device_name[0] >= L'a') && (device_name[0] <= L'z')))); | 309 ((device_name[0] >= L'a') && (device_name[0] <= L'z')))); |
310 } | 310 } |
311 | 311 |
312 // Returns the name of the device specified by |pnp_device_id|. | 312 // Returns the name of the device specified by |pnp_device_id|. |
313 base::string16 GetDeviceNameOnBlockingThread( | 313 base::string16 GetDeviceNameOnBlockingThread( |
314 IPortableDeviceManager* portable_device_manager, | 314 IPortableDeviceManager* portable_device_manager, |
315 const base::string16& pnp_device_id) { | 315 const base::string16& pnp_device_id) { |
316 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
317 DCHECK(portable_device_manager); | 316 DCHECK(portable_device_manager); |
317 base::ThreadRestrictions::AssertIOAllowed(); | |
blundell
2017/05/17 15:24:52
Is this the canonical change to make here?
Yeol Park
2017/05/19 04:34:51
I used TaskScheduler instead of blocking pool and
blundell
2017/05/22 09:29:39
I would do this in a different CL, yes.
Yeol Park
2017/06/23 04:59:20
Done in https://codereview.chromium.org/2943923002
| |
318 base::string16 name; | 318 base::string16 name; |
319 GetFriendlyName(pnp_device_id, portable_device_manager, &name) || | 319 GetFriendlyName(pnp_device_id, portable_device_manager, &name) || |
320 GetDeviceDescription(pnp_device_id, portable_device_manager, &name) || | 320 GetDeviceDescription(pnp_device_id, portable_device_manager, &name) || |
321 GetManufacturerName(pnp_device_id, portable_device_manager, &name); | 321 GetManufacturerName(pnp_device_id, portable_device_manager, &name); |
322 return name; | 322 return name; |
323 } | 323 } |
324 | 324 |
325 // Access the device and gets the device storage details. On success, returns | 325 // Access the device and gets the device storage details. On success, returns |
326 // true and populates |storage_objects| with device storage details. | 326 // true and populates |storage_objects| with device storage details. |
327 bool GetDeviceStorageObjectsOnBlockingThread( | 327 bool GetDeviceStorageObjectsOnBlockingThread( |
328 const base::string16& pnp_device_id, | 328 const base::string16& pnp_device_id, |
329 PortableDeviceWatcherWin::StorageObjects* storage_objects) { | 329 PortableDeviceWatcherWin::StorageObjects* storage_objects) { |
330 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
331 DCHECK(storage_objects); | 330 DCHECK(storage_objects); |
331 base::ThreadRestrictions::AssertIOAllowed(); | |
332 base::win::ScopedComPtr<IPortableDevice> device; | 332 base::win::ScopedComPtr<IPortableDevice> device; |
333 if (!SetUp(pnp_device_id, &device)) | 333 if (!SetUp(pnp_device_id, &device)) |
334 return false; | 334 return false; |
335 | 335 |
336 base::string16 device_serial_num; | 336 base::string16 device_serial_num; |
337 if (!GetObjectUniqueId(device.Get(), WPD_DEVICE_OBJECT_ID, | 337 if (!GetObjectUniqueId(device.Get(), WPD_DEVICE_OBJECT_ID, |
338 &device_serial_num)) { | 338 &device_serial_num)) { |
339 return false; | 339 return false; |
340 } | 340 } |
341 | 341 |
(...skipping 16 matching lines...) Expand all Loading... | |
358 return true; | 358 return true; |
359 } | 359 } |
360 | 360 |
361 // Accesses the device and gets the device details (name, storage info, etc). | 361 // Accesses the device and gets the device details (name, storage info, etc). |
362 // On success returns true and fills in |device_details|. On failure, returns | 362 // On success returns true and fills in |device_details|. On failure, returns |
363 // false. |pnp_device_id| specifies the plug and play device ID string. | 363 // false. |pnp_device_id| specifies the plug and play device ID string. |
364 bool GetDeviceInfoOnBlockingThread( | 364 bool GetDeviceInfoOnBlockingThread( |
365 IPortableDeviceManager* portable_device_manager, | 365 IPortableDeviceManager* portable_device_manager, |
366 const base::string16& pnp_device_id, | 366 const base::string16& pnp_device_id, |
367 PortableDeviceWatcherWin::DeviceDetails* device_details) { | 367 PortableDeviceWatcherWin::DeviceDetails* device_details) { |
368 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
369 DCHECK(portable_device_manager); | 368 DCHECK(portable_device_manager); |
370 DCHECK(device_details); | 369 DCHECK(device_details); |
371 DCHECK(!pnp_device_id.empty()); | 370 DCHECK(!pnp_device_id.empty()); |
371 base::ThreadRestrictions::AssertIOAllowed(); | |
372 device_details->name = GetDeviceNameOnBlockingThread(portable_device_manager, | 372 device_details->name = GetDeviceNameOnBlockingThread(portable_device_manager, |
373 pnp_device_id); | 373 pnp_device_id); |
374 if (IsMassStoragePortableDevice(pnp_device_id, device_details->name)) | 374 if (IsMassStoragePortableDevice(pnp_device_id, device_details->name)) |
375 return false; | 375 return false; |
376 | 376 |
377 device_details->location = pnp_device_id; | 377 device_details->location = pnp_device_id; |
378 PortableDeviceWatcherWin::StorageObjects storage_objects; | 378 PortableDeviceWatcherWin::StorageObjects storage_objects; |
379 return GetDeviceStorageObjectsOnBlockingThread( | 379 return GetDeviceStorageObjectsOnBlockingThread( |
380 pnp_device_id, &device_details->storage_objects); | 380 pnp_device_id, &device_details->storage_objects); |
381 } | 381 } |
382 | 382 |
383 // Wrapper function to get an instance of portable device manager. On success, | 383 // Wrapper function to get an instance of portable device manager. On success, |
384 // returns true and fills in |portable_device_mgr|. On failure, returns false. | 384 // returns true and fills in |portable_device_mgr|. On failure, returns false. |
385 bool GetPortableDeviceManager( | 385 bool GetPortableDeviceManager( |
386 base::win::ScopedComPtr<IPortableDeviceManager>* portable_device_mgr) { | 386 base::win::ScopedComPtr<IPortableDeviceManager>* portable_device_mgr) { |
387 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 387 base::ThreadRestrictions::AssertIOAllowed(); |
388 HRESULT hr = portable_device_mgr->CreateInstance( | 388 HRESULT hr = portable_device_mgr->CreateInstance( |
389 __uuidof(PortableDeviceManager), NULL, CLSCTX_INPROC_SERVER); | 389 __uuidof(PortableDeviceManager), NULL, CLSCTX_INPROC_SERVER); |
390 if (SUCCEEDED(hr)) | 390 if (SUCCEEDED(hr)) |
391 return true; | 391 return true; |
392 | 392 |
393 // Either there is no portable device support (Windows XP with old versions of | 393 // Either there is no portable device support (Windows XP with old versions of |
394 // Media Player) or the thread does not have COM initialized. | 394 // Media Player) or the thread does not have COM initialized. |
395 DCHECK_NE(CO_E_NOTINITIALIZED, hr); | 395 DCHECK_NE(CO_E_NOTINITIALIZED, hr); |
396 return false; | 396 return false; |
397 } | 397 } |
398 | 398 |
399 // Enumerates the attached portable devices. On success, returns true and fills | 399 // Enumerates the attached portable devices. On success, returns true and fills |
400 // in |devices| with the attached portable device details. On failure, returns | 400 // in |devices| with the attached portable device details. On failure, returns |
401 // false. | 401 // false. |
402 bool EnumerateAttachedDevicesOnBlockingThread( | 402 bool EnumerateAttachedDevicesOnBlockingThread( |
403 PortableDeviceWatcherWin::Devices* devices) { | 403 PortableDeviceWatcherWin::Devices* devices) { |
404 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
405 DCHECK(devices); | 404 DCHECK(devices); |
405 base::ThreadRestrictions::AssertIOAllowed(); | |
406 base::win::ScopedComPtr<IPortableDeviceManager> portable_device_mgr; | 406 base::win::ScopedComPtr<IPortableDeviceManager> portable_device_mgr; |
407 if (!GetPortableDeviceManager(&portable_device_mgr)) | 407 if (!GetPortableDeviceManager(&portable_device_mgr)) |
408 return false; | 408 return false; |
409 | 409 |
410 // Get the total number of devices found on the system. | 410 // Get the total number of devices found on the system. |
411 DWORD pnp_device_count = 0; | 411 DWORD pnp_device_count = 0; |
412 HRESULT hr = portable_device_mgr->GetDevices(NULL, &pnp_device_count); | 412 HRESULT hr = portable_device_mgr->GetDevices(NULL, &pnp_device_count); |
413 if (FAILED(hr)) | 413 if (FAILED(hr)) |
414 return false; | 414 return false; |
415 | 415 |
(...skipping 13 matching lines...) Expand all Loading... | |
429 return !devices->empty(); | 429 return !devices->empty(); |
430 } | 430 } |
431 | 431 |
432 // Handles the device attach event message on a media task runner. | 432 // Handles the device attach event message on a media task runner. |
433 // |pnp_device_id| specifies the attached plug and play device ID string. On | 433 // |pnp_device_id| specifies the attached plug and play device ID string. On |
434 // success, returns true and populates |device_details| with device information. | 434 // success, returns true and populates |device_details| with device information. |
435 // On failure, returns false. | 435 // On failure, returns false. |
436 bool HandleDeviceAttachedEventOnBlockingThread( | 436 bool HandleDeviceAttachedEventOnBlockingThread( |
437 const base::string16& pnp_device_id, | 437 const base::string16& pnp_device_id, |
438 PortableDeviceWatcherWin::DeviceDetails* device_details) { | 438 PortableDeviceWatcherWin::DeviceDetails* device_details) { |
439 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
440 DCHECK(device_details); | 439 DCHECK(device_details); |
440 base::ThreadRestrictions::AssertIOAllowed(); | |
441 base::win::ScopedComPtr<IPortableDeviceManager> portable_device_mgr; | 441 base::win::ScopedComPtr<IPortableDeviceManager> portable_device_mgr; |
442 if (!GetPortableDeviceManager(&portable_device_mgr)) | 442 if (!GetPortableDeviceManager(&portable_device_mgr)) |
443 return false; | 443 return false; |
444 // Sometimes, portable device manager doesn't have the new device details. | 444 // Sometimes, portable device manager doesn't have the new device details. |
445 // Refresh the manager device list to update its details. | 445 // Refresh the manager device list to update its details. |
446 portable_device_mgr->RefreshDeviceList(); | 446 portable_device_mgr->RefreshDeviceList(); |
447 return GetDeviceInfoOnBlockingThread(portable_device_mgr.Get(), pnp_device_id, | 447 return GetDeviceInfoOnBlockingThread(portable_device_mgr.Get(), pnp_device_id, |
448 device_details); | 448 device_details); |
449 } | 449 } |
450 | 450 |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
665 if (storage_notifications_) { | 665 if (storage_notifications_) { |
666 storage_notifications_->ProcessDetach( | 666 storage_notifications_->ProcessDetach( |
667 storage_map_iter->second.device_id()); | 667 storage_map_iter->second.device_id()); |
668 } | 668 } |
669 storage_map_.erase(storage_map_iter); | 669 storage_map_.erase(storage_map_iter); |
670 } | 670 } |
671 device_map_.erase(device_iter); | 671 device_map_.erase(device_iter); |
672 } | 672 } |
673 | 673 |
674 } // namespace storage_monitor | 674 } // namespace storage_monitor |
OLD | NEW |