Skip to content

Truly independent web browser

Hub+UI/Qt: Translate and find the location from the UI

Andreas Kling3da49b063a1c
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 6b782e6bc19..ccbeb5d2c49 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -3043,8 +3043,9 @@ void Application::apply_view_options(Badge<ViewImplementation>, ViewImplementati
bool Application::sites_can_ask_for_location() const
{
- // NB: Tests get their positions from emulation, so they need no provider.
- auto has_location_source = Core::GeolocationProvider::is_available() || web_content_options().is_test_mode == IsTestMode::Yes;
+ // NB: Tests and WebDriver sessions get their positions from emulation, so they need no provider.
+ auto has_location_source = Core::GeolocationProvider::is_available() || web_content_options().is_test_mode == IsTestMode::Yes
+ || browser_options().webdriver_browser_endpoint.has_value();
return settings().location_policy() == LocationPolicy::Ask && has_location_source;
}
diff --git a/Services/Hub/Application.cpp b/Services/Hub/Application.cpp
index f298235a93f..bfff4664baf 100644
--- a/Services/Hub/Application.cpp
+++ b/Services/Hub/Application.cpp
@@ -50,6 +50,18 @@ Hub::UIInfo const* Application::ui_info() const
return m_ui_info.has_value() ? &*m_ui_info : nullptr;
}
+bool Application::ui_has_location_source() const
+{
+ auto const* info = ui_info();
+ return info && info->has_location_source;
+}
+
+bool Application::ui_has_translator() const
+{
+ auto const* info = ui_info();
+ return info && info->has_translator;
+}
+
void Application::set_ui_info(Hub::UIInfo info)
{
auto scrollbar_track_press_jumps_changed = !m_ui_info.has_value() || m_ui_info->scrollbar_track_press_jumps != info.scrollbar_track_press_jumps;
diff --git a/Services/Hub/Application.h b/Services/Hub/Application.h
index 55a64b082dc..97c58578c1b 100644
--- a/Services/Hub/Application.h
+++ b/Services/Hub/Application.h
@@ -61,6 +61,8 @@ public:
virtual void notify_webdriver_window_created(String const& handle) override;
void set_ui_info(Hub::UIInfo);
StringView ui_mach_server_name() const { return m_ui_mach_server_name; }
+ bool ui_has_location_source() const;
+ bool ui_has_translator() const;
void did_resolve_external_url_handler(u64 request_id, Optional<u64> handler_id, String application_name, bool is_ladybird);
void launch_external_url(u64 handler_id, URL::URL const&, Function<void(bool)>) const;
void did_launch_external_url(u64 request_id, bool launched);
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index 5a89804af6f..eb397087953 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -315,6 +315,50 @@ void ConnectionFromUI::pip_did_close(u64 view_id, bool video_keeps_playing)
window->did_close(video_keeps_playing);
}
+void ConnectionFromUI::geolocation_position(u64 id, Core::GeolocationCoordinates coordinates)
+{
+ if (auto* provider = GeolocationProviderInUI::the())
+ provider->did_receive_position(id, move(coordinates));
+}
+
+void ConnectionFromUI::geolocation_error(u64 id, Core::GeolocationError error)
+{
+ if (auto* provider = GeolocationProviderInUI::the())
+ provider->did_receive_error(id, move(error));
+}
+
+void ConnectionFromUI::geolocation_authorization_changed(Core::GeolocationAuthorization authorization)
+{
+ if (auto* provider = GeolocationProviderInUI::the())
+ provider->did_change_authorization(authorization);
+}
+
+void ConnectionFromUI::translator_language_detected(u64 request_id, Optional<String> language)
+{
+ if (auto* translator = TranslatorInUI::the())
+ translator->did_detect_language(request_id, move(language));
+}
+
+void ConnectionFromUI::translator_target_languages_ready(u64 request_id, Vector<WebView::Translator::TargetLanguage> target_languages)
+{
+ if (auto* translator = TranslatorInUI::the())
+ translator->did_get_target_languages(request_id, move(target_languages));
+}
+
+void ConnectionFromUI::translator_translation_finished(u64 request_id, Optional<Vector<Web::TranslationRun>> runs)
+{
+ if (auto* translator = TranslatorInUI::the())
+ translator->did_translate(request_id, move(runs));
+}
+
+void ConnectionFromUI::system_time_zone_changed()
+{
+ WebView::WebContentClient::for_each_client([](WebView::WebContentClient& client) {
+ client.async_system_time_zone_changed();
+ return IterationDecision::Continue;
+ });
+}
+
void ConnectionFromUI::notification_shown(u64 id)
{
if (auto* backend = NotificationBackendInUI::the(); backend && backend->on_shown)
@@ -494,7 +538,6 @@ static WebView::SessionStore& session_store_of(WebView::IsPrivate is_private)
Messages::HubServer::InitializeResponse ConnectionFromUI::initialize()
{
- auto& application = Application::the();
auto const& options = Application::browser_options();
InitialState state;
@@ -506,7 +549,6 @@ Messages::HubServer::InitializeResponse ConnectionFromUI::initialize()
state.raw_urls = options.raw_urls;
state.crash_report_directory = WebView::CrashReportStore::the().directory();
state.devtools_port = options.devtools_port;
- state.has_translator = application.translator() != nullptr;
auto const& profile = Application::profile();
state.profile_identifier = profile.identifier();
state.profile_identity = profile.paths().identity;
diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.h
index 8136bb73585..b376b83ab7a 100644
--- a/Services/Hub/ConnectionFromUI.h
+++ b/Services/Hub/ConnectionFromUI.h
@@ -126,6 +126,13 @@ private:
virtual void notification_failed(u64 id) override;
virtual void notification_clicked(u64 id) override;
virtual void notification_closed(u64 id, bool by_user) override;
+ virtual void geolocation_position(u64 id, Core::GeolocationCoordinates coordinates) override;
+ virtual void geolocation_error(u64 id, Core::GeolocationError error) override;
+ virtual void geolocation_authorization_changed(Core::GeolocationAuthorization authorization) override;
+ virtual void system_time_zone_changed() override;
+ virtual void translator_language_detected(u64 request_id, Optional<String> language) override;
+ virtual void translator_target_languages_ready(u64 request_id, Vector<WebView::Translator::TargetLanguage> target_languages) override;
+ virtual void translator_translation_finished(u64 request_id, Optional<Vector<Web::TranslationRun>> runs) override;
virtual void external_url_handler_resolved(u64 request_id, Optional<u64> handler_id, String application_name, bool is_ladybird) override;
virtual void external_url_launched(u64 request_id, bool launched) override;
virtual void omnibox_open(u64 client_id, WebView::IsPrivate is_private) override;
diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipc
index 9862465602c..d301c34f427 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -1,5 +1,6 @@
#include <LibHTTP/Header.h>
#include <LibCompositing/Types.h>
+#include <LibCore/GeolocationAuthorization.h>
#include <LibGfx/Color.h>
#include <LibGfx/Cursor.h>
#include <LibGfx/GpuDeviceIdentity.h>
@@ -12,6 +13,7 @@
#include <LibWebCommon/NotificationsAPI/NotificationData.h>
#include <LibURL/URL.h>
#include <LibWebCommon/CSS/PreferredColorScheme.h>
+#include <LibWebCommon/Page/Translation.h>
#include <LibWebCommon/WebView/DictionaryLookup.h>
#include <LibWebCommon/HTML/FileFilter.h>
#include <LibWebCommon/HTML/SelectItem.h>
@@ -219,6 +221,9 @@ struct Hub::UIInfo {
Optional<String> system_font_family;
Web::CSS::PreferredColorScheme interface_color_scheme;
bool supports_picture_in_picture;
+ bool has_location_source;
+ bool has_translator;
+ Optional<Core::GeolocationAuthorization> location_authorization;
bool supports_selection_clipboard;
bool supports_system_menu_bar;
bool supports_vertical_tabs;
@@ -242,7 +247,6 @@ struct Hub::InitialState {
Vector<ByteString> raw_urls;
ByteString crash_report_directory;
Optional<u16> devtools_port;
- bool has_translator;
ByteString profile_identifier;
ByteString profile_identity;
bool profile_is_temporary;
@@ -294,6 +298,17 @@ endpoint HubClient
pip_close(u64 view_id) =|
notification_show(u64 id, u64 page_id, Optional<u64> replaces, Web::NotificationsAPI::NotificationData data, String attribution, Optional<Gfx::ShareableBitmap> icon) =|
notification_close(u64 id) =|
+ geolocation_start() =|
+ geolocation_request_position(u64 id) =|
+ geolocation_cancel_position_request(u64 id) =|
+ geolocation_start_watching(u64 id) =|
+ geolocation_stop_watching(u64 id) =|
+ translator_detect_language(u64 request_id, Web::TranslationLanguageSample sample) =|
+ translator_target_languages(u64 request_id, String source_language, Vector<String> candidates) =|
+ translator_create_session(u64 session_id, String source_language, String target_language) =|
+ translator_close_session(u64 session_id) =|
+ translator_translate(u64 request_id, u64 session_id, Web::TranslationUnit unit) =|
+ translator_open_language_download_settings() =|
app_focus_view_from_notification(u64 view_id) =|
app_open_url_in_new_tab_from_notification(URL::URL url, Optional<u64> view_id, WebView::IsPrivate is_private) =|
external_url_resolve_handler(u64 request_id, URL::URL url) =|
diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipc
index 4b76df28daa..23b6cbe709d 100644
--- a/Services/Hub/HubServer.ipc
+++ b/Services/Hub/HubServer.ipc
@@ -2,6 +2,8 @@
#include <AK/StringUtils.h>
#include <Hub/HubClientEndpoint.h>
#include <LibCore/AnonymousBuffer.h>
+#include <LibCore/GeolocationAuthorization.h>
+#include <LibCore/GeolocationProvider.h>
#include <LibGfx/Color.h>
#include <LibGfx/Point.h>
#include <LibGfx/ShareableBitmap.h>
@@ -16,8 +18,32 @@
#include <LibWebView/ProcessSnapshot.h>
#include <LibWebView/SessionHistory.h>
#include <LibWebView/SessionStore.h>
+#include <LibWebView/Translator.h>
#include <LibWebView/ViewImplementation.h>
+[ExistingCpp]
+struct WebView::Translator::TargetLanguage {
+ String language;
+ bool needs_download;
+}
+
+[ExistingCpp]
+struct Core::GeolocationCoordinates {
+ double latitude;
+ double longitude;
+ double accuracy;
+ Optional<double> altitude;
+ Optional<double> altitude_accuracy;
+ Optional<double> heading;
+ Optional<double> speed;
+}
+
+[ExistingCpp]
+struct Core::GeolocationError {
+ Core::GeolocationError::Type type;
+ String message;
+}
+
[ExistingCpp]
struct WebView::ViewImplementation::ScreenRects {
Vector<Web::DevicePixelRect> rects;
@@ -272,6 +298,13 @@ endpoint HubServer
notification_failed(u64 id) =|
notification_clicked(u64 id) =|
notification_closed(u64 id, bool by_user) =|
+ geolocation_position(u64 id, Core::GeolocationCoordinates coordinates) =|
+ geolocation_error(u64 id, Core::GeolocationError error) =|
+ geolocation_authorization_changed(Core::GeolocationAuthorization authorization) =|
+ system_time_zone_changed() =|
+ translator_language_detected(u64 request_id, Optional<String> language) =|
+ translator_target_languages_ready(u64 request_id, Vector<WebView::Translator::TargetLanguage> target_languages) =|
+ translator_translation_finished(u64 request_id, Optional<Vector<Web::TranslationRun>> runs) =|
external_url_handler_resolved(u64 request_id, Optional<u64> handler_id, String application_name, bool is_ladybird) =|
external_url_launched(u64 request_id, bool launched) =|
omnibox_open(u64 client_id, WebView::IsPrivate is_private) =|
diff --git a/Services/Hub/Proxies.cpp b/Services/Hub/Proxies.cpp
index 6ec0e7a715d..ba4b8950a2b 100644
--- a/Services/Hub/Proxies.cpp
+++ b/Services/Hub/Proxies.cpp
@@ -93,6 +93,172 @@ void NotificationBackendInUI::close(u64 id)
ui->async_notification_close(id);
}
+static GeolocationProviderInUI* s_geolocation_provider = nullptr;
+
+GeolocationProviderInUI::GeolocationProviderInUI()
+{
+ s_geolocation_provider = this;
+ if (auto const* info = Application::the().ui_info(); info && info->location_authorization.has_value())
+ m_authorization = *info->location_authorization;
+ // NB: The UI's provider tells the Hub whenever the authorization changes, but only once it exists.
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_geolocation_start();
+}
+
+GeolocationProviderInUI::~GeolocationProviderInUI()
+{
+ s_geolocation_provider = nullptr;
+}
+
+GeolocationProviderInUI* GeolocationProviderInUI::the()
+{
+ return s_geolocation_provider;
+}
+
+ErrorOr<Core::GeolocationProvider::RequestId, Core::GeolocationError> GeolocationProviderInUI::request_current_position(SuccessCallback on_success, ErrorCallback on_error)
+{
+ auto id = m_next_id++;
+ m_clients.set(id, make_ref_counted<Client>(Kind::CurrentPosition, move(on_success), move(on_error)));
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_geolocation_request_position(id);
+ return id;
+}
+
+void GeolocationProviderInUI::cancel_current_position_request(RequestId id)
+{
+ if (!m_clients.remove(id))
+ return;
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_geolocation_cancel_position_request(id);
+}
+
+ErrorOr<Core::GeolocationProvider::WatchId, Core::GeolocationError> GeolocationProviderInUI::start_watching_position(SuccessCallback on_success, ErrorCallback on_error)
+{
+ auto id = m_next_id++;
+ m_clients.set(id, make_ref_counted<Client>(Kind::Watch, move(on_success), move(on_error)));
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_geolocation_start_watching(id);
+ return id;
+}
+
+void GeolocationProviderInUI::stop_watching_position(WatchId id)
+{
+ if (!m_clients.remove(id))
+ return;
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_geolocation_stop_watching(id);
+}
+
+void GeolocationProviderInUI::did_receive_position(u64 id, Core::GeolocationCoordinates coordinates)
+{
+ RefPtr<Client> client = m_clients.get(id).value_or(nullptr);
+ if (!client)
+ return;
+ if (client->kind == Kind::CurrentPosition)
+ m_clients.remove(id);
+ client->on_success(move(coordinates));
+}
+
+void GeolocationProviderInUI::did_receive_error(u64 id, Core::GeolocationError error)
+{
+ RefPtr<Client> client = m_clients.get(id).value_or(nullptr);
+ if (!client)
+ return;
+ if (client->kind == Kind::CurrentPosition)
+ m_clients.remove(id);
+ client->on_error(move(error));
+}
+
+void GeolocationProviderInUI::did_change_authorization(Core::GeolocationAuthorization authorization)
+{
+ m_authorization = authorization;
+ if (on_authorization_change)
+ on_authorization_change();
+}
+
+static TranslatorInUI* s_translator = nullptr;
+
+TranslatorInUI::TranslatorInUI()
+{
+ s_translator = this;
+}
+
+TranslatorInUI::~TranslatorInUI()
+{
+ s_translator = nullptr;
+}
+
+TranslatorInUI* TranslatorInUI::the()
+{
+ return s_translator;
+}
+
+void TranslatorInUI::detect_language(Web::TranslationLanguageSample const& sample, Function<void(Optional<String>)> on_complete)
+{
+ auto request_id = m_next_request_id++;
+ m_pending_detections.set(request_id, move(on_complete));
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_translator_detect_language(request_id, sample);
+}
+
+void TranslatorInUI::did_detect_language(u64 request_id, Optional<String> language)
+{
+ if (auto on_complete = m_pending_detections.take(request_id); on_complete.has_value())
+ (*on_complete)(move(language));
+}
+
+void TranslatorInUI::target_languages(String const& source_language, Vector<String> const& candidates, Function<void(Vector<TargetLanguage>)> on_complete)
+{
+ auto request_id = m_next_request_id++;
+ m_pending_target_languages.set(request_id, move(on_complete));
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_translator_target_languages(request_id, source_language, candidates);
+}
+
+void TranslatorInUI::did_get_target_languages(u64 request_id, Vector<TargetLanguage> target_languages)
+{
+ if (auto on_complete = m_pending_target_languages.take(request_id); on_complete.has_value())
+ (*on_complete)(move(target_languages));
+}
+
+// NB: The Hub never waits for the UI, so it picks the session's ID itself.
+WebView::Translator::SessionId TranslatorInUI::create_session(String const& source_language, String const& target_language)
+{
+ auto session_id = m_next_session_id++;
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_translator_create_session(session_id, source_language, target_language);
+ return session_id;
+}
+
+void TranslatorInUI::close_session(SessionId session_id)
+{
+ m_pending_translations.remove_all_matching([&](auto, auto const& translation) {
+ return translation.session_id == session_id;
+ });
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_translator_close_session(session_id);
+}
+
+void TranslatorInUI::translate(SessionId session_id, Web::TranslationUnit unit, Function<void(Optional<Vector<Web::TranslationRun>>)> on_complete)
+{
+ auto request_id = m_next_request_id++;
+ m_pending_translations.set(request_id, { session_id, move(on_complete) });
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_translator_translate(request_id, session_id, unit);
+}
+
+void TranslatorInUI::did_translate(u64 request_id, Optional<Vector<Web::TranslationRun>> runs)
+{
+ if (auto translation = m_pending_translations.take(request_id); translation.has_value())
+ translation->on_complete(move(runs));
+}
+
+void TranslatorInUI::open_language_download_settings()
+{
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_translator_open_language_download_settings();
+}
+
ExternalURLHandlerInUI::ExternalURLHandlerInUI(u64 handler_id, String application_name, bool is_ladybird)
: ExternalURLHandler(move(application_name), is_ladybird)
, m_handler_id(handler_id)
diff --git a/Services/Hub/Proxies.h b/Services/Hub/Proxies.h
index aa84249496a..ea4e3f65e5d 100644
--- a/Services/Hub/Proxies.h
+++ b/Services/Hub/Proxies.h
@@ -6,10 +6,15 @@
#pragma once
+#include <AK/HashMap.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/RefCounted.h>
#include <Hub/View.h>
+#include <LibCore/GeolocationProvider.h>
#include <LibWebView/ExternalURLHandler.h>
#include <LibWebView/NotificationBackend.h>
#include <LibWebView/PictureInPictureWindow.h>
+#include <LibWebView/Translator.h>
namespace Hub {
@@ -48,6 +53,80 @@ private:
virtual void close(u64 id) override;
};
+class GeolocationProviderInUI final : public Core::GeolocationProvider {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ GeolocationProviderInUI();
+ virtual ~GeolocationProviderInUI() override;
+
+ static GeolocationProviderInUI* the();
+
+ void did_receive_position(u64 id, Core::GeolocationCoordinates);
+ void did_receive_error(u64 id, Core::GeolocationError);
+ void did_change_authorization(Core::GeolocationAuthorization);
+
+private:
+ enum class Kind : u8 {
+ CurrentPosition,
+ Watch,
+ };
+ // NB: A callback may stop or start a watch, so a client must stay alive while its callbacks run.
+ struct Client : public RefCounted<Client> {
+ Client(Kind kind, SuccessCallback on_success, ErrorCallback on_error)
+ : kind(kind)
+ , on_success(move(on_success))
+ , on_error(move(on_error))
+ {
+ }
+
+ Kind kind;
+ SuccessCallback on_success;
+ ErrorCallback on_error;
+ };
+
+ virtual ErrorOr<RequestId, Core::GeolocationError> request_current_position(SuccessCallback, ErrorCallback) override;
+ virtual void cancel_current_position_request(RequestId) override;
+ virtual ErrorOr<WatchId, Core::GeolocationError> start_watching_position(SuccessCallback, ErrorCallback) override;
+ virtual void stop_watching_position(WatchId) override;
+ virtual Core::GeolocationAuthorization authorization() const override { return m_authorization; }
+
+ HashMap<u64, NonnullRefPtr<Client>> m_clients;
+ u64 m_next_id { 1 };
+ Core::GeolocationAuthorization m_authorization { Core::GeolocationAuthorization::Unknown };
+};
+
+class TranslatorInUI final : public WebView::Translator {
+public:
+ TranslatorInUI();
+ virtual ~TranslatorInUI() override;
+
+ static TranslatorInUI* the();
+
+ void did_detect_language(u64 request_id, Optional<String> language);
+ void did_get_target_languages(u64 request_id, Vector<TargetLanguage>);
+ void did_translate(u64 request_id, Optional<Vector<Web::TranslationRun>>);
+
+private:
+ struct PendingTranslation {
+ SessionId session_id { 0 };
+ Function<void(Optional<Vector<Web::TranslationRun>>)> on_complete;
+ };
+
+ virtual void detect_language(Web::TranslationLanguageSample const&, Function<void(Optional<String>)> on_complete) override;
+ virtual void target_languages(String const& source_language, Vector<String> const& candidates, Function<void(Vector<TargetLanguage>)> on_complete) override;
+ virtual SessionId create_session(String const& source_language, String const& target_language) override;
+ virtual void close_session(SessionId) override;
+ virtual void translate(SessionId, Web::TranslationUnit, Function<void(Optional<Vector<Web::TranslationRun>>)> on_complete) override;
+ virtual void open_language_download_settings() override;
+
+ HashMap<u64, Function<void(Optional<String>)>> m_pending_detections;
+ HashMap<u64, Function<void(Vector<TargetLanguage>)>> m_pending_target_languages;
+ HashMap<u64, PendingTranslation> m_pending_translations;
+ u64 m_next_request_id { 1 };
+ SessionId m_next_session_id { 1 };
+};
+
class ExternalURLHandlerInUI final : public WebView::ExternalURLHandler {
public:
ExternalURLHandlerInUI(u64 handler_id, String application_name, bool is_ladybird);
diff --git a/Services/Hub/main.cpp b/Services/Hub/main.cpp
index 38744e75a09..e090ceebc8c 100644
--- a/Services/Hub/main.cpp
+++ b/Services/Hub/main.cpp
@@ -6,6 +6,8 @@
#include <Hub/Application.h>
#include <Hub/ConnectionFromUI.h>
+#include <Hub/Proxies.h>
+#include <LibCore/GeolocationProvider.h>
#include <LibIPC/StartupTransport.h>
#include <LibMain/Main.h>
@@ -32,6 +34,13 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
if (!Hub::Application::browser_options().headless_mode.has_value()) {
auto transport = TRY(IPC::take_over_startup_transport(application->ui_mach_server_name()));
connection = Hub::ConnectionFromUI::construct(move(transport));
+
+ // NB: Translating and finding the location need a run loop on macOS, and the UI is the process that has one.
+ if (application->ui_has_translator())
+ application->set_translator(make<Hub::TranslatorInUI>());
+ Core::GeolocationProvider::set_provider_functions(
+ []() -> ErrorOr<NonnullOwnPtr<Core::GeolocationProvider>> { return make<Hub::GeolocationProviderInUI>(); },
+ [] { return Hub::Application::the().ui_has_location_source(); });
}
return application->execute();
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index cb8456443c0..62847d9dbd2 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -7,6 +7,7 @@
#include <AK/Hex.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/Environment.h>
+#include <LibCore/GeolocationProvider.h>
#include <LibCore/Process.h>
#include <LibCore/Socket.h>
#include <LibCore/System.h>
@@ -38,6 +39,7 @@
#include <Hub/ActionMirror.h>
#include <LibWebView/NoOpNotificationBackend.h>
#include <LibWebView/SessionStore.h>
+#include <LibWebView/Translator.h>
#include <LibWebView/URL.h>
#include <LibWebView/Utilities.h>
#include <LibWebView/WebContentPage.h>
@@ -64,6 +66,10 @@
# include <UI/Qt/ScrollbarPreferencesMac.h>
#endif
+#if defined(LADYBIRD_HAS_TRANSLATOR)
+# include <UI/Qt/TranslatorMac.h>
+#endif
+
#if defined(AK_OS_LINUX)
# include <UI/Qt/X11Window.h>
#endif
@@ -95,9 +101,12 @@
# include <QUtiMimeConverter>
#endif
+#if defined(AK_OS_MACOS) || defined(AK_OS_WINDOWS)
+# include <LibCore/TimeZoneWatcher.h>
+#endif
+
#if defined(AK_OS_WINDOWS)
# include <AK/Windows.h>
-# include <LibCore/TimeZoneWatcher.h>
# include <QAbstractNativeEventFilter>
#endif
@@ -676,6 +685,13 @@ Hub::UIInfo Application::current_ui_info() const
info.system_font_family = system_font_family();
info.interface_color_scheme = interface_color_scheme();
info.supports_picture_in_picture = supports_picture_in_picture();
+ info.has_location_source = Core::GeolocationProvider::is_available();
+ // NB: The Hub reads this before it first asks for a position, which is long before the UI's provider could tell it.
+ if (info.has_location_source) {
+ if (auto provider = Core::GeolocationProvider::create(); !provider.is_error())
+ info.location_authorization = provider.value()->authorization();
+ }
+ info.has_translator = has_translator();
info.supports_selection_clipboard = supports_clipboard_type(ClipboardType::Selection);
info.supports_system_menu_bar = supports_system_menu_bar();
info.supports_vertical_tabs = supports_vertical_tabs();
@@ -694,6 +710,10 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)
{
auto hub_path = TRY(hub_executable_path());
+# if defined(LADYBIRD_HAS_TRANSLATOR)
+ m_translator = create_platform_translator();
+# endif
+
IPC::MessageBuffer ui_info_buffer;
IPC::Encoder encoder { ui_info_buffer };
TRY(encoder.encode(current_ui_info()));
@@ -789,7 +809,6 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)
m_browser_options.urls = move(state.urls);
m_browser_options.raw_urls = move(state.raw_urls);
m_browser_options.devtools_port = state.devtools_port;
- m_has_translator = state.has_translator;
# if defined(AK_OS_MACOS)
set_profile_application_icon(state.profile_identifier, state.profile_identity, state.profile_is_temporary);
# endif
@@ -806,6 +825,17 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)
};
});
+# if defined(AK_OS_MACOS)
+ // NB: The system tells of this through the run loop, and only this process has one.
+ if (!Core::Environment::has("TZ"sv)) {
+ m_time_zone_watcher = TRY(Core::TimeZoneWatcher::create());
+ m_time_zone_watcher->on_time_zone_changed = [] {
+ if (auto* hub = HubConnection::the_if_connected())
+ hub->async_system_time_zone_changed();
+ };
+ }
+# endif
+
QObject::connect(&SystemAppearance::the(), &SystemAppearance::changed, [this] {
if (auto* hub = HubConnection::the_if_connected())
hub->async_ui_info_changed(current_ui_info());
@@ -841,7 +871,7 @@ Vector<WebView::ProcessSnapshot> Application::process_snapshots()
return snapshots;
}
-bool Application::has_translator() const { return m_has_translator; }
+bool Application::has_translator() const { return m_translator != nullptr; }
Optional<int> Application::exit_status_if_done() const { return m_exit_status_if_done; }
ErrorOr<int> Application::execute()
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index a4d73f31ae7..a5a2e44b742 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -264,13 +264,18 @@ private:
Core::EventLoop* m_event_loop { nullptr };
WebView::BrowserOptions m_browser_options;
- bool m_has_translator { false };
Optional<int> m_exit_status_if_done;
Core::Platform::ProcessStatistics m_ui_process_statistics;
+# if defined(AK_OS_MACOS)
+ OwnPtr<Core::TimeZoneWatcher> m_time_zone_watcher;
+# endif
#else
OwnPtr<Hub::Application> m_hub;
RefPtr<Hub::ConnectionFromUI> m_connection_from_ui;
#endif
+ WebView::Translator* translator() { return m_translator.ptr(); }
+
+ OwnPtr<WebView::Translator> m_translator;
OwnPtr<WebView::Settings> m_settings;
OwnPtr<WebView::BookmarkStore> m_bookmark_store;
RefPtr<HubConnection> m_hub_connection;
diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cpp
index d9fdc024080..37c577291ba 100644
--- a/UI/Qt/HubConnection.cpp
+++ b/UI/Qt/HubConnection.cpp
@@ -465,6 +465,142 @@ void HubConnection::notification_close(u64 id)
Application::the().notification_backend().close(id);
}
+Core::GeolocationProvider* HubConnection::ensure_geolocation_provider()
+{
+ if (m_geolocation_provider)
+ return m_geolocation_provider.ptr();
+
+ auto provider = Core::GeolocationProvider::create();
+ if (provider.is_error()) {
+ dbgln("Unable to create the geolocation provider: {}", provider.error());
+ return nullptr;
+ }
+ m_geolocation_provider = provider.release_value();
+ m_geolocation_provider->on_authorization_change = [this] {
+ async_geolocation_authorization_changed(m_geolocation_provider->authorization());
+ };
+ async_geolocation_authorization_changed(m_geolocation_provider->authorization());
+ return m_geolocation_provider.ptr();
+}
+
+static Core::GeolocationError geolocation_provider_unavailable_error()
+{
+ return { Core::GeolocationError::Type::PositionUnavailable, "The location provider is not available"_string };
+}
+
+// NB: Our provider tells the Hub of changes to the system's authorization from when it exists, so the Hub has it made
+// as soon as it needs the location rather than with the first position request.
+void HubConnection::geolocation_start()
+{
+ (void)ensure_geolocation_provider();
+}
+
+void HubConnection::geolocation_request_position(u64 id)
+{
+ auto* provider = ensure_geolocation_provider();
+ if (!provider) {
+ async_geolocation_error(id, geolocation_provider_unavailable_error());
+ return;
+ }
+ auto request = provider->request_current_position(
+ [this, id](Core::GeolocationCoordinates coordinates) {
+ m_geolocation_requests.remove(id);
+ async_geolocation_position(id, coordinates);
+ },
+ [this, id](Core::GeolocationError error) {
+ m_geolocation_requests.remove(id);
+ async_geolocation_error(id, error);
+ });
+ if (request.is_error()) {
+ async_geolocation_error(id, request.release_error());
+ return;
+ }
+ m_geolocation_requests.set(id, request.value());
+}
+
+void HubConnection::geolocation_cancel_position_request(u64 id)
+{
+ if (auto request = m_geolocation_requests.take(id); request.has_value())
+ m_geolocation_provider->cancel_current_position_request(*request);
+}
+
+void HubConnection::geolocation_start_watching(u64 id)
+{
+ auto* provider = ensure_geolocation_provider();
+ if (!provider) {
+ async_geolocation_error(id, geolocation_provider_unavailable_error());
+ return;
+ }
+ auto watch = provider->start_watching_position(
+ [this, id](Core::GeolocationCoordinates coordinates) { async_geolocation_position(id, coordinates); },
+ [this, id](Core::GeolocationError error) { async_geolocation_error(id, error); });
+ if (watch.is_error()) {
+ async_geolocation_error(id, watch.release_error());
+ return;
+ }
+ m_geolocation_watches.set(id, watch.value());
+}
+
+void HubConnection::geolocation_stop_watching(u64 id)
+{
+ if (auto watch = m_geolocation_watches.take(id); watch.has_value())
+ m_geolocation_provider->stop_watching_position(*watch);
+}
+
+void HubConnection::translator_detect_language(u64 request_id, Web::TranslationLanguageSample sample)
+{
+ auto* translator = Application::the().translator();
+ if (!translator) {
+ async_translator_language_detected(request_id, {});
+ return;
+ }
+ translator->detect_language(sample, [this, request_id](Optional<String> language) {
+ async_translator_language_detected(request_id, language);
+ });
+}
+
+void HubConnection::translator_target_languages(u64 request_id, String source_language, Vector<String> candidates)
+{
+ auto* translator = Application::the().translator();
+ if (!translator) {
+ async_translator_target_languages_ready(request_id, {});
+ return;
+ }
+ translator->target_languages(source_language, candidates, [this, request_id](Vector<WebView::Translator::TargetLanguage> target_languages) {
+ async_translator_target_languages_ready(request_id, target_languages);
+ });
+}
+
+void HubConnection::translator_create_session(u64 session_id, String source_language, String target_language)
+{
+ if (auto* translator = Application::the().translator())
+ m_translator_sessions.set(session_id, translator->create_session(source_language, target_language));
+}
+
+void HubConnection::translator_close_session(u64 session_id)
+{
+ if (auto session = m_translator_sessions.take(session_id); session.has_value())
+ Application::the().translator()->close_session(*session);
+}
+
+void HubConnection::translator_translate(u64 request_id, u64 session_id, Web::TranslationUnit unit)
+{
+ auto session = m_translator_sessions.get(session_id);
+ if (!session.has_value()) {
+ async_translator_translation_finished(request_id, {});
+ return;
+ }
+ Application::the().translator()->translate(*session, move(unit), [this, request_id](Optional<Vector<Web::TranslationRun>> runs) {
+ async_translator_translation_finished(request_id, runs);
+ });
+}
+
+void HubConnection::translator_open_language_download_settings()
+{
+ if (auto* translator = Application::the().translator())
+ translator->open_language_download_settings();
+}
+
void HubConnection::app_focus_view_from_notification(u64 view_id)
{
Application::the().focus_view_from_notification(view_id);
diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.h
index 9e90e018b91..0eafa4f8524 100644
--- a/UI/Qt/HubConnection.h
+++ b/UI/Qt/HubConnection.h
@@ -9,6 +9,7 @@
#include <AK/JsonObject.h>
#include <Hub/HubClientEndpoint.h>
#include <Hub/HubServerEndpoint.h>
+#include <LibCore/GeolocationProvider.h>
#include <LibIPC/ConnectionToServer.h>
#include <LibWebView/ReportSubmission.h>
@@ -37,9 +38,15 @@ public:
void send_settings_changes(JsonValue const&);
private:
+ Core::GeolocationProvider* ensure_geolocation_provider();
+
bool m_did_initialize { false };
u64 m_next_report_post_id { 1 };
HashMap<u64, Function<void(WebView::ReportSubmission::PostResponse)>> m_report_posts;
+ OwnPtr<Core::GeolocationProvider> m_geolocation_provider;
+ HashMap<u64, Core::GeolocationProvider::RequestId> m_geolocation_requests;
+ HashMap<u64, Core::GeolocationProvider::WatchId> m_geolocation_watches;
+ HashMap<u64, WebView::Translator::SessionId> m_translator_sessions;
// The Hub owns the settings. The settings here are its last word, plus the changes it has yet to confirm.
struct SettingsChange {
@@ -67,6 +74,17 @@ private:
virtual void pip_close(u64 view_id) override;
virtual void notification_show(u64 id, u64 page_id, Optional<u64> replaces, Web::NotificationsAPI::NotificationData data, String attribution, Optional<Gfx::ShareableBitmap> icon) override;
virtual void notification_close(u64 id) override;
+ virtual void geolocation_start() override;
+ virtual void geolocation_request_position(u64 id) override;
+ virtual void geolocation_cancel_position_request(u64 id) override;
+ virtual void geolocation_start_watching(u64 id) override;
+ virtual void geolocation_stop_watching(u64 id) override;
+ virtual void translator_detect_language(u64 request_id, Web::TranslationLanguageSample sample) override;
+ virtual void translator_target_languages(u64 request_id, String source_language, Vector<String> candidates) override;
+ virtual void translator_create_session(u64 session_id, String source_language, String target_language) override;
+ virtual void translator_close_session(u64 session_id) override;
+ virtual void translator_translate(u64 request_id, u64 session_id, Web::TranslationUnit unit) override;
+ virtual void translator_open_language_download_settings() override;
virtual void app_focus_view_from_notification(u64 view_id) override;
virtual void app_open_url_in_new_tab_from_notification(URL::URL url, Optional<u64> view_id, WebView::IsPrivate is_private) override;
virtual void external_url_resolve_handler(u64 request_id, URL::URL url) override;