BroadcastChannel.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Realm.h>
  7. #include <LibWeb/Bindings/BroadcastChannelPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/HTML/BroadcastChannel.h>
  10. #include <LibWeb/HTML/EventNames.h>
  11. namespace Web::HTML {
  12. GC_DEFINE_ALLOCATOR(BroadcastChannel);
  13. GC::Ref<BroadcastChannel> BroadcastChannel::construct_impl(JS::Realm& realm, FlyString const& name)
  14. {
  15. return realm.create<BroadcastChannel>(realm, name);
  16. }
  17. BroadcastChannel::BroadcastChannel(JS::Realm& realm, FlyString const& name)
  18. : DOM::EventTarget(realm)
  19. , m_channel_name(name)
  20. {
  21. }
  22. void BroadcastChannel::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(BroadcastChannel);
  26. }
  27. // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-broadcastchannel-close
  28. void BroadcastChannel::close()
  29. {
  30. // The close() method steps are to set this's closed flag to true.
  31. m_closed_flag = true;
  32. }
  33. // https://html.spec.whatwg.org/multipage/web-messaging.html#handler-broadcastchannel-onmessage
  34. void BroadcastChannel::set_onmessage(GC::Ptr<WebIDL::CallbackType> event_handler)
  35. {
  36. set_event_handler_attribute(HTML::EventNames::message, event_handler);
  37. }
  38. // https://html.spec.whatwg.org/multipage/web-messaging.html#handler-broadcastchannel-onmessage
  39. GC::Ptr<WebIDL::CallbackType> BroadcastChannel::onmessage()
  40. {
  41. return event_handler_attribute(HTML::EventNames::message);
  42. }
  43. // https://html.spec.whatwg.org/multipage/web-messaging.html#handler-broadcastchannel-onmessageerror
  44. void BroadcastChannel::set_onmessageerror(GC::Ptr<WebIDL::CallbackType> event_handler)
  45. {
  46. set_event_handler_attribute(HTML::EventNames::messageerror, event_handler);
  47. }
  48. // https://html.spec.whatwg.org/multipage/web-messaging.html#handler-broadcastchannel-onmessageerror
  49. GC::Ptr<WebIDL::CallbackType> BroadcastChannel::onmessageerror()
  50. {
  51. return event_handler_attribute(HTML::EventNames::messageerror);
  52. }
  53. }