Truly independent web browser
LibWebView: Show past searches in history as searches
Andreas Kling0786e5979b8c
diff --git a/Documentation/Omnibox.md b/Documentation/Omnibox.mdindex 829700474dd..3b34d54cda9 100644--- a/Documentation/Omnibox.md+++ b/Documentation/Omnibox.md@@ -184,6 +184,7 @@ The current limits are soft:- At most four local navigation rows before deferred local rows.- At most three remote rows before deferred remote rows.+- At most three past searches before deferred past searches. They do not count as 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.cppindex ee12af339c9..3d43d7dd36a 100644--- a/Libraries/LibWebView/Autocomplete.cpp+++ b/Libraries/LibWebView/Autocomplete.cpp@@ -523,6 +523,7 @@ void Autocomplete::local_query_complete(AutocompleteQueryID query_id, Vector<Autreturn;m_local_suggestions = move(suggestions);+ show_search_results_pages_as_searches(m_trimmed_query, m_local_suggestions, m_host.autocomplete_settings().search_engine_settings().engine);add_visited_host_families(m_visited_host_families, m_local_suggestions);m_local_query_complete = true;diff --git a/Libraries/LibWebView/AutocompleteMuxer.cpp b/Libraries/LibWebView/AutocompleteMuxer.cppindex 6e4440ec0ce..d7110ba7611 100644--- a/Libraries/LibWebView/AutocompleteMuxer.cpp+++ b/Libraries/LibWebView/AutocompleteMuxer.cpp@@ -5,6 +5,7 @@*/#include <AK/Array.h>+#include <AK/CharacterTypes.h>#include <AK/HashMap.h>#include <AK/QuickSort.h>#include <AK/Utf8View.h>@@ -54,6 +55,129 @@ static QueryParameter split_query_parameter(StringView parameter)return { parameter.substring_view(0, *equals), parameter.substring_view(*equals + 1) };}+// Query parameters that search engines add to their results pages, which say how the search was made but do not change+// what it finds.+static bool is_search_bookkeeping_query_parameter(StringView name, StringView value)+{+ static constexpr Array bookkeeping_parameter_names {+ "aqs"sv, "atb"sv, "bih"sv, "biw"sv, "client"sv, "cvid"sv, "dpr"sv, "ei"sv, "form"sv, "ghc"sv, "gs_lcrp"sv,+ "gs_lp"sv, "ie"sv, "iflsig"sv, "oe"sv, "oq"sv, "pq"sv, "qs"sv, "rlz"sv, "sa"sv, "sc"sv, "sca_esv"sv, "sca_upv"sv,+ "sclient"sv, "sk"sv, "source"sv, "sourceid"sv, "sxsrf"sv, "t"sv, "uact"sv, "ved"sv+ };+ // NB: These name the results' tab. Only the tab of web results is the plain search.+ if (name.equals_ignoring_ascii_case("ia"sv) || name.equals_ignoring_ascii_case("cat"sv))+ return value.equals_ignoring_ascii_case("web"sv);+ return any_of(bookkeeping_parameter_names, [&](auto bookkeeping_name) { return name.equals_ignoring_ascii_case(bookkeeping_name); });+}++static Optional<String> decode_query_parameter_value(StringView value)+{+ auto plus_as_space = ByteString { value }.replace("+"sv, " "sv, ReplaceMode::All);+ auto decoded = URL::percent_decode(plus_as_space);+ auto result = String::from_byte_string(decoded);+ if (result.is_error())+ return {};+ return result.release_value();+}++// The search terms of a page of the search engine's results, when the page shows nothing but the results for them.+static Optional<String> search_terms_of_results_page(URL::URL const& url, URL::URL const& results_url)+{+ if (!results_url.query().has_value() || url.fragment().has_value() || !url.query().has_value())+ return {};+ if (url.scheme() != results_url.scheme() || url.port() != results_url.port() || url.serialize_path() != results_url.serialize_path())+ return {};+ if (autocomplete_host_family(url) != autocomplete_host_family(results_url))+ return {};++ Optional<StringView> terms_parameter_name;+ Vector<StringView> fixed_parameters;+ for (auto parameter : results_url.query()->split_view('&')) {+ auto [name, value] = split_query_parameter(parameter);+ if (value == "%s"sv)+ terms_parameter_name = name;+ // NB: A parameter that is ignored on a results page, such as which client made the search, is not required of+ // one either.+ else if (!is_tracking_query_parameter(name) && !is_search_bookkeeping_query_parameter(name, value))+ fixed_parameters.append(parameter);+ }+ if (!terms_parameter_name.has_value())+ return {};++ // NB: Each fixed parameter is crossed off once, so that a repeated one cannot stand in for one that is missing.+ Optional<String> terms;+ for (auto parameter : url.query()->split_view('&')) {+ auto [name, value] = split_query_parameter(parameter);+ if (name == *terms_parameter_name) {+ if (terms.has_value())+ return {};+ terms = decode_query_parameter_value(value);+ if (!terms.has_value())+ return {};+ } else if (auto fixed_index = fixed_parameters.find_first_index(parameter); fixed_index.has_value()) {+ fixed_parameters.remove(*fixed_index);+ } else if (!is_tracking_query_parameter(name) && !is_search_bookkeeping_query_parameter(name, value)) {+ return {};+ }+ }+ if (!fixed_parameters.is_empty() || !terms.has_value())+ return {};++ auto trimmed_terms = terms->bytes_as_string_view().trim_whitespace();+ if (trimmed_terms.is_empty())+ return {};+ return MUST(String::from_utf8(trimmed_terms));+}++// NB: Search terms are raw phrases, so case is folded beyond ASCII: "über" starts a word of "Über alles".+static bool folded_query_starts_a_word_of(StringView folded_query, StringView terms)+{+ auto folded_terms = MUST(MUST(String::from_utf8(terms)).to_casefold());+ auto text = folded_terms.bytes_as_string_view();+ if (text.starts_with(folded_query))+ return true;+ for (size_t index = 1; index < text.length(); ++index) {+ // NB: A word starts after a space or a mark such as a quote or hyphen. Bytes beyond ASCII are part of a letter.+ auto previous = text[index - 1];+ if (is_ascii(previous) && !is_ascii_alphanumeric(previous) && text.substring_view(index).starts_with(folded_query))+ return true;+ }+ return false;+}++void show_search_results_pages_as_searches(StringView query, Vector<AutocompleteSuggestion>& suggestions, Optional<SearchEngine> const& search_engine)+{+ if (!search_engine.has_value())+ return;+ auto results_url = URL::Parser::basic_parse(search_engine->query_url);+ if (!results_url.has_value())+ return;+ auto folded_query = MUST(MUST(String::from_utf8(query)).to_casefold());++ for (auto& suggestion : suggestions) {+ if (suggestion.source != AutocompleteSuggestionSource::History && suggestion.source != AutocompleteSuggestionSource::Adaptive)+ continue;+ auto url = URL::Parser::basic_parse(suggestion.text);+ if (!url.has_value())+ continue;+ auto terms = search_terms_of_results_page(*url, *results_url);+ // NB: A results page stays a page unless the query starts a word of its terms, so one found only by its address+ // or title is not shown as a search.+ if (!terms.has_value() || !folded_query_starts_a_word_of(folded_query, *terms))+ continue;++ suggestion.source = AutocompleteSuggestionSource::Search;+ suggestion.text = *terms;+ suggestion.title = terms.release_value();+ suggestion.subtitle = {};+ suggestion.favicon_png = {};+ suggestion.match_class = AutocompleteMatchClass::None;+ // NB: A past search is offered, but Return goes to the text as typed or to an address it completes to.+ suggestion.can_be_automatically_selected = false;+ suggestion.can_be_inline_completed = false;+ }+}+static Optional<URL::URL> url_of_suggestion(AutocompleteSuggestion const& suggestion){// NB: Most rows hold serialized URLs. The row for the text as typed holds that text, and a learned row holds the@@ -243,13 +367,22 @@ static bool is_local_navigation(AutocompleteSuggestion const& suggestion)return suggestion.source != AutocompleteSuggestionSource::Search && suggestion.source != AutocompleteSuggestionSource::RemoteURL;}+// A search the user made before, found in their history or learned from the omnibox, ranks by how well it matches.+static bool is_past_search(AutocompleteSuggestion const& suggestion)+{+ return suggestion.source == AutocompleteSuggestionSource::Search+ && (suggestion.match_relevance != 0 || suggestion.history_relevance != 0 || suggestion.adaptive_relevance != 0);+}+static bool is_remote_suggestion(AutocompleteSuggestion const& suggestion, String const& normalized_query){if (suggestion.source == AutocompleteSuggestionSource::RemoteURL)return true;// NB: The search that follows a typed address like "setup.py" is for the text as typed, not a remote suggestion.+ // NB: A past search is the user's own, so it does not take the place of what the search engine suggests.return suggestion.source == AutocompleteSuggestionSource::Search&& !suggestion.is_verbatim+ && !is_past_search(suggestion)&& normalize_omnibox_destination(suggestion.text, OmniboxDestinationKind::Search) != normalized_query;}@@ -452,6 +585,7 @@ Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(Vector<size_t> deferred_source_indices;size_t local_navigation_count = 0;size_t remote_suggestion_count = 0;+ size_t past_search_count = 0;auto normalized_query = normalize_omnibox_destination(query, OmniboxDestinationKind::Search);auto verbatim_index = candidates.find_first_index_if([](auto const& candidate) { return candidate.is_verbatim; });@@ -462,8 +596,12 @@ Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(&& origin.has_value()&& origin_counts.get(*origin).value_or(0) >= origin_limit;auto candidate_is_remote_suggestion = is_remote_suggestion(candidate, normalized_query);+ // NB: Past searches take no remote slot, but have a limit of their own, so that many results pages in history+ // cannot fill the list.+ auto candidate_is_past_search = !candidate.is_verbatim && is_past_search(candidate);auto exceeds_source_limit = (is_local_navigation(candidate) && local_navigation_count >= 4)- || (candidate_is_remote_suggestion && remote_suggestion_count >= 3);+ || (candidate_is_remote_suggestion && remote_suggestion_count >= 3)+ || (candidate_is_past_search && past_search_count >= 3);auto must_reserve_verbatim_slot = verbatim_index.has_value()&& index < *verbatim_index&& result.size() + 1 == result_limit;@@ -479,6 +617,7 @@ Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(origin_counts.set(*origin, origin_counts.get(*origin).value_or(0) + 1);local_navigation_count += is_local_navigation(candidate) ? 1 : 0;remote_suggestion_count += candidate_is_remote_suggestion ? 1 : 0;+ past_search_count += candidate_is_past_search ? 1 : 0;result.append(candidate);}diff --git a/Libraries/LibWebView/AutocompleteMuxer.h b/Libraries/LibWebView/AutocompleteMuxer.hindex bf188c9b960..1fa67429cb9 100644--- a/Libraries/LibWebView/AutocompleteMuxer.h+++ b/Libraries/LibWebView/AutocompleteMuxer.h@@ -18,6 +18,10 @@ WEBVIEW_API Optional<String> autocomplete_host_family(URL::URL 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>);+// Rows for pages of the search engine's results, for search terms that the query starts a word of, become the searches+// for those terms.+WEBVIEW_API void show_search_results_pages_as_searches(StringView query, Vector<AutocompleteSuggestion>&, Optional<SearchEngine> const&);+WEBVIEW_API Vector<AutocompleteSuggestion> mux_autocomplete_suggestions(StringView query,Optional<AutocompleteSuggestion> verbatim_suggestion,diff --git a/Tests/LibWebView/TestAutocompleteMuxer.cpp b/Tests/LibWebView/TestAutocompleteMuxer.cppindex 36ee34b0288..b5b986948cd 100644--- a/Tests/LibWebView/TestAutocompleteMuxer.cpp+++ b/Tests/LibWebView/TestAutocompleteMuxer.cpp@@ -7,6 +7,7 @@#include <LibTest/TestCase.h>#include <LibWebView/AutocompleteMuxer.h>#include <LibWebView/OmniboxEngagement.h>+#include <LibWebView/SearchEngine.h>namespace {@@ -354,6 +355,66 @@ TEST_CASE(typed_address_keeps_its_text_when_merged_with_a_history_row_and_a_bookEXPECT(results[0].is_verbatim);}+static Optional<WebView::SearchEngine> duckduckgo()+{+ return WebView::builtin_search_engines().first_matching([](auto const& engine) { return engine.name == "DuckDuckGo"sv; }).copy();+}++TEST_CASE(a_results_page_of_the_search_engine_shows_as_its_search)+{+ Vector<WebView::AutocompleteSuggestion> local_rows {+ history("https://duckduckgo.com/?q=ladybird+browser&t=h_&ia=web"sv, 1000),+ history("https://duckduckgo.com/?q=ladybird%20browser"sv, 990),+ };+ WebView::show_search_results_pages_as_searches("lady"sv, local_rows, duckduckgo());++ auto results = WebView::mux_autocomplete_suggestions(+ "lady"sv,+ search("lady"sv, 900, true),+ move(local_rows),+ WebView::make_remote_autocomplete_suggestions({ "ladybird browser"_string }),+ 8);++ // The two pages and the search engine's suggestion are one search, and Return still searches for the typed text.+ EXPECT_EQ(results.size(), 2u);+ EXPECT_EQ(results[0].text, "lady"sv);+ EXPECT_EQ(results[1].source, WebView::AutocompleteSuggestionSource::Search);+ EXPECT_EQ(results[1].text, "ladybird browser"sv);+ EXPECT(!results[1].can_be_automatically_selected);+}++TEST_CASE(a_results_page_that_is_more_than_the_search_stays_a_page)+{+ Vector<WebView::AutocompleteSuggestion> local_rows {+ history("https://duckduckgo.com/?q=ladybird&ia=images"sv, 1000),+ history("https://www.google.com/search?q=ladybird"sv, 990),+ history("https://duckduckgo.com/?q=ladybird#more"sv, 980),+ };+ WebView::show_search_results_pages_as_searches("lady"sv, local_rows, duckduckgo());+ for (auto const& row : local_rows)+ EXPECT_EQ(row.source, WebView::AutocompleteSuggestionSource::History);++ // The page was found by its address, not by what was searched for.+ Vector<WebView::AutocompleteSuggestion> found_by_address { history("https://duckduckgo.com/?q=ladybird"sv, 1000) };+ WebView::show_search_results_pages_as_searches("duck"sv, found_by_address, duckduckgo());+ EXPECT_EQ(found_by_address[0].source, WebView::AutocompleteSuggestionSource::History);+}++TEST_CASE(a_results_page_shows_as_its_search_only_for_a_query_that_starts_a_word)+{+ auto row_for = [](StringView query, StringView url) {+ Vector<WebView::AutocompleteSuggestion> rows { history(url, 1000) };+ WebView::show_search_results_pages_as_searches(query, rows, duckduckgo());+ return rows.take_first();+ };++ auto other_case = row_for("über"sv, "https://duckduckgo.com/?q=%C3%9Cber+alles"sv);+ EXPECT_EQ(other_case.source, WebView::AutocompleteSuggestionSource::Search);+ EXPECT_EQ(other_case.text, "Über alles"sv);+ EXPECT_EQ(row_for("ladybird"sv, "https://duckduckgo.com/?q=%22ladybird%22+browser"sv).source, WebView::AutocompleteSuggestionSource::Search);+ EXPECT_EQ(row_for("bersee"sv, "https://duckduckgo.com/?q=%C3%BCbersee"sv).source, WebView::AutocompleteSuggestionSource::History);+}+TEST_CASE(http_and_https_destinations_are_not_deduplicated){auto results = WebView::mux_autocomplete_suggestions(diff --git a/Tests/LibWebView/TestOmnibox.cpp b/Tests/LibWebView/TestOmnibox.cppindex 436f72d8f08..9c95b6fcafa 100644--- a/Tests/LibWebView/TestOmnibox.cpp+++ b/Tests/LibWebView/TestOmnibox.cpp@@ -1719,6 +1719,7 @@ struct ScenarioProfile {local_rows.extend(WebView::rank_history_suggestions(trimmed_query, history, limit, scenario_now()));local_rows.extend(WebView::rank_bookmark_suggestions(trimmed_query, bookmarks, limit));local_rows.extend(WebView::rank_engagement_suggestions(trimmed_query, engagements, limit, scenario_now()));+ WebView::show_search_results_pages_as_searches(trimmed_query, local_rows, search_engine);auto remote_rows = WebView::make_remote_autocomplete_suggestions(move(remote_suggestions), local_rows);auto rows = WebView::mux_autocomplete_suggestions(