Serenity Operating System
at master 254 lines 13 kB view raw
1/* 2 * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com> 3 * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#include <AK/Platform.h> 9 10#if !defined(AK_OS_SERENITY) 11# define AK_DONT_REPLACE_STD 12#endif 13 14#include <AK/Badge.h> 15#include <AK/DeprecatedString.h> 16#include <AK/Function.h> 17#include <AK/LexicalPath.h> 18#include <AK/NonnullOwnPtr.h> 19#include <AK/String.h> 20#include <AK/URL.h> 21#include <AK/Vector.h> 22#include <LibCore/ArgsParser.h> 23#include <LibCore/DeprecatedFile.h> 24#include <LibCore/EventLoop.h> 25#include <LibCore/File.h> 26#include <LibCore/Timer.h> 27#include <LibGfx/Bitmap.h> 28#include <LibGfx/Font/FontDatabase.h> 29#include <LibGfx/PNGWriter.h> 30#include <LibGfx/Point.h> 31#include <LibGfx/Rect.h> 32#include <LibGfx/ShareableBitmap.h> 33#include <LibGfx/Size.h> 34#include <LibGfx/StandardCursor.h> 35#include <LibGfx/SystemTheme.h> 36#include <LibIPC/File.h> 37#include <LibWeb/Cookie/Cookie.h> 38#include <LibWeb/Cookie/ParsedCookie.h> 39#include <LibWeb/Loader/FrameLoader.h> 40#include <LibWebView/ViewImplementation.h> 41#include <LibWebView/WebContentClient.h> 42 43#if !defined(AK_OS_SERENITY) 44# include <Ladybird/HelperProcess.h> 45# include <QCoreApplication> 46#endif 47 48class HeadlessWebContentView final : public WebView::ViewImplementation { 49public: 50 static ErrorOr<NonnullOwnPtr<HeadlessWebContentView>> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, StringView web_driver_ipc_path) 51 { 52 auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView())); 53 54#if defined(AK_OS_SERENITY) 55 view->m_client_state.client = TRY(WebView::WebContentClient::try_create(*view)); 56 57 if (!web_driver_ipc_path.is_empty()) 58 view->client().async_connect_to_webdriver(web_driver_ipc_path); 59#else 60 auto candidate_web_content_paths = TRY(get_paths_for_helper_process("WebContent"sv)); 61 view->m_client_state.client = TRY(view->launch_web_content_process(candidate_web_content_paths, web_driver_ipc_path)); 62#endif 63 64 view->client().async_update_system_theme(move(theme)); 65 view->client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query()); 66 67 view->client().async_set_viewport_rect({ { 0, 0 }, window_size }); 68 view->client().async_set_window_size(window_size); 69 70 return view; 71 } 72 73 RefPtr<Gfx::Bitmap> take_screenshot() 74 { 75 return client().take_document_screenshot().bitmap(); 76 } 77 78 ErrorOr<String> dump_layout_tree() 79 { 80 return String::from_deprecated_string(client().dump_layout_tree()); 81 } 82 83 Function<void(const URL&)> on_load_finish; 84 85private: 86 HeadlessWebContentView() = default; 87 88 void notify_server_did_layout(Badge<WebView::WebContentClient>, Gfx::IntSize) override { } 89 void notify_server_did_paint(Badge<WebView::WebContentClient>, i32) override { } 90 void notify_server_did_invalidate_content_rect(Badge<WebView::WebContentClient>, Gfx::IntRect const&) override { } 91 void notify_server_did_change_selection(Badge<WebView::WebContentClient>) override { } 92 void notify_server_did_request_cursor_change(Badge<WebView::WebContentClient>, Gfx::StandardCursor) override { } 93 void notify_server_did_change_title(Badge<WebView::WebContentClient>, DeprecatedString const&) override { } 94 void notify_server_did_request_scroll(Badge<WebView::WebContentClient>, i32, i32) override { } 95 void notify_server_did_request_scroll_to(Badge<WebView::WebContentClient>, Gfx::IntPoint) override { } 96 void notify_server_did_request_scroll_into_view(Badge<WebView::WebContentClient>, Gfx::IntRect const&) override { } 97 void notify_server_did_enter_tooltip_area(Badge<WebView::WebContentClient>, Gfx::IntPoint, DeprecatedString const&) override { } 98 void notify_server_did_leave_tooltip_area(Badge<WebView::WebContentClient>) override { } 99 void notify_server_did_hover_link(Badge<WebView::WebContentClient>, const URL&) override { } 100 void notify_server_did_unhover_link(Badge<WebView::WebContentClient>) override { } 101 void notify_server_did_click_link(Badge<WebView::WebContentClient>, const URL&, DeprecatedString const&, unsigned) override { } 102 void notify_server_did_middle_click_link(Badge<WebView::WebContentClient>, const URL&, DeprecatedString const&, unsigned) override { } 103 void notify_server_did_start_loading(Badge<WebView::WebContentClient>, const URL&, bool) override { } 104 105 void notify_server_did_finish_loading(Badge<WebView::WebContentClient>, const URL& url) override 106 { 107 if (on_load_finish) 108 on_load_finish(url); 109 } 110 111 void notify_server_did_request_navigate_back(Badge<WebView::WebContentClient>) override { } 112 void notify_server_did_request_navigate_forward(Badge<WebView::WebContentClient>) override { } 113 void notify_server_did_request_refresh(Badge<WebView::WebContentClient>) override { } 114 void notify_server_did_request_context_menu(Badge<WebView::WebContentClient>, Gfx::IntPoint) override { } 115 void notify_server_did_request_link_context_menu(Badge<WebView::WebContentClient>, Gfx::IntPoint, const URL&, DeprecatedString const&, unsigned) override { } 116 void notify_server_did_request_image_context_menu(Badge<WebView::WebContentClient>, Gfx::IntPoint, const URL&, DeprecatedString const&, unsigned, Gfx::ShareableBitmap const&) override { } 117 void notify_server_did_request_alert(Badge<WebView::WebContentClient>, DeprecatedString const&) override { } 118 void notify_server_did_request_confirm(Badge<WebView::WebContentClient>, DeprecatedString const&) override { } 119 void notify_server_did_request_prompt(Badge<WebView::WebContentClient>, DeprecatedString const&, DeprecatedString const&) override { } 120 void notify_server_did_request_set_prompt_text(Badge<WebView::WebContentClient>, DeprecatedString const&) override { } 121 void notify_server_did_request_accept_dialog(Badge<WebView::WebContentClient>) override { } 122 void notify_server_did_request_dismiss_dialog(Badge<WebView::WebContentClient>) override { } 123 void notify_server_did_get_source(const URL&, DeprecatedString const&) override { } 124 void notify_server_did_get_dom_tree(DeprecatedString const&) override { } 125 void notify_server_did_get_dom_node_properties(i32, DeprecatedString const&, DeprecatedString const&, DeprecatedString const&, DeprecatedString const&) override { } 126 void notify_server_did_get_accessibility_tree(DeprecatedString const&) override { } 127 void notify_server_did_output_js_console_message(i32) override { } 128 void notify_server_did_get_js_console_messages(i32, Vector<DeprecatedString> const&, Vector<DeprecatedString> const&) override { } 129 void notify_server_did_change_favicon(Gfx::Bitmap const&) override { } 130 Vector<Web::Cookie::Cookie> notify_server_did_request_all_cookies(Badge<WebView::WebContentClient>, URL const&) override { return {}; } 131 Optional<Web::Cookie::Cookie> notify_server_did_request_named_cookie(Badge<WebView::WebContentClient>, URL const&, DeprecatedString const&) override { return {}; } 132 DeprecatedString notify_server_did_request_cookie(Badge<WebView::WebContentClient>, const URL&, Web::Cookie::Source) override { return {}; } 133 void notify_server_did_set_cookie(Badge<WebView::WebContentClient>, const URL&, Web::Cookie::ParsedCookie const&, Web::Cookie::Source) override { } 134 void notify_server_did_update_cookie(Badge<WebView::WebContentClient>, Web::Cookie::Cookie const&) override { } 135 void notify_server_did_close_browsing_context(Badge<WebView::WebContentClient>) override { } 136 void notify_server_did_update_resource_count(i32) override { } 137 void notify_server_did_request_restore_window() override { } 138 Gfx::IntPoint notify_server_did_request_reposition_window(Gfx::IntPoint) override { return {}; } 139 Gfx::IntSize notify_server_did_request_resize_window(Gfx::IntSize) override { return {}; } 140 Gfx::IntRect notify_server_did_request_maximize_window() override { return {}; } 141 Gfx::IntRect notify_server_did_request_minimize_window() override { return {}; } 142 Gfx::IntRect notify_server_did_request_fullscreen_window() override { return {}; } 143 144 void notify_server_did_request_file(Badge<WebView::WebContentClient>, DeprecatedString const& path, i32 request_id) override 145 { 146 auto file = Core::File::open(path, Core::File::OpenMode::Read); 147 148 if (file.is_error()) 149 client().async_handle_file_return(file.error().code(), {}, request_id); 150 else 151 client().async_handle_file_return(0, IPC::File(*file.value()), request_id); 152 } 153 154 void notify_server_did_finish_handling_input_event(bool) override { } 155 void update_zoom() override { } 156 void create_client() override { } 157}; 158 159static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, HeadlessWebContentView& view, int screenshot_timeout) 160{ 161 // FIXME: Allow passing the output path as an argument. 162 static constexpr auto output_file_path = "output.png"sv; 163 164 if (Core::DeprecatedFile::exists(output_file_path)) 165 TRY(Core::DeprecatedFile::remove(output_file_path, Core::DeprecatedFile::RecursionMode::Disallowed)); 166 167 outln("Taking screenshot after {} seconds", screenshot_timeout); 168 169 auto timer = TRY(Core::Timer::create_single_shot( 170 screenshot_timeout * 1000, 171 [&]() { 172 if (auto screenshot = view.take_screenshot()) { 173 outln("Saving screenshot to {}", output_file_path); 174 175 auto output_file = MUST(Core::File::open(output_file_path, Core::File::OpenMode::Write)); 176 auto image_buffer = MUST(Gfx::PNGWriter::encode(*screenshot)); 177 MUST(output_file->write_until_depleted(image_buffer.bytes())); 178 } else { 179 warnln("No screenshot available"); 180 } 181 182 event_loop.quit(0); 183 })); 184 185 timer->start(); 186 return timer; 187} 188 189static ErrorOr<URL> format_url(StringView url) 190{ 191 if (Core::DeprecatedFile::exists(url)) 192 return URL::create_with_file_scheme(Core::DeprecatedFile::real_path_for(url)); 193 194 URL formatted_url { url }; 195 if (!formatted_url.is_valid()) 196 formatted_url = TRY(String::formatted("http://{}", url)); 197 198 return formatted_url; 199} 200 201ErrorOr<int> serenity_main(Main::Arguments arguments) 202{ 203#if !defined(AK_OS_SERENITY) 204 QCoreApplication app(arguments.argc, arguments.argv); 205#endif 206 Core::EventLoop event_loop; 207 208 int screenshot_timeout = 1; 209 StringView url; 210 auto resources_folder = "/res"sv; 211 StringView web_driver_ipc_path; 212 bool dump_layout_tree = false; 213 214 Core::ArgsParser args_parser; 215 args_parser.set_general_help("This utility runs the Browser in headless mode."); 216 args_parser.add_option(screenshot_timeout, "Take a screenshot after [n] seconds (default: 1)", "screenshot", 's', "n"); 217 args_parser.add_option(dump_layout_tree, "Dump layout tree and exit", "dump-layout-tree", 'd'); 218 args_parser.add_option(resources_folder, "Path of the base resources folder (defaults to /res)", "resources", 'r', "resources-root-path"); 219 args_parser.add_option(web_driver_ipc_path, "Path to the WebDriver IPC socket", "webdriver-ipc-path", 0, "path"); 220 args_parser.add_positional_argument(url, "URL to open", "url", Core::ArgsParser::Required::Yes); 221 args_parser.parse(arguments); 222 223 Gfx::FontDatabase::set_default_font_query("Katica 10 400 0"); 224 Gfx::FontDatabase::set_window_title_font_query("Katica 10 700 0"); 225 Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0"); 226 227 auto fonts_path = LexicalPath::join(resources_folder, "fonts"sv); 228 Gfx::FontDatabase::set_default_fonts_lookup_path(fonts_path.string()); 229 230 auto theme_path = LexicalPath::join(resources_folder, "themes"sv, "Default.ini"sv); 231 auto theme = TRY(Gfx::load_system_theme(theme_path.string())); 232 233 // FIXME: Allow passing the window size as an argument. 234 static constexpr Gfx::IntSize window_size { 800, 600 }; 235 236 auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, web_driver_ipc_path)); 237 RefPtr<Core::Timer> timer; 238 239 if (dump_layout_tree) { 240 view->on_load_finish = [&](auto const&) { 241 auto layout_tree = view->dump_layout_tree().release_value_but_fixme_should_propagate_errors(); 242 243 outln("{}", layout_tree); 244 fflush(stdout); 245 246 event_loop.quit(0); 247 }; 248 } else if (web_driver_ipc_path.is_empty()) { 249 timer = TRY(load_page_for_screenshot_and_exit(event_loop, *view, screenshot_timeout)); 250 } 251 252 view->load(TRY(format_url(url))); 253 return event_loop.exec(); 254}