InProcessWebView.cpp 12 KB

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