Storage.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.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/PlatformObject.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. namespace Web::HTML {
  12. class Storage : public Bindings::PlatformObject {
  13. WEB_PLATFORM_OBJECT(Storage, Bindings::PlatformObject);
  14. GC_DECLARE_ALLOCATOR(Storage);
  15. public:
  16. [[nodiscard]] static GC::Ref<Storage> create(JS::Realm&);
  17. ~Storage();
  18. size_t length() const;
  19. Optional<String> key(size_t index);
  20. Optional<String> get_item(StringView key) const;
  21. WebIDL::ExceptionOr<void> set_item(String const& key, String const& value);
  22. void remove_item(StringView key);
  23. void clear();
  24. auto const& map() const { return m_map; }
  25. void dump() const;
  26. private:
  27. explicit Storage(JS::Realm&);
  28. virtual void initialize(JS::Realm&) override;
  29. // ^PlatformObject
  30. virtual Optional<JS::Value> item_value(size_t index) const override;
  31. virtual JS::Value named_item_value(FlyString const&) const override;
  32. virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&) override;
  33. virtual Vector<FlyString> supported_property_names() const override;
  34. virtual WebIDL::ExceptionOr<void> set_value_of_indexed_property(u32, JS::Value) override;
  35. virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const& key, JS::Value value) override;
  36. void reorder();
  37. void broadcast(StringView key, StringView old_value, StringView new_value);
  38. OrderedHashMap<String, String> m_map;
  39. };
  40. }