Skip to content

Truly independent web browser

LibWebView+UI/Qt: Treat remote suggestions that are addresses as URLs

Andreas Kling822ea4fb4a46
diff --git a/Documentation/Omnibox.md b/Documentation/Omnibox.md
index 73075d0994f..9de9bd4d094 100644
--- a/Documentation/Omnibox.md
+++ b/Documentation/Omnibox.md
@@ -52,7 +52,7 @@ generation. Stale results can therefore neither reopen the popup nor become an E
`AutocompleteSuggestion` contains:
-- Its source: literal URL, history, bookmark, adaptive engagement, or search.
+- Its source: literal URL, history, bookmark, adaptive engagement, search, or remote URL.
- Destination text and optional title, subtitle, and favicon.
- The input used for presentation highlighting.
- A match class and separate relevance components.
@@ -72,6 +72,20 @@ not otherwise treated as the same destination.
Search destinations are identified by Unicode case-folded text with whitespace normalized. This
allows an exact remote suggestion to merge with the verbatim search action.
+A remote suggestion that is an https address on a public site, taken as it would be if the user
+typed it, is a remote URL rather than a search. Rows do not show the scheme, so an http address
+stays a search rather than look like the https site. Addresses with an IP, localhost, a port or no
+public suffix stay searches, so the search engine cannot name the user's own machine or network
+directly. A public host name can still resolve there, as it can for any link on the web. Addresses
+with a user name, such as `https://[email protected]/`, also stay searches, since they name
+one site and go to another. An address without a scheme, such as `google.com`, may be a
+phrase like `setup.py` instead, so it is a remote URL only when a local candidate is on the same
+host, ignoring `www.`. Until the local candidates arrive, such an address on a site among the
+local candidates for the earlier text, from which remote suggestions carry over, stays a remote URL.
+Any other such address is left out, so a row does not turn from a search into a page. A remote URL
+merges with local candidates for the same URL, and with a local presentation of the same origin,
+whose title and favicon are kept.
+
When history, bookmark, and adaptive candidates identify the same URL, their score components and
eligibility flags are combined. Bookmark presentation is preferred so its title and favicon remain
visible.
@@ -162,7 +176,7 @@ constructs a bounded result list while preserving the verbatim action and limiti
The current limits are soft:
- At most four local navigation rows before deferred local rows.
-- At most three remote search rows before deferred remote rows.
+- At most three remote rows before deferred remote rows.
- At most two rows per origin for ordinary host queries.
- At most one row per origin and five total rows for one- and two-character host queries.
diff --git a/Libraries/LibWebView/Autocomplete.cpp b/Libraries/LibWebView/Autocomplete.cpp
index 425814e1d13..ef87e4d8e93 100644
--- a/Libraries/LibWebView/Autocomplete.cpp
+++ b/Libraries/LibWebView/Autocomplete.cpp
@@ -8,10 +8,13 @@
#include <AK/CharacterTypes.h>
#include <AK/Debug.h>
#include <AK/Find.h>
+#include <AK/HashTable.h>
#include <AK/QuickSort.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Timer.h>
#include <LibTextCodec/Decoder.h>
+#include <LibURL/Parser.h>
+#include <LibURL/PublicSuffixData.h>
#include <LibURL/URL.h>
#include <LibWebCommon/MimeSniff/MimeType.h>
#include <LibWebView/Autocomplete.h>
@@ -61,6 +64,7 @@ void Autocomplete::cancel_pending_query()
m_trimmed_query = {};
m_local_suggestions.clear();
m_remote_suggestions.clear();
+ m_visited_host_families.clear();
m_external_url.clear();
m_external_url_handler_query_complete = true;
m_external_url_has_handler = false;
@@ -131,12 +135,9 @@ Vector<AutocompleteMatchRange> autocomplete_match_ranges(StringView input, Strin
return merged_ranges;
}
-Vector<String> filter_remote_autocomplete_suggestions(StringView input, Vector<String> suggestions)
+bool remote_autocomplete_suggestion_matches_input(StringView suggestion, StringView input)
{
- suggestions.remove_all_matching([input](auto const& suggestion) {
- return !suggestion.bytes_as_string_view().starts_with(input, CaseSensitivity::CaseInsensitive);
- });
- return suggestions;
+ return suggestion.starts_with(input, CaseSensitivity::CaseInsensitive);
}
Vector<AutocompleteSuggestion> web_ui_autocomplete_suggestions(StringView input)
@@ -234,6 +235,13 @@ static AutocompleteSuggestion make_literal_url_suggestion(StringView query)
};
}
+// Whether the text starts with a scheme for the web, rather than one being guessed for it.
+static bool starts_with_web_scheme(StringView text)
+{
+ text = text.trim_whitespace();
+ return text.starts_with("http://"sv, CaseSensitivity::CaseInsensitive) || text.starts_with("https://"sv, CaseSensitivity::CaseInsensitive);
+}
+
static Optional<AutocompleteSuggestion> literal_url_suggestion(StringView query)
{
if (query.is_empty() || classify_user_input(query).classification != UserInputClassification::InternalURL)
@@ -249,32 +257,114 @@ Optional<AutocompleteSuggestion> make_verbatim_autocomplete_suggestion(StringVie
return search_for_query_suggestion(search_engine, query);
}
-static Vector<AutocompleteSuggestion> make_remote_suggestions(Vector<String> remote_suggestions)
+// A remote suggestion that is a web address goes to that address rather than to a search for it. Only a public site
+// counts: the search engine must not name the user's own machine or network. Text without a scheme, such as
+// "google.com", may well be a phrase like "setup.py" instead, so it counts only for a site the user has been to.
+static RemoteAutocompleteSuggestion classify_remote_autocomplete_suggestion(String suggestion)
+{
+ auto as_search = [&] { return RemoteAutocompleteSuggestion { .text = move(suggestion), .url = {}, .host_family_the_user_must_have_visited = {} }; };
+
+ auto classified_suggestion = classify_user_input_ignoring_local_files(suggestion);
+ if (classified_suggestion.classification != UserInputClassification::InternalURL || !classified_suggestion.url.has_value())
+ return as_search();
+
+ // NB: Rows do not show the scheme, so an http address would look like the https one for the same site. An address
+ // with a user name, such as "https://[email protected]/", names one site and goes to another.
+ auto const& url = *classified_suggestion.url;
+ if (url.scheme() != "https"sv || url.port().has_value() || !url.username().is_empty() || !url.password().is_empty())
+ return as_search();
+
+ auto const& host = url.host();
+ if (!host.has_value() || !host->is_domain() || host->is_loopback_or_localhost())
+ return as_search();
+
+ auto const& domain = host->get<String>();
+ auto public_suffix = URL::PublicSuffixData::find_matching_public_suffix(domain, URL::PublicSuffixData::IncludeStarRule::No);
+ if (!public_suffix.has_value() || *public_suffix == domain)
+ return as_search();
+
+ Optional<String> host_family_the_user_must_have_visited;
+ if (!starts_with_web_scheme(suggestion)) {
+ host_family_the_user_must_have_visited = autocomplete_host_family(url);
+ if (!host_family_the_user_must_have_visited.has_value())
+ return as_search();
+ }
+
+ return { .text = move(suggestion), .url = url.serialize(), .host_family_the_user_must_have_visited = move(host_family_the_user_must_have_visited) };
+}
+
+static Vector<RemoteAutocompleteSuggestion> classify_remote_autocomplete_suggestions(Vector<String> suggestions)
{
static constexpr auto maximum_remote_candidates = 50uz;
+ Vector<RemoteAutocompleteSuggestion> classified_suggestions;
+ classified_suggestions.ensure_capacity(min(suggestions.size(), maximum_remote_candidates));
+ for (size_t index = 0; index < suggestions.size() && index < maximum_remote_candidates; ++index)
+ classified_suggestions.unchecked_append(classify_remote_autocomplete_suggestion(move(suggestions[index])));
+ return classified_suggestions;
+}
+
+static void add_visited_host_families(HashTable<String>& visited_host_families, ReadonlySpan<AutocompleteSuggestion> local_suggestions)
+{
+ for (auto const& suggestion : local_suggestions) {
+ if (suggestion.source == AutocompleteSuggestionSource::Search || suggestion.source == AutocompleteSuggestionSource::ChooseSearchEngine)
+ continue;
+ auto url = URL::Parser::basic_parse(suggestion.text);
+ // NB: A learned row holds the address the user went to as they typed it, such as "google.com" or
+ // "google.com:8443", which the URL parser takes for a scheme and a path. Such text is classified as Return
+ // would take it.
+ if (suggestion.source == AutocompleteSuggestionSource::Adaptive && (!url.has_value() || url->has_an_opaque_path()))
+ url = classify_user_input(suggestion.text).url;
+ if (!url.has_value())
+ continue;
+ if (auto family = autocomplete_host_family(*url); family.has_value())
+ visited_host_families.set(family.release_value());
+ }
+}
+
+static Vector<AutocompleteSuggestion> make_remote_autocomplete_suggestions(ReadonlySpan<RemoteAutocompleteSuggestion> remote_suggestions, HashTable<String> const& visited_host_families, LocalSuggestionsAreComplete local_suggestions_are_complete)
+{
+ if (remote_suggestions.is_empty())
+ return {};
+
Vector<AutocompleteSuggestion> suggestions;
- suggestions.ensure_capacity(min(remote_suggestions.size(), maximum_remote_candidates));
+ suggestions.ensure_capacity(remote_suggestions.size());
+
+ for (size_t index = 0; index < remote_suggestions.size(); ++index) {
+ auto const& remote_suggestion = remote_suggestions[index];
+
+ auto is_url = false;
+ if (remote_suggestion.url.has_value()) {
+ auto const& required_host_family = remote_suggestion.host_family_the_user_must_have_visited;
+ is_url = !required_host_family.has_value() || visited_host_families.contains(*required_host_family);
+ // NB: Shown as a search now, it could turn into a page under the user once the local rows arrive.
+ if (!is_url && local_suggestions_are_complete == LocalSuggestionsAreComplete::No)
+ continue;
+ }
- i32 relevance = 700;
- for (size_t index = 0; index < remote_suggestions.size() && index < maximum_remote_candidates; ++index) {
suggestions.unchecked_append({
- .source = AutocompleteSuggestionSource::Search,
- .text = move(remote_suggestions[index]),
+ .source = is_url ? AutocompleteSuggestionSource::RemoteURL : AutocompleteSuggestionSource::Search,
+ .text = is_url ? *remote_suggestion.url : remote_suggestion.text,
.title = {},
.subtitle = {},
.favicon_png = {},
.highlight_input = {},
- .relevance = relevance,
+ .relevance = 700 - static_cast<i32>(index),
.is_verbatim = false,
.can_be_automatically_selected = false,
.can_be_inline_completed = false,
});
- --relevance;
}
return suggestions;
}
+Vector<AutocompleteSuggestion> make_remote_autocomplete_suggestions(Vector<String> remote_suggestions, ReadonlySpan<AutocompleteSuggestion> local_suggestions, LocalSuggestionsAreComplete local_suggestions_are_complete)
+{
+ HashTable<String> visited_host_families;
+ add_visited_host_families(visited_host_families, local_suggestions);
+ return make_remote_autocomplete_suggestions(classify_remote_autocomplete_suggestions(move(remote_suggestions)).span(), visited_host_families, local_suggestions_are_complete);
+}
+
void Autocomplete::search_engine_settings_changed()
{
cancel_pending_query();
@@ -294,7 +384,12 @@ void Autocomplete::query_autocomplete_engine(AutocompleteQueryID query_id, Strin
m_query_id = query_id;
auto trimmed_query = MUST(String::from_utf8(query.bytes_as_string_view().trim_whitespace()));
- m_remote_suggestions = filter_remote_autocomplete_suggestions(trimmed_query, move(m_remote_suggestions));
+ m_remote_suggestions.remove_all_matching([&](auto const& suggestion) {
+ return !remote_autocomplete_suggestion_matches_input(suggestion.text, trimmed_query);
+ });
+ // NB: The sites the user visited only matter for remote rows that carry over to this query.
+ if (m_remote_suggestions.is_empty())
+ m_visited_host_families.clear();
m_trimmed_query = move(trimmed_query);
m_query = move(query);
m_local_query_complete = false;
@@ -402,9 +497,9 @@ void Autocomplete::start_remote_query(AutocompleteQueryID query_id, SearchEngine
warnln("Unable to handle autocomplete response: {}", result.error());
deliver_current_result();
} else {
- m_remote_suggestions = result.release_value();
+ dbgln_if(WEBVIEW_HISTORY_DEBUG, "[History] Remote autocomplete suggestions for '{}': {}", query, history_log_suggestions(result.value()));
+ m_remote_suggestions = classify_remote_autocomplete_suggestions(result.release_value());
- dbgln_if(WEBVIEW_HISTORY_DEBUG, "[History] Remote autocomplete suggestions for '{}': {}", query, history_log_suggestions(m_remote_suggestions));
deliver_current_result();
}
});
@@ -416,6 +511,7 @@ void Autocomplete::local_query_complete(AutocompleteQueryID query_id, Vector<Aut
return;
m_local_suggestions = move(suggestions);
+ add_visited_host_families(m_visited_host_families, m_local_suggestions);
m_local_query_complete = true;
dbgln_if(WEBVIEW_HISTORY_DEBUG, "[History] Local autocomplete suggestions for '{}': {}", m_trimmed_query, log_autocomplete_suggestions(m_local_suggestions));
@@ -449,11 +545,13 @@ void Autocomplete::deliver_current_result()
}
}
additional_suggestions.extend(m_local_suggestions);
+ auto remote_suggestions = make_remote_autocomplete_suggestions(m_remote_suggestions.span(), m_visited_host_families,
+ m_local_query_complete ? LocalSuggestionsAreComplete::Yes : LocalSuggestionsAreComplete::No);
auto merged_suggestions = mux_autocomplete_suggestions(
m_trimmed_query,
move(verbatim_suggestion),
move(additional_suggestions),
- make_remote_suggestions(m_remote_suggestions),
+ move(remote_suggestions),
m_max_suggestions);
for (auto& suggestion : merged_suggestions)
suggestion.highlight_input = m_trimmed_query;
diff --git a/Libraries/LibWebView/Autocomplete.h b/Libraries/LibWebView/Autocomplete.h
index 8cd70907e23..f46b8ef9ee5 100644
--- a/Libraries/LibWebView/Autocomplete.h
+++ b/Libraries/LibWebView/Autocomplete.h
@@ -10,6 +10,7 @@
#include <AK/ByteBuffer.h>
#include <AK/Error.h>
#include <AK/Function.h>
+#include <AK/HashTable.h>
#include <AK/Optional.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
@@ -42,6 +43,8 @@ enum class AutocompleteSuggestionSource {
Bookmark,
Adaptive,
Search,
+ // A remote suggestion that is an address rather than something to search for.
+ RemoteURL,
// Without a search engine, a query that would be searched for offers to choose one instead.
ChooseSearchEngine,
};
@@ -88,9 +91,28 @@ struct WEBVIEW_API AutocompleteBookmark {
Optional<ByteBuffer> favicon_png;
};
+// A remote suggestion, classified once when it arrives.
+struct RemoteAutocompleteSuggestion {
+ String text;
+ // The address it goes to, if it is a web address on a public site.
+ Optional<String> url;
+ // Text without a scheme is an address only if the user has been to this site.
+ Optional<String> host_family_the_user_must_have_visited;
+};
+
+enum class LocalSuggestionsAreComplete {
+ No,
+ Yes,
+};
+
WEBVIEW_API String autocomplete_suggestion_display_text(AutocompleteSuggestion const&);
WEBVIEW_API Vector<AutocompleteMatchRange> autocomplete_match_ranges(StringView input, StringView text);
-WEBVIEW_API Vector<String> filter_remote_autocomplete_suggestions(StringView input, Vector<String> suggestions);
+// Remote suggestions for the previous text stay while the user types, as long as they still start with the text.
+WEBVIEW_API bool remote_autocomplete_suggestion_matches_input(StringView suggestion, StringView input);
+// The rows for remote suggestions. A web address without a scheme is a page only for a site among the local rows. Until
+// the local rows are complete, such an address for a site not among them yet is left out, rather than shown as a search
+// that turns into a page.
+WEBVIEW_API Vector<AutocompleteSuggestion> make_remote_autocomplete_suggestions(Vector<String> remote_suggestions, ReadonlySpan<AutocompleteSuggestion> local_suggestions = {}, LocalSuggestionsAreComplete = LocalSuggestionsAreComplete::Yes);
// The row for the text as typed: the address it is, or else a search for it.
WEBVIEW_API Optional<AutocompleteSuggestion> make_verbatim_autocomplete_suggestion(StringView query, Optional<SearchEngine> const&);
WEBVIEW_API Vector<AutocompleteSuggestion> web_ui_autocomplete_suggestions(StringView input);
@@ -157,7 +179,10 @@ private:
bool m_external_url_has_handler { false };
Optional<URL::URL> m_external_url;
Vector<AutocompleteSuggestion> m_local_suggestions;
- Vector<String> m_remote_suggestions;
+ // NB: The sites among the local rows, which tell which remote addresses are pages. They are kept for as long as remote
+ // rows carry over to the next text, so a remote page stays one while the local rows for that text load.
+ HashTable<String> m_visited_host_families;
+ Vector<RemoteAutocompleteSuggestion> m_remote_suggestions;
RefPtr<Core::Timer> m_remote_query_timer;
Function<void()> m_cancel_remote_query;
};
diff --git a/Libraries/LibWebView/AutocompleteMuxer.cpp b/Libraries/LibWebView/AutocompleteMuxer.cpp
index beb50b7bd98..07f84a216d5 100644
--- a/Libraries/LibWebView/AutocompleteMuxer.cpp
+++ b/Libraries/LibWebView/AutocompleteMuxer.cpp
@@ -23,6 +23,24 @@ bool autocomplete_suggestions_have_same_destination(AutocompleteSuggestion const
return autocomplete_urls_match(left.text, right.text);
}
+// A local row for the same page as a remote address ranks as the remote address did, so the popup keeps the rank the
+// search engine gave it. Only a row that cannot become the default takes it, though: the search engine must not choose
+// the default row or the inline completion.
+static i32 relevance_of_local_row_for_remote_address(AutocompleteSuggestion const& one, AutocompleteSuggestion const& other)
+{
+ // NB: The two rows may come in either order.
+ auto const& local = one.source == AutocompleteSuggestionSource::RemoteURL ? other : one;
+ auto const& remote = one.source == AutocompleteSuggestionSource::RemoteURL ? one : other;
+ if (local.can_be_automatically_selected || local.can_be_inline_completed)
+ return local.relevance;
+ return max(local.relevance, remote.relevance);
+}
+
+static bool is_remote_address_meeting_local_row(AutocompleteSuggestion const& one, AutocompleteSuggestion const& other)
+{
+ return (one.source == AutocompleteSuggestionSource::RemoteURL) != (other.source == AutocompleteSuggestionSource::RemoteURL);
+}
+
static void merge_suggestion(AutocompleteSuggestion& existing, AutocompleteSuggestion suggestion)
{
auto is_verbatim = existing.is_verbatim || suggestion.is_verbatim;
@@ -32,13 +50,17 @@ static void merge_suggestion(AutocompleteSuggestion& existing, AutocompleteSugge
auto history_relevance = max(existing.history_relevance, suggestion.history_relevance);
auto bookmark_relevance = max(existing.bookmark_relevance, suggestion.bookmark_relevance);
auto adaptive_relevance = max(existing.adaptive_relevance, suggestion.adaptive_relevance);
- auto relevance = max(existing.relevance, suggestion.relevance);
+ auto relevance = is_remote_address_meeting_local_row(existing, suggestion)
+ ? relevance_of_local_row_for_remote_address(existing, suggestion)
+ : max(existing.relevance, suggestion.relevance);
if (match_relevance != 0 || history_relevance != 0 || bookmark_relevance != 0 || adaptive_relevance != 0)
relevance = max(relevance, match_relevance + history_relevance + bookmark_relevance + adaptive_relevance);
auto suggestion_has_preferred_presentation = suggestion.source == AutocompleteSuggestionSource::Bookmark
|| (existing.source == AutocompleteSuggestionSource::Adaptive && suggestion.source == AutocompleteSuggestionSource::History)
- || (existing.source == AutocompleteSuggestionSource::LiteralURL && suggestion.source != AutocompleteSuggestionSource::LiteralURL);
+ || (existing.source == AutocompleteSuggestionSource::LiteralURL && suggestion.source != AutocompleteSuggestionSource::LiteralURL && suggestion.source != AutocompleteSuggestionSource::RemoteURL)
+ // NB: A remote address has no title or favicon, so a local row for the same page is shown in its place.
+ || (existing.source == AutocompleteSuggestionSource::RemoteURL && suggestion.source != AutocompleteSuggestionSource::RemoteURL);
if (suggestion_has_preferred_presentation) {
existing = move(suggestion);
} else {
@@ -71,26 +93,35 @@ static void append_or_merge(Vector<AutocompleteSuggestion>& suggestions, Autocom
suggestions.append(move(suggestion));
}
+Optional<String> autocomplete_host_family(URL::URL const& url)
+{
+ if (!url.host().has_value())
+ return {};
+ auto host = MUST(String::from_utf8(url.serialized_host())).to_ascii_lowercase();
+ if (host.bytes_as_string_view().starts_with("www."sv))
+ return MUST(String::from_utf8(host.bytes_as_string_view().substring_view(4)));
+ return host;
+}
+
static Optional<String> origin_family(AutocompleteSuggestion const& suggestion)
{
if (suggestion.source == AutocompleteSuggestionSource::Search)
return {};
auto url = URL::Parser::basic_parse(suggestion.text);
- if (!url.has_value() || !url->host().has_value())
+ if (!url.has_value())
return {};
- auto host = MUST(String::from_utf8(url->serialized_host())).to_ascii_lowercase();
- if (host.bytes_as_string_view().starts_with("www."sv))
- return MUST(String::from_utf8(host.bytes_as_string_view().substring_view(4)));
- return host;
+ return autocomplete_host_family(*url);
}
static bool is_local_navigation(AutocompleteSuggestion const& suggestion)
{
- return suggestion.source != AutocompleteSuggestionSource::Search;
+ return suggestion.source != AutocompleteSuggestionSource::Search && suggestion.source != AutocompleteSuggestionSource::RemoteURL;
}
-static bool is_remote_search(AutocompleteSuggestion const& suggestion)
+static bool is_remote_suggestion(AutocompleteSuggestion const& suggestion)
{
+ if (suggestion.source == AutocompleteSuggestionSource::RemoteURL)
+ return true;
return suggestion.source == AutocompleteSuggestionSource::Search && !suggestion.is_verbatim;
}
@@ -157,9 +188,14 @@ static void coalesce_equivalent_origin_presentations(Vector<AutocompleteSuggesti
if (!group_titles[*existing_index].has_value() && candidate.title.has_value())
group_titles[*existing_index] = candidate.title;
+ // NB: A remote address has no title or favicon, so a local presentation of the same origin is kept over it.
auto& existing = representatives[*existing_index];
- if (suggestion_is_better(candidate, existing))
+ if (is_remote_address_meeting_local_row(existing, candidate)) {
+ if (existing.source == AutocompleteSuggestionSource::RemoteURL)
+ existing = move(candidate);
+ } else if (suggestion_is_better(candidate, existing)) {
existing = move(candidate);
+ }
}
candidates = move(representatives);
@@ -237,7 +273,13 @@ Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(
continue;
}
+ // NB: A local row is kept over a remote address for another page of the site, with its own rank.
auto& existing = representatives[*existing_index];
+ if (is_remote_address_meeting_local_row(existing, candidate)) {
+ if (existing.source == AutocompleteSuggestionSource::RemoteURL)
+ existing = move(candidate);
+ continue;
+ }
auto candidate_preference = short_query_origin_preference(candidate, query_length);
auto existing_preference = short_query_origin_preference(existing, query_length);
if (candidate_preference > existing_preference
@@ -267,7 +309,7 @@ Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(
HashMap<String, size_t> origin_counts;
Vector<size_t> deferred_source_indices;
size_t local_navigation_count = 0;
- size_t remote_search_count = 0;
+ size_t remote_suggestion_count = 0;
auto verbatim_index = candidates.find_first_index_if([](auto const& candidate) { return candidate.is_verbatim; });
for (size_t index = 0; index < candidates.size() && result.size() < result_limit; ++index) {
@@ -277,7 +319,7 @@ Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(
&& origin.has_value()
&& origin_counts.get(*origin).value_or(0) >= origin_limit;
auto exceeds_source_limit = (is_local_navigation(candidate) && local_navigation_count >= 4)
- || (is_remote_search(candidate) && remote_search_count >= 3);
+ || (is_remote_suggestion(candidate) && remote_suggestion_count >= 3);
auto must_reserve_verbatim_slot = verbatim_index.has_value()
&& index < *verbatim_index
&& result.size() + 1 == result_limit;
@@ -292,7 +334,7 @@ Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(
if (origin.has_value())
origin_counts.set(*origin, origin_counts.get(*origin).value_or(0) + 1);
local_navigation_count += is_local_navigation(candidate) ? 1 : 0;
- remote_search_count += is_remote_search(candidate) ? 1 : 0;
+ remote_suggestion_count += is_remote_suggestion(candidate) ? 1 : 0;
result.append(candidate);
}
diff --git a/Libraries/LibWebView/AutocompleteMuxer.h b/Libraries/LibWebView/AutocompleteMuxer.h
index 0c8c03a274a..94cd09d3cc2 100644
--- a/Libraries/LibWebView/AutocompleteMuxer.h
+++ b/Libraries/LibWebView/AutocompleteMuxer.h
@@ -13,6 +13,8 @@
namespace WebView {
+// The host of a URL without "www.", which the pages of a site share.
+WEBVIEW_API Optional<String> autocomplete_host_family(URL::URL const&);
WEBVIEW_API bool autocomplete_suggestions_have_same_destination(AutocompleteSuggestion const&, AutocompleteSuggestion const&);
WEBVIEW_API Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(
diff --git a/Libraries/LibWebView/URL.cpp b/Libraries/LibWebView/URL.cpp
index 3c3cb34aaf2..fa1f76761e4 100644
--- a/Libraries/LibWebView/URL.cpp
+++ b/Libraries/LibWebView/URL.cpp
@@ -113,6 +113,13 @@ ClassifiedUserInput classify_user_input(StringView location, AppendTLD append_tl
return {};
}
+ return classify_user_input_ignoring_local_files(location, append_tld);
+}
+
+ClassifiedUserInput classify_user_input_ignoring_local_files(StringView location, AppendTLD append_tld)
+{
+ location = location.trim_whitespace();
+
bool https_scheme_was_guessed = false;
bool input_has_explicit_port = false;
ByteString schemeless_location;
diff --git a/Libraries/LibWebView/URL.h b/Libraries/LibWebView/URL.h
index c0a0d47ae54..55b759786ed 100644
--- a/Libraries/LibWebView/URL.h
+++ b/Libraries/LibWebView/URL.h
@@ -32,6 +32,9 @@ struct ClassifiedUserInput {
};
WEBVIEW_API ClassifiedUserInput classify_user_input(StringView, AppendTLD = AppendTLD::No);
+// Like classify_user_input(), but without checking whether the text names a local file that exists, such as "setup.py".
+// An absolute path is still taken for a file: URL. This suits text that did not come from the user.
+WEBVIEW_API ClassifiedUserInput classify_user_input_ignoring_local_files(StringView, AppendTLD = AppendTLD::No);
WEBVIEW_API Optional<URL::URL> sanitize_url(StringView, Optional<SearchEngine> const& search_engine = {}, AppendTLD = AppendTLD::No);
WEBVIEW_API bool location_looks_like_url(StringView, AppendTLD = AppendTLD::No);
WEBVIEW_API bool is_url_handled_internally(URL::URL const&);
diff --git a/Tests/LibWebView/TestAutocompleteMuxer.cpp b/Tests/LibWebView/TestAutocompleteMuxer.cpp
index f35eb66cb55..7a7bd576377 100644
--- a/Tests/LibWebView/TestAutocompleteMuxer.cpp
+++ b/Tests/LibWebView/TestAutocompleteMuxer.cpp
@@ -481,14 +481,182 @@ TEST_CASE(presentation_match_ranges_are_case_insensitive_and_merged)
TEST_CASE(remote_suggestions_are_filtered_for_a_new_input)
{
- auto suggestions = WebView::filter_remote_autocomplete_suggestions("reddit soc"sv, {
- "reddit soccer"_string,
- "Reddit social"_string,
- "reddit streams"_string,
- "soccer"_string,
- });
+ EXPECT(WebView::remote_autocomplete_suggestion_matches_input("reddit soccer"sv, "reddit soc"sv));
+ EXPECT(WebView::remote_autocomplete_suggestion_matches_input("Reddit social"sv, "reddit soc"sv));
+ EXPECT(!WebView::remote_autocomplete_suggestion_matches_input("reddit streams"sv, "reddit soc"sv));
+ EXPECT(!WebView::remote_autocomplete_suggestion_matches_input("soccer"sv, "reddit soc"sv));
+}
+
+TEST_CASE(remote_web_addresses_are_not_searches)
+{
+ auto suggestions = WebView::make_remote_autocomplete_suggestions({
+ "https://google.com/"_string,
+ "http://example.com/page"_string,
+ "google.com"_string,
+ "google"_string,
+ "https google"_string,
+ });
+
+ EXPECT_EQ(suggestions.size(), 5u);
+ EXPECT(suggestions[0].source == WebView::AutocompleteSuggestionSource::RemoteURL);
+ EXPECT_EQ(suggestions[0].text, "https://google.com/"sv);
+ // Rows do not show the scheme, so an http address would look like the https site.
+ EXPECT(suggestions[1].source == WebView::AutocompleteSuggestionSource::Search);
+ EXPECT_EQ(suggestions[1].text, "http://example.com/page"sv);
+ // An address without a scheme may be a phrase, so it is a search unless the user has been to the site.
+ EXPECT(suggestions[2].source == WebView::AutocompleteSuggestionSource::Search);
+ EXPECT_EQ(suggestions[2].text, "google.com"sv);
+ EXPECT(suggestions[3].source == WebView::AutocompleteSuggestionSource::Search);
+ EXPECT(suggestions[4].source == WebView::AutocompleteSuggestionSource::Search);
+}
+
+TEST_CASE(remote_web_address_without_a_scheme_is_a_page_for_a_visited_site)
+{
+ Vector<WebView::AutocompleteSuggestion> local_suggestions;
+ local_suggestions.append(history("https://www.google.com/maps"sv, 800));
+ local_suggestions.append(search("setup.py"sv, 900, true));
+
+ auto suggestions = WebView::make_remote_autocomplete_suggestions({
+ "google.com"_string,
+ "setup.py"_string,
+ "readme.md"_string,
+ },
+ local_suggestions);
+
+ EXPECT_EQ(suggestions.size(), 3u);
+ EXPECT(suggestions[0].source == WebView::AutocompleteSuggestionSource::RemoteURL);
+ EXPECT_EQ(suggestions[0].text, "https://google.com/"sv);
+ EXPECT(suggestions[1].source == WebView::AutocompleteSuggestionSource::Search);
+ EXPECT(suggestions[2].source == WebView::AutocompleteSuggestionSource::Search);
+}
+
+TEST_CASE(remote_web_address_without_a_scheme_is_a_page_for_a_site_typed_before)
+{
+ for (auto typed_address : { "google.com"sv, "google.com:8443"sv }) {
+ Vector<WebView::AutocompleteSuggestion> local_suggestions;
+ local_suggestions.append(suggestion(WebView::AutocompleteSuggestionSource::Adaptive, typed_address, 800, false));
+
+ auto suggestions = WebView::make_remote_autocomplete_suggestions({ "google.com"_string }, local_suggestions);
+
+ EXPECT_EQ(suggestions.size(), 1u);
+ EXPECT(suggestions[0].source == WebView::AutocompleteSuggestionSource::RemoteURL);
+ }
+}
+
+TEST_CASE(remote_bare_web_address_waits_for_the_local_rows)
+{
+ // Before the local rows arrive, a bare address is left out, as it could still turn out to be a page. One for a site
+ // among the rows so far is a page already, and the rest keep their rank.
+ Vector<WebView::AutocompleteSuggestion> local_suggestions;
+ local_suggestions.append(history("https://www.google.com/maps"sv, 800));
+ auto suggestions = WebView::make_remote_autocomplete_suggestions({ "ladybird.org"_string, "google.com"_string, "google maps"_string },
+ local_suggestions, WebView::LocalSuggestionsAreComplete::No);
EXPECT_EQ(suggestions.size(), 2u);
- EXPECT_EQ(suggestions[0], "reddit soccer"sv);
- EXPECT_EQ(suggestions[1], "Reddit social"sv);
+ EXPECT(suggestions[0].source == WebView::AutocompleteSuggestionSource::RemoteURL);
+ EXPECT_EQ(suggestions[0].text, "https://google.com/"sv);
+ EXPECT_EQ(suggestions[0].relevance, 699);
+ EXPECT(suggestions[1].source == WebView::AutocompleteSuggestionSource::Search);
+ EXPECT_EQ(suggestions[1].text, "google maps"sv);
+ EXPECT_EQ(suggestions[1].relevance, 698);
+}
+
+TEST_CASE(remote_web_address_merges_with_history_for_the_same_address)
+{
+ auto visited = history("https://google.com/"sv, 1000);
+ visited.title = "Google"_string;
+ visited.favicon_png = MUST(ByteBuffer::copy("favicon"sv.bytes()));
+
+ auto results = WebView::mux_autocomplete_suggestions(
+ "https://goo"sv,
+ {},
+ { move(visited) },
+ WebView::make_remote_autocomplete_suggestions({ "https://google.com/"_string }),
+ 8);
+
+ EXPECT_EQ(results.size(), 1u);
+ EXPECT(results[0].source == WebView::AutocompleteSuggestionSource::History);
+ EXPECT_EQ(results[0].title, "Google"sv);
+ EXPECT(results[0].favicon_png.has_value());
+}
+
+TEST_CASE(a_remote_address_before_a_local_row_for_the_same_page_shows_as_the_local_row)
+{
+ auto visited = history("https://example.com/"sv, 900, false);
+ visited.title = "Example"_string;
+
+ auto results = WebView::mux_autocomplete_suggestions(
+ "exa"sv,
+ {},
+ { suggestion(WebView::AutocompleteSuggestionSource::RemoteURL, "https://example.com/"sv, 950, false), move(visited) },
+ {},
+ 8);
+
+ EXPECT_EQ(results.size(), 1u);
+ EXPECT_EQ(results[0].source, WebView::AutocompleteSuggestionSource::History);
+ EXPECT_EQ(results[0].title, "Example"sv);
+}
+
+TEST_CASE(remote_web_address_yields_to_a_local_presentation_of_its_origin)
+{
+ auto visited = history("https://www.google.com/"sv, 200, false);
+ visited.favicon_png = MUST(ByteBuffer::copy("favicon"sv.bytes()));
+
+ auto results = WebView::mux_autocomplete_suggestions(
+ "https://goo"sv,
+ {},
+ { move(visited) },
+ WebView::make_remote_autocomplete_suggestions({ "https://google.com/"_string }),
+ 8);
+
+ EXPECT_EQ(results.size(), 1u);
+ EXPECT_EQ(results[0].text, "https://www.google.com/"sv);
+ EXPECT(results[0].favicon_png.has_value());
+}
+
+TEST_CASE(a_short_query_keeps_a_local_row_over_a_remote_address_of_the_same_site)
+{
+ auto results = WebView::mux_autocomplete_suggestions(
+ "fo"sv,
+ {},
+ { history("https://foo.com/a"sv, 300, false) },
+ WebView::make_remote_autocomplete_suggestions({ "https://foo.com/b"_string }),
+ 8);
+
+ EXPECT(results.contains([](auto const& result) { return result.text == "https://foo.com/a"sv; }));
+ EXPECT(!results.contains([](auto const& result) { return result.text == "https://foo.com/b"sv; }));
+ // The remote address is for another page, so its rank is not the local row's.
+ for (auto const& result : results) {
+ if (result.text == "https://foo.com/a"sv)
+ EXPECT_EQ(result.relevance, 300);
+ }
+}
+
+TEST_CASE(remote_web_address_keeps_a_typed_address)
+{
+ auto results = WebView::mux_autocomplete_suggestions(
+ "https://google.com/"sv,
+ suggestion(WebView::AutocompleteSuggestionSource::LiteralURL, "https://google.com/"sv, 900, true, true),
+ {},
+ WebView::make_remote_autocomplete_suggestions({ "https://google.com/"_string }),
+ 8);
+
+ EXPECT_EQ(results.size(), 1u);
+ EXPECT(results[0].source == WebView::AutocompleteSuggestionSource::LiteralURL);
+ EXPECT(results[0].is_verbatim);
+}
+
+TEST_CASE(remote_suggestions_do_not_link_into_the_users_network)
+{
+ auto suggestions = WebView::make_remote_autocomplete_suggestions({
+ "localhost:631"_string,
+ "192.168.1.1"_string,
+ "http://10.0.0.1/admin"_string,
+ "http://router.local/"_string,
+ "https://example.com:8443/"_string,
+ "http://localhost/"_string,
+ });
+
+ for (auto const& suggestion : suggestions)
+ EXPECT_EQ(suggestion.source, WebView::AutocompleteSuggestionSource::Search);
}
diff --git a/UI/Qt/Autocomplete.cpp b/UI/Qt/Autocomplete.cpp
index 9022ec5763d..a34a63f7c5d 100644
--- a/UI/Qt/Autocomplete.cpp
+++ b/UI/Qt/Autocomplete.cpp
@@ -188,11 +188,14 @@ public:
case HighlightInputRole:
return qstring_from_ak_string(suggestion.highlight_input);
case StartsGroupRole: {
- // Pages that follow search suggestions start a group of their own.
+ // Pages that follow search suggestions start a group of their own. An address the search engine suggested
+ // stays among its other suggestions.
if (suggestion_index == 0)
return false;
- auto is_search = [](auto const& suggestion) { return suggestion.source == WebView::AutocompleteSuggestionSource::Search; };
- return is_search(m_suggestions[suggestion_index - 1]) != is_search(suggestion);
+ auto is_from_search = [](auto const& suggestion) {
+ return suggestion.source == WebView::AutocompleteSuggestionSource::Search || suggestion.source == WebView::AutocompleteSuggestionSource::RemoteURL;
+ };
+ return is_from_search(m_suggestions[suggestion_index - 1]) != is_from_search(suggestion);
}
default:
return {};