MessageChannel.cpp 1.7 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. WebIDL::ExceptionOr<JS::NonnullGCPtr<MessageChannel>> MessageChannel::construct_impl(JS::Realm& realm)
  12. {
  13. return MUST_OR_THROW_OOM(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).release_value_but_fixme_should_propagate_errors();
  20. // 2. Set this's port 2 to a new MessagePort in this's relevant Realm.
  21. m_port2 = MessagePort::create(realm).release_value_but_fixme_should_propagate_errors();
  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. JS::ThrowCompletionOr<void> MessageChannel::initialize(JS::Realm& realm)
  33. {
  34. MUST_OR_THROW_OOM(Base::initialize(realm));
  35. set_prototype(&Bindings::ensure_web_prototype<Bindings::MessageChannelPrototype>(realm, "MessageChannel"));
  36. return {};
  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. }