ladybird/Libraries/LibWeb/StorageAPI/StorageKey.cpp
Shannon Booth 70df8122b1 LibWeb/Storage: Add a storage key getter that takes a URL::Origin
When we serialize blob URL entries we do not serialize the entire
environment settings object and only use it's origin. To allow
a Blob URL entry to access its relevant storage key, add a getter
that simply takes an origin.
2025-01-21 19:22:07 +00:00

46 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/StorageAPI/StorageKey.h>
namespace Web::StorageAPI {
// https://storage.spec.whatwg.org/#obtain-a-storage-key
Optional<StorageKey> 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 keys 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;
}
StorageKey obtain_a_storage_key_for_non_storage_purposes(URL::Origin const& origin)
{
// NOTE: This function exists as there are cases where we don't have the full environment object, but we still need to obtain a storage key.
return { origin };
}
// 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 environments origin if environment is an environment settings object; otherwise environments creation URLs origin.
if (is<HTML::EnvironmentSettingsObject>(environment)) {
auto const& settings = static_cast<HTML::EnvironmentSettingsObject const&>(environment);
return { settings.origin() };
}
return { environment.creation_url.origin() };
}
}