2023-08-20 16:14:31 -04:00
|
|
|
/*
|
2025-03-20 12:59:44 -04:00
|
|
|
* Copyright (c) 2023-2025, Tim Flynn <trflynn89@ladybird.org>
|
2023-08-20 16:14:31 -04:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-28 12:30:34 -04:00
|
|
|
#include <AK/Enumerate.h>
|
2023-08-20 16:14:31 -04:00
|
|
|
#include <LibGfx/Font/FontDatabase.h>
|
|
|
|
|
#include <LibMain/Main.h>
|
2025-02-22 21:23:29 +13:00
|
|
|
#include <LibURL/Parser.h>
|
2024-06-29 22:24:01 -06:00
|
|
|
#include <LibWebView/Application.h>
|
2025-03-15 17:00:58 -04:00
|
|
|
#include <LibWebView/BrowserProcess.h>
|
2024-11-10 09:23:10 -05:00
|
|
|
#include <LibWebView/EventLoop/EventLoopImplementationMacOS.h>
|
2024-11-10 10:26:07 -05:00
|
|
|
#include <LibWebView/MachPortServer.h>
|
2025-03-20 12:59:44 -04:00
|
|
|
#include <LibWebView/Settings.h>
|
2023-10-13 11:04:07 -04:00
|
|
|
#include <LibWebView/URL.h>
|
2024-11-10 10:26:07 -05:00
|
|
|
#include <LibWebView/Utilities.h>
|
2024-06-20 21:34:51 +03:00
|
|
|
#include <LibWebView/ViewImplementation.h>
|
|
|
|
|
#include <LibWebView/WebContentClient.h>
|
2023-08-20 16:14:31 -04:00
|
|
|
|
|
|
|
|
#import <Application/Application.h>
|
|
|
|
|
#import <Application/ApplicationDelegate.h>
|
2024-11-09 12:50:33 -05:00
|
|
|
#import <Interface/Tab.h>
|
|
|
|
|
#import <Interface/TabController.h>
|
2023-08-20 16:14:31 -04:00
|
|
|
|
|
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
|
# error "This project requires ARC"
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-07-30 14:01:05 -04:00
|
|
|
static void open_urls_from_client(Vector<URL::URL> const& urls, WebView::NewWindow new_window)
|
2024-04-28 12:30:34 -04:00
|
|
|
{
|
|
|
|
|
ApplicationDelegate* delegate = [NSApp delegate];
|
2024-07-30 14:01:05 -04:00
|
|
|
Tab* tab = new_window == WebView::NewWindow::Yes ? nil : [delegate activeTab];
|
2024-04-28 12:30:34 -04:00
|
|
|
|
|
|
|
|
for (auto [i, url] : enumerate(urls)) {
|
|
|
|
|
auto activate_tab = i == 0 ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No;
|
|
|
|
|
|
|
|
|
|
auto* controller = [delegate createNewTab:url
|
|
|
|
|
fromTab:tab
|
|
|
|
|
activateTab:activate_tab];
|
|
|
|
|
|
|
|
|
|
tab = (Tab*)[controller window];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 16:14:31 -04:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
|
|
|
{
|
2023-12-11 11:19:41 -07:00
|
|
|
AK::set_rich_debug_enabled(true);
|
|
|
|
|
|
2024-04-15 17:39:48 -06:00
|
|
|
Application* application = [Application sharedApplication];
|
2023-08-20 16:14:31 -04:00
|
|
|
|
2024-11-10 09:23:10 -05:00
|
|
|
Core::EventLoopManager::install(*new WebView::EventLoopManagerMacOS);
|
2025-03-20 12:59:44 -04:00
|
|
|
|
|
|
|
|
[application setupWebViewApplication:arguments];
|
2023-08-20 16:14:31 -04:00
|
|
|
|
2024-11-10 10:26:07 -05:00
|
|
|
WebView::platform_init();
|
2023-08-20 16:14:31 -04:00
|
|
|
|
2025-03-15 17:00:58 -04:00
|
|
|
WebView::BrowserProcess browser_process;
|
2024-07-30 14:01:05 -04:00
|
|
|
|
2025-03-15 16:56:52 -04:00
|
|
|
if (auto const& browser_options = WebView::Application::browser_options(); browser_options.force_new_process == WebView::ForceNewProcess::No) {
|
2025-03-15 17:00:58 -04:00
|
|
|
auto disposition = TRY(browser_process.connect(browser_options.raw_urls, browser_options.new_window));
|
2024-07-30 14:01:05 -04:00
|
|
|
|
2025-03-15 17:00:58 -04:00
|
|
|
if (disposition == WebView::BrowserProcess::ProcessDisposition::ExitProcess) {
|
2024-07-30 14:01:05 -04:00
|
|
|
outln("Opening in existing process");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-04-28 12:30:34 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-15 17:00:58 -04:00
|
|
|
browser_process.on_new_tab = [&](auto const& raw_urls) {
|
2024-07-30 14:01:05 -04:00
|
|
|
open_urls_from_client(raw_urls, WebView::NewWindow::No);
|
2024-04-28 12:30:34 -04:00
|
|
|
};
|
|
|
|
|
|
2025-03-15 17:00:58 -04:00
|
|
|
browser_process.on_new_window = [&](auto const& raw_urls) {
|
2024-07-30 14:01:05 -04:00
|
|
|
open_urls_from_client(raw_urls, WebView::NewWindow::Yes);
|
2024-04-28 12:30:34 -04:00
|
|
|
};
|
|
|
|
|
|
2024-11-10 10:26:07 -05:00
|
|
|
auto mach_port_server = make<WebView::MachPortServer>();
|
|
|
|
|
WebView::set_mach_server_name(mach_port_server->server_port_name());
|
|
|
|
|
|
2024-07-30 14:01:05 -04:00
|
|
|
mach_port_server->on_receive_child_mach_port = [&](auto pid, auto port) {
|
|
|
|
|
WebView::Application::the().set_process_mach_port(pid, move(port));
|
2024-04-04 14:13:14 -06:00
|
|
|
};
|
2024-11-10 10:26:07 -05:00
|
|
|
mach_port_server->on_receive_backing_stores = [](WebView::MachPortServer::BackingStoresMessage message) {
|
2024-06-29 22:24:01 -06:00
|
|
|
if (auto view = WebView::WebContentClient::view_for_pid_and_page_id(message.pid, message.page_id); view.has_value())
|
|
|
|
|
view->did_allocate_iosurface_backing_stores(message.front_backing_store_id, move(message.front_backing_store_port), message.back_backing_store_id, move(message.back_backing_store_port));
|
2024-06-20 21:34:51 +03:00
|
|
|
};
|
2024-04-04 14:13:14 -06:00
|
|
|
|
2024-11-13 15:33:02 -05:00
|
|
|
TRY([application launchServices]);
|
2024-06-26 13:44:42 -06:00
|
|
|
|
2024-09-05 18:19:51 -04:00
|
|
|
auto* delegate = [[ApplicationDelegate alloc] init];
|
2023-08-20 16:14:31 -04:00
|
|
|
[NSApp setDelegate:delegate];
|
|
|
|
|
|
2024-07-30 14:01:05 -04:00
|
|
|
return WebView::Application::the().execute();
|
2023-08-20 16:14:31 -04:00
|
|
|
}
|