main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #define AK_DONT_REPLACE_STD
  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/File.h>
  17. #include <LibCore/LocalServer.h>
  18. #include <LibCore/System.h>
  19. #include <LibCore/SystemServerTakeover.h>
  20. #include <LibIPC/ConnectionFromClient.h>
  21. #include <LibMain/Main.h>
  22. #include <LibWeb/Loader/ContentFilter.h>
  23. #include <LibWeb/Loader/FrameLoader.h>
  24. #include <LibWeb/Loader/ResourceLoader.h>
  25. #include <LibWeb/WebSockets/WebSocket.h>
  26. #include <QGuiApplication>
  27. #include <QSocketNotifier>
  28. #include <QTimer>
  29. #include <WebContent/ConnectionFromClient.h>
  30. #include <WebContent/WebDriverConnection.h>
  31. static ErrorOr<void> load_content_filters();
  32. extern String s_serenity_resource_root;
  33. struct DeferredInvokerQt final : IPC::DeferredInvoker {
  34. virtual ~DeferredInvokerQt() = default;
  35. virtual void schedule(Function<void()> callback) override
  36. {
  37. QTimer::singleShot(0, move(callback));
  38. }
  39. };
  40. template<typename ConnectionType, typename... Args>
  41. static ErrorOr<NonnullRefPtr<ConnectionType>> create_connection_from_passed_socket(int passing_socket_fd, StringView socket_name, QSocketNotifier& notifier, Args&&... args)
  42. {
  43. auto socket = TRY(Core::take_over_socket_from_system_server(socket_name));
  44. auto client = TRY(ConnectionType::try_create(move(socket), std::forward<Args>(args)...));
  45. VERIFY(passing_socket_fd >= 0);
  46. client->set_fd_passing_socket(TRY(Core::Stream::LocalSocket::adopt_fd(passing_socket_fd)));
  47. notifier.setSocket(client->socket().fd().value());
  48. notifier.setEnabled(true);
  49. QObject::connect(&notifier, &QSocketNotifier::activated, [client]() mutable {
  50. client->socket().notifier()->on_ready_to_read();
  51. });
  52. client->set_deferred_invoker(make<DeferredInvokerQt>());
  53. return client;
  54. }
  55. ErrorOr<int> serenity_main(Main::Arguments arguments)
  56. {
  57. // NOTE: This is only used for the Core::Socket inside the IPC connection.
  58. // FIXME: Refactor things so we can get rid of this somehow.
  59. Core::EventLoop event_loop;
  60. QGuiApplication app(arguments.argc, arguments.argv);
  61. platform_init();
  62. Web::Platform::EventLoopPlugin::install(*new Ladybird::EventLoopPluginQt);
  63. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPluginLadybird);
  64. Web::ResourceLoader::initialize(RequestManagerQt::create());
  65. Web::WebSockets::WebSocketClientManager::initialize(Ladybird::WebSocketClientManagerLadybird::create());
  66. Web::FrameLoader::set_default_favicon_path(String::formatted("{}/res/icons/16x16/app-browser.png", s_serenity_resource_root));
  67. Web::Platform::FontPlugin::install(*new Ladybird::FontPluginQt);
  68. Web::FrameLoader::set_error_page_url(String::formatted("file://{}/res/html/error.html", s_serenity_resource_root));
  69. auto maybe_content_filter_error = load_content_filters();
  70. if (maybe_content_filter_error.is_error())
  71. dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
  72. int webcontent_fd_passing_socket { -1 };
  73. int webdriver_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.add_option(webdriver_fd_passing_socket, "File descriptor of the passing socket for the WebDriver connection", "webdriver-fd-passing-socket", 'd', "webdriver_fd_passing_socket");
  77. args_parser.parse(arguments);
  78. QSocketNotifier webcontent_notifier(QSocketNotifier::Type::Read);
  79. auto webcontent_client = TRY(create_connection_from_passed_socket<WebContent::ConnectionFromClient>(webcontent_fd_passing_socket, "WebContent"sv, webcontent_notifier));
  80. QSocketNotifier webdriver_notifier(QSocketNotifier::Type::Read);
  81. RefPtr<WebContent::WebDriverConnection> webdriver_client;
  82. if (webdriver_fd_passing_socket >= 0)
  83. webdriver_client = TRY(create_connection_from_passed_socket<WebContent::WebDriverConnection>(webdriver_fd_passing_socket, "WebDriver"sv, webdriver_notifier, *webcontent_client, webcontent_client->page_host()));
  84. return app.exec();
  85. }
  86. static ErrorOr<void> load_content_filters()
  87. {
  88. auto file_or_error = Core::Stream::File::open(String::formatted("{}/home/anon/.config/BrowserContentFilters.txt", s_serenity_resource_root), Core::Stream::OpenMode::Read);
  89. if (file_or_error.is_error())
  90. file_or_error = Core::Stream::File::open(String::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::Stream::OpenMode::Read);
  91. if (file_or_error.is_error())
  92. return file_or_error.release_error();
  93. auto file = file_or_error.release_value();
  94. auto ad_filter_list = TRY(Core::Stream::BufferedFile::create(move(file)));
  95. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  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. Web::ContentFilter::the().add_pattern(line);
  100. }
  101. }
  102. return {};
  103. }