MessageChannel.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. JS_DEFINE_ALLOCATOR(MessageChannel);
  12. WebIDL::ExceptionOr<JS::NonnullGCPtr<MessageChannel>> MessageChannel::construct_impl(JS::Realm& realm)
  13. {
  14. return realm.heap().allocate<MessageChannel>(realm, realm);
  15. }
  16. MessageChannel::MessageChannel(JS::Realm& realm)
  17. : PlatformObject(realm)
  18. {
  19. // 1. Set this's port 1 to a new MessagePort in this's relevant Realm.
  20. m_port1 = MessagePort::create(realm);
  21. // 2. Set this's port 2 to a new MessagePort in this's relevant Realm.
  22. m_port2 = MessagePort::create(realm);
  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);
  31. visitor.visit(m_port2);
  32. }
  33. void MessageChannel::initialize(JS::Realm& realm)
  34. {
  35. Base::initialize(realm);
  36. WEB_SET_PROTOTYPE_FOR_INTERFACE(MessageChannel);
  37. }
  38. MessagePort* MessageChannel::port1()
  39. {
  40. return m_port1;
  41. }
  42. MessagePort* MessageChannel::port2()
  43. {
  44. return m_port2;
  45. }
  46. MessagePort const* MessageChannel::port1() const
  47. {
  48. return m_port1;
  49. }
  50. MessagePort const* MessageChannel::port2() const
  51. {
  52. return m_port2;
  53. }
  54. }