MessageChannel.cpp 1.5 KB

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