main.cpp 6.5 KB

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