ClientConnection.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  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 <LibWeb/Bindings/MainThreadVM.h>
  17. #include <LibWeb/Cookie/ParsedCookie.h>
  18. #include <LibWeb/DOM/Document.h>
  19. #include <LibWeb/Dump.h>
  20. #include <LibWeb/Layout/InitialContainingBlock.h>
  21. #include <LibWeb/Loader/ResourceLoader.h>
  22. #include <LibWeb/Page/BrowsingContext.h>
  23. #include <WebContent/ClientConnection.h>
  24. #include <WebContent/PageHost.h>
  25. #include <WebContent/WebContentClientEndpoint.h>
  26. #include <pthread.h>
  27. namespace WebContent {
  28. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  29. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  30. : IPC::ClientConnection<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), client_id)
  31. , m_page_host(PageHost::create(*this))
  32. {
  33. s_connections.set(client_id, *this);
  34. m_paint_flush_timer = Core::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); });
  35. }
  36. ClientConnection::~ClientConnection()
  37. {
  38. }
  39. void ClientConnection::die()
  40. {
  41. s_connections.remove(client_id());
  42. if (s_connections.is_empty())
  43. Core::EventLoop::current().quit(0);
  44. }
  45. Web::Page& ClientConnection::page()
  46. {
  47. return m_page_host->page();
  48. }
  49. const Web::Page& ClientConnection::page() const
  50. {
  51. return m_page_host->page();
  52. }
  53. void ClientConnection::update_system_theme(const Core::AnonymousBuffer& theme_buffer)
  54. {
  55. Gfx::set_system_theme(theme_buffer);
  56. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_buffer);
  57. m_page_host->set_palette_impl(*impl);
  58. }
  59. void ClientConnection::update_system_fonts(String const& default_font_query, String const& fixed_width_font_query)
  60. {
  61. Gfx::FontDatabase::set_default_font_query(default_font_query);
  62. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  63. }
  64. void ClientConnection::update_screen_rects(const Vector<Gfx::IntRect>& rects, u32 main_screen)
  65. {
  66. m_page_host->set_screen_rects(rects, main_screen);
  67. }
  68. void ClientConnection::load_url(const URL& url)
  69. {
  70. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadURL: url={}", url);
  71. String process_name;
  72. if (url.host().is_empty())
  73. process_name = "WebContent";
  74. else
  75. process_name = String::formatted("WebContent: {}", url.host());
  76. pthread_setname_np(pthread_self(), process_name.characters());
  77. page().load(url);
  78. }
  79. void ClientConnection::load_html(const String& html, const URL& url)
  80. {
  81. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::LoadHTML: html={}, url={}", html, url);
  82. page().load_html(html, url);
  83. }
  84. void ClientConnection::set_viewport_rect(const Gfx::IntRect& rect)
  85. {
  86. dbgln_if(SPAM_DEBUG, "handle: WebContentServer::SetViewportRect: rect={}", rect);
  87. m_page_host->set_viewport_rect(rect);
  88. }
  89. void ClientConnection::add_backing_store(i32 backing_store_id, const Gfx::ShareableBitmap& bitmap)
  90. {
  91. m_backing_stores.set(backing_store_id, *bitmap.bitmap());
  92. }
  93. void ClientConnection::remove_backing_store(i32 backing_store_id)
  94. {
  95. m_backing_stores.remove(backing_store_id);
  96. }
  97. void ClientConnection::paint(const Gfx::IntRect& content_rect, i32 backing_store_id)
  98. {
  99. for (auto& pending_paint : m_pending_paint_requests) {
  100. if (pending_paint.bitmap_id == backing_store_id) {
  101. pending_paint.content_rect = content_rect;
  102. return;
  103. }
  104. }
  105. auto it = m_backing_stores.find(backing_store_id);
  106. if (it == m_backing_stores.end()) {
  107. did_misbehave("Client requested paint with backing store ID");
  108. return;
  109. }
  110. auto& bitmap = *it->value;
  111. m_pending_paint_requests.append({ content_rect, bitmap, backing_store_id });
  112. m_paint_flush_timer->start();
  113. }
  114. void ClientConnection::flush_pending_paint_requests()
  115. {
  116. for (auto& pending_paint : m_pending_paint_requests) {
  117. m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap);
  118. async_did_paint(pending_paint.content_rect, pending_paint.bitmap_id);
  119. }
  120. m_pending_paint_requests.clear();
  121. }
  122. void ClientConnection::mouse_down(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers)
  123. {
  124. page().handle_mousedown(position, button, modifiers);
  125. }
  126. void ClientConnection::mouse_move(const Gfx::IntPoint& position, [[maybe_unused]] unsigned int button, unsigned int buttons, unsigned int modifiers)
  127. {
  128. page().handle_mousemove(position, buttons, modifiers);
  129. }
  130. void ClientConnection::mouse_up(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers)
  131. {
  132. page().handle_mouseup(position, button, modifiers);
  133. }
  134. void ClientConnection::mouse_wheel(const Gfx::IntPoint& position, unsigned int button, [[maybe_unused]] unsigned int buttons, unsigned int modifiers, i32 wheel_delta)
  135. {
  136. page().handle_mousewheel(position, button, modifiers, wheel_delta);
  137. }
  138. void ClientConnection::key_down(i32 key, unsigned int modifiers, u32 code_point)
  139. {
  140. page().handle_keydown((KeyCode)key, modifiers, code_point);
  141. }
  142. void ClientConnection::debug_request(const String& request, const String& argument)
  143. {
  144. if (request == "dump-dom-tree") {
  145. if (auto* doc = page().top_level_browsing_context().active_document())
  146. Web::dump_tree(*doc);
  147. }
  148. if (request == "dump-layout-tree") {
  149. if (auto* doc = page().top_level_browsing_context().active_document()) {
  150. if (auto* icb = doc->layout_node())
  151. Web::dump_tree(*icb);
  152. }
  153. }
  154. if (request == "dump-style-sheets") {
  155. if (auto* doc = page().top_level_browsing_context().active_document()) {
  156. for (auto& sheet : doc->style_sheets().sheets()) {
  157. Web::dump_sheet(sheet);
  158. }
  159. }
  160. }
  161. if (request == "collect-garbage") {
  162. Web::Bindings::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  163. }
  164. if (request == "set-line-box-borders") {
  165. bool state = argument == "on";
  166. m_page_host->set_should_show_line_box_borders(state);
  167. page().top_level_browsing_context().set_needs_display(page().top_level_browsing_context().viewport_rect());
  168. }
  169. if (request == "clear-cache") {
  170. Web::ResourceLoader::the().clear_cache();
  171. }
  172. if (request == "spoof-user-agent") {
  173. Web::ResourceLoader::the().set_user_agent(argument);
  174. }
  175. if (request == "same-origin-policy") {
  176. m_page_host->page().set_same_origin_policy_enabled(argument == "on");
  177. }
  178. }
  179. void ClientConnection::get_source()
  180. {
  181. if (auto* doc = page().top_level_browsing_context().active_document()) {
  182. async_did_get_source(doc->url(), doc->source());
  183. }
  184. }
  185. void ClientConnection::inspect_dom_tree()
  186. {
  187. if (auto* doc = page().top_level_browsing_context().active_document()) {
  188. async_did_get_dom_tree(doc->dump_dom_tree_as_json());
  189. }
  190. }
  191. Messages::WebContentServer::InspectDomNodeResponse ClientConnection::inspect_dom_node(i32 node_id)
  192. {
  193. if (auto* doc = page().top_level_browsing_context().active_document()) {
  194. Web::DOM::Node* node = Web::DOM::Node::from_id(node_id);
  195. if (!node || (&node->document() != doc)) {
  196. doc->set_inspected_node(nullptr);
  197. return { false, "", "" };
  198. }
  199. doc->set_inspected_node(node);
  200. if (node->is_element()) {
  201. auto& element = verify_cast<Web::DOM::Element>(*node);
  202. if (!element.specified_css_values())
  203. return { false, "", "" };
  204. auto serialize_json = [](Web::CSS::StyleProperties const& properties) -> String {
  205. StringBuilder builder;
  206. JsonObjectSerializer serializer(builder);
  207. properties.for_each_property([&](auto property_id, auto& value) {
  208. serializer.add(Web::CSS::string_from_property_id(property_id), value.to_string());
  209. });
  210. serializer.finish();
  211. return builder.to_string();
  212. };
  213. String specified_values_json = serialize_json(*element.specified_css_values());
  214. String computed_values_json = serialize_json(element.computed_style());
  215. return { true, specified_values_json, computed_values_json };
  216. }
  217. }
  218. return { false, "", "" };
  219. }
  220. Messages::WebContentServer::GetHoveredNodeIdResponse ClientConnection::get_hovered_node_id()
  221. {
  222. if (auto* document = page().top_level_browsing_context().active_document()) {
  223. auto hovered_node = document->hovered_node();
  224. if (hovered_node)
  225. return hovered_node->id();
  226. }
  227. return (i32)0;
  228. }
  229. void ClientConnection::initialize_js_console(Badge<PageHost>)
  230. {
  231. auto* document = page().top_level_browsing_context().active_document();
  232. auto interpreter = document->interpreter().make_weak_ptr();
  233. if (m_interpreter.ptr() == interpreter.ptr())
  234. return;
  235. m_interpreter = interpreter;
  236. m_console_client = make<WebContentConsoleClient>(interpreter->global_object().console(), interpreter, *this);
  237. interpreter->global_object().console().set_client(*m_console_client.ptr());
  238. }
  239. void ClientConnection::js_console_input(const String& js_source)
  240. {
  241. if (m_console_client)
  242. m_console_client->handle_input(js_source);
  243. }
  244. void ClientConnection::run_javascript(String const& js_source)
  245. {
  246. if (!page().top_level_browsing_context().active_document())
  247. return;
  248. auto& interpreter = page().top_level_browsing_context().active_document()->interpreter();
  249. auto parser = JS::Parser(JS::Lexer(js_source));
  250. auto program = parser.parse_program();
  251. interpreter.run(interpreter.global_object(), *program);
  252. if (interpreter.vm().exception()) {
  253. dbgln("Exception :(");
  254. interpreter.vm().clear_exception();
  255. }
  256. }
  257. void ClientConnection::js_console_request_messages(i32 start_index)
  258. {
  259. m_console_client->send_messages(start_index);
  260. }
  261. Messages::WebContentServer::GetSelectedTextResponse ClientConnection::get_selected_text()
  262. {
  263. return page().focused_context().selected_text();
  264. }
  265. void ClientConnection::select_all()
  266. {
  267. page().focused_context().select_all();
  268. page().client().page_did_change_selection();
  269. }
  270. Messages::WebContentServer::DumpLayoutTreeResponse ClientConnection::dump_layout_tree()
  271. {
  272. auto* document = page().top_level_browsing_context().active_document();
  273. if (!document)
  274. return String { "(no DOM tree)" };
  275. auto* layout_root = document->layout_node();
  276. if (!layout_root)
  277. return String { "(no layout tree)" };
  278. StringBuilder builder;
  279. Web::dump_tree(builder, *layout_root);
  280. return builder.to_string();
  281. }
  282. }