| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-01-30 23:43:18 +01:00
										 |  |  |  * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-09-03 11:14:37 +01:00
										 |  |  |  * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-17 18:39:00 +01:00
										 |  |  | #include <AK/Debug.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-07 16:35:10 +01:00
										 |  |  | #include <AK/JsonObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | #include <LibGfx/Bitmap.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-21 18:10:23 +02:00
										 |  |  | #include <LibGfx/FontDatabase.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | #include <LibGfx/SystemTheme.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-27 21:44:49 -06:00
										 |  |  | #include <LibJS/Console.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  | #include <LibJS/Heap/Heap.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-27 21:44:49 -06:00
										 |  |  | #include <LibJS/Interpreter.h>
 | 
					
						
							|  |  |  | #include <LibJS/Parser.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-03 10:41:07 +01:00
										 |  |  | #include <LibWeb/Bindings/MainThreadVM.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-15 10:36:20 -04:00
										 |  |  | #include <LibWeb/Cookie/ParsedCookie.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Dump.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/InitialContainingBlockBox.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-29 15:31:09 -04:00
										 |  |  | #include <LibWeb/Loader/ResourceLoader.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  | #include <LibWeb/Page/BrowsingContext.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | #include <WebContent/ClientConnection.h>
 | 
					
						
							|  |  |  | #include <WebContent/PageHost.h>
 | 
					
						
							|  |  |  | #include <WebContent/WebContentClientEndpoint.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-30 23:43:18 +01:00
										 |  |  | #include <pthread.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace WebContent { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static HashMap<int, RefPtr<ClientConnection>> s_connections; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 13:23:39 +02:00
										 |  |  | ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id) | 
					
						
							| 
									
										
										
										
											2020-09-12 11:44:00 +02:00
										 |  |  |     : IPC::ClientConnection<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), client_id) | 
					
						
							| 
									
										
										
										
											2020-06-17 18:00:18 +02:00
										 |  |  |     , m_page_host(PageHost::create(*this)) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     s_connections.set(client_id, *this); | 
					
						
							| 
									
										
										
										
											2020-07-05 16:26:15 +02:00
										 |  |  |     m_paint_flush_timer = Core::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); }); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ClientConnection::~ClientConnection() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ClientConnection::die() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     s_connections.remove(client_id()); | 
					
						
							| 
									
										
										
										
											2020-07-06 13:26:40 +02:00
										 |  |  |     if (s_connections.is_empty()) | 
					
						
							|  |  |  |         Core::EventLoop::current().quit(0); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Web::Page& ClientConnection::page() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_page_host->page(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Web::Page& ClientConnection::page() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_page_host->page(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::update_system_theme(const Core::AnonymousBuffer& theme_buffer) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     Gfx::set_system_theme(theme_buffer); | 
					
						
							|  |  |  |     auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  |     m_page_host->set_palette_impl(*impl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-21 18:10:23 +02:00
										 |  |  | void ClientConnection::update_system_fonts(String const& default_font_query, String const& fixed_width_font_query) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Gfx::FontDatabase::set_default_font_query(default_font_query); | 
					
						
							|  |  |  |     Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 06:16:06 -06:00
										 |  |  | void ClientConnection::update_screen_rects(const Vector<Gfx::IntRect>& rects, u32 main_screen) | 
					
						
							| 
									
										
										
										
											2021-04-04 00:12:37 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-13 06:16:06 -06:00
										 |  |  |     m_page_host->set_screen_rects(rects, main_screen); | 
					
						
							| 
									
										
										
										
											2021-04-04 00:12:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::load_url(const URL& url) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadURL: url={}", url); | 
					
						
							| 
									
										
										
										
											2021-01-30 23:43:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     String process_name; | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     if (url.host().is_empty()) | 
					
						
							| 
									
										
										
										
											2021-01-30 23:43:18 +01:00
										 |  |  |         process_name = "WebContent"; | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |         process_name = String::formatted("WebContent: {}", url.host()); | 
					
						
							| 
									
										
										
										
											2021-01-30 23:43:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pthread_setname_np(pthread_self(), process_name.characters()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     page().load(url); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::load_html(const String& html, const URL& url) | 
					
						
							| 
									
										
										
										
											2020-10-08 21:11:01 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadHTML: html={}, url={}", html, url); | 
					
						
							|  |  |  |     page().load_html(html, url); | 
					
						
							| 
									
										
										
										
											2020-10-08 21:11:01 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::set_viewport_rect(const Gfx::IntRect& rect) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     dbgln_if(SPAM_DEBUG, "handle: WebContentServer::SetViewportRect: rect={}", rect); | 
					
						
							|  |  |  |     m_page_host->set_viewport_rect(rect); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::add_backing_store(i32 backing_store_id, const Gfx::ShareableBitmap& bitmap) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     m_backing_stores.set(backing_store_id, *bitmap.bitmap()); | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::remove_backing_store(i32 backing_store_id) | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     m_backing_stores.remove(backing_store_id); | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::paint(const Gfx::IntRect& content_rect, i32 backing_store_id) | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-05 16:26:15 +02:00
										 |  |  |     for (auto& pending_paint : m_pending_paint_requests) { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |         if (pending_paint.bitmap_id == backing_store_id) { | 
					
						
							|  |  |  |             pending_paint.content_rect = content_rect; | 
					
						
							| 
									
										
										
										
											2020-07-05 16:26:15 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     auto it = m_backing_stores.find(backing_store_id); | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  |     if (it == m_backing_stores.end()) { | 
					
						
							|  |  |  |         did_misbehave("Client requested paint with backing store ID"); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-05 16:26:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  |     auto& bitmap = *it->value; | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     m_pending_paint_requests.append({ content_rect, bitmap, backing_store_id }); | 
					
						
							| 
									
										
										
										
											2020-07-05 16:26:15 +02:00
										 |  |  |     m_paint_flush_timer->start(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ClientConnection::flush_pending_paint_requests() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (auto& pending_paint : m_pending_paint_requests) { | 
					
						
							|  |  |  |         m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap); | 
					
						
							| 
									
										
										
										
											2021-05-03 13:33:59 +02:00
										 |  |  |         async_did_paint(pending_paint.content_rect, pending_paint.bitmap_id); | 
					
						
							| 
									
										
										
										
											2020-07-05 16:26:15 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     m_pending_paint_requests.clear(); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::mouse_down(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     page().handle_mousedown(position, button, modifiers); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::mouse_move(const Gfx::IntPoint& position, [[maybe_unused]] unsigned int button, unsigned int buttons, unsigned int modifiers) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     page().handle_mousemove(position, buttons, modifiers); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::mouse_up(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     page().handle_mouseup(position, button, modifiers); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::mouse_wheel(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers, i32 wheel_delta) | 
					
						
							| 
									
										
										
										
											2021-02-22 19:45:41 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     page().handle_mousewheel(position, button, modifiers, wheel_delta); | 
					
						
							| 
									
										
										
										
											2021-02-22 19:45:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::key_down(i32 key, unsigned int modifiers, u32 code_point) | 
					
						
							| 
									
										
										
										
											2020-08-03 19:58:59 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     page().handle_keydown((KeyCode)key, modifiers, code_point); | 
					
						
							| 
									
										
										
										
											2020-08-03 19:58:59 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::debug_request(const String& request, const String& argument) | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     if (request == "dump-dom-tree") { | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |         if (auto* doc = page().top_level_browsing_context().document()) | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  |             Web::dump_tree(*doc); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     if (request == "dump-layout-tree") { | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |         if (auto* doc = page().top_level_browsing_context().document()) { | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  |             if (auto* icb = doc->layout_node()) | 
					
						
							|  |  |  |                 Web::dump_tree(*icb); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     if (request == "dump-style-sheets") { | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |         if (auto* doc = page().top_level_browsing_context().document()) { | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  |             for (auto& sheet : doc->style_sheets().sheets()) { | 
					
						
							|  |  |  |                 Web::dump_sheet(sheet); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     if (request == "collect-garbage") { | 
					
						
							| 
									
										
										
										
											2021-02-03 10:41:07 +01:00
										 |  |  |         Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true); | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     if (request == "set-line-box-borders") { | 
					
						
							|  |  |  |         bool state = argument == "on"; | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  |         m_page_host->set_should_show_line_box_borders(state); | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |         page().top_level_browsing_context().set_needs_display(page().top_level_browsing_context().viewport_rect()); | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-29 15:31:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     if (request == "clear-cache") { | 
					
						
							| 
									
										
										
										
											2021-03-29 15:31:09 -04:00
										 |  |  |         Web::ResourceLoader::the().clear_cache(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-14 15:13:51 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |     if (request == "spoof-user-agent") { | 
					
						
							|  |  |  |         Web::ResourceLoader::the().set_user_agent(argument); | 
					
						
							| 
									
										
										
										
											2021-04-14 15:13:51 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  | void ClientConnection::get_source() | 
					
						
							| 
									
										
										
										
											2021-02-23 06:14:12 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-30 12:36:53 +02:00
										 |  |  |     if (auto* doc = page().top_level_browsing_context().document()) { | 
					
						
							| 
									
										
										
										
											2021-05-03 13:33:59 +02:00
										 |  |  |         async_did_get_source(doc->url(), doc->source()); | 
					
						
							| 
									
										
										
										
											2021-02-23 06:14:12 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 16:35:10 +01:00
										 |  |  | void ClientConnection::inspect_dom_tree() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (auto* doc = page().top_level_browsing_context().document()) { | 
					
						
							|  |  |  |         async_did_get_dom_tree(doc->dump_dom_tree_as_json()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 12:31:55 +01:00
										 |  |  | Messages::WebContentServer::InspectDomNodeResponse ClientConnection::inspect_dom_node(i32 node_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (auto* doc = page().top_level_browsing_context().document()) { | 
					
						
							|  |  |  |         Web::DOM::Node* node = Web::DOM::Node::from_id(node_id); | 
					
						
							|  |  |  |         if (!node || (&node->document() != doc)) { | 
					
						
							|  |  |  |             doc->set_inspected_node(nullptr); | 
					
						
							|  |  |  |             return { false, "", "" }; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         doc->set_inspected_node(node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (node->is_element()) { | 
					
						
							|  |  |  |             auto& element = verify_cast<Web::DOM::Element>(*node); | 
					
						
							|  |  |  |             if (!element.specified_css_values()) | 
					
						
							|  |  |  |                 return { false, "", "" }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             auto serialize_json = [](Web::CSS::StyleProperties const& properties) -> String { | 
					
						
							|  |  |  |                 StringBuilder builder; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 JsonObjectSerializer serializer(builder); | 
					
						
							|  |  |  |                 properties.for_each_property([&](auto property_id, auto& value) { | 
					
						
							|  |  |  |                     serializer.add(Web::CSS::string_from_property_id(property_id), value.to_string()); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |                 serializer.finish(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return builder.to_string(); | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             String specified_values_json = serialize_json(*element.specified_css_values()); | 
					
						
							|  |  |  |             String computed_values_json = serialize_json(element.computed_style()); | 
					
						
							|  |  |  |             return { true, specified_values_json, computed_values_json }; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { false, "", "" }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 17:33:10 +01:00
										 |  |  | Messages::WebContentServer::GetHoveredNodeIdResponse ClientConnection::get_hovered_node_id() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (auto* document = page().top_level_browsing_context().document()) { | 
					
						
							|  |  |  |         auto hovered_node = document->hovered_node(); | 
					
						
							|  |  |  |         if (hovered_node) | 
					
						
							|  |  |  |             return hovered_node->id(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (i32)0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 11:14:25 +01:00
										 |  |  | void ClientConnection::initialize_js_console(Badge<PageHost>) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* document = page().top_level_browsing_context().document(); | 
					
						
							|  |  |  |     auto interpreter = document->interpreter().make_weak_ptr(); | 
					
						
							|  |  |  |     if (m_interpreter.ptr() == interpreter.ptr()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_interpreter = interpreter; | 
					
						
							|  |  |  |     m_console_client = make<WebContentConsoleClient>(interpreter->global_object().console(), interpreter, *this); | 
					
						
							|  |  |  |     interpreter->global_object().console().set_client(*m_console_client.ptr()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-03 15:52:56 +02:00
										 |  |  | void ClientConnection::js_console_input(const String& js_source) | 
					
						
							| 
									
										
										
										
											2021-02-27 21:44:49 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_console_client) | 
					
						
							| 
									
										
										
										
											2021-05-02 19:54:34 +02:00
										 |  |  |         m_console_client->handle_input(js_source); | 
					
						
							| 
									
										
										
										
											2021-02-27 21:44:49 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 16:56:42 +02:00
										 |  |  | void ClientConnection::run_javascript(String const& js_source) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!page().top_level_browsing_context().document()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto& interpreter = page().top_level_browsing_context().document()->interpreter(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto parser = JS::Parser(JS::Lexer(js_source)); | 
					
						
							|  |  |  |     auto program = parser.parse_program(); | 
					
						
							|  |  |  |     interpreter.run(interpreter.global_object(), *program); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (interpreter.vm().exception()) { | 
					
						
							|  |  |  |         dbgln("Exception :("); | 
					
						
							|  |  |  |         interpreter.vm().clear_exception(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 11:45:36 +01:00
										 |  |  | void ClientConnection::js_console_request_messages(i32 start_index) | 
					
						
							| 
									
										
										
										
											2021-09-04 11:14:25 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-04 11:45:36 +01:00
										 |  |  |     m_console_client->send_messages(start_index); | 
					
						
							| 
									
										
										
										
											2021-09-04 11:14:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:32:55 -04:00
										 |  |  | Messages::WebContentServer::GetSelectedTextResponse ClientConnection::get_selected_text() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return page().focused_context().selected_text(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:53:55 -04:00
										 |  |  | void ClientConnection::select_all() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     page().focused_context().select_all(); | 
					
						
							|  |  |  |     page().client().page_did_change_selection(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } |