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