Truly independent web browser
LibWebView: Fan web storage changes out from the StorageJar
sideshowbarker1dcbfe6dc637
diff --git a/Libraries/LibWeb/HTML/Storage.cpp b/Libraries/LibWeb/HTML/Storage.cppindex f30d18cbef5..7d6ea53204a 100644--- a/Libraries/LibWeb/HTML/Storage.cpp+++ b/Libraries/LibWeb/HTML/Storage.cpp@@ -183,10 +183,6 @@ void Storage::broadcast(Optional<Utf16View> key, Optional<Utf16View> old_value,// 1. Let thisDocument be storage's relevant global object's associated Document.auto const& this_document = m_window->associated_document();- auto storage_endpoint = type() == Type::Local ? StorageAPI::StorageEndpointType::LocalStorage : StorageAPI::StorageEndpointType::SessionStorage;- auto to_string = [](Utf16View view) { return Utf16String::from_utf16(view); };- m_window->page().client().page_did_broadcast_storage_change(storage_endpoint, this_document.url().serialize(), key.map(to_string), old_value.map(to_string), new_value.map(to_string));-// 2. Let url be the serialization of thisDocument's URL.auto url = utf16_string_from_url_ascii(this_document.url().serialize());diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex 506f2d1a6e4..dd076a8d99d 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -802,7 +802,6 @@ public:virtual Vector<Utf16String> page_did_request_storage_keys([[maybe_unused]] Web::StorageAPI::StorageEndpointType storage_endpoint, [[maybe_unused]] Web::HTML::EnvironmentId const& environment_id) { return {}; }virtual u64 page_did_request_storage_usage([[maybe_unused]] Web::HTML::EnvironmentId const& environment_id) { return {}; }virtual void page_did_clear_storage([[maybe_unused]] Web::StorageAPI::StorageEndpointType storage_endpoint, [[maybe_unused]] Web::HTML::EnvironmentId const& environment_id) { }- virtual void page_did_broadcast_storage_change([[maybe_unused]] Web::StorageAPI::StorageEndpointType storage_endpoint, [[maybe_unused]] String const& url, [[maybe_unused]] Optional<Utf16String> const& key, [[maybe_unused]] Optional<Utf16String> const& old_value, [[maybe_unused]] Optional<Utf16String> const& new_value) { }virtual URL::BlobURLEntry::Token page_did_add_blob_url_entry(HTML::EnvironmentSettingsObject const&, [[maybe_unused]] Utf16String const& url, [[maybe_unused]] FileAPI::SerializedBlobURLEntry const& entry) { return 0; }virtual void page_did_remove_blob_url_entries(HTML::EnvironmentSettingsObject const&, [[maybe_unused]] Vector<Utf16String> const& urls) { }diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex dec319faf69..2504fd5d5cc 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -26,6 +26,7 @@#include <LibDatabase/Database.h>#include <LibDevTools/DevToolsServer.h>#include <LibDevTools/FirefoxClient.h>+#include <LibDevTools/StorageHelpers.h>#include <LibFileSystem/FileSystem.h>#include <LibGfx/SystemTheme.h>#include <LibHTTP/Cache/CacheMode.h>@@ -66,6 +67,7 @@#include <LibWebView/SessionStore.h>#include <LibWebView/SiteCompatibility.h>#include <LibWebView/SpellChecker.h>+#include <LibWebView/StorageAreaRegistry.h>#include <LibWebView/TabPerformanceMonitor.h>#include <LibWebView/URL.h>#include <LibWebView/UserAgent.h>@@ -1603,9 +1605,69 @@ void Application::launch_spare_web_content_process()});}+// DevTools hears of every change to a jar, whoever made it, keyed on the host of the page that made it — or on the+// storage key, when the change came from the browser itself.+static void notify_devtools_of_storage_change(StorageJar& jar, StorageChange const& change, Optional<String> const& url)+{+ Optional<String> host;+ if (url.has_value())+ host = DevTools::storage_host_for_url(*url);+ if (!host.has_value())+ host = change.storage_key;++ DevTools::DevToolsDelegate::StorageChange::Type type;+ switch (change.type) {+ case StorageChange::Type::Set:+ type = change.old_value.has_value() ? DevTools::DevToolsDelegate::StorageChange::Type::Changed : DevTools::DevToolsDelegate::StorageChange::Type::Added;+ break;+ case StorageChange::Type::Removed:+ type = DevTools::DevToolsDelegate::StorageChange::Type::Deleted;+ break;+ case StorageChange::Type::Cleared:+ case StorageChange::Type::Reset:+ type = DevTools::DevToolsDelegate::StorageChange::Type::Cleared;+ break;+ }++ Vector<DevTools::DevToolsDelegate::StorageChange> devtools_changes;+ devtools_changes.append({+ .storage_endpoint = change.storage_endpoint,+ .host = *host,+ .type = type,+ .key = change.key.map([](auto const& key) { return key.to_utf8(); }),+ });++ // A reset says only that the bottle changed, not how; DevTools can't be told that, so it hears the bottle emptied+ // and then every item still in it added back.+ if (change.type == StorageChange::Type::Reset) {+ for (auto const& item : jar.get_all_items(change.storage_endpoint, change.storage_key)) {+ devtools_changes.append({+ .storage_endpoint = change.storage_endpoint,+ .host = *host,+ .type = DevTools::DevToolsDelegate::StorageChange::Type::Added,+ .key = item.key.to_utf8(),+ });+ }+ }++ ViewImplementation::for_each_view([&](ViewImplementation& view) {+ bool owns_jar = view.session().storage_jar.ptr() == &jar || (view.has_traversable() && &view.traversable().session_storage() == &jar);+ if (owns_jar) {+ for (auto const& devtools_change : devtools_changes)+ view.notify_storage_changed(devtools_change);+ }+ return IterationDecision::Continue;+ });+}+ErrorOr<void> Application::launch_services(){m_default_session = BrowsingSession::create(IsPrivate::No);+ StorageAreaRegistry::the().on_change_for_devtools = [this](StorageJar& jar, WebView::StorageChange const& change, Optional<String> const& url) {+ // Only an attached DevTools has anyone to tell, and the lookup below isn't free on a page's write path.+ if (m_devtools)+ notify_devtools_of_storage_change(jar, change, url);+ };m_settings_observer = make<ApplicationSettingsObserver>();m_bookmark_store_observer = make<ApplicationBookmarkStoreObserver>();@@ -3317,31 +3379,27 @@ void Application::inspect_storage(DevTools::TabDescription const& description, Wview->inspect_storage(storage_endpoint, request_id);}-ErrorOr<Optional<String>> Application::set_storage_item(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& storage_key, String const& key, String const& value) const+// The jar a tab's storage of this kind lives in: session storage belongs to the tab's traversable, local storage to+// its browsing session. Every DevTools edit goes through the jar, like every other write, so the pages mirroring the+// bottle hear of it the same way.+static ErrorOr<StorageJar*> storage_jar_for_devtools(ViewImplementation& view, Web::StorageAPI::StorageEndpointType storage_endpoint){- auto view = ViewImplementation::find_view_by_id(description.id);- if (!view.has_value())- return Error::from_string_literal("Unable to locate tab");-if (storage_endpoint == Web::StorageAPI::StorageEndpointType::SessionStorage) {- auto key_utf16 = Utf16String::from_utf8(key);- auto value_utf16 = Utf16String::from_utf8(value);- auto result = view->set_session_storage_item(key_utf16, value_utf16);- if (!result.has_value())+ if (!view.has_traversable())return Error::from_string_literal("Unable to locate session storage");- return TRY(storage_set_result_to_error_or_old_value(result.release_value()));+ return &view.traversable().session_storage();}+ return view.session().storage_jar.ptr();+}- auto key_utf16 = Utf16String::from_utf8(key);- auto value_utf16 = Utf16String::from_utf8(value);- auto old_value = TRY(storage_set_result_to_error_or_old_value(view->session().storage_jar->set_item(storage_endpoint, storage_key, key_utf16, value_utf16)));- if (!old_value.has_value()) {- view->notify_storage_changed({ storage_endpoint, storage_key, DevTools::DevToolsDelegate::StorageChange::Type::Added, key });- } else if (*old_value != value) {- view->notify_storage_changed({ storage_endpoint, storage_key, DevTools::DevToolsDelegate::StorageChange::Type::Changed, key });- }+ErrorOr<Optional<String>> Application::set_storage_item(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& storage_key, String const& key, String const& value) const+{+ auto view = ViewImplementation::find_view_by_id(description.id);+ if (!view.has_value())+ return Error::from_string_literal("Unable to locate tab");- return old_value;+ auto* storage_jar = TRY(storage_jar_for_devtools(*view, storage_endpoint));+ return TRY(storage_set_result_to_error_or_old_value(storage_jar->set_item(storage_endpoint, storage_key, Utf16String::from_utf8(key), Utf16String::from_utf8(value))));}ErrorOr<Optional<String>> Application::remove_storage_item(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& storage_key, String const& key) const@@ -3350,16 +3408,13 @@ ErrorOr<Optional<String>> Application::remove_storage_item(DevTools::TabDescriptif (!view.has_value())return Error::from_string_literal("Unable to locate tab");- if (storage_endpoint == Web::StorageAPI::StorageEndpointType::SessionStorage)- return view->remove_session_storage_item(Utf16String::from_utf8(key)).map([](auto const& old_value) { return old_value.to_utf8(); });-+ auto* storage_jar = TRY(storage_jar_for_devtools(*view, storage_endpoint));auto key_utf16 = Utf16String::from_utf8(key);- auto old_value = view->session().storage_jar->get_item(storage_endpoint, storage_key, key_utf16);+ auto old_value = storage_jar->get_item(storage_endpoint, storage_key, key_utf16);if (!old_value.has_value())return Optional<String> {};- view->session().storage_jar->remove_item(storage_endpoint, storage_key, key_utf16);- view->notify_storage_changed({ storage_endpoint, storage_key, DevTools::DevToolsDelegate::StorageChange::Type::Deleted, key });+ storage_jar->remove_item(storage_endpoint, storage_key, key_utf16);return old_value->to_utf8();}@@ -3369,17 +3424,8 @@ ErrorOr<void> Application::clear_storage(DevTools::TabDescription const& descripif (!view.has_value())return Error::from_string_literal("Unable to locate tab");- if (storage_endpoint == Web::StorageAPI::StorageEndpointType::SessionStorage) {- view->clear_session_storage();- return {};- }-- auto keys = view->session().storage_jar->get_all_keys(storage_endpoint, storage_key);- if (keys.is_empty())- return {};-- view->session().storage_jar->clear_storage_key(storage_endpoint, storage_key);- view->notify_storage_changed({ storage_endpoint, storage_key, DevTools::DevToolsDelegate::StorageChange::Type::Cleared, {} });+ auto* storage_jar = TRY(storage_jar_for_devtools(*view, storage_endpoint));+ storage_jar->clear_storage_key(storage_endpoint, storage_key);return {};}diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txtindex 134b390c00a..a7d8fe10e6e 100644--- a/Libraries/LibWebView/CMakeLists.txt+++ b/Libraries/LibWebView/CMakeLists.txt@@ -64,6 +64,7 @@ set(SOURCESSessionStore.cppSettings.cppSiteCompatibility.cpp+ StorageAreaRegistry.cppStorageJar.cppTestSpellChecker.cppURL.cppdiff --git a/Libraries/LibWebView/StorageAreaRegistry.cpp b/Libraries/LibWebView/StorageAreaRegistry.cppnew file mode 100644index 00000000000..bb4e3b7de81--- /dev/null+++ b/Libraries/LibWebView/StorageAreaRegistry.cpp@@ -0,0 +1,176 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibWebView/StorageAreaRegistry.h>++namespace WebView {++StorageAreaRegistry& StorageAreaRegistry::the()+{+ // Outlives the process, rather than running a destructor at exit.+ static auto* registry = new StorageAreaRegistry;+ return *registry;+}++void StorageAreaRegistry::remove_areas_of(Badge<StorageJar>, StorageJar& jar)+{+ auto areas = m_areas.take(&jar);+ if (!areas.has_value())+ return;+ for (auto const& entry : *areas) {+ for (auto const& listener_id : entry.value.listener_ids)+ m_live_connections.remove(listener_id.value);+ }+}++StorageAreaRegistry::Area* StorageAreaRegistry::area_for(StorageJar& jar, StorageBottleLocation const& location)+{+ auto areas = m_areas.get(&jar);+ if (!areas.has_value())+ return nullptr;+ auto area = areas->get(location);+ if (!area.has_value())+ return nullptr;+ return &*area;+}++Optional<StorageAreaRegistry::Connection> StorageAreaRegistry::connect(StorageAreaListener& listener, StorageJar& jar, StorageEndpointType storage_endpoint, String const& storage_key)+{+ if (storage_endpoint != StorageEndpointType::LocalStorage && storage_endpoint != StorageEndpointType::SessionStorage)+ return {};++ StorageBottleLocation location { storage_endpoint, storage_key };+ auto& area = m_areas.ensure(&jar).ensure(location, [&] { return Area { &jar, storage_endpoint, storage_key, {} }; });++ // A listener that connects again to an area it already mirrors has let its old mirror go; anything still+ // addressed to that one is dropped from here on.+ if (auto previous = area.listener_ids.get(&listener); previous.has_value())+ m_live_connections.remove(*previous);++ Web::StorageAPI::StorageAreaId id { m_next_id++ };+ area.listener_ids.set(&listener, id);+ m_live_connections.set(id, LiveConnection { &jar, location, &listener });+ m_issued_ids.ensure(&listener).set(id);++ jar.mark_accessed(storage_endpoint, storage_key);+ return Connection { id, jar.get_all_items(storage_endpoint, storage_key), jar.bottle_usage(storage_endpoint, storage_key) };+}++void StorageAreaRegistry::disconnect(StorageAreaListener& listener, Web::StorageAPI::StorageAreaId id)+{+ auto live = m_live_connections.get(id);+ if (!live.has_value() || live->listener != &listener)+ return;+ auto connection = *live;+ m_live_connections.remove(id);++ auto* area = area_for(*connection.jar, connection.location);+ if (!area)+ return;+ if (auto current = area->listener_ids.get(&listener); current.has_value() && *current == id)+ area->listener_ids.remove(&listener);+ if (!area->listener_ids.is_empty())+ return;++ // An area lives as long as something mirrors it.+ auto areas = m_areas.get(connection.jar);+ areas->remove(connection.location);+ if (areas->is_empty())+ m_areas.remove(connection.jar);+}++void StorageAreaRegistry::remove_listener(StorageAreaListener& listener)+{+ auto ids = m_issued_ids.take(&listener);+ if (!ids.has_value())+ return;+ for (auto id : *ids)+ disconnect(listener, id);+}++bool StorageAreaRegistry::was_issued_to(StorageAreaListener const& listener, Web::StorageAPI::StorageAreaId id) const+{+ auto ids = m_issued_ids.get(&listener);+ return ids.has_value() && ids->contains(id);+}++StorageAreaRegistry::Area* StorageAreaRegistry::connected_area(StorageAreaListener& listener, Web::StorageAPI::StorageAreaId id)+{+ auto live = m_live_connections.get(id);+ if (!live.has_value() || live->listener != &listener)+ return nullptr;+ return area_for(*live->jar, live->location);+}++void StorageAreaRegistry::set_item(StorageAreaListener& listener, Web::StorageAPI::StorageAreaId id, Utf16String const& key, Utf16String const& value, String const& url)+{+ auto* area = connected_area(listener, id);+ if (!area)+ return;++ m_current_write = CurrentWrite { &listener, url };+ auto result = area->jar->set_item(area->storage_endpoint, area->storage_key, key, value);+ m_current_write.clear();++ // The writer applied the write to its mirror before sending it, on the strength of the same quota rule — so a+ // refusal means another process filled the bottle in between, and the writer has to reload it to agree again.+ if (result.has<StorageOperationError>())+ listener.storage_area_write_refused(id, area->jar->get_all_items(area->storage_endpoint, area->storage_key));+ else+ listener.storage_area_write_acknowledged(id);+}++void StorageAreaRegistry::remove_item(StorageAreaListener& listener, Web::StorageAPI::StorageAreaId id, Utf16String const& key, String const& url)+{+ auto* area = connected_area(listener, id);+ if (!area)+ return;++ m_current_write = CurrentWrite { &listener, url };+ area->jar->remove_item(area->storage_endpoint, area->storage_key, key);+ m_current_write.clear();+ listener.storage_area_write_acknowledged(id);+}++void StorageAreaRegistry::clear(StorageAreaListener& listener, Web::StorageAPI::StorageAreaId id, String const& url)+{+ auto* area = connected_area(listener, id);+ if (!area)+ return;++ m_current_write = CurrentWrite { &listener, url };+ area->jar->clear_storage_key(area->storage_endpoint, area->storage_key);+ m_current_write.clear();+ listener.storage_area_write_acknowledged(id);+}++void StorageAreaRegistry::did_change(Badge<StorageJar>, StorageJar& jar, StorageChange const& change)+{+ Optional<String> writer_url;+ StorageAreaListener* writer = nullptr;+ if (m_current_write.has_value()) {+ writer = m_current_write->writer;+ writer_url = m_current_write->url;+ }++ if (auto* area = area_for(jar, { change.storage_endpoint, change.storage_key })) {+ // The writer already applied its own change; everyone else learns of it here, each under its own id.+ for (auto const& listener_id : area->listener_ids) {+ auto* listener = listener_id.key;+ if (listener == writer)+ continue;+ if (change.type == StorageChange::Type::Reset)+ listener->storage_area_reset(listener_id.value, jar.get_all_items(change.storage_endpoint, change.storage_key));+ else+ listener->storage_area_changed(listener_id.value, change.key, change.old_value, change.new_value, writer_url.value_or(change.storage_key));+ }+ }++ if (on_change_for_devtools)+ on_change_for_devtools(jar, change, writer_url);+}++}diff --git a/Libraries/LibWebView/StorageAreaRegistry.h b/Libraries/LibWebView/StorageAreaRegistry.hnew file mode 100644index 00000000000..f5c0dcd84ae--- /dev/null+++ b/Libraries/LibWebView/StorageAreaRegistry.h@@ -0,0 +1,111 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Badge.h>+#include <AK/Function.h>+#include <AK/HashMap.h>+#include <AK/HashTable.h>+#include <AK/Optional.h>+#include <AK/String.h>+#include <AK/Utf16String.h>+#include <AK/Vector.h>+#include <LibWebCommon/StorageAPI/StorageAreaId.h>+#include <LibWebView/Export.h>+#include <LibWebView/Forward.h>+#include <LibWebView/StorageJar.h>++namespace WebView {++// A process that mirrors a storage area. Every change to the area's bottle reaches it here, except one it made itself,+// which it hears acknowledged or refused instead.+class WEBVIEW_API StorageAreaListener {+public:+ virtual ~StorageAreaListener() = default;++ // A null key means the bottle was cleared. The url is the document URL of the page that made the change, or the+ // storage key when no page did.+ virtual void storage_area_changed(Web::StorageAPI::StorageAreaId, Optional<Utf16String> const& key, Optional<Utf16String> const& old_value, Optional<Utf16String> const& new_value, String const& url) = 0;+ virtual void storage_area_write_acknowledged(Web::StorageAPI::StorageAreaId) = 0;+ // A write the jar refused, with everything the bottle holds, so the writer can fall back in line with it.+ virtual void storage_area_write_refused(Web::StorageAPI::StorageAreaId, Vector<StorageItem>) = 0;+ // The bottle changed in a way not worth describing key by key; this is everything it holds now.+ virtual void storage_area_reset(Web::StorageAPI::StorageAreaId, Vector<StorageItem>) = 0;+};++// The storage areas the browser process hands out: one per bottle of one jar, each knowing the processes that mirror+// it. Every jar reports its changes here, so a change made anywhere — by a page, by DevTools, by clearing site data —+// reaches every mirror of the bottle it happened in. Nothing else has to remember to broadcast.+class WEBVIEW_API StorageAreaRegistry {+public:+ AK_ALLOC_WITH_KMALLOC;++ static StorageAreaRegistry& the();++ struct Connection {+ Web::StorageAPI::StorageAreaId id;+ Vector<StorageItem> items;+ u64 usage { 0 };+ };++ // Registers the listener at the exact point the items were read, so no change is missed or seen twice, and marks+ // the bottle accessed, since the listener's reads won't reach the jar. Every connection gets an id of its own,+ // even to an area others mirror too: a message meant for a connection that has since gone must never reach the+ // one that took its place. Only web storage is mirrored; asking for any other endpoint gets nothing, so no+ // process can write to a bottle whose endpoint has no quota.+ Optional<Connection> connect(StorageAreaListener&, StorageJar&, StorageEndpointType, String const& storage_key);+ void disconnect(StorageAreaListener&, Web::StorageAPI::StorageAreaId);+ // The listener's connection is gone.+ void remove_listener(StorageAreaListener&);++ // An area ID is a claim about the sender. One a listener was ever given stays valid for the listener's life: a+ // message it sent while the connection existed can arrive after it is gone, and such a message is dropped, not+ // held against it.+ bool was_issued_to(StorageAreaListener const&, Web::StorageAPI::StorageAreaId) const;++ // Writes from a listener. Each is dropped when the ID names no connection the listener still has.+ void set_item(StorageAreaListener&, Web::StorageAPI::StorageAreaId, Utf16String const& key, Utf16String const& value, String const& url);+ void remove_item(StorageAreaListener&, Web::StorageAPI::StorageAreaId, Utf16String const& key, String const& url);+ void clear(StorageAreaListener&, Web::StorageAPI::StorageAreaId, String const& url);++ // Every change to every jar, with the URL of the page that made it when a page did.+ Function<void(StorageJar&, StorageChange const&, Optional<String> const& url)> on_change_for_devtools;++ void did_change(Badge<StorageJar>, StorageJar&, StorageChange const&);+ void remove_areas_of(Badge<StorageJar>, StorageJar&);++private:+ struct Area {+ StorageJar* jar { nullptr };+ StorageEndpointType storage_endpoint;+ String storage_key;+ HashMap<StorageAreaListener*, Web::StorageAPI::StorageAreaId> listener_ids;+ };++ // What a live id names.+ struct LiveConnection {+ StorageJar* jar { nullptr };+ StorageBottleLocation location;+ StorageAreaListener* listener { nullptr };+ };++ struct CurrentWrite {+ StorageAreaListener* writer { nullptr };+ String url;+ };++ Area* area_for(StorageJar&, StorageBottleLocation const&);+ Area* connected_area(StorageAreaListener&, Web::StorageAPI::StorageAreaId);++ HashMap<StorageJar*, HashMap<StorageBottleLocation, Area>> m_areas;+ HashMap<Web::StorageAPI::StorageAreaId, LiveConnection> m_live_connections;+ HashMap<StorageAreaListener const*, HashTable<Web::StorageAPI::StorageAreaId>> m_issued_ids;+ Optional<CurrentWrite> m_current_write;+ u64 m_next_id { 1 };+};++}diff --git a/Libraries/LibWebView/StorageJar.cpp b/Libraries/LibWebView/StorageJar.cppindex 405dd2f1358..02a30a10edb 100644--- a/Libraries/LibWebView/StorageJar.cpp+++ b/Libraries/LibWebView/StorageJar.cpp@@ -8,6 +8,7 @@#include <AK/NonnullOwnPtr.h>#include <AK/StdLibExtras.h>#include <LibDatabase/Database.h>+#include <LibWebView/StorageAreaRegistry.h>#include <LibWebView/StorageJar.h>using Web::StorageAPI::quota_for;@@ -69,10 +70,15 @@ StorageJar::StorageJar(Optional<PersistedStorage> persisted_storage){}-StorageJar::~StorageJar() = default;+StorageJar::~StorageJar()+{+ StorageAreaRegistry::the().remove_areas_of({}, *this);+}void StorageJar::notify(StorageChange const& change){+ // The registry hears of every change to every jar, by construction: nothing installed and nothing to forget.+ StorageAreaRegistry::the().did_change({}, *this, change);if (on_change)on_change(change);}diff --git a/Libraries/LibWebView/StorageJar.h b/Libraries/LibWebView/StorageJar.hindex a1fee199dd1..6c3feb7962f 100644--- a/Libraries/LibWebView/StorageJar.h+++ b/Libraries/LibWebView/StorageJar.h@@ -75,8 +75,8 @@ public:~StorageJar();- // Called after every change this jar applies, whoever asked for it. Every write goes through the jar, so a mirror- // of a bottle that listens here hears about every change to it.+ // Called after every change this jar applies, whoever asked for it, after the StorageAreaRegistry has heard of+ // it. The registry is told directly, so nothing here can be unhooked by mistake.Function<void(StorageChange const&)> on_change;Optional<Utf16String> get_item(StorageEndpointType storage_endpoint, String const& storage_key, Utf16String const& bottle_key);diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex a206c7d1cef..4bfd32397c6 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -1620,21 +1620,6 @@ void ViewImplementation::inspect_storage(Web::StorageAPI::StorageEndpointType stpage().async_inspect_storage(storage_endpoint, request_id);}-Optional<StorageSetResult> ViewImplementation::set_session_storage_item(Utf16String const& key, Utf16String const& value)-{- return page().set_session_storage_item(key, value);-}--Optional<Utf16String> ViewImplementation::remove_session_storage_item(Utf16String const& key)-{- return page().remove_session_storage_item(key);-}--bool ViewImplementation::clear_session_storage()-{- return page().clear_session_storage();-}-void ViewImplementation::inspect_accessibility_tree(){page().async_inspect_accessibility_tree();diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex 82d36fbfedf..21ef78f482a 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -309,9 +309,6 @@ public:void inspect_dom_tree();void inspect_storage(Web::StorageAPI::StorageEndpointType, u64 request_id);- Optional<StorageSetResult> set_session_storage_item(Utf16String const& key, Utf16String const& value);- Optional<Utf16String> remove_session_storage_item(Utf16String const& key);- bool clear_session_storage();void inspect_accessibility_tree();void request_accessibility_tree();void perform_accessibility_action(i64 node_id, String action);diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex 81beb174e43..79ff84ca9e4 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -126,6 +126,7 @@ WebContentClient::~WebContentClient()page.value->close();cancel_navigation_transactions();remove_blob_url_entries();+ StorageAreaRegistry::the().remove_listener(*this);WorkerProcessManager::the().remove_web_content_owner(*this);GamepadManager::the().client_disconnected(*this);clients().remove(this);@@ -392,6 +393,11 @@ bool WebContentClient::may_act_for_page(Web::PageId page_id) constreturn m_pages.contains(page_id) || m_unassigned_initial_page_id == page_id;}+bool WebContentClient::may_act_for_storage_area(Web::StorageAPI::StorageAreaId area_id) const+{+ return StorageAreaRegistry::the().was_issued_to(*this, area_id);+}+Optional<CanonicalNavigable&> WebContentClient::hosted_navigable(Web::HTML::CrossProcessId navigable_id){Optional<CanonicalNavigable&> result;@@ -538,6 +544,7 @@ void WebContentClient::did_lose_process()cancel_navigation_transactions();fail_renderer_owned_downloads();remove_blob_url_entries();+ StorageAreaRegistry::the().remove_listener(*this);if (requested_close)return;@@ -696,6 +703,71 @@ Messages::WebContentClient::DidRequestStorageUsageResponse WebContentClient::didreturn 0u;}+Messages::WebContentClient::ConnectToStorageAreaResponse WebContentClient::connect_to_storage_area(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id)+{+ if (auto* page = this->page(page_id))+ return page->connect_to_storage_area(storage_endpoint, move(environment_id));++ // A closed page has no storage to connect to; the mirror stays detached.+ return { {}, {}, {}, 0 };+}++void WebContentClient::disconnect_from_storage_area(Web::StorageAPI::StorageAreaId area_id)+{+ StorageAreaRegistry::the().disconnect(*this, area_id);+}++void WebContentClient::set_storage_item(Web::StorageAPI::StorageAreaId area_id, Utf16String key, Utf16String value, String url)+{+ StorageAreaRegistry::the().set_item(*this, area_id, key, value, url);+}++void WebContentClient::remove_storage_item(Web::StorageAPI::StorageAreaId area_id, Utf16String key, String url)+{+ StorageAreaRegistry::the().remove_item(*this, area_id, key, url);+}++void WebContentClient::clear_storage_area(Web::StorageAPI::StorageAreaId area_id, String url)+{+ StorageAreaRegistry::the().clear(*this, area_id, url);+}++static void split_items(Vector<StorageItem> const& items, Vector<Utf16String>& keys, Vector<Utf16String>& values)+{+ keys.ensure_capacity(items.size());+ values.ensure_capacity(items.size());+ for (auto const& item : items) {+ keys.unchecked_append(item.key);+ values.unchecked_append(item.value);+ }+}++void WebContentClient::storage_area_changed(Web::StorageAPI::StorageAreaId area_id, Optional<Utf16String> const& key, Optional<Utf16String> const& old_value, Optional<Utf16String> const& new_value, String const& url)+{+ async_storage_area_changed(area_id, key, old_value, new_value, url);+}++void WebContentClient::storage_area_write_acknowledged(Web::StorageAPI::StorageAreaId area_id)+{+ async_storage_area_write_acknowledged(area_id);+}++void WebContentClient::storage_area_write_refused(Web::StorageAPI::StorageAreaId area_id, Vector<StorageItem> items)+{+ Vector<Utf16String> keys;+ Vector<Utf16String> values;+ split_items(items, keys, values);+ async_storage_area_write_refused(area_id, move(keys), move(values));+}++void WebContentClient::storage_area_reset(Web::StorageAPI::StorageAreaId area_id, Vector<StorageItem> items)+{+ Vector<Utf16String> keys;+ Vector<Utf16String> values;+ split_items(items, keys, values);+ async_storage_area_reset(area_id, move(keys), move(values));+}+Messages::WebContentClient::DidStartDownloadWithoutRequestResponse WebContentClient::did_start_download_without_request(Web::PageId page_id, URL::URL url, ByteString suggested_filename, Optional<u64> total_size){if (auto* page = this->page(page_id))diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.hindex c7e227a4268..9ee824b372d 100644--- a/Libraries/LibWebView/WebContentClient.h+++ b/Libraries/LibWebView/WebContentClient.h@@ -59,6 +59,7 @@#include <LibWebView/ChildIdentity.h>#include <LibWebView/Forward.h>#include <LibWebView/RequestServerSiteBindings.h>+#include <LibWebView/StorageAreaRegistry.h>#include <LibWebView/WebContentPage.h>#include <WebContent/WebContentClientEndpoint.h>#include <WebContent/WebContentServerEndpoint.h>@@ -69,7 +70,8 @@ class ViewImplementation;class WEBVIEW_API WebContentClient final: public IPC::ConnectionToServer<WebContentClientEndpoint, WebContentServerEndpoint, WebContentClientPageRoutingStub>- , public WebContentClientEndpoint {+ , public WebContentClientEndpoint+ , public StorageAreaListener {C_OBJECT_ABSTRACT(WebContentClient);friend class WebContentTestClient;@@ -142,6 +144,9 @@ public:// True for every page ID the UI process has handed to this connection, closed pages included, since a// message the connection sent while it had the page can arrive after the page is gone.virtual bool may_act_for_page(Web::PageId page_id) const override;+ // True for every storage area ID the UI process has handed to this connection, torn-down areas included, for+ // the same reason.+ virtual bool may_act_for_storage_area(Web::StorageAPI::StorageAreaId) const override;Optional<u64> exclusive_performance_owner() const;@@ -188,6 +193,17 @@ private:virtual Messages::WebContentClient::DidRequestStorageItemResponse did_request_storage_item(Web::PageId page_id, Web::StorageAPI::StorageEndpointType, Web::HTML::EnvironmentId environment_id, Utf16String bottle_key) override;virtual Messages::WebContentClient::DidRequestStorageKeysResponse did_request_storage_keys(Web::PageId page_id, Web::StorageAPI::StorageEndpointType, Web::HTML::EnvironmentId environment_id) override;virtual Messages::WebContentClient::DidRequestStorageUsageResponse did_request_storage_usage(Web::PageId page_id, Web::HTML::EnvironmentId environment_id) override;+ virtual Messages::WebContentClient::ConnectToStorageAreaResponse connect_to_storage_area(Web::PageId page_id, Web::StorageAPI::StorageEndpointType, Web::HTML::EnvironmentId environment_id) override;+ virtual void disconnect_from_storage_area(Web::StorageAPI::StorageAreaId) override;+ virtual void set_storage_item(Web::StorageAPI::StorageAreaId, Utf16String key, Utf16String value, String url) override;+ virtual void remove_storage_item(Web::StorageAPI::StorageAreaId, Utf16String key, String url) override;+ virtual void clear_storage_area(Web::StorageAPI::StorageAreaId, String url) override;++ // StorageAreaListener: what the registry tells this process about the areas it mirrors.+ virtual void storage_area_changed(Web::StorageAPI::StorageAreaId, Optional<Utf16String> const& key, Optional<Utf16String> const& old_value, Optional<Utf16String> const& new_value, String const& url) override;+ virtual void storage_area_write_acknowledged(Web::StorageAPI::StorageAreaId) override;+ virtual void storage_area_write_refused(Web::StorageAPI::StorageAreaId, Vector<StorageItem>) override;+ virtual void storage_area_reset(Web::StorageAPI::StorageAreaId, Vector<StorageItem>) override;virtual Messages::WebContentClient::DidStartDownloadWithoutRequestResponse did_start_download_without_request(Web::PageId page_id, URL::URL, ByteString suggested_filename, Optional<u64> total_size) override;virtual Messages::WebContentClient::DidStartDownloadResponse did_start_download(Web::PageId page_id, Web::HTML::CrossProcessId navigable_id, Optional<Utf16String> navigation_id, URL::URL, ByteString suggested_filename, Optional<u64> total_size, int request_server_client_id, u64 request_server_request_id, ByteBuffer initial_data) override;virtual Messages::WebContentClient::DidRequestNewWebViewResponse did_request_new_web_view(Web::PageId page_id, Web::HTML::ActivateTab, Web::HTML::WebViewHints, Optional<Web::HTML::CrossProcessId> opener_navigable_id, Optional<URL::URL> opener_base_url, Utf16String target_name, Web::HTML::SandboxingFlagSet popup_sandboxing_flag_set) override;diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 0c98737d458..5f612c2e266 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -10,7 +10,6 @@#include <LibCore/ElapsedTimer.h>#include <LibCore/EventLoop.h>#include <LibCore/StatsLog.h>-#include <LibDevTools/StorageHelpers.h>#include <LibHTTP/Cookie/ParsedCookie.h>#include <LibWebCommon/Geolocation/GeolocationPositionErrorCode.h>#include <LibWebCommon/HTML/BrowsingContext.h>@@ -31,6 +30,7 @@#include <LibWebView/NavigationLoader.h>#include <LibWebView/PictureInPictureManager.h>#include <LibWebView/RequestServerManager.h>+#include <LibWebView/StorageAreaRegistry.h>#include <LibWebView/StorageJar.h>#include <LibWebView/URL.h>#include <LibWebView/Utilities.h>@@ -1284,6 +1284,28 @@ void WebContentPage::did_clear_storage(Web::StorageAPI::StorageEndpointType storstorage_jar->clear_storage_key(storage_endpoint, *canonical_key);}+Messages::WebContentClient::ConnectToStorageAreaResponse WebContentPage::connect_to_storage_area(Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id)+{+ auto canonical_key = canonical_storage_key(environment_id);+ auto* storage_jar = this->storage_jar(storage_endpoint);+ if (!canonical_key.has_value() || !storage_jar)+ return { {}, {}, {}, 0 };++ auto connection = StorageAreaRegistry::the().connect(client(), *storage_jar, storage_endpoint, *canonical_key);+ if (!connection.has_value())+ return { {}, {}, {}, 0 };++ Vector<Utf16String> keys;+ Vector<Utf16String> values;+ keys.ensure_capacity(connection->items.size());+ values.ensure_capacity(connection->items.size());+ for (auto& item : connection->items) {+ keys.unchecked_append(move(item.key));+ values.unchecked_append(move(item.value));+ }+ return { connection->id, move(keys), move(values), connection->usage };+}+void WebContentPage::did_change_needs_beforeunload_check(bool needs_beforeunload_check){m_needs_beforeunload_check = needs_beforeunload_check;@@ -1908,32 +1930,6 @@ void WebContentPage::did_receive_network_response_headers(u64 request_id, u32 st}}-void WebContentPage::did_change_storage_item(Web::StorageAPI::StorageEndpointType storage_endpoint, String url, Optional<Utf16String> key, Optional<Utf16String> old_value, Optional<Utf16String> new_value)-{- if (displays_tab()) {- auto host = DevTools::storage_host_for_url(url);- if (!host.has_value())- return;-- DevTools::DevToolsDelegate::StorageChange::Type type;- if (!key.has_value())- type = DevTools::DevToolsDelegate::StorageChange::Type::Cleared;- else if (!old_value.has_value())- type = DevTools::DevToolsDelegate::StorageChange::Type::Added;- else if (!new_value.has_value())- type = DevTools::DevToolsDelegate::StorageChange::Type::Deleted;- else- type = DevTools::DevToolsDelegate::StorageChange::Type::Changed;-- view().notify_storage_changed({- .storage_endpoint = storage_endpoint,- .host = host.release_value(),- .type = type,- .key = key.has_value() ? Optional<String> { key->to_utf8() } : Optional<String> {},- });- }-}-void WebContentPage::did_update_indexed_database(String update){if (displays_tab())diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex 689b3fba4a3..afe7818da58 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -236,6 +236,7 @@ private:virtual void did_remove_storage_item(Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id, Utf16String bottle_key) override;Messages::WebContentClient::DidRequestStorageKeysResponse did_request_storage_keys(Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id);virtual void did_clear_storage(Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id) override;+ Messages::WebContentClient::ConnectToStorageAreaResponse connect_to_storage_area(Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id);virtual void did_change_needs_beforeunload_check(bool needs_beforeunload_check) override;virtual void did_consume_user_activation(Web::HTML::UserActivationConsumption consumption) override;virtual void webdriver_user_prompt_handling_complete(u64 request_id, Web::WebDriver::Response response) override;@@ -296,7 +297,6 @@ private:virtual void did_get_dom_node_html(String html) override;virtual void did_resolve_dom_node_url(u64 request_id, String resolved_url) override;virtual void did_receive_network_response_headers(u64 request_id, u32 status_code, Optional<String> reason_phrase, Vector<HTTP::Header> response_headers, Requests::CacheState cache_state) override;- virtual void did_change_storage_item(Web::StorageAPI::StorageEndpointType storage_endpoint, String url, Optional<Utf16String> key, Optional<Utf16String> old_value, Optional<Utf16String> new_value) override;virtual void did_update_indexed_database(String update) override;virtual void did_request_clipboard_entries(u64 request_id) override;virtual void did_request_spell_check(u64 request_id, Utf16String text) override;diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cppindex d80d3324a6f..1d29b8f158a 100644--- a/Services/WebContent/ConnectionFromClient.cpp+++ b/Services/WebContent/ConnectionFromClient.cpp@@ -1246,65 +1246,20 @@ void ConnectionFromClient::inspect_storage(Web::PageId page_id, Web::StorageAPI:async_did_inspect_storage(page_id, request_id, storage_items.serialized());}-static Optional<GC::Ref<Web::HTML::Storage>> active_session_storage_for_page(PageClient& page)+void ConnectionFromClient::storage_area_changed(Web::StorageAPI::StorageAreaId, Optional<Utf16String>, Optional<Utf16String>, Optional<Utf16String>, String){- auto document = page.page().local_traversable()->active_document();- if (!document || !document->window())- return {};-- auto storage_or_error = document->window()->session_storage();- if (storage_or_error.is_exception())- return {};-- return storage_or_error.release_value();}-Messages::WebContentServer::SetSessionStorageItemResponse ConnectionFromClient::set_session_storage_item(Web::PageId page_id, Utf16String key, Utf16String value)+void ConnectionFromClient::storage_area_write_acknowledged(Web::StorageAPI::StorageAreaId){- auto page = this->page(page_id);- if (!page.has_value())- return Optional<WebView::StorageSetResult> {};-- auto storage = active_session_storage_for_page(*page);- if (!storage.has_value())- return Optional<WebView::StorageSetResult> {};-- auto old_value = (*storage)->get_item(key);- auto result = (*storage)->set_item(key, value);- if (result.is_exception())- return WebView::StorageSetResult { WebView::StorageOperationError::QuotaExceededError };-- return WebView::StorageSetResult { move(old_value) };}-Messages::WebContentServer::RemoveSessionStorageItemResponse ConnectionFromClient::remove_session_storage_item(Web::PageId page_id, Utf16String key)+void ConnectionFromClient::storage_area_write_refused(Web::StorageAPI::StorageAreaId, Vector<Utf16String>, Vector<Utf16String>){- auto page = this->page(page_id);- if (!page.has_value())- return Optional<Utf16String> {};-- auto storage = active_session_storage_for_page(*page);- if (!storage.has_value())- return Optional<Utf16String> {};-- auto old_value = (*storage)->get_item(key);- if (old_value.has_value())- (*storage)->remove_item(key);- return old_value;}-Messages::WebContentServer::ClearSessionStorageResponse ConnectionFromClient::clear_session_storage(Web::PageId page_id)+void ConnectionFromClient::storage_area_reset(Web::StorageAPI::StorageAreaId, Vector<Utf16String>, Vector<Utf16String>){- auto page = this->page(page_id);- if (!page.has_value())- return false;-- auto storage = active_session_storage_for_page(*page);- if (!storage.has_value() || (*storage)->length() == 0)- return false;-- (*storage)->clear();- return true;}void ConnectionFromClient::inspect_dom_node(Web::PageId page_id, WebView::DOMNodeProperties::Type property_type, Web::UniqueNodeID node_id, Optional<Web::CSS::PseudoElement> pseudo_element, JsonValue options_value)diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.hindex 5c4ec5e1a17..22886c1f3af 100644--- a/Services/WebContent/ConnectionFromClient.h+++ b/Services/WebContent/ConnectionFromClient.h@@ -169,9 +169,10 @@ private:virtual void get_source(Web::PageId page_id) override;virtual void inspect_dom_tree(Web::PageId page_id) override;virtual void inspect_storage(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, u64 request_id) override;- virtual Messages::WebContentServer::SetSessionStorageItemResponse set_session_storage_item(Web::PageId page_id, Utf16String key, Utf16String value) override;- virtual Messages::WebContentServer::RemoveSessionStorageItemResponse remove_session_storage_item(Web::PageId page_id, Utf16String key) override;- virtual Messages::WebContentServer::ClearSessionStorageResponse clear_session_storage(Web::PageId page_id) override;+ virtual void storage_area_changed(Web::StorageAPI::StorageAreaId area_id, Optional<Utf16String> key, Optional<Utf16String> old_value, Optional<Utf16String> new_value, String url) override;+ virtual void storage_area_write_acknowledged(Web::StorageAPI::StorageAreaId area_id) override;+ virtual void storage_area_write_refused(Web::StorageAPI::StorageAreaId area_id, Vector<Utf16String> keys, Vector<Utf16String> values) override;+ virtual void storage_area_reset(Web::StorageAPI::StorageAreaId area_id, Vector<Utf16String> keys, Vector<Utf16String> values) override;virtual void inspect_dom_node(Web::PageId page_id, WebView::DOMNodeProperties::Type, Web::UniqueNodeID node_id, Optional<Web::CSS::PseudoElement> pseudo_element, JsonValue options) override;virtual void inspect_grid_layouts(Web::PageId page_id, Web::UniqueNodeID root_node_id) override;virtual void inspect_current_grid(Web::PageId page_id, Web::UniqueNodeID node_id) override;diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex de11fd8a7b0..f8d14ab4387 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -1601,11 +1601,6 @@ void PageClient::page_did_clear_storage(Web::StorageAPI::StorageEndpointType sto}}-void PageClient::page_did_broadcast_storage_change(Web::StorageAPI::StorageEndpointType storage_endpoint, String const& url, Optional<Utf16String> const& key, Optional<Utf16String> const& old_value, Optional<Utf16String> const& new_value)-{- client().async_did_change_storage_item(m_id, storage_endpoint, url, key, old_value, new_value);-}-void PageClient::page_did_update_indexed_database(String const& url, Web::IndexedDB::TransactionChanges const& changes){if (!has_devtools_client())diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.hindex 5aad954ba97..743a44ff476 100644--- a/Services/WebContent/PageClient.h+++ b/Services/WebContent/PageClient.h@@ -305,7 +305,6 @@ private:virtual Vector<Utf16String> page_did_request_storage_keys(Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId const& environment_id) override;virtual u64 page_did_request_storage_usage(Web::HTML::EnvironmentId const& environment_id) override;virtual void page_did_clear_storage(Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId const& environment_id) override;- virtual void page_did_broadcast_storage_change(Web::StorageAPI::StorageEndpointType storage_endpoint, String const& url, Optional<Utf16String> const& key, Optional<Utf16String> const& old_value, Optional<Utf16String> const& new_value) override;virtual void page_did_update_indexed_database(String const& url, Web::IndexedDB::TransactionChanges const&) override;virtual void page_did_update_resource_count(i32) override;virtual NewWebViewResult page_did_request_new_web_view(Web::HTML::ActivateTab, Web::HTML::WebViewHints, Optional<Web::HTML::CrossProcessId> opener_navigable_id, Optional<URL::URL> opener_base_url, Utf16String const& target_name, Web::HTML::SandboxingFlagSet popup_sandboxing_flag_set) override;diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipcindex 3a70b0a3a61..0ba2c828e06 100644--- a/Services/WebContent/WebContentClient.ipc+++ b/Services/WebContent/WebContentClient.ipc@@ -61,6 +61,7 @@#include <LibWebCommon/Page/ScreenWakeLockState.h>#include <LibWebCommon/Page/SpellingError.h>#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>+#include <LibWebCommon/StorageAPI/StorageAreaId.h>#include <LibWebCommon/StorageAPI/StorageEndpoint.h>#include <LibWebCommon/WebDriver/Response.h>#include <LibWebCommon/WebView/AccessibilityNodeData.h>@@ -202,7 +203,11 @@ endpoint WebContentClient routed by Web::PageId page_id[NotRouted] did_request_storage_keys(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id) => (Vector<Utf16String> keys)did_clear_storage(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id) => ()[NotRouted] did_request_storage_usage(Web::PageId page_id, Web::HTML::EnvironmentId environment_id) => (u64 usage)- did_change_storage_item(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, String url, Optional<Utf16String> key, Optional<Utf16String> old_value, Optional<Utf16String> new_value) =|+ [NotRouted] connect_to_storage_area(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id) => (Optional<Web::StorageAPI::StorageAreaId> area_id, Vector<Utf16String> keys, Vector<Utf16String> values, u64 usage)+ disconnect_from_storage_area(Web::StorageAPI::StorageAreaId area_id) =|+ set_storage_item(Web::StorageAPI::StorageAreaId area_id, Utf16String key, Utf16String value, String url) =|+ remove_storage_item(Web::StorageAPI::StorageAreaId area_id, Utf16String key, String url) =|+ clear_storage_area(Web::StorageAPI::StorageAreaId area_id, String url) =|did_update_indexed_database(Web::PageId page_id, String update) =|did_post_broadcast_channel_message(Web::PageId page_id, Web::HTML::PostedBroadcastChannelMessage message) =|diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipcindex ebe459af163..60433949752 100644--- a/Services/WebContent/WebContentServer.ipc+++ b/Services/WebContent/WebContentServer.ipc@@ -50,6 +50,7 @@#include <LibWebCommon/Page/SpellingError.h>#include <LibWebCommon/Page/ViewportIsFullscreen.h>#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>+#include <LibWebCommon/StorageAPI/StorageAreaId.h>#include <LibWebCommon/StorageAPI/StorageEndpoint.h>#include <LibWebCommon/WebDriver/Capabilities.h>#include <LibWebCommon/WebDriver/Response.h>@@ -155,9 +156,10 @@ endpoint WebContentServer routed by Web::PageId page_idget_source(Web::PageId page_id) =|inspect_dom_tree(Web::PageId page_id) =|inspect_storage(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, u64 request_id) =|- [NotRouted] set_session_storage_item(Web::PageId page_id, Utf16String key, Utf16String value) => (Optional<WebView::StorageSetResult> result)- [NotRouted] remove_session_storage_item(Web::PageId page_id, Utf16String key) => (Optional<Utf16String> old_value)- [NotRouted] clear_session_storage(Web::PageId page_id) => (bool did_clear)+ storage_area_changed(Web::StorageAPI::StorageAreaId area_id, Optional<Utf16String> key, Optional<Utf16String> old_value, Optional<Utf16String> new_value, String url) =|+ storage_area_write_acknowledged(Web::StorageAPI::StorageAreaId area_id) =|+ storage_area_write_refused(Web::StorageAPI::StorageAreaId area_id, Vector<Utf16String> keys, Vector<Utf16String> values) =|+ storage_area_reset(Web::StorageAPI::StorageAreaId area_id, Vector<Utf16String> keys, Vector<Utf16String> values) =|inspect_dom_node(Web::PageId page_id, WebView::DOMNodeProperties::Type property_type, Web::UniqueNodeID node_id, Optional<Web::CSS::PseudoElement> pseudo_element, JsonValue options) =|inspect_grid_layouts(Web::PageId page_id, Web::UniqueNodeID root_node_id) =|inspect_current_grid(Web::PageId page_id, Web::UniqueNodeID node_id) =|diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txtindex 460e2123778..4df37e664fe 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -23,6 +23,7 @@ set(TEST_SOURCESTestSessionStore.cppTestSiteCompatibilityDataLoading.cppTestStartupSettings.cpp+ TestStorageAreaRegistry.cppTestStorageJar.cppTestTabPerformanceStats.cppTestWebViewURL.cppdiff --git a/Tests/LibWebView/TestStorageAreaRegistry.cpp b/Tests/LibWebView/TestStorageAreaRegistry.cppnew file mode 100644index 00000000000..5de44c1c269--- /dev/null+++ b/Tests/LibWebView/TestStorageAreaRegistry.cpp@@ -0,0 +1,368 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/Vector.h>+#include <LibCore/System.h>+#include <LibTest/TestCase.h>+#include <LibWebView/StorageAreaRegistry.h>+#include <LibWebView/StorageJar.h>++using Web::StorageAPI::StorageAreaId;+using WebView::StorageEndpointType;++namespace {++struct Event {+ enum class Kind : u8 {+ Changed,+ Acknowledged,+ Refused,+ Reset,+ };++ Kind kind;+ StorageAreaId area_id;+ Optional<Utf16String> key;+ Optional<Utf16String> old_value;+ Optional<Utf16String> new_value;+ String url;+ Vector<WebView::StorageItem> items;+};++class FakeListener final : public WebView::StorageAreaListener {+public:+ Vector<Event> events;++ virtual void storage_area_changed(StorageAreaId area_id, Optional<Utf16String> const& key, Optional<Utf16String> const& old_value, Optional<Utf16String> const& new_value, String const& url) override+ {+ events.append({ Event::Kind::Changed, area_id, key, old_value, new_value, url, {} });+ }+ virtual void storage_area_write_acknowledged(StorageAreaId area_id) override+ {+ events.append({ Event::Kind::Acknowledged, area_id, {}, {}, {}, {}, {} });+ }+ virtual void storage_area_write_refused(StorageAreaId area_id, Vector<WebView::StorageItem> items) override+ {+ events.append({ Event::Kind::Refused, area_id, {}, {}, {}, {}, move(items) });+ }+ virtual void storage_area_reset(StorageAreaId area_id, Vector<WebView::StorageItem> items) override+ {+ events.append({ Event::Kind::Reset, area_id, {}, {}, {}, {}, move(items) });+ }+};++auto const KEY = "https://example.com"_string;+auto const URL = "https://example.com/page.html"_string;++}++TEST_CASE(connect_returns_items_and_shares_the_area_between_listeners)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ jar->set_item(StorageEndpointType::LocalStorage, KEY, "a"_utf16, "1"_utf16);++ FakeListener first;+ FakeListener second;+ auto first_connection = registry.connect(first, *jar, StorageEndpointType::LocalStorage, KEY).value();+ auto second_connection = registry.connect(second, *jar, StorageEndpointType::LocalStorage, KEY).value();++ // Every connection gets an id of its own, even to one area, so a message meant for a connection that has since+ // gone can never reach the one that took its place.+ EXPECT(first_connection.id != second_connection.id);+ EXPECT_EQ(first_connection.items.size(), 1u);+ EXPECT_EQ(first_connection.items[0].key, "a"_utf16);+ EXPECT_EQ(first_connection.items[0].value, "1"_utf16);+ EXPECT_EQ(first_connection.usage, 2u);+ EXPECT(registry.was_issued_to(first, first_connection.id));+ EXPECT(registry.was_issued_to(second, second_connection.id));+ EXPECT(!registry.was_issued_to(second, first_connection.id));++ // A different bottle of the same jar is a different area.+ auto session_connection = registry.connect(first, *jar, StorageEndpointType::SessionStorage, KEY).value();+ EXPECT(session_connection.id != first_connection.id);+ EXPECT(session_connection.items.is_empty());+}++TEST_CASE(write_fans_out_to_others_and_acks_the_writer)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ FakeListener a;+ FakeListener b;+ FakeListener elsewhere;+ auto id = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ auto b_id = registry.connect(b, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ (void)registry.connect(elsewhere, *jar, StorageEndpointType::LocalStorage, "https://other.example"_string).value();++ registry.set_item(a, id, "k"_utf16, "v"_utf16, URL);++ EXPECT_EQ(a.events.size(), 1u);+ EXPECT_EQ(a.events[0].kind, Event::Kind::Acknowledged);+ EXPECT_EQ(a.events[0].area_id, id);++ EXPECT_EQ(b.events.size(), 1u);+ EXPECT_EQ(b.events[0].kind, Event::Kind::Changed);+ EXPECT_EQ(b.events[0].area_id, b_id);+ EXPECT_EQ(b.events[0].key, Optional<Utf16String> { "k"_utf16 });+ EXPECT_EQ(b.events[0].old_value, Optional<Utf16String> {});+ EXPECT_EQ(b.events[0].new_value, Optional<Utf16String> { "v"_utf16 });+ EXPECT_EQ(b.events[0].url, URL);++ EXPECT(elsewhere.events.is_empty());+ EXPECT_EQ(jar->get_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16), Optional<Utf16String> { "v"_utf16 });++ // A write of the value already there changes nothing, but the writer still hears it was handled.+ registry.set_item(a, id, "k"_utf16, "v"_utf16, URL);+ EXPECT_EQ(a.events.size(), 2u);+ EXPECT_EQ(a.events[1].kind, Event::Kind::Acknowledged);+ EXPECT_EQ(b.events.size(), 1u);++ registry.remove_item(a, id, "k"_utf16, URL);+ EXPECT_EQ(b.events.size(), 2u);+ EXPECT_EQ(b.events[1].kind, Event::Kind::Changed);+ EXPECT_EQ(b.events[1].old_value, Optional<Utf16String> { "v"_utf16 });+ EXPECT_EQ(b.events[1].new_value, Optional<Utf16String> {});++ registry.set_item(b, b_id, "k"_utf16, "w"_utf16, URL);+ registry.clear(a, id, URL);+ EXPECT_EQ(b.events.size(), 4u);+ EXPECT_EQ(b.events[3].kind, Event::Kind::Changed);+ EXPECT_EQ(b.events[3].key, Optional<Utf16String> {});+ EXPECT_EQ(a.events.size(), 5u);+ EXPECT_EQ(a.events[4].kind, Event::Kind::Acknowledged);+}++TEST_CASE(refused_write_sends_the_snapshot_to_the_writer_only)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ FakeListener a;+ FakeListener b;+ auto id = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ auto b_id = registry.connect(b, *jar, StorageEndpointType::LocalStorage, KEY).value().id;++ auto big = Utf16String::repeated('x', Web::StorageAPI::StorageEndpoint::LOCAL_STORAGE_QUOTA - 8);+ registry.set_item(b, b_id, "big"_utf16, big, URL);+ a.events.clear();+ b.events.clear();++ registry.set_item(a, id, "more"_utf16, "xxxxxxxx"_utf16, URL);++ EXPECT_EQ(a.events.size(), 1u);+ EXPECT_EQ(a.events[0].kind, Event::Kind::Refused);+ EXPECT_EQ(a.events[0].area_id, id);+ EXPECT_EQ(a.events[0].items.size(), 1u);+ EXPECT_EQ(a.events[0].items[0].key, "big"_utf16);+ EXPECT(b.events.is_empty());+}++TEST_CASE(browser_side_changes_reach_every_listener_with_the_storage_key_as_url)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ FakeListener a;+ FakeListener b;+ auto id = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ auto b_id = registry.connect(b, *jar, StorageEndpointType::LocalStorage, KEY).value().id;++ jar->set_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16, "v"_utf16);+ EXPECT_EQ(a.events.size(), 1u);+ EXPECT_EQ(b.events.size(), 1u);+ EXPECT_EQ(a.events[0].kind, Event::Kind::Changed);+ EXPECT_EQ(a.events[0].area_id, id);+ EXPECT_EQ(b.events[0].area_id, b_id);+ EXPECT_EQ(a.events[0].url, KEY);++ jar->clear_storage_key(StorageEndpointType::LocalStorage, KEY);+ EXPECT_EQ(a.events.size(), 2u);+ EXPECT_EQ(a.events[1].kind, Event::Kind::Changed);+ EXPECT_EQ(a.events[1].key, Optional<Utf16String> {});+ EXPECT_EQ(b.events.size(), 2u);+}++TEST_CASE(reset_after_removing_items_accessed_since_reaches_every_listener)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ jar->set_item(StorageEndpointType::LocalStorage, KEY, "kept"_utf16, "1"_utf16);+ FakeListener a;+ FakeListener b;+ auto id = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ (void)registry.connect(b, *jar, StorageEndpointType::LocalStorage, KEY).value();+ a.events.clear();+ b.events.clear();++ jar->remove_items_accessed_since(UnixDateTime::earliest());++ EXPECT_EQ(a.events.size(), 1u);+ EXPECT_EQ(a.events[0].kind, Event::Kind::Reset);+ EXPECT_EQ(a.events[0].area_id, id);+ EXPECT(a.events[0].items.is_empty());+ EXPECT_EQ(b.events.size(), 1u);+ EXPECT_EQ(b.events[0].kind, Event::Kind::Reset);+}++TEST_CASE(a_removed_listener_hears_nothing_more)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ FakeListener a;+ FakeListener b;+ auto id = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ (void)registry.connect(b, *jar, StorageEndpointType::LocalStorage, KEY).value();++ registry.remove_listener(b);+ registry.set_item(a, id, "k"_utf16, "v"_utf16, URL);++ EXPECT(b.events.is_empty());+ EXPECT_EQ(a.events.size(), 1u);+ EXPECT(!registry.was_issued_to(b, id));+}++TEST_CASE(a_disconnected_listener_hears_nothing_more_but_keeps_its_claim)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ FakeListener a;+ FakeListener b;+ auto id = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ auto b_id = registry.connect(b, *jar, StorageEndpointType::LocalStorage, KEY).value().id;++ registry.disconnect(b, b_id);+ registry.set_item(a, id, "k"_utf16, "v"_utf16, URL);++ EXPECT(b.events.is_empty());+ EXPECT(registry.was_issued_to(b, b_id));++ // A write from a listener that has disconnected is dropped, not applied.+ registry.set_item(b, b_id, "k"_utf16, "stale"_utf16, URL);+ EXPECT_EQ(jar->get_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16), Optional<Utf16String> { "v"_utf16 });+ EXPECT(b.events.is_empty());+}++TEST_CASE(writes_for_a_torn_down_area_are_dropped_and_the_claim_stays_valid)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ FakeListener a;+ StorageAreaId id { 0 };+ {+ auto jar = WebView::StorageJar::create();+ id = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ }++ EXPECT(registry.was_issued_to(a, id));+ registry.set_item(a, id, "k"_utf16, "v"_utf16, URL);+ registry.remove_item(a, id, "k"_utf16, URL);+ registry.clear(a, id, URL);+ registry.disconnect(a, id);+ EXPECT(a.events.is_empty());+}++TEST_CASE(reconnecting_gets_a_fresh_id_and_a_message_for_the_old_one_is_dropped)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ FakeListener a;+ FakeListener b;+ auto first = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ auto b_id = registry.connect(b, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ registry.disconnect(a, first);++ // The area lives on for b, and a's next connection to it still gets an id nobody has used.+ auto second = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ EXPECT(second != first);+ EXPECT(second != b_id);++ // A write a sent under its old id, still in flight when it reconnected, is dropped — not applied to the area a+ // now mirrors under the new id, and not answered under either id.+ registry.set_item(a, first, "k"_utf16, "stale"_utf16, URL);+ EXPECT_EQ(jar->get_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16), Optional<Utf16String> {});+ EXPECT(a.events.is_empty());+ EXPECT(b.events.is_empty());++ registry.set_item(a, second, "k"_utf16, "fresh"_utf16, URL);+ EXPECT_EQ(a.events.size(), 1u);+ EXPECT_EQ(a.events[0].area_id, second);+ EXPECT_EQ(b.events.size(), 1u);+ EXPECT_EQ(b.events[0].area_id, b_id);+ registry.remove_listener(a);+ registry.remove_listener(b);+}++TEST_CASE(areas_are_per_jar)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto ordinary = WebView::StorageJar::create();+ auto private_jar = WebView::StorageJar::create();+ FakeListener a;+ FakeListener b;+ auto ordinary_id = registry.connect(a, *ordinary, StorageEndpointType::LocalStorage, KEY).value().id;+ auto private_id = registry.connect(b, *private_jar, StorageEndpointType::LocalStorage, KEY).value().id;+ EXPECT(ordinary_id != private_id);++ registry.set_item(a, ordinary_id, "k"_utf16, "v"_utf16, URL);+ EXPECT(b.events.is_empty());+ EXPECT_EQ(private_jar->get_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16), Optional<Utf16String> {});+}++// Only web storage is mirrored. A process asking for any other endpoint — one with no quota, say — gets nothing, and+// the jar gets no bottle for it.+TEST_CASE(connect_refuses_endpoints_that_are_not_web_storage)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();++ FakeListener a;+ EXPECT(!registry.connect(a, *jar, StorageEndpointType::Caches, KEY).has_value());+ EXPECT(!registry.connect(a, *jar, StorageEndpointType::IndexedDB, KEY).has_value());+ EXPECT(!registry.connect(a, *jar, StorageEndpointType::ServiceWorkerRegistrations, KEY).has_value());+ EXPECT(registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).has_value());+ EXPECT(registry.connect(a, *jar, StorageEndpointType::SessionStorage, KEY).has_value());+ EXPECT_EQ(jar->bottle_usage(StorageEndpointType::Caches, KEY), 0u);+}++TEST_CASE(connect_marks_the_bottle_accessed)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ jar->set_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16, "v"_utf16);+ MUST(Core::System::sleep_ms(2));+ auto since = UnixDateTime::now();+ MUST(Core::System::sleep_ms(2));++ FakeListener a;+ (void)registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value();+ jar->remove_items_accessed_since(since);+ EXPECT_EQ(jar->get_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16), Optional<Utf16String> {});+}++TEST_CASE(devtools_hears_every_change_with_the_writer_url)+{+ auto& registry = WebView::StorageAreaRegistry::the();+ auto jar = WebView::StorageJar::create();+ struct Seen {+ WebView::StorageChange::Type type;+ Optional<String> url;+ };+ Vector<Seen> seen;+ registry.on_change_for_devtools = [&](WebView::StorageJar& changed_jar, WebView::StorageChange const& change, Optional<String> const& url) {+ EXPECT_EQ(&changed_jar, jar.ptr());+ seen.append({ change.type, url });+ };++ FakeListener a;+ auto id = registry.connect(a, *jar, StorageEndpointType::LocalStorage, KEY).value().id;+ registry.set_item(a, id, "k"_utf16, "v"_utf16, URL);+ jar->remove_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16);++ EXPECT_EQ(seen.size(), 2u);+ EXPECT_EQ(seen[0].type, WebView::StorageChange::Type::Set);+ EXPECT_EQ(seen[0].url, Optional<String> { URL });+ EXPECT_EQ(seen[1].type, WebView::StorageChange::Type::Removed);+ EXPECT_EQ(seen[1].url, Optional<String> {});+ registry.on_change_for_devtools = nullptr;+}