| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_USB_USB_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_USB_USB_SERVICE_H_ |
| 6 #define CHROME_BROWSER_USB_USB_SERVICE_H_ | 6 #define CHROME_BROWSER_USB_USB_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/threading/non_thread_safe.h" |
| 13 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
| 14 #include "chrome/browser/usb/usb_device.h" | 15 #include "chrome/browser/usb/usb_device_handle.h" |
| 15 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 16 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 16 #include "third_party/libusb/src/libusb/libusb.h" | 17 #include "third_party/libusb/src/libusb/libusb.h" |
| 17 | 18 |
| 18 class UsbEventHandler; | 19 class UsbDevice; |
| 19 typedef libusb_context* PlatformUsbContext; | 20 class UsbContext; |
| 21 |
| 22 typedef struct libusb_context* PlatformUsbContext; |
| 23 typedef struct libusb_device* PlatformUsbDevice; |
| 20 | 24 |
| 21 // The USB service handles creating and managing an event handler thread that is | 25 // The USB service handles creating and managing an event handler thread that is |
| 22 // used to manage and dispatch USB events. It is also responsbile for device | 26 // used to manage and dispatch USB events. It is also responsbile for device |
| 23 // discovery on the system, which allows it to re-use device handles to prevent | 27 // discovery on the system, which allows it to re-use device handles to prevent |
| 24 // competition for the same USB device. | 28 // competition for the same USB device. |
| 25 class UsbService : public BrowserContextKeyedService { | 29 // |
| 30 // This class should commonly be used on FILE thread. |
| 31 class UsbService : public BrowserContextKeyedService, |
| 32 public base::NonThreadSafe { |
| 26 public: | 33 public: |
| 27 UsbService(); | 34 UsbService(); |
| 28 virtual ~UsbService(); | 35 virtual ~UsbService(); |
| 29 | 36 |
| 30 // Cleanup must be invoked before the service is destroyed. It interrupts the | 37 virtual void Shutdown() OVERRIDE; |
| 31 // event handling thread and disposes of open devices. | |
| 32 void Cleanup(); | |
| 33 | |
| 34 // Find all of the devices attached to the system that are identified by | 38 // Find all of the devices attached to the system that are identified by |
| 35 // |vendor_id| and |product_id|, inserting them into |devices|. Clears | 39 // |vendor_id| and |product_id|, inserting them into |devices|. Clears |
| 36 // |devices| before use. Calls |callback| once |devices| is populated. | 40 // |devices| before use. Calls |callback| once |devices| is populated. |
| 37 void FindDevices(const uint16 vendor_id, | 41 void FindDevices(const uint16 vendor_id, const uint16 product_id, |
| 38 const uint16 product_id, | 42 const int interface_id, std::vector<int>* devices, |
| 39 int interface_id, | |
| 40 std::vector<scoped_refptr<UsbDevice> >* devices, | |
| 41 const base::Callback<void()>& callback); | 43 const base::Callback<void()>& callback); |
| 42 | 44 |
| 45 // Open a device for further communication. |
| 46 scoped_refptr<UsbDeviceHandle> OpenDevice(int device); |
| 47 |
| 43 // This function should not be called by normal code. It is invoked by a | 48 // This function should not be called by normal code. It is invoked by a |
| 44 // UsbDevice's Close function and disposes of the associated platform handle. | 49 // UsbDevice's Close function and disposes of the associated platform handle. |
| 45 void CloseDevice(scoped_refptr<UsbDevice> device); | 50 void CloseDeviceHandle(scoped_refptr<UsbDeviceHandle> device); |
| 51 |
| 52 // Schedule an update to USB device info. |
| 53 // This method can be called on any thread. |
| 54 void ScheduleEnumerateDevice(); |
| 46 | 55 |
| 47 private: | 56 private: |
| 48 // RefCountedPlatformUsbDevice takes care of managing the underlying reference | |
| 49 // count of a single PlatformUsbDevice. This allows us to construct things | |
| 50 // like vectors of RefCountedPlatformUsbDevices and not worry about having to | |
| 51 // explicitly unreference them after use. | |
| 52 class RefCountedPlatformUsbDevice { | |
| 53 public: | |
| 54 explicit RefCountedPlatformUsbDevice(PlatformUsbDevice device); | |
| 55 RefCountedPlatformUsbDevice(const RefCountedPlatformUsbDevice& other); | |
| 56 virtual ~RefCountedPlatformUsbDevice(); | |
| 57 PlatformUsbDevice device(); | |
| 58 | |
| 59 private: | |
| 60 PlatformUsbDevice device_; | |
| 61 }; | |
| 62 | |
| 63 typedef std::vector<RefCountedPlatformUsbDevice> DeviceVector; | |
| 64 | |
| 65 // Return true if |device|'s vendor and product identifiers match |vendor_id| | 57 // Return true if |device|'s vendor and product identifiers match |vendor_id| |
| 66 // and |product_id|. | 58 // and |product_id|. |
| 67 static bool DeviceMatches(PlatformUsbDevice device, | 59 static bool DeviceMatches(const UsbDevice* device, const uint16 vendor_id, |
| 68 const uint16 vendor_id, | |
| 69 const uint16 product_id); | 60 const uint16 product_id); |
| 70 | 61 |
| 71 // FindDevicesImpl is called by FindDevices on ChromeOS after the permission | 62 // FindDevicesImpl is called by FindDevices on ChromeOS after the permission |
| 72 // broker has signalled that permission has been granted to access the | 63 // broker has signaled that permission has been granted to access the |
| 73 // underlying device nodes. On other platforms, it is called directly by | 64 // underlying device nodes. On other platforms, it is called directly by |
| 74 // FindDevices. | 65 // FindDevices. |
| 75 void FindDevicesImpl(const uint16 vendor_id, | 66 void FindDevicesImpl(const uint16 vendor_id, const uint16 product_id, |
| 76 const uint16 product_id, | 67 std::vector<int>* devices, |
| 77 std::vector<scoped_refptr<UsbDevice> >* devices, | 68 const base::Callback<void()>& callback, bool success); |
| 78 const base::Callback<void()>& callback, | |
| 79 bool success); | |
| 80 | 69 |
| 81 // Populates |output| with the result of enumerating all attached USB devices. | 70 // Enumerate USB devices from OS and Update devices_ map. |
| 82 void EnumerateDevices(DeviceVector* output); | 71 void EnumerateDevices(); |
| 83 | 72 |
| 84 // If a UsbDevice wrapper corresponding to |device| has already been created, | 73 scoped_refptr<UsbContext> context_; |
| 85 // returns it. Otherwise, opens the device, creates a wrapper, and associates | 74 int next_unique_id_; |
| 86 // the wrapper with the device internally. | |
| 87 UsbDevice* LookupOrCreateDevice(PlatformUsbDevice device); | |
| 88 | 75 |
| 89 PlatformUsbContext context_; | 76 bool device_enumeration_scheduled_; |
| 90 UsbEventHandler* event_handler_; | |
| 91 | 77 |
| 92 // The devices_ map contains scoped_refptrs to all open devices, indicated by | 78 // The devices_ map contains all connected devices. |
| 93 // their vendor and product id. This allows for reusing an open device without | 79 // They are not to be used directly outside UsbService. Instead, FindDevice |
| 94 // creating another platform handle for it. | 80 // methods returns their id for accessing them. |
| 95 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap; | 81 typedef std::map<PlatformUsbDevice, UsbDevice*> DeviceMap; |
| 96 DeviceMap devices_; | 82 DeviceMap devices_; |
| 97 | 83 |
| 98 DISALLOW_COPY_AND_ASSIGN(UsbService); | 84 DISALLOW_COPY_AND_ASSIGN(UsbService); |
| 99 }; | 85 }; |
| 100 | 86 |
| 101 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ | 87 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ |
| OLD | NEW |