ClientConnection.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Badge.h>
  27. #include <AK/Debug.h>
  28. #include <LibGfx/Bitmap.h>
  29. #include <LibGfx/SystemTheme.h>
  30. #include <LibJS/Console.h>
  31. #include <LibJS/Heap/Heap.h>
  32. #include <LibJS/Interpreter.h>
  33. #include <LibJS/Parser.h>
  34. #include <LibJS/Runtime/VM.h>
  35. #include <LibWeb/Bindings/MainThreadVM.h>
  36. #include <LibWeb/DOM/Document.h>
  37. #include <LibWeb/Dump.h>
  38. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  39. #include <LibWeb/Loader/ResourceLoader.h>
  40. #include <LibWeb/Page/Frame.h>
  41. #include <WebContent/ClientConnection.h>
  42. #include <WebContent/PageHost.h>
  43. #include <WebContent/WebContentClientEndpoint.h>
  44. #include <pthread.h>
  45. namespace WebContent {
  46. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  47. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  48. : IPC::ClientConnection<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), client_id)
  49. , m_page_host(PageHost::create(*this))
  50. {
  51. s_connections.set(client_id, *this);
  52. m_paint_flush_timer = Core::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); });
  53. }
  54. ClientConnection::~ClientConnection()
  55. {
  56. }
  57. void ClientConnection::die()
  58. {
  59. s_connections.remove(client_id());
  60. if (s_connections.is_empty())
  61. Core::EventLoop::current().quit(0);
  62. }
  63. Web::Page& ClientConnection::page()
  64. {
  65. return m_page_host->page();
  66. }
  67. const Web::Page& ClientConnection::page() const
  68. {
  69. return m_page_host->page();
  70. }
  71. OwnPtr<Messages::WebContentServer::GreetResponse> ClientConnection::handle(const Messages::WebContentServer::Greet&)
  72. {
  73. return make<Messages::WebContentServer::GreetResponse>();
  74. }
  75. void ClientConnection::handle(const Messages::WebContentServer::UpdateSystemTheme& message)
  76. {
  77. Gfx::set_system_theme(message.theme_buffer());
  78. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(message.theme_buffer());
  79. m_page_host->set_palette_impl(*impl);
  80. }
  81. void ClientConnection::handle(const Messages::WebContentServer::UpdateScreenRect& message)
  82. {
  83. m_page_host->set_screen_rect(message.rect());
  84. }
  85. void ClientConnection::handle(const Messages::WebContentServer::LoadURL& message)
  86. {
  87. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadURL: url={}", message.url());
  88. String process_name;
  89. if (message.url().host().is_empty())
  90. process_name = "WebContent";
  91. else
  92. process_name = String::formatted("WebContent: {}", message.url().host());
  93. pthread_setname_np(pthread_self(), process_name.characters());
  94. page().load(message.url());
  95. }
  96. void ClientConnection::handle(const Messages::WebContentServer::LoadHTML& message)
  97. {
  98. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadHTML: html={}, url={}", message.html(), message.url());
  99. page().load_html(message.html(), message.url());
  100. }
  101. void ClientConnection::handle(const Messages::WebContentServer::SetViewportRect& message)
  102. {
  103. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::SetViewportRect: rect={}", message.rect());
  104. m_page_host->set_viewport_rect(message.rect());
  105. }
  106. void ClientConnection::handle(const Messages::WebContentServer::AddBackingStore& message)
  107. {
  108. m_backing_stores.set(message.backing_store_id(), *message.bitmap().bitmap());
  109. }
  110. void ClientConnection::handle(const Messages::WebContentServer::RemoveBackingStore& message)
  111. {
  112. m_backing_stores.remove(message.backing_store_id());
  113. }
  114. void ClientConnection::handle(const Messages::WebContentServer::Paint& message)
  115. {
  116. for (auto& pending_paint : m_pending_paint_requests) {
  117. if (pending_paint.bitmap_id == message.backing_store_id()) {
  118. pending_paint.content_rect = message.content_rect();
  119. return;
  120. }
  121. }
  122. auto it = m_backing_stores.find(message.backing_store_id());
  123. if (it == m_backing_stores.end()) {
  124. did_misbehave("Client requested paint with backing store ID");
  125. return;
  126. }
  127. auto& bitmap = *it->value;
  128. m_pending_paint_requests.append({ message.content_rect(), bitmap, message.backing_store_id() });
  129. m_paint_flush_timer->start();
  130. }
  131. void ClientConnection::flush_pending_paint_requests()
  132. {
  133. for (auto& pending_paint : m_pending_paint_requests) {
  134. m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap);
  135. post_message(Messages::WebContentClient::DidPaint(pending_paint.content_rect, pending_paint.bitmap_id));
  136. }
  137. m_pending_paint_requests.clear();
  138. }
  139. void ClientConnection::handle(const Messages::WebContentServer::MouseDown& message)
  140. {
  141. page().handle_mousedown(message.position(), message.button(), message.modifiers());
  142. }
  143. void ClientConnection::handle(const Messages::WebContentServer::MouseMove& message)
  144. {
  145. page().handle_mousemove(message.position(), message.buttons(), message.modifiers());
  146. }
  147. void ClientConnection::handle(const Messages::WebContentServer::MouseUp& message)
  148. {
  149. page().handle_mouseup(message.position(), message.button(), message.modifiers());
  150. }
  151. void ClientConnection::handle(const Messages::WebContentServer::MouseWheel& message)
  152. {
  153. page().handle_mousewheel(message.position(), message.button(), message.modifiers(), message.wheel_delta());
  154. }
  155. void ClientConnection::handle(const Messages::WebContentServer::KeyDown& message)
  156. {
  157. page().handle_keydown((KeyCode)message.key(), message.modifiers(), message.code_point());
  158. }
  159. void ClientConnection::handle(const Messages::WebContentServer::DebugRequest& message)
  160. {
  161. if (message.request() == "dump-dom-tree") {
  162. if (auto* doc = page().main_frame().document())
  163. Web::dump_tree(*doc);
  164. }
  165. if (message.request() == "dump-layout-tree") {
  166. if (auto* doc = page().main_frame().document()) {
  167. if (auto* icb = doc->layout_node())
  168. Web::dump_tree(*icb);
  169. }
  170. }
  171. if (message.request() == "dump-style-sheets") {
  172. if (auto* doc = page().main_frame().document()) {
  173. for (auto& sheet : doc->style_sheets().sheets()) {
  174. Web::dump_sheet(sheet);
  175. }
  176. }
  177. }
  178. if (message.request() == "collect-garbage") {
  179. Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  180. }
  181. if (message.request() == "set-line-box-borders") {
  182. bool state = message.argument() == "on";
  183. m_page_host->set_should_show_line_box_borders(state);
  184. page().main_frame().set_needs_display(page().main_frame().viewport_rect());
  185. }
  186. if (message.request() == "clear-cache") {
  187. Web::ResourceLoader::the().clear_cache();
  188. }
  189. if (message.request() == "spoof-user-agent") {
  190. Web::ResourceLoader::the().set_user_agent(message.argument());
  191. }
  192. }
  193. void ClientConnection::handle(const Messages::WebContentServer::GetSource&)
  194. {
  195. if (auto* doc = page().main_frame().document()) {
  196. post_message(Messages::WebContentClient::DidGetSource(doc->url(), doc->source()));
  197. }
  198. }
  199. void ClientConnection::handle(const Messages::WebContentServer::JSConsoleInitialize&)
  200. {
  201. if (auto* document = page().main_frame().document()) {
  202. auto interpreter = document->interpreter().make_weak_ptr();
  203. if (m_interpreter.ptr() == interpreter.ptr())
  204. return;
  205. m_interpreter = interpreter;
  206. m_console_client = make<WebContentConsoleClient>(interpreter->global_object().console(), interpreter, *this);
  207. interpreter->global_object().console().set_client(*m_console_client.ptr());
  208. }
  209. }
  210. void ClientConnection::handle(const Messages::WebContentServer::JSConsoleInput& message)
  211. {
  212. if (m_console_client)
  213. m_console_client->handle_input(message.js_source());
  214. }
  215. }