main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Platform.h>
  7. #include <Ladybird/Utilities.h>
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibCore/Directory.h>
  10. #include <LibCore/EventLoop.h>
  11. #include <LibCore/Process.h>
  12. #include <LibCore/StandardPaths.h>
  13. #include <LibCore/System.h>
  14. #include <LibCore/TCPServer.h>
  15. #include <LibMain/Main.h>
  16. #include <WebDriver/Client.h>
  17. static Vector<ByteString> certificates;
  18. static ErrorOr<pid_t> launch_process(StringView application, ReadonlySpan<char const*> arguments)
  19. {
  20. auto paths = TRY(get_paths_for_helper_process(application));
  21. ErrorOr<pid_t> result = -1;
  22. for (auto const& path : paths) {
  23. auto path_view = path.view();
  24. result = Core::Process::spawn(path_view, arguments, {}, Core::Process::KeepAsChild::Yes);
  25. if (!result.is_error())
  26. break;
  27. }
  28. return result;
  29. }
  30. static ErrorOr<pid_t> launch_browser(ByteString const& socket_path, bool use_qt_networking, bool force_cpu_painting)
  31. {
  32. auto arguments = Vector {
  33. "--webdriver-content-path",
  34. socket_path.characters(),
  35. };
  36. Vector<ByteString> certificate_args;
  37. for (auto const& certificate : certificates) {
  38. certificate_args.append(ByteString::formatted("--certificate={}", certificate));
  39. arguments.append(certificate_args.last().view().characters_without_null_termination());
  40. }
  41. arguments.append("--allow-popups");
  42. arguments.append("--force-new-process");
  43. if (use_qt_networking)
  44. arguments.append("--enable-qt-networking");
  45. if (force_cpu_painting)
  46. arguments.append("--force-cpu-painting");
  47. arguments.append("about:blank");
  48. return launch_process("Ladybird"sv, arguments.span());
  49. }
  50. static ErrorOr<pid_t> launch_headless_browser(ByteString const& socket_path)
  51. {
  52. auto resources = ByteString::formatted("{}/res", s_ladybird_resource_root);
  53. return launch_process("headless-browser"sv,
  54. Array {
  55. "--resources",
  56. resources.characters(),
  57. "--webdriver-content-path",
  58. socket_path.characters(),
  59. "about:blank",
  60. });
  61. }
  62. ErrorOr<int> serenity_main(Main::Arguments arguments)
  63. {
  64. AK::set_rich_debug_enabled(true);
  65. auto listen_address = "0.0.0.0"sv;
  66. int port = 8000;
  67. bool enable_qt_networking = false;
  68. bool force_cpu_painting = false;
  69. Core::ArgsParser args_parser;
  70. args_parser.add_option(listen_address, "IP address to listen on", "listen-address", 'l', "listen_address");
  71. args_parser.add_option(port, "Port to listen on", "port", 'p', "port");
  72. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  73. args_parser.add_option(enable_qt_networking, "Launch browser with Qt networking enabled", "enable-qt-networking");
  74. args_parser.add_option(force_cpu_painting, "Launch browser with GPU painting disabled", "force-cpu-painting");
  75. args_parser.parse(arguments);
  76. auto ipv4_address = IPv4Address::from_string(listen_address);
  77. if (!ipv4_address.has_value()) {
  78. warnln("Invalid listen address: {}", listen_address);
  79. return 1;
  80. }
  81. if ((u16)port != port) {
  82. warnln("Invalid port number: {}", port);
  83. return 1;
  84. }
  85. platform_init();
  86. auto webdriver_socket_path = ByteString::formatted("{}/webdriver", TRY(Core::StandardPaths::runtime_directory()));
  87. TRY(Core::Directory::create(webdriver_socket_path, Core::Directory::CreateDirectories::Yes));
  88. Core::EventLoop loop;
  89. auto server = TRY(Core::TCPServer::try_create());
  90. // FIXME: Propagate errors
  91. server->on_ready_to_accept = [&] {
  92. auto maybe_client_socket = server->accept();
  93. if (maybe_client_socket.is_error()) {
  94. warnln("Failed to accept the client: {}", maybe_client_socket.error());
  95. return;
  96. }
  97. auto maybe_buffered_socket = Core::BufferedTCPSocket::create(maybe_client_socket.release_value());
  98. if (maybe_buffered_socket.is_error()) {
  99. warnln("Could not obtain a buffered socket for the client: {}", maybe_buffered_socket.error());
  100. return;
  101. }
  102. auto launch_browser_callback = [&](ByteString const& socket_path) {
  103. return launch_browser(socket_path, enable_qt_networking, force_cpu_painting);
  104. };
  105. auto maybe_client = WebDriver::Client::try_create(maybe_buffered_socket.release_value(), { move(launch_browser_callback), launch_headless_browser }, server);
  106. if (maybe_client.is_error()) {
  107. warnln("Could not create a WebDriver client: {}", maybe_client.error());
  108. return;
  109. }
  110. };
  111. TRY(server->listen(ipv4_address.value(), port, Core::TCPServer::AllowAddressReuse::Yes));
  112. outln("Listening on {}:{}", ipv4_address.value(), port);
  113. return loop.exec();
  114. }