InProcessWebView.cpp 12 KB

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