Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / usb / usb_service.h
blob55e797211eb39f280db3ae9a388d8578908fcb1a
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.
5 #ifndef CHROME_BROWSER_USB_USB_SERVICE_H_
6 #define CHROME_BROWSER_USB_USB_SERVICE_H_
8 #include <map>
9 #include <utility>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
16 namespace base {
18 template <class T> class DeleteHelper;
20 } // namespace base
22 struct InitUsbContextTraits;
23 template <typename T> struct DefaultSingletonTraits;
25 typedef struct libusb_device* PlatformUsbDevice;
26 typedef struct libusb_context* PlatformUsbContext;
28 class UsbContext;
29 class UsbDevice;
31 // The USB service handles creating and managing an event handler thread that is
32 // used to manage and dispatch USB events. It is also responsible for device
33 // discovery on the system, which allows it to re-use device handles to prevent
34 // competition for the same USB device.
35 class UsbService {
36 public:
37 typedef scoped_ptr<std::vector<scoped_refptr<UsbDevice> > >
38 ScopedDeviceVector;
40 // Must be called on FILE thread.
41 // Returns NULL when failed to initialized.
42 static UsbService* GetInstance();
44 scoped_refptr<UsbDevice> GetDeviceById(uint32 unique_id);
46 // Get all of the devices attached to the system, inserting them into
47 // |devices|. Clears |devices| before use. The result will be sorted by id
48 // in increasing order. Must be called on FILE thread.
49 void GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices);
51 private:
52 friend class base::DeleteHelper<UsbService>;
54 explicit UsbService(PlatformUsbContext context);
55 virtual ~UsbService();
57 // Return true if |device|'s vendor and product identifiers match |vendor_id|
58 // and |product_id|.
59 static bool DeviceMatches(scoped_refptr<UsbDevice> device,
60 const uint16 vendor_id,
61 const uint16 product_id);
63 // Enumerate USB devices from OS and Update devices_ map.
64 void RefreshDevices();
66 scoped_refptr<UsbContext> context_;
68 // TODO(ikarienator): Figure out a better solution.
69 uint32 next_unique_id_;
71 // The map from PlatformUsbDevices to UsbDevices.
72 typedef std::map<PlatformUsbDevice, scoped_refptr<UsbDevice> > DeviceMap;
73 DeviceMap devices_;
75 DISALLOW_COPY_AND_ASSIGN(UsbService);
78 #endif // CHROME_BROWSER_USB_USB_SERVICE_H_