| 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 "device/usb/usb_context.h" |
| 6 | 6 |
| 7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| 11 #include "components/usb_service/usb_error.h" | 11 #include "device/usb/usb_error.h" |
| 12 #include "third_party/libusb/src/libusb/interrupt.h" | 12 #include "third_party/libusb/src/libusb/interrupt.h" |
| 13 #include "third_party/libusb/src/libusb/libusb.h" | 13 #include "third_party/libusb/src/libusb/libusb.h" |
| 14 | 14 |
| 15 namespace usb_service { | 15 namespace device { |
| 16 | 16 |
| 17 // The UsbEventHandler works around a design flaw in the libusb interface. There | 17 // The UsbEventHandler works around a design flaw in the libusb interface. There |
| 18 // is currently no way to signal to libusb that any caller into one of the event | 18 // is currently no way to signal to libusb that any caller into one of the event |
| 19 // handler calls should return without handling any events. | 19 // handler calls should return without handling any events. |
| 20 class UsbContext::UsbEventHandler : public base::PlatformThread::Delegate { | 20 class UsbContext::UsbEventHandler : public base::PlatformThread::Delegate { |
| 21 public: | 21 public: |
| 22 explicit UsbEventHandler(libusb_context* context); | 22 explicit UsbEventHandler(libusb_context* context); |
| 23 virtual ~UsbEventHandler(); | 23 virtual ~UsbEventHandler(); |
| 24 | 24 |
| 25 // base::PlatformThread::Delegate | 25 // base::PlatformThread::Delegate |
| 26 virtual void ThreadMain() OVERRIDE; | 26 virtual void ThreadMain() OVERRIDE; |
| 27 | 27 |
| 28 private: | 28 private: |
| 29 base::subtle::Atomic32 running_; | 29 base::subtle::Atomic32 running_; |
| 30 libusb_context* context_; | 30 libusb_context* context_; |
| 31 base::PlatformThreadHandle thread_handle_; | 31 base::PlatformThreadHandle thread_handle_; |
| 32 base::WaitableEvent start_polling_; | 32 base::WaitableEvent start_polling_; |
| 33 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler); | 33 DISALLOW_COPY_AND_ASSIGN(UsbEventHandler); |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 UsbContext::UsbEventHandler::UsbEventHandler(libusb_context* context) | 36 UsbContext::UsbEventHandler::UsbEventHandler(libusb_context* context) |
| 37 : context_(context), | 37 : context_(context), thread_handle_(0), start_polling_(false, false) { |
| 38 thread_handle_(0), | |
| 39 start_polling_(false, false) { | |
| 40 base::subtle::Release_Store(&running_, 1); | 38 base::subtle::Release_Store(&running_, 1); |
| 41 bool success = base::PlatformThread::Create(0, this, &thread_handle_); | 39 bool success = base::PlatformThread::Create(0, this, &thread_handle_); |
| 42 DCHECK(success) << "Failed to create USB IO handling thread."; | 40 DCHECK(success) << "Failed to create USB IO handling thread."; |
| 43 start_polling_.Wait(); | 41 start_polling_.Wait(); |
| 44 } | 42 } |
| 45 | 43 |
| 46 UsbContext::UsbEventHandler::~UsbEventHandler() { | 44 UsbContext::UsbEventHandler::~UsbEventHandler() { |
| 47 base::subtle::Release_Store(&running_, 0); | 45 base::subtle::Release_Store(&running_, 0); |
| 48 libusb_interrupt_handle_event(context_); | 46 libusb_interrupt_handle_event(context_); |
| 49 base::PlatformThread::Join(thread_handle_); | 47 base::PlatformThread::Join(thread_handle_); |
| 50 } | 48 } |
| 51 | 49 |
| 52 void UsbContext::UsbEventHandler::ThreadMain() { | 50 void UsbContext::UsbEventHandler::ThreadMain() { |
| 53 base::PlatformThread::SetName("UsbEventHandler"); | 51 base::PlatformThread::SetName("UsbEventHandler"); |
| 54 VLOG(1) << "UsbEventHandler started."; | 52 VLOG(1) << "UsbEventHandler started."; |
| 55 | 53 |
| 56 if (base::subtle::Acquire_Load(&running_)) { | 54 if (base::subtle::Acquire_Load(&running_)) { |
| 57 start_polling_.Signal(); | 55 start_polling_.Signal(); |
| 58 } | 56 } |
| 59 while (base::subtle::Acquire_Load(&running_)) { | 57 while (base::subtle::Acquire_Load(&running_)) { |
| 60 const int rv = libusb_handle_events(context_); | 58 const int rv = libusb_handle_events(context_); |
| 61 if (rv != LIBUSB_SUCCESS) { | 59 if (rv != LIBUSB_SUCCESS) { |
| 62 VLOG(1) << "Failed to handle events: " << ConvertErrorToString(rv); | 60 VLOG(1) << "Failed to handle events: " |
| 61 << ConvertPlatformUsbErrorToString(rv); |
| 63 } | 62 } |
| 64 } | 63 } |
| 65 VLOG(1) << "UsbEventHandler shutting down."; | 64 VLOG(1) << "UsbEventHandler shutting down."; |
| 66 } | 65 } |
| 67 | 66 |
| 68 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { | 67 UsbContext::UsbContext(PlatformUsbContext context) : context_(context) { |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 70 event_handler_ = new UsbEventHandler(context_); | 69 event_handler_ = new UsbEventHandler(context_); |
| 71 } | 70 } |
| 72 | 71 |
| 73 UsbContext::~UsbContext() { | 72 UsbContext::~UsbContext() { |
| 74 // destruction of UsbEventHandler is a blocking operation. | 73 // destruction of UsbEventHandler is a blocking operation. |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
| 76 delete event_handler_; | 75 delete event_handler_; |
| 77 event_handler_ = NULL; | 76 event_handler_ = NULL; |
| 78 libusb_exit(context_); | 77 libusb_exit(context_); |
| 79 } | 78 } |
| 80 | 79 |
| 81 } // namespace usb_service | 80 } // namespace device |
| OLD | NEW |