Serenity Operating System
at master 129 lines 5.3 kB view raw
1/* 2 * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#define AK_DONT_REPLACE_STD 8 9#include "../EventLoopPluginQt.h" 10#include "../FontPluginQt.h" 11#include "../ImageCodecPluginLadybird.h" 12#include "../RequestManagerQt.h" 13#include "../Utilities.h" 14#include "../WebSocketClientManagerLadybird.h" 15#include <AK/LexicalPath.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 <LibMain/Main.h> 23#include <LibWeb/Loader/ContentFilter.h> 24#include <LibWeb/Loader/FrameLoader.h> 25#include <LibWeb/Loader/ResourceLoader.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 34static ErrorOr<void> load_content_filters(); 35 36extern DeprecatedString s_serenity_resource_root; 37 38struct DeferredInvokerQt final : IPC::DeferredInvoker { 39 virtual ~DeferredInvokerQt() = default; 40 virtual void schedule(Function<void()> callback) override 41 { 42 QTimer::singleShot(0, move(callback)); 43 } 44}; 45 46template<typename ClientType> 47static void proxy_socket_through_notifier(ClientType& client, QSocketNotifier& notifier) 48{ 49 notifier.setSocket(client.socket().fd().value()); 50 notifier.setEnabled(true); 51 52 QObject::connect(&notifier, &QSocketNotifier::activated, [&client]() mutable { 53 client.socket().notifier()->on_ready_to_read(); 54 }); 55 56 client.set_deferred_invoker(make<DeferredInvokerQt>()); 57} 58 59ErrorOr<int> serenity_main(Main::Arguments arguments) 60{ 61 // NOTE: This is only used for the Core::Socket inside the IPC connection. 62 // FIXME: Refactor things so we can get rid of this somehow. 63 Core::EventLoop event_loop; 64 65 QGuiApplication app(arguments.argc, arguments.argv); 66 67 platform_init(); 68 69 Web::Platform::EventLoopPlugin::install(*new Ladybird::EventLoopPluginQt); 70 Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPluginLadybird); 71 72 Web::ResourceLoader::initialize(RequestManagerQt::create()); 73 Web::WebSockets::WebSocketClientManager::initialize(Ladybird::WebSocketClientManagerLadybird::create()); 74 75 Web::FrameLoader::set_default_favicon_path(DeprecatedString::formatted("{}/res/icons/16x16/app-browser.png", s_serenity_resource_root)); 76 77 Web::Platform::FontPlugin::install(*new Ladybird::FontPluginQt); 78 79 Web::FrameLoader::set_error_page_url(DeprecatedString::formatted("file://{}/res/html/error.html", s_serenity_resource_root)); 80 81 auto maybe_content_filter_error = load_content_filters(); 82 if (maybe_content_filter_error.is_error()) 83 dbgln("Failed to load content filters: {}", maybe_content_filter_error.error()); 84 85 int webcontent_fd_passing_socket { -1 }; 86 DeprecatedString webdriver_content_ipc_path; 87 88 Core::ArgsParser args_parser; 89 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"); 90 args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path"); 91 args_parser.parse(arguments); 92 93 VERIFY(webcontent_fd_passing_socket >= 0); 94 95 auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv)); 96 auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket))); 97 webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(webcontent_fd_passing_socket))); 98 99 QSocketNotifier webcontent_notifier(QSocketNotifier::Type::Read); 100 proxy_socket_through_notifier(*webcontent_client, webcontent_notifier); 101 102 QSocketNotifier webdriver_notifier(QSocketNotifier::Type::Read); 103 RefPtr<WebContent::WebDriverConnection> webdriver_client; 104 if (!webdriver_content_ipc_path.is_empty()) { 105 webdriver_client = TRY(WebContent::WebDriverConnection::connect(webcontent_client->page_host(), webdriver_content_ipc_path)); 106 proxy_socket_through_notifier(*webdriver_client, webdriver_notifier); 107 } 108 109 return app.exec(); 110} 111 112static ErrorOr<void> load_content_filters() 113{ 114 auto file_or_error = Core::File::open(DeprecatedString::formatted("{}/home/anon/.config/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read); 115 if (file_or_error.is_error()) 116 file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read); 117 if (file_or_error.is_error()) 118 return file_or_error.release_error(); 119 auto file = file_or_error.release_value(); 120 auto ad_filter_list = TRY(Core::BufferedFile::create(move(file))); 121 auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); 122 while (TRY(ad_filter_list->can_read_line())) { 123 auto line = TRY(ad_filter_list->read_line(buffer)); 124 if (!line.is_empty()) { 125 Web::ContentFilter::the().add_pattern(line); 126 } 127 } 128 return {}; 129}