InProcessWebView.cpp 13 KB

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