MessageChannel.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/MessageChannelPrototype.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/MessageChannel.h>
  9. #include <LibWeb/HTML/MessagePort.h>
  10. #include <LibWeb/HTML/Window.h>
  11. namespace Web::HTML {
  12. JS::NonnullGCPtr<MessageChannel> MessageChannel::create_with_global_object(HTML::Window& window)
  13. {
  14. return *window.heap().allocate<MessageChannel>(window.realm(), window);
  15. }
  16. MessageChannel::MessageChannel(HTML::Window& window)
  17. : PlatformObject(window.realm())
  18. {
  19. set_prototype(&window.ensure_web_prototype<Bindings::MessageChannelPrototype>("MessageChannel"));
  20. // 1. Set this's port 1 to a new MessagePort in this's relevant Realm.
  21. m_port1 = MessagePort::create(window);
  22. // 2. Set this's port 2 to a new MessagePort in this's relevant Realm.
  23. m_port2 = MessagePort::create(window);
  24. // 3. Entangle this's port 1 and this's port 2.
  25. m_port1->entangle_with(*m_port2);
  26. }
  27. MessageChannel::~MessageChannel() = default;
  28. void MessageChannel::visit_edges(Cell::Visitor& visitor)
  29. {
  30. Base::visit_edges(visitor);
  31. visitor.visit(m_port1.ptr());
  32. visitor.visit(m_port2.ptr());
  33. }
  34. MessagePort* MessageChannel::port1()
  35. {
  36. return m_port1;
  37. }
  38. MessagePort* MessageChannel::port2()
  39. {
  40. return m_port2;
  41. }
  42. MessagePort const* MessageChannel::port1() const
  43. {
  44. return m_port1;
  45. }
  46. MessagePort const* MessageChannel::port2() const
  47. {
  48. return m_port2;
  49. }
  50. }