hid.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 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_HID_HID_H_
  5. #define THIRD_PARTY_BLINK_RENDERER_MODULES_HID_HID_H_
  6. #include "mojo/public/cpp/bindings/associated_receiver.h"
  7. #include "services/device/public/mojom/hid.mojom-blink-forward.h"
  8. #include "third_party/blink/public/mojom/hid/hid.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_lifecycle_observer.h"
  12. #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
  13. #include "third_party/blink/renderer/platform/heap/handle.h"
  14. #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
  15. #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
  16. #include "third_party/blink/renderer/platform/scheduler/public/frame_or_worker_scheduler.h"
  17. namespace blink {
  18. class ExecutionContext;
  19. class HIDDevice;
  20. class HIDDeviceRequestOptions;
  21. class ScriptPromiseResolver;
  22. class ScriptState;
  23. class HID : public EventTargetWithInlineData,
  24. public ExecutionContextClient,
  25. public device::mojom::blink::HidManagerClient {
  26. DEFINE_WRAPPERTYPEINFO();
  27. public:
  28. explicit HID(ExecutionContext& context);
  29. ~HID() override;
  30. // EventTarget:
  31. ExecutionContext* GetExecutionContext() const override;
  32. const AtomicString& InterfaceName() const override;
  33. // device::mojom::HidManagerClient:
  34. void DeviceAdded(device::mojom::blink::HidDeviceInfoPtr device_info) override;
  35. void DeviceRemoved(
  36. device::mojom::blink::HidDeviceInfoPtr device_info) override;
  37. // Web-exposed interfaces:
  38. DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect)
  39. DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect)
  40. ScriptPromise getDevices(ScriptState*, ExceptionState&);
  41. ScriptPromise requestDevice(ScriptState*,
  42. const HIDDeviceRequestOptions*,
  43. ExceptionState&);
  44. void Connect(const String& device_guid,
  45. mojo::PendingRemote<device::mojom::blink::HidConnectionClient>
  46. connection_client,
  47. device::mojom::blink::HidManager::ConnectCallback callback);
  48. void Trace(Visitor*) const override;
  49. protected:
  50. // EventTarget:
  51. void AddedEventListener(const AtomicString& event_type,
  52. RegisteredEventListener&) override;
  53. private:
  54. // Returns the HIDDevice matching |info| from |device_cache_|. If the device
  55. // is not in the cache, a new device is created and added to the cache.
  56. HIDDevice* GetOrCreateDevice(device::mojom::blink::HidDeviceInfoPtr info);
  57. // Opens a connection to HidService, or does nothing if the connection is
  58. // already open.
  59. void EnsureServiceConnection();
  60. void OnServiceConnectionError();
  61. void FinishGetDevices(ScriptPromiseResolver*,
  62. Vector<device::mojom::blink::HidDeviceInfoPtr>);
  63. void FinishRequestDevice(ScriptPromiseResolver*,
  64. Vector<device::mojom::blink::HidDeviceInfoPtr>);
  65. HeapMojoRemote<mojom::blink::HidService,
  66. HeapMojoWrapperMode::kWithoutContextObserver>
  67. service_;
  68. mojo::AssociatedReceiver<device::mojom::blink::HidManagerClient> receiver_{
  69. this};
  70. HeapHashSet<Member<ScriptPromiseResolver>> get_devices_promises_;
  71. HeapHashSet<Member<ScriptPromiseResolver>> request_device_promises_;
  72. HeapHashMap<String, WeakMember<HIDDevice>> device_cache_;
  73. FrameOrWorkerScheduler::SchedulingAffectingFeatureHandle
  74. feature_handle_for_scheduler_;
  75. };
  76. } // namespace blink
  77. #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_HID_HID_H_