main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "../AudioCodecPluginLadybird.h"
  7. #include "../EventLoopImplementationQt.h"
  8. #include "../FontPluginQt.h"
  9. #include "../ImageCodecPluginLadybird.h"
  10. #include "../RequestManagerQt.h"
  11. #include "../Utilities.h"
  12. #include "../WebSocketClientManagerLadybird.h"
  13. #include <AK/LexicalPath.h>
  14. #include <AK/Platform.h>
  15. #include <LibCore/ArgsParser.h>
  16. #include <LibCore/EventLoop.h>
  17. #include <LibCore/LocalServer.h>
  18. #include <LibCore/System.h>
  19. #include <LibCore/SystemServerTakeover.h>
  20. #include <LibIPC/ConnectionFromClient.h>
  21. #include <LibMain/Main.h>
  22. #include <LibWeb/Bindings/MainThreadVM.h>
  23. #include <LibWeb/Loader/ContentFilter.h>
  24. #include <LibWeb/Loader/FrameLoader.h>
  25. #include <LibWeb/Loader/ResourceLoader.h>
  26. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  27. #include <LibWeb/Platform/EventLoopPluginSerenity.h>
  28. #include <LibWeb/WebSockets/WebSocket.h>
  29. #include <QGuiApplication>
  30. #include <QTimer>
  31. #include <WebContent/ConnectionFromClient.h>
  32. #include <WebContent/PageHost.h>
  33. #include <WebContent/WebDriverConnection.h>
  34. #if defined(AK_OS_MACOS)
  35. # include "MacOSSetup.h"
  36. #endif
  37. static ErrorOr<void> load_content_filters();
  38. static ErrorOr<void> load_autoplay_allowlist();
  39. extern DeprecatedString s_serenity_resource_root;
  40. ErrorOr<int> serenity_main(Main::Arguments arguments)
  41. {
  42. QGuiApplication app(arguments.argc, arguments.argv);
  43. #if defined(AK_OS_MACOS)
  44. prohibit_interaction();
  45. #endif
  46. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  47. Core::EventLoop event_loop;
  48. platform_init();
  49. Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
  50. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPluginLadybird);
  51. Web::Platform::AudioCodecPlugin::install_creation_hook([] {
  52. return Ladybird::AudioCodecPluginLadybird::create();
  53. });
  54. Web::ResourceLoader::initialize(RequestManagerQt::create());
  55. Web::WebSockets::WebSocketClientManager::initialize(Ladybird::WebSocketClientManagerLadybird::create());
  56. Web::FrameLoader::set_default_favicon_path(DeprecatedString::formatted("{}/res/icons/16x16/app-browser.png", s_serenity_resource_root));
  57. int webcontent_fd_passing_socket { -1 };
  58. bool is_layout_test_mode = false;
  59. Core::ArgsParser args_parser;
  60. args_parser.add_option(webcontent_fd_passing_socket, "File descriptor of the passing socket for the WebContent connection", "webcontent-fd-passing-socket", 'c', "webcontent_fd_passing_socket");
  61. args_parser.add_option(is_layout_test_mode, "Is layout test mode", "layout-test-mode", 0);
  62. args_parser.parse(arguments);
  63. VERIFY(webcontent_fd_passing_socket >= 0);
  64. Web::Platform::FontPlugin::install(*new Ladybird::FontPluginQt(is_layout_test_mode));
  65. Web::FrameLoader::set_error_page_url(DeprecatedString::formatted("file://{}/res/html/error.html", s_serenity_resource_root));
  66. TRY(Web::Bindings::initialize_main_thread_vm());
  67. auto maybe_content_filter_error = load_content_filters();
  68. if (maybe_content_filter_error.is_error())
  69. dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
  70. auto maybe_autoplay_allowlist_error = load_autoplay_allowlist();
  71. if (maybe_autoplay_allowlist_error.is_error())
  72. dbgln("Failed to load autoplay allowlist: {}", maybe_autoplay_allowlist_error.error());
  73. auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv));
  74. auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
  75. webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(webcontent_fd_passing_socket)));
  76. return event_loop.exec();
  77. }
  78. static ErrorOr<void> load_content_filters()
  79. {
  80. auto file_or_error = Core::File::open(DeprecatedString::formatted("{}/home/anon/.config/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  81. if (file_or_error.is_error())
  82. file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  83. if (file_or_error.is_error())
  84. return file_or_error.release_error();
  85. auto file = file_or_error.release_value();
  86. auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file)));
  87. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  88. Vector<String> patterns;
  89. while (TRY(ad_filter_list->can_read_line())) {
  90. auto line = TRY(ad_filter_list->read_line(buffer));
  91. if (line.is_empty())
  92. continue;
  93. auto pattern = TRY(String::from_utf8(line));
  94. TRY(patterns.try_append(move(pattern)));
  95. }
  96. auto& content_filter = Web::ContentFilter::the();
  97. TRY(content_filter.set_patterns(patterns));
  98. return {};
  99. }
  100. static ErrorOr<void> load_autoplay_allowlist()
  101. {
  102. auto file_or_error = Core::File::open(TRY(String::formatted("{}/home/anon/.config/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  103. if (file_or_error.is_error())
  104. file_or_error = Core::File::open(TRY(String::formatted("{}/res/ladybird/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  105. if (file_or_error.is_error())
  106. return file_or_error.release_error();
  107. auto file = file_or_error.release_value();
  108. auto allowlist = TRY(Core::InputBufferedFile::create(move(file)));
  109. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  110. Vector<String> origins;
  111. while (TRY(allowlist->can_read_line())) {
  112. auto line = TRY(allowlist->read_line(buffer));
  113. if (line.is_empty())
  114. continue;
  115. auto domain = TRY(String::from_utf8(line));
  116. TRY(origins.try_append(move(domain)));
  117. }
  118. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  119. TRY(autoplay_allowlist.enable_for_origins(origins));
  120. return {};
  121. }