StorageEvent.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/StorageEventPrototype.h>
  8. #include <LibWeb/HTML/Storage.h>
  9. #include <LibWeb/HTML/StorageEvent.h>
  10. namespace Web::HTML {
  11. GC_DEFINE_ALLOCATOR(StorageEvent);
  12. GC::Ref<StorageEvent> StorageEvent::create(JS::Realm& realm, FlyString const& event_name, StorageEventInit const& event_init)
  13. {
  14. auto event = realm.create<StorageEvent>(realm, event_name, event_init);
  15. event->set_is_trusted(true);
  16. return event;
  17. }
  18. GC::Ref<StorageEvent> StorageEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, StorageEventInit const& event_init)
  19. {
  20. return realm.create<StorageEvent>(realm, event_name, event_init);
  21. }
  22. StorageEvent::~StorageEvent() = default;
  23. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storageevent-initstorageevent
  24. void StorageEvent::init_storage_event(String const& type, bool bubbles, bool cancelable,
  25. Optional<String> const& key, Optional<String> const& old_value, Optional<String> const& new_value,
  26. String const& url, GC::Ptr<Storage> storage_area)
  27. {
  28. // The initStorageEvent(type, bubbles, cancelable, key, oldValue, newValue, url, storageArea) method must initialize
  29. // the event in a manner analogous to the similarly-named initEvent() method. [DOM]
  30. if (dispatched())
  31. return;
  32. initialize_event(type, bubbles, cancelable);
  33. m_key = key;
  34. m_old_value = old_value;
  35. m_new_value = new_value;
  36. m_url = url;
  37. m_storage_area = storage_area;
  38. }
  39. StorageEvent::StorageEvent(JS::Realm& realm, FlyString const& event_name, StorageEventInit const& event_init)
  40. : DOM::Event(realm, event_name, event_init)
  41. , m_key(event_init.key)
  42. , m_old_value(event_init.old_value)
  43. , m_new_value(event_init.new_value)
  44. , m_url(event_init.url)
  45. , m_storage_area(event_init.storage_area)
  46. {
  47. }
  48. void StorageEvent::initialize(JS::Realm& realm)
  49. {
  50. Base::initialize(realm);
  51. WEB_SET_PROTOTYPE_FOR_INTERFACE(StorageEvent);
  52. }
  53. void StorageEvent::visit_edges(Visitor& visitor)
  54. {
  55. Base::visit_edges(visitor);
  56. visitor.visit(m_storage_area);
  57. }
  58. }