main.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/IterationDecision.h>
  9. #include <Applications/Browser/Browser.h>
  10. #include <Applications/Browser/BrowserWindow.h>
  11. #include <Applications/Browser/CookieJar.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/File.h>
  17. #include <LibCore/FileWatcher.h>
  18. #include <LibCore/StandardPaths.h>
  19. #include <LibCore/System.h>
  20. #include <LibDesktop/Launcher.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. String g_search_engine;
  32. String g_home_url;
  33. String g_new_tab_url;
  34. Vector<String> g_content_filters;
  35. bool g_content_filters_enabled { true };
  36. Vector<String> g_proxies;
  37. HashMap<String, size_t> g_proxy_mappings;
  38. IconBag g_icon_bag;
  39. String g_webdriver_content_ipc_path;
  40. }
  41. static ErrorOr<void> load_content_filters()
  42. {
  43. auto file = TRY(Core::Stream::File::open(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::Stream::OpenMode::Read));
  44. auto ad_filter_list = TRY(Core::Stream::BufferedFile::create(move(file)));
  45. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  46. while (TRY(ad_filter_list->can_read_line())) {
  47. auto line = TRY(ad_filter_list->read_line(buffer));
  48. if (!line.is_empty())
  49. Browser::g_content_filters.append(line);
  50. }
  51. return {};
  52. }
  53. ErrorOr<int> serenity_main(Main::Arguments arguments)
  54. {
  55. if (getuid() == 0) {
  56. warnln("Refusing to run as root");
  57. return 1;
  58. }
  59. TRY(Core::System::pledge("stdio recvfd sendfd unix fattr cpath rpath wpath proc exec"));
  60. Vector<String> specified_urls;
  61. Core::ArgsParser args_parser;
  62. args_parser.add_positional_argument(specified_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  63. args_parser.add_option(Browser::g_webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path");
  64. args_parser.parse(arguments);
  65. auto app = TRY(GUI::Application::try_create(arguments));
  66. Config::pledge_domain("Browser");
  67. Config::monitor_domain("Browser");
  68. // Connect to LaunchServer immediately and let it know that we won't ask for anything other than opening
  69. // the user's downloads directory.
  70. // FIXME: This should go away with a standalone download manager at some point.
  71. TRY(Desktop::Launcher::add_allowed_url(URL::create_with_file_scheme(Core::StandardPaths::downloads_directory())));
  72. TRY(Desktop::Launcher::seal_allowlist());
  73. if (!Browser::g_webdriver_content_ipc_path.is_empty())
  74. specified_urls.empend("about:blank");
  75. TRY(Core::System::unveil("/sys/kernel/processes", "r"));
  76. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  77. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  78. TRY(Core::System::unveil("/tmp/session/%sid/portal/image", "rw"));
  79. TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw"));
  80. TRY(Core::System::unveil("/tmp/session/%sid/portal/request", "rw"));
  81. TRY(Core::System::unveil("/home", "rwc"));
  82. TRY(Core::System::unveil("/res", "r"));
  83. TRY(Core::System::unveil("/etc/passwd", "r"));
  84. TRY(Core::System::unveil("/etc/timezone", "r"));
  85. TRY(Core::System::unveil("/bin/BrowserSettings", "x"));
  86. TRY(Core::System::unveil("/bin/Browser", "x"));
  87. TRY(Core::System::unveil(nullptr, nullptr));
  88. Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create()));
  89. auto app_icon = GUI::Icon::default_icon("app-browser"sv);
  90. Browser::g_home_url = Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, "file:///res/html/misc/welcome.html"sv);
  91. Browser::g_new_tab_url = Config::read_string("Browser"sv, "Preferences"sv, "NewTab"sv, "file:///res/html/misc/new-tab.html"sv);
  92. Browser::g_search_engine = Config::read_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, {});
  93. Browser::g_content_filters_enabled = Config::read_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, true);
  94. Browser::g_icon_bag = TRY(Browser::IconBag::try_create());
  95. TRY(load_content_filters());
  96. for (auto& group : Config::list_groups("Browser"sv)) {
  97. if (!group.starts_with("Proxy:"sv))
  98. continue;
  99. for (auto& key : Config::list_keys("Browser"sv, group)) {
  100. auto proxy_spec = group.substring_view(6);
  101. auto existing_proxy = Browser::g_proxies.find(proxy_spec);
  102. if (existing_proxy.is_end())
  103. Browser::g_proxies.append(proxy_spec);
  104. Browser::g_proxy_mappings.set(key, existing_proxy.index());
  105. }
  106. }
  107. auto url_from_argument_string = [](String const& string) -> URL {
  108. if (Core::File::exists(string)) {
  109. return URL::create_with_file_scheme(Core::File::real_path_for(string));
  110. }
  111. return Browser::url_from_user_input(string);
  112. };
  113. URL first_url = Browser::url_from_user_input(Browser::g_home_url);
  114. if (!specified_urls.is_empty())
  115. first_url = url_from_argument_string(specified_urls.first());
  116. Browser::CookieJar cookie_jar;
  117. auto window = Browser::BrowserWindow::construct(cookie_jar, first_url);
  118. auto content_filters_watcher = TRY(Core::FileWatcher::create());
  119. content_filters_watcher->on_change = [&](Core::FileWatcherEvent const&) {
  120. dbgln("Reloading content filters because config file changed");
  121. auto error = load_content_filters();
  122. if (error.is_error()) {
  123. dbgln("Reloading content filters failed: {}", error.release_error());
  124. return;
  125. }
  126. window->content_filters_changed();
  127. };
  128. TRY(content_filters_watcher->add_watch(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::FileWatcherEvent::Type::ContentModified));
  129. app->on_action_enter = [&](GUI::Action& action) {
  130. if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
  131. auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
  132. if (!tab)
  133. return;
  134. tab->action_entered(action);
  135. }
  136. };
  137. app->on_action_leave = [&](auto& action) {
  138. if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
  139. auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
  140. if (!tab)
  141. return;
  142. tab->action_left(action);
  143. }
  144. };
  145. for (size_t i = 1; i < specified_urls.size(); ++i)
  146. window->create_new_tab(url_from_argument_string(specified_urls[i]), false);
  147. window->show();
  148. window->broadcast_window_position(window->position());
  149. window->broadcast_window_size(window->size());
  150. return app->exec();
  151. }