Storage.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <AK/String.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Bindings/StoragePrototype.h>
  10. #include <LibWeb/HTML/Storage.h>
  11. namespace Web::HTML {
  12. GC_DEFINE_ALLOCATOR(Storage);
  13. GC::Ref<Storage> Storage::create(JS::Realm& realm)
  14. {
  15. return realm.create<Storage>(realm);
  16. }
  17. Storage::Storage(JS::Realm& realm)
  18. : Bindings::PlatformObject(realm)
  19. {
  20. m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
  21. .supports_indexed_properties = true,
  22. .supports_named_properties = true,
  23. .has_indexed_property_setter = true,
  24. .has_named_property_setter = true,
  25. .has_named_property_deleter = true,
  26. .indexed_property_setter_has_identifier = true,
  27. .named_property_setter_has_identifier = true,
  28. .named_property_deleter_has_identifier = true,
  29. };
  30. }
  31. Storage::~Storage() = default;
  32. void Storage::initialize(JS::Realm& realm)
  33. {
  34. Base::initialize(realm);
  35. WEB_SET_PROTOTYPE_FOR_INTERFACE(Storage);
  36. }
  37. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-length
  38. size_t Storage::length() const
  39. {
  40. // The length getter steps are to return this's map's size.
  41. return m_map.size();
  42. }
  43. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-key
  44. Optional<String> Storage::key(size_t index)
  45. {
  46. // 1. If index is greater than or equal to this's map's size, then return null.
  47. if (index >= m_map.size())
  48. return {};
  49. // 2. Let keys be the result of running get the keys on this's map.
  50. auto keys = m_map.keys();
  51. // 3. Return keys[index].
  52. return keys[index];
  53. }
  54. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-getitem
  55. Optional<String> Storage::get_item(StringView key) const
  56. {
  57. // 1. If this's map[key] does not exist, then return null.
  58. auto it = m_map.find(key);
  59. if (it == m_map.end())
  60. return {};
  61. // 2. Return this's map[key].
  62. return it->value;
  63. }
  64. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-setitem
  65. WebIDL::ExceptionOr<void> Storage::set_item(String const& key, String const& value)
  66. {
  67. // 1. Let oldValue be null.
  68. String old_value;
  69. // 2. Let reorder be true.
  70. bool reorder = true;
  71. // 3. If this's map[key] exists:
  72. if (auto it = m_map.find(key); it != m_map.end()) {
  73. // 1. Set oldValue to this's map[key].
  74. old_value = it->value;
  75. // 2. If oldValue is value, then return.
  76. if (old_value == value)
  77. return {};
  78. // 3. Set reorder to false.
  79. reorder = false;
  80. }
  81. // FIXME: 4. If value cannot be stored, then throw a "QuotaExceededError" DOMException exception.
  82. // 5. Set this's map[key] to value.
  83. m_map.set(key, value);
  84. // 6. If reorder is true, then reorder this.
  85. if (reorder)
  86. this->reorder();
  87. // 7. Broadcast this with key, oldValue, and value.
  88. broadcast(key, old_value, value);
  89. return {};
  90. }
  91. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-removeitem
  92. void Storage::remove_item(StringView key)
  93. {
  94. // 1. If this's map[key] does not exist, then return null.
  95. // FIXME: Return null?
  96. auto it = m_map.find(key);
  97. if (it == m_map.end())
  98. return;
  99. // 2. Set oldValue to this's map[key].
  100. auto old_value = it->value;
  101. // 3. Remove this's map[key].
  102. m_map.remove(it);
  103. // 4. Reorder this.
  104. reorder();
  105. // 5. Broadcast this with key, oldValue, and null.
  106. broadcast(key, old_value, {});
  107. }
  108. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-clear
  109. void Storage::clear()
  110. {
  111. // 1. Clear this's map.
  112. m_map.clear();
  113. // 2. Broadcast this with null, null, and null.
  114. broadcast({}, {}, {});
  115. }
  116. // https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-reorder
  117. void Storage::reorder()
  118. {
  119. // To reorder a Storage object storage, reorder storage's map's entries in an implementation-defined manner.
  120. // NOTE: This basically means that we're not required to maintain any particular iteration order.
  121. }
  122. // https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-broadcast
  123. void Storage::broadcast(StringView key, StringView old_value, StringView new_value)
  124. {
  125. (void)key;
  126. (void)old_value;
  127. (void)new_value;
  128. // FIXME: Implement.
  129. }
  130. Vector<FlyString> Storage::supported_property_names() const
  131. {
  132. // The supported property names on a Storage object storage are the result of running get the keys on storage's map.
  133. Vector<FlyString> names;
  134. names.ensure_capacity(m_map.size());
  135. for (auto const& key : m_map.keys())
  136. names.unchecked_append(key);
  137. return names;
  138. }
  139. Optional<JS::Value> Storage::item_value(size_t index) const
  140. {
  141. // Handle index as a string since that's our key type
  142. auto key = String::number(index);
  143. auto value = get_item(key);
  144. if (!value.has_value())
  145. return {};
  146. return JS::PrimitiveString::create(vm(), value.release_value());
  147. }
  148. JS::Value Storage::named_item_value(FlyString const& name) const
  149. {
  150. auto value = get_item(name);
  151. if (!value.has_value())
  152. // AD-HOC: Spec leaves open to a description at: https://html.spec.whatwg.org/multipage/webstorage.html#the-storage-interface
  153. // However correct behavior expected here: https://github.com/whatwg/html/issues/8684
  154. return JS::js_undefined();
  155. return JS::PrimitiveString::create(vm(), value.release_value());
  156. }
  157. WebIDL::ExceptionOr<Bindings::PlatformObject::DidDeletionFail> Storage::delete_value(String const& name)
  158. {
  159. remove_item(name);
  160. return DidDeletionFail::NotRelevant;
  161. }
  162. WebIDL::ExceptionOr<void> Storage::set_value_of_indexed_property(u32 index, JS::Value unconverted_value)
  163. {
  164. // Handle index as a string since that's our key type
  165. auto key = String::number(index);
  166. return set_value_of_named_property(key, unconverted_value);
  167. }
  168. WebIDL::ExceptionOr<void> Storage::set_value_of_named_property(String const& key, JS::Value unconverted_value)
  169. {
  170. // NOTE: Since PlatformObject does not know the type of value, we must convert it ourselves.
  171. // The type of `value` is `DOMString`.
  172. auto value = TRY(unconverted_value.to_string(vm()));
  173. return set_item(key, value);
  174. }
  175. void Storage::dump() const
  176. {
  177. dbgln("Storage ({} key(s))", m_map.size());
  178. size_t i = 0;
  179. for (auto const& it : m_map) {
  180. dbgln("[{}] \"{}\": \"{}\"", i, it.key, it.value);
  181. ++i;
  182. }
  183. }
  184. }