main.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "../AudioCodecPluginQt.h"
  7. #include "../EventLoopImplementationQt.h"
  8. #include "../FontPlugin.h"
  9. #include "../HelperProcess.h"
  10. #include "../ImageCodecPlugin.h"
  11. #include "../RequestManagerQt.h"
  12. #include "../Utilities.h"
  13. #include "../WebSocketClientManagerQt.h"
  14. #include <AK/LexicalPath.h>
  15. #include <LibAudio/Loader.h>
  16. #include <LibCore/ArgsParser.h>
  17. #include <LibCore/EventLoop.h>
  18. #include <LibCore/LocalServer.h>
  19. #include <LibCore/System.h>
  20. #include <LibCore/SystemServerTakeover.h>
  21. #include <LibIPC/ConnectionFromClient.h>
  22. #include <LibJS/Bytecode/Interpreter.h>
  23. #include <LibMain/Main.h>
  24. #include <LibWeb/Bindings/MainThreadVM.h>
  25. #include <LibWeb/HTML/Window.h>
  26. #include <LibWeb/Loader/ContentFilter.h>
  27. #include <LibWeb/Loader/FrameLoader.h>
  28. #include <LibWeb/Loader/ResourceLoader.h>
  29. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  30. #include <LibWeb/Platform/AudioCodecPluginAgnostic.h>
  31. #include <LibWeb/Platform/EventLoopPluginSerenity.h>
  32. #include <LibWeb/WebSockets/WebSocket.h>
  33. #include <LibWebView/RequestServerAdapter.h>
  34. #include <LibWebView/WebSocketClientAdapter.h>
  35. #include <QCoreApplication>
  36. #include <WebContent/ConnectionFromClient.h>
  37. #include <WebContent/PageHost.h>
  38. #include <WebContent/WebDriverConnection.h>
  39. static ErrorOr<void> load_content_filters();
  40. static ErrorOr<void> load_autoplay_allowlist();
  41. extern DeprecatedString s_serenity_resource_root;
  42. ErrorOr<int> serenity_main(Main::Arguments arguments)
  43. {
  44. QCoreApplication app(arguments.argc, arguments.argv);
  45. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  46. Core::EventLoop event_loop;
  47. platform_init();
  48. Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
  49. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPlugin);
  50. Web::Platform::AudioCodecPlugin::install_creation_hook([](auto loader) {
  51. #if defined(HAVE_PULSEAUDIO)
  52. return Web::Platform::AudioCodecPluginAgnostic::create(move(loader));
  53. #else
  54. return Ladybird::AudioCodecPluginQt::create(move(loader));
  55. #endif
  56. });
  57. Web::FrameLoader::set_default_favicon_path(DeprecatedString::formatted("{}/res/icons/16x16/app-browser.png", s_serenity_resource_root));
  58. int webcontent_fd_passing_socket { -1 };
  59. bool is_layout_test_mode = false;
  60. bool use_javascript_bytecode = false;
  61. bool use_lagom_networking = false;
  62. Core::ArgsParser args_parser;
  63. 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");
  64. args_parser.add_option(is_layout_test_mode, "Is layout test mode", "layout-test-mode", 0);
  65. args_parser.add_option(use_javascript_bytecode, "Enable JavaScript bytecode VM", "use-bytecode", 0);
  66. args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "use-lagom-networking", 0);
  67. args_parser.parse(arguments);
  68. if (use_lagom_networking) {
  69. auto candidate_request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
  70. auto request_server_client = TRY(launch_request_server_process(candidate_request_server_paths, s_serenity_resource_root));
  71. Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(request_server_client))));
  72. auto candidate_web_socket_paths = TRY(get_paths_for_helper_process("WebSocket"sv));
  73. auto web_socket_client = TRY(launch_web_socket_process(candidate_web_socket_paths, s_serenity_resource_root));
  74. Web::WebSockets::WebSocketClientManager::initialize(TRY(WebView::WebSocketClientManagerAdapter::try_create(move(web_socket_client))));
  75. } else {
  76. Web::ResourceLoader::initialize(Ladybird::RequestManagerQt::create());
  77. Web::WebSockets::WebSocketClientManager::initialize(Ladybird::WebSocketClientManagerQt::create());
  78. }
  79. Web::HTML::Window::set_internals_object_exposed(is_layout_test_mode);
  80. JS::Bytecode::Interpreter::set_enabled(use_javascript_bytecode);
  81. VERIFY(webcontent_fd_passing_socket >= 0);
  82. Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(is_layout_test_mode));
  83. Web::FrameLoader::set_error_page_url(DeprecatedString::formatted("file://{}/res/html/error.html", s_serenity_resource_root));
  84. TRY(Web::Bindings::initialize_main_thread_vm());
  85. auto maybe_content_filter_error = load_content_filters();
  86. if (maybe_content_filter_error.is_error())
  87. dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
  88. auto maybe_autoplay_allowlist_error = load_autoplay_allowlist();
  89. if (maybe_autoplay_allowlist_error.is_error())
  90. dbgln("Failed to load autoplay allowlist: {}", maybe_autoplay_allowlist_error.error());
  91. auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv));
  92. auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
  93. webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(webcontent_fd_passing_socket)));
  94. return event_loop.exec();
  95. }
  96. static ErrorOr<void> load_content_filters()
  97. {
  98. auto file_or_error = Core::File::open(DeprecatedString::formatted("{}/home/anon/.config/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  99. if (file_or_error.is_error())
  100. file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  101. if (file_or_error.is_error())
  102. return file_or_error.release_error();
  103. auto file = file_or_error.release_value();
  104. auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file)));
  105. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  106. Vector<String> patterns;
  107. while (TRY(ad_filter_list->can_read_line())) {
  108. auto line = TRY(ad_filter_list->read_line(buffer));
  109. if (line.is_empty())
  110. continue;
  111. auto pattern = TRY(String::from_utf8(line));
  112. TRY(patterns.try_append(move(pattern)));
  113. }
  114. auto& content_filter = Web::ContentFilter::the();
  115. TRY(content_filter.set_patterns(patterns));
  116. return {};
  117. }
  118. static ErrorOr<void> load_autoplay_allowlist()
  119. {
  120. auto file_or_error = Core::File::open(TRY(String::formatted("{}/home/anon/.config/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  121. if (file_or_error.is_error())
  122. file_or_error = Core::File::open(TRY(String::formatted("{}/res/ladybird/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  123. if (file_or_error.is_error())
  124. return file_or_error.release_error();
  125. auto file = file_or_error.release_value();
  126. auto allowlist = TRY(Core::InputBufferedFile::create(move(file)));
  127. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  128. Vector<String> origins;
  129. while (TRY(allowlist->can_read_line())) {
  130. auto line = TRY(allowlist->read_line(buffer));
  131. if (line.is_empty())
  132. continue;
  133. auto domain = TRY(String::from_utf8(line));
  134. TRY(origins.try_append(move(domain)));
  135. }
  136. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  137. TRY(autoplay_allowlist.enable_for_origins(origins));
  138. return {};
  139. }