MessageChannel.cpp 1.4 KB

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