ClientConnection.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <WebContent/ClientConnection.h>
  31. #include <WebContent/PageHost.h>
  32. #include <WebContent/WebContentClientEndpoint.h>
  33. #include <pthread.h>
  34. namespace WebContent {
  35. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  36. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  37. : IPC::ClientConnection<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), client_id)
  38. , m_page_host(PageHost::create(*this))
  39. {
  40. s_connections.set(client_id, *this);
  41. m_paint_flush_timer = Core::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); });
  42. }
  43. ClientConnection::~ClientConnection()
  44. {
  45. }
  46. void ClientConnection::die()
  47. {
  48. s_connections.remove(client_id());
  49. if (s_connections.is_empty())
  50. Core::EventLoop::current().quit(0);
  51. }
  52. Web::Page& ClientConnection::page()
  53. {
  54. return m_page_host->page();
  55. }
  56. const Web::Page& ClientConnection::page() const
  57. {
  58. return m_page_host->page();
  59. }
  60. OwnPtr<Messages::WebContentServer::GreetResponse> ClientConnection::handle(const Messages::WebContentServer::Greet& message)
  61. {
  62. set_client_pid(message.client_pid());
  63. return make<Messages::WebContentServer::GreetResponse>(client_id(), getpid());
  64. }
  65. void ClientConnection::handle(const Messages::WebContentServer::UpdateSystemTheme& message)
  66. {
  67. Gfx::set_system_theme(message.theme_buffer());
  68. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(message.theme_buffer());
  69. m_page_host->set_palette_impl(*impl);
  70. }
  71. void ClientConnection::handle(const Messages::WebContentServer::LoadURL& message)
  72. {
  73. dbgln<SPAM_DEBUG>("handle: WebContentServer::LoadURL: url={}", message.url());
  74. String process_name;
  75. if (message.url().host().is_empty())
  76. process_name = "WebContent";
  77. else
  78. process_name = String::formatted("WebContent: {}", message.url().host());
  79. pthread_setname_np(pthread_self(), process_name.characters());
  80. page().load(message.url());
  81. }
  82. void ClientConnection::handle(const Messages::WebContentServer::LoadHTML& message)
  83. {
  84. dbgln<SPAM_DEBUG>("handle: WebContentServer::LoadHTML: html={}, url={}", message.html(), message.url());
  85. page().load_html(message.html(), message.url());
  86. }
  87. void ClientConnection::handle(const Messages::WebContentServer::SetViewportRect& message)
  88. {
  89. dbgln<SPAM_DEBUG>("handle: WebContentServer::SetViewportRect: rect={}", message.rect());
  90. m_page_host->set_viewport_rect(message.rect());
  91. }
  92. void ClientConnection::handle(const Messages::WebContentServer::AddBackingStore& message)
  93. {
  94. m_backing_stores.set(message.backing_store_id(), *message.bitmap().bitmap());
  95. }
  96. void ClientConnection::handle(const Messages::WebContentServer::RemoveBackingStore& message)
  97. {
  98. m_backing_stores.remove(message.backing_store_id());
  99. }
  100. void ClientConnection::handle(const Messages::WebContentServer::Paint& message)
  101. {
  102. for (auto& pending_paint : m_pending_paint_requests) {
  103. if (pending_paint.bitmap_id == message.backing_store_id()) {
  104. pending_paint.content_rect = message.content_rect();
  105. return;
  106. }
  107. }
  108. auto it = m_backing_stores.find(message.backing_store_id());
  109. if (it == m_backing_stores.end()) {
  110. did_misbehave("Client requested paint with backing store ID");
  111. return;
  112. }
  113. auto& bitmap = *it->value;
  114. m_pending_paint_requests.append({ message.content_rect(), bitmap, message.backing_store_id() });
  115. m_paint_flush_timer->start();
  116. }
  117. void ClientConnection::flush_pending_paint_requests()
  118. {
  119. for (auto& pending_paint : m_pending_paint_requests) {
  120. m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap);
  121. post_message(Messages::WebContentClient::DidPaint(pending_paint.content_rect, pending_paint.bitmap_id));
  122. }
  123. m_pending_paint_requests.clear();
  124. }
  125. void ClientConnection::handle(const Messages::WebContentServer::MouseDown& message)
  126. {
  127. page().handle_mousedown(message.position(), message.button(), message.modifiers());
  128. }
  129. void ClientConnection::handle(const Messages::WebContentServer::MouseMove& message)
  130. {
  131. page().handle_mousemove(message.position(), message.buttons(), message.modifiers());
  132. }
  133. void ClientConnection::handle(const Messages::WebContentServer::MouseUp& message)
  134. {
  135. page().handle_mouseup(message.position(), message.button(), message.modifiers());
  136. }
  137. void ClientConnection::handle(const Messages::WebContentServer::KeyDown& message)
  138. {
  139. page().handle_keydown((KeyCode)message.key(), message.modifiers(), message.code_point());
  140. }
  141. }