diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 390be0a0257..94ac688b035 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -631,6 +631,7 @@ set(SOURCES SecureContexts/AbstractOperations.cpp SRI/SRI.cpp StorageAPI/NavigatorStorage.cpp + StorageAPI/StorageKey.cpp StorageAPI/StorageManager.cpp Streams/AbstractOperations.cpp Streams/ByteLengthQueuingStrategy.cpp diff --git a/Userland/Libraries/LibWeb/StorageAPI/StorageKey.cpp b/Userland/Libraries/LibWeb/StorageAPI/StorageKey.cpp new file mode 100644 index 00000000000..5682d05b817 --- /dev/null +++ b/Userland/Libraries/LibWeb/StorageAPI/StorageKey.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::StorageAPI { + +// https://storage.spec.whatwg.org/#obtain-a-storage-key +Optional obtain_a_storage_key(HTML::Environment const& environment) +{ + // 1. Let key be the result of running obtain a storage key for non-storage purposes with environment. + auto key = obtain_a_storage_key_for_non_storage_purposes(environment); + + // 2. If key’s origin is an opaque origin, then return failure. + if (key.origin.is_opaque()) + return {}; + + // FIXME: 3. If the user has disabled storage, then return failure. + + // 4. Return key. + return key; +} + +// https://storage.spec.whatwg.org/#obtain-a-storage-key-for-non-storage-purposes +StorageKey obtain_a_storage_key_for_non_storage_purposes(HTML::Environment const& environment) +{ + // 1. Let origin be environment’s origin if environment is an environment settings object; otherwise environment’s creation URL’s origin. + if (is(environment)) { + auto const& settings = static_cast(environment); + // FIXME: EnvironmentSettingsObject::origin() should be const :| + auto& mutable_settings = const_cast(settings); + return { mutable_settings.origin() }; + } + return { DOMURL::url_origin(environment.creation_url) }; +} + +} diff --git a/Userland/Libraries/LibWeb/StorageAPI/StorageKey.h b/Userland/Libraries/LibWeb/StorageAPI/StorageKey.h new file mode 100644 index 00000000000..e8a4051500a --- /dev/null +++ b/Userland/Libraries/LibWeb/StorageAPI/StorageKey.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Web::StorageAPI { + +// https://storage.spec.whatwg.org/#storage-keys +struct StorageKey { + + // A storage key is a tuple consisting of an origin (an origin). [HTML] + // NOTE: This is expected to change; see Client-Side Storage Partitioning https://privacycg.github.io/storage-partitioning/. + HTML::Origin origin; + + friend bool operator==(StorageKey const& a, StorageKey const& b) + { + // To determine whether a storage key A equals storage key B, run these steps: + // 1. If A’s origin is not same origin with B’s origin, then return false. + // 2. Return true. + return a.origin.is_same_origin(b.origin); + } +}; + +Optional obtain_a_storage_key(HTML::Environment const&); +StorageKey obtain_a_storage_key_for_non_storage_purposes(HTML::Environment const&); + +}