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