| 
									
										
										
										
											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> | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Badge.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-17 18:39:00 +01:00
										 |  |  | #include <AK/Debug.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | #include <LibGfx/Bitmap.h>
 | 
					
						
							|  |  |  | #include <LibGfx/SystemTheme.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  | #include <LibJS/Heap/Heap.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-03 10:41:07 +01:00
										 |  |  | #include <LibWeb/Bindings/MainThreadVM.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Dump.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/InitialContainingBlockBox.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Page/Frame.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-01-31 09:24:46 +01:00
										 |  |  | OwnPtr<Messages::WebContentServer::GreetResponse> ClientConnection::handle(const Messages::WebContentServer::Greet&) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-01 11:26:41 +01:00
										 |  |  |     return make<Messages::WebContentServer::GreetResponse>(); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ClientConnection::handle(const Messages::WebContentServer::UpdateSystemTheme& message) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-16 17:22:35 +01:00
										 |  |  |     Gfx::set_system_theme(message.theme_buffer()); | 
					
						
							|  |  |  |     auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(message.theme_buffer()); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  |     m_page_host->set_palette_impl(*impl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ClientConnection::handle(const Messages::WebContentServer::LoadURL& message) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |     dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadURL: url={}", message.url()); | 
					
						
							| 
									
										
										
										
											2021-01-30 23:43:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     String process_name; | 
					
						
							|  |  |  |     if (message.url().host().is_empty()) | 
					
						
							|  |  |  |         process_name = "WebContent"; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         process_name = String::formatted("WebContent: {}", message.url().host()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pthread_setname_np(pthread_self(), process_name.characters()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  |     page().load(message.url()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 21:11:01 +01:00
										 |  |  | void ClientConnection::handle(const Messages::WebContentServer::LoadHTML& message) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |     dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadHTML: html={}, url={}", message.html(), message.url()); | 
					
						
							| 
									
										
										
										
											2020-10-08 21:11:01 +01:00
										 |  |  |     page().load_html(message.html(), message.url()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | void ClientConnection::handle(const Messages::WebContentServer::SetViewportRect& message) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |     dbgln_if(SPAM_DEBUG, "handle: WebContentServer::SetViewportRect: rect={}", message.rect()); | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  |     m_page_host->set_viewport_rect(message.rect()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  | void ClientConnection::handle(const Messages::WebContentServer::AddBackingStore& message) | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  |     m_backing_stores.set(message.backing_store_id(), *message.bitmap().bitmap()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ClientConnection::handle(const Messages::WebContentServer::RemoveBackingStore& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_backing_stores.remove(message.backing_store_id()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  | void ClientConnection::handle(const Messages::WebContentServer::Paint& message) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-05 16:26:15 +02:00
										 |  |  |     for (auto& pending_paint : m_pending_paint_requests) { | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  |         if (pending_paint.bitmap_id == message.backing_store_id()) { | 
					
						
							| 
									
										
										
										
											2020-07-05 16:26:15 +02:00
										 |  |  |             pending_paint.content_rect = message.content_rect(); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 23:15:32 +01:00
										 |  |  |     auto it = m_backing_stores.find(message.backing_store_id()); | 
					
						
							|  |  |  |     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; | 
					
						
							|  |  |  |     m_pending_paint_requests.append({ message.content_rect(), bitmap, message.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-01-16 23:15:32 +01:00
										 |  |  |         post_message(Messages::WebContentClient::DidPaint(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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ClientConnection::handle(const Messages::WebContentServer::MouseDown& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     page().handle_mousedown(message.position(), message.button(), message.modifiers()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ClientConnection::handle(const Messages::WebContentServer::MouseMove& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     page().handle_mousemove(message.position(), message.buttons(), message.modifiers()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ClientConnection::handle(const Messages::WebContentServer::MouseUp& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     page().handle_mouseup(message.position(), message.button(), message.modifiers()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:58:59 +02:00
										 |  |  | void ClientConnection::handle(const Messages::WebContentServer::KeyDown& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     page().handle_keydown((KeyCode)message.key(), message.modifiers(), message.code_point()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 09:06:25 +01:00
										 |  |  | void ClientConnection::handle(const Messages::WebContentServer::DebugRequest& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (message.request() == "dump-dom-tree") { | 
					
						
							|  |  |  |         if (auto* doc = page().main_frame().document()) | 
					
						
							|  |  |  |             Web::dump_tree(*doc); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (message.request() == "dump-layout-tree") { | 
					
						
							|  |  |  |         if (auto* doc = page().main_frame().document()) { | 
					
						
							|  |  |  |             if (auto* icb = doc->layout_node()) | 
					
						
							|  |  |  |                 Web::dump_tree(*icb); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (message.request() == "dump-style-sheets") { | 
					
						
							|  |  |  |         if (auto* doc = page().main_frame().document()) { | 
					
						
							|  |  |  |             for (auto& sheet : doc->style_sheets().sheets()) { | 
					
						
							|  |  |  |                 Web::dump_sheet(sheet); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (message.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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (message.request() == "set-line-box-borders") { | 
					
						
							|  |  |  |         bool state = message.argument() == "on"; | 
					
						
							|  |  |  |         m_page_host->set_should_show_line_box_borders(state); | 
					
						
							|  |  |  |         page().main_frame().set_needs_display(page().main_frame().viewport_rect()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 17:31:42 +02:00
										 |  |  | } |