MessagePort.cpp 4.3 KB

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