MessagePort.cpp 4.6 KB

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