main.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <LibConfig/Client.h>
  15. #include <LibCore/ArgsParser.h>
  16. #include <LibCore/FileWatcher.h>
  17. #include <LibCore/StandardPaths.h>
  18. #include <LibCore/System.h>
  19. #include <LibDesktop/Launcher.h>
  20. #include <LibFileSystem/FileSystem.h>
  21. #include <LibGUI/Application.h>
  22. #include <LibGUI/BoxLayout.h>
  23. #include <LibGUI/Icon.h>
  24. #include <LibGUI/TabWidget.h>
  25. #include <LibMain/Main.h>
  26. #include <LibWeb/Loader/ResourceLoader.h>
  27. #include <LibWebView/OutOfProcessWebView.h>
  28. #include <LibWebView/RequestServerAdapter.h>
  29. #include <unistd.h>
  30. namespace Browser {
  31. DeprecatedString g_search_engine;
  32. DeprecatedString g_home_url;
  33. DeprecatedString g_new_tab_url;
  34. Vector<DeprecatedString> g_content_filters;
  35. bool g_content_filters_enabled { true };
  36. Vector<String> g_autoplay_allowlist;
  37. bool g_autoplay_allowed_on_all_websites { false };
  38. Vector<DeprecatedString> g_proxies;
  39. HashMap<DeprecatedString, size_t> g_proxy_mappings;
  40. IconBag g_icon_bag;
  41. DeprecatedString g_webdriver_content_ipc_path;
  42. }
  43. static ErrorOr<void> load_content_filters()
  44. {
  45. auto file = TRY(Core::File::open(DeprecatedString::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::File::OpenMode::Read));
  46. auto ad_filter_list = TRY(Core::BufferedFile::create(move(file)));
  47. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  48. Browser::g_content_filters.clear_with_capacity();
  49. while (TRY(ad_filter_list->can_read_line())) {
  50. auto line = TRY(ad_filter_list->read_line(buffer));
  51. if (!line.is_empty())
  52. Browser::g_content_filters.append(line);
  53. }
  54. return {};
  55. }
  56. static ErrorOr<void> load_autoplay_allowlist()
  57. {
  58. auto file = TRY(Core::File::open(TRY(String::formatted("{}/BrowserAutoplayAllowlist.txt", Core::StandardPaths::config_directory())), Core::File::OpenMode::Read));
  59. auto allowlist = TRY(Core::BufferedFile::create(move(file)));
  60. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  61. Browser::g_autoplay_allowlist.clear_with_capacity();
  62. while (TRY(allowlist->can_read_line())) {
  63. auto line = TRY(allowlist->read_line(buffer));
  64. if (line.is_empty())
  65. continue;
  66. auto domain = TRY(String::from_utf8(line));
  67. TRY(Browser::g_autoplay_allowlist.try_append(move(domain)));
  68. }
  69. return {};
  70. }
  71. ErrorOr<int> serenity_main(Main::Arguments arguments)
  72. {
  73. if (getuid() == 0) {
  74. warnln("Refusing to run as root");
  75. return 1;
  76. }
  77. TRY(Core::System::pledge("stdio recvfd sendfd unix fattr cpath rpath wpath proc exec"));
  78. Vector<DeprecatedString> specified_urls;
  79. Core::ArgsParser args_parser;
  80. args_parser.add_positional_argument(specified_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  81. args_parser.add_option(Browser::g_webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path");
  82. args_parser.parse(arguments);
  83. auto app = TRY(GUI::Application::try_create(arguments));
  84. Config::pledge_domain("Browser");
  85. Config::monitor_domain("Browser");
  86. // Connect to LaunchServer immediately and let it know that we won't ask for anything other than opening
  87. // the user's downloads directory.
  88. // FIXME: This should go away with a standalone download manager at some point.
  89. TRY(Desktop::Launcher::add_allowed_url(URL::create_with_file_scheme(Core::StandardPaths::downloads_directory())));
  90. TRY(Desktop::Launcher::seal_allowlist());
  91. if (!Browser::g_webdriver_content_ipc_path.is_empty())
  92. specified_urls.empend("about:blank");
  93. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  94. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  95. TRY(Core::System::unveil("/tmp/session/%sid/portal/image", "rw"));
  96. TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw"));
  97. TRY(Core::System::unveil("/tmp/session/%sid/portal/request", "rw"));
  98. TRY(Core::System::unveil("/tmp/session/%sid/portal/sql", "rw"));
  99. TRY(Core::System::unveil("/home", "rwc"));
  100. TRY(Core::System::unveil("/res", "r"));
  101. TRY(Core::System::unveil("/etc/passwd", "r"));
  102. TRY(Core::System::unveil("/etc/timezone", "r"));
  103. TRY(Core::System::unveil("/bin/BrowserSettings", "x"));
  104. TRY(Core::System::unveil("/bin/Browser", "x"));
  105. TRY(Core::System::unveil(nullptr, nullptr));
  106. Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create()));
  107. auto app_icon = GUI::Icon::default_icon("app-browser"sv);
  108. Browser::g_home_url = Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, "file:///res/html/misc/welcome.html"sv);
  109. Browser::g_new_tab_url = Config::read_string("Browser"sv, "Preferences"sv, "NewTab"sv, "file:///res/html/misc/new-tab.html"sv);
  110. Browser::g_search_engine = Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, {});
  111. Browser::g_content_filters_enabled = Config::read_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, true);
  112. Browser::g_autoplay_allowed_on_all_websites = Config::read_bool("Browser"sv, "Preferences"sv, "AllowAutoplayOnAllWebsites"sv, false);
  113. Browser::g_icon_bag = TRY(Browser::IconBag::try_create());
  114. auto database = TRY(Browser::Database::create());
  115. TRY(load_content_filters());
  116. TRY(load_autoplay_allowlist());
  117. for (auto& group : Config::list_groups("Browser"sv)) {
  118. if (!group.starts_with("Proxy:"sv))
  119. continue;
  120. for (auto& key : Config::list_keys("Browser"sv, group)) {
  121. auto proxy_spec = group.substring_view(6);
  122. auto existing_proxy = Browser::g_proxies.find(proxy_spec);
  123. if (existing_proxy.is_end())
  124. Browser::g_proxies.append(proxy_spec);
  125. Browser::g_proxy_mappings.set(key, existing_proxy.index());
  126. }
  127. }
  128. auto url_from_argument_string = [](DeprecatedString const& string) -> ErrorOr<URL> {
  129. if (FileSystem::exists(string)) {
  130. return URL::create_with_file_scheme(TRY(FileSystem::real_path(string)).to_deprecated_string());
  131. }
  132. return Browser::url_from_user_input(string);
  133. };
  134. URL first_url = Browser::url_from_user_input(Browser::g_home_url);
  135. if (!specified_urls.is_empty())
  136. first_url = TRY(url_from_argument_string(specified_urls.first()));
  137. auto cookie_jar = TRY(Browser::CookieJar::create(*database));
  138. auto window = Browser::BrowserWindow::construct(cookie_jar, first_url);
  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(DeprecatedString::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(DeprecatedString::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. for (size_t i = 1; i < specified_urls.size(); ++i)
  177. window->create_new_tab(TRY(url_from_argument_string(specified_urls[i])), Web::HTML::ActivateTab::No);
  178. window->show();
  179. window->broadcast_window_position(window->position());
  180. window->broadcast_window_size(window->size());
  181. return app->exec();
  182. }