PageHost.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2020-2021, 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 <LibGfx/Painter.h>
  29. #include <LibGfx/SystemTheme.h>
  30. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  31. #include <LibWeb/Page/Frame.h>
  32. #include <WebContent/WebContentClientEndpoint.h>
  33. namespace WebContent {
  34. PageHost::PageHost(ClientConnection& client)
  35. : m_client(client)
  36. , m_page(make<Web::Page>(*this))
  37. {
  38. setup_palette();
  39. }
  40. PageHost::~PageHost()
  41. {
  42. }
  43. void PageHost::setup_palette()
  44. {
  45. // FIXME: Get the proper palette from our peer somehow
  46. auto buffer = Core::AnonymousBuffer::create_with_size(sizeof(Gfx::SystemTheme));
  47. auto* theme = buffer.data<Gfx::SystemTheme>();
  48. theme->color[(int)Gfx::ColorRole::Window] = Color::Magenta;
  49. theme->color[(int)Gfx::ColorRole::WindowText] = Color::Cyan;
  50. m_palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
  51. }
  52. Gfx::Palette PageHost::palette() const
  53. {
  54. return Gfx::Palette(*m_palette_impl);
  55. }
  56. void PageHost::set_palette_impl(const Gfx::PaletteImpl& impl)
  57. {
  58. m_palette_impl = impl;
  59. }
  60. Web::Layout::InitialContainingBlockBox* PageHost::layout_root()
  61. {
  62. auto* document = page().main_frame().document();
  63. if (!document)
  64. return nullptr;
  65. return document->layout_node();
  66. }
  67. void PageHost::paint(const Gfx::IntRect& content_rect, Gfx::Bitmap& target)
  68. {
  69. Gfx::Painter painter(target);
  70. Gfx::IntRect bitmap_rect { {}, content_rect.size() };
  71. auto* layout_root = this->layout_root();
  72. if (!layout_root) {
  73. painter.fill_rect(bitmap_rect, Color::White);
  74. return;
  75. }
  76. Web::PaintContext context(painter, palette(), content_rect.top_left());
  77. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  78. context.set_viewport_rect(content_rect);
  79. layout_root->paint_all_phases(context);
  80. }
  81. void PageHost::set_viewport_rect(const Gfx::IntRect& rect)
  82. {
  83. page().main_frame().set_viewport_rect(rect);
  84. }
  85. void PageHost::page_did_invalidate(const Gfx::IntRect& content_rect)
  86. {
  87. m_client.post_message(Messages::WebContentClient::DidInvalidateContentRect(content_rect));
  88. }
  89. void PageHost::page_did_change_selection()
  90. {
  91. m_client.post_message(Messages::WebContentClient::DidChangeSelection());
  92. }
  93. void PageHost::page_did_request_cursor_change(Gfx::StandardCursor cursor)
  94. {
  95. m_client.post_message(Messages::WebContentClient::DidRequestCursorChange((u32)cursor));
  96. }
  97. void PageHost::page_did_layout()
  98. {
  99. auto* layout_root = this->layout_root();
  100. VERIFY(layout_root);
  101. auto content_size = enclosing_int_rect(layout_root->absolute_rect()).size();
  102. m_client.post_message(Messages::WebContentClient::DidLayout(content_size));
  103. }
  104. void PageHost::page_did_change_title(const String& title)
  105. {
  106. m_client.post_message(Messages::WebContentClient::DidChangeTitle(title));
  107. }
  108. void PageHost::page_did_request_scroll(int wheel_delta)
  109. {
  110. m_client.post_message(Messages::WebContentClient::DidRequestScroll(wheel_delta));
  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_enter_tooltip_area(const Gfx::IntPoint& content_position, const String& title)
  117. {
  118. m_client.post_message(Messages::WebContentClient::DidEnterTooltipArea(content_position, title));
  119. }
  120. void PageHost::page_did_leave_tooltip_area()
  121. {
  122. m_client.post_message(Messages::WebContentClient::DidLeaveTooltipArea());
  123. }
  124. void PageHost::page_did_hover_link(const URL& url)
  125. {
  126. m_client.post_message(Messages::WebContentClient::DidHoverLink(url));
  127. }
  128. void PageHost::page_did_unhover_link()
  129. {
  130. m_client.post_message(Messages::WebContentClient::DidUnhoverLink());
  131. }
  132. void PageHost::page_did_click_link(const URL& url, const String& target, unsigned modifiers)
  133. {
  134. m_client.post_message(Messages::WebContentClient::DidClickLink(url, target, modifiers));
  135. }
  136. void PageHost::page_did_middle_click_link(const URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
  137. {
  138. m_client.post_message(Messages::WebContentClient::DidMiddleClickLink(url, target, modifiers));
  139. }
  140. void PageHost::page_did_start_loading(const URL& url)
  141. {
  142. m_client.post_message(Messages::WebContentClient::DidStartLoading(url));
  143. }
  144. void PageHost::page_did_finish_loading(const URL& url)
  145. {
  146. m_client.post_message(Messages::WebContentClient::DidFinishLoading(url));
  147. }
  148. void PageHost::page_did_request_context_menu(const Gfx::IntPoint& content_position)
  149. {
  150. m_client.post_message(Messages::WebContentClient::DidRequestContextMenu(content_position));
  151. }
  152. void PageHost::page_did_request_link_context_menu(const Gfx::IntPoint& content_position, const URL& url, const String& target, unsigned modifiers)
  153. {
  154. m_client.post_message(Messages::WebContentClient::DidRequestLinkContextMenu(content_position, url, target, modifiers));
  155. }
  156. void PageHost::page_did_request_alert(const String& message)
  157. {
  158. m_client.send_sync<Messages::WebContentClient::DidRequestAlert>(message);
  159. }
  160. bool PageHost::page_did_request_confirm(const String& message)
  161. {
  162. return m_client.send_sync<Messages::WebContentClient::DidRequestConfirm>(message)->result();
  163. }
  164. String PageHost::page_did_request_prompt(const String& message, const String& default_)
  165. {
  166. return m_client.send_sync<Messages::WebContentClient::DidRequestPrompt>(message, default_)->response();
  167. }
  168. void PageHost::page_did_change_favicon(const Gfx::Bitmap& favicon)
  169. {
  170. m_client.post_message(Messages::WebContentClient::DidChangeFavicon(favicon.to_shareable_bitmap()));
  171. }
  172. void PageHost::page_did_request_image_context_menu(const Gfx::IntPoint& content_position, const URL& url, const String& target, unsigned modifiers, const Gfx::Bitmap* bitmap)
  173. {
  174. m_client.post_message(Messages::WebContentClient::DidRequestImageContextMenu(content_position, url, target, modifiers, bitmap->to_shareable_bitmap()));
  175. }
  176. String PageHost::page_did_request_cookie(const URL& url, Web::Cookie::Source source)
  177. {
  178. return m_client.send_sync<Messages::WebContentClient::DidRequestCookie>(url, static_cast<u8>(source))->cookie();
  179. }
  180. void PageHost::page_did_set_cookie(const URL& url, const String& cookie, Web::Cookie::Source source)
  181. {
  182. m_client.post_message(Messages::WebContentClient::DidSetCookie(url, cookie, static_cast<u8>(source)));
  183. }
  184. }