LibWeb: Implement emulated Geolocation position retrieval

This implements enough of the Geolocation spec that it is now possible
for websites to retrieve the current geo position or try to watch for
updates (which currently never happen).

As it stands now, it only returns a single emulated position that points
to San Francisco.
This commit is contained in:
Jelle Raaijmakers
2025-06-22 20:26:03 +02:00
committed by Jelle Raaijmakers
parent 53c35c5d3b
commit 71f03cb785
Notes: github-actions[bot] 2025-06-24 09:35:05 +00:00
11 changed files with 477 additions and 48 deletions

View File

@@ -9,6 +9,7 @@
#include <LibGfx/SkiaBackendContext.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Geolocation/GeolocationCoordinates.h>
#include <LibWeb/HTML/BrowsingContextGroup.h>
#include <LibWeb/HTML/DocumentState.h>
#include <LibWeb/HTML/Navigation.h>
@@ -74,6 +75,8 @@ TraversableNavigable::~TraversableNavigable() = default;
void TraversableNavigable::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
if (m_emulated_position_data.has<GC::Ref<Geolocation::GeolocationCoordinates>>())
visitor.visit(m_emulated_position_data.get<GC::Ref<Geolocation::GeolocationCoordinates>>());
visitor.visit(m_session_history_entries);
visitor.visit(m_session_history_traversal_queue);
visitor.visit(m_storage_shed);
@@ -164,6 +167,24 @@ WebIDL::ExceptionOr<GC::Ref<TraversableNavigable>> TraversableNavigable::create_
auto traversable = TRY(create_a_new_top_level_traversable(page, nullptr, {}));
page->set_top_level_traversable(traversable);
// AD-HOC: Set the default top-level emulated position data for the traversable, which points to Market St. SF.
// FIXME: We should not emulate by default, but ask the user what to do. E.g. disable Geolocation, set an emulated
// position, or allow Ladybird to engage with the system's geolocation services. This is completely separate
// from the permission model for "powerful features" such as Geolocation.
auto& realm = traversable->active_document()->realm();
auto emulated_position_coordinates = realm.create<Geolocation::GeolocationCoordinates>(
realm,
Geolocation::CoordinatesData {
.accuracy = 100.0,
.latitude = 37.7647658,
.longitude = -122.4345892,
.altitude = 60.0,
.altitude_accuracy = 10.0,
.heading = 0.0,
.speed = 0.0,
});
traversable->set_emulated_position_data(emulated_position_coordinates);
// AD-HOC: Mark the about:blank document as finished parsing if we're only going to about:blank
// Skip the initial navigation as well. This matches the behavior of the window open steps.
@@ -1434,4 +1455,18 @@ void TraversableNavigable::start_display_list_rendering(NonnullRefPtr<Painting::
m_rendering_thread.enqueue_rendering_task(move(display_list), move(scroll_state_snapshot), move(backing_store), move(callback));
}
// https://w3c.github.io/geolocation/#dfn-emulated-position-data
Geolocation::EmulatedPositionData const& TraversableNavigable::emulated_position_data() const
{
VERIFY(is_top_level_traversable());
return m_emulated_position_data;
}
// https://w3c.github.io/geolocation/#dfn-emulated-position-data
void TraversableNavigable::set_emulated_position_data(Geolocation::EmulatedPositionData data)
{
VERIFY(is_top_level_traversable());
m_emulated_position_data = data;
}
}