2024-09-14 20:02:27 -06:00
|
|
|
|
/*
|
|
|
|
|
|
* 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);
|
|
|
|
|
|
|
2026-04-01 16:55:14 -04:00
|
|
|
|
// AD-HOC: file:// URLs are opaque, but other browsers support storage on file:// URLs.
|
|
|
|
|
|
if (key.origin.is_opaque_file_origin())
|
|
|
|
|
|
key.origin = URL::Origin { "file"_string, String {}, {} };
|
|
|
|
|
|
|
2024-09-14 20:02:27 -06:00
|
|
|
|
// 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-19 18:15:06 +13:00
|
|
|
|
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 };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-14 20:02:27 -06:00
|
|
|
|
// 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.
|
2026-04-01 16:55:14 -04:00
|
|
|
|
auto origin = [&]() {
|
|
|
|
|
|
if (auto const* settings = as_if<HTML::EnvironmentSettingsObject>(environment))
|
|
|
|
|
|
return settings->origin();
|
|
|
|
|
|
return environment.creation_url.origin();
|
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Return a tuple consisting of origin.
|
|
|
|
|
|
return { move(origin) };
|
2024-09-14 20:02:27 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|