InProcessWebView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/LexicalPath.h>
  27. #include <AK/URL.h>
  28. #include <LibCore/File.h>
  29. #include <LibCore/MimeData.h>
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/Clipboard.h>
  33. #include <LibGUI/MessageBox.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/ScrollBar.h>
  36. #include <LibGUI/Window.h>
  37. #include <LibGfx/ImageDecoder.h>
  38. #include <LibGfx/ShareableBitmap.h>
  39. #include <LibJS/Runtime/Value.h>
  40. #include <LibWeb/DOM/Element.h>
  41. #include <LibWeb/DOM/ElementFactory.h>
  42. #include <LibWeb/DOM/Text.h>
  43. #include <LibWeb/Dump.h>
  44. #include <LibWeb/HTML/HTMLAnchorElement.h>
  45. #include <LibWeb/HTML/HTMLImageElement.h>
  46. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  47. #include <LibWeb/InProcessWebView.h>
  48. #include <LibWeb/Layout/LayoutBreak.h>
  49. #include <LibWeb/Layout/LayoutDocument.h>
  50. #include <LibWeb/Layout/LayoutNode.h>
  51. #include <LibWeb/Layout/LayoutText.h>
  52. #include <LibWeb/Loader/ResourceLoader.h>
  53. #include <LibWeb/Page/EventHandler.h>
  54. #include <LibWeb/Page/Frame.h>
  55. #include <LibWeb/Painting/PaintContext.h>
  56. #include <LibWeb/UIEvents/MouseEvent.h>
  57. #include <stdio.h>
  58. //#define SELECTION_DEBUG
  59. REGISTER_WIDGET(Web, InProcessWebView)
  60. namespace Web {
  61. InProcessWebView::InProcessWebView()
  62. : m_page(make<Page>(*this))
  63. {
  64. set_should_hide_unnecessary_scrollbars(true);
  65. set_background_role(ColorRole::Base);
  66. m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
  67. GUI::Clipboard::the().set_plain_text(selected_text());
  68. });
  69. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  70. select_all();
  71. });
  72. }
  73. InProcessWebView::~InProcessWebView()
  74. {
  75. }
  76. void InProcessWebView::select_all()
  77. {
  78. auto* layout_root = this->layout_root();
  79. if (!layout_root)
  80. return;
  81. const LayoutNode* first_layout_node = layout_root;
  82. for (;;) {
  83. auto* next = first_layout_node->next_in_pre_order();
  84. if (!next)
  85. break;
  86. first_layout_node = next;
  87. if (is<LayoutText>(*first_layout_node))
  88. break;
  89. }
  90. const LayoutNode* last_layout_node = first_layout_node;
  91. for (const LayoutNode* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) {
  92. if (is<LayoutText>(*layout_node))
  93. last_layout_node = layout_node;
  94. }
  95. ASSERT(first_layout_node);
  96. ASSERT(last_layout_node);
  97. int last_layout_node_index_in_node = 0;
  98. if (is<LayoutText>(*last_layout_node))
  99. last_layout_node_index_in_node = downcast<LayoutText>(*last_layout_node).text_for_rendering().length() - 1;
  100. layout_root->set_selection({ { first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node } });
  101. update();
  102. }
  103. String InProcessWebView::selected_text() const
  104. {
  105. return page().focused_frame().selected_text();
  106. }
  107. void InProcessWebView::page_did_layout()
  108. {
  109. ASSERT(layout_root());
  110. set_content_size(layout_root()->size().to_type<int>());
  111. }
  112. void InProcessWebView::page_did_change_title(const String& title)
  113. {
  114. if (on_title_change)
  115. on_title_change(title);
  116. }
  117. void InProcessWebView::page_did_set_document_in_main_frame(DOM::Document* document)
  118. {
  119. if (on_set_document)
  120. on_set_document(document);
  121. layout_and_sync_size();
  122. scroll_to_top();
  123. update();
  124. }
  125. void InProcessWebView::page_did_start_loading(const URL& url)
  126. {
  127. if (on_load_start)
  128. on_load_start(url);
  129. }
  130. void InProcessWebView::page_did_change_selection()
  131. {
  132. update();
  133. }
  134. void InProcessWebView::page_did_request_cursor_change(Gfx::StandardCursor cursor)
  135. {
  136. if (window())
  137. window()->set_cursor(cursor);
  138. }
  139. void InProcessWebView::page_did_request_context_menu(const Gfx::IntPoint& content_position)
  140. {
  141. if (on_context_menu_request)
  142. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  143. }
  144. 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)
  145. {
  146. if (on_link_context_menu_request)
  147. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  148. }
  149. 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)
  150. {
  151. if (!on_image_context_menu_request)
  152. return;
  153. Gfx::ShareableBitmap shareable_bitmap;
  154. if (bitmap)
  155. shareable_bitmap = Gfx::ShareableBitmap(*bitmap);
  156. on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), shareable_bitmap);
  157. }
  158. void InProcessWebView::page_did_click_link(const URL& url, const String& target, unsigned modifiers)
  159. {
  160. if (on_link_click)
  161. on_link_click(url, target, modifiers);
  162. }
  163. void InProcessWebView::page_did_middle_click_link(const URL& url, const String& target, unsigned modifiers)
  164. {
  165. if (on_link_middle_click)
  166. on_link_middle_click(url, target, modifiers);
  167. }
  168. void InProcessWebView::page_did_enter_tooltip_area(const Gfx::IntPoint& content_position, const String& title)
  169. {
  170. GUI::Application::the()->show_tooltip(title, screen_relative_rect().location().translated(to_widget_position(content_position)), nullptr);
  171. }
  172. void InProcessWebView::page_did_leave_tooltip_area()
  173. {
  174. GUI::Application::the()->hide_tooltip();
  175. }
  176. void InProcessWebView::page_did_hover_link(const URL& url)
  177. {
  178. if (on_link_hover)
  179. on_link_hover(url);
  180. }
  181. void InProcessWebView::page_did_unhover_link()
  182. {
  183. if (on_link_hover)
  184. on_link_hover({});
  185. }
  186. void InProcessWebView::page_did_invalidate(const Gfx::IntRect&)
  187. {
  188. update();
  189. }
  190. void InProcessWebView::page_did_change_favicon(const Gfx::Bitmap& bitmap)
  191. {
  192. if (on_favicon_change)
  193. on_favicon_change(bitmap);
  194. }
  195. void InProcessWebView::layout_and_sync_size()
  196. {
  197. if (!document())
  198. return;
  199. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  200. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  201. page().main_frame().set_size(available_size());
  202. document()->layout();
  203. set_content_size(layout_root()->size().to_type<int>());
  204. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  205. // since the scrollbars now take up some of the available space.
  206. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  207. page().main_frame().set_size(available_size());
  208. document()->layout();
  209. set_content_size(layout_root()->size().to_type<int>());
  210. }
  211. page().main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  212. #ifdef HTML_DEBUG
  213. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  214. ::dump_tree(*layout_root());
  215. #endif
  216. }
  217. void InProcessWebView::resize_event(GUI::ResizeEvent& event)
  218. {
  219. GUI::ScrollableWidget::resize_event(event);
  220. layout_and_sync_size();
  221. }
  222. void InProcessWebView::paint_event(GUI::PaintEvent& event)
  223. {
  224. GUI::Frame::paint_event(event);
  225. GUI::Painter painter(*this);
  226. painter.add_clip_rect(widget_inner_rect());
  227. painter.add_clip_rect(event.rect());
  228. if (!layout_root()) {
  229. painter.fill_rect(event.rect(), palette().color(background_role()));
  230. return;
  231. }
  232. painter.fill_rect(event.rect(), document()->background_color(palette()));
  233. if (auto background_bitmap = document()->background_image()) {
  234. painter.draw_tiled_bitmap(event.rect(), *background_bitmap);
  235. }
  236. painter.translate(frame_thickness(), frame_thickness());
  237. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  238. PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() });
  239. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  240. context.set_viewport_rect(viewport_rect_in_content_coordinates());
  241. context.set_has_focus(is_focused());
  242. layout_root()->paint_all_phases(context);
  243. }
  244. void InProcessWebView::mousemove_event(GUI::MouseEvent& event)
  245. {
  246. page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers());
  247. GUI::ScrollableWidget::mousemove_event(event);
  248. }
  249. void InProcessWebView::mousedown_event(GUI::MouseEvent& event)
  250. {
  251. page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers());
  252. GUI::ScrollableWidget::mousedown_event(event);
  253. }
  254. void InProcessWebView::mouseup_event(GUI::MouseEvent& event)
  255. {
  256. page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers());
  257. GUI::ScrollableWidget::mouseup_event(event);
  258. }
  259. void InProcessWebView::keydown_event(GUI::KeyEvent& event)
  260. {
  261. bool page_accepted_event = page().handle_keydown(event.key(), event.modifiers(), event.code_point());
  262. if (event.modifiers() == 0) {
  263. switch (event.key()) {
  264. case Key_Home:
  265. vertical_scrollbar().set_value(0);
  266. break;
  267. case Key_End:
  268. vertical_scrollbar().set_value(vertical_scrollbar().max());
  269. break;
  270. case Key_Down:
  271. vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
  272. break;
  273. case Key_Up:
  274. vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
  275. break;
  276. case Key_Left:
  277. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
  278. break;
  279. case Key_Right:
  280. horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
  281. break;
  282. case Key_PageDown:
  283. vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
  284. break;
  285. case Key_PageUp:
  286. vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
  287. break;
  288. default:
  289. if (!page_accepted_event) {
  290. ScrollableWidget::keydown_event(event);
  291. return;
  292. }
  293. break;
  294. }
  295. }
  296. event.accept();
  297. }
  298. URL InProcessWebView::url() const
  299. {
  300. if (!page().main_frame().document())
  301. return {};
  302. return page().main_frame().document()->url();
  303. }
  304. void InProcessWebView::reload()
  305. {
  306. load(url());
  307. }
  308. void InProcessWebView::load_html(const StringView& html, const URL& url)
  309. {
  310. HTML::HTMLDocumentParser parser(html, "utf-8");
  311. parser.run(url);
  312. set_document(&parser.document());
  313. }
  314. bool InProcessWebView::load(const URL& url)
  315. {
  316. set_override_cursor(Gfx::StandardCursor::None);
  317. return page().main_frame().loader().load(url, FrameLoader::Type::Navigation);
  318. }
  319. const LayoutDocument* InProcessWebView::layout_root() const
  320. {
  321. return document() ? document()->layout_node() : nullptr;
  322. }
  323. LayoutDocument* InProcessWebView::layout_root()
  324. {
  325. if (!document())
  326. return nullptr;
  327. return const_cast<LayoutDocument*>(document()->layout_node());
  328. }
  329. void InProcessWebView::page_did_request_scroll_into_view(const Gfx::IntRect& rect)
  330. {
  331. scroll_into_view(rect, true, true);
  332. set_override_cursor(Gfx::StandardCursor::None);
  333. }
  334. void InProcessWebView::load_empty_document()
  335. {
  336. page().main_frame().set_document(nullptr);
  337. }
  338. DOM::Document* InProcessWebView::document()
  339. {
  340. return page().main_frame().document();
  341. }
  342. const DOM::Document* InProcessWebView::document() const
  343. {
  344. return page().main_frame().document();
  345. }
  346. void InProcessWebView::set_document(DOM::Document* document)
  347. {
  348. page().main_frame().set_document(document);
  349. }
  350. void InProcessWebView::did_scroll()
  351. {
  352. page().main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  353. page().main_frame().did_scroll({});
  354. }
  355. void InProcessWebView::drop_event(GUI::DropEvent& event)
  356. {
  357. if (event.mime_data().has_urls()) {
  358. if (on_url_drop) {
  359. on_url_drop(event.mime_data().urls().first());
  360. return;
  361. }
  362. }
  363. ScrollableWidget::drop_event(event);
  364. }
  365. void InProcessWebView::page_did_request_alert(const String& message)
  366. {
  367. GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
  368. }
  369. }