Storage.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <LibWeb/Bindings/LegacyPlatformObject.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. namespace Web::HTML {
  12. class Storage : public Bindings::LegacyPlatformObject {
  13. WEB_PLATFORM_OBJECT(Storage, Bindings::LegacyPlatformObject);
  14. public:
  15. [[nodiscard]] static JS::NonnullGCPtr<Storage> create(JS::Realm&);
  16. ~Storage();
  17. size_t length() const;
  18. Optional<String> key(size_t index);
  19. Optional<String> get_item(StringView key) const;
  20. WebIDL::ExceptionOr<void> set_item(String const& key, String const& value);
  21. void remove_item(StringView key);
  22. void clear();
  23. auto const& map() const { return m_map; }
  24. void dump() const;
  25. private:
  26. explicit Storage(JS::Realm&);
  27. virtual void initialize(JS::Realm&) override;
  28. // ^LegacyPlatformObject
  29. virtual WebIDL::ExceptionOr<JS::Value> named_item_value(DeprecatedFlyString const&) const override;
  30. virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(DeprecatedString const&) override;
  31. virtual Vector<DeprecatedString> supported_property_names() const override;
  32. virtual WebIDL::ExceptionOr<void> set_value_of_named_property(DeprecatedString const& key, JS::Value value) override;
  33. virtual bool supports_indexed_properties() const override { return false; }
  34. virtual bool supports_named_properties() const override { return true; }
  35. virtual bool has_indexed_property_setter() const override { return false; }
  36. virtual bool has_named_property_setter() const override { return true; }
  37. virtual bool has_named_property_deleter() const override { return true; }
  38. virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return true; }
  39. virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return false; }
  40. virtual bool has_global_interface_extended_attribute() const override { return false; }
  41. virtual bool indexed_property_setter_has_identifier() const override { return false; }
  42. virtual bool named_property_setter_has_identifier() const override { return true; }
  43. virtual bool named_property_deleter_has_identifier() const override { return true; }
  44. void reorder();
  45. void broadcast(StringView key, StringView old_value, StringView new_value);
  46. OrderedHashMap<String, String> m_map;
  47. };
  48. }