main.cpp 6.6 KB

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