main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "../EventLoopImplementationQt.h"
  7. #include "../EventLoopPluginQt.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 <LibCore/ArgsParser.h>
  15. #include <LibCore/EventLoop.h>
  16. #include <LibCore/LocalServer.h>
  17. #include <LibCore/System.h>
  18. #include <LibCore/SystemServerTakeover.h>
  19. #include <LibIPC/ConnectionFromClient.h>
  20. #include <LibMain/Main.h>
  21. #include <LibWeb/Bindings/MainThreadVM.h>
  22. #include <LibWeb/Loader/ContentFilter.h>
  23. #include <LibWeb/Loader/FrameLoader.h>
  24. #include <LibWeb/Loader/ResourceLoader.h>
  25. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  26. #include <LibWeb/WebSockets/WebSocket.h>
  27. #include <QGuiApplication>
  28. #include <QSocketNotifier>
  29. #include <QTimer>
  30. #include <WebContent/ConnectionFromClient.h>
  31. #include <WebContent/PageHost.h>
  32. #include <WebContent/WebDriverConnection.h>
  33. static ErrorOr<void> load_content_filters();
  34. static ErrorOr<void> load_autoplay_allowlist();
  35. extern DeprecatedString s_serenity_resource_root;
  36. struct DeferredInvokerQt final : IPC::DeferredInvoker {
  37. virtual ~DeferredInvokerQt() = default;
  38. virtual void schedule(Function<void()> callback) override
  39. {
  40. QTimer::singleShot(0, move(callback));
  41. }
  42. };
  43. template<typename ClientType>
  44. static void proxy_socket_through_notifier(ClientType& client, QSocketNotifier& notifier)
  45. {
  46. notifier.setSocket(client.socket().fd().value());
  47. notifier.setEnabled(true);
  48. QObject::connect(&notifier, &QSocketNotifier::activated, [&client]() mutable {
  49. client.socket().notifier()->on_activation();
  50. });
  51. client.set_deferred_invoker(make<DeferredInvokerQt>());
  52. }
  53. ErrorOr<int> serenity_main(Main::Arguments arguments)
  54. {
  55. QGuiApplication app(arguments.argc, arguments.argv);
  56. Core::EventLoop::make_implementation = Ladybird::EventLoopImplementationQt::create;
  57. Core::EventLoop event_loop;
  58. platform_init();
  59. Web::Platform::EventLoopPlugin::install(*new Ladybird::EventLoopPluginQt);
  60. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPluginLadybird);
  61. Web::ResourceLoader::initialize(RequestManagerQt::create());
  62. Web::WebSockets::WebSocketClientManager::initialize(Ladybird::WebSocketClientManagerLadybird::create());
  63. Web::FrameLoader::set_default_favicon_path(DeprecatedString::formatted("{}/res/icons/16x16/app-browser.png", s_serenity_resource_root));
  64. Web::Platform::FontPlugin::install(*new Ladybird::FontPluginQt);
  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. int webcontent_fd_passing_socket { -1 };
  74. Core::ArgsParser args_parser;
  75. 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");
  76. args_parser.parse(arguments);
  77. VERIFY(webcontent_fd_passing_socket >= 0);
  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. QSocketNotifier webcontent_notifier(QSocketNotifier::Type::Read);
  82. proxy_socket_through_notifier(*webcontent_client, webcontent_notifier);
  83. QSocketNotifier webdriver_notifier(QSocketNotifier::Type::Read);
  84. webcontent_client->page_host().on_webdriver_connection = [&](auto& webdriver) {
  85. proxy_socket_through_notifier(webdriver, webdriver_notifier);
  86. };
  87. return event_loop.exec();
  88. }
  89. static ErrorOr<void> load_content_filters()
  90. {
  91. auto file_or_error = Core::File::open(DeprecatedString::formatted("{}/home/anon/.config/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  92. if (file_or_error.is_error())
  93. file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  94. if (file_or_error.is_error())
  95. return file_or_error.release_error();
  96. auto file = file_or_error.release_value();
  97. auto ad_filter_list = TRY(Core::BufferedFile::create(move(file)));
  98. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  99. Vector<String> patterns;
  100. while (TRY(ad_filter_list->can_read_line())) {
  101. auto line = TRY(ad_filter_list->read_line(buffer));
  102. if (line.is_empty())
  103. continue;
  104. auto pattern = TRY(String::from_utf8(line));
  105. TRY(patterns.try_append(move(pattern)));
  106. }
  107. auto& content_filter = Web::ContentFilter::the();
  108. TRY(content_filter.set_patterns(patterns));
  109. return {};
  110. }
  111. static ErrorOr<void> load_autoplay_allowlist()
  112. {
  113. auto file_or_error = Core::File::open(TRY(String::formatted("{}/home/anon/.config/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  114. if (file_or_error.is_error())
  115. file_or_error = Core::File::open(TRY(String::formatted("{}/res/ladybird/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  116. if (file_or_error.is_error())
  117. return file_or_error.release_error();
  118. auto file = file_or_error.release_value();
  119. auto allowlist = TRY(Core::BufferedFile::create(move(file)));
  120. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  121. Vector<String> origins;
  122. while (TRY(allowlist->can_read_line())) {
  123. auto line = TRY(allowlist->read_line(buffer));
  124. if (line.is_empty())
  125. continue;
  126. auto domain = TRY(String::from_utf8(line));
  127. TRY(origins.try_append(move(domain)));
  128. }
  129. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  130. TRY(autoplay_allowlist.enable_for_origins(origins));
  131. return {};
  132. }