diff options
author | blundell <blundell@chromium.org> | 2015-06-16 23:49:03 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-17 06:50:29 +0000 |
commit | 66c202106ea7ab3f3c078a5edfb4abf0d23cee94 (patch) | |
tree | ec08ca46ad37f3f76539440fef0817df868d42f9 /chrome/browser/autocomplete | |
parent | e7cd12af5b21f71f1e8dc8a2f5dfb476d44afa0b (diff) | |
download | chromium_src-66c202106ea7ab3f3c078a5edfb4abf0d23cee94.zip chromium_src-66c202106ea7ab3f3c078a5edfb4abf0d23cee94.tar.gz chromium_src-66c202106ea7ab3f3c078a5edfb4abf0d23cee94.tar.bz2 |
Componentize HistoryURLProvider/ScoredHistoryMatch.
No changes are needed.
BUG=371538
Review URL: https://codereview.chromium.org/1181113005
Cr-Commit-Position: refs/heads/master@{#334780}
Diffstat (limited to 'chrome/browser/autocomplete')
13 files changed, 8 insertions, 2909 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_controller.cc b/chrome/browser/autocomplete/autocomplete_controller.cc index f6da70a..bd8de21 100644 --- a/chrome/browser/autocomplete/autocomplete_controller.cc +++ b/chrome/browser/autocomplete/autocomplete_controller.cc @@ -16,12 +16,12 @@ #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" #include "chrome/browser/autocomplete/builtin_provider.h" #include "chrome/browser/autocomplete/history_quick_provider.h" -#include "chrome/browser/autocomplete/history_url_provider.h" #include "chrome/browser/autocomplete/in_memory_url_index_factory.h" #include "chrome/browser/autocomplete/shortcuts_provider.h" #include "chrome/browser/autocomplete/zero_suggest_provider.h" #include "chrome/browser/chrome_notification_types.h" #include "components/omnibox/bookmark_provider.h" +#include "components/omnibox/history_url_provider.h" #include "components/omnibox/keyword_provider.h" #include "components/omnibox/omnibox_field_trial.h" #include "components/omnibox/search_provider.h" diff --git a/chrome/browser/autocomplete/history_quick_provider.cc b/chrome/browser/autocomplete/history_quick_provider.cc index 705b3f5..d67dab8 100644 --- a/chrome/browser/autocomplete/history_quick_provider.cc +++ b/chrome/browser/autocomplete/history_quick_provider.cc @@ -16,7 +16,6 @@ #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" -#include "chrome/browser/autocomplete/history_url_provider.h" #include "chrome/browser/autocomplete/in_memory_url_index.h" #include "chrome/browser/bookmarks/bookmark_model_factory.h" #include "chrome/browser/profiles/profile.h" @@ -31,6 +30,7 @@ #include "components/metrics/proto/omnibox_input_type.pb.h" #include "components/omnibox/autocomplete_match_type.h" #include "components/omnibox/autocomplete_result.h" +#include "components/omnibox/history_url_provider.h" #include "components/omnibox/in_memory_url_index_types.h" #include "components/omnibox/omnibox_field_trial.h" #include "components/search_engines/template_url.h" diff --git a/chrome/browser/autocomplete/history_quick_provider_unittest.cc b/chrome/browser/autocomplete/history_quick_provider_unittest.cc index eefa7b3..d1b8cb7 100644 --- a/chrome/browser/autocomplete/history_quick_provider_unittest.cc +++ b/chrome/browser/autocomplete/history_quick_provider_unittest.cc @@ -18,7 +18,6 @@ #include "base/strings/utf_string_conversions.h" #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" -#include "chrome/browser/autocomplete/history_url_provider.h" #include "chrome/browser/autocomplete/in_memory_url_index.h" #include "chrome/browser/autocomplete/in_memory_url_index_factory.h" #include "chrome/browser/autocomplete/url_index_private_data.h" @@ -37,6 +36,7 @@ #include "components/metrics/proto/omnibox_event.pb.h" #include "components/omnibox/autocomplete_match.h" #include "components/omnibox/autocomplete_result.h" +#include "components/omnibox/history_url_provider.h" #include "components/search_engines/search_terms_data.h" #include "components/search_engines/template_url.h" #include "components/search_engines/template_url_service.h" diff --git a/chrome/browser/autocomplete/history_url_provider.cc b/chrome/browser/autocomplete/history_url_provider.cc deleted file mode 100644 index 4765b48..0000000 --- a/chrome/browser/autocomplete/history_url_provider.cc +++ /dev/null @@ -1,1170 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/autocomplete/history_url_provider.h" - -#include <algorithm> - -#include "base/basictypes.h" -#include "base/bind.h" -#include "base/command_line.h" -#include "base/location.h" -#include "base/message_loop/message_loop.h" -#include "base/metrics/histogram.h" -#include "base/prefs/pref_service.h" -#include "base/single_thread_task_runner.h" -#include "base/strings/string_util.h" -#include "base/strings/utf_string_conversions.h" -#include "base/time/time.h" -#include "chrome/browser/autocomplete/scored_history_match.h" -#include "components/bookmarks/browser/bookmark_utils.h" -#include "components/history/core/browser/history_backend.h" -#include "components/history/core/browser/history_database.h" -#include "components/history/core/browser/history_service.h" -#include "components/history/core/browser/history_types.h" -#include "components/metrics/proto/omnibox_input_type.pb.h" -#include "components/omnibox/autocomplete_match.h" -#include "components/omnibox/autocomplete_provider_listener.h" -#include "components/omnibox/autocomplete_result.h" -#include "components/omnibox/in_memory_url_index_types.h" -#include "components/omnibox/omnibox_field_trial.h" -#include "components/omnibox/url_prefix.h" -#include "components/search_engines/search_terms_data.h" -#include "components/search_engines/template_url_service.h" -#include "components/url_fixer/url_fixer.h" -#include "net/base/net_util.h" -#include "net/base/registry_controlled_domains/registry_controlled_domain.h" -#include "url/gurl.h" -#include "url/third_party/mozilla/url_parse.h" -#include "url/url_util.h" - -namespace { - -// Acts like the > operator for URLInfo classes. -bool CompareHistoryMatch(const history::HistoryMatch& a, - const history::HistoryMatch& b) { - // A URL that has been typed at all is better than one that has never been - // typed. (Note "!"s on each side) - if (!a.url_info.typed_count() != !b.url_info.typed_count()) - return a.url_info.typed_count() > b.url_info.typed_count(); - - // Innermost matches (matches after any scheme or "www.") are better than - // non-innermost matches. - if (a.innermost_match != b.innermost_match) - return a.innermost_match; - - // URLs that have been typed more often are better. - if (a.url_info.typed_count() != b.url_info.typed_count()) - return a.url_info.typed_count() > b.url_info.typed_count(); - - // For URLs that have each been typed once, a host (alone) is better than a - // page inside. - if ((a.url_info.typed_count() == 1) && (a.IsHostOnly() != b.IsHostOnly())) - return a.IsHostOnly(); - - // URLs that have been visited more often are better. - if (a.url_info.visit_count() != b.url_info.visit_count()) - return a.url_info.visit_count() > b.url_info.visit_count(); - - // URLs that have been visited more recently are better. - return a.url_info.last_visit() > b.url_info.last_visit(); -} - -// Sorts and dedups the given list of matches. -void SortAndDedupMatches(history::HistoryMatches* matches) { - // Sort by quality, best first. - std::sort(matches->begin(), matches->end(), &CompareHistoryMatch); - - // Remove duplicate matches (caused by the search string appearing in one of - // the prefixes as well as after it). Consider the following scenario: - // - // User has visited "http://http.com" once and "http://htaccess.com" twice. - // User types "http". The autocomplete search with prefix "http://" returns - // the first host, while the search with prefix "" returns both hosts. Now - // we sort them into rank order: - // http://http.com (innermost_match) - // http://htaccess.com (!innermost_match, url_info.visit_count == 2) - // http://http.com (!innermost_match, url_info.visit_count == 1) - // - // The above scenario tells us we can't use std::unique(), since our - // duplicates are not always sequential. It also tells us we should remove - // the lower-quality duplicate(s), since otherwise the returned results won't - // be ordered correctly. This is easy to do: we just always remove the later - // element of a duplicate pair. - // Be careful! Because the vector contents may change as we remove elements, - // we use an index instead of an iterator in the outer loop, and don't - // precalculate the ending position. - for (size_t i = 0; i < matches->size(); ++i) { - for (history::HistoryMatches::iterator j(matches->begin() + i + 1); - j != matches->end(); ) { - if ((*matches)[i].url_info.url() == j->url_info.url()) - j = matches->erase(j); - else - ++j; - } - } -} - -// Calculates a new relevance score applying half-life time decaying to |count| -// using |time_since_last_visit| and |score_buckets|. This function will never -// return a score higher than |undecayed_relevance|; in other words, it can only -// demote the old score. -double CalculateRelevanceUsingScoreBuckets( - const HUPScoringParams::ScoreBuckets& score_buckets, - const base::TimeDelta& time_since_last_visit, - int undecayed_relevance, - int count) { - // Back off if above relevance cap. - if ((score_buckets.relevance_cap() != -1) && - (undecayed_relevance >= score_buckets.relevance_cap())) - return undecayed_relevance; - - // Time based decay using half-life time. - double decayed_count = count; - if (decayed_count > 0) - decayed_count *= score_buckets.HalfLifeTimeDecay(time_since_last_visit); - - // Find a threshold where decayed_count >= bucket. - const HUPScoringParams::ScoreBuckets::CountMaxRelevance* score_bucket = NULL; - for (size_t i = 0; i < score_buckets.buckets().size(); ++i) { - score_bucket = &score_buckets.buckets()[i]; - if (decayed_count >= score_bucket->first) - break; // Buckets are in descending order, so we can ignore the rest. - } - - return (score_bucket && (undecayed_relevance > score_bucket->second)) ? - score_bucket->second : undecayed_relevance; -} - -// Returns a new relevance score for the given |match| based on the -// |old_relevance| score and |scoring_params|. The new relevance score is -// guaranteed to be less than or equal to |old_relevance|. In other words, this -// function can only demote a score, never boost it. Returns |old_relevance| if -// experimental scoring is disabled. -int CalculateRelevanceScoreUsingScoringParams( - const history::HistoryMatch& match, - int old_relevance, - const HUPScoringParams& scoring_params) { - if (!scoring_params.experimental_scoring_enabled) - return old_relevance; - - const base::TimeDelta time_since_last_visit = - base::Time::Now() - match.url_info.last_visit(); - - int relevance = CalculateRelevanceUsingScoreBuckets( - scoring_params.typed_count_buckets, time_since_last_visit, old_relevance, - match.url_info.typed_count()); - - // Additional demotion (on top of typed_count demotion) of URLs that were - // never typed. - if (match.url_info.typed_count() == 0) { - relevance = CalculateRelevanceUsingScoreBuckets( - scoring_params.visited_count_buckets, time_since_last_visit, relevance, - match.url_info.visit_count()); - } - - DCHECK_LE(relevance, old_relevance); - return relevance; -} - -// Extracts typed_count, visit_count, and last_visited time from the URLRow and -// puts them in the additional info field of the |match| for display in -// about:omnibox. -void RecordAdditionalInfoFromUrlRow(const history::URLRow& info, - AutocompleteMatch* match) { - match->RecordAdditionalInfo("typed count", info.typed_count()); - match->RecordAdditionalInfo("visit count", info.visit_count()); - match->RecordAdditionalInfo("last visit", info.last_visit()); -} - -// If |create_if_necessary| is true, ensures that |matches| contains an entry -// for |info|, creating a new such entry if necessary (using |input_location| -// and |match_in_scheme|). -// -// If |promote| is true, this also ensures the entry is the first element in -// |matches|, moving or adding it to the front as appropriate. When |promote| -// is false, existing matches are left in place, and newly added matches are -// placed at the back. -// -// It's OK to call this function with both |create_if_necessary| and |promote| -// false, in which case we'll do nothing. -// -// Returns whether the match exists regardless if it was promoted/created. -bool CreateOrPromoteMatch(const history::URLRow& info, - size_t input_location, - bool match_in_scheme, - history::HistoryMatches* matches, - bool create_if_necessary, - bool promote) { - // |matches| may already have an entry for this. - for (history::HistoryMatches::iterator i(matches->begin()); - i != matches->end(); ++i) { - if (i->url_info.url() == info.url()) { - // Rotate it to the front if the caller wishes. - if (promote) - std::rotate(matches->begin(), i, i + 1); - return true; - } - } - - if (!create_if_necessary) - return false; - - // No entry, so create one. - history::HistoryMatch match(info, input_location, match_in_scheme, true); - if (promote) - matches->push_front(match); - else - matches->push_back(match); - - return true; -} - -// Returns whether |match| is suitable for inline autocompletion. -bool CanPromoteMatchForInlineAutocomplete(const history::HistoryMatch& match) { - // We can promote this match if it's been typed at least n times, where n == 1 - // for "simple" (host-only) URLs and n == 2 for others. We set a higher bar - // for these long URLs because it's less likely that users will want to visit - // them again. Even though we don't increment the typed_count for pasted-in - // URLs, if the user manually edits the URL or types some long thing in by - // hand, we wouldn't want to immediately start autocompleting it. - return match.url_info.typed_count() && - ((match.url_info.typed_count() > 1) || match.IsHostOnly()); -} - -// Given the user's |input| and a |match| created from it, reduce the match's -// URL to just a host. If this host still matches the user input, return it. -// Returns the empty string on failure. -GURL ConvertToHostOnly(const history::HistoryMatch& match, - const base::string16& input) { - // See if we should try to do host-only suggestions for this URL. Nonstandard - // schemes means there's no authority section, so suggesting the host name - // is useless. File URLs are standard, but host suggestion is not useful for - // them either. - const GURL& url = match.url_info.url(); - if (!url.is_valid() || !url.IsStandard() || url.SchemeIsFile()) - return GURL(); - - // Transform to a host-only match. Bail if the host no longer matches the - // user input (e.g. because the user typed more than just a host). - GURL host = url.GetWithEmptyPath(); - if ((host.spec().length() < (match.input_location + input.length()))) - return GURL(); // User typing is longer than this host suggestion. - - const base::string16 spec = base::UTF8ToUTF16(host.spec()); - if (spec.compare(match.input_location, input.length(), input)) - return GURL(); // User typing is no longer a prefix. - - return host; -} - -} // namespace - -// ----------------------------------------------------------------- -// SearchTermsDataSnapshot - -// Implementation of SearchTermsData that takes a snapshot of another -// SearchTermsData by copying all the responses to the different getters into -// member strings, then returning those strings when its own getters are called. -// This will typically be constructed on the UI thread from -// UIThreadSearchTermsData but is subsequently safe to use on any thread. -class SearchTermsDataSnapshot : public SearchTermsData { - public: - explicit SearchTermsDataSnapshot(const SearchTermsData& search_terms_data); - ~SearchTermsDataSnapshot() override; - - std::string GoogleBaseURLValue() const override; - std::string GetApplicationLocale() const override; - base::string16 GetRlzParameterValue(bool from_app_list) const override; - std::string GetSearchClient() const override; - bool EnableAnswersInSuggest() const override; - bool IsShowingSearchTermsOnSearchResultsPages() const override; - std::string InstantExtendedEnabledParam(bool for_search) const override; - std::string ForceInstantResultsParam(bool for_prerender) const override; - std::string NTPIsThemedParam() const override; - std::string GoogleImageSearchSource() const override; - - private: - std::string google_base_url_value_; - std::string application_locale_; - base::string16 rlz_parameter_value_; - std::string search_client_; - bool enable_answers_in_suggest_; - bool is_showing_search_terms_on_search_results_pages_; - std::string instant_extended_enabled_param_; - std::string instant_extended_enabled_param_for_search_; - std::string force_instant_results_param_; - std::string force_instant_results_param_for_prerender_; - std::string ntp_is_themed_param_; - std::string google_image_search_source_; - - DISALLOW_COPY_AND_ASSIGN(SearchTermsDataSnapshot); -}; - -SearchTermsDataSnapshot::SearchTermsDataSnapshot( - const SearchTermsData& search_terms_data) - : google_base_url_value_(search_terms_data.GoogleBaseURLValue()), - application_locale_(search_terms_data.GetApplicationLocale()), - rlz_parameter_value_(search_terms_data.GetRlzParameterValue(false)), - search_client_(search_terms_data.GetSearchClient()), - enable_answers_in_suggest_(search_terms_data.EnableAnswersInSuggest()), - is_showing_search_terms_on_search_results_pages_( - search_terms_data.IsShowingSearchTermsOnSearchResultsPages()), - instant_extended_enabled_param_( - search_terms_data.InstantExtendedEnabledParam(false)), - instant_extended_enabled_param_for_search_( - search_terms_data.InstantExtendedEnabledParam(true)), - force_instant_results_param_( - search_terms_data.ForceInstantResultsParam(false)), - force_instant_results_param_for_prerender_( - search_terms_data.ForceInstantResultsParam(true)), - ntp_is_themed_param_(search_terms_data.NTPIsThemedParam()), - google_image_search_source_(search_terms_data.GoogleImageSearchSource()) { -} - -SearchTermsDataSnapshot::~SearchTermsDataSnapshot() { -} - -std::string SearchTermsDataSnapshot::GoogleBaseURLValue() const { - return google_base_url_value_; -} - -std::string SearchTermsDataSnapshot::GetApplicationLocale() const { - return application_locale_; -} - -base::string16 SearchTermsDataSnapshot::GetRlzParameterValue( - bool from_app_list) const { - return rlz_parameter_value_; -} - -std::string SearchTermsDataSnapshot::GetSearchClient() const { - return search_client_; -} - -bool SearchTermsDataSnapshot::EnableAnswersInSuggest() const { - return enable_answers_in_suggest_; -} - -bool SearchTermsDataSnapshot::IsShowingSearchTermsOnSearchResultsPages() const { - return is_showing_search_terms_on_search_results_pages_; -} - -std::string SearchTermsDataSnapshot::InstantExtendedEnabledParam( - bool for_search) const { - return for_search ? instant_extended_enabled_param_ : - instant_extended_enabled_param_for_search_; -} - -std::string SearchTermsDataSnapshot::ForceInstantResultsParam( - bool for_prerender) const { - return for_prerender ? force_instant_results_param_ : - force_instant_results_param_for_prerender_; -} - -std::string SearchTermsDataSnapshot::NTPIsThemedParam() const { - return ntp_is_themed_param_; -} - -std::string SearchTermsDataSnapshot::GoogleImageSearchSource() const { - return google_image_search_source_; -} - -// ----------------------------------------------------------------- -// HistoryURLProvider - -// These ugly magic numbers will go away once we switch all scoring -// behavior (including URL-what-you-typed) to HistoryQuick provider. -const int HistoryURLProvider::kScoreForBestInlineableResult = 1413; -const int HistoryURLProvider::kScoreForUnvisitedIntranetResult = 1403; -const int HistoryURLProvider::kScoreForWhatYouTypedResult = 1203; -const int HistoryURLProvider::kBaseScoreForNonInlineableResult = 900; - -// VisitClassifier is used to classify the type of visit to a particular url. -class HistoryURLProvider::VisitClassifier { - public: - enum Type { - INVALID, // Navigations to the URL are not allowed. - UNVISITED_INTRANET, // A navigable URL for which we have no visit data but - // which is known to refer to a visited intranet host. - VISITED, // The site has been previously visited. - }; - - VisitClassifier(HistoryURLProvider* provider, - const AutocompleteInput& input, - history::URLDatabase* db); - - // Returns the type of visit for the specified input. - Type type() const { return type_; } - - // Returns the URLRow for the visit. - const history::URLRow& url_row() const { return url_row_; } - - private: - HistoryURLProvider* provider_; - history::URLDatabase* db_; - Type type_; - history::URLRow url_row_; - - DISALLOW_COPY_AND_ASSIGN(VisitClassifier); -}; - -HistoryURLProvider::VisitClassifier::VisitClassifier( - HistoryURLProvider* provider, - const AutocompleteInput& input, - history::URLDatabase* db) - : provider_(provider), - db_(db), - type_(INVALID) { - const GURL& url = input.canonicalized_url(); - // Detect email addresses. These cases will look like "http://user@site/", - // and because the history backend strips auth creds, we'll get a bogus exact - // match below if the user has visited "site". - if (!url.is_valid() || - ((input.type() == metrics::OmniboxInputType::UNKNOWN) && - input.parts().username.is_nonempty() && - !input.parts().password.is_nonempty() && - !input.parts().path.is_nonempty())) - return; - - if (db_->GetRowForURL(url, &url_row_)) { - type_ = VISITED; - return; - } - - if (provider_->CanFindIntranetURL(db_, input)) { - // The user typed an intranet hostname that they've visited (albeit with a - // different port and/or path) before. - url_row_ = history::URLRow(url); - type_ = UNVISITED_INTRANET; - } -} - -HistoryURLProviderParams::HistoryURLProviderParams( - const AutocompleteInput& input, - bool trim_http, - const AutocompleteMatch& what_you_typed_match, - const std::string& languages, - TemplateURL* default_search_provider, - const SearchTermsData& search_terms_data) - : message_loop(base::MessageLoop::current()), - input(input), - prevent_inline_autocomplete(input.prevent_inline_autocomplete()), - trim_http(trim_http), - what_you_typed_match(what_you_typed_match), - failed(false), - exact_suggestion_is_in_history(false), - promote_type(NEITHER), - languages(languages), - default_search_provider(default_search_provider ? - new TemplateURL(default_search_provider->data()) : NULL), - search_terms_data(new SearchTermsDataSnapshot(search_terms_data)) { -} - -HistoryURLProviderParams::~HistoryURLProviderParams() { -} - -HistoryURLProvider::HistoryURLProvider(AutocompleteProviderClient* client, - AutocompleteProviderListener* listener) - : HistoryProvider(AutocompleteProvider::TYPE_HISTORY_URL, client), - listener_(listener), - params_(NULL) { - // Initialize HUP scoring params based on the current experiment. - OmniboxFieldTrial::GetExperimentalHUPScoringParams(&scoring_params_); -} - -void HistoryURLProvider::Start(const AutocompleteInput& input, - bool minimal_changes, - bool called_due_to_focus) { - // NOTE: We could try hard to do less work in the |minimal_changes| case - // here; some clever caching would let us reuse the raw matches from the - // history DB without re-querying. However, we'd still have to go back to - // the history thread to mark these up properly, and if pass 2 is currently - // running, we'd need to wait for it to return to the main thread before - // doing this (we can't just write new data for it to read due to thread - // safety issues). At that point it's just as fast, and easier, to simply - // re-run the query from scratch and ignore |minimal_changes|. - - // Cancel any in-progress query. - Stop(false, false); - - matches_.clear(); - - if (called_due_to_focus || - (input.type() == metrics::OmniboxInputType::INVALID) || - (input.type() == metrics::OmniboxInputType::FORCED_QUERY)) - return; - - // Do some fixup on the user input before matching against it, so we provide - // good results for local file paths, input with spaces, etc. - const FixupReturn fixup_return(FixupUserInput(input)); - if (!fixup_return.first) - return; - url::Parsed parts; - url_fixer::SegmentURL(fixup_return.second, &parts); - AutocompleteInput fixed_up_input(input); - fixed_up_input.UpdateText(fixup_return.second, base::string16::npos, parts); - - // Create a match for what the user typed. - const bool trim_http = !AutocompleteInput::HasHTTPScheme(input.text()); - AutocompleteMatch what_you_typed_match(SuggestExactInput( - fixed_up_input.text(), fixed_up_input.canonicalized_url(), trim_http)); - what_you_typed_match.relevance = CalculateRelevance(WHAT_YOU_TYPED, 0); - - // Add the WYT match as a fallback in case we can't get the history service or - // URL DB; otherwise, we'll replace this match lower down. Don't do this for - // queries, though -- while we can sometimes mark up a match for them, it's - // not what the user wants, and just adds noise. - if (fixed_up_input.type() != metrics::OmniboxInputType::QUERY) - matches_.push_back(what_you_typed_match); - - // We'll need the history service to run both passes, so try to obtain it. - history::HistoryService* const history_service = client()->HistoryService(); - if (!history_service) - return; - - // Get the default search provider and search terms data now since we have to - // retrieve these on the UI thread, and the second pass runs on the history - // thread. |template_url_service| can be NULL when testing. - TemplateURLService* template_url_service = client()->GetTemplateURLService(); - TemplateURL* default_search_provider = template_url_service ? - template_url_service->GetDefaultSearchProvider() : NULL; - - // Create the data structure for the autocomplete passes. We'll save this off - // onto the |params_| member for later deletion below if we need to run pass - // 2. - scoped_ptr<HistoryURLProviderParams> params(new HistoryURLProviderParams( - fixed_up_input, trim_http, what_you_typed_match, - client()->AcceptLanguages(), default_search_provider, - client()->GetSearchTermsData())); - // Note that we use the non-fixed-up input here, since fixup may strip - // trailing whitespace. - params->prevent_inline_autocomplete = PreventInlineAutocomplete(input); - - // Pass 1: Get the in-memory URL database, and use it to find and promote - // the inline autocomplete match, if any. - history::URLDatabase* url_db = history_service->InMemoryDatabase(); - // url_db can be NULL if it hasn't finished initializing (or failed to - // initialize). In this case all we can do is fall back on the second - // pass. - // - // TODO(pkasting): We should just block here until this loads. Any time - // someone unloads the history backend, we'll get inconsistent inline - // autocomplete behavior here. - if (url_db) { - DoAutocomplete(NULL, url_db, params.get()); - matches_.clear(); - PromoteMatchesIfNecessary(*params); - // NOTE: We don't reset |params| here since at least the |promote_type| - // field on it will be read by the second pass -- see comments in - // DoAutocomplete(). - } - - // Pass 2: Ask the history service to call us back on the history thread, - // where we can read the full on-disk DB. - if (input.want_asynchronous_matches()) { - done_ = false; - params_ = params.release(); // This object will be destroyed in - // QueryComplete() once we're done with it. - history_service->ScheduleAutocomplete( - base::Bind(&HistoryURLProvider::ExecuteWithDB, this, params_)); - } -} - -void HistoryURLProvider::Stop(bool clear_cached_results, - bool due_to_user_inactivity) { - done_ = true; - - if (params_) - params_->cancel_flag.Set(); -} - -AutocompleteMatch HistoryURLProvider::SuggestExactInput( - const base::string16& text, - const GURL& destination_url, - bool trim_http) { - // The FormattedStringWithEquivalentMeaning() call below requires callers to - // be on the main thread. - DCHECK(thread_checker_.CalledOnValidThread()); - - AutocompleteMatch match(this, 0, false, - AutocompleteMatchType::URL_WHAT_YOU_TYPED); - - if (destination_url.is_valid()) { - match.destination_url = destination_url; - - // Trim off "http://" if the user didn't type it. - DCHECK(!trim_http || !AutocompleteInput::HasHTTPScheme(text)); - base::string16 display_string( - net::FormatUrl(destination_url, std::string(), - net::kFormatUrlOmitAll & ~net::kFormatUrlOmitHTTP, - net::UnescapeRule::SPACES, NULL, NULL, NULL)); - const size_t offset = trim_http ? TrimHttpPrefix(&display_string) : 0; - match.fill_into_edit = - AutocompleteInput::FormattedStringWithEquivalentMeaning( - destination_url, display_string, client()->SchemeClassifier()); - match.allowed_to_be_default_match = true; - // NOTE: Don't set match.inline_autocompletion to something non-empty here; - // it's surprising and annoying. - - // Try to highlight "innermost" match location. If we fix up "w" into - // "www.w.com", we want to highlight the fifth character, not the first. - // This relies on match.destination_url being the non-prefix-trimmed version - // of match.contents. - match.contents = display_string; - const URLPrefix* best_prefix = URLPrefix::BestURLPrefix( - base::UTF8ToUTF16(destination_url.spec()), text); - // It's possible for match.destination_url to not contain the user's input - // at all (so |best_prefix| is NULL), for example if the input is - // "view-source:x" and |destination_url| has an inserted "http://" in the - // middle. - if (best_prefix == NULL) { - AutocompleteMatch::ClassifyMatchInString(text, match.contents, - ACMatchClassification::URL, - &match.contents_class); - } else { - AutocompleteMatch::ClassifyLocationInString( - best_prefix->prefix.length() - offset, text.length(), - match.contents.length(), ACMatchClassification::URL, - &match.contents_class); - } - } - - return match; -} - -void HistoryURLProvider::ExecuteWithDB(HistoryURLProviderParams* params, - history::HistoryBackend* backend, - history::URLDatabase* db) { - // We may get called with a NULL database if it couldn't be properly - // initialized. - if (!db) { - params->failed = true; - } else if (!params->cancel_flag.IsSet()) { - base::TimeTicks beginning_time = base::TimeTicks::Now(); - - DoAutocomplete(backend, db, params); - - UMA_HISTOGRAM_TIMES("Autocomplete.HistoryAsyncQueryTime", - base::TimeTicks::Now() - beginning_time); - } - - // Return the results (if any) to the main thread. - params->message_loop->task_runner()->PostTask( - FROM_HERE, base::Bind(&HistoryURLProvider::QueryComplete, this, params)); -} - -HistoryURLProvider::~HistoryURLProvider() { - // Note: This object can get leaked on shutdown if there are pending - // requests on the database (which hold a reference to us). Normally, these - // messages get flushed for each thread. We do a round trip from main, to - // history, back to main while holding a reference. If the main thread - // completes before the history thread, the message to delegate back to the - // main thread will not run and the reference will leak. Therefore, don't do - // anything on destruction. -} - -// static -int HistoryURLProvider::CalculateRelevance(MatchType match_type, - int match_number) { - switch (match_type) { - case INLINE_AUTOCOMPLETE: - return kScoreForBestInlineableResult; - - case UNVISITED_INTRANET: - return kScoreForUnvisitedIntranetResult; - - case WHAT_YOU_TYPED: - return kScoreForWhatYouTypedResult; - - default: // NORMAL - return kBaseScoreForNonInlineableResult + match_number; - } -} - -// static -ACMatchClassifications HistoryURLProvider::ClassifyDescription( - const base::string16& input_text, - const base::string16& description) { - base::string16 clean_description = - bookmarks::CleanUpTitleForMatching(description); - TermMatches description_matches(SortAndDeoverlapMatches( - MatchTermInString(input_text, clean_description, 0))); - WordStarts description_word_starts; - String16VectorFromString16(clean_description, false, - &description_word_starts); - // If HistoryURL retrieves any matches (and hence we reach this code), we - // are guaranteed that the beginning of input_text must be a word break. - WordStarts offsets(1, 0u); - description_matches = ScoredHistoryMatch::FilterTermMatchesByWordStarts( - description_matches, offsets, description_word_starts, 0, - std::string::npos); - return SpansFromTermMatch( - description_matches, clean_description.length(), false); -} - -void HistoryURLProvider::DoAutocomplete(history::HistoryBackend* backend, - history::URLDatabase* db, - HistoryURLProviderParams* params) { - // Get the matching URLs from the DB. - params->matches.clear(); - history::URLRows url_matches; - const URLPrefixes& prefixes = URLPrefix::GetURLPrefixes(); - for (URLPrefixes::const_iterator i(prefixes.begin()); i != prefixes.end(); - ++i) { - if (params->cancel_flag.IsSet()) - return; // Canceled in the middle of a query, give up. - - // We only need kMaxMatches results in the end, but before we get there we - // need to promote lower-quality matches that are prefixes of higher-quality - // matches, and remove lower-quality redirects. So we ask for more results - // than we need, of every prefix type, in hopes this will give us far more - // than enough to work with. CullRedirects() will then reduce the list to - // the best kMaxMatches results. - db->AutocompleteForPrefix( - base::UTF16ToUTF8(i->prefix + params->input.text()), kMaxMatches * 2, - !backend, &url_matches); - for (history::URLRows::const_iterator j(url_matches.begin()); - j != url_matches.end(); ++j) { - const URLPrefix* best_prefix = URLPrefix::BestURLPrefix( - base::UTF8ToUTF16(j->url().spec()), base::string16()); - DCHECK(best_prefix); - params->matches.push_back(history::HistoryMatch( - *j, i->prefix.length(), !i->num_components, - i->num_components >= best_prefix->num_components)); - } - } - - // Create sorted list of suggestions. - CullPoorMatches(params); - SortAndDedupMatches(¶ms->matches); - - // Try to create a shorter suggestion from the best match. - // We consider the what you typed match eligible for display when it's - // navigable and there's a reasonable chance the user intended to do - // something other than search. We use a variety of heuristics to determine - // this, e.g. whether the user explicitly typed a scheme, or if omnibox - // searching has been disabled by policy. In the cases where we've parsed as - // UNKNOWN, we'll still show an accidental search infobar if need be. - VisitClassifier classifier(this, params->input, db); - params->have_what_you_typed_match = - (params->input.type() != metrics::OmniboxInputType::QUERY) && - ((params->input.type() != metrics::OmniboxInputType::UNKNOWN) || - (classifier.type() == VisitClassifier::UNVISITED_INTRANET) || - !params->trim_http || - (AutocompleteInput::NumNonHostComponents(params->input.parts()) > 0) || - !params->default_search_provider); - const bool have_shorter_suggestion_suitable_for_inline_autocomplete = - PromoteOrCreateShorterSuggestion(db, params); - - // Check whether what the user typed appears in history. - const bool can_check_history_for_exact_match = - // Checking what_you_typed_match.allowed_to_be_default_match tells us - // whether SuggestExactInput() succeeded in constructing a valid match. - params->what_you_typed_match.allowed_to_be_default_match && - // Additionally, in the case where the user has typed "foo.com" and - // visited (but not typed) "foo/", and the input is "foo", the first pass - // will fall into the FRONT_HISTORY_MATCH case for "foo.com" but the - // second pass can suggest the exact input as a better URL. Since we need - // both passes to agree, and since during the first pass there's no way to - // know about "foo/", ensure that if the promote type was set to - // FRONT_HISTORY_MATCH during the first pass, the second pass will not - // consider the exact suggestion to be in history and therefore will not - // suggest the exact input as a better match. (Note that during the first - // pass, this conditional will always succeed since |promote_type| is - // initialized to NEITHER.) - (params->promote_type != HistoryURLProviderParams::FRONT_HISTORY_MATCH); - params->exact_suggestion_is_in_history = can_check_history_for_exact_match && - FixupExactSuggestion(db, classifier, params); - - // If we succeeded in fixing up the exact match based on the user's history, - // we should treat it as the best match regardless of input type. If not, - // then we check whether there's an inline autocompletion we can create from - // this input, so we can promote that as the best match. - if (params->exact_suggestion_is_in_history) { - params->promote_type = HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH; - } else if (!params->matches.empty() && - (have_shorter_suggestion_suitable_for_inline_autocomplete || - CanPromoteMatchForInlineAutocomplete(params->matches[0]))) { - // Note that we promote this inline-autocompleted match even when - // params->prevent_inline_autocomplete is true. This is safe because in - // this case the match will be marked as "not allowed to be default", and - // a non-inlined match that is "allowed to be default" will be reordered - // above it by the controller/AutocompleteResult. We ensure there is such - // a match in two ways: - // * If params->have_what_you_typed_match is true, we force the - // what-you-typed match to be added in this case. See comments in - // PromoteMatchesIfNecessary(). - // * Otherwise, we should have some sort of QUERY or UNKNOWN input that - // the SearchProvider will provide a defaultable WYT match for. - params->promote_type = HistoryURLProviderParams::FRONT_HISTORY_MATCH; - } else { - // Failed to promote any URLs. Use the What You Typed match, if we have it. - params->promote_type = params->have_what_you_typed_match ? - HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH : - HistoryURLProviderParams::NEITHER; - } - - const size_t max_results = - kMaxMatches + (params->exact_suggestion_is_in_history ? 1 : 0); - if (backend) { - // Remove redirects and trim list to size. We want to provide up to - // kMaxMatches results plus the What You Typed result, if it was added to - // params->matches above. - CullRedirects(backend, ¶ms->matches, max_results); - } else if (params->matches.size() > max_results) { - // Simply trim the list to size. - params->matches.resize(max_results); - } -} - -void HistoryURLProvider::PromoteMatchesIfNecessary( - const HistoryURLProviderParams& params) { - if (params.promote_type == HistoryURLProviderParams::NEITHER) - return; - if (params.promote_type == HistoryURLProviderParams::FRONT_HISTORY_MATCH) { - matches_.push_back( - HistoryMatchToACMatch(params, 0, INLINE_AUTOCOMPLETE, - CalculateRelevance(INLINE_AUTOCOMPLETE, 0))); - } - // There are two cases where we need to add the what-you-typed-match: - // * If params.promote_type is WHAT_YOU_TYPED_MATCH, we're being explicitly - // directed to. - // * If params.have_what_you_typed_match is true, then params.promote_type - // can't be NEITHER (see code near the end of DoAutocomplete()), so if - // it's not WHAT_YOU_TYPED_MATCH, it must be FRONT_HISTORY_MATCH, and - // we'll have promoted the history match above. If - // params.prevent_inline_autocomplete is also true, then this match - // will be marked "not allowed to be default", and we need to add the - // what-you-typed match to ensure there's a legal default match for the - // controller/AutocompleteResult to promote. (If - // params.have_what_you_typed_match is false, the SearchProvider should - // take care of adding this defaultable match.) - if ((params.promote_type == HistoryURLProviderParams::WHAT_YOU_TYPED_MATCH) || - (params.prevent_inline_autocomplete && - params.have_what_you_typed_match)) { - matches_.push_back(params.what_you_typed_match); - } -} - -void HistoryURLProvider::QueryComplete( - HistoryURLProviderParams* params_gets_deleted) { - // Ensure |params_gets_deleted| gets deleted on exit. - scoped_ptr<HistoryURLProviderParams> params(params_gets_deleted); - - // If the user hasn't already started another query, clear our member pointer - // so we can't write into deleted memory. - if (params_ == params_gets_deleted) - params_ = NULL; - - // Don't send responses for queries that have been canceled. - if (params->cancel_flag.IsSet()) - return; // Already set done_ when we canceled, no need to set it again. - - // Don't modify |matches_| if the query failed, since it might have a default - // match in it, whereas |params->matches| will be empty. - if (!params->failed) { - matches_.clear(); - PromoteMatchesIfNecessary(*params); - - // Determine relevance of highest scoring match, if any. - int relevance = matches_.empty() ? - CalculateRelevance(NORMAL, - static_cast<int>(params->matches.size() - 1)) : - matches_[0].relevance; - - // Convert the history matches to autocomplete matches. If we promoted the - // first match, skip over it. - const size_t first_match = - (params->exact_suggestion_is_in_history || - (params->promote_type == - HistoryURLProviderParams::FRONT_HISTORY_MATCH)) ? 1 : 0; - for (size_t i = first_match; i < params->matches.size(); ++i) { - // All matches score one less than the previous match. - --relevance; - // The experimental scoring must not change the top result's score. - if (!matches_.empty()) { - relevance = CalculateRelevanceScoreUsingScoringParams( - params->matches[i], relevance, scoring_params_); - } - matches_.push_back(HistoryMatchToACMatch(*params, i, NORMAL, relevance)); - } - } - - done_ = true; - listener_->OnProviderUpdate(true); -} - -bool HistoryURLProvider::FixupExactSuggestion( - history::URLDatabase* db, - const VisitClassifier& classifier, - HistoryURLProviderParams* params) const { - MatchType type = INLINE_AUTOCOMPLETE; - switch (classifier.type()) { - case VisitClassifier::INVALID: - return false; - case VisitClassifier::UNVISITED_INTRANET: - type = UNVISITED_INTRANET; - break; - default: - DCHECK_EQ(VisitClassifier::VISITED, classifier.type()); - // We have data for this match, use it. - params->what_you_typed_match.deletable = true; - params->what_you_typed_match.description = classifier.url_row().title(); - RecordAdditionalInfoFromUrlRow(classifier.url_row(), - ¶ms->what_you_typed_match); - params->what_you_typed_match.description_class = ClassifyDescription( - params->input.text(), params->what_you_typed_match.description); - if (!classifier.url_row().typed_count()) { - // If we reach here, we must be in the second pass, and we must not have - // this row's data available during the first pass. That means we - // either scored it as WHAT_YOU_TYPED or UNVISITED_INTRANET, and to - // maintain the ordering between passes consistent, we need to score it - // the same way here. - type = CanFindIntranetURL(db, params->input) ? - UNVISITED_INTRANET : WHAT_YOU_TYPED; - } - break; - } - - params->what_you_typed_match.relevance = CalculateRelevance(type, 0); - - // If there are any other matches, then don't promote this match here, in - // hopes the caller will be able to inline autocomplete a better suggestion. - // DoAutocomplete() will fall back on this match if inline autocompletion - // fails. This matches how we react to never-visited URL inputs in the non- - // intranet case. - if (type == UNVISITED_INTRANET && !params->matches.empty()) - return false; - - // Put it on the front of the HistoryMatches for redirect culling. - CreateOrPromoteMatch(classifier.url_row(), base::string16::npos, false, - ¶ms->matches, true, true); - return true; -} - -bool HistoryURLProvider::CanFindIntranetURL( - history::URLDatabase* db, - const AutocompleteInput& input) const { - // Normally passing the first two conditions below ought to guarantee the - // third condition, but because FixupUserInput() can run and modify the - // input's text and parts between Parse() and here, it seems better to be - // paranoid and check. - if ((input.type() != metrics::OmniboxInputType::UNKNOWN) || - !base::LowerCaseEqualsASCII(input.scheme(), url::kHttpScheme) || - !input.parts().host.is_nonempty()) - return false; - const std::string host(base::UTF16ToUTF8( - input.text().substr(input.parts().host.begin, input.parts().host.len))); - const size_t registry_length = - net::registry_controlled_domains::GetRegistryLength( - host, - net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, - net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); - return registry_length == 0 && db->IsTypedHost(host); -} - -bool HistoryURLProvider::PromoteOrCreateShorterSuggestion( - history::URLDatabase* db, - HistoryURLProviderParams* params) { - if (params->matches.empty()) - return false; // No matches, nothing to do. - - // Determine the base URL from which to search, and whether that URL could - // itself be added as a match. We can add the base iff it's not "effectively - // the same" as any "what you typed" match. - const history::HistoryMatch& match = params->matches[0]; - GURL search_base = ConvertToHostOnly(match, params->input.text()); - bool can_add_search_base_to_matches = !params->have_what_you_typed_match; - if (search_base.is_empty()) { - // Search from what the user typed when we couldn't reduce the best match - // to a host. Careful: use a substring of |match| here, rather than the - // first match in |params|, because they might have different prefixes. If - // the user typed "google.com", params->what_you_typed_match will hold - // "http://google.com/", but |match| might begin with - // "http://www.google.com/". - // TODO: this should be cleaned up, and is probably incorrect for IDN. - std::string new_match = match.url_info.url().possibly_invalid_spec(). - substr(0, match.input_location + params->input.text().length()); - search_base = GURL(new_match); - if (search_base.is_empty()) - return false; // Can't construct a URL from which to start a search. - } else if (!can_add_search_base_to_matches) { - can_add_search_base_to_matches = - (search_base != params->what_you_typed_match.destination_url); - } - if (search_base == match.url_info.url()) - return false; // Couldn't shorten |match|, so no URLs to search over. - - // Search the DB for short URLs between our base and |match|. - history::URLRow info(search_base); - bool promote = true; - // A short URL is only worth suggesting if it's been visited at least a third - // as often as the longer URL. - const int min_visit_count = ((match.url_info.visit_count() - 1) / 3) + 1; - // For stability between the in-memory and on-disk autocomplete passes, when - // the long URL has been typed before, only suggest shorter URLs that have - // also been typed. Otherwise, the on-disk pass could suggest a shorter URL - // (which hasn't been typed) that the in-memory pass doesn't know about, - // thereby making the top match, and thus the behavior of inline - // autocomplete, unstable. - const int min_typed_count = match.url_info.typed_count() ? 1 : 0; - if (!db->FindShortestURLFromBase(search_base.possibly_invalid_spec(), - match.url_info.url().possibly_invalid_spec(), min_visit_count, - min_typed_count, can_add_search_base_to_matches, &info)) { - if (!can_add_search_base_to_matches) - return false; // Couldn't find anything and can't add the search base. - - // Try to get info on the search base itself. Promote it to the top if the - // original best match isn't good enough to autocomplete. - db->GetRowForURL(search_base, &info); - promote = match.url_info.typed_count() <= 1; - } - - // Promote or add the desired URL to the list of matches. - const bool ensure_can_inline = - promote && CanPromoteMatchForInlineAutocomplete(match); - return CreateOrPromoteMatch(info, match.input_location, match.match_in_scheme, - ¶ms->matches, true, promote) && - ensure_can_inline; -} - -void HistoryURLProvider::CullPoorMatches( - HistoryURLProviderParams* params) const { - const base::Time& threshold(history::AutocompleteAgeThreshold()); - for (history::HistoryMatches::iterator i(params->matches.begin()); - i != params->matches.end(); ) { - if (RowQualifiesAsSignificant(i->url_info, threshold) && - (!params->default_search_provider || - !params->default_search_provider->IsSearchURL( - i->url_info.url(), *params->search_terms_data))) { - ++i; - } else { - i = params->matches.erase(i); - } - } -} - -void HistoryURLProvider::CullRedirects(history::HistoryBackend* backend, - history::HistoryMatches* matches, - size_t max_results) const { - for (size_t source = 0; - (source < matches->size()) && (source < max_results); ) { - const GURL& url = (*matches)[source].url_info.url(); - // TODO(brettw) this should go away when everything uses GURL. - history::RedirectList redirects; - backend->QueryRedirectsFrom(url, &redirects); - if (!redirects.empty()) { - // Remove all but the first occurrence of any of these redirects in the - // search results. We also must add the URL we queried for, since it may - // not be the first match and we'd want to remove it. - // - // For example, when A redirects to B and our matches are [A, X, B], - // we'll get B as the redirects from, and we want to remove the second - // item of that pair, removing B. If A redirects to B and our matches are - // [B, X, A], we'll want to remove A instead. - redirects.push_back(url); - source = RemoveSubsequentMatchesOf(matches, source, redirects); - } else { - // Advance to next item. - source++; - } - } - - if (matches->size() > max_results) - matches->resize(max_results); -} - -size_t HistoryURLProvider::RemoveSubsequentMatchesOf( - history::HistoryMatches* matches, - size_t source_index, - const std::vector<GURL>& remove) const { - size_t next_index = source_index + 1; // return value = item after source - - // Find the first occurrence of any URL in the redirect chain. We want to - // keep this one since it is rated the highest. - history::HistoryMatches::iterator first(std::find_first_of( - matches->begin(), matches->end(), remove.begin(), remove.end(), - history::HistoryMatch::EqualsGURL)); - DCHECK(first != matches->end()) << "We should have always found at least the " - "original URL."; - - // Find any following occurrences of any URL in the redirect chain, these - // should be deleted. - for (history::HistoryMatches::iterator next(std::find_first_of(first + 1, - matches->end(), remove.begin(), remove.end(), - history::HistoryMatch::EqualsGURL)); - next != matches->end(); next = std::find_first_of(next, matches->end(), - remove.begin(), remove.end(), history::HistoryMatch::EqualsGURL)) { - // Remove this item. When we remove an item before the source index, we - // need to shift it to the right and remember that so we can return it. - next = matches->erase(next); - if (static_cast<size_t>(next - matches->begin()) < next_index) - --next_index; - } - return next_index; -} - -AutocompleteMatch HistoryURLProvider::HistoryMatchToACMatch( - const HistoryURLProviderParams& params, - size_t match_number, - MatchType match_type, - int relevance) { - // The FormattedStringWithEquivalentMeaning() call below requires callers to - // be on the main thread. - DCHECK(thread_checker_.CalledOnValidThread()); - - const history::HistoryMatch& history_match = params.matches[match_number]; - const history::URLRow& info = history_match.url_info; - AutocompleteMatch match(this, relevance, - !!info.visit_count(), AutocompleteMatchType::HISTORY_URL); - match.typed_count = info.typed_count(); - match.destination_url = info.url(); - DCHECK(match.destination_url.is_valid()); - size_t inline_autocomplete_offset = - history_match.input_location + params.input.text().length(); - std::string languages = (match_type == WHAT_YOU_TYPED) ? - std::string() : params.languages; - const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & - ~((params.trim_http && !history_match.match_in_scheme) ? - 0 : net::kFormatUrlOmitHTTP); - match.fill_into_edit = - AutocompleteInput::FormattedStringWithEquivalentMeaning( - info.url(), net::FormatUrl(info.url(), languages, format_types, - net::UnescapeRule::SPACES, NULL, NULL, - &inline_autocomplete_offset), - client()->SchemeClassifier()); - if (!params.prevent_inline_autocomplete && - (inline_autocomplete_offset != base::string16::npos)) { - DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length()); - match.inline_autocompletion = - match.fill_into_edit.substr(inline_autocomplete_offset); - } - // The latter part of the test effectively asks "is the inline completion - // empty?" (i.e., is this match effectively the what-you-typed match?). - match.allowed_to_be_default_match = !params.prevent_inline_autocomplete || - ((inline_autocomplete_offset != base::string16::npos) && - (inline_autocomplete_offset >= match.fill_into_edit.length())); - - size_t match_start = history_match.input_location; - match.contents = net::FormatUrl(info.url(), languages, - format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start); - if ((match_start != base::string16::npos) && - (inline_autocomplete_offset != base::string16::npos) && - (inline_autocomplete_offset != match_start)) { - DCHECK(inline_autocomplete_offset > match_start); - AutocompleteMatch::ClassifyLocationInString(match_start, - inline_autocomplete_offset - match_start, match.contents.length(), - ACMatchClassification::URL, &match.contents_class); - } else { - AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, - match.contents.length(), ACMatchClassification::URL, - &match.contents_class); - } - match.description = info.title(); - match.description_class = - ClassifyDescription(params.input.text(), match.description); - RecordAdditionalInfoFromUrlRow(info, &match); - return match; -} diff --git a/chrome/browser/autocomplete/history_url_provider.h b/chrome/browser/autocomplete/history_url_provider.h deleted file mode 100644 index 53796f4..0000000 --- a/chrome/browser/autocomplete/history_url_provider.h +++ /dev/null @@ -1,344 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ -#define CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ - -#include <string> -#include <vector> - -#include "base/compiler_specific.h" -#include "base/synchronization/cancellation_flag.h" -#include "base/threading/thread_checker.h" -#include "components/history/core/browser/history_match.h" -#include "components/omnibox/autocomplete_input.h" -#include "components/omnibox/history_provider.h" -#include "components/omnibox/omnibox_field_trial.h" -#include "components/search_engines/template_url.h" - -class AutocompleteProviderListener; -class SearchTermsData; - -namespace base { -class MessageLoop; -} - -namespace history { -class HistoryBackend; -class URLDatabase; -} - -// How history autocomplete works -// ============================== -// -// Read down this diagram for temporal ordering. -// -// Main thread History thread -// ----------- -------------- -// AutocompleteController::Start -// -> HistoryURLProvider::Start -// -> SuggestExactInput -// [params_ allocated] -// -> DoAutocomplete (for inline autocomplete) -// -> URLDatabase::AutocompleteForPrefix (on in-memory DB) -// -> HistoryService::ScheduleAutocomplete -// (return to controller) ---- -// / -// HistoryBackend::ScheduleAutocomplete -// -> HistoryURLProvider::ExecuteWithDB -// -> DoAutocomplete -// -> URLDatabase::AutocompleteForPrefix -// / -// HistoryService::QueryComplete -// [params_ destroyed] -// -> AutocompleteProviderListener::OnProviderUpdate -// -// The autocomplete controller calls us, and must be called back, on the main -// thread. When called, we run two autocomplete passes. The first pass runs -// synchronously on the main thread and queries the in-memory URL database. -// This pass promotes matches for inline autocomplete if applicable. We do -// this synchronously so that users get consistent behavior when they type -// quickly and hit enter, no matter how loaded the main history database is. -// Doing this synchronously also prevents inline autocomplete from being -// "flickery" in the AutocompleteEdit. Because the in-memory DB does not have -// redirect data, results other than the top match might change between the -// two passes, so we can't just decide to use this pass' matches as the final -// results. -// -// The second autocomplete pass uses the full history database, which must be -// queried on the history thread. Start() asks the history service schedule to -// callback on the history thread with a pointer to the main database. When we -// are done doing queries, we schedule a task on the main thread that notifies -// the AutocompleteController that we're done. -// -// The communication between these threads is done using a -// HistoryURLProviderParams object. This is allocated in the main thread, and -// normally deleted in QueryComplete(). So that both autocomplete passes can -// use the same code, we also use this to hold results during the first -// autocomplete pass. -// -// While the second pass is running, the AutocompleteController may cancel the -// request. This can happen frequently when the user is typing quickly. In -// this case, the main thread sets params_->cancel, which the background thread -// checks periodically. If it finds the flag set, it stops what it's doing -// immediately and calls back to the main thread. (We don't delete the params -// on the history thread, because we should only do that when we can safely -// NULL out params_, and that must be done on the main thread.) - -// Used to communicate autocomplete parameters between threads via the history -// service. -struct HistoryURLProviderParams { - // See comments on |promote_type| below. - enum PromoteType { - WHAT_YOU_TYPED_MATCH, - FRONT_HISTORY_MATCH, - NEITHER, - }; - - HistoryURLProviderParams(const AutocompleteInput& input, - bool trim_http, - const AutocompleteMatch& what_you_typed_match, - const std::string& languages, - TemplateURL* default_search_provider, - const SearchTermsData& search_terms_data); - ~HistoryURLProviderParams(); - - base::MessageLoop* message_loop; - - // A copy of the autocomplete input. We need the copy since this object will - // live beyond the original query while it runs on the history thread. - AutocompleteInput input; - - // Should inline autocompletion be disabled? This is initalized from - // |input.prevent_inline_autocomplete()|, but set to false is the input - // contains trailing white space. - bool prevent_inline_autocomplete; - - // Set when "http://" should be trimmed from the beginning of the URLs. - bool trim_http; - - // A match corresponding to what the user typed. - AutocompleteMatch what_you_typed_match; - - // Set by the main thread to cancel this request. If this flag is set when - // the query runs, the query will be abandoned. This allows us to avoid - // running queries that are no longer needed. Since we don't care if we run - // the extra queries, the lack of signaling is not a problem. - base::CancellationFlag cancel_flag; - - // Set by ExecuteWithDB() on the history thread when the query could not be - // performed because the history system failed to properly init the database. - // If this is set when the main thread is called back, it avoids changing - // |matches_| at all, so it won't delete the default match Start() creates. - bool failed; - - // List of matches written by DoAutocomplete(). Upon its return the provider - // converts this list to ACMatches and places them in |matches_|. - history::HistoryMatches matches; - - // True if the suggestion for exactly what the user typed appears as a known - // URL in the user's history. In this case, this will also be the first match - // in |matches|. - // - // NOTE: There are some complications related to keeping things consistent - // between passes and how we deal with intranet URLs, which are too complex to - // explain here; see the implementations of DoAutocomplete() and - // FixupExactSuggestion() for specific comments. - bool exact_suggestion_is_in_history; - - // Tells the provider whether to promote the what you typed match, the first - // element of |matches|, or neither as the first AutocompleteMatch. If - // |exact_suggestion_is_in_history| is true (and thus "the what you typed - // match" and "the first element of |matches|" represent the same thing), this - // will be set to WHAT_YOU_TYPED_MATCH. - // - // NOTE: The second pass of DoAutocomplete() checks what the first pass set - // this to. See comments in DoAutocomplete(). - PromoteType promote_type; - - // True if |what_you_typed_match| is eligible for display. If this is true, - // PromoteMatchesIfNecessary() may choose to place |what_you_typed_match| on - // |matches_| even when |promote_type| is not WHAT_YOU_TYPED_MATCH. - bool have_what_you_typed_match; - - // Languages we should pass to gfx::GetCleanStringFromUrl. - std::string languages; - - // The default search provider and search terms data necessary to cull results - // that correspond to searches (on the default engine). These can only be - // obtained on the UI thread, so we have to copy them into here to pass them - // to the history thread. We use a scoped_ptr<TemplateURL> for the DSP since - // TemplateURLs can't be copied by value. We use a scoped_ptr<SearchTermsData> - // so that we can store a snapshot of the SearchTermsData accessible from the - // history thread. - scoped_ptr<TemplateURL> default_search_provider; - scoped_ptr<SearchTermsData> search_terms_data; - - private: - DISALLOW_COPY_AND_ASSIGN(HistoryURLProviderParams); -}; - -// This class is an autocomplete provider and is also a pseudo-internal -// component of the history system. See comments above. -class HistoryURLProvider : public HistoryProvider { - public: - // Various values used in scoring, made public so other providers - // can insert results in appropriate ranges relative to these. - static const int kScoreForBestInlineableResult; - static const int kScoreForUnvisitedIntranetResult; - static const int kScoreForWhatYouTypedResult; - static const int kBaseScoreForNonInlineableResult; - - HistoryURLProvider(AutocompleteProviderClient* client, - AutocompleteProviderListener* listener); - - // HistoryProvider: - void Start(const AutocompleteInput& input, - bool minimal_changes, - bool called_due_to_focus) override; - void Stop(bool clear_cached_results, bool due_to_user_inactivity) override; - - // Returns a match representing a navigation to |destination_url| given user - // input of |text|. |trim_http| controls whether the match's |fill_into_edit| - // and |contents| should have any HTTP scheme stripped off, and should not be - // set to true if |text| contains an http prefix. - // NOTES: This does not set the relevance of the returned match, as different - // callers want different behavior. Callers must set this manually. - // This function should only be called on the UI thread. - AutocompleteMatch SuggestExactInput(const base::string16& text, - const GURL& destination_url, - bool trim_http); - - // Runs the history query on the history thread, called by the history - // system. The history database MAY BE NULL in which case it is not - // available and we should return no data. Also schedules returning the - // results to the main thread - void ExecuteWithDB(HistoryURLProviderParams* params, - history::HistoryBackend* backend, - history::URLDatabase* db); - - private: - FRIEND_TEST_ALL_PREFIXES(HistoryURLProviderTest, HUPScoringExperiment); - - enum MatchType { - NORMAL, - WHAT_YOU_TYPED, - INLINE_AUTOCOMPLETE, - UNVISITED_INTRANET, // An intranet site that has never been visited. - }; - class VisitClassifier; - - ~HistoryURLProvider() override; - - // Determines the relevance for a match, given its type. If |match_type| is - // NORMAL, |match_number| is a number indicating the relevance of the match - // (higher == more relevant). For other values of |match_type|, - // |match_number| is ignored. Only called some of the time; for some matches, - // relevancy scores are assigned consecutively decreasing (1416, 1415, ...). - static int CalculateRelevance(MatchType match_type, int match_number); - - // Returns a set of classifications that highlight all the occurrences of - // |input_text| at word breaks in |description|. - static ACMatchClassifications ClassifyDescription( - const base::string16& input_text, - const base::string16& description); - - // Actually runs the autocomplete job on the given database, which is - // guaranteed not to be NULL. Used by both autocomplete passes, and therefore - // called on multiple different threads (though not simultaneously). - void DoAutocomplete(history::HistoryBackend* backend, - history::URLDatabase* db, - HistoryURLProviderParams* params); - - // May promote the what you typed match, the first history match in - // params->matches, or both to the front of |matches_|, depending on the - // values of params->promote_type, params->have_what_you_typed_match, and - // params->prevent_inline_autocomplete. - void PromoteMatchesIfNecessary(const HistoryURLProviderParams& params); - - // Dispatches the results to the autocomplete controller. Called on the - // main thread by ExecuteWithDB when the results are available. - // Frees params_gets_deleted on exit. - void QueryComplete(HistoryURLProviderParams* params_gets_deleted); - - // Looks up the info for params->what_you_typed_match in the DB. If found, - // fills in the title, promotes the match's priority to that of an inline - // autocomplete match (maybe it should be slightly better?), and places it on - // the front of params->matches (so we pick the right matches to throw away - // when culling redirects to/from it). Returns whether a match was promoted. - bool FixupExactSuggestion(history::URLDatabase* db, - const VisitClassifier& classifier, - HistoryURLProviderParams* params) const; - - // Helper function for FixupExactSuggestion, this returns true if the input - // corresponds to some intranet URL where the user has previously visited the - // host in question. In this case the input should be treated as a URL. - bool CanFindIntranetURL(history::URLDatabase* db, - const AutocompleteInput& input) const; - - // Sees if a shorter version of the best match should be created, and if so - // places it at the front of params->matches. This can suggest history URLs - // that are prefixes of the best match (if they've been visited enough, - // compared to the best match), or create host-only suggestions even when they - // haven't been visited before: if the user visited http://example.com/asdf - // once, we'll suggest http://example.com/ even if they've never been to it. - // Returns true if a match was successfully created/promoted that we're - // willing to inline autocomplete. - bool PromoteOrCreateShorterSuggestion( - history::URLDatabase* db, - HistoryURLProviderParams* params); - - // Removes results that have been rarely typed or visited, and not any time - // recently. The exact parameters for this heuristic can be found in the - // function body. Also culls results corresponding to queries from the default - // search engine. These are low-quality, difficult-to-understand matches for - // users, and the SearchProvider should surface past queries in a better way - // anyway. - void CullPoorMatches(HistoryURLProviderParams* params) const; - - // Removes results that redirect to each other, leaving at most |max_results| - // results. - void CullRedirects(history::HistoryBackend* backend, - history::HistoryMatches* matches, - size_t max_results) const; - - // Helper function for CullRedirects, this removes all but the first - // occurance of [any of the set of strings in |remove|] from the |matches| - // list. - // - // The return value is the index of the item that is after the item in the - // input identified by |source_index|. If |source_index| or an item before - // is removed, the next item will be shifted, and this allows the caller to - // pick up on the next one when this happens. - size_t RemoveSubsequentMatchesOf(history::HistoryMatches* matches, - size_t source_index, - const std::vector<GURL>& remove) const; - - // Converts a specified |match_number| from params.matches into an - // autocomplete match for display. If experimental scoring is enabled, the - // final relevance score might be different from the given |relevance|. - // NOTE: This function should only be called on the UI thread. - AutocompleteMatch HistoryMatchToACMatch( - const HistoryURLProviderParams& params, - size_t match_number, - MatchType match_type, - int relevance); - - AutocompleteProviderListener* listener_; - - // Params for the current query. The provider should not free this directly; - // instead, it is passed as a parameter through the history backend, and the - // parameter itself is freed once it's no longer needed. The only reason we - // keep this member is so we can set the cancel bit on it. - HistoryURLProviderParams* params_; - - // Params controlling experimental behavior of this provider. - HUPScoringParams scoring_params_; - - base::ThreadChecker thread_checker_; - - DISALLOW_COPY_AND_ASSIGN(HistoryURLProvider); -}; - -#endif // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ diff --git a/chrome/browser/autocomplete/history_url_provider_unittest.cc b/chrome/browser/autocomplete/history_url_provider_unittest.cc index 3f3105b..c7196d2 100644 --- a/chrome/browser/autocomplete/history_url_provider_unittest.cc +++ b/chrome/browser/autocomplete/history_url_provider_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/autocomplete/history_url_provider.h" +#include "components/omnibox/history_url_provider.h" #include <algorithm> diff --git a/chrome/browser/autocomplete/in_memory_url_index.h b/chrome/browser/autocomplete/in_memory_url_index.h index 2b3199c..c881a1f 100644 --- a/chrome/browser/autocomplete/in_memory_url_index.h +++ b/chrome/browser/autocomplete/in_memory_url_index.h @@ -18,11 +18,11 @@ #include "base/memory/weak_ptr.h" #include "base/strings/string16.h" #include "base/task/cancelable_task_tracker.h" -#include "chrome/browser/autocomplete/scored_history_match.h" #include "components/history/core/browser/history_db_task.h" #include "components/history/core/browser/history_service_observer.h" #include "components/history/core/browser/history_types.h" #include "components/keyed_service/core/keyed_service.h" +#include "components/omnibox/scored_history_match.h" class HistoryQuickProviderTest; diff --git a/chrome/browser/autocomplete/scored_history_match.cc b/chrome/browser/autocomplete/scored_history_match.cc deleted file mode 100644 index d90f930..0000000 --- a/chrome/browser/autocomplete/scored_history_match.cc +++ /dev/null @@ -1,699 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/autocomplete/scored_history_match.h" - -#include <math.h> - -#include <algorithm> -#include <vector> - -#include "base/logging.h" -#include "base/numerics/safe_conversions.h" -#include "base/strings/string_number_conversions.h" -#include "base/strings/string_split.h" -#include "base/strings/string_util.h" -#include "base/strings/utf_offset_string_conversions.h" -#include "base/strings/utf_string_conversions.h" -#include "chrome/browser/autocomplete/history_url_provider.h" -#include "components/bookmarks/browser/bookmark_utils.h" -#include "components/omnibox/omnibox_field_trial.h" -#include "components/omnibox/url_prefix.h" - -namespace { - -// The number of days of recency scores to precompute. -const int kDaysToPrecomputeRecencyScoresFor = 366; - -// The number of raw term score buckets use; raw term scores greater this are -// capped at the score of the largest bucket. -const int kMaxRawTermScore = 30; - -// Pre-computed information to speed up calculating recency scores. -// |days_ago_to_recency_score| is a simple array mapping how long ago a page was -// visited (in days) to the recency score we should assign it. This allows easy -// lookups of scores without requiring math. This is initialized by -// InitDaysAgoToRecencyScoreArray called by -// ScoredHistoryMatch::Init(). -float days_ago_to_recency_score[kDaysToPrecomputeRecencyScoresFor]; - -// Pre-computed information to speed up calculating topicality scores. -// |raw_term_score_to_topicality_score| is a simple array mapping how raw terms -// scores (a weighted sum of the number of hits for the term, weighted by how -// important the hit is: hostname, path, etc.) to the topicality score we should -// assign it. This allows easy lookups of scores without requiring math. This -// is initialized by InitRawTermScoreToTopicalityScoreArray() called from -// ScoredHistoryMatch::Init(). -float raw_term_score_to_topicality_score[kMaxRawTermScore]; - -// Precalculates raw_term_score_to_topicality_score, used in -// GetTopicalityScore(). -void InitRawTermScoreToTopicalityScoreArray() { - for (int term_score = 0; term_score < kMaxRawTermScore; ++term_score) { - float topicality_score; - if (term_score < 10) { - // If the term scores less than 10 points (no full-credit hit, or - // no combination of hits that score that well), then the topicality - // score is linear in the term score. - topicality_score = 0.1 * term_score; - } else { - // For term scores of at least ten points, pass them through a log - // function so a score of 10 points gets a 1.0 (to meet up exactly - // with the linear component) and increases logarithmically until - // maxing out at 30 points, with computes to a score around 2.1. - topicality_score = (1.0 + 2.25 * log10(0.1 * term_score)); - } - raw_term_score_to_topicality_score[term_score] = topicality_score; - } -} - -// Pre-calculates days_ago_to_recency_score, used in GetRecencyScore(). -void InitDaysAgoToRecencyScoreArray() { - for (int days_ago = 0; days_ago < kDaysToPrecomputeRecencyScoresFor; - days_ago++) { - int unnormalized_recency_score; - if (days_ago <= 4) { - unnormalized_recency_score = 100; - } else if (days_ago <= 14) { - // Linearly extrapolate between 4 and 14 days so 14 days has a score - // of 70. - unnormalized_recency_score = 70 + (14 - days_ago) * (100 - 70) / (14 - 4); - } else if (days_ago <= 31) { - // Linearly extrapolate between 14 and 31 days so 31 days has a score - // of 50. - unnormalized_recency_score = 50 + (31 - days_ago) * (70 - 50) / (31 - 14); - } else if (days_ago <= 90) { - // Linearly extrapolate between 30 and 90 days so 90 days has a score - // of 30. - unnormalized_recency_score = 30 + (90 - days_ago) * (50 - 30) / (90 - 30); - } else { - // Linearly extrapolate between 90 and 365 days so 365 days has a score - // of 10. - unnormalized_recency_score = - 10 + (365 - days_ago) * (20 - 10) / (365 - 90); - } - days_ago_to_recency_score[days_ago] = unnormalized_recency_score / 100.0; - if (days_ago > 0) { - DCHECK_LE(days_ago_to_recency_score[days_ago], - days_ago_to_recency_score[days_ago - 1]); - } - } -} - -} // namespace - -// static -const size_t ScoredHistoryMatch::kMaxVisitsToScore = 10; -bool ScoredHistoryMatch::also_do_hup_like_scoring_ = false; -int ScoredHistoryMatch::bookmark_value_ = 1; -bool ScoredHistoryMatch::fix_frequency_bugs_ = false; -bool ScoredHistoryMatch::allow_tld_matches_ = false; -bool ScoredHistoryMatch::allow_scheme_matches_ = false; -size_t ScoredHistoryMatch::num_title_words_to_allow_ = 10u; -bool ScoredHistoryMatch::hqp_experimental_scoring_enabled_ = false; -float ScoredHistoryMatch::topicality_threshold_ = -1; -std::vector<ScoredHistoryMatch::ScoreMaxRelevance>* - ScoredHistoryMatch::hqp_relevance_buckets_ = nullptr; - -ScoredHistoryMatch::ScoredHistoryMatch() - : ScoredHistoryMatch(history::URLRow(), - VisitInfoVector(), - std::string(), - base::string16(), - String16Vector(), - WordStarts(), - RowWordStarts(), - false, - base::Time::Max()) { -} - -ScoredHistoryMatch::ScoredHistoryMatch( - const history::URLRow& row, - const VisitInfoVector& visits, - const std::string& languages, - const base::string16& lower_string, - const String16Vector& terms_vector, - const WordStarts& terms_to_word_starts_offsets, - const RowWordStarts& word_starts, - bool is_url_bookmarked, - base::Time now) - : HistoryMatch(row, 0, false, false), raw_score(0), can_inline(false) { - // NOTE: Call Init() before doing any validity checking to ensure that the - // class is always initialized after an instance has been constructed. In - // particular, this ensures that the class is initialized after an instance - // has been constructed via the no-args constructor. - ScoredHistoryMatch::Init(); - - GURL gurl = row.url(); - if (!gurl.is_valid()) - return; - - // Figure out where each search term appears in the URL and/or page title - // so that we can score as well as provide autocomplete highlighting. - base::OffsetAdjuster::Adjustments adjustments; - base::string16 url = - bookmarks::CleanUpUrlForMatching(gurl, languages, &adjustments); - base::string16 title = bookmarks::CleanUpTitleForMatching(row.title()); - int term_num = 0; - for (const auto& term : terms_vector) { - TermMatches url_term_matches = MatchTermInString(term, url, term_num); - TermMatches title_term_matches = MatchTermInString(term, title, term_num); - if (url_term_matches.empty() && title_term_matches.empty()) { - // A term was not found in either URL or title - reject. - return; - } - url_matches.insert(url_matches.end(), url_term_matches.begin(), - url_term_matches.end()); - title_matches.insert(title_matches.end(), title_term_matches.begin(), - title_term_matches.end()); - ++term_num; - } - - // Sort matches by offset and eliminate any which overlap. - // TODO(mpearson): Investigate whether this has any meaningful - // effect on scoring. (It's necessary at some point: removing - // overlaps and sorting is needed to decide what to highlight in the - // suggestion string. But this sort and de-overlap doesn't have to - // be done before scoring.) - url_matches = SortAndDeoverlapMatches(url_matches); - title_matches = SortAndDeoverlapMatches(title_matches); - - // We can inline autocomplete a match if: - // 1) there is only one search term - // 2) AND the match begins immediately after one of the prefixes in - // URLPrefix such as http://www and https:// (note that one of these - // is the empty prefix, for cases where the user has typed the scheme) - // 3) AND the search string does not end in whitespace (making it look to - // the IMUI as though there is a single search term when actually there - // is a second, empty term). - // |best_inlineable_prefix| stores the inlineable prefix computed in - // clause (2) or NULL if no such prefix exists. (The URL is not inlineable.) - // Note that using the best prefix here means that when multiple - // prefixes match, we'll choose to inline following the longest one. - // For a URL like "http://www.washingtonmutual.com", this means - // typing "w" will inline "ashington..." instead of "ww.washington...". - if (!url_matches.empty() && (terms_vector.size() == 1) && - !IsWhitespace(*lower_string.rbegin())) { - const base::string16 gurl_spec = base::UTF8ToUTF16(gurl.spec()); - const URLPrefix* best_inlineable_prefix = - URLPrefix::BestURLPrefix(gurl_spec, terms_vector[0]); - if (best_inlineable_prefix) { - // When inline autocompleting this match, we're going to use the part of - // the URL following the end of the matching text. However, it's possible - // that FormatUrl(), when formatting this suggestion for display, - // mucks with the text. We need to ensure that the text we're thinking - // about highlighting isn't in the middle of a mucked sequence. In - // particular, for the omnibox input of "x" or "xn", we may get a match - // in a punycoded domain name such as http://www.xn--blahblah.com/. - // When FormatUrl() processes the xn--blahblah part of the hostname, it'll - // transform the whole thing into a series of unicode characters. It's - // impossible to give the user an inline autocompletion of the text - // following "x" or "xn" in this case because those characters no longer - // exist in the displayed URL string. - size_t offset = - best_inlineable_prefix->prefix.length() + terms_vector[0].length(); - base::OffsetAdjuster::UnadjustOffset(adjustments, &offset); - if (offset != base::string16::npos) { - // Initialize innermost_match. - // The idea here is that matches that occur in the scheme or - // "www." are worse than matches which don't. For the URLs - // "http://www.google.com" and "http://wellsfargo.com", we want - // the omnibox input "w" to cause the latter URL to rank higher - // than the former. Note that this is not the same as checking - // whether one match's inlinable prefix has more components than - // the other match's, since in this example, both matches would - // have an inlinable prefix of "http://", which is one component. - // - // Instead, we look for the overall best (i.e., most components) - // prefix of the current URL, and then check whether the inlinable - // prefix has that many components. If it does, this is an - // "innermost" match, and should be boosted. In the example - // above, the best prefixes for the two URLs have two and one - // components respectively, while the inlinable prefixes each - // have one component; this means the first match is not innermost - // and the second match is innermost, resulting in us boosting the - // second match. - // - // Now, the code that implements this. - // The deepest prefix for this URL regardless of where the match is. - const URLPrefix* best_prefix = - URLPrefix::BestURLPrefix(gurl_spec, base::string16()); - DCHECK(best_prefix); - // If the URL is inlineable, we must have a match. Note the prefix that - // makes it inlineable may be empty. - can_inline = true; - innermost_match = (best_inlineable_prefix->num_components == - best_prefix->num_components); - } - } - } - - const float topicality_score = GetTopicalityScore( - terms_vector.size(), url, terms_to_word_starts_offsets, word_starts); - const float frequency_score = GetFrequency(now, is_url_bookmarked, visits); - raw_score = base::saturated_cast<int>(GetFinalRelevancyScore( - topicality_score, frequency_score, *hqp_relevance_buckets_)); - - if (also_do_hup_like_scoring_ && can_inline) { - // HistoryURL-provider-like scoring gives any match that is - // capable of being inlined a certain minimum score. Some of these - // are given a higher score that lets them be shown in inline. - // This test here derives from the test in - // HistoryURLProvider::PromoteMatchForInlineAutocomplete(). - const bool promote_to_inline = - (row.typed_count() > 1) || (IsHostOnly() && (row.typed_count() == 1)); - int hup_like_score = - promote_to_inline - ? HistoryURLProvider::kScoreForBestInlineableResult - : HistoryURLProvider::kBaseScoreForNonInlineableResult; - - // Also, if the user types the hostname of a host with a typed - // visit, then everything from that host get given inlineable scores - // (because the URL-that-you-typed will go first and everything - // else will be assigned one minus the previous score, as coded - // at the end of HistoryURLProvider::DoAutocomplete(). - if (base::UTF8ToUTF16(gurl.host()) == terms_vector[0]) - hup_like_score = HistoryURLProvider::kScoreForBestInlineableResult; - - // HistoryURLProvider has the function PromoteOrCreateShorterSuggestion() - // that's meant to promote prefixes of the best match (if they've - // been visited enough related to the best match) or - // create/promote host-only suggestions (even if they've never - // been typed). The code is complicated and we don't try to - // duplicate the logic here. Instead, we handle a simple case: in - // low-typed-count ranges, give host-only matches (i.e., - // http://www.foo.com/ vs. http://www.foo.com/bar.html) a boost so - // that the host-only match outscores all the other matches that - // would normally have the same base score. This behavior is not - // identical to what happens in HistoryURLProvider even in these - // low typed count ranges--sometimes it will create/promote when - // this test does not (indeed, we cannot create matches like HUP - // can) and vice versa--but the underlying philosophy is similar. - if (!promote_to_inline && IsHostOnly()) - hup_like_score++; - - // All the other logic to goes into hup-like-scoring happens in - // the tie-breaker case of MatchScoreGreater(). - - // Incorporate hup_like_score into raw_score. - raw_score = std::max(raw_score, hup_like_score); - } - - // Now that we're done processing this entry, correct the offsets of the - // matches in |url_matches| so they point to offsets in the original URL - // spec, not the cleaned-up URL string that we used for matching. - std::vector<size_t> offsets = OffsetsFromTermMatches(url_matches); - base::OffsetAdjuster::UnadjustOffsets(adjustments, &offsets); - url_matches = ReplaceOffsetsInTermMatches(url_matches, offsets); -} - -ScoredHistoryMatch::~ScoredHistoryMatch() { -} - -// Comparison function for sorting ScoredMatches by their scores with -// intelligent tie-breaking. -bool ScoredHistoryMatch::MatchScoreGreater(const ScoredHistoryMatch& m1, - const ScoredHistoryMatch& m2) { - if (m1.raw_score != m2.raw_score) - return m1.raw_score > m2.raw_score; - - // This tie-breaking logic is inspired by / largely copied from the - // ordering logic in history_url_provider.cc CompareHistoryMatch(). - - // A URL that has been typed at all is better than one that has never been - // typed. (Note "!"s on each side.) - if (!m1.url_info.typed_count() != !m2.url_info.typed_count()) - return m1.url_info.typed_count() > m2.url_info.typed_count(); - - // Innermost matches (matches after any scheme or "www.") are better than - // non-innermost matches. - if (m1.innermost_match != m2.innermost_match) - return m1.innermost_match; - - // URLs that have been typed more often are better. - if (m1.url_info.typed_count() != m2.url_info.typed_count()) - return m1.url_info.typed_count() > m2.url_info.typed_count(); - - // For URLs that have each been typed once, a host (alone) is better - // than a page inside. - if (m1.url_info.typed_count() == 1) { - if (m1.IsHostOnly() != m2.IsHostOnly()) - return m1.IsHostOnly(); - } - - // URLs that have been visited more often are better. - if (m1.url_info.visit_count() != m2.url_info.visit_count()) - return m1.url_info.visit_count() > m2.url_info.visit_count(); - - // URLs that have been visited more recently are better. - return m1.url_info.last_visit() > m2.url_info.last_visit(); -} - -// static -TermMatches ScoredHistoryMatch::FilterTermMatchesByWordStarts( - const TermMatches& term_matches, - const WordStarts& terms_to_word_starts_offsets, - const WordStarts& word_starts, - size_t start_pos, - size_t end_pos) { - // Return early if no filtering is needed. - if (start_pos == std::string::npos) - return term_matches; - TermMatches filtered_matches; - WordStarts::const_iterator next_word_starts = word_starts.begin(); - WordStarts::const_iterator end_word_starts = word_starts.end(); - for (const auto& term_match : term_matches) { - const size_t term_offset = - terms_to_word_starts_offsets[term_match.term_num]; - // Advance next_word_starts until it's >= the position of the term we're - // considering (adjusted for where the word begins within the term). - while ((next_word_starts != end_word_starts) && - (*next_word_starts < (term_match.offset + term_offset))) - ++next_word_starts; - // Add the match if it's before the position we start filtering at or - // after the position we stop filtering at (assuming we have a position - // to stop filtering at) or if it's at a word boundary. - if ((term_match.offset < start_pos) || - ((end_pos != std::string::npos) && (term_match.offset >= end_pos)) || - ((next_word_starts != end_word_starts) && - (*next_word_starts == term_match.offset + term_offset))) - filtered_matches.push_back(term_match); - } - return filtered_matches; -} - -// static -void ScoredHistoryMatch::Init() { - static bool initialized = false; - - if (initialized) - return; - - initialized = true; - also_do_hup_like_scoring_ = OmniboxFieldTrial::HQPAlsoDoHUPLikeScoring(); - bookmark_value_ = OmniboxFieldTrial::HQPBookmarkValue(); - fix_frequency_bugs_ = OmniboxFieldTrial::HQPFixFrequencyScoringBugs(); - allow_tld_matches_ = OmniboxFieldTrial::HQPAllowMatchInTLDValue(); - allow_scheme_matches_ = OmniboxFieldTrial::HQPAllowMatchInSchemeValue(); - num_title_words_to_allow_ = OmniboxFieldTrial::HQPNumTitleWordsToAllow(); - - InitRawTermScoreToTopicalityScoreArray(); - InitDaysAgoToRecencyScoreArray(); - InitHQPExperimentalParams(); -} - -float ScoredHistoryMatch::GetTopicalityScore( - const int num_terms, - const base::string16& url, - const WordStarts& terms_to_word_starts_offsets, - const RowWordStarts& word_starts) { - // A vector that accumulates per-term scores. The strongest match--a - // match in the hostname at a word boundary--is worth 10 points. - // Everything else is less. In general, a match that's not at a word - // boundary is worth about 1/4th or 1/5th of a match at the word boundary - // in the same part of the URL/title. - DCHECK_GT(num_terms, 0); - std::vector<int> term_scores(num_terms, 0); - WordStarts::const_iterator next_word_starts = - word_starts.url_word_starts_.begin(); - WordStarts::const_iterator end_word_starts = - word_starts.url_word_starts_.end(); - const size_t question_mark_pos = url.find('?'); - const size_t colon_pos = url.find(':'); - // The + 3 skips the // that probably appears in the protocol - // after the colon. If the protocol doesn't have two slashes after - // the colon, that's okay--all this ends up doing is starting our - // search for the next / a few characters into the hostname. The - // only times this can cause problems is if we have a protocol without - // a // after the colon and the hostname is only one or two characters. - // This isn't worth worrying about. - const size_t end_of_hostname_pos = (colon_pos != std::string::npos) - ? url.find('/', colon_pos + 3) - : url.find('/'); - size_t last_part_of_hostname_pos = (end_of_hostname_pos != std::string::npos) - ? url.rfind('.', end_of_hostname_pos) - : url.rfind('.'); - // Loop through all URL matches and score them appropriately. - // First, filter all matches not at a word boundary and in the path (or - // later). - url_matches = FilterTermMatchesByWordStarts( - url_matches, terms_to_word_starts_offsets, word_starts.url_word_starts_, - end_of_hostname_pos, std::string::npos); - if (colon_pos != std::string::npos) { - // Also filter matches not at a word boundary and in the scheme. - url_matches = FilterTermMatchesByWordStarts( - url_matches, terms_to_word_starts_offsets, word_starts.url_word_starts_, - 0, colon_pos); - } - for (const auto& url_match : url_matches) { - const size_t term_offset = terms_to_word_starts_offsets[url_match.term_num]; - // Advance next_word_starts until it's >= the position of the term we're - // considering (adjusted for where the word begins within the term). - while ((next_word_starts != end_word_starts) && - (*next_word_starts < (url_match.offset + term_offset))) { - ++next_word_starts; - } - const bool at_word_boundary = - (next_word_starts != end_word_starts) && - (*next_word_starts == url_match.offset + term_offset); - if ((question_mark_pos != std::string::npos) && - (url_match.offset > question_mark_pos)) { - // The match is in a CGI ?... fragment. - DCHECK(at_word_boundary); - term_scores[url_match.term_num] += 5; - } else if ((end_of_hostname_pos != std::string::npos) && - (url_match.offset > end_of_hostname_pos)) { - // The match is in the path. - DCHECK(at_word_boundary); - term_scores[url_match.term_num] += 8; - } else if ((colon_pos == std::string::npos) || - (url_match.offset > colon_pos)) { - // The match is in the hostname. - if ((last_part_of_hostname_pos == std::string::npos) || - (url_match.offset < last_part_of_hostname_pos)) { - // Either there are no dots in the hostname or this match isn't - // the last dotted component. - term_scores[url_match.term_num] += at_word_boundary ? 10 : 2; - } else { - // The match is in the last part of a dotted hostname (usually this - // is the top-level domain .com, .net, etc.). - if (allow_tld_matches_) - term_scores[url_match.term_num] += at_word_boundary ? 10 : 0; - } - } else { - // The match is in the protocol (a.k.a. scheme). - // Matches not at a word boundary should have been filtered already. - DCHECK(at_word_boundary); - match_in_scheme = true; - if (allow_scheme_matches_) - term_scores[url_match.term_num] += 10; - } - } - // Now do the analogous loop over all matches in the title. - next_word_starts = word_starts.title_word_starts_.begin(); - end_word_starts = word_starts.title_word_starts_.end(); - size_t word_num = 0; - title_matches = FilterTermMatchesByWordStarts( - title_matches, terms_to_word_starts_offsets, - word_starts.title_word_starts_, 0, std::string::npos); - for (const auto& title_match : title_matches) { - const size_t term_offset = - terms_to_word_starts_offsets[title_match.term_num]; - // Advance next_word_starts until it's >= the position of the term we're - // considering (adjusted for where the word begins within the term). - while ((next_word_starts != end_word_starts) && - (*next_word_starts < (title_match.offset + term_offset))) { - ++next_word_starts; - ++word_num; - } - if (word_num >= num_title_words_to_allow_) - break; // only count the first ten words - DCHECK(next_word_starts != end_word_starts); - DCHECK_EQ(*next_word_starts, title_match.offset + term_offset) - << "not at word boundary"; - term_scores[title_match.term_num] += 8; - } - // TODO(mpearson): Restore logic for penalizing out-of-order matches. - // (Perhaps discount them by 0.8?) - // TODO(mpearson): Consider: if the earliest match occurs late in the string, - // should we discount it? - // TODO(mpearson): Consider: do we want to score based on how much of the - // input string the input covers? (I'm leaning toward no.) - - // Compute the topicality_score as the sum of transformed term_scores. - float topicality_score = 0; - for (int term_score : term_scores) { - // Drop this URL if it seems like a term didn't appear or, more precisely, - // didn't appear in a part of the URL or title that we trust enough - // to give it credit for. For instance, terms that appear in the middle - // of a CGI parameter get no credit. Almost all the matches dropped - // due to this test would look stupid if shown to the user. - if (term_score == 0) - return 0; - topicality_score += raw_term_score_to_topicality_score[std::min( - term_score, kMaxRawTermScore - 1)]; - } - // TODO(mpearson): If there are multiple terms, consider taking the - // geometric mean of per-term scores rather than the arithmetic mean. - - const float final_topicality_score = topicality_score / num_terms; - - // Demote the URL if the topicality score is less than threshold. - if (hqp_experimental_scoring_enabled_ && - (final_topicality_score < topicality_threshold_)) { - return 0.0; - } - - return final_topicality_score; -} - -float ScoredHistoryMatch::GetRecencyScore(int last_visit_days_ago) const { - // Lookup the score in days_ago_to_recency_score, treating - // everything older than what we've precomputed as the oldest thing - // we've precomputed. The std::max is to protect against corruption - // in the database (in case last_visit_days_ago is negative). - return days_ago_to_recency_score[std::max( - std::min(last_visit_days_ago, kDaysToPrecomputeRecencyScoresFor - 1), 0)]; -} - -float ScoredHistoryMatch::GetFrequency(const base::Time& now, - const bool bookmarked, - const VisitInfoVector& visits) const { - // Compute the weighted average |value_of_transition| over the last at - // most kMaxVisitsToScore visits, where each visit is weighted using - // GetRecencyScore() based on how many days ago it happened. Use - // kMaxVisitsToScore as the denominator for the average regardless of - // how many visits there were in order to penalize a match that has - // fewer visits than kMaxVisitsToScore. - float summed_visit_points = 0; - const size_t max_visit_to_score = - std::min(visits.size(), ScoredHistoryMatch::kMaxVisitsToScore); - for (size_t i = 0; i < max_visit_to_score; ++i) { - const ui::PageTransition page_transition = fix_frequency_bugs_ ? - ui::PageTransitionStripQualifier(visits[i].second) : visits[i].second; - int value_of_transition = - (page_transition == ui::PAGE_TRANSITION_TYPED) ? 20 : 1; - if (bookmarked) - value_of_transition = std::max(value_of_transition, bookmark_value_); - const float bucket_weight = - GetRecencyScore((now - visits[i].first).InDays()); - summed_visit_points += (value_of_transition * bucket_weight); - } - if (fix_frequency_bugs_) - return summed_visit_points / ScoredHistoryMatch::kMaxVisitsToScore; - return visits.size() * summed_visit_points / - ScoredHistoryMatch::kMaxVisitsToScore; -} - -// static -float ScoredHistoryMatch::GetFinalRelevancyScore( - float topicality_score, - float frequency_score, - const std::vector<ScoreMaxRelevance>& hqp_relevance_buckets) { - DCHECK(hqp_relevance_buckets.size() > 0); - DCHECK_EQ(hqp_relevance_buckets[0].first, 0.0); - - if (topicality_score == 0) - return 0; - // Here's how to interpret intermediate_score: Suppose the omnibox - // has one input term. Suppose we have a URL for which the omnibox - // input term has a single URL hostname hit at a word boundary. (This - // implies topicality_score = 1.0.). Then the intermediate_score for - // this URL will depend entirely on the frequency_score with - // this interpretation: - // - a single typed visit more than three months ago, no other visits -> 0.2 - // - a visit every three days, no typed visits -> 0.706 - // - a visit every day, no typed visits -> 0.916 - // - a single typed visit yesterday, no other visits -> 2.0 - // - a typed visit once a week -> 11.77 - // - a typed visit every three days -> 14.12 - // - at least ten typed visits today -> 20.0 (maximum score) - // - // The below code maps intermediate_score to the range [0, 1399]. - // For example: - // HQP default scoring buckets: "0.0:400,1.5:600,12.0:1300,20.0:1399" - // We will linearly interpolate the scores between: - // 0 to 1.5 --> 400 to 600 - // 1.5 to 12.0 --> 600 to 1300 - // 12.0 to 20.0 --> 1300 to 1399 - // >= 20.0 --> 1399 - // - // The score maxes out at 1399 (i.e., cannot beat a good inlineable result - // from HistoryURL provider). - const float intermediate_score = topicality_score * frequency_score; - - // Find the threshold where intermediate score is greater than bucket. - size_t i = 1; - for (; i < hqp_relevance_buckets.size(); ++i) { - const ScoreMaxRelevance& hqp_bucket = hqp_relevance_buckets[i]; - if (intermediate_score >= hqp_bucket.first) { - continue; - } - const ScoreMaxRelevance& previous_bucket = hqp_relevance_buckets[i - 1]; - const float slope = ((hqp_bucket.second - previous_bucket.second) / - (hqp_bucket.first - previous_bucket.first)); - return (previous_bucket.second + - (slope * (intermediate_score - previous_bucket.first))); - } - // It will reach this stage when the score is > highest bucket score. - // Return the highest bucket score. - return hqp_relevance_buckets[i - 1].second; -} - -// static -void ScoredHistoryMatch::InitHQPExperimentalParams() { - // These are default HQP relevance scoring buckets. - // See GetFinalRelevancyScore() for details. - std::string hqp_relevance_buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399"; - - // Fetch the experiment params if they are any. - hqp_experimental_scoring_enabled_ = - OmniboxFieldTrial::HQPExperimentalScoringEnabled(); - - if (hqp_experimental_scoring_enabled_) { - // Add the topicality threshold from experiment params. - float hqp_experimental_topicality_threhold = - OmniboxFieldTrial::HQPExperimentalTopicalityThreshold(); - topicality_threshold_ = hqp_experimental_topicality_threhold; - - // Add the HQP experimental scoring buckets. - std::string hqp_experimental_scoring_buckets = - OmniboxFieldTrial::HQPExperimentalScoringBuckets(); - if (!hqp_experimental_scoring_buckets.empty()) - hqp_relevance_buckets_str = hqp_experimental_scoring_buckets; - } - - // Parse the hqp_relevance_buckets_str string once and store them in vector - // which is easy to access. - hqp_relevance_buckets_ = - new std::vector<ScoredHistoryMatch::ScoreMaxRelevance>(); - - bool is_valid_bucket_str = GetHQPBucketsFromString(hqp_relevance_buckets_str, - hqp_relevance_buckets_); - DCHECK(is_valid_bucket_str); -} - -// static -bool ScoredHistoryMatch::GetHQPBucketsFromString( - const std::string& buckets_str, - std::vector<ScoreMaxRelevance>* hqp_buckets) { - DCHECK(hqp_buckets != NULL); - DCHECK(!buckets_str.empty()); - - base::StringPairs kv_pairs; - if (base::SplitStringIntoKeyValuePairs(buckets_str, ':', ',', &kv_pairs)) { - for (base::StringPairs::const_iterator it = kv_pairs.begin(); - it != kv_pairs.end(); ++it) { - ScoreMaxRelevance bucket; - bool is_valid_intermediate_score = - base::StringToDouble(it->first, &bucket.first); - DCHECK(is_valid_intermediate_score); - bool is_valid_hqp_score = base::StringToInt(it->second, &bucket.second); - DCHECK(is_valid_hqp_score); - hqp_buckets->push_back(bucket); - } - return true; - } - return false; -} diff --git a/chrome/browser/autocomplete/scored_history_match.h b/chrome/browser/autocomplete/scored_history_match.h deleted file mode 100644 index d9b5ae45..0000000 --- a/chrome/browser/autocomplete/scored_history_match.h +++ /dev/null @@ -1,196 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ -#define CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ - -#include <string> -#include <vector> - -#include "base/strings/string16.h" -#include "base/time/time.h" -#include "components/history/core/browser/history_match.h" -#include "components/history/core/browser/history_types.h" -#include "components/omnibox/in_memory_url_index_types.h" -#include "testing/gtest/include/gtest/gtest_prod.h" - -class ScoredHistoryMatchTest; - -// An HistoryMatch that has a score as well as metrics defining where in the -// history item's URL and/or page title matches have occurred. -struct ScoredHistoryMatch : public history::HistoryMatch { - // ScoreMaxRelevance maps from an intermediate-score to the maximum - // final-relevance score given to a URL for this intermediate score. - // This is used to store the score ranges of HQP relevance buckets. - // Please see GetFinalRelevancyScore() for details. - typedef std::pair<double, int> ScoreMaxRelevance; - - // Required for STL, we don't use this directly. - ScoredHistoryMatch(); - - // Initializes the ScoredHistoryMatch with a raw score calculated for the - // history item given in |row| with recent visits as indicated in |visits|. It - // first determines if the row qualifies by seeing if all of the terms in - // |terms_vector| occur in |row|. If so, calculates a raw score. This raw - // score is in part determined by whether the matches occur at word - // boundaries, the locations of which are stored in |word_starts|. For some - // terms, it's appropriate to look for the word boundary within the term. For - // instance, the term ".net" should look for a word boundary at the "n". These - // offsets (".net" should have an offset of 1) come from - // |terms_to_word_starts_offsets|. |is_url_bookmarked| indicates whether the - // match's URL is referenced by any bookmarks, which can also affect the raw - // score. The raw score allows the matches to be ordered and can be used to - // influence the final score calculated by the client of this index. If the - // row does not qualify the raw score will be 0. |languages| is used to help - // parse/format the URL before looking for the terms. - ScoredHistoryMatch(const history::URLRow& row, - const VisitInfoVector& visits, - const std::string& languages, - const base::string16& lower_string, - const String16Vector& terms_vector, - const WordStarts& terms_to_word_starts_offsets, - const RowWordStarts& word_starts, - bool is_url_bookmarked, - base::Time now); - - ~ScoredHistoryMatch(); - - // Compares two matches by score. Functor supporting URLIndexPrivateData's - // HistoryItemsForTerms function. Looks at particular fields within - // with url_info to make tie-breaking a bit smarter. - static bool MatchScoreGreater(const ScoredHistoryMatch& m1, - const ScoredHistoryMatch& m2); - - // Returns |term_matches| after removing all matches that are not at a - // word break that are in the range [|start_pos|, |end_pos|). - // start_pos == string::npos is treated as start_pos = length of string. - // (In other words, no matches will be filtered.) - // end_pos == string::npos is treated as end_pos = length of string. - static TermMatches FilterTermMatchesByWordStarts( - const TermMatches& term_matches, - const WordStarts& terms_to_word_starts_offsets, - const WordStarts& word_starts, - size_t start_pos, - size_t end_pos); - - // The maximum number of recent visits to examine in GetFrequency(). - // Public so url_index_private_data.cc knows how many visits it is - // expected to deliver (at minimum) to this class. - static const size_t kMaxVisitsToScore; - - // An interim score taking into consideration location and completeness - // of the match. - int raw_score; - - // Both these TermMatches contain the set of matches that are considered - // important. At this time, that means they exclude mid-word matches - // except in the hostname of the URL. (Technically, during early - // construction of ScoredHistoryMatch, they may contain all matches, but - // unimportant matches are eliminated by GetTopicalityScore(), called - // during construction.) - - // Term matches within the URL. - TermMatches url_matches; - // Term matches within the page title. - TermMatches title_matches; - - // True if this is a candidate for in-line autocompletion. - bool can_inline; - - private: - friend class ScoredHistoryMatchTest; - FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, GetFinalRelevancyScore); - FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, GetHQPBucketsFromString); - FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringBookmarks); - FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringScheme); - FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringTLD); - - // Initialize ScoredHistoryMatch statics. Must be called before any other - // method of ScoredHistoryMatch and before creating any instances. - static void Init(); - - // Return a topicality score based on how many matches appear in the url and - // the page's title and where they are (e.g., at word boundaries). Revises - // url_matches and title_matches in the process so they only reflect matches - // used for scoring. (For instance, some mid-word matches are not given - // credit in scoring.) - float GetTopicalityScore(const int num_terms, - const base::string16& cleaned_up_url, - const WordStarts& terms_to_word_starts_offsets, - const RowWordStarts& word_starts); - - // Returns a recency score based on |last_visit_days_ago|, which is - // how many days ago the page was last visited. - float GetRecencyScore(int last_visit_days_ago) const; - - // Examines the first kMaxVisitsToScore and return a score (higher is - // better) based the rate of visits, whether the page is bookmarked, and - // how often those visits are typed navigations (i.e., explicitly - // invoked by the user). |now| is passed in to avoid unnecessarily - // recomputing it frequently. - float GetFrequency(const base::Time& now, - const bool bookmarked, - const VisitInfoVector& visits) const; - - // Combines the two component scores into a final score that's - // an appropriate value to use as a relevancy score. Scoring buckets are - // specified through |hqp_relevance_buckets|. Please see the function - // implementation for more details. - static float GetFinalRelevancyScore( - float topicality_score, - float frequency_score, - const std::vector<ScoreMaxRelevance>& hqp_relevance_buckets); - - // Initializes the HQP experimental params: |hqp_relevance_buckets_| - // to default buckets. If hqp experimental scoring is enabled, it - // fetches the |hqp_experimental_scoring_enabled_|, |topicality_threshold_| - // and |hqp_relevance_buckets_| from omnibox field trials. - static void InitHQPExperimentalParams(); - - // Helper function to parse the string containing the scoring buckets. - // For example, - // String: "0.0:400,1.5:600,12.0:1300,20.0:1399" - // Buckets: vector[(0.0, 400),(1.5,600),(12.0,1300),(20.0,1399)] - // Returns false, in case if it fail to parse the string. - static bool GetHQPBucketsFromString( - const std::string& buckets_str, - std::vector<ScoreMaxRelevance>* hqp_buckets); - - // If true, assign raw scores to be max(whatever it normally would be, a - // score that's similar to the score HistoryURL provider would assign). - static bool also_do_hup_like_scoring_; - - // Untyped visits to bookmarked pages score this, compared to 1 for - // untyped visits to non-bookmarked pages and 20 for typed visits. - static int bookmark_value_; - - // True if we should fix certain bugs in frequency scoring. - static bool fix_frequency_bugs_; - - // If true, we allow input terms to match in the TLD (e.g., ".com"). - static bool allow_tld_matches_; - - // If true, we allow input terms to match in the scheme (e.g., "http://"). - static bool allow_scheme_matches_; - - // The number of title words examined when computing topicality scores. - // Words beyond this number are ignored. - static size_t num_title_words_to_allow_; - - // True, if hqp experimental scoring is enabled. - static bool hqp_experimental_scoring_enabled_; - - // |topicality_threshold_| is used to control the topicality scoring. - // If |topicality_threshold_| > 0, then URLs with topicality-score < threshold - // are given topicality score of 0. By default it is initalized to -1. - static float topicality_threshold_; - - // |hqp_relevance_buckets_| gives mapping from (topicality*frequency) - // to the final relevance scoring. Please see GetFinalRelevancyScore() - // for more details and scoring method. - static std::vector<ScoreMaxRelevance>* hqp_relevance_buckets_; -}; -typedef std::vector<ScoredHistoryMatch> ScoredHistoryMatches; - -#endif // CHROME_BROWSER_AUTOCOMPLETE_SCORED_HISTORY_MATCH_H_ diff --git a/chrome/browser/autocomplete/scored_history_match_unittest.cc b/chrome/browser/autocomplete/scored_history_match_unittest.cc deleted file mode 100644 index cb1e578..0000000 --- a/chrome/browser/autocomplete/scored_history_match_unittest.cc +++ /dev/null @@ -1,492 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/autocomplete/scored_history_match.h" - -#include <algorithm> - -#include "base/auto_reset.h" -#include "base/bind.h" -#include "base/memory/scoped_ptr.h" -#include "base/strings/string16.h" -#include "base/strings/utf_string_conversions.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -using base::ASCIIToUTF16; -using testing::ElementsAre; -using testing::Pair; - -namespace { - -// Returns a VisitInfoVector that includes |num_visits| spread over the -// last |frequency|*|num_visits| days (relative to |now|). A frequency of -// one means one visit each day, two means every other day, etc. -VisitInfoVector CreateVisitInfoVector(int num_visits, - int frequency, - base::Time now) { - VisitInfoVector visits; - for (int i = 0; i < num_visits; ++i) { - visits.push_back( - std::make_pair(now - base::TimeDelta::FromDays(i * frequency), - ui::PAGE_TRANSITION_LINK)); - } - return visits; -} - -} // namespace - -class ScoredHistoryMatchTest : public testing::Test { - protected: - // Convenience function to create a history::URLRow with basic data for |url|, - // |title|, |visit_count|, and |typed_count|. |days_since_last_visit| gives - // the number of days ago to which to set the URL's last_visit. - history::URLRow MakeURLRow(const char* url, - const char* title, - int visit_count, - int days_since_last_visit, - int typed_count); - - // Convenience function to set the word starts information from a - // history::URLRow's URL and title. - void PopulateWordStarts(const history::URLRow& url_row, - RowWordStarts* word_starts); - - // Convenience functions for easily creating vectors of search terms. - String16Vector Make1Term(const char* term) const; - String16Vector Make2Terms(const char* term_1, const char* term_2) const; - - // Convenience function for GetTopicalityScore() that builds the term match - // and word break information automatically that are needed to call - // GetTopicalityScore(). It only works for scoring a single term, not - // multiple terms. - float GetTopicalityScoreOfTermAgainstURLAndTitle(const base::string16& term, - const base::string16& url, - const base::string16& title); -}; - -history::URLRow ScoredHistoryMatchTest::MakeURLRow(const char* url, - const char* title, - int visit_count, - int days_since_last_visit, - int typed_count) { - history::URLRow row(GURL(url), 0); - row.set_title(ASCIIToUTF16(title)); - row.set_visit_count(visit_count); - row.set_typed_count(typed_count); - row.set_last_visit(base::Time::NowFromSystemTime() - - base::TimeDelta::FromDays(days_since_last_visit)); - return row; -} - -void ScoredHistoryMatchTest::PopulateWordStarts(const history::URLRow& url_row, - RowWordStarts* word_starts) { - String16SetFromString16(ASCIIToUTF16(url_row.url().spec()), - &word_starts->url_word_starts_); - String16SetFromString16(url_row.title(), &word_starts->title_word_starts_); -} - -String16Vector ScoredHistoryMatchTest::Make1Term(const char* term) const { - String16Vector original_terms; - original_terms.push_back(ASCIIToUTF16(term)); - return original_terms; -} - -String16Vector ScoredHistoryMatchTest::Make2Terms(const char* term_1, - const char* term_2) const { - String16Vector original_terms; - original_terms.push_back(ASCIIToUTF16(term_1)); - original_terms.push_back(ASCIIToUTF16(term_2)); - return original_terms; -} - -float ScoredHistoryMatchTest::GetTopicalityScoreOfTermAgainstURLAndTitle( - const base::string16& term, - const base::string16& url, - const base::string16& title) { - // Make an empty match and simply populate the fields we need in order - // to call GetTopicalityScore(). - ScoredHistoryMatch scored_match; - scored_match.url_matches = MatchTermInString(term, url, 0); - scored_match.title_matches = MatchTermInString(term, title, 0); - RowWordStarts word_starts; - String16SetFromString16(url, &word_starts.url_word_starts_); - String16SetFromString16(title, &word_starts.title_word_starts_); - WordStarts one_word_no_offset(1, 0u); - return scored_match.GetTopicalityScore(1, url, one_word_no_offset, - word_starts); -} - -TEST_F(ScoredHistoryMatchTest, Scoring) { - // We use NowFromSystemTime() because MakeURLRow uses the same function - // to calculate last visit time when building a row. - base::Time now = base::Time::NowFromSystemTime(); - - history::URLRow row_a(MakeURLRow("http://fedcba", "abcd bcd", 3, 30, 1)); - RowWordStarts word_starts_a; - PopulateWordStarts(row_a, &word_starts_a); - WordStarts one_word_no_offset(1, 0u); - VisitInfoVector visits_a = CreateVisitInfoVector(3, 30, now); - // Mark one visit as typed. - visits_a[0].second = ui::PAGE_TRANSITION_TYPED; - ScoredHistoryMatch scored_a(row_a, visits_a, std::string(), - ASCIIToUTF16("abc"), Make1Term("abc"), - one_word_no_offset, word_starts_a, false, now); - - // Test scores based on visit_count. - history::URLRow row_b(MakeURLRow("http://abcdef", "abcd bcd", 10, 30, 1)); - RowWordStarts word_starts_b; - PopulateWordStarts(row_b, &word_starts_b); - VisitInfoVector visits_b = CreateVisitInfoVector(10, 30, now); - visits_b[0].second = ui::PAGE_TRANSITION_TYPED; - ScoredHistoryMatch scored_b(row_b, visits_b, std::string(), - ASCIIToUTF16("abc"), Make1Term("abc"), - one_word_no_offset, word_starts_b, false, now); - EXPECT_GT(scored_b.raw_score, scored_a.raw_score); - - // Test scores based on last_visit. - history::URLRow row_c(MakeURLRow("http://abcdef", "abcd bcd", 3, 10, 1)); - RowWordStarts word_starts_c; - PopulateWordStarts(row_c, &word_starts_c); - VisitInfoVector visits_c = CreateVisitInfoVector(3, 10, now); - visits_c[0].second = ui::PAGE_TRANSITION_TYPED; - ScoredHistoryMatch scored_c(row_c, visits_c, std::string(), - ASCIIToUTF16("abc"), Make1Term("abc"), - one_word_no_offset, word_starts_c, false, now); - EXPECT_GT(scored_c.raw_score, scored_a.raw_score); - - // Test scores based on typed_count. - history::URLRow row_d(MakeURLRow("http://abcdef", "abcd bcd", 3, 30, 3)); - RowWordStarts word_starts_d; - PopulateWordStarts(row_d, &word_starts_d); - VisitInfoVector visits_d = CreateVisitInfoVector(3, 30, now); - visits_d[0].second = ui::PAGE_TRANSITION_TYPED; - visits_d[1].second = ui::PAGE_TRANSITION_TYPED; - visits_d[2].second = ui::PAGE_TRANSITION_TYPED; - ScoredHistoryMatch scored_d(row_d, visits_d, std::string(), - ASCIIToUTF16("abc"), Make1Term("abc"), - one_word_no_offset, word_starts_d, false, now); - EXPECT_GT(scored_d.raw_score, scored_a.raw_score); - - // Test scores based on a terms appearing multiple times. - history::URLRow row_e(MakeURLRow( - "http://csi.csi.csi/csi_csi", - "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 3)); - RowWordStarts word_starts_e; - PopulateWordStarts(row_e, &word_starts_e); - const VisitInfoVector visits_e = visits_d; - ScoredHistoryMatch scored_e(row_e, visits_e, std::string(), - ASCIIToUTF16("csi"), Make1Term("csi"), - one_word_no_offset, word_starts_e, false, now); - EXPECT_LT(scored_e.raw_score, 1400); - - // Test that a result with only a mid-term match (i.e., not at a word - // boundary) scores 0. - ScoredHistoryMatch scored_f(row_a, visits_a, std::string(), - ASCIIToUTF16("cd"), Make1Term("cd"), - one_word_no_offset, word_starts_a, false, now); - EXPECT_EQ(scored_f.raw_score, 0); -} - -TEST_F(ScoredHistoryMatchTest, ScoringBookmarks) { - // We use NowFromSystemTime() because MakeURLRow uses the same function - // to calculate last visit time when building a row. - base::Time now = base::Time::NowFromSystemTime(); - - std::string url_string("http://fedcba"); - const GURL url(url_string); - history::URLRow row(MakeURLRow(url_string.c_str(), "abcd bcd", 8, 3, 1)); - RowWordStarts word_starts; - PopulateWordStarts(row, &word_starts); - WordStarts one_word_no_offset(1, 0u); - VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); - ScoredHistoryMatch scored(row, visits, std::string(), ASCIIToUTF16("abc"), - Make1Term("abc"), one_word_no_offset, word_starts, - false, now); - // Now check that if URL is bookmarked then its score increases. - base::AutoReset<int> reset(&ScoredHistoryMatch::bookmark_value_, 5); - ScoredHistoryMatch scored_with_bookmark( - row, visits, std::string(), ASCIIToUTF16("abc"), Make1Term("abc"), - one_word_no_offset, word_starts, true, now); - EXPECT_GT(scored_with_bookmark.raw_score, scored.raw_score); -} - -TEST_F(ScoredHistoryMatchTest, ScoringTLD) { - // We use NowFromSystemTime() because MakeURLRow uses the same function - // to calculate last visit time when building a row. - base::Time now = base::Time::NowFromSystemTime(); - - // By default the URL should not be returned for a query that includes "com". - std::string url_string("http://fedcba.com/"); - const GURL url(url_string); - history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); - RowWordStarts word_starts; - PopulateWordStarts(row, &word_starts); - WordStarts two_words_no_offsets(2, 0u); - VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); - ScoredHistoryMatch scored(row, visits, std::string(), ASCIIToUTF16("fed com"), - Make2Terms("fed", "com"), two_words_no_offsets, - word_starts, false, now); - EXPECT_EQ(0, scored.raw_score); - - // Now allow credit for the match in the TLD. - base::AutoReset<bool> reset(&ScoredHistoryMatch::allow_tld_matches_, true); - ScoredHistoryMatch scored_with_tld( - row, visits, std::string(), ASCIIToUTF16("fed com"), - Make2Terms("fed", "com"), two_words_no_offsets, word_starts, false, now); - EXPECT_GT(scored_with_tld.raw_score, 0); -} - -TEST_F(ScoredHistoryMatchTest, ScoringScheme) { - // We use NowFromSystemTime() because MakeURLRow uses the same function - // to calculate last visit time when building a row. - base::Time now = base::Time::NowFromSystemTime(); - - // By default the URL should not be returned for a query that includes "http". - std::string url_string("http://fedcba/"); - const GURL url(url_string); - history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); - RowWordStarts word_starts; - PopulateWordStarts(row, &word_starts); - WordStarts two_words_no_offsets(2, 0u); - VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); - ScoredHistoryMatch scored(row, visits, std::string(), - ASCIIToUTF16("fed http"), Make2Terms("fed", "http"), - two_words_no_offsets, word_starts, false, now); - EXPECT_EQ(0, scored.raw_score); - - // Now allow credit for the match in the scheme. - base::AutoReset<bool> reset(&ScoredHistoryMatch::allow_scheme_matches_, true); - ScoredHistoryMatch scored_with_scheme( - row, visits, std::string(), ASCIIToUTF16("fed http"), - Make2Terms("fed", "http"), two_words_no_offsets, word_starts, false, now); - EXPECT_GT(scored_with_scheme.raw_score, 0); -} - -TEST_F(ScoredHistoryMatchTest, Inlining) { - // We use NowFromSystemTime() because MakeURLRow uses the same function - // to calculate last visit time when building a row. - base::Time now = base::Time::NowFromSystemTime(); - RowWordStarts word_starts; - WordStarts one_word_no_offset(1, 0u); - VisitInfoVector visits; - - { - history::URLRow row( - MakeURLRow("http://www.google.com", "abcdef", 3, 30, 1)); - PopulateWordStarts(row, &word_starts); - ScoredHistoryMatch scored_a(row, visits, std::string(), ASCIIToUTF16("g"), - Make1Term("g"), one_word_no_offset, word_starts, - false, now); - EXPECT_TRUE(scored_a.can_inline); - EXPECT_FALSE(scored_a.match_in_scheme); - ScoredHistoryMatch scored_b(row, visits, std::string(), ASCIIToUTF16("w"), - Make1Term("w"), one_word_no_offset, word_starts, - false, now); - EXPECT_TRUE(scored_b.can_inline); - EXPECT_FALSE(scored_b.match_in_scheme); - ScoredHistoryMatch scored_c(row, visits, std::string(), ASCIIToUTF16("h"), - Make1Term("h"), one_word_no_offset, word_starts, - false, now); - EXPECT_TRUE(scored_c.can_inline); - EXPECT_TRUE(scored_c.match_in_scheme); - ScoredHistoryMatch scored_d(row, visits, std::string(), ASCIIToUTF16("o"), - Make1Term("o"), one_word_no_offset, word_starts, - false, now); - EXPECT_FALSE(scored_d.can_inline); - EXPECT_FALSE(scored_d.match_in_scheme); - } - - { - history::URLRow row(MakeURLRow("http://teams.foo.com", "abcdef", 3, 30, 1)); - PopulateWordStarts(row, &word_starts); - ScoredHistoryMatch scored_a(row, visits, std::string(), ASCIIToUTF16("t"), - Make1Term("t"), one_word_no_offset, word_starts, - false, now); - EXPECT_TRUE(scored_a.can_inline); - EXPECT_FALSE(scored_a.match_in_scheme); - ScoredHistoryMatch scored_b(row, visits, std::string(), ASCIIToUTF16("f"), - Make1Term("f"), one_word_no_offset, word_starts, - false, now); - EXPECT_FALSE(scored_b.can_inline); - EXPECT_FALSE(scored_b.match_in_scheme); - ScoredHistoryMatch scored_c(row, visits, std::string(), ASCIIToUTF16("o"), - Make1Term("o"), one_word_no_offset, word_starts, - false, now); - EXPECT_FALSE(scored_c.can_inline); - EXPECT_FALSE(scored_c.match_in_scheme); - } - - { - history::URLRow row( - MakeURLRow("https://www.testing.com", "abcdef", 3, 30, 1)); - PopulateWordStarts(row, &word_starts); - ScoredHistoryMatch scored_a(row, visits, std::string(), ASCIIToUTF16("t"), - Make1Term("t"), one_word_no_offset, word_starts, - false, now); - EXPECT_TRUE(scored_a.can_inline); - EXPECT_FALSE(scored_a.match_in_scheme); - ScoredHistoryMatch scored_b(row, visits, std::string(), ASCIIToUTF16("h"), - Make1Term("h"), one_word_no_offset, word_starts, - false, now); - EXPECT_TRUE(scored_b.can_inline); - EXPECT_TRUE(scored_b.match_in_scheme); - ScoredHistoryMatch scored_c(row, visits, std::string(), ASCIIToUTF16("w"), - Make1Term("w"), one_word_no_offset, word_starts, - false, now); - EXPECT_TRUE(scored_c.can_inline); - EXPECT_FALSE(scored_c.match_in_scheme); - } - - { - history::URLRow row( - MakeURLRow("http://www.xn--1lq90ic7f1rc.cn/xnblah", "abcd", 3, 30, 1)); - PopulateWordStarts(row, &word_starts); - ScoredHistoryMatch scored_a(row, visits, "zh-CN", ASCIIToUTF16("x"), - Make1Term("x"), one_word_no_offset, word_starts, - false, now); - EXPECT_FALSE(scored_a.can_inline); - EXPECT_FALSE(scored_a.match_in_scheme); - ScoredHistoryMatch scored_b(row, visits, "zh-CN", ASCIIToUTF16("xn"), - Make1Term("xn"), one_word_no_offset, - word_starts, false, now); - EXPECT_FALSE(scored_b.can_inline); - EXPECT_FALSE(scored_b.match_in_scheme); - ScoredHistoryMatch scored_c(row, visits, "zh-CN", ASCIIToUTF16("w"), - Make1Term("w"), one_word_no_offset, - word_starts, false, now); - EXPECT_TRUE(scored_c.can_inline); - EXPECT_FALSE(scored_c.match_in_scheme); - } -} - -TEST_F(ScoredHistoryMatchTest, GetTopicalityScoreTrailingSlash) { - const float hostname = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com/"), - ASCIIToUTF16("Non-Matching Title")); - const float hostname_no_slash = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com"), - ASCIIToUTF16("Non-Matching Title")); - EXPECT_EQ(hostname_no_slash, hostname); -} - -// This function only tests scoring of single terms that match exactly -// once somewhere in the URL or title. -TEST_F(ScoredHistoryMatchTest, GetTopicalityScore) { - base::string16 url = ASCIIToUTF16( - "http://abc.def.com/path1/path2?" - "arg1=val1&arg2=val2#hash_component"); - base::string16 title = ASCIIToUTF16("here is a title"); - const float hostname_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("abc"), url, title); - const float hostname_mid_word_score = - GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16("bc"), url, - title); - const float domain_name_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("def"), url, title); - const float domain_name_mid_word_score = - GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16("ef"), url, - title); - const float tld_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("com"), url, title); - const float tld_mid_word_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("om"), url, title); - const float path_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("path1"), url, title); - const float path_mid_word_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("ath1"), url, title); - const float arg_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("arg2"), url, title); - const float arg_mid_word_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("rg2"), url, title); - const float protocol_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("htt"), url, title); - const float protocol_mid_word_score = - GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16("tt"), url, - title); - const float title_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("her"), url, title); - const float title_mid_word_score = GetTopicalityScoreOfTermAgainstURLAndTitle( - ASCIIToUTF16("er"), url, title); - // Verify hostname and domain name > path > arg. - EXPECT_GT(hostname_score, path_score); - EXPECT_GT(domain_name_score, path_score); - EXPECT_GT(path_score, arg_score); - // Verify that domain name > path and domain name > arg for non-word - // boundaries. - EXPECT_GT(hostname_mid_word_score, path_mid_word_score); - EXPECT_GT(domain_name_mid_word_score, path_mid_word_score); - EXPECT_GT(domain_name_mid_word_score, arg_mid_word_score); - EXPECT_GT(hostname_mid_word_score, arg_mid_word_score); - // Also verify that the matches at non-word-boundaries all score - // worse than the matches at word boundaries. These three sets suffice. - EXPECT_GT(arg_score, hostname_mid_word_score); - EXPECT_GT(arg_score, domain_name_mid_word_score); - EXPECT_GT(title_score, title_mid_word_score); - // Check that title matches fit somewhere reasonable compared to the - // various types of URL matches. - EXPECT_GT(title_score, arg_score); - EXPECT_GT(arg_score, title_mid_word_score); - // Finally, verify that protocol matches and top level domain name - // matches (.com, .net, etc.) score worse than some of the mid-word - // matches that actually count. - EXPECT_GT(hostname_mid_word_score, protocol_score); - EXPECT_GT(hostname_mid_word_score, protocol_mid_word_score); - EXPECT_GT(hostname_mid_word_score, tld_score); - EXPECT_GT(hostname_mid_word_score, tld_mid_word_score); -} - -// Test the function GetFinalRelevancyScore(). -TEST_F(ScoredHistoryMatchTest, GetFinalRelevancyScore) { - // hqp_relevance_buckets = "0.0:100,1.0:200,4.0:500,8.0:900,10.0:1000"; - std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets; - hqp_buckets.push_back(std::make_pair(0.0, 100)); - hqp_buckets.push_back(std::make_pair(1.0, 200)); - hqp_buckets.push_back(std::make_pair(4.0, 500)); - hqp_buckets.push_back(std::make_pair(8.0, 900)); - hqp_buckets.push_back(std::make_pair(10.0, 1000)); - // Check when topicality score is zero. - float topicality_score = 0.0; - float frequency_score = 10.0; - // intermediate_score = 0.0 * 10.0 = 0.0. - EXPECT_EQ(0, ScoredHistoryMatch::GetFinalRelevancyScore( - topicality_score, frequency_score, hqp_buckets)); - - // Check when intermediate score falls at the border range. - topicality_score = 0.4f; - frequency_score = 10.0f; - // intermediate_score = 0.5 * 10.0 = 4.0. - EXPECT_EQ(500, ScoredHistoryMatch::GetFinalRelevancyScore( - topicality_score, frequency_score, hqp_buckets)); - - // Checking the score that falls into one of the buckets. - topicality_score = 0.5f; - frequency_score = 10.0f; - // intermediate_score = 0.5 * 10.0 = 5.0. - EXPECT_EQ(600, // 500 + (((900 - 500)/(8 -4)) * 1) = 600. - ScoredHistoryMatch::GetFinalRelevancyScore( - topicality_score, frequency_score, hqp_buckets)); - - // Never give the score greater than maximum specified. - topicality_score = 0.5f; - frequency_score = 22.0f; - // intermediate_score = 0.5 * 22.0 = 11.0 - EXPECT_EQ(1000, ScoredHistoryMatch::GetFinalRelevancyScore( - topicality_score, frequency_score, hqp_buckets)); -} - -// Test the function GetHQPBucketsFromString(). -TEST_F(ScoredHistoryMatchTest, GetHQPBucketsFromString) { - std::string buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399"; - std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets; - - EXPECT_TRUE( - ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str, &hqp_buckets)); - EXPECT_THAT(hqp_buckets, ElementsAre(Pair(0.0, 400), Pair(1.5, 600), - Pair(12.0, 1300), Pair(20.0, 1399))); - // invalid string. - buckets_str = "0.0,400,1.5,600"; - EXPECT_FALSE( - ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str, &hqp_buckets)); -} diff --git a/chrome/browser/autocomplete/search_provider_unittest.cc b/chrome/browser/autocomplete/search_provider_unittest.cc index e591c7a3..8bdb943 100644 --- a/chrome/browser/autocomplete/search_provider_unittest.cc +++ b/chrome/browser/autocomplete/search_provider_unittest.cc @@ -20,7 +20,6 @@ #include "chrome/browser/autocomplete/autocomplete_controller.h" #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" -#include "chrome/browser/autocomplete/history_url_provider.h" #include "chrome/browser/history/history_service_factory.h" #include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/signin/account_tracker_service_factory.h" @@ -38,6 +37,7 @@ #include "components/omnibox/autocomplete_match.h" #include "components/omnibox/autocomplete_provider.h" #include "components/omnibox/autocomplete_provider_listener.h" +#include "components/omnibox/history_url_provider.h" #include "components/omnibox/omnibox_field_trial.h" #include "components/omnibox/omnibox_switches.h" #include "components/omnibox/suggestion_answer.h" diff --git a/chrome/browser/autocomplete/url_index_private_data.h b/chrome/browser/autocomplete/url_index_private_data.h index c6f4761..df4bee2 100644 --- a/chrome/browser/autocomplete/url_index_private_data.h +++ b/chrome/browser/autocomplete/url_index_private_data.h @@ -12,9 +12,9 @@ #include "base/gtest_prod_util.h" #include "base/memory/ref_counted.h" #include "chrome/browser/autocomplete/in_memory_url_index_cache.pb.h" -#include "chrome/browser/autocomplete/scored_history_match.h" #include "components/history/core/browser/history_service.h" #include "components/omnibox/in_memory_url_index_types.h" +#include "components/omnibox/scored_history_match.h" class HistoryQuickProviderTest; diff --git a/chrome/browser/autocomplete/zero_suggest_provider.cc b/chrome/browser/autocomplete/zero_suggest_provider.cc index 4bc9c36..987219d8 100644 --- a/chrome/browser/autocomplete/zero_suggest_provider.cc +++ b/chrome/browser/autocomplete/zero_suggest_provider.cc @@ -18,7 +18,6 @@ #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" -#include "chrome/browser/autocomplete/history_url_provider.h" #include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search_engines/template_url_service_factory.h" @@ -29,6 +28,7 @@ #include "components/omnibox/autocomplete_input.h" #include "components/omnibox/autocomplete_match.h" #include "components/omnibox/autocomplete_provider_listener.h" +#include "components/omnibox/history_url_provider.h" #include "components/omnibox/omnibox_field_trial.h" #include "components/omnibox/search_provider.h" #include "components/pref_registry/pref_registry_syncable.h" |