InProcessWebView.cpp 13 KB

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