Truly independent web browser
LibWebView+Hub+UI/Qt: Handle typed text and external URLs in the UI
Andreas Kling2c0f01a107fd
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex deb76944876..92d3f065b00 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -31,7 +31,6 @@#include <LibWebView/CanonicalSessionHistoryEntry.h>#include <LibWebView/CrashReportOutbox.h>#include <LibWebView/CrashReportReview.h>-#include <LibWebView/ErrorHTML.h>#include <LibWebView/FaviconStore.h>#include <LibWebView/HelperProcess.h>#include <LibWebView/HistoryDebug.h>@@ -531,62 +530,8 @@ void ViewImplementation::load(Web::HTML::PreparedNavigationDescriptor navigationdump_session_history("load"sv);}-void ViewImplementation::load_from_user_input(URL::URL const& url)-{- if (!is_url_handled_internally(url)) {- handle_external_url_from_user_input(url);- return;- }-- load(url);-}--void ViewImplementation::load_from_user_input(StringView input)-{- load_from_user_input(input, sanitize_url(input, Application::settings().search_engine_settings().engine));-}--void ViewImplementation::load_from_user_input(StringView input, Optional<URL::URL> fallback_url)-{- auto classified_input = classify_user_input(input);-- if (classified_input.classification != UserInputClassification::ExternalURL) {- if (fallback_url.has_value())- load(*fallback_url);- else- load_navigation_error_page(input);- return;- }-- VERIFY(classified_input.url.has_value());- auto external_url = classified_input.url.release_value();-- if (fallback_url.has_value() && *fallback_url == external_url) {- handle_external_url_from_user_input(external_url);- return;- }-- auto input_string = MUST(String::from_utf8(input));- auto view_id = m_view_id;- handle_external_url_from_user_input(external_url, [view_id, input = move(input_string), fallback_url = move(fallback_url)] {- auto view = ViewImplementation::find_view_by_id(view_id);- if (!view.has_value())- return;-- if (fallback_url.has_value())- view->load(*fallback_url);- else- view->load_navigation_error_page(input);- });-}-void ViewImplementation::open_url_in_new_tab(URL::URL const& url, Web::HTML::ActivateTab activate_tab){- if (!is_url_handled_internally(url)) {- handle_external_url_from_user_input(url);- return;- }-if (auto* view = create_view_for_new_tab_or_window(is_private())) {view->load(url);return;@@ -627,11 +572,6 @@ void ViewImplementation::open_navigation_in_new_window(Web::HTML::PreparedNavigavoid ViewImplementation::open_url_in_new_window(URL::URL const& url, IsPrivate is_private){- if (!is_url_handled_internally(url)) {- handle_external_url_from_user_input(url);- return;- }-if (auto* view = create_view_for_new_tab_or_window(is_private)) {view->load(url);return;@@ -652,17 +592,6 @@ void ViewImplementation::load_html(StringView html)set_loading_state(true);}-void ViewImplementation::load_navigation_error_page(StringView text)-{- auto message = MUST(String::formatted("Failed to load \"{}\"", text));-- StringBuilder builder;- builder.appendff(ERROR_HTML_HEADER, ""sv, ERROR_SVG, message);- builder.append("<p>If you were trying to enter a search query, please enable search in <a href=\"about:settings#search\">settings</a>.</p>"sv);- builder.append(ERROR_HTML_FOOTER);- load_html(builder.string_view());-}-void ViewImplementation::reload(BypassCache bypass_cache){m_history_visit_transition_for_next_load = HistoryVisitTransition::Reload;@@ -1118,11 +1047,11 @@ void ViewImplementation::handle_external_url(Badge<WebContentPage>, URL::URL urlhandle_external_url(move(url), move(initiator_origin), has_transient_activation);}-void ViewImplementation::handle_external_url_from_user_input(URL::URL const& url, Function<void()> on_handler_unavailable)+void ViewImplementation::handle_external_url_from_user_input(URL::URL const& url){// Browser UI actions can submit several URLs at once, for example when opening a bookmark folder. Keep those// requests ordered while a handler lookup or confirmation is in progress.- m_pending_external_url_requests.enqueue({ url, this->url().origin(), move(on_handler_unavailable) });+ m_pending_external_url_requests.enqueue({ url, this->url().origin() });process_next_external_url_request();}@@ -1131,7 +1060,7 @@ void ViewImplementation::process_next_external_url_request()while (!m_external_url_confirmation_pending && !m_pending_external_url_requests.is_empty()) {auto request = m_pending_external_url_requests.dequeue();m_external_url_request_policy.allow_request_from_browser_ui(request.url);- handle_external_url(move(request.url), move(request.initiator_origin), false, move(request.on_handler_unavailable));+ handle_external_url(move(request.url), move(request.initiator_origin), false);}}@@ -1150,7 +1079,7 @@ void ViewImplementation::complete_external_url_request()});}-void ViewImplementation::handle_external_url(URL::URL url, URL::Origin initiator_origin, bool has_transient_activation, Function<void()> on_handler_unavailable)+void ViewImplementation::handle_external_url(URL::URL url, URL::Origin initiator_origin, bool has_transient_activation){if (m_external_url_confirmation_pending)return;@@ -1170,11 +1099,6 @@ void ViewImplementation::handle_external_url(URL::URL url, URL::Origin initiatorreturn;if (headless_mode) {- if (on_handler_unavailable) {- on_handler_unavailable();- return;- }-Application::the().display_error_dialog(MUST(String::formatted("External URL handling is unavailable in headless mode: {}", url)));return;}@@ -1182,7 +1106,7 @@ void ViewImplementation::handle_external_url(URL::URL url, URL::Origin initiatorm_external_url_confirmation_pending = true;auto view_id = m_view_id;auto url_for_callback = url;- Application::the().resolve_external_url_handler(url, [view_id, url = move(url_for_callback), initiator_origin = move(initiator_origin), action, request_source = *request_source, on_handler_unavailable = move(on_handler_unavailable)](RefPtr<ExternalURLHandler> handler) mutable {+ Application::the().resolve_external_url_handler(url, [view_id, url = move(url_for_callback), initiator_origin = move(initiator_origin), action, request_source = *request_source](RefPtr<ExternalURLHandler> handler) mutable {auto view = ViewImplementation::find_view_by_id(view_id);if (!view.has_value())return;@@ -1199,12 +1123,6 @@ void ViewImplementation::handle_external_url(URL::URL url, URL::Origin initiator// navigable, navigationParams's id, navigationParams's navigation timing type, and navigationParams's// user involvement.// AD-HOC: We can't implement this directly, so show a warning dialog instead.- if (on_handler_unavailable) {- view->complete_external_url_request();- on_handler_unavailable();- return;- }-auto error_message = MUST(String::formatted("No application is registered to open {}", url));Application::the().display_error_dialog(error_message);if (auto current_view = ViewImplementation::find_view_by_id(view_id); current_view.has_value())@@ -1214,11 +1132,6 @@ void ViewImplementation::handle_external_url(URL::URL url, URL::Origin initiatorif (handler->is_ladybird()) {view->complete_external_url_request();- if (on_handler_unavailable) {- on_handler_unavailable();- return;- }-if (request_source == ExternalURLRequestSource::Page)view->m_external_url_request_policy.allow_next_request();return;diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex 507764e566a..1776fee0ea1 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -235,9 +235,6 @@ public:void set_has_system_focus(bool);void load(URL::URL const&, Web::Bindings::NavigationHistoryBehavior = Web::Bindings::NavigationHistoryBehavior::Auto);- void load_from_user_input(URL::URL const&);- void load_from_user_input(StringView);- void load_from_user_input(StringView, Optional<URL::URL> fallback_url);void open_url_in_new_tab(URL::URL const&, Web::HTML::ActivateTab);void open_url_in_new_window(URL::URL const&, IsPrivate);void load(Web::HTML::PreparedNavigationDescriptor);@@ -245,7 +242,6 @@ public:void open_navigation_in_new_window(Web::HTML::PreparedNavigationDescriptor, IsPrivate);void set_next_history_visit_transition(HistoryVisitTransition transition) { m_history_visit_transition_for_next_load = transition; }void load_html(StringView);- void load_navigation_error_page(StringView);void reload(BypassCache = BypassCache::No);void reload_for_webdriver_bidi(BypassCache, Utf16String navigation_id);@@ -869,8 +865,8 @@ protected:virtual void global_privacy_control_changed() override;void send_context_menu_request(ContextMenuRequest, Gfx::IntPoint content_position);- void handle_external_url(URL::URL, URL::Origin, bool has_transient_activation, Function<void()> on_handler_unavailable = {});- void handle_external_url_from_user_input(URL::URL const&, Function<void()> on_handler_unavailable = {});+ void handle_external_url(URL::URL, URL::Origin, bool has_transient_activation);+ void handle_external_url_from_user_input(URL::URL const&);void complete_external_url_request();void process_next_external_url_request();void request_text_fragment_url(Optional<u64> generation_id);@@ -994,7 +990,6 @@ protected:struct PendingExternalURLRequest {URL::URL url;URL::Origin initiator_origin;- Function<void()> on_handler_unavailable;};Queue<PendingExternalURLRequest> m_pending_external_url_requests;ExternalURLRequestPolicy m_external_url_request_policy;diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cppindex 01654c7c9ba..ba047e8ebd9 100644--- a/Services/Hub/ConnectionFromUI.cpp+++ b/Services/Hub/ConnectionFromUI.cpp@@ -782,22 +782,6 @@ void ConnectionFromUI::view_load(u64 view_id, URL::URL url, Web::Bindings::Navig}}-void ConnectionFromUI::view_load_url_from_user_input(u64 view_id, URL::URL url)-{- if (auto* view = find_view(view_id)) {- view->load_from_user_input(url);- view->push_state_to_ui();- }-}--void ConnectionFromUI::view_load_from_user_input(u64 view_id, ByteString input, Optional<URL::URL> fallback_url)-{- if (auto* view = find_view(view_id)) {- view->load_from_user_input(input, move(fallback_url));- view->push_state_to_ui();- }-}-void ConnectionFromUI::view_load_html(u64 view_id, ByteString html){if (auto* view = find_view(view_id)) {@@ -806,14 +790,6 @@ void ConnectionFromUI::view_load_html(u64 view_id, ByteString html)}}-void ConnectionFromUI::view_load_navigation_error_page(u64 view_id, ByteString html)-{- if (auto* view = find_view(view_id)) {- view->load_navigation_error_page(html);- view->push_state_to_ui();- }-}-void ConnectionFromUI::view_find_in_page(u64 view_id, Utf16String query, AK::CaseSensitivity case_sensitivity, bool highlight_all_matches){if (auto* view = find_view(view_id)) {diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.hindex 77e022193e5..56562e18101 100644--- a/Services/Hub/ConnectionFromUI.h+++ b/Services/Hub/ConnectionFromUI.h@@ -144,10 +144,7 @@ private:virtual void view_enqueue_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) override;virtual void view_enqueue_pinch_event(u64 view_id, Web::PinchEvent event) override;virtual void view_load(u64 view_id, URL::URL url, Web::Bindings::NavigationHistoryBehavior history_behavior) override;- virtual void view_load_url_from_user_input(u64 view_id, URL::URL url) override;- virtual void view_load_from_user_input(u64 view_id, ByteString input, Optional<URL::URL> fallback_url) override;virtual void view_load_html(u64 view_id, ByteString html) override;- virtual void view_load_navigation_error_page(u64 view_id, ByteString html) override;virtual void view_find_in_page(u64 view_id, Utf16String query, AK::CaseSensitivity case_sensitivity, bool highlight_all_matches) override;virtual void view_set_window_position(u64 view_id, Gfx::IntPoint position) override;virtual void view_set_window_size(u64 view_id, Gfx::IntSize size) override;diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipcindex e382feb9a99..9bd99f88bd1 100644--- a/Services/Hub/HubServer.ipc+++ b/Services/Hub/HubServer.ipc@@ -212,10 +212,7 @@ endpoint HubServerview_enqueue_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) =|view_enqueue_pinch_event(u64 view_id, Web::PinchEvent event) =|view_load(u64 view_id, URL::URL url, Web::Bindings::NavigationHistoryBehavior history_behavior) =|- view_load_url_from_user_input(u64 view_id, URL::URL url) =|- view_load_from_user_input(u64 view_id, ByteString input, Optional<URL::URL> fallback_url) =|view_load_html(u64 view_id, ByteString html) =|- view_load_navigation_error_page(u64 view_id, ByteString html) =|view_find_in_page(u64 view_id, Utf16String query, AK::CaseSensitivity case_sensitivity, bool highlight_all_matches) =|view_set_window_position(u64 view_id, Gfx::IntPoint position) =|view_set_window_size(u64 view_id, Gfx::IntSize size) =|diff --git a/UI/Qt/RemoteView.cpp b/UI/Qt/RemoteView.cppindex 057568ff3e7..e3cb6e2d6e9 100644--- a/UI/Qt/RemoteView.cpp+++ b/UI/Qt/RemoteView.cpp@@ -9,9 +9,12 @@# include <Hub/Application.h># include <LibWebView/PageTranslation.h>#endif+#include <LibCore/EventLoop.h>#include <LibGfx/ImageFormats/PNGWriter.h>#include <LibURL/InternalURLs.h>#include <LibUnicode/DisplayNames.h>+#include <LibWebView/ErrorHTML.h>+#include <LibWebView/ExternalURLHandler.h>#include <LibWebView/FaviconStore.h>#include <LibWebView/HistoryStore.h>#include <LibWebView/PageTranslation.h>@@ -557,4 +560,150 @@ void RemoteView::did_make_link_to_highlight(Optional<URL::URL> url)m_copy_link_to_highlight_action->set_enabled(m_link_to_highlight.has_value());}+void RemoteView::load_from_user_input(URL::URL const& url)+{+ if (!WebView::is_url_handled_internally(url)) {+ open_external_url_from_user_input(url);+ return;+ }+ load(url);+}++void RemoteView::load_from_user_input(StringView input)+{+ load_from_user_input(input, WebView::sanitize_url(input, Application::settings().search_engine_settings().engine));+}++void RemoteView::load_from_user_input(StringView input, Optional<URL::URL> fallback_url)+{+ auto classified_input = WebView::classify_user_input(input);++ if (classified_input.classification != WebView::UserInputClassification::ExternalURL) {+ if (fallback_url.has_value())+ load(*fallback_url);+ else+ load_navigation_error_page(input);+ return;+ }++ VERIFY(classified_input.url.has_value());+ auto external_url = classified_input.url.release_value();++ if (fallback_url.has_value() && *fallback_url == external_url) {+ open_external_url_from_user_input(external_url);+ return;+ }++ open_external_url_from_user_input(external_url, [view_id = view_id(), input = MUST(String::from_utf8(input)), fallback_url = move(fallback_url)] {+ auto* view = RemoteView::find(view_id);+ if (!view)+ return;+ if (fallback_url.has_value())+ view->load(*fallback_url);+ else+ view->load_navigation_error_page(input);+ });+}++void RemoteView::open_url_in_new_tab(URL::URL const& url, Web::HTML::ActivateTab activate_tab)+{+ if (!WebView::is_url_handled_internally(url)) {+ open_external_url_from_user_input(url);+ return;+ }+ HubConnection::the().async_view_open_url_in_new_tab(view_id(), url, activate_tab);+}++void RemoteView::open_url_in_new_window(URL::URL const& url, WebView::IsPrivate is_private)+{+ if (!WebView::is_url_handled_internally(url)) {+ open_external_url_from_user_input(url);+ return;+ }+ HubConnection::the().async_view_open_url_in_new_window(view_id(), url, is_private);+}++void RemoteView::load_navigation_error_page(StringView input)+{+ auto message = MUST(String::formatted("Failed to load \"{}\"", input));++ StringBuilder builder;+ builder.appendff(WebView::ERROR_HTML_HEADER, ""sv, WebView::ERROR_SVG, message);+ builder.append("<p>If you were trying to enter a search query, please enable search in <a href=\"about:settings#search\">settings</a>.</p>"sv);+ builder.append(WebView::ERROR_HTML_FOOTER);+ load_html(builder.string_view());+}++void RemoteView::open_external_url_from_user_input(URL::URL const& url, Function<void()> on_handler_unavailable)+{+ m_pending_external_urls.enqueue({ url, this->url().origin(), move(on_handler_unavailable) });+ open_next_external_url();+}++void RemoteView::open_next_external_url()+{+ while (!m_external_url_pending && !m_pending_external_urls.is_empty()) {+ auto request = m_pending_external_urls.dequeue();+ if (WebView::ExternalURLRequestPolicy::is_denied_scheme(request.url.scheme()))+ continue;++ m_external_url_pending = true;+ auto url = request.url;+ Application::the().resolve_external_url_handler(url, [view_id = view_id(), request = move(request)](RefPtr<WebView::ExternalURLHandler> handler) mutable {+ if (auto* view = RemoteView::find(view_id))+ view->open_external_url_with_handler(move(request), move(handler));+ });+ }+}++void RemoteView::open_external_url_with_handler(PendingExternalURL request, RefPtr<WebView::ExternalURLHandler> handler)+{+ auto launch = [view_id = view_id()](WebView::ExternalURLHandler const& handler, URL::URL const& url) {+ handler.launch(url, [view_id](bool success) {+ if (!success && RemoteView::find(view_id))+ Application::the().display_error_dialog("Unable to open external URL"sv);+ });+ };++ if (!handler || handler->is_ladybird()) {+ finish_external_url();+ if (request.on_handler_unavailable)+ request.on_handler_unavailable();+ else if (!handler)+ Application::the().display_error_dialog(MUST(String::formatted("No application is registered to open {}", request.url)));+ return;+ }++ // Don't prompt when the user asked for a mailto: URL. This matches other browsers.+ if (request.url.scheme() == "mailto"sv) {+ launch(*handler, request.url);+ finish_external_url();+ return;+ }++ if (!on_request_external_url_confirmation) {+ finish_external_url();+ return;+ }++ on_request_external_url_confirmation(request.url, request.initiator_origin, handler->application_name(), [view_id = view_id(), url = request.url, handler, launch](bool accepted) {+ if (accepted)+ launch(*handler, url);+ if (auto* view = RemoteView::find(view_id))+ view->finish_external_url();+ });+}++void RemoteView::finish_external_url()+{+ VERIFY(m_external_url_pending);+ m_external_url_pending = false;+ if (m_pending_external_urls.is_empty())+ return;+ Core::deferred_invoke([view_id = view_id()] {+ if (auto* view = RemoteView::find(view_id))+ view->open_next_external_url();+ });+}+}diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.hindex 9f1b3116e2c..10200f1ae85 100644--- a/UI/Qt/RemoteView.h+++ b/UI/Qt/RemoteView.h@@ -7,6 +7,7 @@#pragma once#include <AK/NonnullOwnPtr.h>+#include <AK/Queue.h>#include <Hub/View.h>#include <LibWebView/ContextMenuRequest.h>#include <LibWebView/HistoryStore.h>@@ -90,17 +91,17 @@ public:void set_presents_from_cpu_memory(bool presents_from_cpu_memory) { HubConnection::the().async_view_set_presents_from_cpu_memory(view_id(), presents_from_cpu_memory); }void set_has_system_focus(bool has_system_focus) { HubConnection::the().async_view_set_has_system_focus(view_id(), has_system_focus); }void load(URL::URL const& url, Web::Bindings::NavigationHistoryBehavior navigation_history_behavior = Web::Bindings::NavigationHistoryBehavior::Auto) { HubConnection::the().async_view_load(view_id(), url, navigation_history_behavior); }- void load_from_user_input(URL::URL const& url) { HubConnection::the().async_view_load_url_from_user_input(view_id(), url); }- void load_from_user_input(StringView input) { HubConnection::the().async_view_load_from_user_input(view_id(), input, {}); }- void load_from_user_input(StringView input, Optional<URL::URL> fallback_url) { HubConnection::the().async_view_load_from_user_input(view_id(), input, move(fallback_url)); }- void open_url_in_new_tab(URL::URL const& url, Web::HTML::ActivateTab activate_tab) { HubConnection::the().async_view_open_url_in_new_tab(view_id(), url, move(activate_tab)); }- void open_url_in_new_window(URL::URL const& url, WebView::IsPrivate is_private) { HubConnection::the().async_view_open_url_in_new_window(view_id(), url, move(is_private)); }+ void load_from_user_input(URL::URL const&);+ void load_from_user_input(StringView input);+ void load_from_user_input(StringView input, Optional<URL::URL> fallback_url);+ void open_url_in_new_tab(URL::URL const&, Web::HTML::ActivateTab);+ void open_url_in_new_window(URL::URL const&, WebView::IsPrivate);void load(Web::HTML::PreparedNavigationDescriptor navigation) { HubConnection::the().async_view_load_navigation(view_id(), move(navigation)); }void open_navigation_in_new_tab(Web::HTML::PreparedNavigationDescriptor navigation, Web::HTML::ActivateTab activate_tab) { HubConnection::the().async_view_open_navigation_in_new_tab(view_id(), move(navigation), activate_tab); }void open_navigation_in_new_window(Web::HTML::PreparedNavigationDescriptor navigation, WebView::IsPrivate is_private) { HubConnection::the().async_view_open_navigation_in_new_window(view_id(), move(navigation), is_private); }void set_next_history_visit_transition(WebView::HistoryVisitTransition transition) { HubConnection::the().async_view_set_next_history_visit_transition(view_id(), move(transition)); }void load_html(StringView html) { HubConnection::the().async_view_load_html(view_id(), html); }- void load_navigation_error_page(StringView html) { HubConnection::the().async_view_load_navigation_error_page(view_id(), html); }+ void load_navigation_error_page(StringView input);void reload(WebView::BypassCache bypass_cache = WebView::BypassCache::No) { HubConnection::the().async_view_reload(view_id(), move(bypass_cache)); }void stop_loading() { HubConnection::the().async_view_stop_loading(view_id()); }bool is_loading() const { return m_state.is_loading; }@@ -283,6 +284,20 @@ private:Optional<u64> m_display_id;Optional<SessionTabId> m_session_tab_id;Optional<String> m_favicon_hash;++ // The external URLs that the user asked for, one at a time, so that their prompts do not overlap.+ struct PendingExternalURL {+ URL::URL url;+ // NB: The page the user asked from, which the prompt names even if the view goes on to another page meanwhile.+ URL::Origin initiator_origin;+ Function<void()> on_handler_unavailable;+ };+ void open_external_url_from_user_input(URL::URL const&, Function<void()> on_handler_unavailable = {});+ void open_next_external_url();+ void open_external_url_with_handler(PendingExternalURL, RefPtr<WebView::ExternalURLHandler>);+ void finish_external_url();+ Queue<PendingExternalURL> m_pending_external_urls;+ bool m_external_url_pending { false };bool m_scales_frames_to_fit { false };Hub::ViewState m_state;HashMap<u64, NavigationListener> m_navigation_listeners;