MessagePort.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <AK/Weakable.h>
  9. #include <LibCore/Socket.h>
  10. #include <LibWeb/Bindings/Transferable.h>
  11. #include <LibWeb/DOM/EventTarget.h>
  12. #include <LibWeb/Forward.h>
  13. namespace Web::HTML {
  14. #define ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(E) \
  15. E(onmessage, HTML::EventNames::message) \
  16. E(onmessageerror, HTML::EventNames::messageerror)
  17. // https://html.spec.whatwg.org/multipage/web-messaging.html#structuredserializeoptions
  18. struct StructuredSerializeOptions {
  19. Vector<JS::Handle<JS::Object>> transfer;
  20. };
  21. // https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports
  22. class MessagePort final : public DOM::EventTarget
  23. , public Bindings::Transferable {
  24. WEB_PLATFORM_OBJECT(MessagePort, DOM::EventTarget);
  25. JS_DECLARE_ALLOCATOR(MessagePort);
  26. public:
  27. [[nodiscard]] static JS::NonnullGCPtr<MessagePort> create(JS::Realm&);
  28. virtual ~MessagePort() override;
  29. // https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
  30. void entangle_with(MessagePort&);
  31. // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
  32. WebIDL::ExceptionOr<void> post_message(JS::Value message, Vector<JS::Handle<JS::Object>> const& transfer);
  33. // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage-options
  34. WebIDL::ExceptionOr<void> post_message(JS::Value message, StructuredSerializeOptions const& options);
  35. void start();
  36. void close();
  37. #undef __ENUMERATE
  38. #define __ENUMERATE(attribute_name, event_name) \
  39. void set_##attribute_name(WebIDL::CallbackType*); \
  40. WebIDL::CallbackType* attribute_name();
  41. ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(__ENUMERATE)
  42. #undef __ENUMERATE
  43. // ^Transferable
  44. virtual WebIDL::ExceptionOr<void> transfer_steps(HTML::TransferDataHolder&) override;
  45. virtual WebIDL::ExceptionOr<void> transfer_receiving_steps(HTML::TransferDataHolder&) override;
  46. virtual HTML::TransferType primary_interface() const override { return HTML::TransferType::MessagePort; }
  47. private:
  48. explicit MessagePort(JS::Realm&);
  49. virtual void initialize(JS::Realm&) override;
  50. virtual void visit_edges(Cell::Visitor&) override;
  51. bool is_entangled() const { return static_cast<bool>(m_socket); }
  52. void disentangle();
  53. WebIDL::ExceptionOr<void> message_port_post_message_steps(JS::GCPtr<MessagePort> target_port, JS::Value message, StructuredSerializeOptions const& options);
  54. // The HTML spec implies(!) that this is MessagePort.[[RemotePort]]
  55. JS::GCPtr<MessagePort> m_remote_port;
  56. // https://html.spec.whatwg.org/multipage/web-messaging.html#has-been-shipped
  57. bool m_has_been_shipped { false };
  58. OwnPtr<Core::LocalSocket> m_socket;
  59. };
  60. }