Truly independent web browser
LibCore: Keep location requests waiting while macOS asks the user
sideshowbarkerc269744feda8
diff --git a/Libraries/LibCore/CMakeLists.txt b/Libraries/LibCore/CMakeLists.txtindex 84d7ed28bc0..bae4425946a 100644--- a/Libraries/LibCore/CMakeLists.txt+++ b/Libraries/LibCore/CMakeLists.txt@@ -13,6 +13,7 @@ set(SOURCESEventLoopImplementation.cppEventReceiver.cppFile.cpp+ GeolocationAuthorization.cppGeolocationProvider.cppImmutableBytes.cppMimeData.cppdiff --git a/Libraries/LibCore/GeolocationAuthorization.cpp b/Libraries/LibCore/GeolocationAuthorization.cppnew file mode 100644index 00000000000..ecb71a706c2--- /dev/null+++ b/Libraries/LibCore/GeolocationAuthorization.cpp@@ -0,0 +1,27 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/Assertions.h>+#include <LibCore/GeolocationAuthorization.h>++namespace Core {++GeolocationAuthorizationAction action_for_authorization(GeolocationAuthorization authorization)+{+ switch (authorization) {+ case GeolocationAuthorization::NotDetermined:+ // The user hasn't answered the system yet, so a request fails only once they say no.+ return GeolocationAuthorizationAction::Wait;+ case GeolocationAuthorization::Denied:+ return GeolocationAuthorizationAction::Fail;+ case GeolocationAuthorization::Authorized:+ case GeolocationAuthorization::Unknown:+ return GeolocationAuthorizationAction::Proceed;+ }+ VERIFY_NOT_REACHED();+}++}diff --git a/Libraries/LibCore/GeolocationAuthorization.h b/Libraries/LibCore/GeolocationAuthorization.hnew file mode 100644index 00000000000..dfe5551509d--- /dev/null+++ b/Libraries/LibCore/GeolocationAuthorization.h@@ -0,0 +1,33 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Types.h>+#include <LibCore/Export.h>++namespace Core {++// Whether the operating system lets the browser itself find the user's location.+enum class GeolocationAuthorization : u8 {+ // The system hasn't asked the user yet, or is asking right now.+ NotDetermined,+ Authorized,+ Denied,+ // The provider can't tell.+ Unknown,+};++// What a provider does with the requests it holds when it learns the system's authorization.+enum class GeolocationAuthorizationAction : u8 {+ Wait,+ Proceed,+ Fail,+};++CORE_API GeolocationAuthorizationAction action_for_authorization(GeolocationAuthorization);++}diff --git a/Libraries/LibCore/GeolocationProvider.h b/Libraries/LibCore/GeolocationProvider.hindex b0f690b3754..fb10e899d3e 100644--- a/Libraries/LibCore/GeolocationProvider.h+++ b/Libraries/LibCore/GeolocationProvider.h@@ -12,6 +12,7 @@#include <AK/Optional.h>#include <AK/String.h>#include <LibCore/Export.h>+#include <LibCore/GeolocationAuthorization.h>namespace Core {@@ -55,6 +56,9 @@ public:virtual void cancel_current_position_request(RequestId) = 0;virtual ErrorOr<WatchId, GeolocationError> start_watching_position(SuccessCallback on_success, ErrorCallback on_error) = 0;virtual void stop_watching_position(WatchId) = 0;++ virtual GeolocationAuthorization authorization() const { return GeolocationAuthorization::Unknown; }+ Function<void()> on_authorization_change;};}diff --git a/Libraries/LibCore/GeolocationProviderCoreLocation.mm b/Libraries/LibCore/GeolocationProviderCoreLocation.mmindex d6b356b766b..4b1ac0ffd83 100644--- a/Libraries/LibCore/GeolocationProviderCoreLocation.mm+++ b/Libraries/LibCore/GeolocationProviderCoreLocation.mm@@ -50,6 +50,7 @@ public:virtual void cancel_current_position_request(RequestId) override;virtual ErrorOr<WatchId, GeolocationError> start_watching_position(SuccessCallback, ErrorCallback) override;virtual void stop_watching_position(WatchId) override;+ virtual GeolocationAuthorization authorization() const override;void did_update_locations(NSArray<CLLocation*>*);void did_fail_with_error(NSError*);@@ -273,14 +274,34 @@ void GeolocationProviderCoreLocation::did_fail_with_error(NSError* error)}}+GeolocationAuthorization GeolocationProviderCoreLocation::authorization() const+{+ if (![CLLocationManager locationServicesEnabled])+ return GeolocationAuthorization::Denied;+ if (m_location_manager.authorizationStatus == kCLAuthorizationStatusNotDetermined)+ return GeolocationAuthorization::NotDetermined;+ return location_access_authorized() ? GeolocationAuthorization::Authorized : GeolocationAuthorization::Denied;+}+void GeolocationProviderCoreLocation::did_change_authorization(){- if (!location_access_authorized()) {+ if (on_authorization_change)+ on_authorization_change();++ // NB: CoreLocation reports the authorization as soon as the location manager exists, before the user has answered+ // the system's prompt, so a request made then waits for the answer rather than failing.+ switch (action_for_authorization(authorization())) {+ case GeolocationAuthorizationAction::Wait:+ return;+ case GeolocationAuthorizationAction::Fail: {auto error = location_access_not_authorized_error();fail_current_position_requests(error);fail_all_watchers(move(error));return;}+ case GeolocationAuthorizationAction::Proceed:+ break;+ }if (!m_current_position_requests.is_empty())[m_location_manager requestLocation];diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txtindex a0faa23a6ba..820df19d4d8 100644--- a/Tests/LibCore/CMakeLists.txt+++ b/Tests/LibCore/CMakeLists.txt@@ -4,6 +4,7 @@ set(TEST_SOURCESTestLibCoreDeferredInvoke.cppTestLibCoreDirectory.cppTestLibCoreEventLoop.cpp+ TestLibCoreGeolocationAuthorization.cppTestLibCoreImmutableBytes.cppTestLibCoreMappedFile.cppTestLibCoreMimeType.cppdiff --git a/Tests/LibCore/TestLibCoreGeolocationAuthorization.cpp b/Tests/LibCore/TestLibCoreGeolocationAuthorization.cppnew file mode 100644index 00000000000..fb998b90320--- /dev/null+++ b/Tests/LibCore/TestLibCoreGeolocationAuthorization.cpp@@ -0,0 +1,30 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCore/GeolocationAuthorization.h>+#include <LibTest/TestCase.h>++using namespace Core;++TEST_CASE(not_determined_keeps_requests_waiting)+{+ EXPECT(action_for_authorization(GeolocationAuthorization::NotDetermined) == GeolocationAuthorizationAction::Wait);+}++TEST_CASE(denied_fails_requests)+{+ EXPECT(action_for_authorization(GeolocationAuthorization::Denied) == GeolocationAuthorizationAction::Fail);+}++TEST_CASE(authorized_proceeds)+{+ EXPECT(action_for_authorization(GeolocationAuthorization::Authorized) == GeolocationAuthorizationAction::Proceed);+}++TEST_CASE(unknown_proceeds)+{+ EXPECT(action_for_authorization(GeolocationAuthorization::Unknown) == GeolocationAuthorizationAction::Proceed);+}