Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashMap.h>
#include <AK/NeverDestroyed.h>
#include <LibGC/Root.h>
#include <LibGC/WeakInlines.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/WrapperWorld.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/HTML/CustomElements/CustomElementAlgorithms.h>
#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::Bindings {
Wrappable::Wrappable() = default;
struct PreservedWrapper {
GC::Weak<WrapperWorld> world;
GC::Ptr<JS::HostObject> wrapper;
};
static HashMap<GCAllocatedWrappable*, Vector<PreservedWrapper>>& non_main_world_preserved_wrappers()
{
static thread_local NeverDestroyed<HashMap<GCAllocatedWrappable*, Vector<PreservedWrapper>>> preserved_wrappers;
return *preserved_wrappers;
}
void GCAllocatedWrappable::finalize()
{
Base::finalize();
if (has_non_main_world_preserved_wrappers())
non_main_world_preserved_wrappers().remove(this);
}
Optional<URL::Origin> Wrappable::extract_an_origin() const
{
return {};
}
Vector<Utf16FlyString> Wrappable::supported_property_names() const
{
return {};
}
bool Wrappable::is_supported_property_name(Utf16FlyString const& name) const
{
return supported_property_names().contains_slow(name);
}
GC::Ref<JS::HostObject> Wrappable::create_wrapper(JS::Realm& wrapper_realm)
{
return create_wrapper_for_wrappable(wrapper_realm, GC::Ref { *this });
}
void Wrappable::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
}
void GCAllocatedWrappable::visit_edges(GC::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
if (main_world_wrapper_is_preserved()) {
VERIFY(m_main_world_wrapper);
visitor.visit(*m_main_world_wrapper);
}
if (has_non_main_world_preserved_wrappers()) {
auto it = non_main_world_preserved_wrappers().find(this);
VERIFY(it != non_main_world_preserved_wrappers().end());
for (auto const& entry : it->value) {
VERIFY(entry.wrapper);
visitor.visit(*entry.wrapper);
}
}
}
GC::Ptr<JS::HostObject> Wrappable::cached_main_world_wrapper(WrapperWorld const& world) const
{
// NB: set_cached_main_world_wrapper() verified that the wrapper belongs to the main world when it cached it, so a
// read only walks to the wrapper's realm again in debug builds.
VERIFY(world.is_main_world());
ASSERT(!m_main_world_wrapper || &host_defined_wrapper_world(m_main_world_wrapper->shape().realm()) == &world);
return m_main_world_wrapper.ptr();
}
GC::Ptr<JS::HostObject> Wrappable::cached_main_world_wrapper() const
{
return m_main_world_wrapper.ptr();
}
void Wrappable::set_cached_main_world_wrapper(JS::HostObject& wrapper)
{
VERIFY(wrapper.shape().realm().host_defined());
auto& wrapper_world = host_defined_wrapper_world(wrapper.shape().realm());
VERIFY(wrapper_world.is_main_world());
if (m_main_world_wrapper) {
VERIFY(&host_defined_wrapper_world(m_main_world_wrapper->shape().realm()) == &wrapper_world);
VERIFY(m_main_world_wrapper.ptr().ptr() == &wrapper);
return;
}
m_main_world_wrapper = wrapper;
}
void Wrappable::clear_cached_main_world_wrapper(JS::HostObject const& wrapper)
{
if (!m_main_world_wrapper || m_main_world_wrapper.ptr().ptr() == &wrapper)
m_main_world_wrapper = nullptr;
}
// Until cache_global_object_wrapper() runs for its realm, a new realm is reachable only through the realm execution
// context that creating it returned, which the GC does not see once it is off the execution context stack. Its global
// object wrapper keeps it alive through the wrapper's shape, so the wrapper stays rooted until then. The realm's agent
// holds the root, so that a realm whose setup never finishes does not leave a root behind that outlives its VM's heap.
static Vector<GC::Root<JS::HostObject>>& global_object_wrappers_of_realms_being_set_up(JS::Realm& realm)
{
return static_cast<HTML::Agent&>(*realm.vm().agent()).global_object_wrappers_of_realms_being_set_up;
}
GC::Ref<JS::HostObject> create_global_object_wrapper(JS::Realm& wrapper_realm, GC::Ref<Wrappable> wrappable)
{
// This helper is for JS::Realm::initialize_host_defined_realm() global
// object callbacks. At that point HostDefined is not installed yet, so the
// wrapper is created first and cache_global_object_wrapper() must be called
// after the caller installs HostDefined/intrinsics for the new realm.
VERIFY(!wrapper_realm.host_defined());
auto wrapper = wrappable->create_wrapper(wrapper_realm);
global_object_wrappers_of_realms_being_set_up(wrapper_realm).append(GC::make_root(wrapper));
return wrapper;
}
JS::Realm& wrapper_realm_for_node(WrapperWorld const& wrapper_world, JS::Realm& preferred_realm, DOM::Node& node)
{
auto& relevant_realm = HTML::relevant_realm(node);
if (relevant_realm.host_defined()) {
auto& relevant_world = host_defined_wrapper_world(relevant_realm);
VERIFY(!relevant_world.is_main_world() || &relevant_world == &wrapper_world);
if (&relevant_world == &wrapper_world)
return relevant_realm;
}
return preferred_realm;
}
JS::Realm& wrapper_realm_for_wrappable(WrapperWorld const& wrapper_world, JS::Realm& preferred_realm, GC::Ref<Wrappable> wrappable)
{
if (!wrapper_world.is_main_world())
return preferred_realm;
if (auto* node = as_if<DOM::Node>(wrappable.ptr()))
return wrapper_realm_for_node(wrapper_world, preferred_realm, *node);
if (auto relevant_global_impl = wrappable->relevant_global_impl()) {
if (auto relevant_global_wrapper = relevant_global_impl->cached_main_world_wrapper(); relevant_global_wrapper && relevant_global_wrapper->shape().realm().host_defined()) {
auto& relevant_global_world = host_defined_wrapper_world(relevant_global_wrapper->shape().realm());
if (&relevant_global_world == &wrapper_world)
return relevant_global_wrapper->shape().realm();
}
}
return preferred_realm;
}
GC::Ref<JS::HostObject> wrap(WrapperWorld& wrapper_world, JS::Realm& preferred_realm, GC::Ref<Wrappable> wrappable)
{
if (wrapper_world.is_main_world()) {
if (auto cached_wrapper = wrappable->cached_main_world_wrapper(wrapper_world))
return *cached_wrapper;
}
auto& actual_wrapper_realm = wrapper_realm_for_wrappable(wrapper_world, preferred_realm, wrappable);
if (!wrapper_world.is_main_world()) {
if (auto cached_wrapper = wrapper_world.wrapper_for(wrappable, actual_wrapper_realm))
return *cached_wrapper;
}
auto wrapper = wrappable->create_wrapper(actual_wrapper_realm);
if (auto* element = as_if<DOM::Element>(wrappable.ptr()))
set_prototype_from_custom_element_definition_if_needed(*element, wrapper);
wrapper_world.set_wrapper(wrappable, wrapper);
return wrapper;
}
GC::Ptr<JS::HostObject> wrap(WrapperWorld& wrapper_world, JS::Realm& preferred_realm, GC::Ptr<Wrappable> wrappable)
{
if (!wrappable)
return nullptr;
return wrap(wrapper_world, preferred_realm, GC::Ref { *wrappable });
}
JS::ThrowCompletionOr<void> set_prototype_of_cached_main_world_wrapper(Wrappable& wrappable, JS::Object& prototype)
{
if (auto wrapper = wrappable.m_main_world_wrapper.ptr()) {
// Custom-element construction in an isolated world must not install
// an isolated-world prototype on any main-world wrapper. The agent
// ownership invariant guarantees that two main-world cells cannot
// legitimately meet here.
if (!wrapper->shape().realm().host_defined() || !prototype.shape().realm().host_defined())
return {};
auto& wrapper_world = host_defined_wrapper_world(wrapper->shape().realm());
auto& prototype_world = host_defined_wrapper_world(prototype.shape().realm());
if (!wrapper_world.is_main_world() || !prototype_world.is_main_world())
return {};
TRY(wrapper->internal_set_prototype_of(&prototype));
}
return {};
}
void preserve_wrapper(Wrappable& wrappable, JS::HostObject& wrapper)
{
// An object of an interface implemented in Rust keeps its wrappers alive itself, as wrapper_is_preserved() says.
if (wrappable.is_owned_by_rust_interface_object())
return;
auto& gc_allocated_wrappable = as<GCAllocatedWrappable>(wrappable);
auto& world = host_defined_wrapper_world(wrapper.shape().realm());
// Script can still run against a detached world's realm (e.g. a pending microtask after
// WrapperWorld::detach()). Nothing in a detached world needs to survive GC, so preservation
// is a no-op rather than a crash.
if (world.is_detached())
return;
if (world.is_main_world()) {
VERIFY(wrappable.m_main_world_wrapper.ptr().ptr() == &wrapper);
gc_allocated_wrappable.set_main_world_wrapper_is_preserved(true);
return;
}
auto& preserved_wrappers = non_main_world_preserved_wrappers().ensure(&gc_allocated_wrappable);
for (auto const& entry : preserved_wrappers) {
if (entry.wrapper.ptr() == &wrapper && entry.world.ptr().ptr() == &world)
return;
}
preserved_wrappers.append({ world, &wrapper });
gc_allocated_wrappable.set_has_non_main_world_preserved_wrappers(true);
world.register_preserved_wrappable(gc_allocated_wrappable);
}
bool wrapper_is_preserved(JS::HostObject const& wrapper)
{
// An object of an interface implemented in Rust keeps its wrapper in the main world alive itself. No other
// world keeps a wrapper of it alive.
if (JS::is_host_instance_of(wrapper, web_bindings_rust_interface_wrapper_host_class))
return host_defined_wrapper_world(wrapper.shape().realm()).is_main_world();
auto const* wrappable = wrappable_impl_from(&wrapper);
if (!wrappable)
return false;
// NB: The wrappers of a Rust object that inherits from a C++ interface hold the C++ object it owns as their
// wrappable.
if (wrappable->is_owned_by_rust_interface_object())
return host_defined_wrapper_world(wrapper.shape().realm()).is_main_world();
auto const& gc_allocated_wrappable = as<GCAllocatedWrappable>(*wrappable);
auto& world = host_defined_wrapper_world(wrapper.shape().realm());
if (world.is_main_world())
return gc_allocated_wrappable.main_world_wrapper_is_preserved() && wrappable->m_main_world_wrapper.ptr().ptr() == &wrapper;
if (gc_allocated_wrappable.has_non_main_world_preserved_wrappers()) {
auto it = non_main_world_preserved_wrappers().find(const_cast<GCAllocatedWrappable*>(&gc_allocated_wrappable));
VERIFY(it != non_main_world_preserved_wrappers().end());
for (auto const& entry : it->value) {
if (entry.wrapper.ptr() == &wrapper && entry.world.ptr().ptr() == &world)
return true;
}
}
return false;
}
void GCAllocatedWrappable::clear_preserved_wrappers(WrapperWorld const& world)
{
VERIFY(!world.is_main_world());
if (!has_non_main_world_preserved_wrappers())
return;
auto& preserved_wrappers_by_wrappable = non_main_world_preserved_wrappers();
auto it = preserved_wrappers_by_wrappable.find(this);
VERIFY(it != preserved_wrappers_by_wrappable.end());
it->value.remove_all_matching([&](auto const& entry) { return entry.world.ptr().ptr() == &world; });
if (it->value.is_empty()) {
preserved_wrappers_by_wrappable.remove(it);
set_has_non_main_world_preserved_wrappers(false);
}
}
void cache_global_object_wrapper(JS::Realm& realm)
{
auto* platform_object = as_platform_object(realm.global_object());
VERIFY(platform_object);
auto* wrappable = wrappable_impl_from(platform_object);
VERIFY(wrappable);
host_defined_wrapper_world(realm).set_wrapper(*wrappable, *platform_object);
global_object_wrappers_of_realms_being_set_up(realm).remove_all_matching([&](auto const& wrapper) {
return wrapper.ptr() == platform_object;
});
}
// NB: A WindowProxy is a platform object without a wrappable.
Wrappable* wrappable_impl_from(JS::Object* object)
{
auto* platform_object = object ? as_platform_object(*object) : nullptr;
if (!platform_object)
return nullptr;
return static_cast<Wrappable*>(platform_object->wrappable().ptr());
}
Wrappable const* wrappable_impl_from(JS::Object const* object)
{
auto const* platform_object = object ? as_platform_object(*object) : nullptr;
if (!platform_object)
return nullptr;
return static_cast<Wrappable const*>(platform_object->wrappable().ptr());
}
}