Storage.cpp 5.6 KB

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