| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-02-28 00:06:10 +00:00
										 |  |  |  * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-12-26 11:56:03 +13:00
										 |  |  |  * Copyright (c) 2024-2025, Shannon Booth <shannon@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-09 16:05:03 -07:00
										 |  |  | #include <LibWeb/Bindings/PlatformObject.h>
 | 
					
						
							| 
									
										
										
										
											2024-12-26 11:56:03 +13:00
										 |  |  | #include <LibWeb/StorageAPI/StorageBottle.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 22:32:49 +13:00
										 |  |  | // https://html.spec.whatwg.org/multipage/webstorage.html#storage-2
 | 
					
						
							| 
									
										
										
										
											2024-01-09 16:05:03 -07:00
										 |  |  | class Storage : public Bindings::PlatformObject { | 
					
						
							|  |  |  |     WEB_PLATFORM_OBJECT(Storage, Bindings::PlatformObject); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_DECLARE_ALLOCATOR(Storage); | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-03 19:44:37 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2024-12-23 22:32:49 +13:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-type
 | 
					
						
							|  |  |  |     enum class Type { | 
					
						
							|  |  |  |         Local, | 
					
						
							|  |  |  |         Session, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-11 18:51:22 +02:00
										 |  |  |     [[nodiscard]] static GC::Ref<Storage> create(JS::Realm&, Type, GC::Ref<StorageAPI::StorageBottle>); | 
					
						
							| 
									
										
										
										
											2024-12-23 22:32:49 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  |     ~Storage(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t length() const; | 
					
						
							| 
									
										
										
										
											2023-08-26 16:30:02 +12:00
										 |  |  |     Optional<String> key(size_t index); | 
					
						
							| 
									
										
										
										
											2025-06-08 23:35:46 +02:00
										 |  |  |     Optional<String> get_item(String const& key) const; | 
					
						
							| 
									
										
										
										
											2023-08-26 16:30:02 +12:00
										 |  |  |     WebIDL::ExceptionOr<void> set_item(String const& key, String const& value); | 
					
						
							| 
									
										
										
										
											2024-12-27 18:47:40 +13:00
										 |  |  |     void remove_item(String const& key); | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  |     void clear(); | 
					
						
							| 
									
										
										
										
											2024-12-23 22:32:49 +13:00
										 |  |  |     Type type() const { return m_type; } | 
					
						
							| 
									
										
										
										
											2022-04-02 00:14:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 19:50:14 +01:00
										 |  |  |     void dump() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2025-06-11 18:51:22 +02:00
										 |  |  |     Storage(JS::Realm&, Type, GC::Ref<StorageAPI::StorageBottle>); | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2024-12-27 20:55:08 +13:00
										 |  |  |     virtual void finalize() override; | 
					
						
							| 
									
										
										
										
											2025-06-11 18:51:22 +02:00
										 |  |  |     virtual void visit_edges(GC::Cell::Visitor&) override; | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-09 16:05:03 -07:00
										 |  |  |     // ^PlatformObject
 | 
					
						
							| 
									
										
										
										
											2024-10-18 00:04:35 +02:00
										 |  |  |     virtual Optional<JS::Value> item_value(size_t index) const override; | 
					
						
							| 
									
										
										
										
											2024-07-25 17:36:10 +12:00
										 |  |  |     virtual JS::Value named_item_value(FlyString const&) const override; | 
					
						
							| 
									
										
										
										
											2023-11-21 12:15:56 +13:00
										 |  |  |     virtual WebIDL::ExceptionOr<DidDeletionFail> delete_value(String const&) override; | 
					
						
							| 
									
										
										
										
											2023-12-24 20:59:00 +01:00
										 |  |  |     virtual Vector<FlyString> supported_property_names() const override; | 
					
						
							| 
									
										
										
										
											2024-10-18 00:04:35 +02:00
										 |  |  |     virtual WebIDL::ExceptionOr<void> set_value_of_indexed_property(u32, JS::Value) override; | 
					
						
							| 
									
										
										
										
											2023-11-21 12:15:56 +13:00
										 |  |  |     virtual WebIDL::ExceptionOr<void> set_value_of_named_property(String const& key, JS::Value value) override; | 
					
						
							| 
									
										
										
										
											2023-02-28 00:06:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  |     void reorder(); | 
					
						
							| 
									
										
										
										
											2024-12-27 18:47:40 +13:00
										 |  |  |     void broadcast(Optional<String> const& key, Optional<String> const& old_value, Optional<String> const& new_value); | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-23 22:32:49 +13:00
										 |  |  |     Type m_type {}; | 
					
						
							| 
									
										
										
										
											2025-06-11 18:51:22 +02:00
										 |  |  |     GC::Ref<StorageAPI::StorageBottle> m_storage_bottle; | 
					
						
							| 
									
										
										
										
											2022-02-08 19:38:29 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |