main.cpp 7.2 KB

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