[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 8 | #include <cmath> |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 9 | |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 10 | #include "base/auto_reset.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 12 | #include "base/i18n/break_iterator.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 13 | #include "base/i18n/case_conversion.h" |
[email protected] | d6e58c6e | 2009-10-10 20:40:50 | [diff] [blame] | 14 | #include "base/i18n/icu_string_conversions.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 15 | #include "base/json/json_string_value_serializer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "base/message_loop.h" |
[email protected] | f5b95ba9 | 2012-03-27 14:05:19 | [diff] [blame] | 17 | #include "base/metrics/histogram.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 18 | #include "base/prefs/pref_service.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 19 | #include "base/strings/string16.h" |
| 20 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 22 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 23 | #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
[email protected] | 9ac4009 | 2010-10-27 23:05:26 | [diff] [blame] | 24 | #include "chrome/browser/autocomplete/autocomplete_match.h" |
[email protected] | 5af9bc8 | 2012-06-29 00:53:48 | [diff] [blame] | 25 | #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
[email protected] | 73c2b163 | 2012-07-02 22:51:38 | [diff] [blame] | 26 | #include "chrome/browser/autocomplete/autocomplete_result.h" |
[email protected] | 2c812ba0 | 2011-07-14 00:23:15 | [diff] [blame] | 27 | #include "chrome/browser/autocomplete/keyword_provider.h" |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 28 | #include "chrome/browser/autocomplete/url_prefix.h" |
[email protected] | 6a2c09f | 2013-01-25 04:50:07 | [diff] [blame] | 29 | #include "chrome/browser/history/history_service.h" |
[email protected] | 9d2db76 | 2012-06-19 00:01:10 | [diff] [blame] | 30 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 31 | #include "chrome/browser/history/in_memory_database.h" |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 32 | #include "chrome/browser/metrics/variations/variations_http_header_provider.h" |
[email protected] | f870a32 | 2009-01-16 21:47:27 | [diff] [blame] | 33 | #include "chrome/browser/net/url_fixer_upper.h" |
[email protected] | 684fcc1 | 2013-03-08 22:50:02 | [diff] [blame] | 34 | #include "chrome/browser/omnibox/omnibox_field_trial.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 35 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 36 | #include "chrome/browser/search/search.h" |
[email protected] | a0ad93ea | 2012-05-07 22:11:53 | [diff] [blame] | 37 | #include "chrome/browser/search_engines/search_engine_type.h" |
[email protected] | 9899a61 | 2012-08-21 23:50:04 | [diff] [blame] | 38 | #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
[email protected] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 39 | #include "chrome/browser/search_engines/template_url_service.h" |
| 40 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | #include "chrome/common/pref_names.h" |
[email protected] | dcf7d35 | 2009-02-26 01:56:02 | [diff] [blame] | 42 | #include "chrome/common/url_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | #include "googleurl/src/url_util.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 44 | #include "grit/generated_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | #include "net/base/escape.h" |
[email protected] | d3cf8682f0 | 2012-02-29 23:29:34 | [diff] [blame] | 46 | #include "net/base/load_flags.h" |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 47 | #include "net/base/net_util.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 48 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 49 | #include "net/http/http_response_headers.h" |
[email protected] | 3dc1bc4 | 2012-06-19 08:20:53 | [diff] [blame] | 50 | #include "net/url_request/url_fetcher.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 51 | #include "net/url_request/url_request_status.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 52 | #include "ui/base/l10n/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 54 | using base::Time; |
| 55 | using base::TimeDelta; |
| 56 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 57 | namespace { |
| 58 | |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 59 | // We keep track in a histogram how many suggest requests we send, how |
| 60 | // many suggest requests we invalidate (e.g., due to a user typing |
| 61 | // another character), and how many replies we receive. |
| 62 | // *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! *** |
| 63 | // (excluding the end-of-list enum value) |
| 64 | // We do not want values of existing enums to change or else it screws |
| 65 | // up the statistics. |
| 66 | enum SuggestRequestsHistogramValue { |
| 67 | REQUEST_SENT = 1, |
| 68 | REQUEST_INVALIDATED, |
| 69 | REPLY_RECEIVED, |
| 70 | MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE |
| 71 | }; |
| 72 | |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 73 | // The verbatim score for an input which is not an URL. |
| 74 | const int kNonURLVerbatimRelevance = 1300; |
| 75 | |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 76 | // Increments the appropriate value in the histogram by one. |
| 77 | void LogOmniboxSuggestRequest( |
| 78 | SuggestRequestsHistogramValue request_value) { |
| 79 | UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value, |
| 80 | MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE); |
| 81 | } |
| 82 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 83 | bool HasMultipleWords(const string16& text) { |
| 84 | base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD); |
| 85 | bool found_word = false; |
| 86 | if (i.Init()) { |
| 87 | while (i.Advance()) { |
| 88 | if (i.IsWord()) { |
| 89 | if (found_word) |
| 90 | return true; |
| 91 | found_word = true; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 98 | } // namespace |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 99 | |
[email protected] | 033f342 | 2012-03-13 21:24:18 | [diff] [blame] | 100 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 101 | // SearchProvider::Providers -------------------------------------------------- |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 102 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 103 | SearchProvider::Providers::Providers(TemplateURLService* template_url_service) |
| 104 | : template_url_service_(template_url_service) { |
| 105 | } |
| 106 | |
| 107 | const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const { |
| 108 | return default_provider_.empty() ? NULL : |
| 109 | template_url_service_->GetTemplateURLForKeyword(default_provider_); |
| 110 | } |
| 111 | |
| 112 | const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const { |
| 113 | return keyword_provider_.empty() ? NULL : |
| 114 | template_url_service_->GetTemplateURLForKeyword(keyword_provider_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 117 | |
| 118 | // SearchProvider ------------------------------------------------------------- |
| 119 | |
| 120 | // static |
| 121 | const int SearchProvider::kDefaultProviderURLFetcherID = 1; |
| 122 | // static |
| 123 | const int SearchProvider::kKeywordProviderURLFetcherID = 2; |
[email protected] | 24dbf230 | 2013-04-05 16:59:25 | [diff] [blame] | 124 | // static |
| 125 | int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 126 | |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 127 | // static |
| 128 | AutocompleteMatch SearchProvider::CreateSearchSuggestion( |
| 129 | Profile* profile, |
| 130 | AutocompleteProvider* autocomplete_provider, |
| 131 | const AutocompleteInput& input, |
| 132 | const string16& query_string, |
| 133 | const string16& input_text, |
| 134 | int relevance, |
| 135 | AutocompleteMatch::Type type, |
| 136 | int accepted_suggestion, |
| 137 | bool is_keyword, |
[email protected] | f3e46eec | 2013-06-11 14:46:28 | [diff] [blame] | 138 | const string16& keyword, |
| 139 | int omnibox_start_margin) { |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 140 | AutocompleteMatch match(autocomplete_provider, relevance, false, type); |
| 141 | |
| 142 | // Bail out now if we don't actually have a valid provider. |
| 143 | match.keyword = keyword; |
| 144 | const TemplateURL* provider_url = match.GetTemplateURL(profile, false); |
| 145 | if (provider_url == NULL) |
| 146 | return match; |
| 147 | |
| 148 | match.contents.assign(query_string); |
| 149 | // We do intra-string highlighting for suggestions - the suggested segment |
| 150 | // will be highlighted, e.g. for input_text = "you" the suggestion may be |
| 151 | // "youtube", so we'll bold the "tube" section: you*tube*. |
| 152 | if (input_text != query_string) { |
| 153 | size_t input_position = match.contents.find(input_text); |
| 154 | if (input_position == string16::npos) { |
| 155 | // The input text is not a substring of the query string, e.g. input |
| 156 | // text is "slasdot" and the query string is "slashdot", so we bold the |
| 157 | // whole thing. |
| 158 | match.contents_class.push_back( |
| 159 | ACMatchClassification(0, ACMatchClassification::MATCH)); |
| 160 | } else { |
| 161 | // TODO(beng): ACMatchClassification::MATCH now seems to just mean |
| 162 | // "bold" this. Consider modifying the terminology. |
| 163 | // We don't iterate over the string here annotating all matches because |
| 164 | // it looks odd to have every occurrence of a substring that may be as |
| 165 | // short as a single character highlighted in a query suggestion result, |
| 166 | // e.g. for input text "s" and query string "southwest airlines", it |
| 167 | // looks odd if both the first and last s are highlighted. |
| 168 | if (input_position != 0) { |
| 169 | match.contents_class.push_back( |
[email protected] | 4b6d916e | 2013-06-07 05:02:08 | [diff] [blame] | 170 | ACMatchClassification(0, ACMatchClassification::MATCH)); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 171 | } |
| 172 | match.contents_class.push_back( |
[email protected] | 4b6d916e | 2013-06-07 05:02:08 | [diff] [blame] | 173 | ACMatchClassification(input_position, ACMatchClassification::NONE)); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 174 | size_t next_fragment_position = input_position + input_text.length(); |
| 175 | if (next_fragment_position < query_string.length()) { |
| 176 | match.contents_class.push_back( |
| 177 | ACMatchClassification(next_fragment_position, |
[email protected] | 4b6d916e | 2013-06-07 05:02:08 | [diff] [blame] | 178 | ACMatchClassification::MATCH)); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } else { |
| 182 | // Otherwise, we're dealing with the "default search" result which has no |
| 183 | // completion. |
| 184 | match.contents_class.push_back( |
| 185 | ACMatchClassification(0, ACMatchClassification::NONE)); |
| 186 | } |
| 187 | |
| 188 | // When the user forced a query, we need to make sure all the fill_into_edit |
| 189 | // values preserve that property. Otherwise, if the user starts editing a |
| 190 | // suggestion, non-Search results will suddenly appear. |
| 191 | if (input.type() == AutocompleteInput::FORCED_QUERY) |
| 192 | match.fill_into_edit.assign(ASCIIToUTF16("?")); |
| 193 | if (is_keyword) |
| 194 | match.fill_into_edit.append(match.keyword + char16(' ')); |
| 195 | if (!input.prevent_inline_autocomplete() && |
| 196 | StartsWith(query_string, input_text, false)) { |
| 197 | match.inline_autocomplete_offset = |
| 198 | match.fill_into_edit.length() + input_text.length(); |
| 199 | } |
| 200 | match.fill_into_edit.append(query_string); |
| 201 | |
| 202 | const TemplateURLRef& search_url = provider_url->url_ref(); |
| 203 | DCHECK(search_url.SupportsReplacement()); |
| 204 | match.search_terms_args.reset( |
| 205 | new TemplateURLRef::SearchTermsArgs(query_string)); |
| 206 | match.search_terms_args->original_query = input_text; |
| 207 | match.search_terms_args->accepted_suggestion = accepted_suggestion; |
[email protected] | f3e46eec | 2013-06-11 14:46:28 | [diff] [blame] | 208 | match.search_terms_args->omnibox_start_margin = omnibox_start_margin; |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 209 | // This is the destination URL sans assisted query stats. This must be set |
| 210 | // so the AutocompleteController can properly de-dupe; the controller will |
| 211 | // eventually overwrite it before it reaches the user. |
| 212 | match.destination_url = |
| 213 | GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get())); |
| 214 | |
| 215 | // Search results don't look like URLs. |
| 216 | match.transition = is_keyword ? |
| 217 | content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED; |
| 218 | |
| 219 | return match; |
| 220 | } |
| 221 | |
[email protected] | 30f5bc9 | 2012-06-26 04:14:55 | [diff] [blame] | 222 | SearchProvider::SearchProvider(AutocompleteProviderListener* listener, |
| 223 | Profile* profile) |
[email protected] | 35f1f4f0 | 2012-09-11 13:17:00 | [diff] [blame] | 224 | : AutocompleteProvider(listener, profile, |
| 225 | AutocompleteProvider::TYPE_SEARCH), |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 226 | providers_(TemplateURLServiceFactory::GetForProfile(profile)), |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 227 | suggest_results_pending_(0), |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 228 | instant_finalized_(false), |
| 229 | field_trial_triggered_(false), |
[email protected] | 9a09139 | 2013-05-04 14:27:24 | [diff] [blame] | 230 | field_trial_triggered_in_session_(false), |
[email protected] | f3e46eec | 2013-06-11 14:46:28 | [diff] [blame] | 231 | suppress_search_suggestions_(false), |
| 232 | omnibox_start_margin_(-1) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 235 | void SearchProvider::FinalizeInstantQuery(const string16& input_text, |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 236 | const InstantSuggestion& suggestion) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 237 | if (done_ || instant_finalized_) |
| 238 | return; |
| 239 | |
| 240 | instant_finalized_ = true; |
| 241 | UpdateDone(); |
| 242 | |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 243 | if (input_text.empty()) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 244 | // We only need to update the listener if we're actually done. |
| 245 | if (done_) |
| 246 | listener_->OnProviderUpdate(false); |
| 247 | return; |
| 248 | } |
| 249 | |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 250 | default_provider_suggestion_ = suggestion; |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 251 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 252 | string16 adjusted_input_text(input_text); |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 253 | AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(), |
| 254 | &adjusted_input_text); |
| 255 | |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 256 | const string16 text = adjusted_input_text + suggestion.text; |
[email protected] | 9acdcdc0 | 2012-05-21 20:58:36 | [diff] [blame] | 257 | bool results_updated = false; |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 258 | // Remove any matches that are identical to |text|. We don't use the |
| 259 | // destination_url for comparison as it varies depending upon the index passed |
| 260 | // to TemplateURL::ReplaceSearchTerms. |
| 261 | for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) { |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 262 | if (((i->type == AutocompleteMatchType::SEARCH_HISTORY) || |
| 263 | (i->type == AutocompleteMatchType::SEARCH_SUGGEST)) && |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 264 | (i->fill_into_edit == text)) { |
[email protected] | e030de6 | 2010-11-24 05:41:19 | [diff] [blame] | 265 | i = matches_.erase(i); |
[email protected] | 9acdcdc0 | 2012-05-21 20:58:36 | [diff] [blame] | 266 | results_updated = true; |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 267 | } else { |
| 268 | ++i; |
| 269 | } |
| 270 | } |
| 271 | |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 272 | // Add the new Instant suggest result. |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 273 | if (suggestion.type == INSTANT_SUGGESTION_SEARCH) { |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 274 | // Instant has a query suggestion. Rank it higher than SEARCH_WHAT_YOU_TYPED |
| 275 | // so that it gets autocompleted. |
| 276 | const int verbatim_relevance = GetVerbatimRelevance(); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 277 | int did_not_accept_default_suggestion = |
| 278 | default_results_.suggest_results.empty() ? |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 279 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 280 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 281 | MatchMap match_map; |
| 282 | AddMatchToMap(text, adjusted_input_text, verbatim_relevance + 1, |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 283 | AutocompleteMatchType::SEARCH_SUGGEST, |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 284 | did_not_accept_default_suggestion, false, &match_map); |
| 285 | if (!match_map.empty()) { |
| 286 | matches_.push_back(match_map.begin()->second); |
| 287 | results_updated = true; |
| 288 | } |
| 289 | } else { |
[email protected] | c112f0fe7 | 2013-01-18 20:30:09 | [diff] [blame] | 290 | // Instant has a URL suggestion. Rank it higher than URL_WHAT_YOU_TYPED so |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 291 | // it gets autocompleted; use kNonURLVerbatimRelevance rather than |
| 292 | // verbatim_relevance so that the score does not change if the user keeps |
| 293 | // typing and the input changes from type UNKNOWN to URL. |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 294 | matches_.push_back(NavigationToMatch( |
[email protected] | a1b5e86 | 2013-03-29 06:38:44 | [diff] [blame] | 295 | NavigationResult(*this, |
| 296 | GURL(UTF16ToUTF8(suggestion.text)), |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 297 | string16(), |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 298 | false, |
| 299 | kNonURLVerbatimRelevance + 1))); |
[email protected] | 9acdcdc0 | 2012-05-21 20:58:36 | [diff] [blame] | 300 | results_updated = true; |
| 301 | } |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 302 | |
[email protected] | 9acdcdc0 | 2012-05-21 20:58:36 | [diff] [blame] | 303 | if (results_updated || done_) |
| 304 | listener_->OnProviderUpdate(results_updated); |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 305 | } |
| 306 | |
[email protected] | 0a633ba9 | 2013-03-26 09:13:47 | [diff] [blame] | 307 | void SearchProvider::ClearInstantSuggestion() { |
| 308 | default_provider_suggestion_ = InstantSuggestion(); |
| 309 | if (done_ || instant_finalized_) |
| 310 | return; |
| 311 | instant_finalized_ = true; |
| 312 | UpdateMatches(); |
| 313 | listener_->OnProviderUpdate(true); |
| 314 | } |
| 315 | |
[email protected] | 9a09139 | 2013-05-04 14:27:24 | [diff] [blame] | 316 | void SearchProvider::SuppressSearchSuggestions() { |
| 317 | suppress_search_suggestions_ = true; |
| 318 | } |
| 319 | |
[email protected] | f3e46eec | 2013-06-11 14:46:28 | [diff] [blame] | 320 | void SearchProvider::SetOmniboxStartMargin(int omnibox_start_margin) { |
| 321 | omnibox_start_margin_ = omnibox_start_margin; |
| 322 | } |
| 323 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 324 | void SearchProvider::Start(const AutocompleteInput& input, |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 325 | bool minimal_changes) { |
[email protected] | 9a09139 | 2013-05-04 14:27:24 | [diff] [blame] | 326 | const bool suppress_search_suggestions = suppress_search_suggestions_; |
| 327 | suppress_search_suggestions_ = false; |
| 328 | |
[email protected] | 04504c24 | 2013-01-22 21:08:55 | [diff] [blame] | 329 | // Do our best to load the model as early as possible. This will reduce |
| 330 | // odds of having the model not ready when really needed (a non-empty input). |
| 331 | TemplateURLService* model = providers_.template_url_service(); |
| 332 | DCHECK(model); |
| 333 | model->Load(); |
| 334 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 335 | matches_.clear(); |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 336 | field_trial_triggered_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 337 | |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 338 | instant_finalized_ = |
| 339 | (input.matches_requested() != AutocompleteInput::ALL_MATCHES); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 340 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 341 | // Can't return search/suggest results for bogus input or without a profile. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 342 | if (!profile_ || (input.type() == AutocompleteInput::INVALID)) { |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 343 | Stop(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 347 | keyword_input_ = input; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 348 | const TemplateURL* keyword_provider = |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 349 | KeywordProvider::GetSubstitutingTemplateURLForInput(model, |
| 350 | &keyword_input_); |
| 351 | if (keyword_provider == NULL) |
| 352 | keyword_input_.Clear(); |
| 353 | else if (keyword_input_.text().empty()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 354 | keyword_provider = NULL; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 355 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 356 | const TemplateURL* default_provider = model->GetDefaultSearchProvider(); |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 357 | if (default_provider && !default_provider->SupportsReplacement()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 358 | default_provider = NULL; |
| 359 | |
| 360 | if (keyword_provider == default_provider) |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame] | 361 | default_provider = NULL; // No use in querying the same provider twice. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 362 | |
| 363 | if (!default_provider && !keyword_provider) { |
| 364 | // No valid providers. |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 365 | Stop(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
| 369 | // If we're still running an old query but have since changed the query text |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 370 | // or the providers, abort the query. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 371 | string16 default_provider_keyword(default_provider ? |
| 372 | default_provider->keyword() : string16()); |
| 373 | string16 keyword_provider_keyword(keyword_provider ? |
| 374 | keyword_provider->keyword() : string16()); |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 375 | if (!minimal_changes || |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 376 | !providers_.equal(default_provider_keyword, keyword_provider_keyword)) { |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 377 | // If Instant has not come back with a suggestion, adjust the previous |
| 378 | // suggestion if possible. If |instant_finalized| is true, we are looking |
| 379 | // for synchronous matches only, so the suggestion is cleared. |
| 380 | if (instant_finalized_) |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 381 | default_provider_suggestion_ = InstantSuggestion(); |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 382 | else |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 383 | AdjustDefaultProviderSuggestion(input_.text(), input.text()); |
| 384 | |
| 385 | // Cancel any in-flight suggest requests. |
| 386 | if (!done_) { |
| 387 | // The Stop(false) call below clears |default_provider_suggestion_|, but |
| 388 | // in this instance we do not want to clear cached results, so we |
| 389 | // restore it. |
| 390 | base::AutoReset<InstantSuggestion> reset(&default_provider_suggestion_, |
| 391 | InstantSuggestion()); |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 392 | Stop(false); |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 393 | } |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 394 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 396 | providers_.set(default_provider_keyword, keyword_provider_keyword); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | |
| 398 | if (input.text().empty()) { |
| 399 | // User typed "?" alone. Give them a placeholder result indicating what |
| 400 | // this syntax does. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 401 | if (default_provider) { |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 402 | AutocompleteMatch match; |
| 403 | match.provider = this; |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 404 | match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 405 | match.contents_class.push_back( |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 406 | ACMatchClassification(0, ACMatchClassification::NONE)); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 407 | match.keyword = providers_.default_provider(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 408 | matches_.push_back(match); |
| 409 | } |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 410 | Stop(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | |
| 414 | input_ = input; |
| 415 | |
[email protected] | 9a09139 | 2013-05-04 14:27:24 | [diff] [blame] | 416 | if (!suppress_search_suggestions) { |
[email protected] | 5cd06c3e | 2012-09-20 03:25:20 | [diff] [blame] | 417 | DoHistoryQuery(minimal_changes); |
| 418 | StartOrStopSuggestQuery(minimal_changes); |
| 419 | } |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 420 | UpdateMatches(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 423 | SearchProvider::Result::Result(bool from_keyword_provider, int relevance) |
| 424 | : from_keyword_provider_(from_keyword_provider), |
| 425 | relevance_(relevance) { |
| 426 | } |
| 427 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 428 | SearchProvider::Result::~Result() {} |
| 429 | |
| 430 | SearchProvider::SuggestResult::SuggestResult(const string16& suggestion, |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 431 | bool from_keyword_provider, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 432 | int relevance) |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 433 | : Result(from_keyword_provider, relevance), |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 434 | suggestion_(suggestion) { |
| 435 | } |
| 436 | |
| 437 | SearchProvider::SuggestResult::~SuggestResult() {} |
| 438 | |
[email protected] | a1b5e86 | 2013-03-29 06:38:44 | [diff] [blame] | 439 | bool SearchProvider::SuggestResult::IsInlineable(const string16& input) const { |
| 440 | return StartsWith(suggestion_, input, false); |
| 441 | } |
| 442 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 443 | int SearchProvider::SuggestResult::CalculateRelevance( |
| 444 | const AutocompleteInput& input, |
| 445 | bool keyword_provider_requested) const { |
| 446 | if (!from_keyword_provider_ && keyword_provider_requested) |
| 447 | return 100; |
| 448 | return ((input.type() == AutocompleteInput::URL) ? 300 : 600); |
| 449 | } |
| 450 | |
| 451 | SearchProvider::NavigationResult::NavigationResult( |
[email protected] | a1b5e86 | 2013-03-29 06:38:44 | [diff] [blame] | 452 | const AutocompleteProvider& provider, |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 453 | const GURL& url, |
| 454 | const string16& description, |
| 455 | bool from_keyword_provider, |
| 456 | int relevance) |
| 457 | : Result(from_keyword_provider, relevance), |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 458 | url_(url), |
[email protected] | a1b5e86 | 2013-03-29 06:38:44 | [diff] [blame] | 459 | formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 460 | url, provider.StringForURLDisplay(url, true, false))), |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 461 | description_(description) { |
| 462 | DCHECK(url_.is_valid()); |
| 463 | } |
| 464 | |
[email protected] | a1b5e86 | 2013-03-29 06:38:44 | [diff] [blame] | 465 | SearchProvider::NavigationResult::~NavigationResult() {} |
| 466 | |
| 467 | bool SearchProvider::NavigationResult::IsInlineable( |
| 468 | const string16& input) const { |
| 469 | return URLPrefix::BestURLPrefix(formatted_url_, input) != NULL; |
| 470 | } |
| 471 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 472 | int SearchProvider::NavigationResult::CalculateRelevance( |
| 473 | const AutocompleteInput& input, |
| 474 | bool keyword_provider_requested) const { |
| 475 | return (from_keyword_provider_ || !keyword_provider_requested) ? 800 : 150; |
| 476 | } |
| 477 | |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 478 | SearchProvider::Results::Results() |
| 479 | : has_suggested_relevance(false), |
| 480 | verbatim_relevance(-1) { |
| 481 | } |
| 482 | |
| 483 | SearchProvider::Results::~Results() { |
| 484 | } |
| 485 | |
| 486 | void SearchProvider::Results::Clear() { |
| 487 | suggest_results.clear(); |
| 488 | navigation_results.clear(); |
| 489 | has_suggested_relevance = false; |
| 490 | verbatim_relevance = -1; |
| 491 | } |
| 492 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 493 | class SearchProvider::CompareScoredResults { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 494 | public: |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 495 | bool operator()(const Result& a, const Result& b) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 496 | // Sort in descending relevance order. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 497 | return a.relevance() > b.relevance(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 498 | } |
| 499 | }; |
| 500 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 501 | void SearchProvider::Run() { |
| 502 | // Start a new request with the current input. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 503 | suggest_results_pending_ = 0; |
[email protected] | a0ad93ea | 2012-05-07 22:11:53 | [diff] [blame] | 504 | time_suggest_request_sent_ = base::TimeTicks::Now(); |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 505 | |
| 506 | default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 507 | providers_.GetDefaultProviderURL(), input_)); |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 508 | keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 509 | providers_.GetKeywordProviderURL(), keyword_input_)); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 510 | |
| 511 | // Both the above can fail if the providers have been modified or deleted |
| 512 | // since the query began. |
| 513 | if (suggest_results_pending_ == 0) { |
| 514 | UpdateDone(); |
| 515 | // We only need to update the listener if we're actually done. |
| 516 | if (done_) |
| 517 | listener_->OnProviderUpdate(false); |
| 518 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 519 | } |
| 520 | |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 521 | void SearchProvider::Stop(bool clear_cached_results) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 522 | StopSuggest(); |
| 523 | done_ = true; |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 524 | default_provider_suggestion_ = InstantSuggestion(); |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 525 | |
| 526 | if (clear_cached_results) |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 527 | ClearAllResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 528 | } |
| 529 | |
[email protected] | 0e9e878 | 2012-05-15 23:01:51 | [diff] [blame] | 530 | void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 531 | provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); |
| 532 | metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); |
| 533 | new_entry.set_provider(AsOmniboxEventProviderType()); |
| 534 | new_entry.set_provider_done(done_); |
[email protected] | fea6d30 | 2013-04-13 02:04:06 | [diff] [blame] | 535 | std::vector<uint32> field_trial_hashes; |
| 536 | OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes); |
| 537 | for (size_t i = 0; i < field_trial_hashes.size(); ++i) { |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 538 | if (field_trial_triggered_) |
[email protected] | fea6d30 | 2013-04-13 02:04:06 | [diff] [blame] | 539 | new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); |
[email protected] | d78d736 | 2012-12-19 18:43:28 | [diff] [blame] | 540 | if (field_trial_triggered_in_session_) { |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 541 | new_entry.mutable_field_trial_triggered_in_session()->Add( |
[email protected] | fea6d30 | 2013-04-13 02:04:06 | [diff] [blame] | 542 | field_trial_hashes[i]); |
[email protected] | d78d736 | 2012-12-19 18:43:28 | [diff] [blame] | 543 | } |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | void SearchProvider::ResetSession() { |
| 548 | field_trial_triggered_in_session_ = false; |
[email protected] | 0e9e878 | 2012-05-15 23:01:51 | [diff] [blame] | 549 | } |
| 550 | |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 551 | void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | DCHECK(!done_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 553 | suggest_results_pending_--; |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 554 | LogOmniboxSuggestRequest(REPLY_RECEIVED); |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 555 | DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 556 | const net::HttpResponseHeaders* const response_headers = |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 557 | source->GetResponseHeaders(); |
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 558 | std::string json_data; |
| 559 | source->GetResponseAsString(&json_data); |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 560 | // JSON is supposed to be UTF-8, but some suggest service providers send JSON |
| 561 | // files in non-UTF-8 encodings. The actual encoding is usually specified in |
| 562 | // the Content-Type header field. |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 563 | if (response_headers) { |
| 564 | std::string charset; |
| 565 | if (response_headers->GetCharset(&charset)) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 566 | string16 data_16; |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 567 | // TODO(jungshik): Switch to CodePageToUTF8 after it's added. |
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 568 | if (base::CodepageToUTF16(json_data, charset.c_str(), |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 569 | base::OnStringConversionError::FAIL, |
| 570 | &data_16)) |
| 571 | json_data = UTF16ToUTF8(data_16); |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 575 | const bool is_keyword = (source == keyword_fetcher_.get()); |
[email protected] | f2916ea | 2013-01-26 02:20:14 | [diff] [blame] | 576 | // Ensure the request succeeded and that the provider used is still available. |
| 577 | // A verbatim match cannot be generated without this provider, causing errors. |
[email protected] | 013e9a0 | 2012-05-18 20:27:10 | [diff] [blame] | 578 | const bool request_succeeded = |
[email protected] | f2916ea | 2013-01-26 02:20:14 | [diff] [blame] | 579 | source->GetStatus().is_success() && source->GetResponseCode() == 200 && |
| 580 | ((is_keyword && providers_.GetKeywordProviderURL()) || |
| 581 | (!is_keyword && providers_.GetDefaultProviderURL())); |
[email protected] | a0ad93ea | 2012-05-07 22:11:53 | [diff] [blame] | 582 | |
| 583 | // Record response time for suggest requests sent to Google. We care |
| 584 | // only about the common case: the Google default provider used in |
| 585 | // non-keyword mode. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 586 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 587 | if (!is_keyword && default_url && |
[email protected] | 9899a61 | 2012-08-21 23:50:04 | [diff] [blame] | 588 | (TemplateURLPrepopulateData::GetEngineType(default_url->url()) == |
| 589 | SEARCH_ENGINE_GOOGLE)) { |
[email protected] | 6dc950f | 2012-07-16 19:49:08 | [diff] [blame] | 590 | const TimeDelta elapsed_time = |
[email protected] | 013e9a0 | 2012-05-18 20:27:10 | [diff] [blame] | 591 | base::TimeTicks::Now() - time_suggest_request_sent_; |
| 592 | if (request_succeeded) { |
| 593 | UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", |
| 594 | elapsed_time); |
| 595 | } else { |
| 596 | UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", |
| 597 | elapsed_time); |
| 598 | } |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 599 | } |
| 600 | |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 601 | bool results_updated = false; |
| 602 | if (request_succeeded) { |
| 603 | JSONStringValueSerializer deserializer(json_data); |
| 604 | deserializer.set_allow_trailing_comma(true); |
| 605 | scoped_ptr<Value> data(deserializer.Deserialize(NULL, NULL)); |
| 606 | results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword); |
| 607 | } |
| 608 | |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 609 | UpdateMatches(); |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 610 | if (done_ || results_updated) |
| 611 | listener_->OnProviderUpdate(results_updated); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 612 | } |
| 613 | |
[email protected] | abe441e | 2013-05-06 12:35:05 | [diff] [blame] | 614 | bool SearchProvider::IsNonInstantSearchDone() const { |
| 615 | return !timer_.IsRunning() && (suggest_results_pending_ == 0); |
| 616 | } |
| 617 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 618 | SearchProvider::~SearchProvider() { |
| 619 | } |
| 620 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 621 | void SearchProvider::DoHistoryQuery(bool minimal_changes) { |
| 622 | // The history query results are synchronous, so if minimal_changes is true, |
| 623 | // we still have the last results and don't need to do anything. |
| 624 | if (minimal_changes) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 625 | return; |
| 626 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 627 | keyword_history_results_.clear(); |
| 628 | default_history_results_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 629 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 630 | HistoryService* const history_service = |
[email protected] | 9d2db76 | 2012-06-19 00:01:10 | [diff] [blame] | 631 | HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 632 | history::URLDatabase* url_db = history_service ? |
| 633 | history_service->InMemoryDatabase() : NULL; |
| 634 | if (!url_db) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 635 | return; |
| 636 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 637 | // Request history for both the keyword and default provider. We grab many |
| 638 | // more matches than we'll ultimately clamp to so that if there are several |
| 639 | // recent multi-word matches who scores are lowered (see |
| 640 | // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring |
| 641 | // matches. Note that this doesn't fix the problem entirely, but merely |
| 642 | // limits it to cases with a very large number of such multi-word matches; for |
| 643 | // now, this seems OK compared with the complexity of a real fix, which would |
| 644 | // require multiple searches and tracking of "single- vs. multi-word" in the |
| 645 | // database. |
| 646 | int num_matches = kMaxMatches * 5; |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 647 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 648 | if (default_url) { |
| 649 | url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(), |
| 650 | num_matches, &default_history_results_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 651 | } |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 652 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 653 | if (keyword_url) { |
| 654 | url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(), |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 655 | keyword_input_.text(), num_matches, &keyword_history_results_); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 656 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 657 | } |
| 658 | |
[email protected] | 6dc950f | 2012-07-16 19:49:08 | [diff] [blame] | 659 | void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 660 | if (!IsQuerySuitableForSuggest()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 661 | StopSuggest(); |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 662 | ClearAllResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 663 | return; |
| 664 | } |
| 665 | |
| 666 | // For the minimal_changes case, if we finished the previous query and still |
| 667 | // have its results, or are allowed to keep running it, just do that, rather |
| 668 | // than starting a new query. |
| 669 | if (minimal_changes && |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 670 | (!default_results_.suggest_results.empty() || |
| 671 | !default_results_.navigation_results.empty() || |
| 672 | !keyword_results_.suggest_results.empty() || |
| 673 | !keyword_results_.navigation_results.empty() || |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 674 | (!done_ && |
| 675 | input_.matches_requested() == AutocompleteInput::ALL_MATCHES))) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 676 | return; |
| 677 | |
| 678 | // We can't keep running any previous query, so halt it. |
| 679 | StopSuggest(); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 680 | |
| 681 | // Remove existing results that cannot inline autocomplete the new input. |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 682 | RemoveAllStaleResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 683 | |
| 684 | // We can't start a new query if we're only allowed synchronous results. |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 685 | if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 686 | return; |
| 687 | |
[email protected] | 24dbf230 | 2013-04-05 16:59:25 | [diff] [blame] | 688 | // To avoid flooding the suggest server, don't send a query until at |
| 689 | // least 100 ms since the last query. |
[email protected] | 515ffa94 | 2012-11-27 20:18:24 | [diff] [blame] | 690 | base::TimeTicks next_suggest_time(time_suggest_request_sent_ + |
| 691 | TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs)); |
| 692 | base::TimeTicks now(base::TimeTicks::Now()); |
| 693 | if (now >= next_suggest_time) { |
| 694 | Run(); |
| 695 | return; |
| 696 | } |
| 697 | timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 698 | } |
| 699 | |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 700 | bool SearchProvider::IsQuerySuitableForSuggest() const { |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 701 | // Don't run Suggest in incognito mode, if the engine doesn't support it, or |
| 702 | // if the user has disabled it. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 703 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 704 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 705 | if (profile_->IsOffTheRecord() || |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 706 | ((!default_url || default_url->suggestions_url().empty()) && |
| 707 | (!keyword_url || keyword_url->suggestions_url().empty())) || |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 708 | !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)) |
| 709 | return false; |
| 710 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 711 | // If the input type might be a URL, we take extra care so that private data |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 712 | // isn't sent to the server. |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 713 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 714 | // FORCED_QUERY means the user is explicitly asking us to search for this, so |
| 715 | // we assume it isn't a URL and/or there isn't private data. |
| 716 | if (input_.type() == AutocompleteInput::FORCED_QUERY) |
| 717 | return true; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 718 | |
[email protected] | f608ea10 | 2013-03-18 15:08:09 | [diff] [blame] | 719 | // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't |
| 720 | // http/https/ftp, we shouldn't send it. Sending things like file: and data: |
| 721 | // is both a waste of time and a disclosure of potentially private, local |
| 722 | // data. Other "schemes" may actually be usernames, and we don't want to send |
| 723 | // passwords. If the scheme is OK, we still need to check other cases below. |
| 724 | // If this is QUERY, then the presence of these schemes means the user |
| 725 | // explicitly typed one, and thus this is probably a URL that's being entered |
| 726 | // and happens to currently be invalid -- in which case we again want to run |
| 727 | // our checks below. Other QUERY cases are less likely to be URLs and thus we |
| 728 | // assume we're OK. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 729 | if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) && |
| 730 | !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) && |
| 731 | !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme)) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 732 | return (input_.type() == AutocompleteInput::QUERY); |
| 733 | |
| 734 | // Don't send URLs with usernames, queries or refs. Some of these are |
| 735 | // private, and the Suggest server is unlikely to have any useful results |
| 736 | // for any of them. Also don't send URLs with ports, as we may initially |
| 737 | // think that a username + password is a host + port (and we don't want to |
| 738 | // send usernames/passwords), and even if the port really is a port, the |
| 739 | // server is once again unlikely to have and useful results. |
| 740 | const url_parse::Parsed& parts = input_.parts(); |
| 741 | if (parts.username.is_nonempty() || parts.port.is_nonempty() || |
| 742 | parts.query.is_nonempty() || parts.ref.is_nonempty()) |
| 743 | return false; |
| 744 | |
| 745 | // Don't send anything for https except the hostname. Hostnames are OK |
| 746 | // because they are visible when the TCP connection is established, but the |
| 747 | // specific path may reveal private information. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 748 | if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) && |
| 749 | parts.path.is_nonempty()) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 750 | return false; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 751 | |
| 752 | return true; |
| 753 | } |
| 754 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 755 | void SearchProvider::StopSuggest() { |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 756 | // Increment the appropriate field in the histogram by the number of |
| 757 | // pending requests that were invalidated. |
| 758 | for (int i = 0; i < suggest_results_pending_; i++) |
| 759 | LogOmniboxSuggestRequest(REQUEST_INVALIDATED); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 760 | suggest_results_pending_ = 0; |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 761 | timer_.Stop(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 762 | // Stop any in-progress URL fetches. |
| 763 | keyword_fetcher_.reset(); |
| 764 | default_fetcher_.reset(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 765 | } |
| 766 | |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 767 | void SearchProvider::ClearAllResults() { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 768 | keyword_results_.Clear(); |
| 769 | default_results_.Clear(); |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 770 | } |
| 771 | |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 772 | void SearchProvider::RemoveAllStaleResults() { |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 773 | // In theory it would be better to run an algorithm like that in |
| 774 | // RemoveStaleResults(...) below that uses all four results lists |
| 775 | // and both verbatim scores at once. However, that will be much |
| 776 | // more complicated for little obvious gain. For code simplicity |
| 777 | // and ease in reasoning about the invariants involved, this code |
| 778 | // removes stales results from the keyword provider and default |
| 779 | // provider independently. |
| 780 | RemoveStaleResults(input_.text(), GetVerbatimRelevance(), |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 781 | &default_results_.suggest_results, |
| 782 | &default_results_.navigation_results); |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 783 | if (!keyword_input_.text().empty()) { |
| 784 | RemoveStaleResults(keyword_input_.text(), GetKeywordVerbatimRelevance(), |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 785 | &keyword_results_.suggest_results, |
| 786 | &keyword_results_.navigation_results); |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 787 | } else { |
| 788 | // User is either in keyword mode with a blank input or out of |
| 789 | // keyword mode entirely. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 790 | keyword_results_.Clear(); |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 791 | } |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 792 | } |
| 793 | |
[email protected] | 42cfd01d0 | 2013-01-23 02:40:02 | [diff] [blame] | 794 | // static |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 795 | void SearchProvider::RemoveStaleResults(const string16& input, |
| 796 | int verbatim_relevance, |
| 797 | SuggestResults* suggest_results, |
| 798 | NavigationResults* navigation_results) { |
| 799 | DCHECK_GE(verbatim_relevance, 0); |
| 800 | // Keep pointers to the head of (the highest scoring elements of) |
| 801 | // |suggest_results| and |navigation_results|. Iterate down the lists |
| 802 | // removing non-inlineable results in order of decreasing relevance |
| 803 | // scores. Stop when the highest scoring element among those remaining |
| 804 | // is inlineable or the element is less than |verbatim_relevance|. |
| 805 | // This allows non-inlineable lower-scoring results to remain |
| 806 | // because (i) they are guaranteed to not be inlined and (ii) |
| 807 | // letting them remain reduces visual jank. For instance, as the |
| 808 | // user types the mis-spelled query "fpobar" (for foobar), the |
| 809 | // suggestion "foobar" will be suggested on every keystroke. If the |
| 810 | // SearchProvider always removes all non-inlineable results, the user will |
| 811 | // see visual jitter/jank as the result disappears and re-appears moments |
| 812 | // later as the suggest server returns results. |
| 813 | SuggestResults::iterator sug_it = suggest_results->begin(); |
| 814 | NavigationResults::iterator nav_it = navigation_results->begin(); |
| 815 | while ((sug_it != suggest_results->end()) || |
| 816 | (nav_it != navigation_results->end())) { |
| 817 | const int sug_rel = |
| 818 | (sug_it != suggest_results->end()) ? sug_it->relevance() : -1; |
| 819 | const int nav_rel = |
| 820 | (nav_it != navigation_results->end()) ? nav_it->relevance() : -1; |
| 821 | if (std::max(sug_rel, nav_rel) < verbatim_relevance) |
| 822 | break; |
| 823 | if (sug_rel > nav_rel) { |
| 824 | // The current top result is a search suggestion. |
| 825 | if (sug_it->IsInlineable(input)) |
| 826 | break; |
| 827 | sug_it = suggest_results->erase(sug_it); |
| 828 | } else if (sug_rel == nav_rel) { |
| 829 | // Have both results and they're tied. |
| 830 | const bool sug_inlineable = sug_it->IsInlineable(input); |
| 831 | const bool nav_inlineable = nav_it->IsInlineable(input); |
| 832 | if (!sug_inlineable) |
| 833 | sug_it = suggest_results->erase(sug_it); |
| 834 | if (!nav_inlineable) |
| 835 | nav_it = navigation_results->erase(nav_it); |
| 836 | if (sug_inlineable || nav_inlineable) |
| 837 | break; |
| 838 | } else { |
| 839 | // The current top result is a navigational suggestion. |
| 840 | if (nav_it->IsInlineable(input)) |
| 841 | break; |
| 842 | nav_it = navigation_results->erase(nav_it); |
| 843 | } |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 847 | void SearchProvider::AdjustDefaultProviderSuggestion( |
| 848 | const string16& previous_input, |
| 849 | const string16& current_input) { |
| 850 | if (default_provider_suggestion_.type == INSTANT_SUGGESTION_URL) { |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 851 | // Description and relevance do not matter in the check for staleness. |
[email protected] | a1b5e86 | 2013-03-29 06:38:44 | [diff] [blame] | 852 | NavigationResult result(*this, |
| 853 | GURL(default_provider_suggestion_.text), |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 854 | string16(), |
| 855 | false, |
| 856 | 100); |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 857 | // If navigation suggestion is stale, clear |default_provider_suggestion_|. |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 858 | if (!result.IsInlineable(current_input)) |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 859 | default_provider_suggestion_ = InstantSuggestion(); |
| 860 | } else { |
| 861 | DCHECK(default_provider_suggestion_.type == INSTANT_SUGGESTION_SEARCH); |
| 862 | // InstantSuggestion of type SEARCH contain only the suggested text, and not |
| 863 | // the full text of the query. This looks at the current and previous input |
| 864 | // to determine if the user is typing forward, and if the new input is |
| 865 | // contained in |default_provider_suggestion_|. If so, the suggestion is |
| 866 | // adjusted and can be kept. Otherwise, it is reset. |
| 867 | if (!previous_input.empty() && |
| 868 | StartsWith(current_input, previous_input, false)) { |
| 869 | // User is typing forward; verify if new input is part of the suggestion. |
| 870 | const string16 new_text = string16(current_input, previous_input.size()); |
| 871 | if (StartsWith(default_provider_suggestion_.text, new_text, false)) { |
| 872 | // New input is a prefix to the previous suggestion, adjust the |
| 873 | // suggestion to strip the prefix. |
| 874 | default_provider_suggestion_.text.erase(0, new_text.size()); |
| 875 | return; |
| 876 | } |
| 877 | } |
| 878 | // If we are here, the search suggestion is stale; reset it. |
| 879 | default_provider_suggestion_ = InstantSuggestion(); |
| 880 | } |
| 881 | } |
| 882 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 883 | void SearchProvider::ApplyCalculatedRelevance() { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 884 | ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results); |
| 885 | ApplyCalculatedSuggestRelevance(&default_results_.suggest_results); |
| 886 | ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results); |
| 887 | ApplyCalculatedNavigationRelevance(&default_results_.navigation_results); |
| 888 | default_results_.has_suggested_relevance = false; |
| 889 | keyword_results_.has_suggested_relevance = false; |
| 890 | default_results_.verbatim_relevance = -1; |
| 891 | keyword_results_.verbatim_relevance = -1; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 892 | } |
| 893 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 894 | void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list) { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 895 | for (size_t i = 0; i < list->size(); ++i) { |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 896 | SuggestResult& result = (*list)[i]; |
| 897 | result.set_relevance( |
| 898 | result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
| 899 | (list->size() - i - 1)); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 903 | void SearchProvider::ApplyCalculatedNavigationRelevance( |
| 904 | NavigationResults* list) { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 905 | for (size_t i = 0; i < list->size(); ++i) { |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 906 | NavigationResult& result = (*list)[i]; |
| 907 | result.set_relevance( |
| 908 | result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
| 909 | (list->size() - i - 1)); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 913 | net::URLFetcher* SearchProvider::CreateSuggestFetcher( |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 914 | int id, |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 915 | const TemplateURL* template_url, |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 916 | const AutocompleteInput& input) { |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 917 | if (!template_url || template_url->suggestions_url().empty()) |
| 918 | return NULL; |
| 919 | |
| 920 | // Bail if the suggestion URL is invalid with the given replacements. |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 921 | TemplateURLRef::SearchTermsArgs search_term_args(input.text()); |
| 922 | search_term_args.cursor_position = input.cursor_position(); |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 923 | GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms( |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 924 | search_term_args)); |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 925 | if (!suggest_url.is_valid()) |
| 926 | return NULL; |
| 927 | |
| 928 | suggest_results_pending_++; |
| 929 | LogOmniboxSuggestRequest(REQUEST_SENT); |
| 930 | |
| 931 | net::URLFetcher* fetcher = |
| 932 | net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this); |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 933 | fetcher->SetRequestContext(profile_->GetRequestContext()); |
[email protected] | d3cf8682f0 | 2012-02-29 23:29:34 | [diff] [blame] | 934 | fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 935 | // Add Chrome experiment state to the request headers. |
| 936 | net::HttpRequestHeaders headers; |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 937 | chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 938 | fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); |
| 939 | fetcher->SetExtraRequestHeaders(headers.ToString()); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 940 | fetcher->Start(); |
| 941 | return fetcher; |
| 942 | } |
| 943 | |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 944 | bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) { |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 945 | string16 query; |
| 946 | ListValue* root_list = NULL; |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 947 | ListValue* results_list = NULL; |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 948 | const string16& input_text = |
| 949 | is_keyword ? keyword_input_.text() : input_.text(); |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 950 | if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) || |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 951 | (query != input_text) || !root_list->GetList(1, &results_list)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 952 | return false; |
| 953 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 954 | // 3rd element: Description list. |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 955 | ListValue* descriptions = NULL; |
| 956 | root_list->GetList(2, &descriptions); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 957 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 958 | // 4th element: Disregard the query URL list for now. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 959 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 960 | // Reset suggested relevance information from the default provider. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 961 | Results* results = is_keyword ? &keyword_results_ : &default_results_; |
| 962 | results->has_suggested_relevance = false; |
| 963 | results->verbatim_relevance = -1; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 964 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 965 | // 5th element: Optional key-value pairs from the Suggest server. |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 966 | ListValue* types = NULL; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 967 | ListValue* relevances = NULL; |
| 968 | DictionaryValue* extras = NULL; |
| 969 | if (root_list->GetDictionary(4, &extras)) { |
| 970 | extras->GetList("google:suggesttype", &types); |
| 971 | |
[email protected] | eb8330d | 2013-06-15 00:55:08 | [diff] [blame^] | 972 | // Discard this list if its size does not match that of the suggestions. |
| 973 | if (extras->GetList("google:suggestrelevance", &relevances) && |
| 974 | relevances->GetSize() != results_list->GetSize()) |
| 975 | relevances = NULL; |
| 976 | extras->GetInteger("google:verbatimrelevance", |
| 977 | &results->verbatim_relevance); |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 978 | |
| 979 | // Check if the active suggest field trial (if any) has triggered either |
| 980 | // for the default provider or keyword provider. |
| 981 | bool triggered = false; |
| 982 | extras->GetBoolean("google:fieldtrialtriggered", &triggered); |
| 983 | field_trial_triggered_ |= triggered; |
| 984 | field_trial_triggered_in_session_ |= triggered; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 985 | } |
| 986 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 987 | // Clear the previous results now that new results are available. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 988 | results->suggest_results.clear(); |
| 989 | results->navigation_results.clear(); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 990 | |
| 991 | string16 result, title; |
| 992 | std::string type; |
| 993 | int relevance = -1; |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 994 | for (size_t index = 0; results_list->GetString(index, &result); ++index) { |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 995 | // Google search may return empty suggestions for weird input characters, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 996 | // they make no sense at all and can cause problems in our code. |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 997 | if (result.empty()) |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 998 | continue; |
| 999 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1000 | // Apply valid suggested relevance scores; discard invalid lists. |
| 1001 | if (relevances != NULL && !relevances->GetInteger(index, &relevance)) |
| 1002 | relevances = NULL; |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 1003 | if (types && types->GetString(index, &type) && (type == "NAVIGATION")) { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1004 | // Do not blindly trust the URL coming from the server to be valid. |
| 1005 | GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(result), std::string())); |
| 1006 | if (url.is_valid()) { |
| 1007 | if (descriptions != NULL) |
| 1008 | descriptions->GetString(index, &title); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1009 | results->navigation_results.push_back( |
| 1010 | NavigationResult(*this, url, title, is_keyword, relevance)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1011 | } |
| 1012 | } else { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1013 | // TODO(kochi): Improve calculator result presentation. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1014 | results->suggest_results.push_back( |
| 1015 | SuggestResult(result, is_keyword, relevance)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1019 | // Apply calculated relevance scores if a valid list was not provided. |
| 1020 | if (relevances == NULL) { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1021 | ApplyCalculatedSuggestRelevance(&results->suggest_results); |
| 1022 | ApplyCalculatedNavigationRelevance(&results->navigation_results); |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1023 | } else { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1024 | results->has_suggested_relevance = true; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1025 | } |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1026 | // Keep the result lists sorted. |
| 1027 | const CompareScoredResults comparator = CompareScoredResults(); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1028 | std::stable_sort(results->suggest_results.begin(), |
| 1029 | results->suggest_results.end(), |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1030 | comparator); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1031 | std::stable_sort(results->navigation_results.begin(), |
| 1032 | results->navigation_results.end(), |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1033 | comparator); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1034 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1035 | } |
| 1036 | |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1037 | void SearchProvider::ConvertResultsToAutocompleteMatches() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1038 | // Convert all the results to matches and add them to a map, so we can keep |
| 1039 | // the most relevant match for each result. |
| 1040 | MatchMap map; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1041 | const Time no_time; |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1042 | int did_not_accept_keyword_suggestion = |
| 1043 | keyword_results_.suggest_results.empty() ? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1044 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 1045 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1046 | |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1047 | int verbatim_relevance = GetVerbatimRelevance(); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1048 | int did_not_accept_default_suggestion = |
| 1049 | default_results_.suggest_results.empty() ? |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1050 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 1051 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1052 | if (verbatim_relevance > 0) { |
| 1053 | AddMatchToMap(input_.text(), input_.text(), verbatim_relevance, |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1054 | AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1055 | did_not_accept_default_suggestion, false, &map); |
| 1056 | } |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1057 | if (!keyword_input_.text().empty()) { |
| 1058 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 1059 | // We only create the verbatim search query match for a keyword |
| 1060 | // if it's not an extension keyword. Extension keywords are handled |
| 1061 | // in KeywordProvider::Start(). (Extensions are complicated...) |
| 1062 | // Note: in this provider, SEARCH_OTHER_ENGINE must correspond |
| 1063 | // to the keyword verbatim search query. Do not create other matches |
| 1064 | // of type SEARCH_OTHER_ENGINE. |
| 1065 | if (keyword_url && !keyword_url->IsExtensionKeyword()) { |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1066 | const int keyword_verbatim_relevance = GetKeywordVerbatimRelevance(); |
| 1067 | if (keyword_verbatim_relevance > 0) { |
| 1068 | AddMatchToMap(keyword_input_.text(), keyword_input_.text(), |
| 1069 | keyword_verbatim_relevance, |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1070 | AutocompleteMatchType::SEARCH_OTHER_ENGINE, |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1071 | did_not_accept_keyword_suggestion, true, &map); |
| 1072 | } |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | const size_t verbatim_matches_size = map.size(); |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 1076 | if (!default_provider_suggestion_.text.empty() && |
[email protected] | 2ac15ba | 2013-01-14 22:38:59 | [diff] [blame] | 1077 | default_provider_suggestion_.type == INSTANT_SUGGESTION_SEARCH && |
| 1078 | !input_.prevent_inline_autocomplete()) |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 1079 | AddMatchToMap(input_.text() + default_provider_suggestion_.text, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1080 | input_.text(), verbatim_relevance + 1, |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1081 | AutocompleteMatchType::SEARCH_SUGGEST, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1082 | did_not_accept_default_suggestion, false, &map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1083 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1084 | AddHistoryResultsToMap(keyword_history_results_, true, |
| 1085 | did_not_accept_keyword_suggestion, &map); |
| 1086 | AddHistoryResultsToMap(default_history_results_, false, |
| 1087 | did_not_accept_default_suggestion, &map); |
| 1088 | |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1089 | AddSuggestResultsToMap(keyword_results_.suggest_results, &map); |
| 1090 | AddSuggestResultsToMap(default_results_.suggest_results, &map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1091 | |
| 1092 | // Now add the most relevant matches from the map to |matches_|. |
| 1093 | matches_.clear(); |
| 1094 | for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |
| 1095 | matches_.push_back(i->second); |
| 1096 | |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 1097 | if (!default_provider_suggestion_.text.empty() && |
[email protected] | 2ac15ba | 2013-01-14 22:38:59 | [diff] [blame] | 1098 | default_provider_suggestion_.type == INSTANT_SUGGESTION_URL && |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 1099 | !input_.prevent_inline_autocomplete()) { |
| 1100 | // See comment in FinalizeInstantQuery() for why we don't use |
| 1101 | // |verbatim_relevance| here. |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 1102 | matches_.push_back(NavigationToMatch( |
[email protected] | a1b5e86 | 2013-03-29 06:38:44 | [diff] [blame] | 1103 | NavigationResult(*this, |
| 1104 | GURL(UTF16ToUTF8(default_provider_suggestion_.text)), |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 1105 | string16(), |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1106 | false, |
| 1107 | kNonURLVerbatimRelevance + 1))); |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 1108 | } |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1109 | AddNavigationResultsToMatches(keyword_results_.navigation_results, true); |
| 1110 | AddNavigationResultsToMatches(default_results_.navigation_results, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1111 | |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1112 | // Allow additional match(es) for verbatim results if present. |
| 1113 | const size_t max_total_matches = kMaxMatches + verbatim_matches_size; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1114 | std::partial_sort(matches_.begin(), |
| 1115 | matches_.begin() + std::min(max_total_matches, matches_.size()), |
| 1116 | matches_.end(), &AutocompleteMatch::MoreRelevant); |
[email protected] | 3723e6e | 2012-06-11 21:06:56 | [diff] [blame] | 1117 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1118 | if (matches_.size() > max_total_matches) |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1119 | matches_.resize(max_total_matches); |
| 1120 | } |
| 1121 | |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1122 | bool SearchProvider::IsTopMatchNavigationInKeywordMode() const { |
| 1123 | return (!providers_.keyword_provider().empty() && |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1124 | (matches_.front().type == AutocompleteMatchType::NAVSUGGEST)); |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1125 | } |
| 1126 | |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1127 | bool SearchProvider::IsTopMatchScoreTooLow() const { |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1128 | // Here we use CalculateRelevanceForVerbatimIgnoringKeywordModeState() |
| 1129 | // rather than CalculateRelevanceForVerbatim() because the latter returns |
| 1130 | // a very low score (250) if keyword mode is active. This is because |
| 1131 | // when keyword mode is active the user probably wants the keyword matches, |
| 1132 | // not matches from the default provider. Hence, we use the version of |
| 1133 | // the function that ignores whether keyword mode is active. This allows |
| 1134 | // SearchProvider to maintain its contract with the AutocompleteController |
| 1135 | // that it will always provide an inlineable match with a reasonable |
| 1136 | // score. |
| 1137 | return matches_.front().relevance < |
| 1138 | CalculateRelevanceForVerbatimIgnoringKeywordModeState(); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | bool SearchProvider::IsTopMatchHighRankSearchForURL() const { |
| 1142 | return input_.type() == AutocompleteInput::URL && |
| 1143 | matches_.front().relevance > CalculateRelevanceForVerbatim() && |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1144 | (matches_.front().type == AutocompleteMatchType::SEARCH_SUGGEST || |
| 1145 | matches_.front().type == |
| 1146 | AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED || |
| 1147 | matches_.front().type == AutocompleteMatchType::SEARCH_OTHER_ENGINE); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | bool SearchProvider::IsTopMatchNotInlinable() const { |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1151 | // Note: this test assumes the SEARCH_OTHER_ENGINE match corresponds to |
| 1152 | // the verbatim search query on the keyword engine. SearchProvider should |
| 1153 | // not create any other match of type SEARCH_OTHER_ENGINE. |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1154 | return |
| 1155 | matches_.front().type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED && |
| 1156 | matches_.front().type != AutocompleteMatchType::URL_WHAT_YOU_TYPED && |
| 1157 | matches_.front().type != AutocompleteMatchType::SEARCH_OTHER_ENGINE && |
| 1158 | matches_.front().inline_autocomplete_offset == string16::npos && |
| 1159 | matches_.front().fill_into_edit != input_.text(); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | void SearchProvider::UpdateMatches() { |
| 1163 | ConvertResultsToAutocompleteMatches(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1164 | |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1165 | // Check constraints that may be violated by suggested relevances. |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1166 | if (!matches_.empty() && |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1167 | (default_results_.has_suggested_relevance || |
| 1168 | default_results_.verbatim_relevance >= 0 || |
| 1169 | keyword_results_.has_suggested_relevance || |
| 1170 | keyword_results_.verbatim_relevance >= 0)) { |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1171 | // These blocks attempt to repair undesirable behavior by suggested |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1172 | // relevances with minimal impact, preserving other suggested relevances. |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1173 | if (IsTopMatchNavigationInKeywordMode()) { |
| 1174 | // Correct the suggested relevance scores if the top match is a |
| 1175 | // navigation in keyword mode, since inlining a navigation match |
| 1176 | // would break the user out of keyword mode. By the way, if the top |
| 1177 | // match is a non-keyword match (query or navsuggestion) in keyword |
| 1178 | // mode, the user would also break out of keyword mode. However, |
| 1179 | // that situation is impossible given the current scoring paradigm |
| 1180 | // and the fact that only one search engine (Google) provides suggested |
| 1181 | // relevance scores at this time. |
| 1182 | DemoteKeywordNavigationMatchesPastTopQuery(); |
| 1183 | ConvertResultsToAutocompleteMatches(); |
| 1184 | DCHECK(!IsTopMatchNavigationInKeywordMode()); |
| 1185 | } |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1186 | if (IsTopMatchScoreTooLow()) { |
[email protected] | 1beee34 | 2012-06-19 22:22:28 | [diff] [blame] | 1187 | // Disregard the suggested verbatim relevance if the top score is below |
| 1188 | // the usual verbatim value. For example, a BarProvider may rely on |
| 1189 | // SearchProvider's verbatim or inlineable matches for input "foo" to |
| 1190 | // always outrank its own lowly-ranked non-inlineable "bar" match. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1191 | default_results_.verbatim_relevance = -1; |
| 1192 | keyword_results_.verbatim_relevance = -1; |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1193 | ConvertResultsToAutocompleteMatches(); |
[email protected] | e6acd00 | 2012-06-16 22:27:47 | [diff] [blame] | 1194 | } |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1195 | if (IsTopMatchHighRankSearchForURL()) { |
[email protected] | e6acd00 | 2012-06-16 22:27:47 | [diff] [blame] | 1196 | // Disregard the suggested search and verbatim relevances if the input |
| 1197 | // type is URL and the top match is a highly-ranked search suggestion. |
| 1198 | // For example, prevent a search for "foo.com" from outranking another |
| 1199 | // provider's navigation for "foo.com" or "foo.com/url_from_history". |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1200 | ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results); |
| 1201 | ApplyCalculatedSuggestRelevance(&default_results_.suggest_results); |
| 1202 | default_results_.verbatim_relevance = -1; |
| 1203 | keyword_results_.verbatim_relevance = -1; |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1204 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1205 | } |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1206 | if (IsTopMatchNotInlinable()) { |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1207 | // Disregard suggested relevances if the top match is not a verbatim |
| 1208 | // match, inlinable, or URL_WHAT_YOU_TYPED (which may be top match |
| 1209 | // regardless of inlining). For example, input "foo" should not |
| 1210 | // invoke a search for "bar", which would happen if the "bar" search |
| 1211 | // match outranked all other matches. |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1212 | ApplyCalculatedRelevance(); |
| 1213 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1214 | } |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1215 | DCHECK(!IsTopMatchNavigationInKeywordMode()); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 1216 | DCHECK(!IsTopMatchScoreTooLow()); |
| 1217 | DCHECK(!IsTopMatchHighRankSearchForURL()); |
| 1218 | DCHECK(!IsTopMatchNotInlinable()); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1219 | } |
| 1220 | |
[email protected] | cc63dea | 2008-08-21 20:56:31 | [diff] [blame] | 1221 | UpdateStarredStateOfMatches(); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1222 | UpdateDone(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | void SearchProvider::AddNavigationResultsToMatches( |
| 1226 | const NavigationResults& navigation_results, |
| 1227 | bool is_keyword) { |
[email protected] | c112f0fe7 | 2013-01-18 20:30:09 | [diff] [blame] | 1228 | if (navigation_results.empty()) |
| 1229 | return; |
| 1230 | |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1231 | if (is_keyword ? |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1232 | keyword_results_.has_suggested_relevance : |
| 1233 | default_results_.has_suggested_relevance) { |
[email protected] | c112f0fe7 | 2013-01-18 20:30:09 | [diff] [blame] | 1234 | for (NavigationResults::const_iterator it = navigation_results.begin(); |
| 1235 | it != navigation_results.end(); ++it) |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1236 | matches_.push_back(NavigationToMatch(*it)); |
[email protected] | c112f0fe7 | 2013-01-18 20:30:09 | [diff] [blame] | 1237 | } else { |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1238 | // Pick the highest-scoring element only in absence of the |
| 1239 | // suggested relevance scores. (The results are already sorted.) |
[email protected] | 6c53584 | 2012-05-15 05:20:55 | [diff] [blame] | 1240 | // TODO(kochi|msw): Add more navigational results if they get more |
| 1241 | // meaningful relevance values; see http://b/1170574. |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1242 | matches_.push_back(NavigationToMatch(navigation_results.front())); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results, |
| 1247 | bool is_keyword, |
| 1248 | int did_not_accept_suggestion, |
| 1249 | MatchMap* map) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1250 | if (results.empty()) |
| 1251 | return; |
| 1252 | |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 1253 | bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() || |
| 1254 | (input_.type() == AutocompleteInput::URL); |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 1255 | const string16& input_text = |
| 1256 | is_keyword ? keyword_input_.text() : input_.text(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1257 | bool input_multiple_words = HasMultipleWords(input_text); |
| 1258 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1259 | SuggestResults scored_results; |
| 1260 | if (!prevent_inline_autocomplete && input_multiple_words) { |
| 1261 | // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit |
| 1262 | // queries if the input also has multiple words. But if we were already |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1263 | // autocompleting a multi-word, multi-visit query, and the current input is |
| 1264 | // still a prefix of it, then changing the autocompletion suddenly feels |
| 1265 | // wrong. To detect this case, first score as if only one word has been |
| 1266 | // typed, then check for a best result that is an autocompleted, multi-word |
| 1267 | // query. If we find one, then just keep that score set. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1268 | scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
| 1269 | false, input_text, is_keyword); |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1270 | if ((scored_results.front().relevance() < |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1271 | AutocompleteResult::kLowestDefaultScore) || |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1272 | !HasMultipleWords(scored_results.front().suggestion())) |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1273 | scored_results.clear(); // Didn't detect the case above, score normally. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1274 | } |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1275 | if (scored_results.empty()) |
| 1276 | scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
| 1277 | input_multiple_words, input_text, |
| 1278 | is_keyword); |
| 1279 | for (SuggestResults::const_iterator i(scored_results.begin()); |
| 1280 | i != scored_results.end(); ++i) { |
| 1281 | AddMatchToMap(i->suggestion(), input_text, i->relevance(), |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1282 | AutocompleteMatchType::SEARCH_HISTORY, |
| 1283 | did_not_accept_suggestion, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1284 | is_keyword, map); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1288 | SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1289 | const HistoryResults& results, |
| 1290 | bool base_prevent_inline_autocomplete, |
| 1291 | bool input_multiple_words, |
| 1292 | const string16& input_text, |
| 1293 | bool is_keyword) { |
[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 1294 | AutocompleteClassifier* classifier = |
| 1295 | AutocompleteClassifierFactory::GetForProfile(profile_); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1296 | SuggestResults scored_results; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1297 | for (HistoryResults::const_iterator i(results.begin()); i != results.end(); |
| 1298 | ++i) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1299 | // Don't autocomplete multi-word queries that have only been seen once |
| 1300 | // unless the user has typed more than one word. |
| 1301 | bool prevent_inline_autocomplete = base_prevent_inline_autocomplete || |
| 1302 | (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term)); |
| 1303 | |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 1304 | // Don't autocomplete search terms that would normally be treated as URLs |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1305 | // when typed. For example, if the user searched for "google.com" and types |
| 1306 | // "goog", don't autocomplete to the search term "google.com". Otherwise, |
| 1307 | // the input will look like a URL but act like a search, which is confusing. |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 1308 | // NOTE: We don't check this in the following cases: |
| 1309 | // * When inline autocomplete is disabled, we won't be inline |
| 1310 | // autocompleting this term, so we don't need to worry about confusion as |
| 1311 | // much. This also prevents calling Classify() again from inside the |
| 1312 | // classifier (which will corrupt state and likely crash), since the |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1313 | // classifier always disables inline autocomplete. |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 1314 | // * When the user has typed the whole term, the "what you typed" history |
| 1315 | // match will outrank us for URL-like inputs anyway, so we need not do |
| 1316 | // anything special. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1317 | if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) { |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 1318 | AutocompleteMatch match; |
[email protected] | f608ea10 | 2013-03-18 15:08:09 | [diff] [blame] | 1319 | classifier->Classify(i->term, false, false, &match, NULL); |
[email protected] | 2905f74 | 2011-10-13 03:51:58 | [diff] [blame] | 1320 | prevent_inline_autocomplete = |
[email protected] | 749e7ae0 | 2012-09-05 18:47:46 | [diff] [blame] | 1321 | !AutocompleteMatch::IsSearchType(match.type); |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 1322 | } |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1323 | |
| 1324 | int relevance = CalculateRelevanceForHistory(i->time, is_keyword, |
| 1325 | prevent_inline_autocomplete); |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1326 | scored_results.push_back(SuggestResult(i->term, is_keyword, relevance)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1327 | } |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1328 | |
| 1329 | // History returns results sorted for us. However, we may have docked some |
| 1330 | // results' scores, so things are no longer in order. Do a stable sort to get |
| 1331 | // things back in order without otherwise disturbing results with equal |
| 1332 | // scores, then force the scores to be unique, so that the order in which |
| 1333 | // they're shown is deterministic. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1334 | std::stable_sort(scored_results.begin(), scored_results.end(), |
| 1335 | CompareScoredResults()); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1336 | int last_relevance = 0; |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1337 | for (SuggestResults::iterator i(scored_results.begin()); |
| 1338 | i != scored_results.end(); ++i) { |
| 1339 | if ((i != scored_results.begin()) && (i->relevance() >= last_relevance)) |
| 1340 | i->set_relevance(last_relevance - 1); |
| 1341 | last_relevance = i->relevance(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1342 | } |
| 1343 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1344 | return scored_results; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1345 | } |
| 1346 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1347 | void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1348 | MatchMap* map) { |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1349 | for (size_t i = 0; i < results.size(); ++i) { |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1350 | const bool is_keyword = results[i].from_keyword_provider(); |
| 1351 | const string16& input = is_keyword ? keyword_input_.text() : input_.text(); |
| 1352 | AddMatchToMap(results[i].suggestion(), input, results[i].relevance(), |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1353 | AutocompleteMatchType::SEARCH_SUGGEST, i, is_keyword, map); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1354 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1355 | } |
| 1356 | |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1357 | int SearchProvider::GetVerbatimRelevance() const { |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 1358 | // Use the suggested verbatim relevance score if it is non-negative (valid), |
| 1359 | // if inline autocomplete isn't prevented (always show verbatim on backspace), |
[email protected] | 1beee34 | 2012-06-19 22:22:28 | [diff] [blame] | 1360 | // and if it won't suppress verbatim, leaving no default provider matches. |
| 1361 | // Otherwise, if the default provider returned no matches and was still able |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 1362 | // to suppress verbatim, the user would have no search/nav matches and may be |
[email protected] | 1beee34 | 2012-06-19 22:22:28 | [diff] [blame] | 1363 | // left unable to search using their default provider from the omnibox. |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 1364 | // Check for results on each verbatim calculation, as results from older |
| 1365 | // queries (on previous input) may be trimmed for failing to inline new input. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1366 | if (default_results_.verbatim_relevance >= 0 && |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1367 | !input_.prevent_inline_autocomplete() && |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1368 | (default_results_.verbatim_relevance > 0 || |
| 1369 | !default_results_.suggest_results.empty() || |
| 1370 | !default_results_.navigation_results.empty())) { |
| 1371 | return default_results_.verbatim_relevance; |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 1372 | } |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1373 | return CalculateRelevanceForVerbatim(); |
| 1374 | } |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 1375 | |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 1376 | int SearchProvider::CalculateRelevanceForVerbatim() const { |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 1377 | if (!providers_.keyword_provider().empty()) |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1378 | return 250; |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1379 | return CalculateRelevanceForVerbatimIgnoringKeywordModeState(); |
| 1380 | } |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1381 | |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1382 | int SearchProvider:: |
| 1383 | CalculateRelevanceForVerbatimIgnoringKeywordModeState() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1384 | switch (input_.type()) { |
| 1385 | case AutocompleteInput::UNKNOWN: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1386 | case AutocompleteInput::QUERY: |
| 1387 | case AutocompleteInput::FORCED_QUERY: |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 1388 | return kNonURLVerbatimRelevance; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1389 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1390 | case AutocompleteInput::URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1391 | return 850; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1392 | |
| 1393 | default: |
| 1394 | NOTREACHED(); |
| 1395 | return 0; |
| 1396 | } |
| 1397 | } |
| 1398 | |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1399 | int SearchProvider::GetKeywordVerbatimRelevance() const { |
| 1400 | // Use the suggested verbatim relevance score if it is non-negative (valid), |
| 1401 | // if inline autocomplete isn't prevented (always show verbatim on backspace), |
| 1402 | // and if it won't suppress verbatim, leaving no keyword provider matches. |
| 1403 | // Otherwise, if the keyword provider returned no matches and was still able |
| 1404 | // to suppress verbatim, the user would have no search/nav matches and may be |
| 1405 | // left unable to search using their keyword provider from the omnibox. |
| 1406 | // Check for results on each verbatim calculation, as results from older |
| 1407 | // queries (on previous input) may be trimmed for failing to inline new input. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1408 | if (keyword_results_.verbatim_relevance >= 0 && |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1409 | !input_.prevent_inline_autocomplete() && |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1410 | (keyword_results_.verbatim_relevance > 0 || |
| 1411 | !keyword_results_.suggest_results.empty() || |
| 1412 | !keyword_results_.navigation_results.empty())) { |
| 1413 | return keyword_results_.verbatim_relevance; |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1414 | } |
| 1415 | return CalculateRelevanceForKeywordVerbatim( |
| 1416 | keyword_input_.type(), keyword_input_.prefer_keyword()); |
| 1417 | } |
| 1418 | |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1419 | // static |
| 1420 | int SearchProvider::CalculateRelevanceForKeywordVerbatim( |
| 1421 | AutocompleteInput::Type type, |
| 1422 | bool prefer_keyword) { |
| 1423 | // This function is responsible for scoring verbatim query matches |
| 1424 | // for non-extension keywords. KeywordProvider::CalculateRelevance() |
| 1425 | // scores verbatim query matches for extension keywords, as well as |
| 1426 | // for keyword matches (i.e., suggestions of a keyword itself, not a |
| 1427 | // suggestion of a query on a keyword search engine). These two |
| 1428 | // functions are currently in sync, but there's no reason we |
| 1429 | // couldn't decide in the future to score verbatim matches |
| 1430 | // differently for extension and non-extension keywords. If you |
| 1431 | // make such a change, however, you should update this comment to |
| 1432 | // describe it, so it's clear why the functions diverge. |
| 1433 | if (prefer_keyword) |
| 1434 | return 1500; |
| 1435 | return (type == AutocompleteInput::QUERY) ? 1450 : 1100; |
| 1436 | } |
| 1437 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1438 | int SearchProvider::CalculateRelevanceForHistory( |
| 1439 | const Time& time, |
| 1440 | bool is_keyword, |
| 1441 | bool prevent_inline_autocomplete) const { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1442 | // The relevance of past searches falls off over time. There are two distinct |
| 1443 | // equations used. If the first equation is used (searches to the primary |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1444 | // provider that we want to inline autocomplete), the score starts at 1399 and |
| 1445 | // falls to 1300. If the second equation is used the relevance of a search 15 |
| 1446 | // minutes ago is discounted 50 points, while the relevance of a search two |
| 1447 | // weeks ago is discounted 450 points. |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1448 | double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.); |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1449 | bool is_primary_provider = is_keyword || !providers_.has_keyword_provider(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1450 | if (is_primary_provider && !prevent_inline_autocomplete) { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1451 | // Searches with the past two days get a different curve. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1452 | const double autocomplete_time = 2 * 24 * 60 * 60; |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1453 | if (elapsed_time < autocomplete_time) { |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame] | 1454 | return (is_keyword ? 1599 : 1399) - static_cast<int>(99 * |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1455 | std::pow(elapsed_time / autocomplete_time, 2.5)); |
| 1456 | } |
| 1457 | elapsed_time -= autocomplete_time; |
| 1458 | } |
| 1459 | |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 1460 | const int score_discount = |
| 1461 | static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1462 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 1463 | // Don't let scores go below 0. Negative relevance scores are meaningful in |
| 1464 | // a different way. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1465 | int base_score; |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1466 | if (is_primary_provider) |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1467 | base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050; |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1468 | else |
| 1469 | base_score = 200; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1470 | return std::max(0, base_score - score_discount); |
| 1471 | } |
| 1472 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 1473 | void SearchProvider::AddMatchToMap(const string16& query_string, |
| 1474 | const string16& input_text, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1475 | int relevance, |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 1476 | AutocompleteMatch::Type type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1477 | int accepted_suggestion, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 1478 | bool is_keyword, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1479 | MatchMap* map) { |
[email protected] | 4eeb54f9 | 2013-04-27 04:01:20 | [diff] [blame] | 1480 | // With Instant Extended, we never want to inline autocomplete search queries |
| 1481 | // -- they should always use grey text if they are to autocomplete at all. So |
| 1482 | // we clamp non-verbatim results to just below the verbatim score to ensure |
| 1483 | // that none of them are inline autocompleted. |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1484 | if (type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED && |
| 1485 | type != AutocompleteMatchType::SEARCH_OTHER_ENGINE && |
[email protected] | 4eeb54f9 | 2013-04-27 04:01:20 | [diff] [blame] | 1486 | chrome::IsInstantExtendedAPIEnabled()) { |
| 1487 | relevance = std::min(kNonURLVerbatimRelevance - 1, relevance); |
| 1488 | } |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 1489 | |
| 1490 | const string16& keyword = is_keyword ? |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 1491 | providers_.keyword_provider() : providers_.default_provider(); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 1492 | AutocompleteMatch match = CreateSearchSuggestion(profile_, this, input_, |
| 1493 | query_string, input_text, relevance, type, accepted_suggestion, |
[email protected] | f3e46eec | 2013-06-11 14:46:28 | [diff] [blame] | 1494 | is_keyword, keyword, omnibox_start_margin_); |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 1495 | if (!match.destination_url.is_valid()) |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 1496 | return; |
| 1497 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1498 | // Try to add |match| to |map|. If a match for |query_string| is already in |
| 1499 | // |map|, replace it if |match| is more relevant. |
| 1500 | // NOTE: Keep this ToLower() call in sync with url_database.cc. |
| 1501 | const std::pair<MatchMap::iterator, bool> i = map->insert( |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 1502 | std::pair<string16, AutocompleteMatch>( |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 1503 | base::i18n::ToLower(query_string), match)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1504 | // NOTE: We purposefully do a direct relevance comparison here instead of |
| 1505 | // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added |
| 1506 | // first" rather than "items alphabetically first" when the scores are equal. |
| 1507 | // The only case this matters is when a user has results with the same score |
| 1508 | // that differ only by capitalization; because the history system returns |
| 1509 | // results sorted by recency, this means we'll pick the most recent such |
| 1510 | // result even if the precision of our relevance score is too low to |
| 1511 | // distinguish the two. |
| 1512 | if (!i.second && (match.relevance > i.first->second.relevance)) |
| 1513 | i.first->second = match; |
| 1514 | } |
| 1515 | |
| 1516 | AutocompleteMatch SearchProvider::NavigationToMatch( |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1517 | const NavigationResult& navigation) { |
| 1518 | const string16& input = navigation.from_keyword_provider() ? |
| 1519 | keyword_input_.text() : input_.text(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1520 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | b7f64d74 | 2013-05-21 04:04:04 | [diff] [blame] | 1521 | AutocompleteMatchType::NAVSUGGEST); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1522 | match.destination_url = navigation.url(); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1523 | |
| 1524 | // First look for the user's input inside the fill_into_edit as it would be |
| 1525 | // without trimming the scheme, so we can find matches at the beginning of the |
| 1526 | // scheme. |
[email protected] | a1b5e86 | 2013-03-29 06:38:44 | [diff] [blame] | 1527 | const string16& untrimmed_fill_into_edit = navigation.formatted_url(); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1528 | const URLPrefix* prefix = |
| 1529 | URLPrefix::BestURLPrefix(untrimmed_fill_into_edit, input); |
| 1530 | size_t match_start = (prefix == NULL) ? |
| 1531 | untrimmed_fill_into_edit.find(input) : prefix->prefix.length(); |
| 1532 | size_t inline_autocomplete_offset = (prefix == NULL) ? |
| 1533 | string16::npos : (match_start + input.length()); |
| 1534 | bool trim_http = !HasHTTPScheme(input) && (!prefix || (match_start != 0)); |
| 1535 | |
| 1536 | // Preserve the forced query '?' prefix in |match.fill_into_edit|. |
| 1537 | // Otherwise, user edits to a suggestion would show non-Search results. |
| 1538 | if (input_.type() == AutocompleteInput::FORCED_QUERY) { |
| 1539 | match.fill_into_edit = ASCIIToUTF16("?"); |
| 1540 | if (inline_autocomplete_offset != string16::npos) |
| 1541 | ++inline_autocomplete_offset; |
| 1542 | } |
| 1543 | |
| 1544 | const std::string languages( |
| 1545 | profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 1546 | const net::FormatUrlTypes format_types = |
| 1547 | net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP); |
| 1548 | match.fill_into_edit += |
| 1549 | AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(), |
| 1550 | net::FormatUrl(navigation.url(), languages, format_types, |
| 1551 | net::UnescapeRule::SPACES, NULL, NULL, |
| 1552 | &inline_autocomplete_offset)); |
| 1553 | if (!input_.prevent_inline_autocomplete()) |
| 1554 | match.inline_autocomplete_offset = inline_autocomplete_offset; |
| 1555 | DCHECK((match.inline_autocomplete_offset == string16::npos) || |
| 1556 | (match.inline_autocomplete_offset <= match.fill_into_edit.length())); |
| 1557 | |
| 1558 | match.contents = net::FormatUrl(navigation.url(), languages, |
| 1559 | format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start); |
| 1560 | // If the first match in the untrimmed string was inside a scheme that we |
| 1561 | // trimmed, look for a subsequent match. |
| 1562 | if (match_start == string16::npos) |
| 1563 | match_start = match.contents.find(input); |
| 1564 | // Safe if |match_start| is npos; also safe if the input is longer than the |
| 1565 | // remaining contents after |match_start|. |
| 1566 | AutocompleteMatch::ClassifyLocationInString(match_start, input.length(), |
| 1567 | match.contents.length(), ACMatchClassification::URL, |
| 1568 | &match.contents_class); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1569 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1570 | match.description = navigation.description(); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1571 | AutocompleteMatch::ClassifyMatchInString(input, match.description, |
| 1572 | ACMatchClassification::NONE, &match.description_class); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1573 | return match; |
| 1574 | } |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1575 | |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1576 | void SearchProvider::DemoteKeywordNavigationMatchesPastTopQuery() { |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1577 | // First, determine the maximum score of any keyword query match (verbatim or |
| 1578 | // query suggestion). |
| 1579 | int max_query_relevance = GetKeywordVerbatimRelevance(); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1580 | if (!keyword_results_.suggest_results.empty()) { |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1581 | max_query_relevance = |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1582 | std::max(keyword_results_.suggest_results.front().relevance(), |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1583 | max_query_relevance); |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1584 | } |
| 1585 | // If no query is supposed to appear, then navigational matches cannot |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1586 | // be demoted past it. Get rid of suggested relevance scores for |
| 1587 | // navsuggestions and introduce the verbatim results again. The keyword |
| 1588 | // verbatim match will outscore the navsuggest matches. |
| 1589 | if (max_query_relevance == 0) { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1590 | ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results); |
| 1591 | ApplyCalculatedNavigationRelevance(&default_results_.navigation_results); |
| 1592 | keyword_results_.verbatim_relevance = -1; |
| 1593 | default_results_.verbatim_relevance = -1; |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1594 | return; |
| 1595 | } |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1596 | // Now we know we can enforce the minimum score constraint even after |
| 1597 | // the navigation matches are demoted. Proceed to demote the navigation |
| 1598 | // matches to enforce the query-must-come-first constraint. |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1599 | // Cap the relevance score of all results. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1600 | for (NavigationResults::iterator it = |
| 1601 | keyword_results_.navigation_results.begin(); |
| 1602 | it != keyword_results_.navigation_results.end(); ++it) { |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 1603 | if (it->relevance() < max_query_relevance) |
| 1604 | return; |
| 1605 | max_query_relevance = std::max(max_query_relevance - 1, 0); |
| 1606 | it->set_relevance(max_query_relevance); |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1607 | } |
[email protected] | d519d39 | 2013-03-27 09:24:46 | [diff] [blame] | 1608 | } |
| 1609 | |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1610 | void SearchProvider::UpdateDone() { |
[email protected] | 2cdf117 | 2012-08-26 12:21:33 | [diff] [blame] | 1611 | // We're done when the timer isn't running, there are no suggest queries |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 1612 | // pending, and we're not waiting on Instant. |
[email protected] | abe441e | 2013-05-06 12:35:05 | [diff] [blame] | 1613 | done_ = IsNonInstantSearchDone() && |
| 1614 | (instant_finalized_ || !chrome::IsInstantEnabled(profile_)); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1615 | } |