main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Browser.h"
  7. #include "BrowserWindow.h"
  8. #include "CookieJar.h"
  9. #include "Tab.h"
  10. #include "WindowActions.h"
  11. #include <AK/StringBuilder.h>
  12. #include <LibConfig/Client.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/File.h>
  15. #include <LibCore/StandardPaths.h>
  16. #include <LibCore/System.h>
  17. #include <LibDesktop/Launcher.h>
  18. #include <LibGUI/Application.h>
  19. #include <LibGUI/BoxLayout.h>
  20. #include <LibGUI/Icon.h>
  21. #include <LibGUI/TabWidget.h>
  22. #include <LibMain/Main.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. namespace Browser {
  26. String g_search_engine;
  27. String g_home_url;
  28. Vector<String> g_content_filters;
  29. }
  30. ErrorOr<int> serenity_main(Main::Arguments arguments)
  31. {
  32. if (getuid() == 0) {
  33. warnln("Refusing to run as root");
  34. return 1;
  35. }
  36. TRY(Core::System::pledge("stdio recvfd sendfd unix cpath rpath wpath", nullptr));
  37. const char* specified_url = nullptr;
  38. Core::ArgsParser args_parser;
  39. args_parser.add_positional_argument(specified_url, "URL to open", "url", Core::ArgsParser::Required::No);
  40. args_parser.parse(arguments);
  41. auto app = GUI::Application::construct(arguments);
  42. Config::pledge_domains("Browser");
  43. // Connect to LaunchServer immediately and let it know that we won't ask for anything other than opening
  44. // the user's downloads directory.
  45. // FIXME: This should go away with a standalone download manager at some point.
  46. if (!Desktop::Launcher::add_allowed_url(URL::create_with_file_protocol(Core::StandardPaths::downloads_directory()))
  47. || !Desktop::Launcher::seal_allowlist()) {
  48. warnln("Failed to set up allowed launch URLs");
  49. return 1;
  50. }
  51. TRY(Core::System::unveil("/home", "rwc"));
  52. TRY(Core::System::unveil("/res", "r"));
  53. TRY(Core::System::unveil("/etc/passwd", "r"));
  54. TRY(Core::System::unveil("/tmp/portal/image", "rw"));
  55. TRY(Core::System::unveil("/tmp/portal/webcontent", "rw"));
  56. TRY(Core::System::unveil("/tmp/portal/request", "rw"));
  57. TRY(Core::System::unveil(nullptr, nullptr));
  58. auto app_icon = GUI::Icon::default_icon("app-browser");
  59. Browser::g_home_url = Config::read_string("Browser", "Preferences", "Home", "about:blank");
  60. Browser::g_search_engine = Config::read_string("Browser", "Preferences", "SearchEngine", {});
  61. auto ad_filter_list_or_error = Core::File::open(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::OpenMode::ReadOnly);
  62. if (!ad_filter_list_or_error.is_error()) {
  63. auto& ad_filter_list = *ad_filter_list_or_error.value();
  64. while (!ad_filter_list.eof()) {
  65. auto line = ad_filter_list.read_line();
  66. if (line.is_empty())
  67. continue;
  68. Browser::g_content_filters.append(move(line));
  69. }
  70. }
  71. URL first_url = Browser::g_home_url;
  72. if (specified_url) {
  73. if (Core::File::exists(specified_url)) {
  74. first_url = URL::create_with_file_protocol(Core::File::real_path_for(specified_url));
  75. } else {
  76. first_url = Browser::url_from_user_input(specified_url);
  77. }
  78. }
  79. Browser::CookieJar cookie_jar;
  80. auto window = Browser::BrowserWindow::construct(cookie_jar, first_url);
  81. app->on_action_enter = [&](GUI::Action& action) {
  82. if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
  83. auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
  84. if (!tab)
  85. return;
  86. tab->action_entered(action);
  87. }
  88. };
  89. app->on_action_leave = [&](auto& action) {
  90. if (auto* browser_window = dynamic_cast<Browser::BrowserWindow*>(app->active_window())) {
  91. auto* tab = static_cast<Browser::Tab*>(browser_window->tab_widget().active_widget());
  92. if (!tab)
  93. return;
  94. tab->action_left(action);
  95. }
  96. };
  97. window->show();
  98. return app->exec();
  99. }