OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_USB_USB_SERVICE_H_ |
| 6 #define CHROME_BROWSER_USB_USB_SERVICE_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <map> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/threading/thread.h" |
| 14 #include "chrome/browser/usb/usb_device.h" |
| 15 #include "third_party/libusb/libusb/libusb.h" |
| 16 |
| 17 typedef libusb_context *PlatformUsbContext; |
| 18 |
| 19 // The USB service handles creating and managing an event handler thread that is |
| 20 // used to manage and dispatch USB events. It is also responsbile for device |
| 21 // discovery on the system, which allows it to re-use device handles to prevent |
| 22 // competition for the same USB device. |
| 23 class UsbService { |
| 24 public: |
| 25 UsbService(); |
| 26 virtual ~UsbService(); |
| 27 |
| 28 // Cleanup must be invoked before the service is destroyed. It interrupts the |
| 29 // event handling thread and disposes of open devices. |
| 30 void Cleanup(); |
| 31 |
| 32 // Find the (topologically) first USB device identified by vendor_id and |
| 33 // product_id. The created device is associated with this service, so that |
| 34 // it can be used to close the device later. |
| 35 UsbDevice *FindDevice(const uint16 vendor_id, const uint16 product_id); |
| 36 |
| 37 // This function should not be called by normal code. It is invoked by a |
| 38 // UsbDevice's Close function and disposes of the associated platform handle. |
| 39 void CloseDevice(scoped_refptr<UsbDevice> device); |
| 40 |
| 41 private: |
| 42 // Posts a HandleEvent task to the event handling thread. |
| 43 void PostHandleEventTask(); |
| 44 |
| 45 // Handles a single USB event. If the service is still running after the event |
| 46 // is handled, posts another HandleEvent callback to the thread. |
| 47 void HandleEvent(); |
| 48 |
| 49 // Shutdown is invoked after event handling has been suspended and is used to |
| 50 // free the platform resources associated with the service. |
| 51 void Shutdown(); |
| 52 |
| 53 bool running_; |
| 54 PlatformUsbContext context_; |
| 55 base::Thread thread_; |
| 56 |
| 57 // The devices_ map contains scoped_refptrs to all open devices, indicated by |
| 58 // their vendor and product id. This allows for reusing an open device without |
| 59 // creating another platform handle for it. |
| 60 typedef std::map<std::pair<uint16, uint16>, scoped_refptr<UsbDevice> > |
| 61 DeviceMap; |
| 62 DeviceMap devices_; |
| 63 |
| 64 DISALLOW_EVIL_CONSTRUCTORS(UsbService); |
| 65 }; |
| 66 |
| 67 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ |
OLD | NEW |