MessagePort.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/EventDispatcher.h>
  7. #include <LibWeb/HTML/EventHandler.h>
  8. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  9. #include <LibWeb/HTML/EventNames.h>
  10. #include <LibWeb/HTML/MessageEvent.h>
  11. #include <LibWeb/HTML/MessagePort.h>
  12. namespace Web::HTML {
  13. JS::NonnullGCPtr<MessagePort> MessagePort::create(HTML::Window& window)
  14. {
  15. return *window.heap().allocate<MessagePort>(window.realm(), window);
  16. }
  17. MessagePort::MessagePort(HTML::Window& window)
  18. : DOM::EventTarget(window.realm())
  19. {
  20. set_prototype(&window.cached_web_prototype("MessagePort"));
  21. }
  22. MessagePort::~MessagePort() = default;
  23. void MessagePort::visit_edges(Cell::Visitor& visitor)
  24. {
  25. Base::visit_edges(visitor);
  26. visitor.visit(m_remote_port.ptr());
  27. }
  28. void MessagePort::disentangle()
  29. {
  30. m_remote_port->m_remote_port = nullptr;
  31. m_remote_port = nullptr;
  32. }
  33. // https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
  34. void MessagePort::entangle_with(MessagePort& remote_port)
  35. {
  36. if (m_remote_port.ptr() == &remote_port)
  37. return;
  38. // 1. If one of the ports is already entangled, then disentangle it and the port that it was entangled with.
  39. if (is_entangled())
  40. disentangle();
  41. if (remote_port.is_entangled())
  42. remote_port.disentangle();
  43. // 2. Associate the two ports to be entangled, so that they form the two parts of a new channel.
  44. // (There is no MessageChannel object that represents this channel.)
  45. remote_port.m_remote_port = this;
  46. m_remote_port = &remote_port;
  47. }
  48. // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
  49. void MessagePort::post_message(JS::Value message)
  50. {
  51. // 1. Let targetPort be the port with which this MessagePort is entangled, if any; otherwise let it be null.
  52. auto* target_port = m_remote_port.ptr();
  53. // FIXME: 2. Let options be «[ "transfer" → transfer ]».
  54. // 3. Run the message port post message steps providing targetPort, message and options.
  55. // https://html.spec.whatwg.org/multipage/web-messaging.html#message-port-post-message-steps
  56. // FIXME: 1. Let transfer be options["transfer"].
  57. // FIXME: 2. If transfer contains this MessagePort, then throw a "DataCloneError" DOMException.
  58. // 3. Let doomed be false.
  59. bool doomed = false;
  60. // FIXME: 4. If targetPort is not null and transfer contains targetPort, then set doomed to true and optionally report to a developer console that the target port was posted to itself, causing the communication channel to be lost.
  61. // FIXME: 5. Let serializeWithTransferResult be StructuredSerializeWithTransfer(message, transfer). Rethrow any exceptions.
  62. // 6. If targetPort is null, or if doomed is true, then return.
  63. if (!target_port || doomed)
  64. return;
  65. // FIXME: 7. Add a task that runs the following steps to the port message queue of targetPort:
  66. // FIXME: This is an ad-hoc hack implementation instead, since we don't currently
  67. // have serialization and deserialization of messages.
  68. main_thread_event_loop().task_queue().add(HTML::Task::create(HTML::Task::Source::PostedMessage, nullptr, [target_port, message]() mutable {
  69. MessageEventInit event_init {};
  70. event_init.data = message;
  71. event_init.origin = "<origin>";
  72. target_port->dispatch_event(*MessageEvent::create(verify_cast<HTML::Window>(target_port->realm().global_object()), HTML::EventNames::message, event_init));
  73. }));
  74. }
  75. void MessagePort::start()
  76. {
  77. // FIXME: Message ports are supposed to be disabled by default.
  78. }
  79. // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-close
  80. void MessagePort::close()
  81. {
  82. // 1. Set this MessagePort object's [[Detached]] internal slot value to true.
  83. m_detached = true;
  84. // 2. If this MessagePort object is entangled, disentangle it.
  85. if (is_entangled())
  86. disentangle();
  87. }
  88. #undef __ENUMERATE
  89. #define __ENUMERATE(attribute_name, event_name) \
  90. void MessagePort::set_##attribute_name(Bindings::CallbackType* value) \
  91. { \
  92. set_event_handler_attribute(event_name, value); \
  93. } \
  94. Bindings::CallbackType* MessagePort::attribute_name() \
  95. { \
  96. return event_handler_attribute(event_name); \
  97. }
  98. ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(__ENUMERATE)
  99. #undef __ENUMERATE
  100. }