main.cpp 9.5 KB

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