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