ClientConnection.cpp 7.2 KB

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