ClientConnection.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }
  41. ClientConnection::~ClientConnection()
  42. {
  43. }
  44. void ClientConnection::die()
  45. {
  46. s_connections.remove(client_id());
  47. }
  48. Web::Page& ClientConnection::page()
  49. {
  50. return m_page_host->page();
  51. }
  52. const Web::Page& ClientConnection::page() const
  53. {
  54. return m_page_host->page();
  55. }
  56. OwnPtr<Messages::WebContentServer::GreetResponse> ClientConnection::handle(const Messages::WebContentServer::Greet& message)
  57. {
  58. set_client_pid(message.client_pid());
  59. return make<Messages::WebContentServer::GreetResponse>(client_id(), getpid());
  60. }
  61. void ClientConnection::handle(const Messages::WebContentServer::UpdateSystemTheme& message)
  62. {
  63. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  64. if (!shared_buffer) {
  65. dbg() << "WebContentServer::UpdateSystemTheme: SharedBuffer already gone! Ignoring :^)";
  66. return;
  67. }
  68. Gfx::set_system_theme(*shared_buffer);
  69. auto impl = Gfx::PaletteImpl::create_with_shared_buffer(*shared_buffer);
  70. m_page_host->set_palette_impl(*impl);
  71. }
  72. void ClientConnection::handle(const Messages::WebContentServer::LoadURL& message)
  73. {
  74. #ifdef DEBUG_SPAM
  75. dbg() << "handle: WebContentServer::LoadURL: url=" << message.url();
  76. #endif
  77. page().load(message.url());
  78. }
  79. void ClientConnection::handle(const Messages::WebContentServer::SetViewportRect& message)
  80. {
  81. #ifdef DEBUG_SPAM
  82. dbg() << "handle: WebContentServer::SetViewportRect: rect=" << message.rect();
  83. #endif
  84. m_page_host->set_viewport_rect(message.rect());
  85. }
  86. void ClientConnection::handle(const Messages::WebContentServer::Paint& message)
  87. {
  88. #ifdef DEBUG_SPAM
  89. dbg() << "handle: WebContentServer::Paint: content_rect=" << message.content_rect() << ", shbuf_id=" << message.shbuf_id();
  90. #endif
  91. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  92. if (!shared_buffer) {
  93. dbg() << "WebContentServer::Paint: SharedBuffer already gone! Ignoring :^)";
  94. return;
  95. }
  96. auto shared_bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGB32, shared_buffer.release_nonnull(), message.content_rect().size());
  97. if (!shared_bitmap) {
  98. did_misbehave("WebContentServer::Paint: Cannot create Gfx::Bitmap wrapper around SharedBuffer");
  99. return;
  100. }
  101. m_page_host->paint(message.content_rect(), *shared_bitmap);
  102. post_message(Messages::WebContentClient::DidPaint(message.content_rect(), message.shbuf_id()));
  103. }
  104. void ClientConnection::handle(const Messages::WebContentServer::MouseDown& message)
  105. {
  106. page().handle_mousedown(message.position(), message.button(), message.modifiers());
  107. }
  108. void ClientConnection::handle(const Messages::WebContentServer::MouseMove& message)
  109. {
  110. page().handle_mousemove(message.position(), message.buttons(), message.modifiers());
  111. }
  112. void ClientConnection::handle(const Messages::WebContentServer::MouseUp& message)
  113. {
  114. page().handle_mouseup(message.position(), message.button(), message.modifiers());
  115. }
  116. }