InProcessWebView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/URL.h>
  7. #include <LibCore/MimeData.h>
  8. #include <LibGUI/Application.h>
  9. #include <LibGUI/InputBox.h>
  10. #include <LibGUI/MessageBox.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Scrollbar.h>
  13. #include <LibGUI/Window.h>
  14. #include <LibGfx/ShareableBitmap.h>
  15. #include <LibWeb/HTML/BrowsingContext.h>
  16. #include <LibWeb/HTML/HTMLAnchorElement.h>
  17. #include <LibWeb/HTML/Parser/HTMLParser.h>
  18. #include <LibWeb/InProcessWebView.h>
  19. #include <LibWeb/Layout/InitialContainingBlock.h>
  20. #include <LibWeb/Layout/TextNode.h>
  21. #include <LibWeb/Loader/ResourceLoader.h>
  22. #include <LibWeb/Page/EventHandler.h>
  23. #include <LibWeb/Painting/PaintContext.h>
  24. #include <LibWeb/Painting/PaintableBox.h>
  25. #include <LibWeb/UIEvents/MouseEvent.h>
  26. REGISTER_WIDGET(Web, InProcessWebView)
  27. namespace Web {
  28. InProcessWebView::InProcessWebView()
  29. : m_page(make<Page>(*this))
  30. {
  31. set_should_hide_unnecessary_scrollbars(true);
  32. set_background_role(ColorRole::Base);
  33. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  34. }
  35. InProcessWebView::~InProcessWebView() = default;
  36. void InProcessWebView::select_all()
  37. {
  38. page().focused_context().select_all();
  39. update();
  40. }
  41. String InProcessWebView::selected_text() const
  42. {
  43. return page().focused_context().selected_text();
  44. }
  45. void InProcessWebView::set_preferred_color_scheme(CSS::PreferredColorScheme color_scheme)
  46. {
  47. m_preferred_color_scheme = color_scheme;
  48. if (auto* document = page().top_level_browsing_context().active_document())
  49. document->invalidate_style();
  50. }
  51. void InProcessWebView::page_did_layout()
  52. {
  53. VERIFY(layout_root());
  54. VERIFY(layout_root()->paint_box());
  55. set_content_size(layout_root()->paint_box()->content_size().to_type<int>());
  56. }
  57. void InProcessWebView::page_did_change_title(const String& title)
  58. {
  59. if (on_title_change)
  60. on_title_change(title);
  61. }
  62. void InProcessWebView::page_did_set_document_in_top_level_browsing_context(DOM::Document* document)
  63. {
  64. if (on_set_document)
  65. on_set_document(document);
  66. layout_and_sync_size();
  67. scroll_to_top();
  68. update();
  69. }
  70. void InProcessWebView::page_did_start_loading(const AK::URL& url)
  71. {
  72. if (on_load_start)
  73. on_load_start(url);
  74. }
  75. void InProcessWebView::page_did_finish_loading(const AK::URL& url)
  76. {
  77. if (on_load_finish)
  78. on_load_finish(url);
  79. }
  80. void InProcessWebView::page_did_change_selection()
  81. {
  82. update();
  83. }
  84. void InProcessWebView::page_did_request_cursor_change(Gfx::StandardCursor cursor)
  85. {
  86. set_override_cursor(cursor);
  87. }
  88. void InProcessWebView::page_did_request_context_menu(const Gfx::IntPoint& content_position)
  89. {
  90. if (on_context_menu_request)
  91. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  92. }
  93. void InProcessWebView::page_did_request_link_context_menu(const Gfx::IntPoint& content_position, const AK::URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
  94. {
  95. if (on_link_context_menu_request)
  96. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  97. }
  98. void InProcessWebView::page_did_request_image_context_menu(const Gfx::IntPoint& content_position, const AK::URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers, const Gfx::Bitmap* bitmap)
  99. {
  100. if (!on_image_context_menu_request)
  101. return;
  102. Gfx::ShareableBitmap shareable_bitmap;
  103. if (bitmap)
  104. shareable_bitmap = bitmap->to_shareable_bitmap();
  105. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), move(shareable_bitmap));
  106. }
  107. void InProcessWebView::page_did_click_link(const AK::URL& url, const String& target, unsigned modifiers)
  108. {
  109. if (on_link_click)
  110. on_link_click(url, target, modifiers);
  111. }
  112. void InProcessWebView::page_did_middle_click_link(const AK::URL& url, const String& target, unsigned modifiers)
  113. {
  114. if (on_link_middle_click)
  115. on_link_middle_click(url, target, modifiers);
  116. }
  117. void InProcessWebView::page_did_enter_tooltip_area([[maybe_unused]] const Gfx::IntPoint& content_position, const String& title)
  118. {
  119. GUI::Application::the()->show_tooltip(title, nullptr);
  120. }
  121. void InProcessWebView::page_did_leave_tooltip_area()
  122. {
  123. GUI::Application::the()->hide_tooltip();
  124. }
  125. void InProcessWebView::page_did_hover_link(const AK::URL& url)
  126. {
  127. if (on_link_hover)
  128. on_link_hover(url);
  129. }
  130. void InProcessWebView::page_did_unhover_link()
  131. {
  132. if (on_link_hover)
  133. on_link_hover({});
  134. }
  135. void InProcessWebView::page_did_invalidate(const Gfx::IntRect&)
  136. {
  137. update();
  138. }
  139. void InProcessWebView::page_did_change_favicon(const Gfx::Bitmap& bitmap)
  140. {
  141. if (on_favicon_change)
  142. on_favicon_change(bitmap);
  143. }
  144. void InProcessWebView::layout_and_sync_size()
  145. {
  146. if (!document())
  147. return;
  148. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  149. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  150. page().top_level_browsing_context().set_size(available_size());
  151. set_content_size(layout_root()->paint_box()->content_size().to_type<int>());
  152. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  153. // since the scrollbars now take up some of the available space.
  154. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  155. page().top_level_browsing_context().set_size(available_size());
  156. set_content_size(layout_root()->paint_box()->content_size().to_type<int>());
  157. }
  158. page().top_level_browsing_context().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() });
  159. }
  160. void InProcessWebView::resize_event(GUI::ResizeEvent& event)
  161. {
  162. GUI::AbstractScrollableWidget::resize_event(event);
  163. layout_and_sync_size();
  164. }
  165. void InProcessWebView::paint_event(GUI::PaintEvent& event)
  166. {
  167. GUI::Frame::paint_event(event);
  168. GUI::Painter painter(*this);
  169. painter.add_clip_rect(widget_inner_rect());
  170. painter.add_clip_rect(event.rect());
  171. if (!layout_root()) {
  172. painter.fill_rect(event.rect(), palette().color(background_role()));
  173. return;
  174. }
  175. painter.translate(frame_thickness(), frame_thickness());
  176. PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() });
  177. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  178. context.set_viewport_rect(viewport_rect_in_content_coordinates());
  179. context.set_has_focus(is_focused());
  180. layout_root()->paint_all_phases(context);
  181. }
  182. void InProcessWebView::mousemove_event(GUI::MouseEvent& event)
  183. {
  184. page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers());
  185. GUI::AbstractScrollableWidget::mousemove_event(event);
  186. }
  187. void InProcessWebView::mousedown_event(GUI::MouseEvent& event)
  188. {
  189. page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers());
  190. GUI::AbstractScrollableWidget::mousedown_event(event);
  191. }
  192. void InProcessWebView::mouseup_event(GUI::MouseEvent& event)
  193. {
  194. page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers());
  195. GUI::AbstractScrollableWidget::mouseup_event(event);
  196. }
  197. void InProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  198. {
  199. page().handle_mousewheel(to_content_position(event.position()), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y());
  200. GUI::AbstractScrollableWidget::mousewheel_event(event);
  201. }
  202. void InProcessWebView::keydown_event(GUI::KeyEvent& event)
  203. {
  204. bool page_accepted_event = page().handle_keydown(event.key(), event.modifiers(), event.code_point());
  205. if (event.modifiers() == 0) {
  206. switch (event.key()) {
  207. case Key_Home:
  208. vertical_scrollbar().set_value(0);
  209. break;
  210. case Key_End:
  211. vertical_scrollbar().set_value(vertical_scrollbar().max());
  212. break;
  213. case Key_Down:
  214. vertical_scrollbar().increase_slider_by_steps(1);
  215. break;
  216. case Key_Up:
  217. vertical_scrollbar().decrease_slider_by_steps(1);
  218. break;
  219. case Key_Left:
  220. horizontal_scrollbar().increase_slider_by_steps(1);
  221. break;
  222. case Key_Right:
  223. horizontal_scrollbar().decrease_slider_by_steps(1);
  224. break;
  225. case Key_PageDown:
  226. vertical_scrollbar().increase_slider_by(frame_inner_rect().height());
  227. break;
  228. case Key_PageUp:
  229. vertical_scrollbar().decrease_slider_by(frame_inner_rect().height());
  230. break;
  231. default:
  232. if (!page_accepted_event) {
  233. AbstractScrollableWidget::keydown_event(event);
  234. return;
  235. }
  236. break;
  237. }
  238. }
  239. event.accept();
  240. }
  241. AK::URL InProcessWebView::url() const
  242. {
  243. if (!page().top_level_browsing_context().active_document())
  244. return {};
  245. return page().top_level_browsing_context().active_document()->url();
  246. }
  247. void InProcessWebView::reload()
  248. {
  249. load(url());
  250. }
  251. void InProcessWebView::load_html(StringView html, const AK::URL& url)
  252. {
  253. page().top_level_browsing_context().loader().load_html(html, url);
  254. }
  255. bool InProcessWebView::load(const AK::URL& url)
  256. {
  257. set_override_cursor(Gfx::StandardCursor::None);
  258. return page().top_level_browsing_context().loader().load(url, FrameLoader::Type::Navigation);
  259. }
  260. const Layout::InitialContainingBlock* InProcessWebView::layout_root() const
  261. {
  262. return document() ? document()->layout_node() : nullptr;
  263. }
  264. Layout::InitialContainingBlock* InProcessWebView::layout_root()
  265. {
  266. if (!document())
  267. return nullptr;
  268. return const_cast<Layout::InitialContainingBlock*>(document()->layout_node());
  269. }
  270. void InProcessWebView::page_did_request_scroll_into_view(const Gfx::IntRect& rect)
  271. {
  272. scroll_into_view(rect, true, true);
  273. set_override_cursor(Gfx::StandardCursor::None);
  274. }
  275. void InProcessWebView::load_empty_document()
  276. {
  277. page().top_level_browsing_context().set_active_document(nullptr);
  278. }
  279. DOM::Document* InProcessWebView::document()
  280. {
  281. return page().top_level_browsing_context().active_document();
  282. }
  283. const DOM::Document* InProcessWebView::document() const
  284. {
  285. return page().top_level_browsing_context().active_document();
  286. }
  287. void InProcessWebView::set_document(DOM::Document* document)
  288. {
  289. page().top_level_browsing_context().set_active_document(document);
  290. }
  291. void InProcessWebView::did_scroll()
  292. {
  293. page().top_level_browsing_context().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() });
  294. }
  295. void InProcessWebView::drop_event(GUI::DropEvent& event)
  296. {
  297. if (event.mime_data().has_urls()) {
  298. if (on_url_drop) {
  299. on_url_drop(event.mime_data().urls().first());
  300. return;
  301. }
  302. }
  303. AbstractScrollableWidget::drop_event(event);
  304. }
  305. void InProcessWebView::page_did_request_alert(const String& message)
  306. {
  307. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  308. }
  309. bool InProcessWebView::page_did_request_confirm(const String& message)
  310. {
  311. auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  312. return confirm_result == GUI::Dialog::ExecResult::ExecOK;
  313. }
  314. String InProcessWebView::page_did_request_prompt(const String& message, const String& default_)
  315. {
  316. String value { default_ };
  317. if (GUI::InputBox::show(window(), value, message, "Prompt") == GUI::InputBox::ExecOK)
  318. return value;
  319. return {};
  320. }
  321. String InProcessWebView::page_did_request_cookie(const AK::URL& url, Cookie::Source source)
  322. {
  323. if (on_get_cookie)
  324. return on_get_cookie(url, source);
  325. return {};
  326. }
  327. void InProcessWebView::page_did_set_cookie(const AK::URL& url, const Cookie::ParsedCookie& cookie, Cookie::Source source)
  328. {
  329. if (on_set_cookie)
  330. on_set_cookie(url, cookie, source);
  331. }
  332. }