Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: chrome/browser/usb/usb_service.h

Issue 18593002: Usb backend refactor step 2: Separate Usb Device Handle and Usb Device (deprecate) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Separate Usb Device Handle and Usb Device Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 responsbile for device
23 // discovery on the system, which allows it to re-use device handles to prevent 28 // discovery on the system, which allows it to re-use device handles to prevent
24 // competition for the same USB device. 29 // competition for the same USB device.
25 class UsbService : public BrowserContextKeyedService { 30 class UsbService : public BrowserContextKeyedService,
31 public base::NonThreadSafe {
26 public: 32 public:
27 UsbService(); 33 UsbService();
28 virtual ~UsbService(); 34 virtual ~UsbService();
29 35
30 // Cleanup must be invoked before the service is destroyed. It interrupts the 36 // BrowserContextKeyedService:
31 // event handling thread and disposes of open devices. 37 virtual void Shutdown() OVERRIDE;
32 void Cleanup();
33 38
34 // Find all of the devices attached to the system that are identified by 39 // Find all of the devices attached to the system that are identified by
35 // |vendor_id| and |product_id|, inserting them into |devices|. Clears 40 // |vendor_id| and |product_id|, inserting them into |devices|. Clears
36 // |devices| before use. Calls |callback| once |devices| is populated. 41 // |devices| before use. Calls |callback| once |devices| is populated.
37 void FindDevices(const uint16 vendor_id, 42 void FindDevices(const uint16 vendor_id,
38 const uint16 product_id, 43 const uint16 product_id,
39 int interface_id, 44 int interface_id,
40 std::vector<scoped_refptr<UsbDevice> >* devices, 45 std::vector<int>* devices,
41 const base::Callback<void()>& callback); 46 const base::Callback<void()>& callback);
42 47
43 // Find all of the devices attached to the system, inserting them into 48 // Get all of the devices attached to the system, inserting them into
44 // |devices|. Clears |devices| before use. 49 // |devices|. Clears |devices| before use.
45 void EnumerateDevices(std::vector<scoped_refptr<UsbDevice> >* devices); 50 void GetDevices(std::vector<int>* devices);
51
52 // Open a device for further communication.
53 scoped_refptr<UsbDevice> OpenDevice(int device);
pfeldman 2013/07/21 08:48:23 So I call this _only_ on UI thread and all subsequ
Bei Zhang 2013/07/21 21:31:52 These UsbService is not thread safe. Methods can o
46 54
47 // This function should not be called by normal code. It is invoked by a 55 // 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. 56 // UsbDevice's Close function and disposes of the associated platform handle.
49 void CloseDevice(scoped_refptr<UsbDevice> device); 57 void CloseDevice(scoped_refptr<UsbDevice> device);
50 58
59 // Schedule an update to USB device info.
60 void ScheduleRefreshDevices();
61
51 private: 62 private:
52 // RefCountedPlatformUsbDevice takes care of managing the underlying reference
53 // count of a single PlatformUsbDevice. This allows us to construct things
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
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698