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>
|
2020-01-18 09:38:21 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-04-25 17:20:23 +02:00
|
|
|
#include "BookmarksBarWidget.h"
|
2020-08-10 22:46:23 +02:00
|
|
|
#include "Browser.h"
|
2021-04-11 10:53:15 -04:00
|
|
|
#include "CookieJar.h"
|
2020-04-23 21:14:31 +02:00
|
|
|
#include "Tab.h"
|
2020-04-24 13:06:17 +01:00
|
|
|
#include "WindowActions.h"
|
2020-08-05 17:40:03 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-12-20 11:47:44 +01:00
|
|
|
#include <Applications/Browser/BrowserWindowGML.h>
|
2020-05-27 21:57:30 +02:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-04-25 03:25:11 +00:00
|
|
|
#include <LibCore/ConfigFile.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2020-08-05 17:40:03 +02:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2021-01-03 12:10:34 +01:00
|
|
|
#include <LibDesktop/Launcher.h>
|
2020-04-24 20:50:06 +02:00
|
|
|
#include <LibGUI/AboutDialog.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>
|
2021-04-09 22:23:32 +02:00
|
|
|
#include <LibGUI/SeparatorWidget.h>
|
2020-04-23 21:14:31 +02:00
|
|
|
#include <LibGUI/TabWidget.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Window.h>
|
2020-04-23 21:14:31 +02:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2021-01-05 18:12:29 +01:00
|
|
|
#include <LibWeb/Loader/ContentFilter.h>
|
2020-06-01 20:42:50 +02:00
|
|
|
#include <LibWeb/Loader/ResourceLoader.h>
|
2019-10-05 10:19:12 +02:00
|
|
|
#include <stdio.h>
|
2019-11-25 00:28:18 +01:00
|
|
|
#include <stdlib.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 {
|
|
|
|
|
|
|
|
String g_home_url;
|
2021-01-30 23:05:21 +01:00
|
|
|
static bool s_single_process = false;
|
2020-05-10 11:18:47 +02:00
|
|
|
|
2020-08-05 17:40:03 +02:00
|
|
|
static String bookmarks_file_path()
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(Core::StandardPaths::config_directory());
|
|
|
|
builder.append("/bookmarks.json");
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:18:47 +02:00
|
|
|
}
|
2020-04-25 17:20:23 +02:00
|
|
|
|
2019-10-05 10:19:12 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-17 08:18:11 +01:00
|
|
|
if (pledge("stdio recvfd sendfd accept unix cpath rpath wpath fattr", nullptr) < 0) {
|
2020-01-11 20:50:27 +01:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:57:30 +02:00
|
|
|
const char* specified_url = nullptr;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2021-01-30 23:05:21 +01:00
|
|
|
args_parser.add_option(Browser::s_single_process, "Single-process mode", "single-process", 's');
|
2020-05-27 21:57:30 +02:00
|
|
|
args_parser.add_positional_argument(specified_url, "URL to open", "url", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
2020-07-04 14:05:19 +02:00
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
2019-10-05 10:19:12 +02:00
|
|
|
|
2020-01-12 13:16:43 +01:00
|
|
|
// Connect to the ProtocolServer immediately so we can drop the "unix" pledge.
|
2020-03-07 10:27:02 +01:00
|
|
|
Web::ResourceLoader::the();
|
2020-01-12 13:16:43 +01: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.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-17 08:18:11 +01:00
|
|
|
if (pledge("stdio recvfd sendfd accept unix cpath rpath wpath", nullptr) < 0) {
|
2020-01-11 20:50:27 +01:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:06:23 +01:00
|
|
|
if (unveil("/home", "rwc") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unveil("/res", "r") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-25 17:21:22 +02:00
|
|
|
if (unveil("/etc/passwd", "r") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
2020-05-26 12:54:58 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 21:41:10 +02:00
|
|
|
if (unveil("/tmp/portal/image", "rw") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:46:37 +02:00
|
|
|
if (unveil("/tmp/portal/webcontent", "rw") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:06:23 +01:00
|
|
|
unveil(nullptr, nullptr);
|
|
|
|
|
2020-11-02 19:30:17 +00:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-browser");
|
|
|
|
|
2020-04-25 03:25:11 +00:00
|
|
|
auto m_config = Core::ConfigFile::get_for_app("Browser");
|
2020-05-10 11:18:47 +02:00
|
|
|
Browser::g_home_url = m_config->read_entry("Preferences", "Home", "about:blank");
|
2020-04-25 03:25:11 +00:00
|
|
|
|
2021-01-05 18:12:29 +01:00
|
|
|
auto ad_filter_list_or_error = Core::File::open(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::IODevice::ReadOnly);
|
|
|
|
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-04-25 17:20:23 +02:00
|
|
|
bool bookmarksbar_enabled = true;
|
2020-08-05 17:40:03 +02:00
|
|
|
auto bookmarks_bar = Browser::BookmarksBarWidget::construct(Browser::bookmarks_file_path(), bookmarksbar_enabled);
|
2020-04-25 17:20:23 +02:00
|
|
|
|
2021-04-11 10:53:15 -04:00
|
|
|
Browser::CookieJar cookie_jar;
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
auto window = GUI::Window::construct();
|
2020-07-31 16:12:11 -06:00
|
|
|
window->resize(640, 480);
|
2020-11-02 19:30:17 +00:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2020-04-24 13:06:17 +01:00
|
|
|
window->set_title("Browser");
|
2019-10-05 10:19:12 +02:00
|
|
|
|
2020-03-03 21:42:48 +01:00
|
|
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
2020-12-20 11:47:44 +01:00
|
|
|
widget.load_from_gml(browser_window_gml);
|
2019-10-05 10:19:12 +02:00
|
|
|
|
2021-04-09 22:23:32 +02:00
|
|
|
auto& top_line = *widget.find_descendant_of_type_named<GUI::HorizontalSeparator>("top_line");
|
|
|
|
|
2021-01-01 00:57:48 -07:00
|
|
|
auto& tab_widget = *widget.find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
|
2019-10-17 21:25:38 +02:00
|
|
|
|
2021-04-09 22:23:32 +02:00
|
|
|
tab_widget.on_tab_count_change = [&](size_t tab_count) {
|
|
|
|
top_line.set_visible(tab_count > 1);
|
|
|
|
};
|
|
|
|
|
2020-04-24 22:47:53 +02:00
|
|
|
auto default_favicon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(default_favicon);
|
2020-04-24 22:47:53 +02:00
|
|
|
|
2020-04-23 21:14:31 +02:00
|
|
|
tab_widget.on_change = [&](auto& active_widget) {
|
|
|
|
auto& tab = static_cast<Browser::Tab&>(active_widget);
|
2020-10-04 12:39:02 +02:00
|
|
|
window->set_title(String::formatted("{} - Browser", tab.title()));
|
2020-04-23 21:14:31 +02:00
|
|
|
tab.did_become_active();
|
2019-10-17 21:25:38 +02:00
|
|
|
};
|
|
|
|
|
2020-05-06 23:04:32 -04:00
|
|
|
tab_widget.on_middle_click = [&](auto& clicked_widget) {
|
|
|
|
auto& tab = static_cast<Browser::Tab&>(clicked_widget);
|
|
|
|
tab.on_tab_close_request(tab);
|
|
|
|
};
|
|
|
|
|
2020-05-18 18:37:12 -04:00
|
|
|
tab_widget.on_context_menu_request = [&](auto& clicked_widget, const GUI::ContextMenuEvent& context_menu_event) {
|
|
|
|
auto& tab = static_cast<Browser::Tab&>(clicked_widget);
|
|
|
|
tab.context_menu_requested(context_menu_event.screen_position());
|
|
|
|
};
|
|
|
|
|
2020-04-23 21:27:34 +02:00
|
|
|
Browser::WindowActions window_actions(*window);
|
|
|
|
|
2020-04-24 13:06:17 +01:00
|
|
|
Function<void(URL url, bool activate)> create_new_tab;
|
|
|
|
create_new_tab = [&](auto url, auto activate) {
|
2021-01-30 23:05:21 +01:00
|
|
|
auto type = Browser::s_single_process ? Browser::Tab::Type::InProcessWebView : Browser::Tab::Type::OutOfProcessWebView;
|
2020-07-06 21:46:37 +02:00
|
|
|
auto& new_tab = tab_widget.add_tab<Browser::Tab>("New tab", type);
|
2019-10-17 21:25:38 +02:00
|
|
|
|
2020-05-19 16:13:30 +01:00
|
|
|
tab_widget.set_bar_visible(!window->is_fullscreen() && tab_widget.children().size() > 1);
|
2020-04-24 22:28:05 +02:00
|
|
|
tab_widget.set_tab_icon(new_tab, default_favicon);
|
|
|
|
|
2020-04-23 21:14:31 +02:00
|
|
|
new_tab.on_title_change = [&](auto title) {
|
|
|
|
tab_widget.set_tab_title(new_tab, title);
|
|
|
|
if (tab_widget.active_widget() == &new_tab)
|
2020-10-04 12:39:02 +02:00
|
|
|
window->set_title(String::formatted("{} - Browser", title));
|
2020-04-23 21:14:31 +02:00
|
|
|
};
|
2019-10-17 21:25:38 +02:00
|
|
|
|
2020-04-24 22:28:05 +02:00
|
|
|
new_tab.on_favicon_change = [&](auto& bitmap) {
|
|
|
|
tab_widget.set_tab_icon(new_tab, &bitmap);
|
|
|
|
};
|
|
|
|
|
2020-04-24 13:06:17 +01:00
|
|
|
new_tab.on_tab_open_request = [&](auto& url) {
|
|
|
|
create_new_tab(url, true);
|
|
|
|
};
|
|
|
|
|
2020-04-23 21:36:17 +02:00
|
|
|
new_tab.on_tab_close_request = [&](auto& tab) {
|
|
|
|
tab_widget.deferred_invoke([&](auto&) {
|
|
|
|
tab_widget.remove_tab(tab);
|
2020-05-19 16:13:30 +01:00
|
|
|
tab_widget.set_bar_visible(!window->is_fullscreen() && tab_widget.children().size() > 1);
|
2020-04-23 21:36:17 +02:00
|
|
|
if (tab_widget.children().is_empty())
|
2020-07-04 14:05:19 +02:00
|
|
|
app->quit();
|
2020-04-23 21:36:17 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-04-13 17:30:41 -04:00
|
|
|
new_tab.on_get_cookie = [&](auto& url, auto source) -> String {
|
|
|
|
return cookie_jar.get_cookie(url, source);
|
2021-04-11 10:53:15 -04:00
|
|
|
};
|
|
|
|
|
2021-04-13 17:30:41 -04:00
|
|
|
new_tab.on_set_cookie = [&](auto& url, auto& cookie, auto source) {
|
|
|
|
cookie_jar.set_cookie(url, cookie, source);
|
2021-04-11 10:53:15 -04:00
|
|
|
};
|
|
|
|
|
2021-04-12 12:12:13 -04:00
|
|
|
new_tab.on_dump_cookies = [&]() {
|
|
|
|
cookie_jar.dump_cookies();
|
|
|
|
};
|
|
|
|
|
2020-04-24 13:06:17 +01:00
|
|
|
new_tab.load(url);
|
2019-10-05 10:19:12 +02:00
|
|
|
|
2020-10-04 17:42:31 +02:00
|
|
|
dbgln("Added new tab {:p}, loading {}", &new_tab, url);
|
|
|
|
|
2020-04-23 21:27:34 +02:00
|
|
|
if (activate)
|
|
|
|
tab_widget.set_active_widget(&new_tab);
|
|
|
|
};
|
|
|
|
|
2020-05-10 11:18:47 +02:00
|
|
|
URL first_url = Browser::g_home_url;
|
2020-06-12 21:29:27 +02: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 13:06:17 +01:00
|
|
|
|
2020-04-23 21:27:34 +02:00
|
|
|
window_actions.on_create_new_tab = [&] {
|
2020-05-10 11:18:47 +02:00
|
|
|
create_new_tab(Browser::g_home_url, true);
|
2019-10-10 22:07:08 +02:00
|
|
|
};
|
|
|
|
|
2020-04-23 21:43:24 +02:00
|
|
|
window_actions.on_next_tab = [&] {
|
|
|
|
tab_widget.activate_next_tab();
|
|
|
|
};
|
|
|
|
|
|
|
|
window_actions.on_previous_tab = [&] {
|
|
|
|
tab_widget.activate_previous_tab();
|
|
|
|
};
|
|
|
|
|
2020-04-24 20:50:06 +02:00
|
|
|
window_actions.on_about = [&] {
|
2020-11-02 19:30:17 +00:00
|
|
|
GUI::AboutDialog::show("Browser", app_icon.bitmap_for_size(32), window);
|
2020-04-24 20:50:06 +02:00
|
|
|
};
|
|
|
|
|
2020-04-25 17:20:23 +02:00
|
|
|
window_actions.on_show_bookmarks_bar = [&](auto& action) {
|
|
|
|
Browser::BookmarksBarWidget::the().set_visible(action.is_checked());
|
|
|
|
};
|
|
|
|
window_actions.show_bookmarks_bar_action().set_checked(bookmarksbar_enabled);
|
|
|
|
|
2020-05-10 11:18:47 +02:00
|
|
|
create_new_tab(first_url, true);
|
2020-04-24 13:06:17 +01:00
|
|
|
window->show();
|
2019-10-05 10:19:12 +02:00
|
|
|
|
2020-07-04 14:05:19 +02:00
|
|
|
return app->exec();
|
2019-10-05 10:19:12 +02:00
|
|
|
}
|