dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / scripting / environments_2.lucb
#==============================================================================================
#
# environments_2 - HTML scripting environments: scripting enabled, the incumbent, current,
# relevant and entry realms and settings objects, secure contexts, serialization
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/Scripting/Environments.cpp (lines 287-610) with the
# inline functions of Environments.h (lines 1-239): whether scripting is enabled for a
# settings object, the module types and resolved module set, the incumbent, principal,
# current, relevant and entry realms, settings objects and global objects, secure
# contexts, serializing a settings object, and the StorageAPI / ServiceWorker objects of
# a settings object (phase 4).
#
# Copyright (c) 2021-2025, Luke Wilde <luke@ladybird.org>
# Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
# Copyright (c) 2022, networkException <networkexception@serenityos.org>
# Copyright (c) 2024-2026, Shannon Booth <shannon@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Scripting enabled and modules ===========================================================
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:287 Web::HTML::is_scripting_enabled.
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-script
pub func html_is_scripting_enabled(settings: HtmlEnvironmentSettingsObject*) -> bool:
# Scripting is enabled for an environment settings object settings when all of the following conditions are true:
# The user agent supports scripting.
# NOTE: This is always true in LibWeb :^)
# FIXME: Do the right thing for workers.
let window = as_if_html_window((gc.Cell*)html_environment_settings_object_global_object(settings)) else return true
# The user has not disabled scripting for realm at this time. (User agents may provide users with the option to disable scripting globally, or in a finer-grained manner, e.g., on a per-origin basis, down to the level of individual realms.)
let document = html_window_associated_document(window)
# NB: WebUI pages are internal pages requiring javascript, so we do not consider user configuration for these.
if not page_is_scripting_enabled(dom_document_page(document)) and not web_url.is_webui_url(dom_document_url(document)):
return false
# Either settings's global object is not a Window object, or settings's global object's associated Document's active sandboxing flag set does not have its sandboxed scripts browsing context flag set.
if (i32)(dom_document_active_sandboxing_flag_set(document) & HtmlSandboxingFlagSet.sandboxed_scripts) != 0:
return false
return true
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:312 Web::HTML::is_scripting_disabled.
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-noscript
pub func html_is_scripting_disabled(settings: HtmlEnvironmentSettingsObject*) -> bool:
# Scripting is disabled for an environment settings object when scripting is not enabled for it, i.e., when any of the above conditions are false.
return not html_is_scripting_enabled(settings)
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:319 Web::HTML::module_type_allowed.
## https://html.spec.whatwg.org/multipage/webappapis.html#module-type-allowed
pub func html_module_type_allowed(arg0: HtmlEnvironmentSettingsObject*, module_type: ak.StringView) -> bool:
# 1. If moduleType is not "javascript-or-wasm", "css", or "json", then return false.
if not ak.string_view_equals(&module_type, "javascript-or-wasm") and not ak.string_view_equals(&module_type, "css") and not ak.string_view_equals(&module_type, "json"):
return false
# FIXME: 2. If moduleType is "css" and the CSSStyleSheet interface is not exposed in settings's realm, then return false.
# 3. Return true.
return true
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:332 Web::HTML::add_module_to_resolved_module_set.
## https://html.spec.whatwg.org/multipage/webappapis.html#add-module-to-resolved-module-set
pub func html_add_module_to_resolved_module_set(settings_object: HtmlEnvironmentSettingsObject*, serialized_base_url: ak.String, normalized_specifier: ak.String, as_url: web_url.Url?):
# 1. Let global be settingsObject's global object.
let global = html_environment_settings_object_global_object(settings_object)
# 2. If global does not implement Window, then return.
let window = as_if_html_window((gc.Cell*)global) else return
# 3. Let record be a new specifier resolution record, with serialized base URL set to serializedBaseURL,
# specifier set to normalizedSpecifier, and specifier as a URL set to asURL.
#
# NOTE: We set 'specifier as a URL set to asURL' as a bool to simplify logic when merging import maps.
var specifier_is_null_or_url_like_that_is_special = true
if let url = as_url:
specifier_is_null_or_url_like_that_is_special = web_url.url_is_special(&url)
let resolution = HtmlSpecifierResolution(serialized_base_url = serialized_base_url, specifier = normalized_specifier, specifier_is_null_or_url_like_that_is_special = specifier_is_null_or_url_like_that_is_special)
# 4. Append record to global's resolved module set.
html_window_append_resolved_module(window, resolution)
# mark: Incumbent, principal and current ========================================================
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:356 Web::HTML::incumbent_realm.
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-incumbent-realm
pub func html_incumbent_realm() -> Realm*:
# Then, the incumbent realm is the realm of the incumbent settings object.
return html_environment_settings_object_realm(html_incumbent_settings_object())
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:363 Web::HTML::incumbent_settings_object.
## https://html.spec.whatwg.org/multipage/webappapis.html#incumbent-settings-object
pub func html_incumbent_settings_object() -> HtmlEnvironmentSettingsObject*:
let event_loop = html_main_thread_event_loop()
let vm = js_cell_vm(event_loop.js_cell())
# 1. Let context be the topmost script-having execution context.
let context = html_top_most_script_having_execution_context(vm)
# 2. If context is null, or if context's skip-when-determining-incumbent counter is greater than zero, then:
var skip = true
if let topmost = context:
skip = topmost.skip_when_determining_incumbent_counter > 0
if skip:
# 1. Assert: the backup incumbent settings object stack is not empty.
# 1. Assert: the backup incumbent realm stack is not empty.
# NOTE: If this assertion fails, it's because the incumbent realm was used with no involvement of JavaScript.
assert(not html_event_loop_is_backup_incumbent_realm_stack_empty(event_loop))
# 2. Return the topmost entry of the backup incumbent realm stack.
return html_event_loop_top_of_backup_incumbent_realm_stack(event_loop)
# 3. Return context's Realm component's settings object.
let topmost = context else trap("VERIFY: a topmost script-having execution context")
let realm = topmost.realm else trap("the topmost script-having execution context has no realm")
return html_principal_realm_settings_object(realm)
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:387 Web::HTML::incumbent_global_object.
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-incumbent-global
pub func html_incumbent_global_object() -> JsObject*:
# Similarly, the incumbent global object is the global object of the incumbent settings object.
return html_environment_settings_object_global_object(html_incumbent_settings_object())
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:394 Web::HTML::principal_realm_settings_object.
## https://whatpr.org/html/9893/webappapis.html#concept-realm-settings-object
pub func html_principal_realm_settings_object(realm: Realm*) -> HtmlEnvironmentSettingsObject*:
# A principal realm has a [[HostDefined]] field, which contains the principal realm's settings object.
return bind_principal_host_defined_environment_settings_object(realm)
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:401 Web::HTML::current_settings_object.
## https://html.spec.whatwg.org/multipage/webappapis.html#current-settings-object
pub func html_current_settings_object() -> HtmlEnvironmentSettingsObject*:
# Then, the current settings object is the environment settings object of the current realm.
return html_principal_realm_settings_object(vm_current_realm(bind_main_thread_vm()) else trap("no current realm"))
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:408 Web::HTML::current_global_object.
## https://html.spec.whatwg.org/multipage/webappapis.html#current-global-object
pub func html_current_global_object() -> JsObject*:
# Similarly, the current global object is the global object of the current realm.
return realm_global_object(vm_current_realm(bind_main_thread_vm()) else trap("no current realm"))
# mark: Relevant ================================================================================
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:415 Web::HTML::relevant_realm.
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-relevant-realm
pub func html_relevant_realm(object: JsObject*) -> Realm*:
# The relevant Realm for a platform object is the value of its [[Realm]] field.
return js_object_shape_realm(object)
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:422 Web::HTML::relevant_settings_object (JS::Object const&).
## https://html.spec.whatwg.org/multipage/webappapis.html#relevant-settings-object
pub func html_relevant_settings_object_js_object(object: JsObject*) -> HtmlEnvironmentSettingsObject*:
# Then, the relevant settings object for a platform object o is the environment settings object of the relevant realm for o.
return bind_principal_host_defined_environment_settings_object(html_relevant_realm(object))
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:428 Web::HTML::relevant_settings_object (DOM::Node const&).
pub func html_relevant_settings_object_dom_node(node: DomNode*) -> HtmlEnvironmentSettingsObject*:
# Then, the relevant settings object for a platform object o is the environment settings object of the relevant Realm for o.
return dom_document_relevant_settings_object(dom_node_document(node))
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:435 Web::HTML::relevant_global_object.
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-relevant-global
pub func html_relevant_global_object(object: JsObject*) -> JsObject*:
# Similarly, the relevant global object for a platform object o is the global object of the relevant Realm for o.
return realm_global_object(html_relevant_realm(object))
# mark: Entry ===================================================================================
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:442 Web::HTML::entry_realm.
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-entry-realm
pub func html_entry_realm() -> Realm*:
let event_loop = html_main_thread_event_loop()
let vm = js_cell_vm(event_loop.js_cell())
# With this in hand, we define the entry execution context to be the most recently pushed item in the JavaScript execution context stack that is a realm execution context.
# The entry realm is the entry execution context's Realm component.
# (vm.last_execution_context_matching: the stack from the top down.)
let stack = vm_execution_context_stack(vm)
var index = stack.length
while index > 0:
index -= 1
let context = stack[index]
let realm = context.realm else continue
if html_environment_settings_object_realm_execution_context(html_principal_realm_settings_object(realm)) == context:
return realm
trap("VERIFY: an entry execution context")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:460 Web::HTML::entry_settings_object.
## https://html.spec.whatwg.org/multipage/webappapis.html#entry-settings-object
pub func html_entry_settings_object() -> HtmlEnvironmentSettingsObject*:
# Then, the entry settings object is the environment settings object of the entry realm.
return bind_principal_host_defined_environment_settings_object(html_entry_realm())
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:467 Web::HTML::entry_global_object.
## https://html.spec.whatwg.org/multipage/webappapis.html#entry-global-object
pub func html_entry_global_object() -> JsObject*:
# Similarly, the entry global object is the global object of the entry realm.
return realm_global_object(html_entry_realm())
# mark: Secure contexts =========================================================================
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:474 Web::HTML::is_secure_context.
## https://html.spec.whatwg.org/multipage/webappapis.html#secure-context
pub func html_is_secure_context(environment: HtmlEnvironment*) -> bool:
# 1. If environment is an environment settings object, then:
if let environment_settings_object = as_if_html_environment_settings_object(environment):
# 1. Let global be environment's global object.
let global = html_environment_settings_object_global_object(environment_settings_object)
# 2. If global is a WorkerGlobalScope, then:
# 1. If global's owner set[0]'s relevant settings object is a secure context, then return true.
# NOTE: We only need to check the 0th item since they will necessarily all be consistent.
# 2. Return false.
# (No WorkerGlobalScope exists before workers, phase 4: every global is a Window or a test global.)
_ = global
# FIXME: 3. If global is a WorkletGlobalScope, then return true.
# NOTE: Worklets can only be created in secure contexts.
# 2. If the result of Is url potentially trustworthy? given environment's top-level creation URL is "Potentially Trustworthy", then return true.
if secure_contexts_is_url_potentially_trustworthy(environment.top_level_creation_url else trap("VERIFY: the environment has a top-level creation URL")) == SecureContextsTrustworthiness.potentially_trustworthy:
return true
# 3. Return false.
return false
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:505 Web::HTML::is_non_secure_context.
## https://html.spec.whatwg.org/multipage/webappapis.html#non-secure-context
pub func html_is_non_secure_context(environment: HtmlEnvironment*) -> bool:
# An environment is a non-secure context if it is not a secure context.
return not html_is_secure_context(environment)
# mark: Serialization, storage and service workers ==============================================
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:511 Web::HTML::EnvironmentSettingsObject::serialize.
pub func html_environment_settings_object_serialize(this: HtmlEnvironmentSettingsObject*) -> HtmlSerializedEnvironmentSettingsObject:
# The lambda serialized_global.
let relevant_settings_object_is_secure_context = html_is_secure_context(this.html_environment())
let window = as_if_html_window((gc.Cell*)html_environment_settings_object_global_object(this)) else trap("unported (P4): VERIFY(is<WorkerGlobalScope>(global_object())): SerializedWorkerGlobalScope")
let serialized_global = HtmlSerializedGlobal.html_serialized_window(HtmlSerializedWindow(associated_document = HtmlSerializedDocument(url = dom_document_url(html_window_associated_document(window)), relevant_settings_object_is_secure_context = relevant_settings_object_is_secure_context)))
let environment = this.html_environment()
return HtmlSerializedEnvironmentSettingsObject(id = environment.id, creation_url = environment.creation_url, top_level_creation_url = environment.top_level_creation_url, top_level_origin = environment.top_level_origin, api_base_url = html_environment_settings_object_api_base_url(this), origin = html_environment_settings_object_origin(this), has_cross_site_ancestor = html_environment_settings_object_has_cross_site_ancestor(this), policy_container = html_policy_container_serialize(html_environment_settings_object_policy_container(this)), cross_origin_isolated_capability = html_environment_settings_object_cross_origin_isolated_capability(this), time_origin = html_environment_settings_object_time_origin(this), global = serialized_global)
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:544 Web::HTML::EnvironmentSettingsObject::storage_manager.
pub func html_environment_settings_object_storage_manager(this: HtmlEnvironmentSettingsObject*) -> StorageApiStorageManager*:
if let storage_manager = this.m_storage_manager:
return storage_manager
# m_storage_manager = realm().create<StorageAPI::StorageManager>(realm());
trap("unported (P4): StorageAPI::StorageManager::create")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:552 Web::HTML::EnvironmentSettingsObject::get_service_worker_registration_object.
## https://w3c.github.io/ServiceWorker/#get-the-service-worker-registration-object
pub func html_environment_settings_object_get_service_worker_registration_object(this: HtmlEnvironmentSettingsObject*, registration: ServiceWorkerRegistration) -> ServiceWorkerServiceWorkerRegistration*:
# 1. Let objectMap be environment’s service worker registration object map.
# FIXME: File spec issue asking if this should be keyed on the registration's scope url only or on the url and the storage key
# 2. If objectMap[registration] does not exist, then:
# 1. Let registrationObject be a new ServiceWorkerRegistration in environment’s Realm.
# 2-5. Set registrationObject’s service worker registration to registration and its installing, waiting
# and active attributes to null.
# 6-8. Set the installing, waiting and active attributes to the service worker objects of registration's
# installing, waiting and active workers, if any.
# 9. Set objectMap[registration] to registrationObject.
# 3. Return objectMap[registration].
trap("unported (P4): ServiceWorker::ServiceWorkerRegistration::create")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.cpp:589 Web::HTML::EnvironmentSettingsObject::get_service_worker_object.
## https://w3c.github.io/ServiceWorker/#get-the-service-worker-object
pub func html_environment_settings_object_get_service_worker_object(this: HtmlEnvironmentSettingsObject*, service_worker: ServiceWorkerServiceWorkerRecord*?) -> ServiceWorkerServiceWorker*:
# 1. Let objectMap be environment’s service worker object map.
# 2. If objectMap[serviceWorker] does not exist, then:
# 1. Let serviceWorkerObj be a new ServiceWorker in environment’s Realm, and associate it with serviceWorker.
# 2. Set serviceWorkerObj’s state to serviceWorker’s state.
# 3. Set objectMap[serviceWorker] to serviceWorkerObj.
# 3. Return objectMap[serviceWorker].
trap("unported (P4): ServiceWorker::ServiceWorker::create")
# mark: Environments.h ==========================================================================
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:60 Web::HTML::Environment::discard_environment (virtual).
## https://html.spec.whatwg.org/multipage/webappapis.html#environment-discarding-steps
pub func html_environment_discard_environment_impl(this: HtmlEnvironment*):
return
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:64 Web::HTML::Environment::Environment.
pub func html_environment_construct(this: HtmlEnvironment*, id: ak.String, creation_url: web_url.Url, top_level_creation_url: web_url.Url?, top_level_origin: web_url.Origin?, target_browsing_context: HtmlBrowsingContext*?):
this.id = id
this.creation_url = creation_url
this.top_level_creation_url = top_level_creation_url
this.top_level_origin = top_level_origin
this.target_browsing_context = target_browsing_context
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:98 Web::HTML::EnvironmentSettingsObject::responsible_document (pure virtual).
## https://html.spec.whatwg.org/multipage/webappapis.html#responsible-document
pub func html_environment_settings_object_responsible_document_impl(this: HtmlEnvironmentSettingsObject*) -> DomDocument*?:
trap("pure virtual: Web::HTML::EnvironmentSettingsObject::responsible_document")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:101 Web::HTML::EnvironmentSettingsObject::api_base_url (pure virtual).
## https://html.spec.whatwg.org/multipage/webappapis.html#api-base-url
pub func html_environment_settings_object_api_base_url_impl(this: HtmlEnvironmentSettingsObject*) -> web_url.Url:
trap("pure virtual: Web::HTML::EnvironmentSettingsObject::api_base_url")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:104 Web::HTML::EnvironmentSettingsObject::origin (pure virtual).
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-origin
pub func html_environment_settings_object_origin_impl(this: HtmlEnvironmentSettingsObject*) -> web_url.Origin:
trap("pure virtual: Web::HTML::EnvironmentSettingsObject::origin")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:107 Web::HTML::EnvironmentSettingsObject::has_cross_site_ancestor (pure virtual).
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-has-cross-site-ancestor
pub func html_environment_settings_object_has_cross_site_ancestor_impl(this: HtmlEnvironmentSettingsObject*) -> bool:
trap("pure virtual: Web::HTML::EnvironmentSettingsObject::has_cross_site_ancestor")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:110 Web::HTML::EnvironmentSettingsObject::policy_container (pure virtual).
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-policy-container
pub func html_environment_settings_object_policy_container_impl(this: HtmlEnvironmentSettingsObject*) -> HtmlPolicyContainer*:
trap("pure virtual: Web::HTML::EnvironmentSettingsObject::policy_container")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:113 Web::HTML::EnvironmentSettingsObject::cross_origin_isolated_capability (pure virtual).
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability
pub func html_environment_settings_object_cross_origin_isolated_capability_impl(this: HtmlEnvironmentSettingsObject*) -> HtmlCanUseCrossOriginIsolatedApIs:
trap("pure virtual: Web::HTML::EnvironmentSettingsObject::cross_origin_isolated_capability")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:116 Web::HTML::EnvironmentSettingsObject::time_origin (pure virtual).
## https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-time-origin
pub func html_environment_settings_object_time_origin_impl(this: HtmlEnvironmentSettingsObject*) -> f64:
trap("pure virtual: Web::HTML::EnvironmentSettingsObject::time_origin")
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:124 Web::HTML::EnvironmentSettingsObject::global_object (const).
pub func html_environment_settings_object_global_object_const(this: HtmlEnvironmentSettingsObject*) -> JsObject*:
return html_environment_settings_object_global_object(this)
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:126 Web::HTML::EnvironmentSettingsObject::universal_global_scope (const).
pub func html_environment_settings_object_universal_global_scope_const(this: HtmlEnvironmentSettingsObject*) -> HtmlUniversalGlobalScopeMixin*:
return html_environment_settings_object_universal_global_scope(this)
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:130 Web::HTML::EnvironmentSettingsObject::fetch_group.
## https://fetch.spec.whatwg.org/#concept-fetch-group
pub func html_environment_settings_object_fetch_group(this: HtmlEnvironmentSettingsObject*) -> ak.IntrusiveList[FetchRecord, FetchRecord*?]*:
return &this.m_fetch_group
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:142 Web::HTML::EnvironmentSettingsObject::discarded.
pub func html_environment_settings_object_discarded(this: HtmlEnvironmentSettingsObject*) -> bool:
return this.m_discarded
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:143 Web::HTML::EnvironmentSettingsObject::set_discarded.
pub func html_environment_settings_object_set_discarded(this: HtmlEnvironmentSettingsObject*, b: bool):
this.m_discarded = b
## Ported from Libraries/LibWeb/HTML/Scripting/Environments.h:157 Web::HTML::EnvironmentSettingsObject::current_wall_time.
## https://w3c.github.io/hr-time/#dfn-eso-current-wall-time
pub func html_environment_settings_object_current_wall_time(this: HtmlEnvironmentSettingsObject*) -> f64:
# An environment settings object settingsObject's current wall time is the result of the following steps:
# 1. Let unsafeWallTime be the wall clock's unsafe current time.
let unsafe_walltime = high_resolution_time_wall_clock_unsafe_current_time()
# 2. Return the result of calling coarsen time with unsafeWallTime and settingsObject's cross-origin isolated capability.
return high_resolution_time_coarsen_time(unsafe_walltime, html_environment_settings_object_cross_origin_isolated_capability(this))