PageHost.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "PageHost.h"
  27. #include "ClientConnection.h"
  28. #include <AK/SharedBuffer.h>
  29. #include <LibGfx/Painter.h>
  30. #include <LibGfx/SystemTheme.h>
  31. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  32. #include <LibWeb/Page/Frame.h>
  33. #include <WebContent/WebContentClientEndpoint.h>
  34. namespace WebContent {
  35. PageHost::PageHost(ClientConnection& client)
  36. : m_client(client)
  37. , m_page(make<Web::Page>(*this))
  38. {
  39. setup_palette();
  40. }
  41. PageHost::~PageHost()
  42. {
  43. }
  44. void PageHost::setup_palette()
  45. {
  46. // FIXME: Get the proper palette from our peer somehow
  47. auto buffer = Core::AnonymousBuffer::create_with_size(sizeof(Gfx::SystemTheme));
  48. auto* theme = buffer.data<Gfx::SystemTheme>();
  49. theme->color[(int)Gfx::ColorRole::Window] = Color::Magenta;
  50. theme->color[(int)Gfx::ColorRole::WindowText] = Color::Cyan;
  51. m_palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
  52. }
  53. Gfx::Palette PageHost::palette() const
  54. {
  55. return Gfx::Palette(*m_palette_impl);
  56. }
  57. void PageHost::set_palette_impl(const Gfx::PaletteImpl& impl)
  58. {
  59. m_palette_impl = impl;
  60. }
  61. Web::Layout::InitialContainingBlockBox* PageHost::layout_root()
  62. {
  63. auto* document = page().main_frame().document();
  64. if (!document)
  65. return nullptr;
  66. return document->layout_node();
  67. }
  68. void PageHost::paint(const Gfx::IntRect& content_rect, Gfx::Bitmap& target)
  69. {
  70. Gfx::Painter painter(target);
  71. Gfx::IntRect bitmap_rect { {}, content_rect.size() };
  72. auto* layout_root = this->layout_root();
  73. if (!layout_root) {
  74. painter.fill_rect(bitmap_rect, Color::White);
  75. return;
  76. }
  77. painter.fill_rect(bitmap_rect, layout_root->document().background_color(palette()));
  78. if (auto background_bitmap = layout_root->document().background_image()) {
  79. painter.draw_tiled_bitmap(bitmap_rect, *background_bitmap);
  80. }
  81. painter.translate(-content_rect.x(), -content_rect.y());
  82. Web::PaintContext context(painter, palette(), Gfx::IntPoint());
  83. context.set_viewport_rect(content_rect);
  84. layout_root->paint_all_phases(context);
  85. }
  86. void PageHost::set_viewport_rect(const Gfx::IntRect& rect)
  87. {
  88. page().main_frame().set_size(rect.size());
  89. if (page().main_frame().document())
  90. page().main_frame().document()->update_layout();
  91. page().main_frame().set_viewport_scroll_offset(rect.location());
  92. }
  93. void PageHost::page_did_invalidate(const Gfx::IntRect& content_rect)
  94. {
  95. m_client.post_message(Messages::WebContentClient::DidInvalidateContentRect(content_rect));
  96. }
  97. void PageHost::page_did_change_selection()
  98. {
  99. m_client.post_message(Messages::WebContentClient::DidChangeSelection());
  100. }
  101. void PageHost::page_did_layout()
  102. {
  103. auto* layout_root = this->layout_root();
  104. ASSERT(layout_root);
  105. auto content_size = enclosing_int_rect(layout_root->absolute_rect()).size();
  106. m_client.post_message(Messages::WebContentClient::DidLayout(content_size));
  107. }
  108. void PageHost::page_did_change_title(const String& title)
  109. {
  110. m_client.post_message(Messages::WebContentClient::DidChangeTitle(title));
  111. }
  112. void PageHost::page_did_request_scroll_into_view(const Gfx::IntRect& rect)
  113. {
  114. m_client.post_message(Messages::WebContentClient::DidRequestScrollIntoView(rect));
  115. }
  116. void PageHost::page_did_hover_link(const URL& url)
  117. {
  118. m_client.post_message(Messages::WebContentClient::DidHoverLink(url));
  119. }
  120. void PageHost::page_did_unhover_link()
  121. {
  122. m_client.post_message(Messages::WebContentClient::DidUnhoverLink());
  123. }
  124. void PageHost::page_did_click_link(const URL& url, const String& target, unsigned modifiers)
  125. {
  126. m_client.post_message(Messages::WebContentClient::DidClickLink(url, target, modifiers));
  127. }
  128. void PageHost::page_did_middle_click_link(const URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
  129. {
  130. m_client.post_message(Messages::WebContentClient::DidMiddleClickLink(url, target, modifiers));
  131. }
  132. void PageHost::page_did_start_loading(const URL& url)
  133. {
  134. m_client.post_message(Messages::WebContentClient::DidStartLoading(url));
  135. }
  136. void PageHost::page_did_finish_loading(const URL& url)
  137. {
  138. m_client.post_message(Messages::WebContentClient::DidFinishLoading(url));
  139. }
  140. void PageHost::page_did_request_context_menu(const Gfx::IntPoint& content_position)
  141. {
  142. m_client.post_message(Messages::WebContentClient::DidRequestContextMenu(content_position));
  143. }
  144. void PageHost::page_did_request_link_context_menu(const Gfx::IntPoint& content_position, const URL& url, const String& target, unsigned modifiers)
  145. {
  146. m_client.post_message(Messages::WebContentClient::DidRequestLinkContextMenu(content_position, url, target, modifiers));
  147. }
  148. void PageHost::page_did_request_alert(const String& message)
  149. {
  150. m_client.send_sync<Messages::WebContentClient::DidRequestAlert>(message);
  151. }
  152. }