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