| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 14:52:44 +03:00
										 |  |  | #include <AK/LexicalPath.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | #include <AK/URL.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/File.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-10 21:03:37 +02:00
										 |  |  | #include <LibCore/MimeData.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-03 20:35:19 +02:00
										 |  |  | #include <LibGUI/Action.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Application.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-03 20:35:19 +02:00
										 |  |  | #include <LibGUI/Clipboard.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:56:13 +02:00
										 |  |  | #include <LibGUI/MessageBox.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Painter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/ScrollBar.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Window.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-03 22:58:33 +01:00
										 |  |  | #include <LibGfx/ImageDecoder.h>
 | 
					
						
							| 
									
										
										
										
											2020-10-02 19:01:51 +02:00
										 |  |  | #include <LibGfx/ShareableBitmap.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-04 22:12:37 +02:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/DOM/Element.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/ElementFactory.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Text.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Dump.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-28 17:21:23 +02:00
										 |  |  | #include <LibWeb/HTML/HTMLAnchorElement.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/HTMLImageElement.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-28 19:27:41 +02:00
										 |  |  | #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:56:13 +02:00
										 |  |  | #include <LibWeb/InProcessWebView.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/BreakNode.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/InitialContainingBlockBox.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/Node.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/TextNode.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | #include <LibWeb/Loader/ResourceLoader.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-28 19:27:41 +02:00
										 |  |  | #include <LibWeb/Page/EventHandler.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Page/Frame.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-18 21:35:44 +02:00
										 |  |  | #include <LibWeb/Painting/PaintContext.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-28 17:21:23 +02:00
										 |  |  | #include <LibWeb/UIEvents/MouseEvent.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 08:28:38 +01:00
										 |  |  | //#define SELECTION_DEBUG
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 09:56:35 +02:00
										 |  |  | REGISTER_WIDGET(Web, InProcessWebView) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | InProcessWebView::InProcessWebView() | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     : m_page(make<Page>(*this)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     set_should_hide_unnecessary_scrollbars(true); | 
					
						
							| 
									
										
										
										
											2020-01-05 00:09:35 +03:00
										 |  |  |     set_background_role(ColorRole::Base); | 
					
						
							| 
									
										
										
										
											2020-10-30 10:58:27 +01:00
										 |  |  |     set_focus_policy(GUI::FocusPolicy::StrongFocus); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) { | 
					
						
							| 
									
										
										
										
											2020-09-05 16:16:01 +02:00
										 |  |  |         GUI::Clipboard::the().set_plain_text(selected_text()); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:35:19 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:54:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) { | 
					
						
							|  |  |  |         select_all(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | InProcessWebView::~InProcessWebView() | 
					
						
							| 
									
										
										
										
											2019-10-04 15:50:04 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::select_all() | 
					
						
							| 
									
										
										
										
											2020-07-03 20:54:53 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto* layout_root = this->layout_root(); | 
					
						
							|  |  |  |     if (!layout_root) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     const Layout::Node* first_layout_node = layout_root; | 
					
						
							| 
									
										
										
										
											2020-07-03 20:54:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         auto* next = first_layout_node->next_in_pre_order(); | 
					
						
							|  |  |  |         if (!next) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         first_layout_node = next; | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |         if (is<Layout::TextNode>(*first_layout_node)) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:54:53 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     const Layout::Node* last_layout_node = first_layout_node; | 
					
						
							| 
									
										
										
										
											2020-07-03 20:54:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     for (const Layout::Node* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) { | 
					
						
							|  |  |  |         if (is<Layout::TextNode>(*layout_node)) | 
					
						
							| 
									
										
										
										
											2020-07-03 20:54:53 +02:00
										 |  |  |             last_layout_node = layout_node; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT(first_layout_node); | 
					
						
							|  |  |  |     ASSERT(last_layout_node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int last_layout_node_index_in_node = 0; | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     if (is<Layout::TextNode>(*last_layout_node)) | 
					
						
							|  |  |  |         last_layout_node_index_in_node = downcast<Layout::TextNode>(*last_layout_node).text_for_rendering().length() - 1; | 
					
						
							| 
									
										
										
										
											2020-07-03 20:54:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 17:54:44 +02:00
										 |  |  |     layout_root->set_selection({ { first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node } }); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:54:53 +02:00
										 |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | String InProcessWebView::selected_text() const | 
					
						
							| 
									
										
										
										
											2020-07-03 20:35:19 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-21 18:58:21 +02:00
										 |  |  |     return page().focused_frame().selected_text(); | 
					
						
							| 
									
										
										
										
											2020-07-03 20:35:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_layout() | 
					
						
							| 
									
										
										
										
											2020-06-23 18:02:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-07 17:25:33 +02:00
										 |  |  |     ASSERT(layout_root()); | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  |     set_content_size(layout_root()->size().to_type<int>()); | 
					
						
							| 
									
										
										
										
											2020-06-23 18:02:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_change_title(const String& title) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_title_change) | 
					
						
							|  |  |  |         on_title_change(title); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_set_document_in_main_frame(DOM::Document* document) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_set_document) | 
					
						
							|  |  |  |         on_set_document(document); | 
					
						
							|  |  |  |     layout_and_sync_size(); | 
					
						
							|  |  |  |     scroll_to_top(); | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_start_loading(const URL& url) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_load_start) | 
					
						
							|  |  |  |         on_load_start(url); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-08 21:44:42 +01:00
										 |  |  | void InProcessWebView::page_did_finish_loading(const URL& url) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_load_finish) | 
					
						
							|  |  |  |         on_load_finish(url); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_change_selection() | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-10 19:25:13 +02:00
										 |  |  | void InProcessWebView::page_did_request_cursor_change(Gfx::StandardCursor cursor) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-05 22:22:44 +01:00
										 |  |  |     set_override_cursor(cursor); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_request_context_menu(const Gfx::IntPoint& content_position) | 
					
						
							| 
									
										
										
										
											2020-06-27 14:21:58 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_context_menu_request) | 
					
						
							|  |  |  |         on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | 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) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_link_context_menu_request) | 
					
						
							| 
									
										
										
										
											2020-07-06 20:00:56 +02:00
										 |  |  |         on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position))); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-02 19:01:51 +02:00
										 |  |  | 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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!on_image_context_menu_request) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     Gfx::ShareableBitmap shareable_bitmap; | 
					
						
							|  |  |  |     if (bitmap) | 
					
						
							|  |  |  |         shareable_bitmap = Gfx::ShareableBitmap(*bitmap); | 
					
						
							|  |  |  |     on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), shareable_bitmap); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_click_link(const URL& url, const String& target, unsigned modifiers) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_link_click) | 
					
						
							| 
									
										
										
										
											2020-07-06 19:41:10 +02:00
										 |  |  |         on_link_click(url, target, modifiers); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_middle_click_link(const URL& url, const String& target, unsigned modifiers) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_link_middle_click) | 
					
						
							| 
									
										
										
										
											2020-07-06 21:18:16 +02:00
										 |  |  |         on_link_middle_click(url, target, modifiers); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 21:26:47 +01:00
										 |  |  | void InProcessWebView::page_did_enter_tooltip_area([[maybe_unused]] const Gfx::IntPoint& content_position, const String& title) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-28 21:26:47 +01:00
										 |  |  |     GUI::Application::the()->show_tooltip(title, nullptr); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_leave_tooltip_area() | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-04 16:52:01 +02:00
										 |  |  |     GUI::Application::the()->hide_tooltip(); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_hover_link(const URL& url) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_link_hover) | 
					
						
							| 
									
										
										
										
											2020-07-05 17:02:44 +02:00
										 |  |  |         on_link_hover(url); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_unhover_link() | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-05 17:02:44 +02:00
										 |  |  |     if (on_link_hover) | 
					
						
							|  |  |  |         on_link_hover({}); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_invalidate(const Gfx::IntRect&) | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_change_favicon(const Gfx::Bitmap& bitmap) | 
					
						
							| 
									
										
										
										
											2020-06-08 21:35:31 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (on_favicon_change) | 
					
						
							|  |  |  |         on_favicon_change(bitmap); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::layout_and_sync_size() | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     if (!document()) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     bool had_vertical_scrollbar = vertical_scrollbar().is_visible(); | 
					
						
							|  |  |  |     bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().set_size(available_size()); | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  |     set_content_size(layout_root()->size().to_type<int>()); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
 | 
					
						
							|  |  |  |     //       since the scrollbars now take up some of the available space.
 | 
					
						
							|  |  |  |     if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |         page().main_frame().set_size(available_size()); | 
					
						
							| 
									
										
										
										
											2020-07-25 21:31:47 -07:00
										 |  |  |         set_content_size(layout_root()->size().to_type<int>()); | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 23:40:57 +01:00
										 |  |  |     page().main_frame().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() }); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::resize_event(GUI::ResizeEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::ScrollableWidget::resize_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     layout_and_sync_size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::paint_event(GUI::PaintEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::Frame::paint_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::Painter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     painter.add_clip_rect(widget_inner_rect()); | 
					
						
							|  |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     if (!layout_root()) { | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |         painter.fill_rect(event.rect(), palette().color(background_role())); | 
					
						
							| 
									
										
										
										
											2019-10-04 21:05:52 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-05 00:09:35 +03:00
										 |  |  |     painter.fill_rect(event.rect(), document()->background_color(palette())); | 
					
						
							| 
									
										
										
										
											2019-10-05 21:51:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     if (auto background_bitmap = document()->background_image()) { | 
					
						
							| 
									
										
										
										
											2019-10-19 11:49:46 +02:00
										 |  |  |         painter.draw_tiled_bitmap(event.rect(), *background_bitmap); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     painter.translate(frame_thickness(), frame_thickness()); | 
					
						
							|  |  |  |     painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 21:35:44 +02:00
										 |  |  |     PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() }); | 
					
						
							| 
									
										
										
										
											2019-10-12 15:02:53 +02:00
										 |  |  |     context.set_should_show_line_box_borders(m_should_show_line_box_borders); | 
					
						
							| 
									
										
										
										
											2020-05-04 22:35:29 +02:00
										 |  |  |     context.set_viewport_rect(viewport_rect_in_content_coordinates()); | 
					
						
							| 
									
										
										
										
											2020-08-14 20:02:46 +02:00
										 |  |  |     context.set_has_focus(is_focused()); | 
					
						
							| 
									
										
										
										
											2020-06-18 18:57:35 +02:00
										 |  |  |     layout_root()->paint_all_phases(context); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::mousemove_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers()); | 
					
						
							| 
									
										
										
										
											2020-06-07 14:40:38 +02:00
										 |  |  |     GUI::ScrollableWidget::mousemove_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::mousedown_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers()); | 
					
						
							| 
									
										
										
										
											2020-06-07 14:40:38 +02:00
										 |  |  |     GUI::ScrollableWidget::mousedown_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::mouseup_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers()); | 
					
						
							| 
									
										
										
										
											2020-06-07 14:40:38 +02:00
										 |  |  |     GUI::ScrollableWidget::mouseup_event(event); | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::keydown_event(GUI::KeyEvent& event) | 
					
						
							| 
									
										
										
										
											2019-10-17 21:36:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-14 19:39:34 +02:00
										 |  |  |     bool page_accepted_event = page().handle_keydown(event.key(), event.modifiers(), event.code_point()); | 
					
						
							| 
									
										
										
										
											2020-08-02 12:10:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-17 21:36:22 +02:00
										 |  |  |     if (event.modifiers() == 0) { | 
					
						
							|  |  |  |         switch (event.key()) { | 
					
						
							|  |  |  |         case Key_Home: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(0); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_End: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().max()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Down: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Up: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Left: | 
					
						
							|  |  |  |             horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Right: | 
					
						
							|  |  |  |             horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_PageDown: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_PageUp: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height()); | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2020-05-27 00:29:57 +03:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2020-08-14 19:39:34 +02:00
										 |  |  |             if (!page_accepted_event) { | 
					
						
							|  |  |  |                 ScrollableWidget::keydown_event(event); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-05-27 00:29:57 +03:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2019-10-17 21:36:22 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     event.accept(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | URL InProcessWebView::url() const | 
					
						
							| 
									
										
										
										
											2020-07-06 21:46:37 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-07 11:03:55 +02:00
										 |  |  |     if (!page().main_frame().document()) | 
					
						
							| 
									
										
										
										
											2020-07-06 21:46:37 +02:00
										 |  |  |         return {}; | 
					
						
							|  |  |  |     return page().main_frame().document()->url(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::reload() | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-06 21:46:37 +02:00
										 |  |  |     load(url()); | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::load_html(const StringView& html, const URL& url) | 
					
						
							| 
									
										
										
										
											2020-06-21 21:37:07 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-08 21:03:16 +01:00
										 |  |  |     page().main_frame().loader().load_html(html, url); | 
					
						
							| 
									
										
										
										
											2020-06-21 21:37:07 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | bool InProcessWebView::load(const URL& url) | 
					
						
							| 
									
										
										
										
											2020-05-16 15:38:13 +04:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-11 14:28:05 +02:00
										 |  |  |     set_override_cursor(Gfx::StandardCursor::None); | 
					
						
							| 
									
										
										
										
											2020-07-07 17:25:33 +02:00
										 |  |  |     return page().main_frame().loader().load(url, FrameLoader::Type::Navigation); | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | const Layout::InitialContainingBlockBox* InProcessWebView::layout_root() const | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return document() ? document()->layout_node() : nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | Layout::InitialContainingBlockBox* InProcessWebView::layout_root() | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!document()) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     return const_cast<Layout::InitialContainingBlockBox*>(document()->layout_node()); | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::page_did_request_scroll_into_view(const Gfx::IntRect& rect) | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-05 14:50:38 +02:00
										 |  |  |     scroll_into_view(rect, true, true); | 
					
						
							| 
									
										
										
										
											2020-09-11 14:28:05 +02:00
										 |  |  |     set_override_cursor(Gfx::StandardCursor::None); | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::load_empty_document() | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().set_document(nullptr); | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | DOM::Document* InProcessWebView::document() | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     return page().main_frame().document(); | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | const DOM::Document* InProcessWebView::document() const | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     return page().main_frame().document(); | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::set_document(DOM::Document* document) | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().set_document(document); | 
					
						
							| 
									
										
										
										
											2020-06-06 13:02:44 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::did_scroll() | 
					
						
							| 
									
										
										
										
											2019-12-18 20:54:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-02 23:40:57 +01:00
										 |  |  |     page().main_frame().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() }); | 
					
						
							| 
									
										
										
										
											2020-06-08 20:31:49 +02:00
										 |  |  |     page().main_frame().did_scroll({}); | 
					
						
							| 
									
										
										
										
											2019-12-18 20:54:23 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:29 +02:00
										 |  |  | void InProcessWebView::drop_event(GUI::DropEvent& event) | 
					
						
							| 
									
										
										
										
											2020-05-10 21:03:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (event.mime_data().has_urls()) { | 
					
						
							|  |  |  |         if (on_url_drop) { | 
					
						
							|  |  |  |             on_url_drop(event.mime_data().urls().first()); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ScrollableWidget::drop_event(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-12 11:56:13 +02:00
										 |  |  | void InProcessWebView::page_did_request_alert(const String& message) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |