Storage.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/DeprecatedString.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/HTML/Storage.h>
  10. namespace Web::HTML {
  11. WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> Storage::create(JS::Realm& realm)
  12. {
  13. return MUST_OR_THROW_OOM(realm.heap().allocate<Storage>(realm, realm));
  14. }
  15. Storage::Storage(JS::Realm& realm)
  16. : Bindings::LegacyPlatformObject(realm)
  17. {
  18. }
  19. Storage::~Storage() = default;
  20. JS::ThrowCompletionOr<void> Storage::initialize(JS::Realm& realm)
  21. {
  22. MUST_OR_THROW_OOM(Base::initialize(realm));
  23. set_prototype(&Bindings::ensure_web_prototype<Bindings::StoragePrototype>(realm, "Storage"));
  24. return {};
  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. DeprecatedString 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. DeprecatedString Storage::get_item(DeprecatedString const& 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(DeprecatedString const& key, DeprecatedString const& value)
  55. {
  56. // 1. Let oldValue be null.
  57. DeprecatedString 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(DeprecatedString const& 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(DeprecatedString const& key, DeprecatedString const& old_value, DeprecatedString const& new_value)
  113. {
  114. (void)key;
  115. (void)old_value;
  116. (void)new_value;
  117. // FIXME: Implement.
  118. }
  119. Vector<DeprecatedString> 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. return m_map.keys();
  123. }
  124. WebIDL::ExceptionOr<JS::Value> Storage::named_item_value(DeprecatedFlyString const& name) const
  125. {
  126. auto value = get_item(name);
  127. if (value.is_null())
  128. return JS::js_null();
  129. return JS::PrimitiveString::create(vm(), value);
  130. }
  131. WebIDL::ExceptionOr<Bindings::LegacyPlatformObject::DidDeletionFail> Storage::delete_value(DeprecatedString const& name)
  132. {
  133. remove_item(name);
  134. return DidDeletionFail::NotRelevant;
  135. }
  136. WebIDL::ExceptionOr<void> Storage::set_value_of_named_property(DeprecatedString const& key, JS::Value unconverted_value)
  137. {
  138. // NOTE: Since LegacyPlatformObject does not know the type of value, we must convert it ourselves.
  139. // The type of `value` is `DOMString`.
  140. auto value = TRY(unconverted_value.to_deprecated_string(vm()));
  141. return set_item(key, value);
  142. }
  143. void Storage::dump() const
  144. {
  145. dbgln("Storage ({} key(s))", m_map.size());
  146. size_t i = 0;
  147. for (auto const& it : m_map) {
  148. dbgln("[{}] \"{}\": \"{}\"", i, it.key, it.value);
  149. ++i;
  150. }
  151. }
  152. }