ClientConnection.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/Heap/Heap.h>
  31. #include <LibJS/Runtime/VM.h>
  32. #include <LibWeb/DOM/Document.h>
  33. #include <LibWeb/Dump.h>
  34. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  35. #include <LibWeb/Page/Frame.h>
  36. #include <WebContent/ClientConnection.h>
  37. #include <WebContent/PageHost.h>
  38. #include <WebContent/WebContentClientEndpoint.h>
  39. #include <pthread.h>
  40. namespace Web::DOM {
  41. extern JS::VM& main_thread_vm();
  42. }
  43. namespace WebContent {
  44. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  45. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  46. : IPC::ClientConnection<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), client_id)
  47. , m_page_host(PageHost::create(*this))
  48. {
  49. s_connections.set(client_id, *this);
  50. m_paint_flush_timer = Core::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); });
  51. }
  52. ClientConnection::~ClientConnection()
  53. {
  54. }
  55. void ClientConnection::die()
  56. {
  57. s_connections.remove(client_id());
  58. if (s_connections.is_empty())
  59. Core::EventLoop::current().quit(0);
  60. }
  61. Web::Page& ClientConnection::page()
  62. {
  63. return m_page_host->page();
  64. }
  65. const Web::Page& ClientConnection::page() const
  66. {
  67. return m_page_host->page();
  68. }
  69. OwnPtr<Messages::WebContentServer::GreetResponse> ClientConnection::handle(const Messages::WebContentServer::Greet&)
  70. {
  71. return make<Messages::WebContentServer::GreetResponse>(client_id());
  72. }
  73. void ClientConnection::handle(const Messages::WebContentServer::UpdateSystemTheme& message)
  74. {
  75. Gfx::set_system_theme(message.theme_buffer());
  76. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(message.theme_buffer());
  77. m_page_host->set_palette_impl(*impl);
  78. }
  79. void ClientConnection::handle(const Messages::WebContentServer::LoadURL& message)
  80. {
  81. dbgln<SPAM_DEBUG>("handle: WebContentServer::LoadURL: url={}", message.url());
  82. String process_name;
  83. if (message.url().host().is_empty())
  84. process_name = "WebContent";
  85. else
  86. process_name = String::formatted("WebContent: {}", message.url().host());
  87. pthread_setname_np(pthread_self(), process_name.characters());
  88. page().load(message.url());
  89. }
  90. void ClientConnection::handle(const Messages::WebContentServer::LoadHTML& message)
  91. {
  92. dbgln<SPAM_DEBUG>("handle: WebContentServer::LoadHTML: html={}, url={}", message.html(), message.url());
  93. page().load_html(message.html(), message.url());
  94. }
  95. void ClientConnection::handle(const Messages::WebContentServer::SetViewportRect& message)
  96. {
  97. dbgln<SPAM_DEBUG>("handle: WebContentServer::SetViewportRect: rect={}", message.rect());
  98. m_page_host->set_viewport_rect(message.rect());
  99. }
  100. void ClientConnection::handle(const Messages::WebContentServer::AddBackingStore& message)
  101. {
  102. m_backing_stores.set(message.backing_store_id(), *message.bitmap().bitmap());
  103. }
  104. void ClientConnection::handle(const Messages::WebContentServer::RemoveBackingStore& message)
  105. {
  106. m_backing_stores.remove(message.backing_store_id());
  107. }
  108. void ClientConnection::handle(const Messages::WebContentServer::Paint& message)
  109. {
  110. for (auto& pending_paint : m_pending_paint_requests) {
  111. if (pending_paint.bitmap_id == message.backing_store_id()) {
  112. pending_paint.content_rect = message.content_rect();
  113. return;
  114. }
  115. }
  116. auto it = m_backing_stores.find(message.backing_store_id());
  117. if (it == m_backing_stores.end()) {
  118. did_misbehave("Client requested paint with backing store ID");
  119. return;
  120. }
  121. auto& bitmap = *it->value;
  122. m_pending_paint_requests.append({ message.content_rect(), bitmap, message.backing_store_id() });
  123. m_paint_flush_timer->start();
  124. }
  125. void ClientConnection::flush_pending_paint_requests()
  126. {
  127. for (auto& pending_paint : m_pending_paint_requests) {
  128. m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap);
  129. post_message(Messages::WebContentClient::DidPaint(pending_paint.content_rect, pending_paint.bitmap_id));
  130. }
  131. m_pending_paint_requests.clear();
  132. }
  133. void ClientConnection::handle(const Messages::WebContentServer::MouseDown& message)
  134. {
  135. page().handle_mousedown(message.position(), message.button(), message.modifiers());
  136. }
  137. void ClientConnection::handle(const Messages::WebContentServer::MouseMove& message)
  138. {
  139. page().handle_mousemove(message.position(), message.buttons(), message.modifiers());
  140. }
  141. void ClientConnection::handle(const Messages::WebContentServer::MouseUp& message)
  142. {
  143. page().handle_mouseup(message.position(), message.button(), message.modifiers());
  144. }
  145. void ClientConnection::handle(const Messages::WebContentServer::KeyDown& message)
  146. {
  147. page().handle_keydown((KeyCode)message.key(), message.modifiers(), message.code_point());
  148. }
  149. void ClientConnection::handle(const Messages::WebContentServer::DebugRequest& message)
  150. {
  151. if (message.request() == "dump-dom-tree") {
  152. if (auto* doc = page().main_frame().document())
  153. Web::dump_tree(*doc);
  154. }
  155. if (message.request() == "dump-layout-tree") {
  156. if (auto* doc = page().main_frame().document()) {
  157. if (auto* icb = doc->layout_node())
  158. Web::dump_tree(*icb);
  159. }
  160. }
  161. if (message.request() == "dump-style-sheets") {
  162. if (auto* doc = page().main_frame().document()) {
  163. for (auto& sheet : doc->style_sheets().sheets()) {
  164. Web::dump_sheet(sheet);
  165. }
  166. }
  167. }
  168. if (message.request() == "collect-garbage") {
  169. ::Web::DOM::main_thread_vm().heap().collect_garbage(JS::Heap::CollectionType::CollectGarbage, true);
  170. }
  171. if (message.request() == "set-line-box-borders") {
  172. bool state = message.argument() == "on";
  173. m_page_host->set_should_show_line_box_borders(state);
  174. page().main_frame().set_needs_display(page().main_frame().viewport_rect());
  175. }
  176. }
  177. }