| 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 "components/usb_service/usb_context.h" | 5 #include "components/usb_service/usb_context.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 10 #include "components/usb_service/usb_error.h" |
| 10 #include "third_party/libusb/src/libusb/interrupt.h" | 11 #include "third_party/libusb/src/libusb/interrupt.h" |
| 11 #include "third_party/libusb/src/libusb/libusb.h" | 12 #include "third_party/libusb/src/libusb/libusb.h" |
| 12 | 13 |
| 13 namespace usb_service { | 14 namespace usb_service { |
| 14 | 15 |
| 15 // The UsbEventHandler works around a design flaw in the libusb interface. There | 16 // The UsbEventHandler works around a design flaw in the libusb interface. There |
| 16 // is currently no way to signal to libusb that any caller into one of the event | 17 // is currently no way to signal to libusb that any caller into one of the event |
| 17 // handler calls should return without handling any events. | 18 // handler calls should return without handling any events. |
| 18 class UsbContext::UsbEventHandler : public base::PlatformThread::Delegate { | 19 class UsbContext::UsbEventHandler : public base::PlatformThread::Delegate { |
| 19 public: | 20 public: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 47 base::subtle::MemoryBarrier(); | 48 base::subtle::MemoryBarrier(); |
| 48 libusb_interrupt_handle_event(context_); | 49 libusb_interrupt_handle_event(context_); |
| 49 base::PlatformThread::Join(thread_handle_); | 50 base::PlatformThread::Join(thread_handle_); |
| 50 } | 51 } |
| 51 | 52 |
| 52 void UsbContext::UsbEventHandler::ThreadMain() { | 53 void UsbContext::UsbEventHandler::ThreadMain() { |
| 53 base::PlatformThread::SetName("UsbEventHandler"); | 54 base::PlatformThread::SetName("UsbEventHandler"); |
| 54 VLOG(1) << "UsbEventHandler started."; | 55 VLOG(1) << "UsbEventHandler started."; |
| 55 if (running_) { | 56 if (running_) { |
| 56 start_polling_.Signal(); | 57 start_polling_.Signal(); |
| 57 libusb_handle_events(context_); | |
| 58 } | 58 } |
| 59 while (running_) | 59 while (running_) { |
| 60 libusb_handle_events(context_); | 60 const int rv = libusb_handle_events(context_); |
| 61 if (rv != LIBUSB_SUCCESS) { |
| 62 LOG(WARNING) << "Failed to handle events: " << ConvertErrorToString(rv); |
| 63 } |
| 64 } |
| 61 VLOG(1) << "UsbEventHandler shutting down."; | 65 VLOG(1) << "UsbEventHandler shutting down."; |
| 62 } | 66 } |
| 63 | 67 |
| 64 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { | 68 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | 69 DCHECK(thread_checker_.CalledOnValidThread()); |
| 66 event_handler_ = new UsbEventHandler(context_); | 70 event_handler_ = new UsbEventHandler(context_); |
| 67 } | 71 } |
| 68 | 72 |
| 69 UsbContext::~UsbContext() { | 73 UsbContext::~UsbContext() { |
| 70 // destruction of UsbEventHandler is a blocking operation. | 74 // destruction of UsbEventHandler is a blocking operation. |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); | 75 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 delete event_handler_; | 76 delete event_handler_; |
| 73 event_handler_ = NULL; | 77 event_handler_ = NULL; |
| 74 libusb_exit(context_); | 78 libusb_exit(context_); |
| 75 } | 79 } |
| 76 | 80 |
| 77 } // namespace usb_service | 81 } // namespace usb_service |
| OLD | NEW |