Storage.cpp 4.0 KB

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