MessageChannel.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/MessageChannelPrototype.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/MessageChannel.h>
  10. #include <LibWeb/HTML/MessagePort.h>
  11. namespace Web::HTML {
  12. GC_DEFINE_ALLOCATOR(MessageChannel);
  13. WebIDL::ExceptionOr<GC::Ref<MessageChannel>> MessageChannel::construct_impl(JS::Realm& realm)
  14. {
  15. return realm.create<MessageChannel>(realm);
  16. }
  17. MessageChannel::MessageChannel(JS::Realm& realm)
  18. : PlatformObject(realm)
  19. {
  20. // 1. Set this's port 1 to a new MessagePort in this's relevant Realm.
  21. m_port1 = MessagePort::create(realm);
  22. // 2. Set this's port 2 to a new MessagePort in this's relevant Realm.
  23. m_port2 = MessagePort::create(realm);
  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);
  32. visitor.visit(m_port2);
  33. }
  34. void MessageChannel::initialize(JS::Realm& realm)
  35. {
  36. Base::initialize(realm);
  37. WEB_SET_PROTOTYPE_FOR_INTERFACE(MessageChannel);
  38. }
  39. MessagePort* MessageChannel::port1()
  40. {
  41. return m_port1;
  42. }
  43. MessagePort* MessageChannel::port2()
  44. {
  45. return m_port2;
  46. }
  47. MessagePort const* MessageChannel::port1() const
  48. {
  49. return m_port1;
  50. }
  51. MessagePort const* MessageChannel::port2() const
  52. {
  53. return m_port2;
  54. }
  55. }