serial.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2018 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_SERIAL_SERIAL_H_
  5. #define THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_SERIAL_H_
  6. #include "third_party/blink/public/mojom/serial/serial.mojom-blink.h"
  7. #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
  8. #include "third_party/blink/renderer/core/dom/events/event_target.h"
  9. #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
  10. #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
  11. #include "third_party/blink/renderer/platform/heap/garbage_collected.h"
  12. #include "third_party/blink/renderer/platform/heap/handle.h"
  13. #include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.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/wtf/functional.h"
  17. namespace blink {
  18. class ExecutionContext;
  19. class ScriptPromiseResolver;
  20. class ScriptState;
  21. class SerialPort;
  22. class SerialPortRequestOptions;
  23. class Serial final : public EventTargetWithInlineData,
  24. public ExecutionContextLifecycleObserver,
  25. public mojom::blink::SerialServiceClient {
  26. DEFINE_WRAPPERTYPEINFO();
  27. public:
  28. explicit Serial(ExecutionContext&);
  29. // EventTarget
  30. ExecutionContext* GetExecutionContext() const override;
  31. const AtomicString& InterfaceName() const override;
  32. // ExecutionContextLifecycleObserver
  33. void ContextDestroyed() override;
  34. // SerialServiceClient
  35. void OnPortAdded(mojom::blink::SerialPortInfoPtr port_info) override;
  36. void OnPortRemoved(mojom::blink::SerialPortInfoPtr port_info) override;
  37. // Web-exposed interfaces
  38. DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect)
  39. DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect)
  40. ScriptPromise getPorts(ScriptState*, ExceptionState&);
  41. ScriptPromise requestPort(ScriptState*,
  42. const SerialPortRequestOptions*,
  43. ExceptionState&);
  44. void GetPort(
  45. const base::UnguessableToken& token,
  46. mojo::PendingReceiver<device::mojom::blink::SerialPort> receiver);
  47. void Trace(Visitor*) const override;
  48. protected:
  49. // EventTarget
  50. void AddedEventListener(const AtomicString& event_type,
  51. RegisteredEventListener&) override;
  52. private:
  53. void EnsureServiceConnection();
  54. void OnServiceConnectionError();
  55. SerialPort* GetOrCreatePort(mojom::blink::SerialPortInfoPtr);
  56. void OnGetPorts(ScriptPromiseResolver*,
  57. Vector<mojom::blink::SerialPortInfoPtr>);
  58. void OnRequestPort(ScriptPromiseResolver*, mojom::blink::SerialPortInfoPtr);
  59. HeapMojoRemote<mojom::blink::SerialService,
  60. HeapMojoWrapperMode::kWithoutContextObserver>
  61. service_;
  62. HeapMojoReceiver<mojom::blink::SerialServiceClient,
  63. Serial,
  64. HeapMojoWrapperMode::kWithoutContextObserver>
  65. receiver_;
  66. HeapHashSet<Member<ScriptPromiseResolver>> get_ports_promises_;
  67. HeapHashSet<Member<ScriptPromiseResolver>> request_port_promises_;
  68. HeapHashMap<String, WeakMember<SerialPort>> port_cache_;
  69. };
  70. } // namespace blink
  71. #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SERIAL_SERIAL_H_