usb.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 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 THIRD_PARTY_BLINK_RENDERER_MODULES_WEBUSB_USB_H_
  5. #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBUSB_USB_H_
  6. #include "services/device/public/mojom/usb_manager.mojom-blink-forward.h"
  7. #include "services/device/public/mojom/usb_manager_client.mojom-blink.h"
  8. #include "third_party/blink/public/mojom/usb/web_usb_service.mojom-blink.h"
  9. #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
  10. #include "third_party/blink/renderer/core/dom/events/event_target.h"
  11. #include "third_party/blink/renderer/core/execution_context/execution_context.h"
  12. #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
  13. #include "third_party/blink/renderer/core/execution_context/security_context.h"
  14. #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
  15. #include "third_party/blink/renderer/platform/heap/handle.h"
  16. #include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver.h"
  17. #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
  18. #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
  19. namespace blink {
  20. class ExceptionState;
  21. class ScriptPromiseResolver;
  22. class ScriptState;
  23. class USBDevice;
  24. class USBDeviceRequestOptions;
  25. class USB final : public EventTargetWithInlineData,
  26. public ExecutionContextLifecycleObserver,
  27. public device::mojom::blink::UsbDeviceManagerClient {
  28. DEFINE_WRAPPERTYPEINFO();
  29. public:
  30. explicit USB(ExecutionContext&);
  31. ~USB() override;
  32. // USB.idl
  33. ScriptPromise getDevices(ScriptState*, ExceptionState&);
  34. ScriptPromise requestDevice(ScriptState*,
  35. const USBDeviceRequestOptions*,
  36. ExceptionState&);
  37. DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect)
  38. DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect)
  39. // EventTarget overrides.
  40. ExecutionContext* GetExecutionContext() const override;
  41. const AtomicString& InterfaceName() const override;
  42. // ExecutionContextLifecycleObserver overrides.
  43. void ContextDestroyed() override;
  44. USBDevice* GetOrCreateDevice(device::mojom::blink::UsbDeviceInfoPtr);
  45. mojom::blink::WebUsbService* GetWebUsbService() const {
  46. return service_.get();
  47. }
  48. void OnGetDevices(ScriptPromiseResolver*,
  49. Vector<device::mojom::blink::UsbDeviceInfoPtr>);
  50. void OnGetPermission(ScriptPromiseResolver*,
  51. device::mojom::blink::UsbDeviceInfoPtr);
  52. // DeviceManagerClient implementation.
  53. void OnDeviceAdded(device::mojom::blink::UsbDeviceInfoPtr) override;
  54. void OnDeviceRemoved(device::mojom::blink::UsbDeviceInfoPtr) override;
  55. void OnServiceConnectionError();
  56. void Trace(Visitor*) const override;
  57. protected:
  58. // EventTarget protected overrides.
  59. void AddedEventListener(const AtomicString& event_type,
  60. RegisteredEventListener&) override;
  61. private:
  62. void EnsureServiceConnection();
  63. bool IsContextSupported() const;
  64. bool IsFeatureEnabled(ReportOptions) const;
  65. HeapMojoRemote<mojom::blink::WebUsbService> service_;
  66. HeapHashSet<Member<ScriptPromiseResolver>> get_devices_requests_;
  67. HeapHashSet<Member<ScriptPromiseResolver>> get_permission_requests_;
  68. HeapMojoAssociatedReceiver<device::mojom::blink::UsbDeviceManagerClient,
  69. USB,
  70. HeapMojoWrapperMode::kWithoutContextObserver>
  71. client_receiver_;
  72. HeapHashMap<String, WeakMember<USBDevice>> device_cache_;
  73. };
  74. } // namespace blink
  75. #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBUSB_USB_H_