ClientConnection.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Badge.h>
  7. #include <AK/Debug.h>
  8. #include <AK/JsonObject.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/FontDatabase.h>
  11. #include <LibGfx/SystemTheme.h>
  12. #include <LibJS/Console.h>
  13. #include <LibJS/Heap/Heap.h>
  14. #include <LibJS/Interpreter.h>
  15. #include <LibJS/Parser.h>
  16. #include <LibJS/Runtime/VM.h>
  17. #include <LibWeb/Bindings/MainThreadVM.h>
  18. #include <LibWeb/Cookie/ParsedCookie.h>
  19. #include <LibWeb/DOM/Document.h>
  20. #include <LibWeb/Dump.h>
  21. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  22. #include <LibWeb/Loader/ResourceLoader.h>
  23. #include <LibWeb/Page/BrowsingContext.h>
  24. #include <WebContent/ClientConnection.h>
  25. #include <WebContent/PageHost.h>
  26. #include <WebContent/WebContentClientEndpoint.h>
  27. #include <pthread.h>
  28. namespace WebContent {
  29. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  30. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  31. : IPC::ClientConnection<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), client_id)
  32. , m_page_host(PageHost::create(*this))
  33. {
  34. s_connections.set(client_id, *this);
  35. m_paint_flush_timer = Core::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); });
  36. }
  37. ClientConnection::~ClientConnection()
  38. {
  39. }
  40. void ClientConnection::die()
  41. {
  42. s_connections.remove(client_id());
  43. if (s_connections.is_empty())
  44. Core::EventLoop::current().quit(0);
  45. }
  46. Web::Page& ClientConnection::page()
  47. {
  48. return m_page_host->page();
  49. }
  50. const Web::Page& ClientConnection::page() const
  51. {
  52. return m_page_host->page();
  53. }
  54. void ClientConnection::update_system_theme(const Core::AnonymousBuffer& theme_buffer)
  55. {
  56. Gfx::set_system_theme(theme_buffer);
  57. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer);
  58. m_page_host->set_palette_impl(*impl);
  59. }
  60. void ClientConnection::update_system_fonts(String const& default_font_query, String const& fixed_width_font_query)
  61. {
  62. Gfx::FontDatabase::set_default_font_query(default_font_query);
  63. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  64. }
  65. void ClientConnection::update_screen_rects(const Vector<Gfx::IntRect>& rects, u32 main_screen)
  66. {
  67. m_page_host->set_screen_rects(rects, main_screen);
  68. }
  69. void ClientConnection::load_url(const URL& url)
  70. {
  71. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadURL: url={}", url);
  72. String process_name;
  73. if (url.host().is_empty())
  74. process_name = "WebContent";
  75. else
  76. process_name = String::formatted("WebContent: {}", url.host());
  77. pthread_setname_np(pthread_self(), process_name.characters());
  78. page().load(url);
  79. }
  80. void ClientConnection::load_html(const String& html, const URL& url)
  81. {
  82. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadHTML: html={}, url={}", html, url);
  83. page().load_html(html, url);
  84. }
  85. void ClientConnection::set_viewport_rect(const Gfx::IntRect& rect)
  86. {
  87. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::SetViewportRect: rect={}", rect);
  88. m_page_host->set_viewport_rect(rect);
  89. }
  90. void ClientConnection::add_backing_store(i32 backing_store_id, const Gfx::ShareableBitmap& bitmap)
  91. {
  92. m_backing_stores.set(backing_store_id, *bitmap.bitmap());
  93. }
  94. void ClientConnection::remove_backing_store(i32 backing_store_id)
  95. {
  96. m_backing_stores.remove(backing_store_id);
  97. }
  98. void ClientConnection::paint(const Gfx::IntRect& content_rect, i32 backing_store_id)
  99. {
  100. for (auto& pending_paint : m_pending_paint_requests) {
  101. if (pending_paint.bitmap_id == backing_store_id) {
  102. pending_paint.content_rect = content_rect;
  103. return;
  104. }
  105. }
  106. auto it = m_backing_stores.find(backing_store_id);
  107. if (it == m_backing_stores.end()) {
  108. did_misbehave("Client requested paint with backing store ID");
  109. return;
  110. }
  111. auto& bitmap = *it->value;
  112. m_pending_paint_requests.append({ content_rect, bitmap, backing_store_id });
  113. m_paint_flush_timer->start();
  114. }
  115. void ClientConnection::flush_pending_paint_requests()
  116. {
  117. for (auto& pending_paint : m_pending_paint_requests) {
  118. m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap);
  119. async_did_paint(pending_paint.content_rect, pending_paint.bitmap_id);
  120. }
  121. m_pending_paint_requests.clear();
  122. }
  123. void ClientConnection::mouse_down(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers)
  124. {
  125. page().handle_mousedown(position, button, modifiers);
  126. }
  127. void ClientConnection::mouse_move(const Gfx::IntPoint& position, [[maybe_unused]] unsigned int button, unsigned int buttons, unsigned int modifiers)
  128. {
  129. page().handle_mousemove(position, buttons, modifiers);
  130. }
  131. void ClientConnection::mouse_up(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers)
  132. {
  133. page().handle_mouseup(position, button, modifiers);
  134. }
  135. void ClientConnection::mouse_wheel(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers, i32 wheel_delta)
  136. {
  137. page().handle_mousewheel(position, button, modifiers, wheel_delta);
  138. }
  139. void ClientConnection::key_down(i32 key, unsigned int modifiers, u32 code_point)
  140. {
  141. page().handle_keydown((KeyCode)key, modifiers, code_point);
  142. }
  143. void ClientConnection::debug_request(const String& request, const String& argument)
  144. {
  145. if (request == "dump-dom-tree") {
  146. if (auto* doc = page().top_level_browsing_context().document())
  147. Web::dump_tree(*doc);
  148. }
  149. if (request == "dump-layout-tree") {
  150. if (auto* doc = page().top_level_browsing_context().document()) {
  151. if (auto* icb = doc->layout_node())
  152. Web::dump_tree(*icb);
  153. }
  154. }
  155. if (request == "dump-style-sheets") {
  156. if (auto* doc = page().top_level_browsing_context().document()) {
  157. for (auto& sheet : doc->style_sheets().sheets()) {
  158. Web::dump_sheet(sheet);
  159. }
  160. }
  161. }
  162. if (request == "collect-garbage") {
  163. Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  164. }
  165. if (request == "set-line-box-borders") {
  166. bool state = argument == "on";
  167. m_page_host->set_should_show_line_box_borders(state);
  168. page().top_level_browsing_context().set_needs_display(page().top_level_browsing_context().viewport_rect());
  169. }
  170. if (request == "clear-cache") {
  171. Web::ResourceLoader::the().clear_cache();
  172. }
  173. if (request == "spoof-user-agent") {
  174. Web::ResourceLoader::the().set_user_agent(argument);
  175. }
  176. }
  177. void ClientConnection::get_source()
  178. {
  179. if (auto* doc = page().top_level_browsing_context().document()) {
  180. async_did_get_source(doc->url(), doc->source());
  181. }
  182. }
  183. void ClientConnection::inspect_dom_tree()
  184. {
  185. if (auto* doc = page().top_level_browsing_context().document()) {
  186. async_did_get_dom_tree(doc->dump_dom_tree_as_json());
  187. }
  188. }
  189. void ClientConnection::js_console_initialize()
  190. {
  191. if (auto* document = page().top_level_browsing_context().document()) {
  192. auto interpreter = document->interpreter().make_weak_ptr();
  193. if (m_interpreter.ptr() == interpreter.ptr())
  194. return;
  195. m_interpreter = interpreter;
  196. m_console_client = make<WebContentConsoleClient>(interpreter->global_object().console(), interpreter, *this);
  197. interpreter->global_object().console().set_client(*m_console_client.ptr());
  198. }
  199. }
  200. void ClientConnection::js_console_input(const String& js_source)
  201. {
  202. if (m_console_client)
  203. m_console_client->handle_input(js_source);
  204. }
  205. Messages::WebContentServer::GetSelectedTextResponse ClientConnection::get_selected_text()
  206. {
  207. return page().focused_context().selected_text();
  208. }
  209. void ClientConnection::select_all()
  210. {
  211. page().focused_context().select_all();
  212. page().client().page_did_change_selection();
  213. }
  214. }