main.cpp 4.3 KB

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