Storage.cpp 4.2 KB

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