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 #include "device/usb/usb_service_impl.h" | 5 #include "device/usb/usb_service_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 #include <memory> | 10 #include <memory> |
11 #include <set> | 11 #include <set> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/barrier_closure.h" | 14 #include "base/barrier_closure.h" |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/task_scheduler/post_task.h" | |
22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
23 #include "components/device_event_log/device_event_log.h" | 24 #include "components/device_event_log/device_event_log.h" |
24 #include "device/usb/usb_device_handle.h" | 25 #include "device/usb/usb_device_handle.h" |
25 #include "device/usb/usb_error.h" | 26 #include "device/usb/usb_error.h" |
26 #include "device/usb/webusb_descriptors.h" | 27 #include "device/usb/webusb_descriptors.h" |
27 #include "net/base/io_buffer.h" | 28 #include "net/base/io_buffer.h" |
28 #include "third_party/libusb/src/libusb/libusb.h" | 29 #include "third_party/libusb/src/libusb/libusb.h" |
29 | 30 |
30 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
31 #define INITGUID | 32 #define INITGUID |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 ReadWebUsbDescriptors(device_handle, base::Bind(&OnReadBosDescriptor, | 210 ReadWebUsbDescriptors(device_handle, base::Bind(&OnReadBosDescriptor, |
210 device_handle, barrier)); | 211 device_handle, barrier)); |
211 } | 212 } |
212 } else { | 213 } else { |
213 failure_closure.Run(); | 214 failure_closure.Run(); |
214 } | 215 } |
215 } | 216 } |
216 | 217 |
217 } // namespace | 218 } // namespace |
218 | 219 |
219 UsbServiceImpl::UsbServiceImpl( | 220 UsbServiceImpl::UsbServiceImpl() |
220 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_in) | 221 : UsbService(nullptr), |
221 : UsbService(std::move(blocking_task_runner_in)), | |
222 #if defined(OS_WIN) | 222 #if defined(OS_WIN) |
223 device_observer_(this), | 223 device_observer_(this), |
224 #endif | 224 #endif |
225 weak_factory_(this) { | 225 weak_factory_(this) { |
226 blocking_task_runner()->PostTask( | 226 base::PostTaskWithTraits( |
robliao
2017/05/02 14:32:59
Calls to this API are done in parallel. If this ta
Reilly Grant (use Gerrit)
2017/05/02 23:41:44
That's fine. RefreshDevices is guaranteed not to p
| |
227 FROM_HERE, | 227 FROM_HERE, |
228 base::TaskTraits() | |
229 .MayBlock() | |
230 .WithPriority(base::TaskPriority::USER_VISIBLE) | |
231 .WithShutdownBehavior( | |
232 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | |
228 base::Bind(&InitializeUsbContextOnBlockingThread, task_runner(), | 233 base::Bind(&InitializeUsbContextOnBlockingThread, task_runner(), |
229 base::Bind(&UsbServiceImpl::OnUsbContext, | 234 base::Bind(&UsbServiceImpl::OnUsbContext, |
230 weak_factory_.GetWeakPtr()))); | 235 weak_factory_.GetWeakPtr()))); |
231 } | 236 } |
232 | 237 |
233 UsbServiceImpl::~UsbServiceImpl() { | 238 UsbServiceImpl::~UsbServiceImpl() { |
234 if (hotplug_enabled_) | 239 if (hotplug_enabled_) |
235 libusb_hotplug_deregister_callback(context_->context(), hotplug_handle_); | 240 libusb_hotplug_deregister_callback(context_->context(), hotplug_handle_); |
236 for (auto* platform_device : ignored_devices_) | 241 for (auto* platform_device : ignored_devices_) |
237 libusb_unref_device(platform_device); | 242 libusb_unref_device(platform_device); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 | 322 |
318 enumeration_in_progress_ = true; | 323 enumeration_in_progress_ = true; |
319 DCHECK(devices_being_enumerated_.empty()); | 324 DCHECK(devices_being_enumerated_.empty()); |
320 | 325 |
321 std::string device_path; | 326 std::string device_path; |
322 if (!pending_path_enumerations_.empty()) { | 327 if (!pending_path_enumerations_.empty()) { |
323 device_path = pending_path_enumerations_.front(); | 328 device_path = pending_path_enumerations_.front(); |
324 pending_path_enumerations_.pop(); | 329 pending_path_enumerations_.pop(); |
325 } | 330 } |
326 | 331 |
327 blocking_task_runner()->PostTask( | 332 base::PostTaskWithTraits( |
328 FROM_HERE, | 333 FROM_HERE, |
334 base::TaskTraits() | |
335 .MayBlock() | |
336 .WithPriority(base::TaskPriority::USER_VISIBLE) | |
337 .WithShutdownBehavior( | |
338 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | |
329 base::Bind(&GetDeviceListOnBlockingThread, device_path, context_, | 339 base::Bind(&GetDeviceListOnBlockingThread, device_path, context_, |
330 task_runner(), base::Bind(&UsbServiceImpl::OnDeviceList, | 340 task_runner(), |
331 weak_factory_.GetWeakPtr()))); | 341 base::Bind(&UsbServiceImpl::OnDeviceList, |
342 weak_factory_.GetWeakPtr()))); | |
332 } | 343 } |
333 | 344 |
334 void UsbServiceImpl::OnDeviceList(libusb_device** platform_devices, | 345 void UsbServiceImpl::OnDeviceList(libusb_device** platform_devices, |
335 size_t device_count) { | 346 size_t device_count) { |
336 DCHECK(CalledOnValidThread()); | 347 DCHECK(CalledOnValidThread()); |
337 if (!platform_devices) { | 348 if (!platform_devices) { |
338 RefreshDevicesComplete(); | 349 RefreshDevicesComplete(); |
339 return; | 350 return; |
340 } | 351 } |
341 | 352 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
430 int rv = libusb_get_device_descriptor(platform_device, &descriptor); | 441 int rv = libusb_get_device_descriptor(platform_device, &descriptor); |
431 if (rv == LIBUSB_SUCCESS) { | 442 if (rv == LIBUSB_SUCCESS) { |
432 if (descriptor.bDeviceClass == LIBUSB_CLASS_HUB) { | 443 if (descriptor.bDeviceClass == LIBUSB_CLASS_HUB) { |
433 // Don't try to enumerate hubs. We never want to connect to a hub. | 444 // Don't try to enumerate hubs. We never want to connect to a hub. |
434 libusb_ref_device(platform_device); | 445 libusb_ref_device(platform_device); |
435 ignored_devices_.insert(platform_device); | 446 ignored_devices_.insert(platform_device); |
436 refresh_complete.Run(); | 447 refresh_complete.Run(); |
437 return; | 448 return; |
438 } | 449 } |
439 | 450 |
440 scoped_refptr<UsbDeviceImpl> device(new UsbDeviceImpl( | 451 scoped_refptr<UsbDeviceImpl> device( |
441 context_, platform_device, descriptor, blocking_task_runner())); | 452 new UsbDeviceImpl(context_, platform_device, descriptor)); |
442 base::Closure add_device = | 453 base::Closure add_device = |
443 base::Bind(&UsbServiceImpl::AddDevice, weak_factory_.GetWeakPtr(), | 454 base::Bind(&UsbServiceImpl::AddDevice, weak_factory_.GetWeakPtr(), |
444 refresh_complete, device); | 455 refresh_complete, device); |
445 base::Closure enumeration_failed = base::Bind( | 456 base::Closure enumeration_failed = base::Bind( |
446 &UsbServiceImpl::EnumerationFailed, weak_factory_.GetWeakPtr(), | 457 &UsbServiceImpl::EnumerationFailed, weak_factory_.GetWeakPtr(), |
447 platform_device, refresh_complete); | 458 platform_device, refresh_complete); |
448 bool read_bos_descriptors = descriptor.bcdUSB >= kUsbVersion2_1; | 459 bool read_bos_descriptors = descriptor.bcdUSB >= kUsbVersion2_1; |
449 | 460 |
450 if (descriptor.iManufacturer == 0 && descriptor.iProduct == 0 && | 461 if (descriptor.iManufacturer == 0 && descriptor.iProduct == 0 && |
451 descriptor.iSerialNumber == 0 && !read_bos_descriptors) { | 462 descriptor.iSerialNumber == 0 && !read_bos_descriptors) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
551 } | 562 } |
552 | 563 |
553 void UsbServiceImpl::EnumerationFailed(PlatformUsbDevice platform_device, | 564 void UsbServiceImpl::EnumerationFailed(PlatformUsbDevice platform_device, |
554 const base::Closure& refresh_complete) { | 565 const base::Closure& refresh_complete) { |
555 libusb_ref_device(platform_device); | 566 libusb_ref_device(platform_device); |
556 ignored_devices_.insert(platform_device); | 567 ignored_devices_.insert(platform_device); |
557 refresh_complete.Run(); | 568 refresh_complete.Run(); |
558 } | 569 } |
559 | 570 |
560 } // namespace device | 571 } // namespace device |
OLD | NEW |