InProcessWebView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/URL.h>
  27. #include <LibCore/File.h>
  28. #include <LibCore/MimeData.h>
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/Clipboard.h>
  32. #include <LibGUI/InputBox.h>
  33. #include <LibGUI/MessageBox.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/ScrollBar.h>
  36. #include <LibGUI/Window.h>
  37. #include <LibGfx/ShareableBitmap.h>
  38. #include <LibWeb/HTML/HTMLAnchorElement.h>
  39. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  40. #include <LibWeb/InProcessWebView.h>
  41. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  42. #include <LibWeb/Layout/Node.h>
  43. #include <LibWeb/Layout/TextNode.h>
  44. #include <LibWeb/Loader/ResourceLoader.h>
  45. #include <LibWeb/Page/EventHandler.h>
  46. #include <LibWeb/Page/Frame.h>
  47. #include <LibWeb/Painting/PaintContext.h>
  48. #include <LibWeb/UIEvents/MouseEvent.h>
  49. REGISTER_WIDGET(Web, InProcessWebView)
  50. namespace Web {
  51. InProcessWebView::InProcessWebView()
  52. : m_page(make<Page>(*this))
  53. {
  54. set_should_hide_unnecessary_scrollbars(true);
  55. set_background_role(ColorRole::Base);
  56. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  57. m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
  58. GUI::Clipboard::the().set_plain_text(selected_text());
  59. });
  60. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  61. select_all();
  62. });
  63. }
  64. InProcessWebView::~InProcessWebView()
  65. {
  66. }
  67. void InProcessWebView::select_all()
  68. {
  69. auto* layout_root = this->layout_root();
  70. if (!layout_root)
  71. return;
  72. const Layout::Node* first_layout_node = layout_root;
  73. for (;;) {
  74. auto* next = first_layout_node->next_in_pre_order();
  75. if (!next)
  76. break;
  77. first_layout_node = next;
  78. if (is<Layout::TextNode>(*first_layout_node))
  79. break;
  80. }
  81. const Layout::Node* last_layout_node = first_layout_node;
  82. for (const Layout::Node* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) {
  83. if (is<Layout::TextNode>(*layout_node))
  84. last_layout_node = layout_node;
  85. }
  86. VERIFY(first_layout_node);
  87. VERIFY(last_layout_node);
  88. int last_layout_node_index_in_node = 0;
  89. if (is<Layout::TextNode>(*last_layout_node))
  90. last_layout_node_index_in_node = downcast<Layout::TextNode>(*last_layout_node).text_for_rendering().length() - 1;
  91. layout_root->set_selection({ { first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node } });
  92. update();
  93. }
  94. String InProcessWebView::selected_text() const
  95. {
  96. return page().focused_frame().selected_text();
  97. }
  98. void InProcessWebView::page_did_layout()
  99. {
  100. VERIFY(layout_root());
  101. set_content_size(layout_root()->size().to_type<int>());
  102. }
  103. void InProcessWebView::page_did_change_title(const String& title)
  104. {
  105. if (on_title_change)
  106. on_title_change(title);
  107. }
  108. void InProcessWebView::page_did_set_document_in_main_frame(DOM::Document* document)
  109. {
  110. if (on_set_document)
  111. on_set_document(document);
  112. layout_and_sync_size();
  113. scroll_to_top();
  114. update();
  115. }
  116. void InProcessWebView::page_did_start_loading(const URL& url)
  117. {
  118. if (on_load_start)
  119. on_load_start(url);
  120. }
  121. void InProcessWebView::page_did_finish_loading(const URL& url)
  122. {
  123. if (on_load_finish)
  124. on_load_finish(url);
  125. }
  126. void InProcessWebView::page_did_change_selection()
  127. {
  128. update();
  129. }
  130. void InProcessWebView::page_did_request_cursor_change(Gfx::StandardCursor cursor)
  131. {
  132. set_override_cursor(cursor);
  133. }
  134. void InProcessWebView::page_did_request_context_menu(const Gfx::IntPoint& content_position)
  135. {
  136. if (on_context_menu_request)
  137. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  138. }
  139. 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)
  140. {
  141. if (on_link_context_menu_request)
  142. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  143. }
  144. 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)
  145. {
  146. if (!on_image_context_menu_request)
  147. return;
  148. Gfx::ShareableBitmap shareable_bitmap;
  149. if (bitmap)
  150. shareable_bitmap = Gfx::ShareableBitmap(*bitmap);
  151. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), shareable_bitmap);
  152. }
  153. void InProcessWebView::page_did_click_link(const URL& url, const String& target, unsigned modifiers)
  154. {
  155. if (on_link_click)
  156. on_link_click(url, target, modifiers);
  157. }
  158. void InProcessWebView::page_did_middle_click_link(const URL& url, const String& target, unsigned modifiers)
  159. {
  160. if (on_link_middle_click)
  161. on_link_middle_click(url, target, modifiers);
  162. }
  163. void InProcessWebView::page_did_enter_tooltip_area([[maybe_unused]] const Gfx::IntPoint& content_position, const String& title)
  164. {
  165. GUI::Application::the()->show_tooltip(title, nullptr);
  166. }
  167. void InProcessWebView::page_did_leave_tooltip_area()
  168. {
  169. GUI::Application::the()->hide_tooltip();
  170. }
  171. void InProcessWebView::page_did_hover_link(const URL& url)
  172. {
  173. if (on_link_hover)
  174. on_link_hover(url);
  175. }
  176. void InProcessWebView::page_did_unhover_link()
  177. {
  178. if (on_link_hover)
  179. on_link_hover({});
  180. }
  181. void InProcessWebView::page_did_invalidate(const Gfx::IntRect&)
  182. {
  183. update();
  184. }
  185. void InProcessWebView::page_did_change_favicon(const Gfx::Bitmap& bitmap)
  186. {
  187. if (on_favicon_change)
  188. on_favicon_change(bitmap);
  189. }
  190. void InProcessWebView::layout_and_sync_size()
  191. {
  192. if (!document())
  193. return;
  194. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  195. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  196. page().main_frame().set_size(available_size());
  197. set_content_size(layout_root()->size().to_type<int>());
  198. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  199. // since the scrollbars now take up some of the available space.
  200. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  201. page().main_frame().set_size(available_size());
  202. set_content_size(layout_root()->size().to_type<int>());
  203. }
  204. page().main_frame().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() });
  205. }
  206. void InProcessWebView::resize_event(GUI::ResizeEvent& event)
  207. {
  208. GUI::ScrollableWidget::resize_event(event);
  209. layout_and_sync_size();
  210. }
  211. void InProcessWebView::paint_event(GUI::PaintEvent& event)
  212. {
  213. GUI::Frame::paint_event(event);
  214. GUI::Painter painter(*this);
  215. painter.add_clip_rect(widget_inner_rect());
  216. painter.add_clip_rect(event.rect());
  217. if (!layout_root()) {
  218. painter.fill_rect(event.rect(), palette().color(background_role()));
  219. return;
  220. }
  221. painter.translate(frame_thickness(), frame_thickness());
  222. PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() });
  223. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  224. context.set_viewport_rect(viewport_rect_in_content_coordinates());
  225. context.set_has_focus(is_focused());
  226. layout_root()->paint_all_phases(context);
  227. }
  228. void InProcessWebView::mousemove_event(GUI::MouseEvent& event)
  229. {
  230. page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers());
  231. GUI::ScrollableWidget::mousemove_event(event);
  232. }
  233. void InProcessWebView::mousedown_event(GUI::MouseEvent& event)
  234. {
  235. page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers());
  236. GUI::ScrollableWidget::mousedown_event(event);
  237. }
  238. void InProcessWebView::mouseup_event(GUI::MouseEvent& event)
  239. {
  240. page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers());
  241. GUI::ScrollableWidget::mouseup_event(event);
  242. }
  243. void InProcessWebView::mousewheel_event(GUI::MouseEvent& event)
  244. {
  245. page().handle_mousewheel(to_content_position(event.position()), event.button(), event.modifiers(), event.wheel_delta());
  246. GUI::ScrollableWidget::mousewheel_event(event);
  247. }
  248. void InProcessWebView::keydown_event(GUI::KeyEvent& event)
  249. {
  250. bool page_accepted_event = page().handle_keydown(event.key(), event.modifiers(), event.code_point());
  251. if (event.modifiers() == 0) {
  252. switch (event.key()) {
  253. case Key_Home:
  254. vertical_scrollbar().set_value(0);
  255. break;
  256. case Key_End:
  257. vertical_scrollbar().set_value(vertical_scrollbar().max());
  258. break;
  259. case Key_Down:
  260. vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
  261. break;
  262. case Key_Up:
  263. vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
  264. break;
  265. case Key_Left:
  266. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
  267. break;
  268. case Key_Right:
  269. horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
  270. break;
  271. case Key_PageDown:
  272. vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
  273. break;
  274. case Key_PageUp:
  275. vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
  276. break;
  277. default:
  278. if (!page_accepted_event) {
  279. ScrollableWidget::keydown_event(event);
  280. return;
  281. }
  282. break;
  283. }
  284. }
  285. event.accept();
  286. }
  287. URL InProcessWebView::url() const
  288. {
  289. if (!page().main_frame().document())
  290. return {};
  291. return page().main_frame().document()->url();
  292. }
  293. void InProcessWebView::reload()
  294. {
  295. load(url());
  296. }
  297. void InProcessWebView::load_html(const StringView& html, const URL& url)
  298. {
  299. page().main_frame().loader().load_html(html, url);
  300. }
  301. bool InProcessWebView::load(const URL& url)
  302. {
  303. set_override_cursor(Gfx::StandardCursor::None);
  304. return page().main_frame().loader().load(url, FrameLoader::Type::Navigation);
  305. }
  306. const Layout::InitialContainingBlockBox* InProcessWebView::layout_root() const
  307. {
  308. return document() ? document()->layout_node() : nullptr;
  309. }
  310. Layout::InitialContainingBlockBox* InProcessWebView::layout_root()
  311. {
  312. if (!document())
  313. return nullptr;
  314. return const_cast<Layout::InitialContainingBlockBox*>(document()->layout_node());
  315. }
  316. void InProcessWebView::page_did_request_scroll_into_view(const Gfx::IntRect& rect)
  317. {
  318. scroll_into_view(rect, true, true);
  319. set_override_cursor(Gfx::StandardCursor::None);
  320. }
  321. void InProcessWebView::load_empty_document()
  322. {
  323. page().main_frame().set_document(nullptr);
  324. }
  325. DOM::Document* InProcessWebView::document()
  326. {
  327. return page().main_frame().document();
  328. }
  329. const DOM::Document* InProcessWebView::document() const
  330. {
  331. return page().main_frame().document();
  332. }
  333. void InProcessWebView::set_document(DOM::Document* document)
  334. {
  335. page().main_frame().set_document(document);
  336. }
  337. void InProcessWebView::did_scroll()
  338. {
  339. page().main_frame().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() });
  340. }
  341. void InProcessWebView::drop_event(GUI::DropEvent& event)
  342. {
  343. if (event.mime_data().has_urls()) {
  344. if (on_url_drop) {
  345. on_url_drop(event.mime_data().urls().first());
  346. return;
  347. }
  348. }
  349. ScrollableWidget::drop_event(event);
  350. }
  351. void InProcessWebView::page_did_request_alert(const String& message)
  352. {
  353. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  354. }
  355. bool InProcessWebView::page_did_request_confirm(const String& message)
  356. {
  357. auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
  358. return confirm_result == GUI::Dialog::ExecResult::ExecOK;
  359. }
  360. String InProcessWebView::page_did_request_prompt(const String& message, const String& default_)
  361. {
  362. String value { default_ };
  363. if (GUI::InputBox::show(window(), value, message, "Prompt") == GUI::InputBox::ExecOK)
  364. return value;
  365. return {};
  366. }
  367. String InProcessWebView::page_did_request_cookie(const URL& url)
  368. {
  369. if (on_get_cookie)
  370. return on_get_cookie(url);
  371. return {};
  372. }
  373. void InProcessWebView::page_did_set_cookie(const URL& url, const String& cookie)
  374. {
  375. if (on_set_cookie)
  376. on_set_cookie(url, cookie);
  377. }
  378. }