Chromium Code Reviews| 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.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 |
| 19 class UsbContext; | |
| 20 class UsbDeviceStub; | |
| 18 class UsbEventHandler; | 21 class UsbEventHandler; |
| 22 class RefCountedPlatformUsbDevice; | |
| 23 | |
| 19 typedef libusb_context* PlatformUsbContext; | 24 typedef libusb_context* PlatformUsbContext; |
| 20 | 25 |
| 21 // The USB service handles creating and managing an event handler thread that is | 26 // 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 | 27 // used to manage and dispatch USB events. It is also responsible for device |
| 23 // discovery on the system, which allows it to re-use device handles to prevent | 28 // discovery on the system. |
| 24 // competition for the same USB device. | |
| 25 class UsbService : public BrowserContextKeyedService { | 29 class UsbService : public BrowserContextKeyedService { |
| 26 public: | 30 public: |
| 27 UsbService(); | 31 UsbService(); |
| 28 virtual ~UsbService(); | 32 virtual ~UsbService(); |
| 29 | 33 |
| 30 // Cleanup must be invoked before the service is destroyed. It interrupts the | 34 // The following methods must be called on FILE thread: |
|
pfeldman
2013/07/22 08:39:45
UsbService is created and deleted on UI thread. Yo
Bei Zhang
2013/07/23 17:58:44
This is the summary of our discussion about this:
| |
| 31 // event handling thread and disposes of open devices. | 35 // |
| 32 void Cleanup(); | |
| 33 | |
| 34 // Find all of the devices attached to the system that are identified by | 36 // Find all of the devices attached to the system that are identified by |
| 35 // |vendor_id| and |product_id|, inserting them into |devices|. Clears | 37 // |vendor_id| and |product_id|, inserting them into |devices|. Clears |
| 36 // |devices| before use. Calls |callback| once |devices| is populated. | 38 // |devices| before use. Calls |callback| once |devices| is populated. |
| 37 void FindDevices(const uint16 vendor_id, | 39 void FindDevices(const uint16 vendor_id, |
| 38 const uint16 product_id, | 40 const uint16 product_id, |
| 39 int interface_id, | 41 int interface_id, |
| 40 std::vector<scoped_refptr<UsbDevice> >* devices, | 42 std::vector<int>* devices, |
| 41 const base::Callback<void()>& callback); | 43 const base::Callback<void()>& callback); |
| 42 | 44 |
| 43 // Find all of the devices attached to the system, inserting them into | 45 // Get all of the devices attached to the system, inserting them into |
| 44 // |devices|. Clears |devices| before use. | 46 // |devices|. Clears |devices| before use. |
| 45 void EnumerateDevices(std::vector<scoped_refptr<UsbDevice> >* devices); | 47 void GetDevices(std::vector<int>* devices); |
|
pfeldman
2013/07/22 08:39:45
I don't think this integer handle makes sense. The
Bei Zhang
2013/07/23 17:58:44
Yes, that will be exactly what the step 3 does.
O
| |
| 48 | |
| 49 // Open a device for further communication. | |
| 50 scoped_refptr<UsbDevice> OpenDevice(int device); | |
| 46 | 51 |
| 47 // This function should not be called by normal code. It is invoked by a | 52 // This function should not be called by normal code. It is invoked by a |
| 48 // UsbDevice's Close function and disposes of the associated platform handle. | 53 // UsbDevice's Close function and disposes of the associated platform handle. |
| 49 void CloseDevice(scoped_refptr<UsbDevice> device); | 54 void CloseDevice(scoped_refptr<UsbDevice> device); |
| 50 | 55 |
| 56 // Schedule an update to USB device info. | |
| 57 void ScheduleRefreshDevices(); | |
| 58 | |
| 51 private: | 59 private: |
| 52 // RefCountedPlatformUsbDevice takes care of managing the underlying reference | 60 // BrowserContextKeyedService: |
| 53 // count of a single PlatformUsbDevice. This allows us to construct things | 61 virtual void Shutdown() OVERRIDE; |
| 54 // like vectors of RefCountedPlatformUsbDevices and not worry about having to | |
| 55 // explicitly unreference them after use. | |
| 56 class RefCountedPlatformUsbDevice { | |
| 57 public: | |
| 58 explicit RefCountedPlatformUsbDevice(PlatformUsbDevice device); | |
| 59 RefCountedPlatformUsbDevice(const RefCountedPlatformUsbDevice& other); | |
| 60 virtual ~RefCountedPlatformUsbDevice(); | |
| 61 PlatformUsbDevice device(); | |
| 62 | |
| 63 private: | |
| 64 PlatformUsbDevice device_; | |
| 65 }; | |
| 66 | |
| 67 typedef std::vector<RefCountedPlatformUsbDevice> DeviceVector; | |
| 68 | 62 |
| 69 // Return true if |device|'s vendor and product identifiers match |vendor_id| | 63 // Return true if |device|'s vendor and product identifiers match |vendor_id| |
| 70 // and |product_id|. | 64 // and |product_id|. |
| 71 static bool DeviceMatches(PlatformUsbDevice device, | 65 static bool DeviceMatches(const UsbDeviceStub* device, |
| 72 const uint16 vendor_id, | 66 const uint16 vendor_id, |
| 73 const uint16 product_id); | 67 const uint16 product_id); |
| 74 | 68 |
| 75 // FindDevicesImpl is called by FindDevices on ChromeOS after the permission | 69 // FindDevicesImpl is called by FindDevices on ChromeOS after the permission |
| 76 // broker has signalled that permission has been granted to access the | 70 // broker has signalled that permission has been granted to access the |
| 77 // underlying device nodes. On other platforms, it is called directly by | 71 // underlying device nodes. On other platforms, it is called directly by |
| 78 // FindDevices. | 72 // FindDevices. |
| 79 void FindDevicesImpl(const uint16 vendor_id, | 73 void FindDevicesImpl(const uint16 vendor_id, |
| 80 const uint16 product_id, | 74 const uint16 product_id, |
| 81 std::vector<scoped_refptr<UsbDevice> >* devices, | 75 std::vector<int>* devices, |
| 82 const base::Callback<void()>& callback, | 76 const base::Callback<void()>& callback, |
| 83 bool success); | 77 bool success); |
| 84 | 78 |
| 85 // Populates |output| with the result of enumerating all attached USB devices. | 79 // Enumerate USB devices from OS and Update devices_ map. |
| 86 void EnumerateDevicesImpl(DeviceVector* output); | 80 void RefreshDevices(); |
| 87 | 81 |
| 88 // If a UsbDevice wrapper corresponding to |device| has already been created, | 82 scoped_refptr<UsbContext> context_; |
| 89 // returns it. Otherwise, opens the device, creates a wrapper, and associates | |
| 90 // the wrapper with the device internally. | |
| 91 UsbDevice* LookupOrCreateDevice(PlatformUsbDevice device); | |
| 92 | 83 |
| 93 PlatformUsbContext context_; | 84 // The next id of device. Can only be accessed from FILE thread. |
| 94 UsbEventHandler* event_handler_; | 85 int next_unique_id_; |
| 95 | 86 |
| 96 // The devices_ map contains scoped_refptrs to all open devices, indicated by | 87 bool device_enumeration_scheduled_; |
| 97 // their vendor and product id. This allows for reusing an open device without | 88 |
| 98 // creating another platform handle for it. | 89 // The devices_ map contains all connected devices. |
| 99 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap; | 90 // They are not to be used directly outside UsbService. Instead, FindDevice |
| 91 // methods returns their id for accessing them. | |
| 92 typedef std::map<PlatformUsbDevice, UsbDeviceStub*> DeviceMap; | |
| 93 typedef std::map<int, UsbDeviceStub*> DeviceMapById; | |
| 100 DeviceMap devices_; | 94 DeviceMap devices_; |
| 95 DeviceMapById devices_by_id_; | |
| 101 | 96 |
| 102 DISALLOW_COPY_AND_ASSIGN(UsbService); | 97 DISALLOW_COPY_AND_ASSIGN(UsbService); |
| 103 }; | 98 }; |
| 104 | 99 |
| 105 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ | 100 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_ |
| OLD | NEW |