Skip to content

Truly independent web browser

LibWebView+Hub+UI/Qt: Take launch URLs from Apple events in the UI

Andreas Kling4a590228065c
diff --git a/Libraries/LibWebView/BrowserProcess.cpp b/Libraries/LibWebView/BrowserProcess.cpp
index a212e6c89c2..5455667152d 100644
--- a/Libraries/LibWebView/BrowserProcess.cpp
+++ b/Libraries/LibWebView/BrowserProcess.cpp
@@ -17,9 +17,6 @@
#endif
#include <LibWebView/Application.h>
#include <LibWebView/BrowserProcess.h>
-#if defined(AK_OS_MACOS)
-# include <LibWebView/LaunchURLsMacOS.h>
-#endif
#include <LibWebView/URL.h>
#include <LibWebView/Utilities.h>
@@ -91,16 +88,6 @@ ErrorOr<void> BrowserProcess::connect_as_client(pid_t pid, Vector<ByteString> co
auto transport_ports = TRY(IPC::bootstrap_transport_from_mach_server(mach_server_name_for_process(process_name, pid)));
auto client = UIProcessClient::construct(make<IPC::Transport>(move(transport_ports.receive_right), move(transport_ports.send_right)));
- // NB: When macOS launches this process to open a URL or a file — e.g. a link clicked in another app, sent to an
- // installed copy of Ladybird other than the one already running for this profile — the URL is in a launch
- // Apple event, never in argv. So hand those over too — or the running process opens just an about:newtab page.
- if (auto urls = take_urls_from_launch_apple_events(); !urls.is_empty()) {
- if (!client->send_sync_but_allow_failure<Messages::UIProcessServer::OpenUrls>(urls))
- dbgln("Failed to send OpenUrls message to UIProcess");
- if (raw_urls.is_empty())
- return {};
- }
-
switch (new_window) {
case NewWindow::Yes:
if (!client->send_sync_but_allow_failure<Messages::UIProcessServer::CreateNewWindow>(raw_urls))
@@ -178,13 +165,6 @@ void BrowserProcess::accept_transport(NonnullOwnPtr<IPC::Transport> transport)
if (this->on_new_window)
this->on_new_window(raw_urls);
};
-
- client->on_open_urls = [this](auto raw_urls) {
- if (this->on_open_urls)
- this->on_open_urls(raw_urls);
- else if (this->on_new_tab)
- this->on_new_tab(raw_urls);
- };
}
BrowserProcess::~BrowserProcess()
@@ -235,10 +215,4 @@ void UIProcessConnectionFromClient::create_new_window(Vector<ByteString> urls)
on_new_window(sanitize_urls(urls));
}
-void UIProcessConnectionFromClient::open_urls(Vector<ByteString> urls)
-{
- if (on_open_urls)
- on_open_urls(sanitize_urls(urls));
-}
-
}
diff --git a/Libraries/LibWebView/BrowserProcess.h b/Libraries/LibWebView/BrowserProcess.h
index 70efcadc446..c182a7cae95 100644
--- a/Libraries/LibWebView/BrowserProcess.h
+++ b/Libraries/LibWebView/BrowserProcess.h
@@ -32,14 +32,12 @@ public:
Function<void(Vector<URL::URL> const&)> on_new_tab;
Function<void(Vector<URL::URL> const&)> on_new_window;
- Function<void(Vector<URL::URL> const&)> on_open_urls;
private:
UIProcessConnectionFromClient(NonnullOwnPtr<IPC::Transport>, int client_id);
virtual void create_new_tab(Vector<ByteString> urls) override;
virtual void create_new_window(Vector<ByteString> urls) override;
- virtual void open_urls(Vector<ByteString> urls) override;
};
class WEBVIEW_API BrowserProcess {
@@ -62,9 +60,6 @@ public:
Function<void(Vector<URL::URL> const&)> on_new_tab;
Function<void(Vector<URL::URL> const&)> on_new_window;
- // URLs another process was launched by the OS to open; e.g. link clicked in another app. Falls back to on_new_tab.
- Function<void(Vector<URL::URL> const&)> on_open_urls;
-
private:
void accept_transport(NonnullOwnPtr<IPC::Transport>);
#if defined(AK_OS_MACOS)
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txt
index 26b14898f86..365b9d5e4d2 100644
--- a/Libraries/LibWebView/CMakeLists.txt
+++ b/Libraries/LibWebView/CMakeLists.txt
@@ -102,7 +102,6 @@ set(SOURCES
if (APPLE)
list(APPEND SOURCES
CrashReportPOSIX.cpp
- LaunchURLsMacOS.cpp
NotificationBackendMacOS.mm
PlatformColorsMacOS.cpp
ProcessReaper.cpp
@@ -181,7 +180,7 @@ if (LINUX)
endif()
if (APPLE)
- target_link_libraries(LibWebView PRIVATE "-framework Carbon" "-framework CoreServices" "-framework Foundation" "-framework UserNotifications")
+ target_link_libraries(LibWebView PRIVATE "-framework CoreServices" "-framework Foundation" "-framework UserNotifications")
endif()
if (LINUX OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
diff --git a/Libraries/LibWebView/UIProcessServer.ipc b/Libraries/LibWebView/UIProcessServer.ipc
index 39452b2f0e2..c7508666ad0 100644
--- a/Libraries/LibWebView/UIProcessServer.ipc
+++ b/Libraries/LibWebView/UIProcessServer.ipc
@@ -1,5 +1,4 @@
endpoint UIProcessServer {
create_new_tab(Vector<ByteString> urls) => ()
create_new_window(Vector<ByteString> urls) => ()
- open_urls(Vector<ByteString> urls) => ()
}
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index e04994b5860..411132962a0 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -43,7 +43,6 @@ ConnectionFromUI::ConnectionFromUI(NonnullOwnPtr<IPC::Transport> transport)
(void)WebView::TabPerformanceMonitor::the();
auto& browser_process = Application::the().browser_process();
- browser_process.on_open_urls = [this](auto const& urls) { async_app_open_urls(urls); };
browser_process.on_new_tab = [this](auto const& urls) { async_app_open_new_tab(urls); };
browser_process.on_new_window = [this](auto const& urls) { async_app_open_new_window(urls); };
}
diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipc
index ac107afcc60..878533cccb6 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -225,7 +225,6 @@ endpoint HubClient
{
crash_reports_changed() =|
request_finished(u64 request_id, bool had_network_error, Optional<u32> response_code, Vector<HTTP::Header> response_headers, ByteString body) =|
- app_open_urls(Vector<URL::URL> urls) =|
app_open_new_tab(Vector<URL::URL> urls) =|
app_open_new_window(Vector<URL::URL> urls) =|
view_selected_text(u64 view_id, u64 request_id, Optional<ByteString> text) =|
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 2fe7e9bb12d..d849d8f3e77 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -70,6 +70,7 @@
#if defined(AK_OS_MACOS)
# include <UI/Qt/ApplicationIcon.h>
+# include <UI/Qt/LaunchURLsMacOS.h>
# include <UI/Qt/MacWindow.h>
# include <UI/Qt/MicrophoneAuthorizerMacOS.h>
# include <UI/Qt/ScrollbarPreferencesMac.h>
@@ -741,6 +742,12 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)
Vector<ByteString> hub_arguments;
for (auto argument : arguments.strings.slice(1))
hub_arguments.append(argument);
+# if defined(AK_OS_MACOS)
+ // NB: When macOS launches the browser to open a URL or a file, such as a link clicked in another app, the URL is in a
+ // launch Apple event to this process, never in argv. The Hub opens it like a URL from the command line, and
+ // hands it to the browser already running for the profile, if there is one.
+ hub_arguments.extend(take_urls_from_launch_apple_events());
+# endif
hub_arguments.append(ByteString::formatted("--ui-info={}", encode_hex(ui_info_buffer.data().span())));
Core::ProcessSpawnOptions spawn_options {
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index 40d9cdc31cf..8c8f1a9dcf1 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -86,7 +86,6 @@ public:
Optional<int> exit_status_if_done() const;
ErrorOr<int> execute();
- Function<void(Vector<URL::URL> const&)> on_open_urls;
Function<void(Vector<URL::URL> const&)> on_new_tab;
Function<void(Vector<URL::URL> const&)> on_new_window;
static WebView::Settings& settings() { return *s_the->m_settings; }
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index 6cfda77389d..703b87c6e9f 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -142,6 +142,7 @@ if (APPLE)
Conversions.mm
DictionaryLookup.mm
ExternalURLHandler.mm
+ LaunchURLsMacOS.cpp
LadybirdAccessibilityElement.mm
MacWindow.mm
MacWindowFocus.mm
@@ -155,7 +156,7 @@ if (APPLE)
WebContentViewAccessibility.mm
WebContentViewMac.mm
)
- target_link_libraries(ladybird_impl PRIVATE Qt::GuiPrivate "-framework ApplicationServices" "-framework AVFoundation" "-framework Cocoa" "-framework CoreServices" "-framework IOSurface" "-framework QuartzCore")
+ target_link_libraries(ladybird_impl PRIVATE Qt::GuiPrivate "-framework ApplicationServices" "-framework AVFoundation" "-framework Carbon" "-framework Cocoa" "-framework CoreServices" "-framework IOSurface" "-framework QuartzCore")
set_source_files_properties(
LadybirdAccessibilityElement.mm
MicrophoneAuthorizerMacOS.mm
diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cpp
index 31b39487d57..dbcce2bf8af 100644
--- a/UI/Qt/HubConnection.cpp
+++ b/UI/Qt/HubConnection.cpp
@@ -684,12 +684,6 @@ void HubConnection::view_page_dumps(u64 view_id, u64 request_id, Optional<String
view->did_get_page_dumps(request_id, move(dumps));
}
-void HubConnection::app_open_urls(Vector<URL::URL> urls)
-{
- if (auto& application = Application::the(); application.on_open_urls)
- application.on_open_urls(urls);
-}
-
void HubConnection::app_open_new_tab(Vector<URL::URL> urls)
{
if (auto& application = Application::the(); application.on_new_tab)
diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.h
index 0b08419d80f..b8fb97cf663 100644
--- a/UI/Qt/HubConnection.h
+++ b/UI/Qt/HubConnection.h
@@ -75,7 +75,6 @@ private:
// ^HubClientEndpoint
virtual void crash_reports_changed() override;
virtual void request_finished(u64 request_id, bool had_network_error, Optional<u32> response_code, Vector<HTTP::Header> response_headers, ByteString body) override;
- virtual void app_open_urls(Vector<URL::URL> urls) override;
virtual void app_open_new_tab(Vector<URL::URL> urls) override;
virtual void app_open_new_window(Vector<URL::URL> urls) override;
virtual void view_selected_text(u64 view_id, u64 request_id, Optional<ByteString> text) override;
diff --git a/Libraries/LibWebView/LaunchURLsMacOS.cpp b/UI/Qt/LaunchURLsMacOS.cpp
similarity index 64%
rename from Libraries/LibWebView/LaunchURLsMacOS.cpp
rename to UI/Qt/LaunchURLsMacOS.cpp
index 2aede2f9a81..e63ff66cf3a 100644
--- a/Libraries/LibWebView/LaunchURLsMacOS.cpp
+++ b/UI/Qt/LaunchURLsMacOS.cpp
@@ -4,11 +4,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibWebView/LaunchURLsMacOS.h>
+#include <UI/Qt/LaunchURLsMacOS.h>
#include <Carbon/Carbon.h>
-namespace WebView {
+namespace Ladybird {
static void append_url_from_descriptor(AEDesc const& descriptor, Vector<ByteString>& urls)
{
@@ -69,12 +69,27 @@ Vector<ByteString> take_urls_from_launch_apple_events()
AEInstallEventHandler(kInternetEventClass, kAEGetURL, get_url_handler, &urls, false);
AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, open_documents_handler, &urls, false);
- // LaunchServices has already queued the launch events by the time this runs — one kAEGetURL event per URL, back to
- // back — so this only takes what is queued, and never waits for more. This is the same check Chrome makes in
- // ProcessSingleton::WaitForAndForwardOpenURLEvent().
+ // LaunchServices has already queued the launch events by the time this runs, one kAEGetURL event per URL, back to
+ // back. So this only takes what is queued, and never waits for more. The first call only lets the event source turn
+ // the queued Apple events into events, and takes none of them.
EventTypeSpec const apple_event_type { kEventClassAppleEvent, kEventAppleEvent };
- EventRef event = nullptr;
- while (ReceiveNextEvent(1, &apple_event_type, kEventDurationNanosecond, true, &event) == noErr) {
+ EventRef first_event = nullptr;
+ (void)ReceiveNextEvent(1, &apple_event_type, kEventDurationNanosecond, false, &first_event);
+
+ // NB: Only the URL events are taken. The other launch events, kAEOpenApplication above all, stay queued for AppKit,
+ // which does not order any window in that is shown later on until it has handled them.
+ auto is_url_event = [](EventRef event, void*) -> Boolean {
+ if (GetEventClass(event) != kEventClassAppleEvent || GetEventKind(event) != kEventAppleEvent)
+ return false;
+ OSType event_class {};
+ OSType event_id {};
+ GetEventParameter(event, kEventParamAEEventClass, typeType, nullptr, sizeof(event_class), nullptr, &event_class);
+ GetEventParameter(event, kEventParamAEEventID, typeType, nullptr, sizeof(event_id), nullptr, &event_id);
+ return (event_class == kInternetEventClass && event_id == kAEGetURL) || (event_class == kCoreEventClass && event_id == kAEOpenDocuments);
+ };
+ while (auto* event = FindSpecificEventInQueue(GetMainEventQueue(), is_url_event, nullptr)) {
+ RetainEvent(event);
+ RemoveEventFromQueue(GetMainEventQueue(), event);
AEProcessEvent(event);
ReleaseEvent(event);
}
diff --git a/Libraries/LibWebView/LaunchURLsMacOS.h b/UI/Qt/LaunchURLsMacOS.h
similarity index 72%
rename from Libraries/LibWebView/LaunchURLsMacOS.h
rename to UI/Qt/LaunchURLsMacOS.h
index 8df797e5614..17d016dafb0 100644
--- a/Libraries/LibWebView/LaunchURLsMacOS.h
+++ b/UI/Qt/LaunchURLsMacOS.h
@@ -9,10 +9,10 @@
#include <AK/ByteString.h>
#include <AK/Vector.h>
-namespace WebView {
+namespace Ladybird {
// Takes the URLs and files from the Apple events that macOS queued when it launched this process to open them. Those
-// never appear in argv — LaunchServices delivers them in kAEGetURL and kAEOpenDocuments events after launch.
+// never appear in argv. LaunchServices delivers them in kAEGetURL and kAEOpenDocuments events after launch.
Vector<ByteString> take_urls_from_launch_apple_events();
}
diff --git a/UI/Qt/main.cpp b/UI/Qt/main.cpp
index 5792537ae23..06e45c2d4f2 100644
--- a/UI/Qt/main.cpp
+++ b/UI/Qt/main.cpp
@@ -88,18 +88,15 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
app->new_window({ file_url });
};
- app->on_open_urls = [&](auto const& urls) {
- for (auto const& url : urls)
- app->on_open_file(url);
- };
-
+ // NB: URLs from outside the browser, such as a link clicked in another app, never open in a private window.
app->on_new_tab = [&](auto const& urls) {
- if (!app->active_window_if_any()) {
+ auto* non_private_window = app->non_private_window_if_any();
+ if (!non_private_window) {
app->new_window(urls);
return;
}
- auto& window = *app->active_window_if_any();
+ auto& window = *non_private_window;
for (size_t i = 0; i < urls.size(); ++i) {
window.new_tab_from_url(urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No, Ladybird::BrowserWindow::TabLocation::end());
}