StorageKey.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOMURL/DOMURL.h>
  7. #include <LibWeb/HTML/Scripting/Environments.h>
  8. #include <LibWeb/StorageAPI/StorageKey.h>
  9. namespace Web::StorageAPI {
  10. // https://storage.spec.whatwg.org/#obtain-a-storage-key
  11. Optional<StorageKey> obtain_a_storage_key(HTML::Environment const& environment)
  12. {
  13. // 1. Let key be the result of running obtain a storage key for non-storage purposes with environment.
  14. auto key = obtain_a_storage_key_for_non_storage_purposes(environment);
  15. // 2. If key’s origin is an opaque origin, then return failure.
  16. if (key.origin.is_opaque())
  17. return {};
  18. // FIXME: 3. If the user has disabled storage, then return failure.
  19. // 4. Return key.
  20. return key;
  21. }
  22. // https://storage.spec.whatwg.org/#obtain-a-storage-key-for-non-storage-purposes
  23. StorageKey obtain_a_storage_key_for_non_storage_purposes(HTML::Environment const& environment)
  24. {
  25. // 1. Let origin be environment’s origin if environment is an environment settings object; otherwise environment’s creation URL’s origin.
  26. if (is<HTML::EnvironmentSettingsObject>(environment)) {
  27. auto const& settings = static_cast<HTML::EnvironmentSettingsObject const&>(environment);
  28. // FIXME: EnvironmentSettingsObject::origin() should be const :|
  29. auto& mutable_settings = const_cast<HTML::EnvironmentSettingsObject&>(settings);
  30. return { mutable_settings.origin() };
  31. }
  32. return { environment.creation_url.origin() };
  33. }
  34. }