Storage.cpp 5.4 KB

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