Serenity Operating System
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
8#pragma once
9
10#include <AK/HashMap.h>
11#include <LibWeb/Bindings/LegacyPlatformObject.h>
12#include <LibWeb/WebIDL/ExceptionOr.h>
13
14namespace Web::HTML {
15
16class Storage : public Bindings::LegacyPlatformObject {
17 WEB_PLATFORM_OBJECT(Storage, Bindings::LegacyPlatformObject);
18
19public:
20 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> create(JS::Realm&);
21 ~Storage();
22
23 size_t length() const;
24 DeprecatedString key(size_t index);
25 DeprecatedString get_item(DeprecatedString const& key) const;
26 WebIDL::ExceptionOr<void> set_item(DeprecatedString const& key, DeprecatedString const& value);
27 void remove_item(DeprecatedString const& key);
28 void clear();
29
30 auto const& map() const { return m_map; }
31
32 void dump() const;
33
34private:
35 explicit Storage(JS::Realm&);
36
37 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
38
39 // ^LegacyPlatformObject
40 virtual WebIDL::ExceptionOr<JS::Value> named_item_value(DeprecatedFlyString const&) const override;
41 virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(DeprecatedString const&) override;
42 virtual Vector<DeprecatedString> supported_property_names() const override;
43 virtual WebIDL::ExceptionOr<void> set_value_of_named_property(DeprecatedString const& key, JS::Value value) override;
44
45 virtual bool supports_indexed_properties() const override { return false; }
46 virtual bool supports_named_properties() const override { return true; }
47 virtual bool has_indexed_property_setter() const override { return false; }
48 virtual bool has_named_property_setter() const override { return true; }
49 virtual bool has_named_property_deleter() const override { return true; }
50 virtual bool has_legacy_override_built_ins_interface_extended_attribute() const override { return true; }
51 virtual bool has_legacy_unenumerable_named_properties_interface_extended_attribute() const override { return false; }
52 virtual bool has_global_interface_extended_attribute() const override { return false; }
53 virtual bool indexed_property_setter_has_identifier() const override { return false; }
54 virtual bool named_property_setter_has_identifier() const override { return true; }
55 virtual bool named_property_deleter_has_identifier() const override { return true; }
56
57 void reorder();
58 void broadcast(DeprecatedString const& key, DeprecatedString const& old_value, DeprecatedString const& new_value);
59
60 OrderedHashMap<DeprecatedString, DeprecatedString> m_map;
61};
62
63}