ClientConnection.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <AK/SharedBuffer.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. namespace WebContent {
  34. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  35. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  36. : IPC::ClientConnection<WebContentClientEndpoint, WebContentServerEndpoint>(*this, move(socket), client_id)
  37. , m_page_host(PageHost::create(*this))
  38. {
  39. s_connections.set(client_id, *this);
  40. m_paint_flush_timer = Core::Timer::create_single_shot(0, [this] { flush_pending_paint_requests(); });
  41. }
  42. ClientConnection::~ClientConnection()
  43. {
  44. }
  45. void ClientConnection::die()
  46. {
  47. s_connections.remove(client_id());
  48. if (s_connections.is_empty())
  49. Core::EventLoop::current().quit(0);
  50. }
  51. Web::Page& ClientConnection::page()
  52. {
  53. return m_page_host->page();
  54. }
  55. const Web::Page& ClientConnection::page() const
  56. {
  57. return m_page_host->page();
  58. }
  59. OwnPtr<Messages::WebContentServer::GreetResponse> ClientConnection::handle(const Messages::WebContentServer::Greet& message)
  60. {
  61. set_client_pid(message.client_pid());
  62. return make<Messages::WebContentServer::GreetResponse>(client_id(), getpid());
  63. }
  64. void ClientConnection::handle(const Messages::WebContentServer::UpdateSystemTheme& message)
  65. {
  66. Gfx::set_system_theme(message.theme_buffer());
  67. auto impl = Gfx::PaletteImpl::create_with_anonymous_buffer(message.theme_buffer());
  68. m_page_host->set_palette_impl(*impl);
  69. }
  70. void ClientConnection::handle(const Messages::WebContentServer::LoadURL& message)
  71. {
  72. #ifdef DEBUG_SPAM
  73. dbg() << "handle: WebContentServer::LoadURL: url=" << message.url();
  74. #endif
  75. page().load(message.url());
  76. }
  77. void ClientConnection::handle(const Messages::WebContentServer::LoadHTML& message)
  78. {
  79. #ifdef DEBUG_SPAM
  80. dbg() << "handle: WebContentServer::LoadHTML: html=" << message.html() << ", url=" << message.url();
  81. #endif
  82. page().load_html(message.html(), message.url());
  83. }
  84. void ClientConnection::handle(const Messages::WebContentServer::SetViewportRect& message)
  85. {
  86. #ifdef DEBUG_SPAM
  87. dbg() << "handle: WebContentServer::SetViewportRect: rect=" << message.rect();
  88. #endif
  89. m_page_host->set_viewport_rect(message.rect());
  90. }
  91. void ClientConnection::handle(const Messages::WebContentServer::Paint& message)
  92. {
  93. #ifdef DEBUG_SPAM
  94. dbg() << "handle: WebContentServer::Paint: content_rect=" << message.content_rect() << ", shbuf_id=" << message.shbuf_id();
  95. #endif
  96. for (auto& pending_paint : m_pending_paint_requests) {
  97. if (pending_paint.bitmap->shbuf_id() == message.shbuf_id()) {
  98. pending_paint.content_rect = message.content_rect();
  99. return;
  100. }
  101. }
  102. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  103. if (!shared_buffer) {
  104. #ifdef DEBUG_SPAM
  105. dbgln("WebContentServer::Paint: SharedBuffer already gone! Ignoring :^)");
  106. #endif
  107. return;
  108. }
  109. auto shared_bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGB32, shared_buffer.release_nonnull(), message.content_rect().size());
  110. if (!shared_bitmap) {
  111. did_misbehave("WebContentServer::Paint: Cannot create Gfx::Bitmap wrapper around SharedBuffer");
  112. return;
  113. }
  114. m_pending_paint_requests.append({ message.content_rect(), shared_bitmap.release_nonnull() });
  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->shbuf_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. }