ClientConnection.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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(Core::LocalSocket& socket, int client_id)
  36. : IPC::ClientConnection<WebContentServerEndpoint>(*this, 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. }
  49. Web::Page& ClientConnection::page()
  50. {
  51. return m_page_host->page();
  52. }
  53. const Web::Page& ClientConnection::page() const
  54. {
  55. return m_page_host->page();
  56. }
  57. OwnPtr<Messages::WebContentServer::GreetResponse> ClientConnection::handle(const Messages::WebContentServer::Greet& message)
  58. {
  59. set_client_pid(message.client_pid());
  60. return make<Messages::WebContentServer::GreetResponse>(client_id(), getpid());
  61. }
  62. void ClientConnection::handle(const Messages::WebContentServer::UpdateSystemTheme& message)
  63. {
  64. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  65. if (!shared_buffer) {
  66. dbg() << "WebContentServer::UpdateSystemTheme: SharedBuffer already gone! Ignoring :^)";
  67. return;
  68. }
  69. Gfx::set_system_theme(*shared_buffer);
  70. auto impl = Gfx::PaletteImpl::create_with_shared_buffer(*shared_buffer);
  71. m_page_host->set_palette_impl(*impl);
  72. }
  73. void ClientConnection::handle(const Messages::WebContentServer::LoadURL& message)
  74. {
  75. #ifdef DEBUG_SPAM
  76. dbg() << "handle: WebContentServer::LoadURL: url=" << message.url();
  77. #endif
  78. page().load(message.url());
  79. }
  80. void ClientConnection::handle(const Messages::WebContentServer::SetViewportRect& message)
  81. {
  82. #ifdef DEBUG_SPAM
  83. dbg() << "handle: WebContentServer::SetViewportRect: rect=" << message.rect();
  84. #endif
  85. m_page_host->set_viewport_rect(message.rect());
  86. }
  87. void ClientConnection::handle(const Messages::WebContentServer::Paint& message)
  88. {
  89. #ifdef DEBUG_SPAM
  90. dbg() << "handle: WebContentServer::Paint: content_rect=" << message.content_rect() << ", shbuf_id=" << message.shbuf_id();
  91. #endif
  92. for (auto& pending_paint : m_pending_paint_requests) {
  93. if (pending_paint.bitmap->shbuf_id() == message.shbuf_id()) {
  94. pending_paint.content_rect = message.content_rect();
  95. return;
  96. }
  97. }
  98. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  99. if (!shared_buffer) {
  100. #ifdef DEBUG_SPAM
  101. dbg() << "WebContentServer::Paint: SharedBuffer already gone! Ignoring :^)";
  102. #endif
  103. return;
  104. }
  105. auto shared_bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGB32, shared_buffer.release_nonnull(), message.content_rect().size());
  106. if (!shared_bitmap) {
  107. did_misbehave("WebContentServer::Paint: Cannot create Gfx::Bitmap wrapper around SharedBuffer");
  108. return;
  109. }
  110. m_pending_paint_requests.append({ message.content_rect(), shared_bitmap.release_nonnull() });
  111. m_paint_flush_timer->start();
  112. }
  113. void ClientConnection::flush_pending_paint_requests()
  114. {
  115. for (auto& pending_paint : m_pending_paint_requests) {
  116. m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap);
  117. post_message(Messages::WebContentClient::DidPaint(pending_paint.content_rect, pending_paint.bitmap->shbuf_id()));
  118. }
  119. m_pending_paint_requests.clear();
  120. }
  121. void ClientConnection::handle(const Messages::WebContentServer::MouseDown& message)
  122. {
  123. page().handle_mousedown(message.position(), message.button(), message.modifiers());
  124. }
  125. void ClientConnection::handle(const Messages::WebContentServer::MouseMove& message)
  126. {
  127. page().handle_mousemove(message.position(), message.buttons(), message.modifiers());
  128. }
  129. void ClientConnection::handle(const Messages::WebContentServer::MouseUp& message)
  130. {
  131. page().handle_mouseup(message.position(), message.button(), message.modifiers());
  132. }
  133. }