Storage.cpp 5.3 KB

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