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