Storage.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DeprecatedString.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/HTML/Storage.h>
  9. namespace Web::HTML {
  10. JS::NonnullGCPtr<Storage> Storage::create(JS::Realm& realm)
  11. {
  12. return realm.heap().allocate<Storage>(realm, realm);
  13. }
  14. Storage::Storage(JS::Realm& realm)
  15. : PlatformObject(realm)
  16. {
  17. }
  18. Storage::~Storage() = default;
  19. void Storage::initialize(JS::Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. set_prototype(&Bindings::ensure_web_prototype<Bindings::StoragePrototype>(realm, "Storage"));
  23. }
  24. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-length
  25. size_t Storage::length() const
  26. {
  27. // The length getter steps are to return this's map's size.
  28. return m_map.size();
  29. }
  30. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-key
  31. DeprecatedString Storage::key(size_t index)
  32. {
  33. // 1. If index is greater than or equal to this's map's size, then return null.
  34. if (index >= m_map.size())
  35. return {};
  36. // 2. Let keys be the result of running get the keys on this's map.
  37. auto keys = m_map.keys();
  38. // 3. Return keys[index].
  39. return keys[index];
  40. }
  41. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-getitem
  42. DeprecatedString Storage::get_item(DeprecatedString const& key) const
  43. {
  44. // 1. If this's map[key] does not exist, then return null.
  45. auto it = m_map.find(key);
  46. if (it == m_map.end())
  47. return {};
  48. // 2. Return this's map[key].
  49. return it->value;
  50. }
  51. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-setitem
  52. WebIDL::ExceptionOr<void> Storage::set_item(DeprecatedString const& key, DeprecatedString const& value)
  53. {
  54. // 1. Let oldValue be null.
  55. DeprecatedString old_value;
  56. // 2. Let reorder be true.
  57. bool reorder = true;
  58. // 3. If this's map[key] exists:
  59. if (auto it = m_map.find(key); it != m_map.end()) {
  60. // 1. Set oldValue to this's map[key].
  61. old_value = it->value;
  62. // 2. If oldValue is value, then return.
  63. if (old_value == value)
  64. return {};
  65. // 3. Set reorder to false.
  66. reorder = false;
  67. }
  68. // FIXME: 4. If value cannot be stored, then throw a "QuotaExceededError" DOMException exception.
  69. // 5. Set this's map[key] to value.
  70. m_map.set(key, value);
  71. // 6. If reorder is true, then reorder this.
  72. if (reorder)
  73. this->reorder();
  74. // 7. Broadcast this with key, oldValue, and value.
  75. broadcast(key, old_value, value);
  76. return {};
  77. }
  78. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-removeitem
  79. void Storage::remove_item(DeprecatedString const& key)
  80. {
  81. // 1. If this's map[key] does not exist, then return null.
  82. // FIXME: Return null?
  83. auto it = m_map.find(key);
  84. if (it == m_map.end())
  85. return;
  86. // 2. Set oldValue to this's map[key].
  87. auto old_value = it->value;
  88. // 3. Remove this's map[key].
  89. m_map.remove(it);
  90. // 4. Reorder this.
  91. reorder();
  92. // 5. Broadcast this with key, oldValue, and null.
  93. broadcast(key, old_value, {});
  94. }
  95. // https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-clear
  96. void Storage::clear()
  97. {
  98. // 1. Clear this's map.
  99. m_map.clear();
  100. // 2. Broadcast this with null, null, and null.
  101. broadcast({}, {}, {});
  102. }
  103. // https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-reorder
  104. void Storage::reorder()
  105. {
  106. // To reorder a Storage object storage, reorder storage's map's entries in an implementation-defined manner.
  107. // NOTE: This basically means that we're not required to maintain any particular iteration order.
  108. }
  109. // https://html.spec.whatwg.org/multipage/webstorage.html#concept-storage-broadcast
  110. void Storage::broadcast(DeprecatedString const& key, DeprecatedString const& old_value, DeprecatedString const& new_value)
  111. {
  112. (void)key;
  113. (void)old_value;
  114. (void)new_value;
  115. // FIXME: Implement.
  116. }
  117. Vector<DeprecatedString> Storage::supported_property_names() const
  118. {
  119. // The supported property names on a Storage object storage are the result of running get the keys on storage's map.
  120. return m_map.keys();
  121. }
  122. void Storage::dump() const
  123. {
  124. dbgln("Storage ({} key(s))", m_map.size());
  125. size_t i = 0;
  126. for (auto const& it : m_map) {
  127. dbgln("[{}] \"{}\": \"{}\"", i, it.key, it.value);
  128. ++i;
  129. }
  130. }
  131. }