BroadcastChannel.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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-name
  28. FlyString BroadcastChannel::name()
  29. {
  30. // The name getter steps are to return this's channel name.
  31. return m_channel_name;
  32. }
  33. // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-broadcastchannel-close
  34. void BroadcastChannel::close()
  35. {
  36. // The close() method steps are to set this's closed flag to true.
  37. m_closed_flag = true;
  38. }
  39. // https://html.spec.whatwg.org/multipage/web-messaging.html#handler-broadcastchannel-onmessage
  40. void BroadcastChannel::set_onmessage(WebIDL::CallbackType* event_handler)
  41. {
  42. set_event_handler_attribute(HTML::EventNames::message, event_handler);
  43. }
  44. // https://html.spec.whatwg.org/multipage/web-messaging.html#handler-broadcastchannel-onmessage
  45. WebIDL::CallbackType* BroadcastChannel::onmessage()
  46. {
  47. return event_handler_attribute(HTML::EventNames::message);
  48. }
  49. // https://html.spec.whatwg.org/multipage/web-messaging.html#handler-broadcastchannel-onmessageerror
  50. void BroadcastChannel::set_onmessageerror(WebIDL::CallbackType* event_handler)
  51. {
  52. set_event_handler_attribute(HTML::EventNames::messageerror, event_handler);
  53. }
  54. // https://html.spec.whatwg.org/multipage/web-messaging.html#handler-broadcastchannel-onmessageerror
  55. WebIDL::CallbackType* BroadcastChannel::onmessageerror()
  56. {
  57. return event_handler_attribute(HTML::EventNames::messageerror);
  58. }
  59. }