2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-01-30 23:05:21 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2022-08-13 14:48:31 +01:00
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
2022-10-06 01:34:25 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2022-08-13 14:48:31 +01:00
|
|
|
#include <AK/IterationDecision.h>
|
2022-01-10 19:06:11 -08:00
|
|
|
#include <Applications/Browser/Browser.h>
|
|
|
|
|
#include <Applications/Browser/BrowserWindow.h>
|
|
|
|
|
#include <Applications/Browser/CookieJar.h>
|
|
|
|
|
#include <Applications/Browser/Tab.h>
|
|
|
|
|
#include <Applications/Browser/WindowActions.h>
|
2021-08-26 00:18:42 +02:00
|
|
|
#include <LibConfig/Client.h>
|
2020-05-27 21:57:30 +02:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2022-08-13 14:48:31 +01:00
|
|
|
#include <LibCore/FileWatcher.h>
|
2020-08-05 17:40:03 +02:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2021-11-23 10:59:50 +01:00
|
|
|
#include <LibCore/System.h>
|
2021-01-03 12:10:34 +01:00
|
|
|
#include <LibDesktop/Launcher.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
2020-11-02 19:30:17 +00:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-04-23 21:14:31 +02:00
|
|
|
#include <LibGUI/TabWidget.h>
|
2021-11-22 19:47:14 +01:00
|
|
|
#include <LibMain/Main.h>
|
2022-04-30 12:06:30 +02:00
|
|
|
#include <LibWeb/Loader/ResourceLoader.h>
|
2022-10-12 23:51:37 +02:00
|
|
|
#include <LibWebView/OutOfProcessWebView.h>
|
2022-04-30 12:06:30 +02:00
|
|
|
#include <LibWebView/RequestServerAdapter.h>
|
2021-03-12 17:29:37 +01:00
|
|
|
#include <unistd.h>
|
2019-10-05 10:19:12 +02:00
|
|
|
|
2020-05-10 11:18:47 +02:00
|
|
|
namespace Browser {
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString g_search_engine;
|
|
|
|
|
DeprecatedString g_home_url;
|
|
|
|
|
DeprecatedString g_new_tab_url;
|
|
|
|
|
Vector<DeprecatedString> g_content_filters;
|
2022-02-01 11:17:37 +01:00
|
|
|
bool g_content_filters_enabled { true };
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> g_proxies;
|
|
|
|
|
HashMap<DeprecatedString, size_t> g_proxy_mappings;
|
2022-01-10 19:06:11 -08:00
|
|
|
IconBag g_icon_bag;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString g_webdriver_content_ipc_path;
|
2020-08-05 17:40:03 +02:00
|
|
|
|
2020-05-10 11:18:47 +02:00
|
|
|
}
|
2020-04-25 17:20:23 +02:00
|
|
|
|
2022-01-20 11:17:52 +00:00
|
|
|
static ErrorOr<void> load_content_filters()
|
|
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
auto file = TRY(Core::Stream::File::open(DeprecatedString::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::Stream::OpenMode::Read));
|
2022-01-20 11:17:52 +00:00
|
|
|
auto ad_filter_list = TRY(Core::Stream::BufferedFile::create(move(file)));
|
2022-01-20 17:47:39 +00:00
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
|
|
|
|
while (TRY(ad_filter_list->can_read_line())) {
|
2022-04-15 14:52:33 +01:00
|
|
|
auto line = TRY(ad_filter_list->read_line(buffer));
|
2022-01-20 17:47:39 +00:00
|
|
|
if (!line.is_empty())
|
|
|
|
|
Browser::g_content_filters.append(line);
|
2022-01-20 11:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 19:47:14 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-10-05 10:19:12 +02:00
|
|
|
{
|
2020-03-31 13:01:55 +02:00
|
|
|
if (getuid() == 0) {
|
2020-10-25 17:46:16 +01:00
|
|
|
warnln("Refusing to run as root");
|
2020-03-31 13:01:55 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 10:50:33 +01:00
|
|
|
TRY(Core::System::pledge("stdio recvfd sendfd unix fattr cpath rpath wpath proc exec"));
|
2020-01-11 20:50:27 +01:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> specified_urls;
|
2020-05-27 21:57:30 +02:00
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2022-08-13 14:48:31 +01:00
|
|
|
args_parser.add_positional_argument(specified_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
|
2022-11-08 10:18:11 -05:00
|
|
|
args_parser.add_option(Browser::g_webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path");
|
2022-10-12 10:50:33 +01:00
|
|
|
|
2021-11-22 19:03:05 -05:00
|
|
|
args_parser.parse(arguments);
|
2020-05-27 21:57:30 +02:00
|
|
|
|
2022-04-16 17:17:39 +01:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2019-10-05 10:19:12 +02:00
|
|
|
|
2022-02-11 17:33:09 +01:00
|
|
|
Config::pledge_domain("Browser");
|
2022-01-31 21:40:48 +01:00
|
|
|
Config::monitor_domain("Browser");
|
2021-08-26 00:18:42 +02:00
|
|
|
|
2021-01-03 12:10:34 +01:00
|
|
|
// Connect to LaunchServer immediately and let it know that we won't ask for anything other than opening
|
|
|
|
|
// the user's downloads directory.
|
|
|
|
|
// FIXME: This should go away with a standalone download manager at some point.
|
2022-09-29 01:30:58 +02:00
|
|
|
TRY(Desktop::Launcher::add_allowed_url(URL::create_with_file_scheme(Core::StandardPaths::downloads_directory())));
|
2021-11-24 00:23:00 +01:00
|
|
|
TRY(Desktop::Launcher::seal_allowlist());
|
2021-01-03 12:10:34 +01:00
|
|
|
|
2022-11-11 14:14:58 -05:00
|
|
|
if (!Browser::g_webdriver_content_ipc_path.is_empty())
|
2022-10-12 10:50:33 +01:00
|
|
|
specified_urls.empend("about:blank");
|
|
|
|
|
|
2022-10-14 21:55:17 +03:00
|
|
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
2022-09-06 00:04:06 -06:00
|
|
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
|
|
|
|
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
|
|
|
|
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/image", "rw"));
|
|
|
|
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw"));
|
|
|
|
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/request", "rw"));
|
2021-11-23 10:59:50 +01:00
|
|
|
TRY(Core::System::unveil("/home", "rwc"));
|
|
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
|
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
2022-01-20 12:27:56 -05:00
|
|
|
TRY(Core::System::unveil("/etc/timezone", "r"));
|
2022-01-31 21:55:26 +01:00
|
|
|
TRY(Core::System::unveil("/bin/BrowserSettings", "x"));
|
2022-10-06 01:34:25 -07:00
|
|
|
TRY(Core::System::unveil("/bin/Browser", "x"));
|
2021-11-23 10:59:50 +01:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-03-26 20:06:23 +01:00
|
|
|
|
2022-04-30 12:06:30 +02:00
|
|
|
Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create()));
|
|
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-browser"sv);
|
2020-11-02 19:30:17 +00:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
Browser::g_home_url = Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, "file:///res/html/misc/welcome.html"sv);
|
|
|
|
|
Browser::g_new_tab_url = Config::read_string("Browser"sv, "Preferences"sv, "NewTab"sv, "file:///res/html/misc/new-tab.html"sv);
|
|
|
|
|
Browser::g_search_engine = Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, {});
|
|
|
|
|
Browser::g_content_filters_enabled = Config::read_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, true);
|
2020-04-25 03:25:11 +00:00
|
|
|
|
2022-01-10 19:06:11 -08:00
|
|
|
Browser::g_icon_bag = TRY(Browser::IconBag::try_create());
|
|
|
|
|
|
2022-01-20 11:17:52 +00:00
|
|
|
TRY(load_content_filters());
|
2021-01-05 18:12:29 +01:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
for (auto& group : Config::list_groups("Browser"sv)) {
|
|
|
|
|
if (!group.starts_with("Proxy:"sv))
|
2022-04-08 01:46:47 +04:30
|
|
|
continue;
|
|
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
for (auto& key : Config::list_keys("Browser"sv, group)) {
|
2022-04-08 01:46:47 +04:30
|
|
|
auto proxy_spec = group.substring_view(6);
|
|
|
|
|
auto existing_proxy = Browser::g_proxies.find(proxy_spec);
|
|
|
|
|
if (existing_proxy.is_end())
|
|
|
|
|
Browser::g_proxies.append(proxy_spec);
|
|
|
|
|
|
|
|
|
|
Browser::g_proxy_mappings.set(key, existing_proxy.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
auto url_from_argument_string = [](DeprecatedString const& string) -> URL {
|
2022-08-13 14:48:31 +01:00
|
|
|
if (Core::File::exists(string)) {
|
2022-09-29 01:30:58 +02:00
|
|
|
return URL::create_with_file_scheme(Core::File::real_path_for(string));
|
2020-06-12 21:29:27 +02:00
|
|
|
}
|
2022-08-13 14:48:31 +01:00
|
|
|
return Browser::url_from_user_input(string);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
URL first_url = Browser::url_from_user_input(Browser::g_home_url);
|
|
|
|
|
if (!specified_urls.is_empty())
|
|
|
|
|
first_url = url_from_argument_string(specified_urls.first());
|
2020-04-24 13:06:17 +01:00
|
|
|
|
2021-05-17 23:15:20 +02:00
|
|
|
Browser::CookieJar cookie_jar;
|
|
|
|
|
auto window = Browser::BrowserWindow::construct(cookie_jar, first_url);
|
2022-10-12 10:50:33 +01:00
|
|
|
|
2022-01-31 20:43:18 +01:00
|
|
|
auto content_filters_watcher = TRY(Core::FileWatcher::create());
|
|
|
|
|
content_filters_watcher->on_change = [&](Core::FileWatcherEvent const&) {
|
|
|
|
|
dbgln("Reloading content filters because config file changed");
|
|
|
|
|
auto error = load_content_filters();
|
|
|
|
|
if (error.is_error()) {
|
|
|
|
|
dbgln("Reloading content filters failed: {}", error.release_error());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-01 11:17:37 +01:00
|
|
|
window->content_filters_changed();
|
2022-01-31 20:43:18 +01:00
|
|
|
};
|
2022-12-04 18:02:33 +00:00
|
|
|
TRY(content_filters_watcher->add_watch(DeprecatedString::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::FileWatcherEvent::Type::ContentModified));
|
2022-01-31 20:43:18 +01:00
|
|
|
|
2021-04-17 23:08:06 +03:00
|
|
|
app->on_action_enter = [&](GUI::Action& action) {
|
2021-05-17 23:15:20 +02:00
|
|
|
if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
|
|
|
|
|
auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
|
|
|
|
|
if (!tab)
|
|
|
|
|
return;
|
|
|
|
|
tab->action_entered(action);
|
|
|
|
|
}
|
2021-04-17 23:08:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
app->on_action_leave = [&](auto& action) {
|
2021-05-17 23:15:20 +02:00
|
|
|
if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
|
|
|
|
|
auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
|
|
|
|
|
if (!tab)
|
|
|
|
|
return;
|
|
|
|
|
tab->action_left(action);
|
|
|
|
|
}
|
2020-04-25 17:20:23 +02:00
|
|
|
};
|
|
|
|
|
|
2022-08-13 14:48:31 +01:00
|
|
|
for (size_t i = 1; i < specified_urls.size(); ++i)
|
|
|
|
|
window->create_new_tab(url_from_argument_string(specified_urls[i]), false);
|
|
|
|
|
|
2020-04-24 13:06:17 +01:00
|
|
|
window->show();
|
2019-10-05 10:19:12 +02:00
|
|
|
|
2022-11-01 15:43:53 -04:00
|
|
|
window->broadcast_window_position(window->position());
|
|
|
|
|
window->broadcast_window_size(window->size());
|
|
|
|
|
|
2020-07-04 14:05:19 +02:00
|
|
|
return app->exec();
|
2019-10-05 10:19:12 +02:00
|
|
|
}
|