InProcessWebView.cpp 12 KB

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