usb_device.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #ifndef SERVICES_DEVICE_USB_USB_DEVICE_H_
  5. #define SERVICES_DEVICE_USB_USB_DEVICE_H_
  6. #include <stdint.h>
  7. #include <list>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/observer_list.h"
  14. #include "base/strings/string_util.h"
  15. #include "services/device/usb/usb_descriptors.h"
  16. #include "url/gurl.h"
  17. namespace device {
  18. class UsbDeviceHandle;
  19. // A UsbDevice object represents a detected USB device, providing basic
  20. // information about it. Methods other than simple property accessors must be
  21. // called from the thread on which this object was created. For further
  22. // manipulation of the device, a UsbDeviceHandle must be created from Open()
  23. // method.
  24. class UsbDevice : public base::RefCountedThreadSafe<UsbDevice> {
  25. public:
  26. using OpenCallback = base::OnceCallback<void(scoped_refptr<UsbDeviceHandle>)>;
  27. using ResultCallback = base::OnceCallback<void(bool success)>;
  28. // This observer interface should be used by objects that need only be
  29. // notified about the removal of a particular device as it is more efficient
  30. // than registering a large number of observers with UsbService::AddObserver.
  31. class Observer {
  32. public:
  33. virtual ~Observer();
  34. // This method is called when the UsbService that created this object
  35. // detects that the device has been disconnected from the host.
  36. virtual void OnDeviceRemoved(scoped_refptr<UsbDevice> device);
  37. };
  38. UsbDevice(const UsbDevice&) = delete;
  39. UsbDevice& operator=(const UsbDevice&) = delete;
  40. const mojom::UsbDeviceInfo& device_info() const { return *device_info_; }
  41. // A unique identifier which remains stable for the lifetime of this device
  42. // object (i.e., until the device is unplugged or the USB service dies.)
  43. const std::string& guid() const { return device_info_->guid; }
  44. // Accessors to basic information.
  45. uint32_t bus_number() const { return device_info_->bus_number; }
  46. uint32_t port_number() const { return device_info_->port_number; }
  47. uint8_t device_class() const { return device_info_->class_code; }
  48. uint8_t device_subclass() const { return device_info_->subclass_code; }
  49. uint8_t device_protocol() const { return device_info_->protocol_code; }
  50. uint16_t vendor_id() const { return device_info_->vendor_id; }
  51. uint16_t product_id() const { return device_info_->product_id; }
  52. uint16_t usb_version() const;
  53. uint16_t device_version() const;
  54. const std::u16string& manufacturer_string() const {
  55. if (device_info_->manufacturer_name)
  56. return *device_info_->manufacturer_name;
  57. return base::EmptyString16();
  58. }
  59. const std::u16string& product_string() const {
  60. if (device_info_->product_name)
  61. return *device_info_->product_name;
  62. return base::EmptyString16();
  63. }
  64. const std::u16string& serial_number() const {
  65. if (device_info_->serial_number)
  66. return *device_info_->serial_number;
  67. return base::EmptyString16();
  68. }
  69. const GURL& webusb_landing_page() const {
  70. if (device_info_->webusb_landing_page)
  71. return *device_info_->webusb_landing_page;
  72. return GURL::EmptyGURL();
  73. }
  74. const std::vector<mojom::UsbConfigurationInfoPtr>& configurations() const {
  75. return device_info_->configurations;
  76. }
  77. const mojom::UsbConfigurationInfo* GetActiveConfiguration() const;
  78. // On ChromeOS the permission_broker service must be used to open USB devices.
  79. // This function asks it to check whether a future Open call will be allowed.
  80. // On all other platforms this is a no-op and always returns true.
  81. virtual void CheckUsbAccess(ResultCallback callback);
  82. // On Android applications must request permission from the user to access a
  83. // USB device before it can be opened. After permission is granted the device
  84. // properties may contain information not previously available. On all other
  85. // platforms this is a no-op and always returns true.
  86. virtual void RequestPermission(ResultCallback callback);
  87. virtual bool permission_granted() const;
  88. // Creates a UsbDeviceHandle for further manipulation.
  89. virtual void Open(OpenCallback callback) = 0;
  90. void AddObserver(Observer* observer);
  91. void RemoveObserver(Observer* observer);
  92. protected:
  93. friend class UsbService;
  94. UsbDevice(uint32_t bus_number, uint32_t port_number);
  95. explicit UsbDevice(mojom::UsbDeviceInfoPtr device_info);
  96. UsbDevice(uint16_t usb_version,
  97. uint8_t device_class,
  98. uint8_t device_subclass,
  99. uint8_t device_protocol,
  100. uint16_t vendor_id,
  101. uint16_t product_id,
  102. uint16_t device_version,
  103. const std::u16string& manufacturer_string,
  104. const std::u16string& product_string,
  105. const std::u16string& serial_number,
  106. uint32_t bus_number,
  107. uint32_t port_number);
  108. virtual ~UsbDevice();
  109. void ActiveConfigurationChanged(int configuration_value);
  110. void NotifyDeviceRemoved();
  111. std::list<UsbDeviceHandle*>& handles() { return handles_; }
  112. // This member must be mutable by subclasses as necessary during device
  113. // enumeration. To preserve the thread safety of this object they must remain
  114. // constant afterwards.
  115. mojom::UsbDeviceInfoPtr device_info_;
  116. private:
  117. friend class base::RefCountedThreadSafe<UsbDevice>;
  118. friend class UsbDeviceHandleImpl;
  119. friend class UsbDeviceHandleMac;
  120. friend class UsbDeviceHandleUsbfs;
  121. friend class UsbDeviceHandleWin;
  122. friend class UsbServiceAndroid;
  123. friend class UsbServiceImpl;
  124. friend class UsbServiceLinux;
  125. friend class UsbServiceMac;
  126. friend class UsbServiceWin;
  127. void OnDisconnect();
  128. void HandleClosed(UsbDeviceHandle* handle);
  129. // Weak pointers to open handles. HandleClosed() will be called before each
  130. // is freed.
  131. std::list<UsbDeviceHandle*> handles_;
  132. base::ObserverList<Observer, true>::Unchecked observer_list_;
  133. };
  134. } // namespace device
  135. #endif // SERVICES_DEVICE_USB_USB_DEVICE_H_