main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 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. arguments.append("--enable-autoplay");
  44. if (force_cpu_painting)
  45. arguments.append("--force-cpu-painting");
  46. arguments.append("about:blank");
  47. return launch_process("Ladybird"sv, arguments.span());
  48. }
  49. static ErrorOr<pid_t> launch_headless_browser(ByteString const& socket_path)
  50. {
  51. auto resources = ByteString::formatted("{}/res", s_ladybird_resource_root);
  52. return launch_process("headless-browser"sv,
  53. Array {
  54. "--resources",
  55. resources.characters(),
  56. "--webdriver-content-path",
  57. socket_path.characters(),
  58. "about:blank",
  59. });
  60. }
  61. ErrorOr<int> serenity_main(Main::Arguments arguments)
  62. {
  63. AK::set_rich_debug_enabled(true);
  64. auto listen_address = "0.0.0.0"sv;
  65. int port = 8000;
  66. bool force_cpu_painting = false;
  67. Core::ArgsParser args_parser;
  68. args_parser.add_option(listen_address, "IP address to listen on", "listen-address", 'l', "listen_address");
  69. args_parser.add_option(port, "Port to listen on", "port", 'p', "port");
  70. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  71. args_parser.add_option(force_cpu_painting, "Launch browser with GPU painting disabled", "force-cpu-painting");
  72. args_parser.parse(arguments);
  73. auto ipv4_address = IPv4Address::from_string(listen_address);
  74. if (!ipv4_address.has_value()) {
  75. warnln("Invalid listen address: {}", listen_address);
  76. return 1;
  77. }
  78. if ((u16)port != port) {
  79. warnln("Invalid port number: {}", port);
  80. return 1;
  81. }
  82. platform_init();
  83. auto webdriver_socket_path = ByteString::formatted("{}/webdriver", TRY(Core::StandardPaths::runtime_directory()));
  84. TRY(Core::Directory::create(webdriver_socket_path, Core::Directory::CreateDirectories::Yes));
  85. Core::EventLoop loop;
  86. auto server = TRY(Core::TCPServer::try_create());
  87. // FIXME: Propagate errors
  88. server->on_ready_to_accept = [&] {
  89. auto maybe_client_socket = server->accept();
  90. if (maybe_client_socket.is_error()) {
  91. warnln("Failed to accept the client: {}", maybe_client_socket.error());
  92. return;
  93. }
  94. auto maybe_buffered_socket = Core::BufferedTCPSocket::create(maybe_client_socket.release_value());
  95. if (maybe_buffered_socket.is_error()) {
  96. warnln("Could not obtain a buffered socket for the client: {}", maybe_buffered_socket.error());
  97. return;
  98. }
  99. auto launch_browser_callback = [&](ByteString const& socket_path) {
  100. return launch_browser(socket_path, force_cpu_painting);
  101. };
  102. auto maybe_client = WebDriver::Client::try_create(maybe_buffered_socket.release_value(), { move(launch_browser_callback), launch_headless_browser }, server);
  103. if (maybe_client.is_error()) {
  104. warnln("Could not create a WebDriver client: {}", maybe_client.error());
  105. return;
  106. }
  107. };
  108. TRY(server->listen(ipv4_address.value(), port, Core::TCPServer::AllowAddressReuse::Yes));
  109. outln("Listening on {}:{}", ipv4_address.value(), port);
  110. return loop.exec();
  111. }