ClientConnection.cpp 5.7 KB

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