main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <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 <QGuiApplication>
  32. #include <QTimer>
  33. #include <WebContent/ConnectionFromClient.h>
  34. #include <WebContent/PageHost.h>
  35. #include <WebContent/WebDriverConnection.h>
  36. #if defined(AK_OS_MACOS)
  37. # include "MacOSSetup.h"
  38. #endif
  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. QGuiApplication app(arguments.argc, arguments.argv);
  45. #if defined(AK_OS_MACOS)
  46. prohibit_interaction();
  47. #endif
  48. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  49. Core::EventLoop event_loop;
  50. platform_init();
  51. Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
  52. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPluginLadybird);
  53. Web::Platform::AudioCodecPlugin::install_creation_hook([](auto loader) {
  54. return Ladybird::AudioCodecPluginLadybird::create(move(loader));
  55. });
  56. Web::ResourceLoader::initialize(RequestManagerQt::create());
  57. Web::WebSockets::WebSocketClientManager::initialize(Ladybird::WebSocketClientManagerLadybird::create());
  58. Web::FrameLoader::set_default_favicon_path(DeprecatedString::formatted("{}/res/icons/16x16/app-browser.png", s_serenity_resource_root));
  59. int webcontent_fd_passing_socket { -1 };
  60. bool is_layout_test_mode = false;
  61. bool use_javascript_bytecode = 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.parse(arguments);
  67. JS::Bytecode::Interpreter::set_enabled(use_javascript_bytecode);
  68. VERIFY(webcontent_fd_passing_socket >= 0);
  69. Web::Platform::FontPlugin::install(*new Ladybird::FontPluginQt(is_layout_test_mode));
  70. Web::FrameLoader::set_error_page_url(DeprecatedString::formatted("file://{}/res/html/error.html", s_serenity_resource_root));
  71. TRY(Web::Bindings::initialize_main_thread_vm());
  72. auto maybe_content_filter_error = load_content_filters();
  73. if (maybe_content_filter_error.is_error())
  74. dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
  75. auto maybe_autoplay_allowlist_error = load_autoplay_allowlist();
  76. if (maybe_autoplay_allowlist_error.is_error())
  77. dbgln("Failed to load autoplay allowlist: {}", maybe_autoplay_allowlist_error.error());
  78. auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv));
  79. auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
  80. webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(webcontent_fd_passing_socket)));
  81. return event_loop.exec();
  82. }
  83. static ErrorOr<void> load_content_filters()
  84. {
  85. auto file_or_error = Core::File::open(DeprecatedString::formatted("{}/home/anon/.config/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  86. if (file_or_error.is_error())
  87. file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  88. if (file_or_error.is_error())
  89. return file_or_error.release_error();
  90. auto file = file_or_error.release_value();
  91. auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file)));
  92. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  93. Vector<String> patterns;
  94. while (TRY(ad_filter_list->can_read_line())) {
  95. auto line = TRY(ad_filter_list->read_line(buffer));
  96. if (line.is_empty())
  97. continue;
  98. auto pattern = TRY(String::from_utf8(line));
  99. TRY(patterns.try_append(move(pattern)));
  100. }
  101. auto& content_filter = Web::ContentFilter::the();
  102. TRY(content_filter.set_patterns(patterns));
  103. return {};
  104. }
  105. static ErrorOr<void> load_autoplay_allowlist()
  106. {
  107. auto file_or_error = Core::File::open(TRY(String::formatted("{}/home/anon/.config/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  108. if (file_or_error.is_error())
  109. file_or_error = Core::File::open(TRY(String::formatted("{}/res/ladybird/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  110. if (file_or_error.is_error())
  111. return file_or_error.release_error();
  112. auto file = file_or_error.release_value();
  113. auto allowlist = TRY(Core::InputBufferedFile::create(move(file)));
  114. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  115. Vector<String> origins;
  116. while (TRY(allowlist->can_read_line())) {
  117. auto line = TRY(allowlist->read_line(buffer));
  118. if (line.is_empty())
  119. continue;
  120. auto domain = TRY(String::from_utf8(line));
  121. TRY(origins.try_append(move(domain)));
  122. }
  123. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  124. TRY(autoplay_allowlist.enable_for_origins(origins));
  125. return {};
  126. }