usb_device_win.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2017 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_WIN_H_
  5. #define SERVICES_DEVICE_USB_USB_DEVICE_WIN_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/containers/flat_map.h"
  10. #include "base/sequence_checker.h"
  11. #include "services/device/usb/usb_device.h"
  12. namespace device {
  13. struct UsbDeviceDescriptor;
  14. struct WebUsbPlatformCapabilityDescriptor;
  15. class UsbDeviceWin : public UsbDevice {
  16. public:
  17. struct FunctionInfo {
  18. int interface_number;
  19. std::wstring driver;
  20. std::wstring path;
  21. };
  22. enum class DriverType {
  23. kUnsupported,
  24. kWinUSB,
  25. kComposite,
  26. };
  27. UsbDeviceWin(const std::wstring& device_path,
  28. const std::wstring& hub_path,
  29. const base::flat_map<int, FunctionInfo>& functions,
  30. uint32_t bus_number,
  31. uint32_t port_number,
  32. DriverType driver_type);
  33. UsbDeviceWin(const UsbDeviceWin&) = delete;
  34. UsbDeviceWin& operator=(const UsbDeviceWin&) = delete;
  35. // UsbDevice implementation:
  36. void Open(OpenCallback callback) override;
  37. protected:
  38. friend class UsbServiceWin;
  39. friend class UsbDeviceHandleWin;
  40. ~UsbDeviceWin() override;
  41. const std::wstring& device_path() const { return device_path_; }
  42. const base::flat_map<int, FunctionInfo>& functions() const {
  43. return functions_;
  44. }
  45. DriverType driver_type() const { return driver_type_; }
  46. // Opens the device's parent hub in order to read the device, configuration
  47. // and string descriptors.
  48. void ReadDescriptors(
  49. scoped_refptr<base::SequencedTaskRunner> blocking_task_runner,
  50. base::OnceCallback<void(bool)> callback);
  51. void UpdateFunction(int interface_number, const FunctionInfo& function_info);
  52. private:
  53. void OnReadDescriptors(base::OnceCallback<void(bool)> callback,
  54. scoped_refptr<UsbDeviceHandle> device_handle,
  55. std::unique_ptr<UsbDeviceDescriptor> descriptor);
  56. void OnReadStringDescriptors(
  57. base::OnceCallback<void(bool)> callback,
  58. scoped_refptr<UsbDeviceHandle> device_handle,
  59. uint8_t i_manufacturer,
  60. uint8_t i_product,
  61. uint8_t i_serial_number,
  62. std::unique_ptr<std::map<uint8_t, std::u16string>> string_map);
  63. void OnReadWebUsbCapabilityDescriptor(
  64. base::OnceCallback<void(bool)> callback,
  65. scoped_refptr<UsbDeviceHandle> device_handle,
  66. const absl::optional<WebUsbPlatformCapabilityDescriptor>& descriptor);
  67. void OnOpenedToReadWebUsbLandingPage(
  68. base::OnceCallback<void(bool)> callback,
  69. uint8_t vendor_code,
  70. uint8_t landing_page_id,
  71. scoped_refptr<UsbDeviceHandle> device_handle);
  72. void OnReadWebUsbLandingPage(base::OnceCallback<void(bool)> callback,
  73. scoped_refptr<UsbDeviceHandle> device_handle,
  74. const GURL& landing_page);
  75. private:
  76. SEQUENCE_CHECKER(sequence_checker_);
  77. const std::wstring device_path_;
  78. const std::wstring hub_path_;
  79. base::flat_map<int, FunctionInfo> functions_;
  80. const DriverType driver_type_;
  81. };
  82. } // namespace device
  83. #endif // SERVICES_DEVICE_USB_USB_DEVICE_WIN_H_