Storage.cpp 4.5 KB

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