Truly independent web browser
LibWebCommon: Share the web storage quota rule and add StorageAreaId
sideshowbarker02b3ede20305
diff --git a/Libraries/LibWebCommon/StorageAPI/StorageAreaId.h b/Libraries/LibWebCommon/StorageAPI/StorageAreaId.hnew file mode 100644index 00000000000..37723816c8d--- /dev/null+++ b/Libraries/LibWebCommon/StorageAPI/StorageAreaId.h@@ -0,0 +1,48 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Concepts.h>+#include <AK/DistinctNumeric.h>+#include <LibIPC/SenderClaim.h>++namespace Web::StorageAPI {++// Identifies one storage area: the map a storage bottle for one storage key keeps, in the process that owns it. The+// UI process hands out area IDs when a WebContent process connects to an area and tracks which connection each one+// went to, so an area ID in a message from a helper process is a claim about the sender. The distinct type keeps that+// claim from being mistaken for an ordinary number.+AK_TYPEDEF_DISTINCT_ORDERED_ID(u64, StorageAreaId);++// An area ID in a message is checked against the receiver's own record of which areas the sender was connected to.+template<typename Stub>+bool verify_sender_claim(Stub& stub, StorageAreaId const& area_id)+{+ if constexpr (requires {+ { stub.may_act_for_storage_area(area_id) } -> SameAs<bool>;+ })+ return stub.may_act_for_storage_area(area_id);+ else+ return true;+}++}++namespace IPC {++template<>+class SenderClaimReceiver<Web::StorageAPI::StorageAreaId> {+public:+ // True when the connection a message came from was given the storage area the message names. A receiver that+ // hands out no area IDs is not the party the claim is made to, so it accepts every area ID.+ virtual bool may_act_for_storage_area(Web::StorageAPI::StorageAreaId) const { return true; }++protected:+ ~SenderClaimReceiver() = default;+};++}diff --git a/Libraries/LibWebCommon/StorageAPI/StorageEndpoint.cpp b/Libraries/LibWebCommon/StorageAPI/StorageEndpoint.cppindex 91a327e6e81..6d4f97086f2 100644--- a/Libraries/LibWebCommon/StorageAPI/StorageEndpoint.cpp+++ b/Libraries/LibWebCommon/StorageAPI/StorageEndpoint.cpp@@ -22,4 +22,35 @@ ReadonlySpan<StorageEndpoint> StorageEndpoint::registered_endpoints()return endpoints;}+Optional<u64> quota_for(StorageEndpointType identifier)+{+ for (auto const& endpoint : StorageEndpoint::registered_endpoints()) {+ if (endpoint.identifier == identifier)+ return endpoint.quota;+ }+ return {};+}++u64 storage_quota_size(Utf16View const& string)+{+ // Counted without encoding: the bytes UTF-8 would take, with a lone surrogate counted as the three bytes it is+ // written as. Every write measures its key and value this way, so building the UTF-8 string would cost a copy of+ // each on the path meant to be fast.+ if (string.has_ascii_storage())+ return string.length_in_code_units();++ u64 size = 0;+ for (auto code_point : string) {+ if (code_point < 0x80)+ size += 1;+ else if (code_point < 0x800)+ size += 2;+ else if (code_point < 0x10000)+ size += 3;+ else+ size += 4;+ }+ return size;+}+}diff --git a/Libraries/LibWebCommon/StorageAPI/StorageEndpoint.h b/Libraries/LibWebCommon/StorageAPI/StorageEndpoint.hindex 5709be1e17a..df2180721a8 100644--- a/Libraries/LibWebCommon/StorageAPI/StorageEndpoint.h+++ b/Libraries/LibWebCommon/StorageAPI/StorageEndpoint.h@@ -10,6 +10,7 @@#include <AK/Optional.h>#include <AK/String.h>#include <AK/Types.h>+#include <AK/Utf16String.h>#include <LibWebCommon/Export.h>#include <LibWebCommon/StorageAPI/StorageType.h>@@ -49,4 +50,12 @@ struct WEBCOMMON_API StorageEndpoint {static ReadonlySpan<StorageEndpoint> registered_endpoints();};+// The quota of the registered storage endpoint with this identifier, or null when it has none.+WEBCOMMON_API Optional<u64> quota_for(StorageEndpointType);++// Bytes a string contributes toward a storage bottle's quota: its UTF-8 length. The process that keeps a bottle and+// the processes that mirror it measure the same way, so a write one accepts is a write the other accepts.+WEBCOMMON_API u64 storage_quota_size(Utf16View const&);+inline u64 storage_quota_size(Utf16String const& string) { return storage_quota_size(string.utf16_view()); }+}diff --git a/Libraries/LibWebView/StorageJar.cpp b/Libraries/LibWebView/StorageJar.cppindex 93fef1d2541..8716e2bd244 100644--- a/Libraries/LibWebView/StorageJar.cpp+++ b/Libraries/LibWebView/StorageJar.cpp@@ -10,10 +10,10 @@#include <LibDatabase/Database.h>#include <LibWebView/StorageJar.h>-namespace WebView {+using Web::StorageAPI::quota_for;+using Web::StorageAPI::storage_quota_size;-// Quota size is specified in https://storage.spec.whatwg.org/#registered-storage-endpoints-static constexpr size_t LOCAL_STORAGE_QUOTA = 5 * MiB;+namespace WebView {static constexpr u32 WEB_STORAGE_SCHEMA_BASELINE_VERSION = 2u;@@ -68,12 +68,6 @@ StorageJar::StorageJar(Optional<PersistedStorage> persisted_storage)StorageJar::~StorageJar() = default;-static size_t storage_quota_size(Utf16String const& string)-{- auto utf8_string = string.to_utf8();- return utf8_string.bytes().size();-}-Optional<Utf16String> StorageJar::get_item(StorageEndpointType storage_endpoint, String const& storage_key, Utf16String const& bottle_key){StorageLocation storage_location { storage_endpoint, storage_key, bottle_key };@@ -173,7 +167,7 @@ StorageSetResult StorageJar::TransientStorage::set_item(StorageLocation const& k}auto new_size = storage_quota_size(key.bottle_key) + storage_quota_size(value);- if (current_size + new_size > LOCAL_STORAGE_QUOTA)+ if (auto quota = quota_for(key.storage_endpoint); quota.has_value() && current_size + new_size > *quota)return StorageOperationError::QuotaExceededError;bottle.items.set(key.bottle_key, { value, UnixDateTime::now(), new_size });@@ -276,7 +270,7 @@ StorageSetResult StorageJar::PersistedStorage::set_item(StorageLocation const& kkey.bottle_key);auto new_size = storage_quota_size(key.bottle_key) + storage_quota_size(value);- if (current_size + new_size > LOCAL_STORAGE_QUOTA)+ if (auto quota = quota_for(key.storage_endpoint); quota.has_value() && current_size + new_size > *quota)return StorageOperationError::QuotaExceededError;database->execute_statement(diff --git a/Tests/LibWebCommon/CMakeLists.txt b/Tests/LibWebCommon/CMakeLists.txtindex 2cc80e0bd28..b76b20f7e24 100644--- a/Tests/LibWebCommon/CMakeLists.txt+++ b/Tests/LibWebCommon/CMakeLists.txt@@ -5,6 +5,7 @@ set(TEST_SOURCESTestKeyEvent.cppTestPermissionsPolicy.cppTestSiteCompatibility.cpp+ TestStorageEndpoint.cppTestStrings.cpp)@@ -16,3 +17,4 @@ target_link_libraries(TestSiteCompatibility PRIVATE LibURL)target_link_libraries(TestGamepadStateBuffer PRIVATE LibCore LibIPC)target_link_libraries(TestKeyEvent PRIVATE LibIPC)target_link_libraries(TestPermissionsPolicy PRIVATE LibHTTP LibIPC LibURL)+target_link_libraries(TestStorageEndpoint PRIVATE LibIPC)diff --git a/Tests/LibWebCommon/TestStorageEndpoint.cpp b/Tests/LibWebCommon/TestStorageEndpoint.cppnew file mode 100644index 00000000000..e07d5ebe6cb--- /dev/null+++ b/Tests/LibWebCommon/TestStorageEndpoint.cpp@@ -0,0 +1,53 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/Utf16String.h>+#include <LibTest/TestCase.h>+#include <LibWebCommon/StorageAPI/StorageAreaId.h>+#include <LibWebCommon/StorageAPI/StorageEndpoint.h>++TEST_CASE(storage_quota_size_counts_utf8_bytes)+{+ EXPECT_EQ(Web::StorageAPI::storage_quota_size(Utf16String {}), 0u);+ EXPECT_EQ(Web::StorageAPI::storage_quota_size("a"_utf16), 1u);+ EXPECT_EQ(Web::StorageAPI::storage_quota_size("é"_utf16), 2u);+ EXPECT_EQ(Web::StorageAPI::storage_quota_size("\U0001f600"_utf16), 4u);+}++TEST_CASE(storage_quota_size_matches_the_utf8_encoding_without_building_it)+{+ // A lone surrogate encodes as three bytes, like to_utf8() writes it.+ char16_t const lone_surrogate[] = { u'a', 0xD800, u'b' };+ Vector<Utf16String> samples = {+ Utf16String {},+ "plain ascii key"_utf16,+ "\u00e9\u00e9 mixed \u4e2d\u6587"_utf16,+ "\U0001f600\U0001f601"_utf16,+ Utf16String::from_utf16(Utf16View { lone_surrogate, 3 }),+ };+ for (auto const& sample : samples) {+ auto encoded = sample.to_utf8();+ EXPECT_EQ(Web::StorageAPI::storage_quota_size(sample), encoded.bytes().size());+ }+ auto one = "a"_utf16;+ EXPECT_EQ(Web::StorageAPI::storage_quota_size(one.utf16_view()), 1u);+}++TEST_CASE(quota_for_registered_endpoints)+{+ EXPECT_EQ(Web::StorageAPI::quota_for(Web::StorageAPI::StorageEndpointType::LocalStorage), Optional<u64> { 5 * MiB });+ EXPECT_EQ(Web::StorageAPI::quota_for(Web::StorageAPI::StorageEndpointType::SessionStorage), Optional<u64> { 5 * MiB });+ EXPECT_EQ(Web::StorageAPI::quota_for(Web::StorageAPI::StorageEndpointType::Caches), Optional<u64> {});+}++TEST_CASE(storage_area_id_is_distinct_and_ordered)+{+ Web::StorageAPI::StorageAreaId first { 1 };+ Web::StorageAPI::StorageAreaId second { 2 };+ EXPECT(first < second);+ EXPECT(first != second);+ static_assert(!IsSame<Web::StorageAPI::StorageAreaId, u64>);+}