Add a service to track devices selected by the user.
[chromium-blink-merge.git] / device / usb / usb_device.h
blobf4bd5915467086be66671c7751e6347770d141f8
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.
5 #ifndef DEVICE_USB_USB_DEVICE_H_
6 #define DEVICE_USB_USB_DEVICE_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/observer_list.h"
13 namespace device {
15 class UsbDeviceHandle;
16 struct UsbConfigDescriptor;
18 // A UsbDevice object represents a detected USB device, providing basic
19 // information about it. For further manipulation of the device, a
20 // UsbDeviceHandle must be created from Open() method.
21 class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> {
22 public:
23 class Observer {
24 public:
25 virtual void OnDisconnect(scoped_refptr<UsbDevice> device) = 0;
28 // Accessors to basic information.
29 uint16 vendor_id() const { return vendor_id_; }
30 uint16 product_id() const { return product_id_; }
31 uint32 unique_id() const { return unique_id_; }
33 #if defined(OS_CHROMEOS)
34 // On ChromeOS, if an interface of a claimed device is not claimed, the
35 // permission broker can change the owner of the device so that the unclaimed
36 // interfaces can be used. If this argument is missing, permission broker will
37 // not be used and this method fails if the device is claimed.
38 virtual void RequestUsbAccess(
39 int interface_id,
40 const base::Callback<void(bool success)>& callback) = 0;
41 #endif // OS_CHROMEOS
43 // Creates a UsbDeviceHandle for further manipulation.
44 // Blocking method. Must be called on FILE thread.
45 virtual scoped_refptr<UsbDeviceHandle> Open() = 0;
47 // Explicitly closes a device handle. This method will be automatically called
48 // by the destructor of a UsbDeviceHandle as well.
49 // Closing a closed handle is a safe
50 // Blocking method. Must be called on FILE thread.
51 virtual bool Close(scoped_refptr<UsbDeviceHandle> handle) = 0;
53 // Gets the UsbConfigDescriptor for the active device configuration.
54 // Blocking method. Must be called on FILE thread.
55 virtual const UsbConfigDescriptor& GetConfiguration() = 0;
57 void AddObserver(Observer* obs) { observer_list_.AddObserver(obs); }
58 void RemoveObserver(Observer* obs) { observer_list_.RemoveObserver(obs); }
60 protected:
61 UsbDevice(uint16 vendor_id, uint16 product_id, uint32 unique_id);
62 virtual ~UsbDevice();
64 void NotifyDisconnect();
66 private:
67 friend class base::RefCountedThreadSafe<UsbDevice>;
69 const uint16 vendor_id_;
70 const uint16 product_id_;
71 const uint32 unique_id_;
73 ObserverList<Observer> observer_list_;
75 DISALLOW_COPY_AND_ASSIGN(UsbDevice);
78 } // namespace device
80 #endif // DEVICE_USB_USB_DEVICE_H_