2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-01-30 19:05:21 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-08-10 17:46:23 -03:00
|
|
|
#include "Browser.h"
|
2021-05-17 18:15:20 -03:00
|
|
|
#include "BrowserWindow.h"
|
2021-04-11 11:53:15 -03:00
|
|
|
#include "CookieJar.h"
|
2020-04-23 16:14:31 -03:00
|
|
|
#include "Tab.h"
|
2020-04-24 09:06:17 -03:00
|
|
|
#include "WindowActions.h"
|
2020-08-05 12:40:03 -03:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-08-25 19:18:42 -03:00
|
|
|
#include <LibConfig/Client.h>
|
2020-05-27 16:57:30 -03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/File.h>
|
2020-08-05 12:40:03 -03:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2021-01-03 08:10:34 -03:00
|
|
|
#include <LibDesktop/Launcher.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
2020-11-02 16:30:17 -03:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-04-23 16:14:31 -03:00
|
|
|
#include <LibGUI/TabWidget.h>
|
2021-04-24 08:54:24 -03:00
|
|
|
#include <LibWeb/HTML/WebSocket.h>
|
2021-01-05 14:12:29 -03:00
|
|
|
#include <LibWeb/Loader/ContentFilter.h>
|
2020-06-01 15:42:50 -03:00
|
|
|
#include <LibWeb/Loader/ResourceLoader.h>
|
2019-10-05 05:19:12 -03:00
|
|
|
#include <stdio.h>
|
2021-03-12 13:29:37 -03:00
|
|
|
#include <unistd.h>
|
2019-10-05 05:19:12 -03:00
|
|
|
|
2020-05-10 06:18:47 -03:00
|
|
|
namespace Browser {
|
|
|
|
|
|
2021-05-18 12:32:28 -03:00
|
|
|
String g_search_engine;
|
2020-05-10 06:18:47 -03:00
|
|
|
String g_home_url;
|
2020-08-05 12:40:03 -03:00
|
|
|
|
2020-05-10 06:18:47 -03:00
|
|
|
}
|
2020-04-25 12:20:23 -03:00
|
|
|
|
2019-10-05 05:19:12 -03:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
2020-03-31 08:01:55 -03:00
|
|
|
if (getuid() == 0) {
|
2020-10-25 13:46:16 -03:00
|
|
|
warnln("Refusing to run as root");
|
2020-03-31 08:01:55 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 18:20:26 -03:00
|
|
|
if (pledge("stdio recvfd sendfd unix cpath rpath wpath", nullptr) < 0) {
|
2020-01-11 16:50:27 -03:00
|
|
|
perror("pledge");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 16:57:30 -03:00
|
|
|
const char* specified_url = nullptr;
|
|
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
args_parser.add_positional_argument(specified_url, "URL to open", "url", Core::ArgsParser::Required::No);
|
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
2019-10-05 05:19:12 -03:00
|
|
|
|
2021-08-25 19:18:42 -03:00
|
|
|
Config::pledge_domains("Browser");
|
|
|
|
|
|
2021-01-03 08:10:34 -03: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.
|
|
|
|
|
if (!Desktop::Launcher::add_allowed_url(URL::create_with_file_protocol(Core::StandardPaths::downloads_directory()))
|
|
|
|
|
|| !Desktop::Launcher::seal_allowlist()) {
|
|
|
|
|
warnln("Failed to set up allowed launch URLs");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 16:06:23 -03:00
|
|
|
if (unveil("/home", "rwc") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unveil("/res", "r") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 12:21:22 -03:00
|
|
|
if (unveil("/etc/passwd", "r") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
2020-05-26 13:54:58 -03:00
|
|
|
}
|
|
|
|
|
|
2020-06-22 16:41:10 -03:00
|
|
|
if (unveil("/tmp/portal/image", "rw") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 16:46:37 -03:00
|
|
|
if (unveil("/tmp/portal/webcontent", "rw") < 0) {
|
|
|
|
|
perror("unveil");
|
2021-06-09 04:59:40 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unveil("/tmp/portal/request", "rw") < 0) {
|
|
|
|
|
perror("unveil");
|
2020-07-06 16:46:37 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 16:06:23 -03:00
|
|
|
unveil(nullptr, nullptr);
|
|
|
|
|
|
2020-11-02 16:30:17 -03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-browser");
|
|
|
|
|
|
2021-08-25 19:18:42 -03:00
|
|
|
Browser::g_home_url = Config::read_string("Browser", "Preferences", "Home", "about:blank");
|
|
|
|
|
Browser::g_search_engine = Config::read_string("Browser", "Preferences", "SearchEngine", {});
|
2020-04-25 00:25:11 -03:00
|
|
|
|
2021-05-12 06:26:43 -03:00
|
|
|
auto ad_filter_list_or_error = Core::File::open(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::OpenMode::ReadOnly);
|
2021-01-05 14:12:29 -03:00
|
|
|
if (!ad_filter_list_or_error.is_error()) {
|
|
|
|
|
auto& ad_filter_list = *ad_filter_list_or_error.value();
|
|
|
|
|
while (!ad_filter_list.eof()) {
|
|
|
|
|
auto line = ad_filter_list.read_line();
|
|
|
|
|
if (line.is_empty())
|
|
|
|
|
continue;
|
|
|
|
|
Web::ContentFilter::the().add_pattern(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-10 06:18:47 -03:00
|
|
|
URL first_url = Browser::g_home_url;
|
2020-06-12 16:29:27 -03:00
|
|
|
if (specified_url) {
|
|
|
|
|
if (Core::File::exists(specified_url)) {
|
|
|
|
|
first_url = URL::create_with_file_protocol(Core::File::real_path_for(specified_url));
|
|
|
|
|
} else {
|
|
|
|
|
first_url = Browser::url_from_user_input(specified_url);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-24 09:06:17 -03:00
|
|
|
|
2021-05-17 18:15:20 -03:00
|
|
|
Browser::CookieJar cookie_jar;
|
|
|
|
|
auto window = Browser::BrowserWindow::construct(cookie_jar, first_url);
|
|
|
|
|
|
2021-04-17 17:08:06 -03:00
|
|
|
app->on_action_enter = [&](GUI::Action& action) {
|
2021-05-17 18:15:20 -03: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 17:08:06 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
app->on_action_leave = [&](auto& action) {
|
2021-05-17 18:15:20 -03: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 12:20:23 -03:00
|
|
|
};
|
|
|
|
|
|
2020-04-24 09:06:17 -03:00
|
|
|
window->show();
|
2019-10-05 05:19:12 -03:00
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
return app->exec();
|
2019-10-05 05:19:12 -03:00
|
|
|
}
|