Skip to content

Truly independent web browser

LibWebView: Work out where each autocomplete row goes once

Andreas Kling2c58084c8542
diff --git a/Libraries/LibWebView/Autocomplete.h b/Libraries/LibWebView/Autocomplete.h
index b75ba73dd67..802469d006b 100644
--- a/Libraries/LibWebView/Autocomplete.h
+++ b/Libraries/LibWebView/Autocomplete.h
@@ -120,7 +120,6 @@ WEBVIEW_API Optional<AutocompleteSuggestion> make_verbatim_autocomplete_suggesti
WEBVIEW_API Optional<AutocompleteSuggestion> make_search_suggestion_for_address_like_text(StringView query, ClassifiedUserInput const&, Optional<SearchEngine> const&);
WEBVIEW_API Vector<AutocompleteSuggestion> web_ui_autocomplete_suggestions(StringView input);
WEBVIEW_API size_t autocomplete_preliminary_limit(size_t max_suggestions);
-WEBVIEW_API bool autocomplete_urls_match(StringView left, StringView right);
WEBVIEW_API bool autocomplete_url_can_complete(StringView query, StringView suggestion);
// The application that runs the omnibox supplies the things suggestions are made from.
diff --git a/Libraries/LibWebView/AutocompleteMuxer.cpp b/Libraries/LibWebView/AutocompleteMuxer.cpp
index 5cc05e36f17..d77da295f00 100644
--- a/Libraries/LibWebView/AutocompleteMuxer.cpp
+++ b/Libraries/LibWebView/AutocompleteMuxer.cpp
@@ -13,14 +13,65 @@
namespace WebView {
-bool autocomplete_suggestions_have_same_destination(AutocompleteSuggestion const& left, AutocompleteSuggestion const& right)
+namespace {
+
+// OPTIMIZATION: Rows are compared with each other row, so where each row goes is worked out once, rather than for each
+// pair of rows.
+struct SuggestionDestination {
+ bool is_search { false };
+ // A row whose text goes nowhere has none, and goes where no other row goes.
+ Optional<String> key;
+};
+
+}
+
+static SuggestionDestination destination_of_suggestion(AutocompleteSuggestion const& suggestion)
{
- if (left.source == AutocompleteSuggestionSource::Search || right.source == AutocompleteSuggestionSource::Search) {
- return left.source == right.source
- && normalize_omnibox_destination(left.text, OmniboxDestinationKind::Search) == normalize_omnibox_destination(right.text, OmniboxDestinationKind::Search);
+ if (suggestion.source == AutocompleteSuggestionSource::Search)
+ return { .is_search = true, .key = normalize_omnibox_destination(suggestion.text, OmniboxDestinationKind::Search) };
+
+ // NB: Most rows hold serialized URLs. The row for the text as typed holds that text, and a learned row holds the
+ // address as the user typed it, unless they chose a row for it. Only such text is classified, as that looks at
+ // the file system.
+ Optional<URL::URL> url;
+ if (suggestion.source != AutocompleteSuggestionSource::LiteralURL)
+ url = URL::Parser::basic_parse(suggestion.text);
+ // NB: The URL parser takes typed text such as "localhost:3000" for a scheme and a path, so a learned row that
+ // parses that way is classified as Return would take it.
+ if (url.has_value() && suggestion.source == AutocompleteSuggestionSource::Adaptive && url->has_an_opaque_path())
+ url.clear();
+ if (!url.has_value())
+ url = classify_user_input(suggestion.text).url;
+ if (!url.has_value())
+ url = URL::Parser::basic_parse(MUST(String::formatted("https://{}", suggestion.text)));
+ if (!url.has_value())
+ return {};
+ return { .is_search = false, .key = url->serialize() };
+}
+
+static bool suggestion_destinations_are_the_same(SuggestionDestination const& left, SuggestionDestination const& right)
+{
+ return left.is_search == right.is_search && left.key.has_value() && left.key == right.key;
+}
+
+Optional<size_t> index_of_suggestion_with_same_destination(AutocompleteSuggestion const& needle, ReadonlySpan<AutocompleteSuggestion> suggestions)
+{
+ // OPTIMIZATION: The row looked for is usually still there as it was, and working out where the row for the text as
+ // typed goes looks at the file system, so rows with the same text are looked for first. The muxer
+ // never gives two rows that go to the same place, so this is still the first that goes there.
+ for (size_t index = 0; index < suggestions.size(); ++index) {
+ if (suggestions[index].source == needle.source && suggestions[index].text == needle.text)
+ return index;
}
- return autocomplete_urls_match(left.text, right.text);
+ auto needle_destination = destination_of_suggestion(needle);
+ if (!needle_destination.key.has_value())
+ return {};
+ for (size_t index = 0; index < suggestions.size(); ++index) {
+ if (suggestion_destinations_are_the_same(needle_destination, destination_of_suggestion(suggestions[index])))
+ return index;
+ }
+ return {};
}
// A local row for the same page as a remote address ranks as the remote address did, so the popup keeps the rank the
@@ -82,15 +133,17 @@ static void merge_suggestion(AutocompleteSuggestion& existing, AutocompleteSugge
existing.adaptive_relevance = adaptive_relevance;
}
-static void append_or_merge(Vector<AutocompleteSuggestion>& suggestions, AutocompleteSuggestion suggestion)
+static void append_or_merge(Vector<AutocompleteSuggestion>& suggestions, Vector<SuggestionDestination>& destinations, AutocompleteSuggestion suggestion)
{
- for (auto& existing : suggestions) {
- if (!autocomplete_suggestions_have_same_destination(existing, suggestion))
+ auto destination = destination_of_suggestion(suggestion);
+ for (size_t index = 0; index < suggestions.size(); ++index) {
+ if (!suggestion_destinations_are_the_same(destinations[index], destination))
continue;
- merge_suggestion(existing, move(suggestion));
+ merge_suggestion(suggestions[index], move(suggestion));
return;
}
suggestions.append(move(suggestion));
+ destinations.append(move(destination));
}
Optional<String> autocomplete_host_family(URL::URL const& url)
@@ -253,13 +306,16 @@ Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(
&& verbatim_suggestion->source == AutocompleteSuggestionSource::LiteralURL;
Vector<AutocompleteSuggestion> candidates;
- candidates.ensure_capacity(local_suggestions.size() + remote_suggestions.size() + (verbatim_suggestion.has_value() ? 1 : 0));
+ Vector<SuggestionDestination> candidate_destinations;
+ auto candidate_capacity = local_suggestions.size() + remote_suggestions.size() + (verbatim_suggestion.has_value() ? 1 : 0);
+ candidates.ensure_capacity(candidate_capacity);
+ candidate_destinations.ensure_capacity(candidate_capacity);
if (verbatim_suggestion.has_value())
- append_or_merge(candidates, verbatim_suggestion.release_value());
+ append_or_merge(candidates, candidate_destinations, verbatim_suggestion.release_value());
for (auto& suggestion : local_suggestions)
- append_or_merge(candidates, move(suggestion));
+ append_or_merge(candidates, candidate_destinations, move(suggestion));
for (auto& suggestion : remote_suggestions)
- append_or_merge(candidates, move(suggestion));
+ append_or_merge(candidates, candidate_destinations, move(suggestion));
auto query_contains_path = query_contains_url_suffix(query);
auto query_length = Utf8View { query }.length();
diff --git a/Libraries/LibWebView/AutocompleteMuxer.h b/Libraries/LibWebView/AutocompleteMuxer.h
index 94cd09d3cc2..bf188c9b960 100644
--- a/Libraries/LibWebView/AutocompleteMuxer.h
+++ b/Libraries/LibWebView/AutocompleteMuxer.h
@@ -15,7 +15,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&);
+// The first of the suggestions that goes where the needle goes.
+WEBVIEW_API Optional<size_t> index_of_suggestion_with_same_destination(AutocompleteSuggestion const& needle, ReadonlySpan<AutocompleteSuggestion>);
WEBVIEW_API Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(
StringView query,
diff --git a/Libraries/LibWebView/Omnibox.cpp b/Libraries/LibWebView/Omnibox.cpp
index 4022b8754ea..9e63b5e6a45 100644
--- a/Libraries/LibWebView/Omnibox.cpp
+++ b/Libraries/LibWebView/Omnibox.cpp
@@ -104,9 +104,7 @@ static Optional<size_t> index_of_suggestion(String const& suggestion_text, Vecto
static Optional<size_t> index_of_suggestion(AutocompleteSuggestion const& needle, Vector<AutocompleteSuggestion> const& suggestions)
{
- return suggestions.find_first_index_if([&](auto const& suggestion) {
- return autocomplete_suggestions_have_same_destination(needle, suggestion);
- });
+ return index_of_suggestion_with_same_destination(needle, suggestions);
}
static Optional<size_t> index_of_automatic_default(Vector<AutocompleteSuggestion> const& suggestions)
diff --git a/Libraries/LibWebView/URL.cpp b/Libraries/LibWebView/URL.cpp
index fa1f76761e4..367083c7d6c 100644
--- a/Libraries/LibWebView/URL.cpp
+++ b/Libraries/LibWebView/URL.cpp
@@ -241,23 +241,6 @@ Optional<URL::URL> url_from_text(StringView text)
return sanitize_url(trimmed_text);
}
-bool autocomplete_urls_match(StringView left, StringView right)
-{
- auto parse_destination = [](StringView value) {
- auto classified_input = classify_user_input(value);
- if (classified_input.url.has_value())
- return classified_input.url;
- return URL::Parser::basic_parse(MUST(String::formatted("https://{}", value)));
- };
-
- auto left_url = parse_destination(left);
- auto right_url = parse_destination(right);
- if (!left_url.has_value() || !right_url.has_value())
- return false;
-
- return left_url->equals(*right_url);
-}
-
bool autocomplete_url_can_complete(StringView query, StringView suggestion)
{
auto suggestion_url = URL::Parser::basic_parse(suggestion);
diff --git a/Tests/LibWebView/TestAutocompleteMuxer.cpp b/Tests/LibWebView/TestAutocompleteMuxer.cpp
index 6fca0f4f5f0..5915888eadc 100644
--- a/Tests/LibWebView/TestAutocompleteMuxer.cpp
+++ b/Tests/LibWebView/TestAutocompleteMuxer.cpp
@@ -683,6 +683,23 @@ TEST_CASE(remote_suggestions_do_not_link_into_the_users_network)
EXPECT_EQ(suggestion.source, WebView::AutocompleteSuggestionSource::Search);
}
+TEST_CASE(learned_address_with_a_port_is_one_row_with_its_page)
+{
+ auto results = WebView::mux_autocomplete_suggestions(
+ "local"sv,
+ search("local"sv, 900, true),
+ {
+ suggestion(WebView::AutocompleteSuggestionSource::Adaptive, "localhost:3000"sv, 1100, true),
+ history("http://localhost:3000/"sv, 1000),
+ },
+ {},
+ 8);
+
+ EXPECT_EQ(results.size(), 2u);
+ EXPECT_EQ(results[0].text, "http://localhost:3000/"sv);
+ EXPECT_EQ(results[1].text, "local"sv);
+}
+
TEST_CASE(the_row_for_a_typed_address_is_not_folded_into_another_host)
{
auto results = WebView::mux_autocomplete_suggestions(
diff --git a/Tests/LibWebView/TestWebViewURL.cpp b/Tests/LibWebView/TestWebViewURL.cpp
index 97d3e2a445b..d633cd9d8c7 100644
--- a/Tests/LibWebView/TestWebViewURL.cpp
+++ b/Tests/LibWebView/TestWebViewURL.cpp
@@ -7,6 +7,7 @@
#include <LibTest/TestCase.h>
#include <LibWebView/Autocomplete.h>
+#include <LibWebView/AutocompleteMuxer.h>
#include <LibWebView/SearchEngine.h>
#include <LibWebView/URL.h>
@@ -81,14 +82,28 @@ static void expect_context_menu_text_not_url(StringView text)
EXPECT(!WebView::url_from_text(text).has_value());
}
+static WebView::AutocompleteSuggestion typed_text_row(StringView text)
+{
+ WebView::AutocompleteSuggestion row;
+ row.source = WebView::AutocompleteSuggestionSource::LiteralURL;
+ row.text = MUST(String::from_utf8(text));
+ return row;
+}
+
+static bool autocomplete_rows_go_to_the_same_place(StringView left, StringView right)
+{
+ auto right_row = typed_text_row(right);
+ return WebView::index_of_suggestion_with_same_destination(typed_text_row(left), { &right_row, 1 }).has_value();
+}
+
static void expect_autocomplete_urls_match(StringView left, StringView right)
{
- EXPECT(WebView::autocomplete_urls_match(left, right));
+ EXPECT(autocomplete_rows_go_to_the_same_place(left, right));
}
static void expect_autocomplete_urls_do_not_match(StringView left, StringView right)
{
- EXPECT(!WebView::autocomplete_urls_match(left, right));
+ EXPECT(!autocomplete_rows_go_to_the_same_place(left, right));
}
static void expect_autocomplete_url_can_complete(StringView query, StringView suggestion)