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