main.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <Applications/Browser/Browser.h>
  9. #include <Applications/Browser/BrowserWindow.h>
  10. #include <Applications/Browser/Tab.h>
  11. #include <Applications/Browser/WindowActions.h>
  12. #include <Applications/BrowserSettings/Defaults.h>
  13. #include <LibConfig/Client.h>
  14. #include <LibCore/ArgsParser.h>
  15. #include <LibCore/FileWatcher.h>
  16. #include <LibCore/StandardPaths.h>
  17. #include <LibCore/System.h>
  18. #include <LibDesktop/Launcher.h>
  19. #include <LibGUI/Application.h>
  20. #include <LibGUI/BoxLayout.h>
  21. #include <LibGUI/Icon.h>
  22. #include <LibGUI/TabWidget.h>
  23. #include <LibMain/Main.h>
  24. #include <LibWeb/Loader/ResourceLoader.h>
  25. #include <LibWebView/CookieJar.h>
  26. #include <LibWebView/Database.h>
  27. #include <LibWebView/OutOfProcessWebView.h>
  28. #include <LibWebView/RequestServerAdapter.h>
  29. #include <LibWebView/SearchEngine.h>
  30. #include <LibWebView/URL.h>
  31. #include <unistd.h>
  32. namespace Browser {
  33. ByteString g_search_engine;
  34. ByteString g_home_url;
  35. ByteString g_new_tab_url;
  36. Vector<String> g_content_filters;
  37. bool g_content_filters_enabled { true };
  38. Vector<String> g_autoplay_allowlist;
  39. bool g_autoplay_allowed_on_all_websites { false };
  40. Vector<ByteString> g_proxies;
  41. HashMap<ByteString, size_t> g_proxy_mappings;
  42. IconBag g_icon_bag;
  43. ByteString g_webdriver_content_ipc_path;
  44. }
  45. static ErrorOr<void> load_content_filters()
  46. {
  47. auto file = TRY(Core::File::open(TRY(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory())), Core::File::OpenMode::Read));
  48. auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file)));
  49. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  50. Browser::g_content_filters.clear_with_capacity();
  51. while (TRY(ad_filter_list->can_read_line())) {
  52. auto line = TRY(ad_filter_list->read_line(buffer));
  53. if (line.is_empty())
  54. continue;
  55. auto pattern = TRY(String::from_utf8(line));
  56. TRY(Browser::g_content_filters.try_append(move(pattern)));
  57. }
  58. return {};
  59. }
  60. static ErrorOr<void> load_autoplay_allowlist()
  61. {
  62. auto file = TRY(Core::File::open(TRY(String::formatted("{}/BrowserAutoplayAllowlist.txt", Core::StandardPaths::config_directory())), Core::File::OpenMode::Read));
  63. auto allowlist = TRY(Core::InputBufferedFile::create(move(file)));
  64. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  65. Browser::g_autoplay_allowlist.clear_with_capacity();
  66. while (TRY(allowlist->can_read_line())) {
  67. auto line = TRY(allowlist->read_line(buffer));
  68. if (line.is_empty())
  69. continue;
  70. auto domain = TRY(String::from_utf8(line));
  71. TRY(Browser::g_autoplay_allowlist.try_append(move(domain)));
  72. }
  73. return {};
  74. }
  75. ErrorOr<int> serenity_main(Main::Arguments arguments)
  76. {
  77. if (getuid() == 0) {
  78. warnln("Refusing to run as root");
  79. return 1;
  80. }
  81. TRY(Core::System::pledge("stdio recvfd sendfd unix fattr cpath rpath wpath proc exec"));
  82. Vector<StringView> specified_urls;
  83. Core::ArgsParser args_parser;
  84. args_parser.add_positional_argument(specified_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  85. 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);
  86. args_parser.parse(arguments);
  87. auto app = TRY(GUI::Application::create(arguments));
  88. Config::pledge_domain("Browser");
  89. Config::monitor_domain("Browser");
  90. // Connect to LaunchServer immediately and let it know that we won't ask for anything other than opening
  91. // the user's downloads directory.
  92. // FIXME: This should go away with a standalone download manager at some point.
  93. TRY(Desktop::Launcher::add_allowed_url(URL::create_with_file_scheme(Core::StandardPaths::downloads_directory())));
  94. TRY(Desktop::Launcher::seal_allowlist());
  95. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  96. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  97. TRY(Core::System::unveil("/tmp/session/%sid/portal/image", "rw"));
  98. TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw"));
  99. TRY(Core::System::unveil("/tmp/session/%sid/portal/request", "rw"));
  100. TRY(Core::System::unveil("/tmp/session/%sid/portal/sql", "rw"));
  101. TRY(Core::System::unveil("/home", "rwc"));
  102. TRY(Core::System::unveil("/res", "r"));
  103. TRY(Core::System::unveil("/etc/passwd", "r"));
  104. TRY(Core::System::unveil("/etc/timezone", "r"));
  105. TRY(Core::System::unveil("/bin/BrowserSettings", "x"));
  106. TRY(Core::System::unveil("/bin/Browser", "x"));
  107. TRY(Core::System::unveil(nullptr, nullptr));
  108. Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create()));
  109. auto app_icon = GUI::Icon::default_icon("app-browser"sv);
  110. Browser::g_home_url = Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, Browser::default_homepage_url);
  111. Browser::g_new_tab_url = Config::read_string("Browser"sv, "Preferences"sv, "NewTab"sv, Browser::default_new_tab_url);
  112. Browser::g_search_engine = Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, WebView::default_search_engine().query_url);
  113. Browser::g_content_filters_enabled = Config::read_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, Browser::default_enable_content_filters);
  114. Browser::g_autoplay_allowed_on_all_websites = Config::read_bool("Browser"sv, "Preferences"sv, "AllowAutoplayOnAllWebsites"sv, Browser::default_allow_autoplay_on_all_websites);
  115. Browser::g_icon_bag = TRY(Browser::IconBag::try_create());
  116. auto database = TRY(WebView::Database::create());
  117. TRY(load_content_filters());
  118. TRY(load_autoplay_allowlist());
  119. for (auto& group : Config::list_groups("Browser"sv)) {
  120. if (!group.starts_with("Proxy:"sv))
  121. continue;
  122. for (auto& key : Config::list_keys("Browser"sv, group)) {
  123. auto proxy_spec = group.substring_view(6);
  124. auto existing_proxy = Browser::g_proxies.find(proxy_spec);
  125. if (existing_proxy.is_end())
  126. Browser::g_proxies.append(proxy_spec);
  127. Browser::g_proxy_mappings.set(key, existing_proxy.index());
  128. }
  129. }
  130. Vector<URL> initial_urls;
  131. for (auto specified_url : specified_urls) {
  132. if (auto url = WebView::sanitize_url(specified_url); url.has_value())
  133. initial_urls.append(url.release_value());
  134. }
  135. if (initial_urls.is_empty())
  136. initial_urls.append(Browser::g_home_url);
  137. auto cookie_jar = TRY(WebView::CookieJar::create(*database));
  138. auto window = Browser::BrowserWindow::construct(cookie_jar, initial_urls);
  139. auto content_filters_watcher = TRY(Core::FileWatcher::create());
  140. content_filters_watcher->on_change = [&](Core::FileWatcherEvent const&) {
  141. dbgln("Reloading content filters because config file changed");
  142. auto error = load_content_filters();
  143. if (error.is_error()) {
  144. dbgln("Reloading content filters failed: {}", error.release_error());
  145. return;
  146. }
  147. window->content_filters_changed();
  148. };
  149. TRY(content_filters_watcher->add_watch(ByteString::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::FileWatcherEvent::Type::ContentModified));
  150. auto autoplay_allowlist_watcher = TRY(Core::FileWatcher::create());
  151. autoplay_allowlist_watcher->on_change = [&](Core::FileWatcherEvent const&) {
  152. dbgln("Reloading autoplay allowlist because config file changed");
  153. if (auto error = load_autoplay_allowlist(); error.is_error()) {
  154. dbgln("Reloading autoplay allowlist failed: {}", error.release_error());
  155. return;
  156. }
  157. window->autoplay_allowlist_changed();
  158. };
  159. TRY(autoplay_allowlist_watcher->add_watch(ByteString::formatted("{}/BrowserAutoplayAllowlist.txt", Core::StandardPaths::config_directory()), Core::FileWatcherEvent::Type::ContentModified));
  160. app->on_action_enter = [&](GUI::Action& action) {
  161. if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
  162. auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
  163. if (!tab)
  164. return;
  165. tab->action_entered(action);
  166. }
  167. };
  168. app->on_action_leave = [&](auto& action) {
  169. if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
  170. auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
  171. if (!tab)
  172. return;
  173. tab->action_left(action);
  174. }
  175. };
  176. window->show();
  177. window->broadcast_window_position(window->position());
  178. window->broadcast_window_size(window->size());
  179. return app->exec();
  180. }