main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <AK/Enumerate.h>
  9. #include <Applications/Browser/Browser.h>
  10. #include <Applications/Browser/BrowserWindow.h>
  11. #include <Applications/Browser/Tab.h>
  12. #include <Applications/Browser/WindowActions.h>
  13. #include <Applications/BrowserSettings/Defaults.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 <LibGUI/Application.h>
  21. #include <LibGUI/BoxLayout.h>
  22. #include <LibGUI/Icon.h>
  23. #include <LibGUI/TabWidget.h>
  24. #include <LibMain/Main.h>
  25. #include <LibWeb/Loader/ResourceLoader.h>
  26. #include <LibWebView/ChromeProcess.h>
  27. #include <LibWebView/CookieJar.h>
  28. #include <LibWebView/Database.h>
  29. #include <LibWebView/OutOfProcessWebView.h>
  30. #include <LibWebView/ProcessManager.h>
  31. #include <LibWebView/RequestServerAdapter.h>
  32. #include <LibWebView/SearchEngine.h>
  33. #include <LibWebView/URL.h>
  34. #include <unistd.h>
  35. namespace Browser {
  36. ByteString g_search_engine;
  37. ByteString g_home_url;
  38. ByteString g_new_tab_url;
  39. Vector<String> g_content_filters;
  40. bool g_content_filters_enabled { true };
  41. Vector<String> g_autoplay_allowlist;
  42. bool g_autoplay_allowed_on_all_websites { false };
  43. Vector<ByteString> g_proxies;
  44. HashMap<ByteString, size_t> g_proxy_mappings;
  45. IconBag g_icon_bag;
  46. ByteString g_webdriver_content_ipc_path;
  47. }
  48. static ErrorOr<void> load_content_filters()
  49. {
  50. auto file = TRY(Core::File::open(TRY(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory())), Core::File::OpenMode::Read));
  51. auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file)));
  52. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  53. Browser::g_content_filters.clear_with_capacity();
  54. while (TRY(ad_filter_list->can_read_line())) {
  55. auto line = TRY(ad_filter_list->read_line(buffer));
  56. if (line.is_empty())
  57. continue;
  58. auto pattern = TRY(String::from_utf8(line));
  59. TRY(Browser::g_content_filters.try_append(move(pattern)));
  60. }
  61. return {};
  62. }
  63. static ErrorOr<void> load_autoplay_allowlist()
  64. {
  65. auto file = TRY(Core::File::open(TRY(String::formatted("{}/BrowserAutoplayAllowlist.txt", Core::StandardPaths::config_directory())), Core::File::OpenMode::Read));
  66. auto allowlist = TRY(Core::InputBufferedFile::create(move(file)));
  67. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  68. Browser::g_autoplay_allowlist.clear_with_capacity();
  69. while (TRY(allowlist->can_read_line())) {
  70. auto line = TRY(allowlist->read_line(buffer));
  71. if (line.is_empty())
  72. continue;
  73. auto domain = TRY(String::from_utf8(line));
  74. TRY(Browser::g_autoplay_allowlist.try_append(move(domain)));
  75. }
  76. return {};
  77. }
  78. enum class NewWindow {
  79. No,
  80. Yes,
  81. };
  82. static Vector<URL::URL> sanitize_urls(Vector<ByteString> const& raw_urls, NewWindow new_window = NewWindow::Yes)
  83. {
  84. Vector<URL::URL> sanitized_urls;
  85. for (auto const& raw_url : raw_urls) {
  86. if (auto url = WebView::sanitize_url(raw_url); url.has_value())
  87. sanitized_urls.append(url.release_value());
  88. }
  89. if (sanitized_urls.is_empty())
  90. sanitized_urls.append(new_window == NewWindow::Yes ? Browser::g_home_url : Browser::g_new_tab_url);
  91. return sanitized_urls;
  92. }
  93. static void open_urls_from_client(Browser::BrowserWindow& window, Vector<ByteString> const& raw_urls, NewWindow new_window)
  94. {
  95. auto urls = sanitize_urls(raw_urls, new_window);
  96. for (auto [i, url] : enumerate(urls)) {
  97. if (new_window == NewWindow::Yes)
  98. outln("New browser windows are not yet supported. Opening URLs in a new tab.");
  99. auto activate_tab = i == 0 ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No;
  100. window.create_new_tab(url, activate_tab);
  101. }
  102. window.show();
  103. }
  104. ErrorOr<int> serenity_main(Main::Arguments arguments)
  105. {
  106. if (getuid() == 0) {
  107. warnln("Refusing to run as root");
  108. return 1;
  109. }
  110. TRY(Core::System::pledge("sigaction stdio recvfd sendfd accept unix fattr cpath rpath wpath proc exec"));
  111. WebView::ProcessManager::initialize();
  112. TRY(Core::System::pledge("stdio recvfd sendfd accept unix fattr cpath rpath wpath proc exec"));
  113. Vector<ByteString> specified_urls;
  114. bool new_window = false;
  115. Core::ArgsParser args_parser;
  116. args_parser.add_positional_argument(specified_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  117. 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);
  118. args_parser.add_option(new_window, "Force opening in a new window", "new-window", 'n');
  119. args_parser.parse(arguments);
  120. auto app = TRY(GUI::Application::create(arguments));
  121. auto const man_file = "/usr/share/man/man1/Applications/Browser.md"sv;
  122. Config::pledge_domains({ "Browser", "FileManager" });
  123. Config::monitor_domain("Browser");
  124. // Connect to LaunchServer immediately and let it know that we won't ask for anything other than opening
  125. // the user's downloads directory.
  126. // FIXME: This should go away with a standalone download manager at some point.
  127. TRY(Desktop::Launcher::add_allowed_url(URL::create_with_file_scheme(Core::StandardPaths::downloads_directory())));
  128. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme(man_file) }));
  129. TRY(Desktop::Launcher::seal_allowlist());
  130. TRY(Core::System::unveil("/tmp/session/%sid/Ladybird.pid", "rwc"));
  131. TRY(Core::System::unveil("/tmp/session/%sid/Ladybird.socket", "rwc"));
  132. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  133. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  134. TRY(Core::System::unveil("/tmp/session/%sid/portal/image", "rw"));
  135. TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw"));
  136. TRY(Core::System::unveil("/tmp/session/%sid/portal/webworker", "rw"));
  137. TRY(Core::System::unveil("/tmp/session/%sid/portal/request", "rw"));
  138. TRY(Core::System::unveil("/tmp/session/%sid/portal/sql", "rw"));
  139. TRY(Core::System::unveil("/home", "rwc"));
  140. TRY(Core::System::unveil("/res", "r"));
  141. TRY(Core::System::unveil("/etc/group", "r"));
  142. TRY(Core::System::unveil("/etc/passwd", "r"));
  143. TRY(Core::System::unveil("/etc/timezone", "r"));
  144. TRY(Core::System::unveil("/etc/FileIconProvider.ini", "r"));
  145. TRY(Core::System::unveil("/sys/kernel/processes", "r"));
  146. TRY(Core::System::unveil("/bin/BrowserSettings", "x"));
  147. TRY(Core::System::unveil("/bin/Browser", "x"));
  148. TRY(Core::System::unveil(nullptr, nullptr));
  149. WebView::ChromeProcess chrome_process;
  150. if (TRY(chrome_process.connect(specified_urls, new_window)) == WebView::ChromeProcess::ProcessDisposition::ExitProcess) {
  151. outln("Opening in existing process");
  152. return 0;
  153. }
  154. Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create()));
  155. auto app_icon = GUI::Icon::default_icon("app-browser"sv);
  156. Browser::g_home_url = Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, Browser::default_homepage_url);
  157. Browser::g_new_tab_url = Config::read_string("Browser"sv, "Preferences"sv, "NewTab"sv, Browser::default_new_tab_url);
  158. Browser::g_search_engine = Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, WebView::default_search_engine().query_url);
  159. Browser::g_content_filters_enabled = Config::read_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, Browser::default_enable_content_filters);
  160. Browser::g_autoplay_allowed_on_all_websites = Config::read_bool("Browser"sv, "Preferences"sv, "AllowAutoplayOnAllWebsites"sv, Browser::default_allow_autoplay_on_all_websites);
  161. Browser::g_icon_bag = TRY(Browser::IconBag::try_create());
  162. auto database = TRY(WebView::Database::create());
  163. TRY(load_content_filters());
  164. TRY(load_autoplay_allowlist());
  165. for (auto& group : Config::list_groups("Browser"sv)) {
  166. if (!group.starts_with("Proxy:"sv))
  167. continue;
  168. for (auto& key : Config::list_keys("Browser"sv, group)) {
  169. auto proxy_spec = group.substring_view(6);
  170. auto existing_proxy = Browser::g_proxies.find(proxy_spec);
  171. if (existing_proxy.is_end())
  172. Browser::g_proxies.append(proxy_spec);
  173. Browser::g_proxy_mappings.set(key, existing_proxy.index());
  174. }
  175. }
  176. auto cookie_jar = TRY(WebView::CookieJar::create(*database));
  177. auto window = Browser::BrowserWindow::construct(cookie_jar, sanitize_urls(specified_urls), man_file);
  178. chrome_process.on_new_tab = [&](auto const& raw_urls) {
  179. open_urls_from_client(*window, raw_urls, NewWindow::No);
  180. };
  181. chrome_process.on_new_window = [&](auto const& raw_urls) {
  182. open_urls_from_client(*window, raw_urls, NewWindow::Yes);
  183. };
  184. auto content_filters_watcher = TRY(Core::FileWatcher::create());
  185. content_filters_watcher->on_change = [&](Core::FileWatcherEvent const&) {
  186. dbgln("Reloading content filters because config file changed");
  187. auto error = load_content_filters();
  188. if (error.is_error()) {
  189. dbgln("Reloading content filters failed: {}", error.release_error());
  190. return;
  191. }
  192. window->content_filters_changed();
  193. };
  194. TRY(content_filters_watcher->add_watch(ByteString::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::FileWatcherEvent::Type::ContentModified));
  195. auto autoplay_allowlist_watcher = TRY(Core::FileWatcher::create());
  196. autoplay_allowlist_watcher->on_change = [&](Core::FileWatcherEvent const&) {
  197. dbgln("Reloading autoplay allowlist because config file changed");
  198. if (auto error = load_autoplay_allowlist(); error.is_error()) {
  199. dbgln("Reloading autoplay allowlist failed: {}", error.release_error());
  200. return;
  201. }
  202. window->autoplay_allowlist_changed();
  203. };
  204. TRY(autoplay_allowlist_watcher->add_watch(ByteString::formatted("{}/BrowserAutoplayAllowlist.txt", Core::StandardPaths::config_directory()), Core::FileWatcherEvent::Type::ContentModified));
  205. app->on_action_enter = [&](GUI::Action& action) {
  206. if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
  207. auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
  208. if (!tab)
  209. return;
  210. tab->action_entered(action);
  211. }
  212. };
  213. app->on_action_leave = [&](auto& action) {
  214. if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
  215. auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
  216. if (!tab)
  217. return;
  218. tab->action_left(action);
  219. }
  220. };
  221. window->show();
  222. window->broadcast_window_position(window->position());
  223. window->broadcast_window_size(window->size());
  224. return app->exec();
  225. }