Truly independent web browser
LibWebView+Hub+UI/Qt: Ask the UI for microphone consent
Andreas Kling46ce210c2505
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txtindex fb2a0a6fc85..aadfac8a5e7 100644--- a/Libraries/LibWebView/CMakeLists.txt+++ b/Libraries/LibWebView/CMakeLists.txt@@ -103,14 +103,13 @@ if (APPLE)list(APPEND SOURCESCrashReportPOSIX.cppLaunchURLsMacOS.cpp- MicrophoneAuthorizerMacOS.mmNotificationBackendMacOS.mmPlatformColorsMacOS.cppProcessReaper.cppQuarantineMacOS.mmStringConversionsMacOS.mm)- set_source_files_properties(MicrophoneAuthorizerMacOS.mm NotificationBackendMacOS.mm QuarantineMacOS.mm StringConversionsMacOS.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")+ set_source_files_properties(NotificationBackendMacOS.mm QuarantineMacOS.mm StringConversionsMacOS.mm PROPERTIES COMPILE_FLAGS "-fobjc-arc")elseif (LINUX)list(APPEND SOURCES AudioDaemonSocket.cpp CrashReportPOSIX.cpp)endif()@@ -182,7 +181,7 @@ if (LINUX)endif()if (APPLE)- target_link_libraries(LibWebView PRIVATE "-framework AVFoundation" "-framework Carbon" "-framework CoreServices" "-framework Foundation" "-framework UserNotifications")+ target_link_libraries(LibWebView PRIVATE "-framework Carbon" "-framework CoreServices" "-framework Foundation" "-framework UserNotifications")endif()if (LINUX OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")diff --git a/Meta/DebugEntitlements.plist b/Meta/DebugEntitlements.plistindex 19aa0834a6e..2259f767881 100644--- a/Meta/DebugEntitlements.plist+++ b/Meta/DebugEntitlements.plist@@ -4,6 +4,8 @@<dict><key>com.apple.security.get-task-allow</key><true/>+ <key>com.apple.security.device.audio-input</key>+ <true/><key>com.apple.security.personal-information.location</key><true/></dict>diff --git a/Meta/Entitlements.plist b/Meta/Entitlements.plistindex b8cf6f9ee0c..e7b33d857e3 100644--- a/Meta/Entitlements.plist+++ b/Meta/Entitlements.plist@@ -2,6 +2,8 @@<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>+ <key>com.apple.security.device.audio-input</key>+ <true/><key>com.apple.security.personal-information.location</key><true/></dict>diff --git a/Services/Hub/Application.cpp b/Services/Hub/Application.cppindex 640b3391c92..06757c1a31e 100644--- a/Services/Hub/Application.cpp+++ b/Services/Hub/Application.cpp@@ -15,10 +15,6 @@#include <LibIPC/Decoder.h>#include <LibWebView/WebContentPage.h>-#if defined(AK_OS_MACOS)-# include <LibWebView/MicrophoneAuthorizerMacOS.h>-#endif-namespace Hub {Application::Application(ApplicationHost* host)@@ -351,10 +347,9 @@ Core::EventLoop& Application::create_platform_event_loop()NonnullOwnPtr<WebView::MicrophoneAuthorizer> Application::create_microphone_authorizer(){-#if defined(AK_OS_MACOS)- if (!browser_options().headless_mode.has_value())- return make<WebView::MicrophoneAuthorizerMacOS>();-#endif+ // NB: The UI tells of no authorization where the system has no say over the microphone.+ if (auto const* info = ui_info(); info && info->microphone_authorization.has_value())+ return make<MicrophoneAuthorizerInUI>(*info->microphone_authorization);return WebView::Application::create_microphone_authorizer();}diff --git a/Services/Hub/CMakeLists.txt b/Services/Hub/CMakeLists.txtindex 8cffb918bee..ac37293ab58 100644--- a/Services/Hub/CMakeLists.txt+++ b/Services/Hub/CMakeLists.txt@@ -31,5 +31,4 @@ add_executable(Hub main.cpp)target_include_directories(Hub PRIVATE ${LADYBIRD_SOURCE_DIR}/Services/)target_link_libraries(Hub PRIVATE hubservice hubipc LibCore LibHTTP LibIPC LibMain LibRequests LibURL LibWebView)-# NB: The hardened runtime refuses a process that asks for the microphone unless it has this entitlement.-ladybird_sign_helper_process(Hub ENTITLEMENTS com.apple.security.device.audio-input)+ladybird_sign_helper_process(Hub)diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cppindex 32539cc624a..f30126e2b9a 100644--- a/Services/Hub/ConnectionFromUI.cpp+++ b/Services/Hub/ConnectionFromUI.cpp@@ -288,6 +288,12 @@ void ConnectionFromUI::geolocation_authorization_changed(Core::GeolocationAuthorprovider->did_change_authorization(authorization);}+void ConnectionFromUI::microphone_authorization_changed(WebView::MicrophoneAuthorization authorization)+{+ if (auto* authorizer = MicrophoneAuthorizerInUI::the())+ authorizer->did_change_authorization(authorization);+}+void ConnectionFromUI::translator_language_detected(u64 request_id, Optional<String> language){if (auto* translator = TranslatorInUI::the())@@ -494,11 +500,6 @@ void ConnectionFromUI::finish_prepared_close(u64 close_id)(*close)();}-void ConnectionFromUI::open_microphone_system_settings()-{- static_cast<WebView::Application&>(Application::the()).microphone_authorizer().open_system_settings();-}-void ConnectionFromUI::add_web_ui_page(String host, String title){WebView::WebUI::add_application_page(move(host), move(title));diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.hindex ad1ffd64cd5..aff8afeb4c5 100644--- a/Services/Hub/ConnectionFromUI.h+++ b/Services/Hub/ConnectionFromUI.h@@ -104,7 +104,6 @@ private:virtual void view_prepare_for_immediate_close(u64 view_id) override;virtual void view_prepare_for_close_without_prompting(u64 view_id, u64 close_id) override;virtual void finish_prepared_close(u64 close_id) override;- virtual void open_microphone_system_settings() override;virtual void pip_did_resize(u64 view_id, Gfx::IntSize size) override;virtual void pip_did_close(u64 view_id, bool video_keeps_playing) override;virtual void notification_shown(u64 id) override;@@ -114,6 +113,7 @@ private: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 microphone_authorization_changed(WebView::MicrophoneAuthorization 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;diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipcindex 843c4d82900..2198c0c1e34 100644--- a/Services/Hub/HubClient.ipc+++ b/Services/Hub/HubClient.ipc@@ -25,6 +25,7 @@#include <LibWebView/ContextMenuRequest.h>#include <LibWebView/FileDownloader.h>#include <LibWebView/HistoryStore.h>+#include <LibWebView/MicrophoneAuthorizer.h>#include <LibWebView/SessionHistory.h>#include <LibWebView/TabPerformanceStats.h>#include <LibWebCommon/Clipboard/SystemClipboard.h>@@ -118,6 +119,7 @@ struct Hub::UIInfo {bool has_spell_checker;bool spell_checker_learns_for_every_tag;bool spell_checker_can_suggest;+ Optional<WebView::MicrophoneAuthorization> microphone_authorization;Optional<Core::GeolocationAuthorization> location_authorization;bool supports_selection_clipboard;bool supports_system_menu_bar;@@ -245,6 +247,7 @@ endpoint HubClienttranslator_close_session(u64 session_id) =|translator_translate(u64 request_id, u64 session_id, Web::TranslationUnit unit) =|translator_open_language_download_settings() =|+ microphone_request_authorization() =|spell_checker_close_document(i64 document_tag) =|spell_checker_check(u64 request_id, i64 document_tag, Utf16String text) =|spell_checker_guesses(u64 request_id, i64 document_tag, Utf16String word) =|diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipcindex 54e1a236dda..fcb28a85f53 100644--- a/Services/Hub/HubServer.ipc+++ b/Services/Hub/HubServer.ipc@@ -22,6 +22,7 @@#include <LibWebCommon/Page/WritingSuggestion.h>#include <LibWebCommon/Clipboard/SystemClipboard.h>#include <LibWebView/HistoryStore.h>+#include <LibWebView/MicrophoneAuthorizer.h>#include <LibWebView/ProcessSnapshot.h>#include <LibWebView/SessionHistory.h>#include <LibWebView/Translator.h>@@ -174,7 +175,6 @@ endpoint HubServerview_prepare_for_immediate_close(u64 view_id) =|view_prepare_for_close_without_prompting(u64 view_id, u64 close_id) =|finish_prepared_close(u64 close_id) =|- open_microphone_system_settings() =|pip_did_resize(u64 view_id, Gfx::IntSize size) =|pip_did_close(u64 view_id, bool video_keeps_playing) =|notification_shown(u64 id) =|@@ -184,6 +184,7 @@ endpoint HubServergeolocation_position(u64 id, Core::GeolocationCoordinates coordinates) =|geolocation_error(u64 id, Core::GeolocationError error) =|geolocation_authorization_changed(Core::GeolocationAuthorization authorization) =|+ microphone_authorization_changed(WebView::MicrophoneAuthorization 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) =|diff --git a/Services/Hub/Proxies.cpp b/Services/Hub/Proxies.cppindex 4ea4dbd338a..7d01d596cae 100644--- a/Services/Hub/Proxies.cpp+++ b/Services/Hub/Proxies.cpp@@ -176,6 +176,50 @@ void GeolocationProviderInUI::did_change_authorization(Core::GeolocationAuthorizon_authorization_change();}+static MicrophoneAuthorizerInUI* s_microphone_authorizer = nullptr;++MicrophoneAuthorizerInUI::MicrophoneAuthorizerInUI(WebView::MicrophoneAuthorization authorization)+ : m_authorization(authorization)+{+ s_microphone_authorizer = this;+}++MicrophoneAuthorizerInUI::~MicrophoneAuthorizerInUI()+{+ s_microphone_authorizer = nullptr;+}++MicrophoneAuthorizerInUI* MicrophoneAuthorizerInUI::the()+{+ return s_microphone_authorizer;+}++void MicrophoneAuthorizerInUI::request_authorization(Function<void(WebView::MicrophoneAuthorization)> on_complete)+{+ // NB: With no UI to ask, nothing would ever answer, and every later request would wait behind this one.+ auto* ui = ConnectionFromUI::the();+ if (!ui) {+ on_complete(m_authorization);+ return;+ }++ // Requests made while the system is asking share its answer.+ m_pending_requests.append(move(on_complete));+ if (m_pending_requests.size() > 1)+ return;+ ui->async_microphone_request_authorization();+}++void MicrophoneAuthorizerInUI::did_change_authorization(WebView::MicrophoneAuthorization authorization)+{+ m_authorization = authorization;+ if (authorization == WebView::MicrophoneAuthorization::NotDetermined)+ return;+ auto pending_requests = move(m_pending_requests);+ for (auto& on_complete : pending_requests)+ on_complete(authorization);+}+static TranslatorInUI* s_translator = nullptr;TranslatorInUI::TranslatorInUI()diff --git a/Services/Hub/Proxies.h b/Services/Hub/Proxies.hindex d1e9564a053..7e39a392fbb 100644--- a/Services/Hub/Proxies.h+++ b/Services/Hub/Proxies.h@@ -12,6 +12,7 @@#include <Hub/View.h>#include <LibCore/GeolocationProvider.h>#include <LibWebView/ExternalURLHandler.h>+#include <LibWebView/MicrophoneAuthorizer.h>#include <LibWebView/NotificationBackend.h>#include <LibWebView/PictureInPictureWindow.h>#include <LibWebView/SpellChecker.h>@@ -97,6 +98,26 @@ private:Core::GeolocationAuthorization m_authorization { Core::GeolocationAuthorization::Unknown };};+// NB: The Hub never waits for the UI, so it keeps the last authorization the UI told it about.+class MicrophoneAuthorizerInUI final : public WebView::MicrophoneAuthorizer {+public:+ explicit MicrophoneAuthorizerInUI(WebView::MicrophoneAuthorization);+ virtual ~MicrophoneAuthorizerInUI() override;++ static MicrophoneAuthorizerInUI* the();++ void did_change_authorization(WebView::MicrophoneAuthorization);++private:+ virtual WebView::MicrophoneAuthorization authorization() const override { return m_authorization; }+ virtual void request_authorization(Function<void(WebView::MicrophoneAuthorization)> on_complete) override;+ // NB: The UI opens the system settings itself.+ virtual void open_system_settings() override { }++ WebView::MicrophoneAuthorization m_authorization;+ Vector<Function<void(WebView::MicrophoneAuthorization)>> m_pending_requests;+};+class TranslatorInUI final : public WebView::Translator {public:TranslatorInUI();diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex 01ce8ce274a..2fe7e9bb12d 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -71,6 +71,7 @@#if defined(AK_OS_MACOS)# include <UI/Qt/ApplicationIcon.h># include <UI/Qt/MacWindow.h>+# include <UI/Qt/MicrophoneAuthorizerMacOS.h># include <UI/Qt/ScrollbarPreferencesMac.h># include <UI/Qt/SpellCheckerMac.h>#endif@@ -705,6 +706,8 @@ Hub::UIInfo Application::current_ui_info() constinfo.has_spell_checker = m_spell_checker != nullptr;info.spell_checker_learns_for_every_tag = m_spell_checker && m_spell_checker->learns_for_every_tag();info.spell_checker_can_suggest = m_spell_checker && m_spell_checker->can_suggest();+ if (m_microphone_authorizer)+ info.microphone_authorization = m_microphone_authorizer->authorization();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();@@ -728,6 +731,7 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)# endif# if defined(AK_OS_MACOS)m_spell_checker = create_platform_spell_checker();+ m_microphone_authorizer = make<MicrophoneAuthorizerMacOS>();# endifIPC::MessageBuffer ui_info_buffer;@@ -863,6 +867,17 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)}# endif+ // NB: The user can give the browser the microphone in the system settings at any time, and comes back to the browser+ // after doing it.+ if (m_microphone_authorizer) {+ QObject::connect(qGuiApp, &QGuiApplication::applicationStateChanged, [this](Qt::ApplicationState state) {+ if (state != Qt::ApplicationActive)+ return;+ if (auto* hub = HubConnection::the_if_connected())+ hub->async_microphone_authorization_changed(m_microphone_authorizer->authorization());+ });+ }+QObject::connect(&SystemAppearance::the(), &SystemAppearance::changed, [this] {if (auto* hub = HubConnection::the_if_connected())hub->async_ui_info_changed(current_ui_info());diff --git a/UI/Qt/Application.h b/UI/Qt/Application.hindex 2d1eb70151e..40d9cdc31cf 100644--- a/UI/Qt/Application.h+++ b/UI/Qt/Application.h@@ -81,6 +81,7 @@ public:static WebView::BrowserOptions const& browser_options() { return WebView::Application::browser_options(); }#endifbool has_translator() const;+ WebView::MicrophoneAuthorizer* microphone_authorizer() { return m_microphone_authorizer.ptr(); }// The status to exit with right away, if the browser has nothing for this process to do.Optional<int> exit_status_if_done() const;ErrorOr<int> execute();@@ -315,6 +316,7 @@ private:OwnPtr<WebView::Translator> m_translator;OwnPtr<WebView::SpellChecker> m_spell_checker;+ OwnPtr<WebView::MicrophoneAuthorizer> m_microphone_authorizer;OwnPtr<WebView::Settings> m_settings;OwnPtr<BookmarkStore> m_bookmark_store;OwnPtr<SessionStore> m_session_store;diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txtindex 05cadf291e8..6cfda77389d 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -145,6 +145,7 @@ if (APPLE)LadybirdAccessibilityElement.mmMacWindow.mmMacWindowFocus.mm+ MicrophoneAuthorizerMacOS.mmNativeTextEditingBindings.mmMenu.mmScrollbarPreferencesMac.mm@@ -154,9 +155,10 @@ if (APPLE)WebContentViewAccessibility.mmWebContentViewMac.mm)- target_link_libraries(ladybird_impl PRIVATE Qt::GuiPrivate "-framework ApplicationServices" "-framework Cocoa" "-framework IOSurface" "-framework QuartzCore")+ target_link_libraries(ladybird_impl PRIVATE Qt::GuiPrivate "-framework ApplicationServices" "-framework AVFoundation" "-framework Cocoa" "-framework CoreServices" "-framework IOSurface" "-framework QuartzCore")set_source_files_properties(LadybirdAccessibilityElement.mm+ MicrophoneAuthorizerMacOS.mmSpellCheckerMac.mmTranslateIconMac.mmTranslatorMac.mmdiff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cppindex 60eb8dc5ea3..09712b69279 100644--- a/UI/Qt/HubConnection.cpp+++ b/UI/Qt/HubConnection.cpp@@ -7,6 +7,7 @@#include <AK/JsonValue.h>#include <LibCore/Process.h>#include <LibWebView/CrashReportOutbox.h>+#include <LibWebView/MicrophoneAuthorizer.h>#include <UI/Qt/Application.h>#include <UI/Qt/ApplicationActions.h>#include <UI/Qt/Bookmarks.h>@@ -529,6 +530,18 @@ void HubConnection::translator_open_language_download_settings()translator->open_language_download_settings();}+void HubConnection::microphone_request_authorization()+{+ auto* authorizer = Application::the().microphone_authorizer();+ if (!authorizer) {+ async_microphone_authorization_changed(WebView::MicrophoneAuthorization::Authorized);+ return;+ }+ authorizer->request_authorization([this](WebView::MicrophoneAuthorization authorization) {+ async_microphone_authorization_changed(authorization);+ });+}+// The Hub picks its own tag for each document, and the platform gets a tag of its own the first time one is used.WebView::SpellChecker::DocumentTag HubConnection::spell_document_tag(WebView::SpellChecker& spell_checker, i64 document_tag){diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.hindex fbaa955a31a..13547711dfc 100644--- a/UI/Qt/HubConnection.h+++ b/UI/Qt/HubConnection.h@@ -95,6 +95,7 @@ private: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 microphone_request_authorization() override;virtual void spell_checker_close_document(i64 document_tag) override;virtual void spell_checker_check(u64 request_id, i64 document_tag, Utf16String text) override;virtual void spell_checker_guesses(u64 request_id, i64 document_tag, Utf16String word) override;diff --git a/Libraries/LibWebView/MicrophoneAuthorizerMacOS.h b/UI/Qt/MicrophoneAuthorizerMacOS.hsimilarity index 70%rename from Libraries/LibWebView/MicrophoneAuthorizerMacOS.hrename to UI/Qt/MicrophoneAuthorizerMacOS.hindex ca38554da38..26ec45ee060 100644--- a/Libraries/LibWebView/MicrophoneAuthorizerMacOS.h+++ b/UI/Qt/MicrophoneAuthorizerMacOS.h@@ -9,7 +9,7 @@#include <AK/NonnullRefPtr.h>#include <LibWebView/MicrophoneAuthorizer.h>-namespace WebView {+namespace Ladybird {namespace Detail {@@ -19,13 +19,13 @@ struct PendingMicrophoneRequests;// The system asks the user on behalf of the process responsible for the browser: the app, or the terminal that ran it.// The AudioServer that opens the device answers to the same one.-class WEBVIEW_API MicrophoneAuthorizerMacOS final : public MicrophoneAuthorizer {+class MicrophoneAuthorizerMacOS final : public WebView::MicrophoneAuthorizer {public:MicrophoneAuthorizerMacOS();virtual ~MicrophoneAuthorizerMacOS() override;- virtual MicrophoneAuthorization authorization() const override;- virtual void request_authorization(Function<void(MicrophoneAuthorization)> on_complete) override;+ virtual WebView::MicrophoneAuthorization authorization() const override;+ virtual void request_authorization(Function<void(WebView::MicrophoneAuthorization)> on_complete) override;virtual void open_system_settings() override;private:diff --git a/Libraries/LibWebView/MicrophoneAuthorizerMacOS.mm b/UI/Qt/MicrophoneAuthorizerMacOS.mmsimilarity index 96%rename from Libraries/LibWebView/MicrophoneAuthorizerMacOS.mmrename to UI/Qt/MicrophoneAuthorizerMacOS.mmindex 671f2dc1f57..2a17c9b6d5b 100644--- a/Libraries/LibWebView/MicrophoneAuthorizerMacOS.mm+++ b/UI/Qt/MicrophoneAuthorizerMacOS.mm@@ -8,12 +8,14 @@#include <AK/RefPtr.h>#include <AK/Vector.h>#include <LibCore/EventLoop.h>-#include <LibWebView/MicrophoneAuthorizerMacOS.h>+#include <UI/Qt/MicrophoneAuthorizerMacOS.h>#import <AVFoundation/AVFoundation.h>#import <CoreServices/CoreServices.h>-namespace WebView {+namespace Ladybird {++using WebView::MicrophoneAuthorization;namespace Detail {diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex b0ec8ed55ac..05171517523 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -574,7 +574,8 @@ void Tab::initialize(Optional<u64> hub_view_id)view().did_show_permission_prompt(request);};popover->on_open_settings = [] {- HubConnection::the().async_open_microphone_system_settings();+ if (auto* authorizer = Application::the().microphone_authorizer())+ authorizer->open_system_settings();};return popover;};