main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
  3. * Copyright (c) 2023-2024, Tim Flynn <trflynn89@ladybird.org>
  4. * Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
  5. * Copyright (c) 2023-2024, Sam Atkins <sam@ladybird.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/ByteString.h>
  10. #include <AK/LexicalPath.h>
  11. #include <AK/Platform.h>
  12. #include <AK/String.h>
  13. #include <Ladybird/Headless/Application.h>
  14. #include <Ladybird/Headless/HeadlessWebView.h>
  15. #include <Ladybird/Headless/Test.h>
  16. #include <Ladybird/Utilities.h>
  17. #include <LibCore/EventLoop.h>
  18. #include <LibCore/File.h>
  19. #include <LibCore/Promise.h>
  20. #include <LibCore/ResourceImplementationFile.h>
  21. #include <LibCore/Timer.h>
  22. #include <LibFileSystem/FileSystem.h>
  23. #include <LibGfx/Bitmap.h>
  24. #include <LibGfx/ImageFormats/PNGWriter.h>
  25. #include <LibGfx/SystemTheme.h>
  26. #include <LibURL/URL.h>
  27. static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, Ladybird::HeadlessWebView& view, URL::URL const& url, int screenshot_timeout)
  28. {
  29. // FIXME: Allow passing the output path as an argument.
  30. static constexpr auto output_file_path = "output.png"sv;
  31. if (FileSystem::exists(output_file_path))
  32. TRY(FileSystem::remove(output_file_path, FileSystem::RecursionMode::Disallowed));
  33. outln("Taking screenshot after {} seconds", screenshot_timeout);
  34. auto timer = Core::Timer::create_single_shot(
  35. screenshot_timeout * 1000,
  36. [&]() {
  37. auto promise = view.take_screenshot();
  38. if (auto screenshot = MUST(promise->await())) {
  39. outln("Saving screenshot to {}", output_file_path);
  40. auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write));
  41. auto image_buffer = MUST(Gfx::PNGWriter::encode(*screenshot));
  42. MUST(output_file->write_until_depleted(image_buffer.bytes()));
  43. } else {
  44. warnln("No screenshot available");
  45. }
  46. event_loop.quit(0);
  47. });
  48. view.load(url);
  49. timer->start();
  50. return timer;
  51. }
  52. ErrorOr<int> serenity_main(Main::Arguments arguments)
  53. {
  54. platform_init();
  55. auto app = Ladybird::Application::create(arguments, "about:newtab"sv);
  56. TRY(app->launch_services());
  57. Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(MUST(String::from_byte_string(app->resources_folder))));
  58. auto theme_path = LexicalPath::join(app->resources_folder, "themes"sv, "Default.ini"sv);
  59. auto theme = TRY(Gfx::load_system_theme(theme_path.string()));
  60. // FIXME: Allow passing the window size as an argument.
  61. static constexpr Gfx::IntSize window_size { 800, 600 };
  62. if (!app->test_root_path.is_empty()) {
  63. app->test_root_path = LexicalPath::absolute_path(TRY(FileSystem::current_working_directory()), app->test_root_path);
  64. TRY(Ladybird::run_tests(theme, window_size));
  65. return 0;
  66. }
  67. auto& view = app->create_web_view(move(theme), window_size);
  68. VERIFY(!WebView::Application::chrome_options().urls.is_empty());
  69. auto const& url = WebView::Application::chrome_options().urls.first();
  70. if (!url.is_valid()) {
  71. warnln("Invalid URL: \"{}\"", url);
  72. return Error::from_string_literal("Invalid URL");
  73. }
  74. if (app->dump_layout_tree || app->dump_text) {
  75. Ladybird::Test test { app->dump_layout_tree ? Ladybird::TestMode::Layout : Ladybird::TestMode::Text };
  76. Ladybird::run_dump_test(view, test, url);
  77. auto completion = MUST(view.test_promise().await());
  78. return completion.result == Ladybird::TestResult::Pass ? 0 : 1;
  79. }
  80. RefPtr<Core::Timer> timer;
  81. if (!WebView::Application::chrome_options().webdriver_content_ipc_path.has_value())
  82. timer = TRY(load_page_for_screenshot_and_exit(Core::EventLoop::current(), view, url, app->screenshot_timeout));
  83. return app->execute();
  84. }