MessagePort.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_DEFINE_ALLOCATOR(MessagePort);
  15. JS::NonnullGCPtr<MessagePort> MessagePort::create(JS::Realm& realm)
  16. {
  17. return realm.heap().allocate<MessagePort>(realm, realm);
  18. }
  19. MessagePort::MessagePort(JS::Realm& realm)
  20. : DOM::EventTarget(realm)
  21. {
  22. }
  23. MessagePort::~MessagePort() = default;
  24. void MessagePort::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. set_prototype(&Bindings::ensure_web_prototype<Bindings::MessagePortPrototype>(realm, "MessagePort"_fly_string));
  28. }
  29. void MessagePort::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_remote_port);
  33. }
  34. // https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports:transfer-steps
  35. WebIDL::ExceptionOr<void> MessagePort::transfer_steps(HTML::TransferDataHolder&)
  36. {
  37. // 1. Set value's has been shipped flag to true.
  38. m_has_been_shipped = true;
  39. // FIXME: 2. Set dataHolder.[[PortMessageQueue]] to value's port message queue.
  40. // FIXME: Support delivery of messages that haven't been delivered yet on the other side
  41. // 3. If value is entangled with another port remotePort, then:
  42. if (is_entangled()) {
  43. // 1. Set remotePort's has been shipped flag to true.
  44. m_remote_port->m_has_been_shipped = true;
  45. // 2. Set dataHolder.[[RemotePort]] to remotePort.
  46. // FIXME: Append an IPC::File to the dataHolder
  47. }
  48. // 4. Otherwise, set dataHolder.[[RemotePort]] to null.
  49. else {
  50. // FIXME: Note in the dataHolder that there are no fds
  51. }
  52. return {};
  53. }
  54. WebIDL::ExceptionOr<void> MessagePort::transfer_receiving_steps(HTML::TransferDataHolder const&)
  55. {
  56. // 1. Set value's has been shipped flag to true.
  57. m_has_been_shipped = true;
  58. // FIXME 2. Move all the tasks that are to fire message events in dataHolder.[[PortMessageQueue]] to the port message queue of value,
  59. // if any, leaving value's port message queue in its initial disabled state, and, if value's relevant global object is a Window,
  60. // associating the moved tasks with value's relevant global object's associated Document.
  61. // FIXME: 3. If dataHolder.[[RemotePort]] is not null, then entangle dataHolder.[[RemotePort]] and value.
  62. // (This will disentangle dataHolder.[[RemotePort]] from the original port that was transferred.)
  63. return {};
  64. }
  65. void MessagePort::disentangle()
  66. {
  67. m_remote_port->m_remote_port = nullptr;
  68. m_remote_port = nullptr;
  69. }
  70. // https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
  71. void MessagePort::entangle_with(MessagePort& remote_port)
  72. {
  73. if (m_remote_port.ptr() == &remote_port)
  74. return;
  75. // 1. If one of the ports is already entangled, then disentangle it and the port that it was entangled with.
  76. if (is_entangled())
  77. disentangle();
  78. if (remote_port.is_entangled())
  79. remote_port.disentangle();
  80. // 2. Associate the two ports to be entangled, so that they form the two parts of a new channel.
  81. // (There is no MessageChannel object that represents this channel.)
  82. remote_port.m_remote_port = this;
  83. m_remote_port = &remote_port;
  84. }
  85. // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
  86. void MessagePort::post_message(JS::Value message)
  87. {
  88. // 1. Let targetPort be the port with which this MessagePort is entangled, if any; otherwise let it be null.
  89. auto* target_port = m_remote_port.ptr();
  90. // FIXME: 2. Let options be «[ "transfer" → transfer ]».
  91. // 3. Run the message port post message steps providing targetPort, message and options.
  92. // https://html.spec.whatwg.org/multipage/web-messaging.html#message-port-post-message-steps
  93. // FIXME: 1. Let transfer be options["transfer"].
  94. // FIXME: 2. If transfer contains this MessagePort, then throw a "DataCloneError" DOMException.
  95. // 3. Let doomed be false.
  96. bool doomed = false;
  97. // 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.
  98. // FIXME: 5. Let serializeWithTransferResult be StructuredSerializeWithTransfer(message, transfer). Rethrow any exceptions.
  99. // 6. If targetPort is null, or if doomed is true, then return.
  100. if (!target_port || doomed)
  101. return;
  102. // FIXME: 7. Add a task that runs the following steps to the port message queue of targetPort:
  103. // FIXME: This is an ad-hoc hack implementation instead, since we don't currently
  104. // have serialization and deserialization of messages.
  105. main_thread_event_loop().task_queue().add(HTML::Task::create(HTML::Task::Source::PostedMessage, nullptr, [target_port, message] {
  106. MessageEventInit event_init {};
  107. event_init.data = message;
  108. event_init.origin = "<origin>"_string;
  109. target_port->dispatch_event(MessageEvent::create(target_port->realm(), HTML::EventNames::message, event_init));
  110. }));
  111. }
  112. void MessagePort::start()
  113. {
  114. // FIXME: Message ports are supposed to be disabled by default.
  115. }
  116. // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-close
  117. void MessagePort::close()
  118. {
  119. // 1. Set this MessagePort object's [[Detached]] internal slot value to true.
  120. set_detached(true);
  121. // 2. If this MessagePort object is entangled, disentangle it.
  122. if (is_entangled())
  123. disentangle();
  124. }
  125. #undef __ENUMERATE
  126. #define __ENUMERATE(attribute_name, event_name) \
  127. void MessagePort::set_##attribute_name(WebIDL::CallbackType* value) \
  128. { \
  129. set_event_handler_attribute(event_name, value); \
  130. } \
  131. WebIDL::CallbackType* MessagePort::attribute_name() \
  132. { \
  133. return event_handler_attribute(event_name); \
  134. }
  135. ENUMERATE_MESSAGE_PORT_EVENT_HANDLERS(__ENUMERATE)
  136. #undef __ENUMERATE
  137. }