1// Copyright 2014 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.
4
5#ifndef DEVICE_USB_USB_DEVICE_IMPL_H_
6#define DEVICE_USB_USB_DEVICE_IMPL_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/callback.h"
12#include "base/threading/thread_checker.h"
13#include "device/usb/usb_descriptors.h"
14#include "device/usb/usb_device.h"
15
16struct libusb_device;
17struct libusb_config_descriptor;
18
19namespace base {
20class SingleThreadTaskRunner;
21}
22
23namespace device {
24
25class UsbDeviceHandleImpl;
26class UsbContext;
27
28typedef libusb_device* PlatformUsbDevice;
29typedef libusb_config_descriptor* PlatformUsbConfigDescriptor;
30
31class UsbDeviceImpl : public UsbDevice {
32 public:
33// UsbDevice implementation:
34#if defined(OS_CHROMEOS)
35  virtual void RequestUsbAccess(
36      int interface_id,
37      const base::Callback<void(bool success)>& callback) OVERRIDE;
38#endif  // OS_CHROMEOS
39  virtual scoped_refptr<UsbDeviceHandle> Open() OVERRIDE;
40  virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) OVERRIDE;
41  virtual const UsbConfigDescriptor& GetConfiguration() OVERRIDE;
42  virtual bool GetManufacturer(base::string16* manufacturer) OVERRIDE;
43  virtual bool GetProduct(base::string16* product) OVERRIDE;
44  virtual bool GetSerialNumber(base::string16* serial_number) OVERRIDE;
45
46 protected:
47  friend class UsbServiceImpl;
48
49  // Called by UsbServiceImpl only;
50  UsbDeviceImpl(scoped_refptr<UsbContext> context,
51                scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
52                PlatformUsbDevice platform_device,
53                uint16 vendor_id,
54                uint16 product_id,
55                uint32 unique_id);
56
57  virtual ~UsbDeviceImpl();
58
59  // Called only be UsbService.
60  void OnDisconnect();
61
62 private:
63  base::ThreadChecker thread_checker_;
64  PlatformUsbDevice platform_device_;
65
66#if defined(USE_UDEV)
67  // On Linux these properties are read from sysfs when the device is enumerated
68  // to avoid hitting the permission broker on Chrome OS for a real string
69  // descriptor request.
70  std::string manufacturer_;
71  std::string product_;
72  std::string serial_number_;
73#endif
74
75  // The active configuration descriptor is not read immediately but cached for
76  // later use.
77  bool current_configuration_cached_;
78  UsbConfigDescriptor current_configuration_;
79
80  // Retain the context so that it will not be released before UsbDevice.
81  scoped_refptr<UsbContext> context_;
82
83  // Opened handles.
84  typedef std::vector<scoped_refptr<UsbDeviceHandleImpl> > HandlesVector;
85  HandlesVector handles_;
86
87  // Reference to the UI thread for permission-broker calls.
88  scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
89
90  DISALLOW_COPY_AND_ASSIGN(UsbDeviceImpl);
91};
92
93}  // namespace device
94
95#endif  // DEVICE_USB_USB_DEVICE_IMPL_H_
96