Skip to content

Commit 4b51a36

Browse files
trflynn89ADKaster
authored andcommitted
Browser: Add support for a singleton chrome process
This partially supports the WebView::ChromeProcess mechanics. New windows aren't totally supported and will just open a new tab for now. When launched via the Browser's AppFile (either through quick launch or the desktop shortcut), a new window will be requested.
1 parent 3990e63 commit 4b51a36

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

Base/res/apps/Browser.af

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[App]
22
Name=&Ladybird
33
Executable=/bin/Browser
4+
Arguments=--new-window
45
Category=&Internet
56

67
[Launcher]

Userland/Applications/Browser/main.cpp

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-License-Identifier: BSD-2-Clause
77
*/
88

9+
#include <AK/Enumerate.h>
910
#include <Applications/Browser/Browser.h>
1011
#include <Applications/Browser/BrowserWindow.h>
1112
#include <Applications/Browser/Tab.h>
@@ -23,6 +24,7 @@
2324
#include <LibGUI/TabWidget.h>
2425
#include <LibMain/Main.h>
2526
#include <LibWeb/Loader/ResourceLoader.h>
27+
#include <LibWebView/ChromeProcess.h>
2628
#include <LibWebView/CookieJar.h>
2729
#include <LibWebView/Database.h>
2830
#include <LibWebView/OutOfProcessWebView.h>
@@ -88,24 +90,60 @@ static ErrorOr<void> load_autoplay_allowlist()
8890
return {};
8991
}
9092

93+
enum class NewWindow {
94+
No,
95+
Yes,
96+
};
97+
98+
static Vector<URL::URL> sanitize_urls(Vector<ByteString> const& raw_urls, NewWindow new_window = NewWindow::Yes)
99+
{
100+
Vector<URL::URL> sanitized_urls;
101+
for (auto const& raw_url : raw_urls) {
102+
if (auto url = WebView::sanitize_url(raw_url); url.has_value())
103+
sanitized_urls.append(url.release_value());
104+
}
105+
106+
if (sanitized_urls.is_empty())
107+
sanitized_urls.append(new_window == NewWindow::Yes ? Browser::g_home_url : Browser::g_new_tab_url);
108+
109+
return sanitized_urls;
110+
}
111+
112+
static void open_urls_from_client(Browser::BrowserWindow& window, Vector<ByteString> const& raw_urls, NewWindow new_window)
113+
{
114+
auto urls = sanitize_urls(raw_urls, new_window);
115+
116+
for (auto [i, url] : enumerate(urls)) {
117+
if (new_window == NewWindow::Yes)
118+
outln("New browser windows are not yet supported. Opening URLs in a new tab.");
119+
120+
auto activate_tab = i == 0 ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No;
121+
window.create_new_tab(url, activate_tab);
122+
}
123+
124+
window.show();
125+
}
126+
91127
ErrorOr<int> serenity_main(Main::Arguments arguments)
92128
{
93129
if (getuid() == 0) {
94130
warnln("Refusing to run as root");
95131
return 1;
96132
}
97133

98-
TRY(Core::System::pledge("sigaction stdio recvfd sendfd unix fattr cpath rpath wpath proc exec"));
134+
TRY(Core::System::pledge("sigaction stdio recvfd sendfd accept unix fattr cpath rpath wpath proc exec"));
99135

100136
WebView::ProcessManager::initialize();
101137

102-
TRY(Core::System::pledge("stdio recvfd sendfd unix fattr cpath rpath wpath proc exec"));
138+
TRY(Core::System::pledge("stdio recvfd sendfd accept unix fattr cpath rpath wpath proc exec"));
103139

104-
Vector<StringView> specified_urls;
140+
Vector<ByteString> specified_urls;
141+
bool new_window = false;
105142

106143
Core::ArgsParser args_parser;
107144
args_parser.add_positional_argument(specified_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
108145
args_parser.add_option(Browser::g_webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
146+
args_parser.add_option(new_window, "Force opening in a new window", "new-window", 'n');
109147

110148
args_parser.parse(arguments);
111149

@@ -122,6 +160,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
122160
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme(man_file) }));
123161
TRY(Desktop::Launcher::seal_allowlist());
124162

163+
TRY(Core::System::unveil("/tmp/session/%sid/Ladybird.pid", "rwc"));
164+
TRY(Core::System::unveil("/tmp/session/%sid/Ladybird.socket", "rwc"));
125165
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
126166
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
127167
TRY(Core::System::unveil("/tmp/session/%sid/portal/image", "rw"));
@@ -140,6 +180,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
140180
TRY(Core::System::unveil("/bin/Browser", "x"));
141181
TRY(Core::System::unveil(nullptr, nullptr));
142182

183+
WebView::ChromeProcess chrome_process;
184+
if (TRY(chrome_process.connect(specified_urls, new_window)) == WebView::ChromeProcess::ProcessDisposition::ExitProcess) {
185+
outln("Opening in existing process");
186+
return 0;
187+
}
188+
143189
Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create()));
144190

145191
auto app_icon = GUI::Icon::default_icon("app-browser"sv);
@@ -170,18 +216,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
170216
}
171217
}
172218

173-
Vector<URL::URL> initial_urls;
174-
175-
for (auto specified_url : specified_urls) {
176-
if (auto url = WebView::sanitize_url(specified_url); url.has_value())
177-
initial_urls.append(url.release_value());
178-
}
219+
auto cookie_jar = TRY(WebView::CookieJar::create(*database));
220+
auto window = Browser::BrowserWindow::construct(cookie_jar, sanitize_urls(specified_urls), man_file);
179221

180-
if (initial_urls.is_empty())
181-
initial_urls.append(Browser::g_home_url);
222+
chrome_process.on_new_tab = [&](auto const& raw_urls) {
223+
open_urls_from_client(*window, raw_urls, NewWindow::No);
224+
};
182225

183-
auto cookie_jar = TRY(WebView::CookieJar::create(*database));
184-
auto window = Browser::BrowserWindow::construct(cookie_jar, initial_urls, man_file);
226+
chrome_process.on_new_window = [&](auto const& raw_urls) {
227+
open_urls_from_client(*window, raw_urls, NewWindow::Yes);
228+
};
185229

186230
auto content_filters_watcher = TRY(Core::FileWatcher::create());
187231
content_filters_watcher->on_change = [&](Core::FileWatcherEvent const&) {

0 commit comments

Comments
 (0)