1// Copyright 2013 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
6#ifndef CHROME_BROWSER_USB_USB_DEVICE_H_
7#define CHROME_BROWSER_USB_USB_DEVICE_H_
8
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/ref_counted.h"
13#include "base/threading/thread_checker.h"
14#include "chrome/browser/usb/usb_interface.h"
15
16struct libusb_device;
17class UsbDeviceHandle;
18class UsbContext;
19
20typedef libusb_device* PlatformUsbDevice;
21
22// A UsbDevice object represents a detected USB device, providing basic
23// information about it. For further manipulation of the device, a
24// UsbDeviceHandle must be created from Open() method.
25class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> {
26 public:
27  // Accessors to basic information.
28  PlatformUsbDevice platform_device() const { return platform_device_; }
29  uint16 vendor_id() const { return vendor_id_; }
30  uint16 product_id() const { return product_id_; }
31
32  // Creates a UsbDeviceHandle for further manipulation.
33  // Blocking method. Must be called on FILE thread.
34  virtual scoped_refptr<UsbDeviceHandle> Open();
35
36  // Explicitly closes a device handle. This method will be automatically called
37  // by the destructor of a UsbDeviceHandle as well.
38  // Closing a closed handle is a safe
39  // Blocking method. Must be called on FILE thread.
40  virtual bool Close(scoped_refptr<UsbDeviceHandle> handle);
41
42  // Lists the interfaces provided by the device and fills the given
43  // UsbConfigDescriptor.
44  // Blocking method. Must be called on FILE thread.
45  virtual bool ListInterfaces(UsbConfigDescriptor* config);
46
47 protected:
48  friend class UsbService;
49  friend class base::RefCountedThreadSafe<UsbDevice>;
50
51  // Called by UsbService only;
52  UsbDevice(scoped_refptr<UsbContext> context,
53            PlatformUsbDevice platform_device,
54            uint16 vendor_id,
55            uint16 product_id);
56
57  // Constructor called in test only.
58  UsbDevice();
59  virtual ~UsbDevice();
60
61  // Called only be UsbService.
62  virtual void OnDisconnect();
63
64 private:
65  PlatformUsbDevice platform_device_;
66  uint16 vendor_id_;
67  uint16 product_id_;
68
69  // Retain the context so that it will not be released before UsbDevice.
70  scoped_refptr<UsbContext> context_;
71
72  // Opened handles.
73  typedef std::vector<scoped_refptr<UsbDeviceHandle> > HandlesVector;
74  HandlesVector handles_;
75
76  base::ThreadChecker thread_checker_;
77
78  DISALLOW_COPY_AND_ASSIGN(UsbDevice);
79};
80
81#endif  // CHROME_BROWSER_USB_USB_DEVICE_H_
82