Storage.cpp 4.0 KB

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