blob: c561490ad1301b5468d61b353253b4df8253852d [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/autocomplete/search_provider.h"
6
[email protected]1cb2dac2010-03-08 21:49:157#include <algorithm>
[email protected]c3a4bd992010-08-18 20:25:018#include <cmath>
[email protected]1cb2dac2010-03-08 21:49:159
[email protected]2041cf342010-02-19 03:15:5910#include "base/callback.h"
[email protected]51124552011-07-16 01:37:1011#include "base/i18n/break_iterator.h"
[email protected]503d03872011-05-06 08:36:2612#include "base/i18n/case_conversion.h"
[email protected]d6e58c6e2009-10-10 20:40:5013#include "base/i18n/icu_string_conversions.h"
[email protected]ffbec692012-02-26 20:26:4214#include "base/json/json_string_value_serializer.h"
[email protected]fa1e0e12013-07-18 00:10:1415#include "base/message_loop/message_loop.h"
[email protected]f5b95ba92012-03-27 14:05:1916#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5717#include "base/prefs/pref_service.h"
[email protected]98570e12013-06-10 19:54:2218#include "base/strings/string16.h"
19#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2020#include "base/strings/utf_string_conversions.h"
[email protected]ea3b9a502011-04-04 14:19:3721#include "chrome/browser/autocomplete/autocomplete_classifier.h"
[email protected]810ffba2012-06-12 01:07:4822#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
[email protected]9ac40092010-10-27 23:05:2623#include "chrome/browser/autocomplete/autocomplete_match.h"
[email protected]5af9bc82012-06-29 00:53:4824#include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
[email protected]73c2b1632012-07-02 22:51:3825#include "chrome/browser/autocomplete/autocomplete_result.h"
[email protected]2c812ba02011-07-14 00:23:1526#include "chrome/browser/autocomplete/keyword_provider.h"
[email protected]371dab12012-06-01 03:23:5527#include "chrome/browser/autocomplete/url_prefix.h"
[email protected]6a2c09f2013-01-25 04:50:0728#include "chrome/browser/history/history_service.h"
[email protected]9d2db762012-06-19 00:01:1029#include "chrome/browser/history/history_service_factory.h"
[email protected]10c2d692012-05-11 05:32:2330#include "chrome/browser/history/in_memory_database.h"
[email protected]ab7780792013-01-10 01:26:0931#include "chrome/browser/metrics/variations/variations_http_header_provider.h"
[email protected]684fcc12013-03-08 22:50:0232#include "chrome/browser/omnibox/omnibox_field_trial.h"
[email protected]8ecad5e2010-12-02 21:18:3333#include "chrome/browser/profiles/profile.h"
[email protected]a7b8e43d2013-03-18 18:52:4334#include "chrome/browser/search/search.h"
[email protected]9899a612012-08-21 23:50:0435#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
[email protected]8e5c89a2011-06-07 18:13:3336#include "chrome/browser/search_engines/template_url_service.h"
37#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]c4a9d4c2013-08-10 07:06:2738#include "chrome/browser/ui/browser.h"
39#include "chrome/browser/ui/browser_finder.h"
40#include "chrome/browser/ui/browser_instant_controller.h"
41#include "chrome/browser/ui/search/instant_controller.h"
[email protected]4f3b4462013-07-27 19:20:1842#include "chrome/common/net/url_fixer_upper.h"
initial.commit09911bf2008-07-26 23:55:2943#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0244#include "chrome/common/url_constants.h"
[email protected]34ac8f32009-02-22 23:03:2745#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2946#include "net/base/escape.h"
[email protected]d3cf8682f02012-02-29 23:29:3447#include "net/base/load_flags.h"
[email protected]371dab12012-06-01 03:23:5548#include "net/base/net_util.h"
[email protected]bd3b4712012-12-18 17:01:3049#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2150#include "net/http/http_response_headers.h"
[email protected]3dc1bc42012-06-19 08:20:5351#include "net/url_request/url_fetcher.h"
[email protected]319d9e6f2009-02-18 19:47:2152#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1753#include "ui/base/l10n/l10n_util.h"
[email protected]761fa4702013-07-02 15:25:1554#include "url/url_util.h"
initial.commit09911bf2008-07-26 23:55:2955
[email protected]bc8bb0cd2013-06-24 21:50:2356
57// Helpers --------------------------------------------------------------------
[email protected]e1acf6f2008-10-27 20:43:3358
[email protected]51124552011-07-16 01:37:1059namespace {
60
[email protected]7706a522012-08-16 17:42:2561// We keep track in a histogram how many suggest requests we send, how
62// many suggest requests we invalidate (e.g., due to a user typing
63// another character), and how many replies we receive.
64// *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
65// (excluding the end-of-list enum value)
66// We do not want values of existing enums to change or else it screws
67// up the statistics.
68enum SuggestRequestsHistogramValue {
69 REQUEST_SENT = 1,
70 REQUEST_INVALIDATED,
71 REPLY_RECEIVED,
72 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE
73};
74
[email protected]90fe2bb2013-01-15 03:42:1375// The verbatim score for an input which is not an URL.
76const int kNonURLVerbatimRelevance = 1300;
77
[email protected]7706a522012-08-16 17:42:2578// Increments the appropriate value in the histogram by one.
79void LogOmniboxSuggestRequest(
80 SuggestRequestsHistogramValue request_value) {
81 UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value,
82 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE);
83}
84
[email protected]51124552011-07-16 01:37:1085bool HasMultipleWords(const string16& text) {
86 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
87 bool found_word = false;
88 if (i.Init()) {
89 while (i.Advance()) {
90 if (i.IsWord()) {
91 if (found_word)
92 return true;
93 found_word = true;
94 }
95 }
96 }
97 return false;
98}
99
[email protected]d1f0a7f2012-06-05 10:26:42100} // namespace
[email protected]51124552011-07-16 01:37:10101
[email protected]033f3422012-03-13 21:24:18102
[email protected]3954c3a2012-04-10 20:17:55103// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:58104
[email protected]85b8d6f2012-05-08 20:53:47105SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
106 : template_url_service_(template_url_service) {
107}
108
109const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
110 return default_provider_.empty() ? NULL :
111 template_url_service_->GetTemplateURLForKeyword(default_provider_);
112}
113
114const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
115 return keyword_provider_.empty() ? NULL :
116 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:24117}
118
[email protected]3954c3a2012-04-10 20:17:55119
[email protected]bc8bb0cd2013-06-24 21:50:23120// SearchProvider::Result -----------------------------------------------------
121
122SearchProvider::Result::Result(bool from_keyword_provider,
[email protected]d30268a2013-06-25 22:31:07123 int relevance,
124 bool relevance_from_server)
[email protected]bc8bb0cd2013-06-24 21:50:23125 : from_keyword_provider_(from_keyword_provider),
[email protected]d30268a2013-06-25 22:31:07126 relevance_(relevance),
127 relevance_from_server_(relevance_from_server) {
[email protected]bc8bb0cd2013-06-24 21:50:23128}
129
130SearchProvider::Result::~Result() {
131}
132
133
134// SearchProvider::SuggestResult ----------------------------------------------
135
136SearchProvider::SuggestResult::SuggestResult(const string16& suggestion,
137 bool from_keyword_provider,
[email protected]d30268a2013-06-25 22:31:07138 int relevance,
139 bool relevance_from_server)
140 : Result(from_keyword_provider, relevance, relevance_from_server),
[email protected]bc8bb0cd2013-06-24 21:50:23141 suggestion_(suggestion) {
142}
143
144SearchProvider::SuggestResult::~SuggestResult() {
145}
146
147bool SearchProvider::SuggestResult::IsInlineable(const string16& input) const {
148 return StartsWith(suggestion_, input, false);
149}
150
151int SearchProvider::SuggestResult::CalculateRelevance(
152 const AutocompleteInput& input,
153 bool keyword_provider_requested) const {
154 if (!from_keyword_provider_ && keyword_provider_requested)
155 return 100;
156 return ((input.type() == AutocompleteInput::URL) ? 300 : 600);
157}
158
159
160// SearchProvider::NavigationResult -------------------------------------------
161
162SearchProvider::NavigationResult::NavigationResult(
163 const AutocompleteProvider& provider,
164 const GURL& url,
165 const string16& description,
166 bool from_keyword_provider,
[email protected]d30268a2013-06-25 22:31:07167 int relevance,
168 bool relevance_from_server)
169 : Result(from_keyword_provider, relevance, relevance_from_server),
[email protected]bc8bb0cd2013-06-24 21:50:23170 url_(url),
171 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning(
172 url, provider.StringForURLDisplay(url, true, false))),
173 description_(description) {
174 DCHECK(url_.is_valid());
175}
176
177SearchProvider::NavigationResult::~NavigationResult() {
178}
179
180bool SearchProvider::NavigationResult::IsInlineable(
181 const string16& input) const {
182 return URLPrefix::BestURLPrefix(formatted_url_, input) != NULL;
183}
184
185int SearchProvider::NavigationResult::CalculateRelevance(
186 const AutocompleteInput& input,
187 bool keyword_provider_requested) const {
188 return (from_keyword_provider_ || !keyword_provider_requested) ? 800 : 150;
189}
190
191
192// SearchProvider::CompareScoredResults ---------------------------------------
193
194class SearchProvider::CompareScoredResults {
195 public:
196 bool operator()(const Result& a, const Result& b) {
197 // Sort in descending relevance order.
198 return a.relevance() > b.relevance();
199 }
200};
201
202
203// SearchProvider::Results ----------------------------------------------------
204
[email protected]d30268a2013-06-25 22:31:07205SearchProvider::Results::Results() : verbatim_relevance(-1) {
[email protected]bc8bb0cd2013-06-24 21:50:23206}
207
208SearchProvider::Results::~Results() {
209}
210
211void SearchProvider::Results::Clear() {
212 suggest_results.clear();
213 navigation_results.clear();
[email protected]bc8bb0cd2013-06-24 21:50:23214 verbatim_relevance = -1;
215}
216
217bool SearchProvider::Results::HasServerProvidedScores() const {
218 if (verbatim_relevance >= 0)
219 return true;
220
[email protected]d30268a2013-06-25 22:31:07221 // Right now either all results of one type will be server-scored or they will
222 // all be locally scored, but in case we change this later, we'll just check
223 // them all.
224 for (SuggestResults::const_iterator i(suggest_results.begin());
225 i != suggest_results.end(); ++i) {
226 if (i->relevance_from_server())
227 return true;
228 }
229 for (NavigationResults::const_iterator i(navigation_results.begin());
230 i != navigation_results.end(); ++i) {
231 if (i->relevance_from_server())
232 return true;
233 }
234
235 return false;
[email protected]bc8bb0cd2013-06-24 21:50:23236}
[email protected]d30268a2013-06-25 22:31:07237
238
[email protected]3954c3a2012-04-10 20:17:55239// SearchProvider -------------------------------------------------------------
240
241// static
242const int SearchProvider::kDefaultProviderURLFetcherID = 1;
[email protected]3954c3a2012-04-10 20:17:55243const int SearchProvider::kKeywordProviderURLFetcherID = 2;
[email protected]24dbf2302013-04-05 16:59:25244int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100;
[email protected]d30268a2013-06-25 22:31:07245const char SearchProvider::kRelevanceFromServerKey[] = "relevance_from_server";
246const char SearchProvider::kTrue[] = "true";
247const char SearchProvider::kFalse[] = "false";
[email protected]3954c3a2012-04-10 20:17:55248
[email protected]bc8bb0cd2013-06-24 21:50:23249SearchProvider::SearchProvider(AutocompleteProviderListener* listener,
250 Profile* profile)
251 : AutocompleteProvider(listener, profile,
252 AutocompleteProvider::TYPE_SEARCH),
253 providers_(TemplateURLServiceFactory::GetForProfile(profile)),
254 suggest_results_pending_(0),
[email protected]bc8bb0cd2013-06-24 21:50:23255 field_trial_triggered_(false),
[email protected]c4a9d4c2013-08-10 07:06:27256 field_trial_triggered_in_session_(false) {
[email protected]bc8bb0cd2013-06-24 21:50:23257}
258
[email protected]cb86ee6f2013-04-28 16:58:15259// static
260AutocompleteMatch SearchProvider::CreateSearchSuggestion(
[email protected]cb86ee6f2013-04-28 16:58:15261 AutocompleteProvider* autocomplete_provider,
[email protected]cb86ee6f2013-04-28 16:58:15262 int relevance,
263 AutocompleteMatch::Type type,
[email protected]bba9e632013-06-28 22:52:19264 const TemplateURL* template_url,
265 const string16& query_string,
266 const string16& input_text,
267 const AutocompleteInput& input,
[email protected]cb86ee6f2013-04-28 16:58:15268 bool is_keyword,
[email protected]bba9e632013-06-28 22:52:19269 int accepted_suggestion,
[email protected]56fa29592013-07-02 20:25:53270 int omnibox_start_margin,
271 bool append_extra_query_params) {
[email protected]cb86ee6f2013-04-28 16:58:15272 AutocompleteMatch match(autocomplete_provider, relevance, false, type);
273
[email protected]bba9e632013-06-28 22:52:19274 if (!template_url)
[email protected]cb86ee6f2013-04-28 16:58:15275 return match;
[email protected]bba9e632013-06-28 22:52:19276 match.keyword = template_url->keyword();
[email protected]cb86ee6f2013-04-28 16:58:15277
278 match.contents.assign(query_string);
279 // We do intra-string highlighting for suggestions - the suggested segment
280 // will be highlighted, e.g. for input_text = "you" the suggestion may be
281 // "youtube", so we'll bold the "tube" section: you*tube*.
282 if (input_text != query_string) {
283 size_t input_position = match.contents.find(input_text);
284 if (input_position == string16::npos) {
285 // The input text is not a substring of the query string, e.g. input
286 // text is "slasdot" and the query string is "slashdot", so we bold the
287 // whole thing.
288 match.contents_class.push_back(
289 ACMatchClassification(0, ACMatchClassification::MATCH));
290 } else {
291 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
292 // "bold" this. Consider modifying the terminology.
293 // We don't iterate over the string here annotating all matches because
294 // it looks odd to have every occurrence of a substring that may be as
295 // short as a single character highlighted in a query suggestion result,
296 // e.g. for input text "s" and query string "southwest airlines", it
297 // looks odd if both the first and last s are highlighted.
298 if (input_position != 0) {
299 match.contents_class.push_back(
[email protected]4b6d916e2013-06-07 05:02:08300 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]cb86ee6f2013-04-28 16:58:15301 }
302 match.contents_class.push_back(
[email protected]4b6d916e2013-06-07 05:02:08303 ACMatchClassification(input_position, ACMatchClassification::NONE));
[email protected]cb86ee6f2013-04-28 16:58:15304 size_t next_fragment_position = input_position + input_text.length();
305 if (next_fragment_position < query_string.length()) {
306 match.contents_class.push_back(
307 ACMatchClassification(next_fragment_position,
[email protected]4b6d916e2013-06-07 05:02:08308 ACMatchClassification::MATCH));
[email protected]cb86ee6f2013-04-28 16:58:15309 }
310 }
311 } else {
[email protected]45f89a92013-08-12 13:41:36312 // Otherwise, |match| is a verbatim (what-you-typed) match, either for the
313 // default provider or a keyword search provider.
[email protected]cb86ee6f2013-04-28 16:58:15314 match.contents_class.push_back(
315 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]45f89a92013-08-12 13:41:36316 match.allowed_to_be_default_match = true;
[email protected]cb86ee6f2013-04-28 16:58:15317 }
318
319 // When the user forced a query, we need to make sure all the fill_into_edit
320 // values preserve that property. Otherwise, if the user starts editing a
321 // suggestion, non-Search results will suddenly appear.
322 if (input.type() == AutocompleteInput::FORCED_QUERY)
323 match.fill_into_edit.assign(ASCIIToUTF16("?"));
324 if (is_keyword)
325 match.fill_into_edit.append(match.keyword + char16(' '));
326 if (!input.prevent_inline_autocomplete() &&
327 StartsWith(query_string, input_text, false)) {
[email protected]518024c2013-07-19 23:40:25328 match.inline_autocompletion = query_string.substr(input_text.length());
[email protected]45f89a92013-08-12 13:41:36329 match.allowed_to_be_default_match = true;
[email protected]cb86ee6f2013-04-28 16:58:15330 }
331 match.fill_into_edit.append(query_string);
332
[email protected]bba9e632013-06-28 22:52:19333 const TemplateURLRef& search_url = template_url->url_ref();
[email protected]cb86ee6f2013-04-28 16:58:15334 DCHECK(search_url.SupportsReplacement());
335 match.search_terms_args.reset(
336 new TemplateURLRef::SearchTermsArgs(query_string));
337 match.search_terms_args->original_query = input_text;
338 match.search_terms_args->accepted_suggestion = accepted_suggestion;
[email protected]f3e46eec2013-06-11 14:46:28339 match.search_terms_args->omnibox_start_margin = omnibox_start_margin;
[email protected]56fa29592013-07-02 20:25:53340 match.search_terms_args->append_extra_query_params =
341 append_extra_query_params;
[email protected]cb86ee6f2013-04-28 16:58:15342 // This is the destination URL sans assisted query stats. This must be set
343 // so the AutocompleteController can properly de-dupe; the controller will
344 // eventually overwrite it before it reaches the user.
345 match.destination_url =
346 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get()));
347
348 // Search results don't look like URLs.
349 match.transition = is_keyword ?
350 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED;
351
352 return match;
353}
354
[email protected]bc8bb0cd2013-06-24 21:50:23355void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
356 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
357 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
358 new_entry.set_provider(AsOmniboxEventProviderType());
359 new_entry.set_provider_done(done_);
360 std::vector<uint32> field_trial_hashes;
361 OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes);
362 for (size_t i = 0; i < field_trial_hashes.size(); ++i) {
363 if (field_trial_triggered_)
364 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]);
365 if (field_trial_triggered_in_session_) {
366 new_entry.mutable_field_trial_triggered_in_session()->Add(
367 field_trial_hashes[i]);
368 }
369 }
370}
371
372void SearchProvider::ResetSession() {
373 field_trial_triggered_in_session_ = false;
[email protected]4ab4c7c2010-11-24 04:49:34374}
375
[email protected]bc8bb0cd2013-06-24 21:50:23376SearchProvider::~SearchProvider() {
377}
378
379// static
380void SearchProvider::RemoveStaleResults(const string16& input,
381 int verbatim_relevance,
382 SuggestResults* suggest_results,
383 NavigationResults* navigation_results) {
384 DCHECK_GE(verbatim_relevance, 0);
385 // Keep pointers to the head of (the highest scoring elements of)
386 // |suggest_results| and |navigation_results|. Iterate down the lists
387 // removing non-inlineable results in order of decreasing relevance
388 // scores. Stop when the highest scoring element among those remaining
389 // is inlineable or the element is less than |verbatim_relevance|.
390 // This allows non-inlineable lower-scoring results to remain
391 // because (i) they are guaranteed to not be inlined and (ii)
392 // letting them remain reduces visual jank. For instance, as the
393 // user types the mis-spelled query "fpobar" (for foobar), the
394 // suggestion "foobar" will be suggested on every keystroke. If the
395 // SearchProvider always removes all non-inlineable results, the user will
396 // see visual jitter/jank as the result disappears and re-appears moments
397 // later as the suggest server returns results.
398 SuggestResults::iterator sug_it = suggest_results->begin();
399 NavigationResults::iterator nav_it = navigation_results->begin();
400 while ((sug_it != suggest_results->end()) ||
401 (nav_it != navigation_results->end())) {
402 const int sug_rel =
403 (sug_it != suggest_results->end()) ? sug_it->relevance() : -1;
404 const int nav_rel =
405 (nav_it != navigation_results->end()) ? nav_it->relevance() : -1;
406 if (std::max(sug_rel, nav_rel) < verbatim_relevance)
407 break;
408 if (sug_rel > nav_rel) {
409 // The current top result is a search suggestion.
410 if (sug_it->IsInlineable(input))
411 break;
412 sug_it = suggest_results->erase(sug_it);
413 } else if (sug_rel == nav_rel) {
414 // Have both results and they're tied.
415 const bool sug_inlineable = sug_it->IsInlineable(input);
416 const bool nav_inlineable = nav_it->IsInlineable(input);
417 if (!sug_inlineable)
418 sug_it = suggest_results->erase(sug_it);
419 if (!nav_inlineable)
420 nav_it = navigation_results->erase(nav_it);
421 if (sug_inlineable || nav_inlineable)
422 break;
423 } else {
424 // The current top result is a navigational suggestion.
425 if (nav_it->IsInlineable(input))
426 break;
427 nav_it = navigation_results->erase(nav_it);
428 }
429 }
430}
431
432// static
433int SearchProvider::CalculateRelevanceForKeywordVerbatim(
434 AutocompleteInput::Type type,
435 bool prefer_keyword) {
436 // This function is responsible for scoring verbatim query matches
437 // for non-extension keywords. KeywordProvider::CalculateRelevance()
438 // scores verbatim query matches for extension keywords, as well as
439 // for keyword matches (i.e., suggestions of a keyword itself, not a
440 // suggestion of a query on a keyword search engine). These two
441 // functions are currently in sync, but there's no reason we
442 // couldn't decide in the future to score verbatim matches
443 // differently for extension and non-extension keywords. If you
444 // make such a change, however, you should update this comment to
445 // describe it, so it's clear why the functions diverge.
446 if (prefer_keyword)
447 return 1500;
448 return (type == AutocompleteInput::QUERY) ? 1450 : 1100;
449}
450
initial.commit09911bf2008-07-26 23:55:29451void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27452 bool minimal_changes) {
[email protected]04504c242013-01-22 21:08:55453 // Do our best to load the model as early as possible. This will reduce
454 // odds of having the model not ready when really needed (a non-empty input).
455 TemplateURLService* model = providers_.template_url_service();
456 DCHECK(model);
457 model->Load();
458
initial.commit09911bf2008-07-26 23:55:29459 matches_.clear();
[email protected]618d6e62012-12-16 05:55:57460 field_trial_triggered_ = false;
initial.commit09911bf2008-07-26 23:55:29461
[email protected]6c85aa02009-02-27 12:08:09462 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29463 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
[email protected]e29249dc52012-07-19 17:33:50464 Stop(false);
initial.commit09911bf2008-07-26 23:55:29465 return;
466 }
467
[email protected]14710852013-02-05 23:45:41468 keyword_input_ = input;
[email protected]257ab712009-04-14 17:16:24469 const TemplateURL* keyword_provider =
[email protected]14710852013-02-05 23:45:41470 KeywordProvider::GetSubstitutingTemplateURLForInput(model,
471 &keyword_input_);
472 if (keyword_provider == NULL)
473 keyword_input_.Clear();
474 else if (keyword_input_.text().empty())
[email protected]257ab712009-04-14 17:16:24475 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24476
[email protected]85b8d6f2012-05-08 20:53:47477 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]9b74ab52012-03-30 16:08:07478 if (default_provider && !default_provider->SupportsReplacement())
[email protected]257ab712009-04-14 17:16:24479 default_provider = NULL;
480
481 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18482 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24483
484 if (!default_provider && !keyword_provider) {
485 // No valid providers.
[email protected]e29249dc52012-07-19 17:33:50486 Stop(false);
initial.commit09911bf2008-07-26 23:55:29487 return;
488 }
489
490 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24491 // or the providers, abort the query.
[email protected]85b8d6f2012-05-08 20:53:47492 string16 default_provider_keyword(default_provider ?
493 default_provider->keyword() : string16());
494 string16 keyword_provider_keyword(keyword_provider ?
495 keyword_provider->keyword() : string16());
[email protected]9e789742011-01-10 23:27:32496 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47497 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]bb900e02013-03-14 14:15:29498 // Cancel any in-flight suggest requests.
[email protected]e1290ee62013-06-26 18:31:15499 if (!done_)
[email protected]e29249dc52012-07-19 17:33:50500 Stop(false);
[email protected]257ab712009-04-14 17:16:24501 }
initial.commit09911bf2008-07-26 23:55:29502
[email protected]85b8d6f2012-05-08 20:53:47503 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29504
505 if (input.text().empty()) {
506 // User typed "?" alone. Give them a placeholder result indicating what
507 // this syntax does.
[email protected]257ab712009-04-14 17:16:24508 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00509 AutocompleteMatch match;
510 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36511 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24512 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35513 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47514 match.keyword = providers_.default_provider();
[email protected]45f89a92013-08-12 13:41:36515 match.allowed_to_be_default_match = true;
[email protected]257ab712009-04-14 17:16:24516 matches_.push_back(match);
517 }
[email protected]e29249dc52012-07-19 17:33:50518 Stop(false);
initial.commit09911bf2008-07-26 23:55:29519 return;
520 }
521
522 input_ = input;
523
[email protected]e1290ee62013-06-26 18:31:15524 DoHistoryQuery(minimal_changes);
525 StartOrStopSuggestQuery(minimal_changes);
[email protected]344946a12012-12-20 12:03:42526 UpdateMatches();
initial.commit09911bf2008-07-26 23:55:29527}
528
[email protected]e29249dc52012-07-19 17:33:50529void SearchProvider::Stop(bool clear_cached_results) {
initial.commit09911bf2008-07-26 23:55:29530 StopSuggest();
531 done_ = true;
[email protected]e29249dc52012-07-19 17:33:50532
533 if (clear_cached_results)
[email protected]71b46152013-05-03 16:39:20534 ClearAllResults();
initial.commit09911bf2008-07-26 23:55:29535}
536
[email protected]10c2d692012-05-11 05:32:23537void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) {
initial.commit09911bf2008-07-26 23:55:29538 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24539 suggest_results_pending_--;
[email protected]7706a522012-08-16 17:42:25540 LogOmniboxSuggestRequest(REPLY_RECEIVED);
[email protected]1cb2dac2010-03-08 21:49:15541 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06542 const net::HttpResponseHeaders* const response_headers =
[email protected]7cc6e5632011-10-25 17:56:12543 source->GetResponseHeaders();
[email protected]c530c852011-10-24 18:18:34544 std::string json_data;
545 source->GetResponseAsString(&json_data);
[email protected]6c85aa02009-02-27 12:08:09546 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
547 // files in non-UTF-8 encodings. The actual encoding is usually specified in
548 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06549 if (response_headers) {
550 std::string charset;
551 if (response_headers->GetCharset(&charset)) {
[email protected]a2fedb1e2011-01-25 15:23:36552 string16 data_16;
[email protected]ec9207d32008-09-26 00:51:06553 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]c530c852011-10-24 18:18:34554 if (base::CodepageToUTF16(json_data, charset.c_str(),
[email protected]a2fedb1e2011-01-25 15:23:36555 base::OnStringConversionError::FAIL,
556 &data_16))
557 json_data = UTF16ToUTF8(data_16);
[email protected]ec9207d32008-09-26 00:51:06558 }
559 }
560
[email protected]d7ad4772012-06-01 03:12:54561 const bool is_keyword = (source == keyword_fetcher_.get());
[email protected]f2916ea2013-01-26 02:20:14562 // Ensure the request succeeded and that the provider used is still available.
563 // A verbatim match cannot be generated without this provider, causing errors.
[email protected]013e9a02012-05-18 20:27:10564 const bool request_succeeded =
[email protected]bc8bb0cd2013-06-24 21:50:23565 source->GetStatus().is_success() && (source->GetResponseCode() == 200) &&
566 (is_keyword ?
567 providers_.GetKeywordProviderURL() :
568 providers_.GetDefaultProviderURL());
[email protected]a0ad93ea2012-05-07 22:11:53569
570 // Record response time for suggest requests sent to Google. We care
571 // only about the common case: the Google default provider used in
572 // non-keyword mode.
[email protected]85b8d6f2012-05-08 20:53:47573 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
[email protected]55ce8f12012-05-09 04:44:08574 if (!is_keyword && default_url &&
[email protected]9899a612012-08-21 23:50:04575 (TemplateURLPrepopulateData::GetEngineType(default_url->url()) ==
576 SEARCH_ENGINE_GOOGLE)) {
[email protected]bc8bb0cd2013-06-24 21:50:23577 const base::TimeDelta elapsed_time =
[email protected]013e9a02012-05-18 20:27:10578 base::TimeTicks::Now() - time_suggest_request_sent_;
579 if (request_succeeded) {
580 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
581 elapsed_time);
582 } else {
583 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
584 elapsed_time);
585 }
[email protected]b4cebf82008-12-29 19:59:08586 }
587
[email protected]d7ad4772012-06-01 03:12:54588 bool results_updated = false;
589 if (request_succeeded) {
590 JSONStringValueSerializer deserializer(json_data);
591 deserializer.set_allow_trailing_comma(true);
592 scoped_ptr<Value> data(deserializer.Deserialize(NULL, NULL));
593 results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword);
594 }
595
[email protected]344946a12012-12-20 12:03:42596 UpdateMatches();
[email protected]d7ad4772012-06-01 03:12:54597 if (done_ || results_updated)
598 listener_->OnProviderUpdate(results_updated);
initial.commit09911bf2008-07-26 23:55:29599}
600
[email protected]bc8bb0cd2013-06-24 21:50:23601void SearchProvider::Run() {
602 // Start a new request with the current input.
603 suggest_results_pending_ = 0;
604 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]abe441e2013-05-06 12:35:05605
[email protected]bc8bb0cd2013-06-24 21:50:23606 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
607 providers_.GetDefaultProviderURL(), input_));
608 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
609 providers_.GetKeywordProviderURL(), keyword_input_));
610
611 // Both the above can fail if the providers have been modified or deleted
612 // since the query began.
613 if (suggest_results_pending_ == 0) {
614 UpdateDone();
615 // We only need to update the listener if we're actually done.
616 if (done_)
617 listener_->OnProviderUpdate(false);
618 }
[email protected]601858c02010-09-01 17:08:20619}
620
[email protected]8d457132010-11-04 18:13:40621void SearchProvider::DoHistoryQuery(bool minimal_changes) {
622 // The history query results are synchronous, so if minimal_changes is true,
623 // we still have the last results and don't need to do anything.
624 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29625 return;
626
[email protected]8d457132010-11-04 18:13:40627 keyword_history_results_.clear();
628 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29629
[email protected]78e5e432013-08-03 02:10:10630 if (OmniboxFieldTrial::SearchHistoryDisable(
631 input_.current_page_classification()))
[email protected]d8cd76b2013-07-10 09:46:16632 return;
633
[email protected]8d457132010-11-04 18:13:40634 HistoryService* const history_service =
[email protected]9d2db762012-06-19 00:01:10635 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
[email protected]8d457132010-11-04 18:13:40636 history::URLDatabase* url_db = history_service ?
637 history_service->InMemoryDatabase() : NULL;
638 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29639 return;
640
[email protected]51124552011-07-16 01:37:10641 // Request history for both the keyword and default provider. We grab many
642 // more matches than we'll ultimately clamp to so that if there are several
643 // recent multi-word matches who scores are lowered (see
644 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
645 // matches. Note that this doesn't fix the problem entirely, but merely
646 // limits it to cases with a very large number of such multi-word matches; for
647 // now, this seems OK compared with the complexity of a real fix, which would
648 // require multiple searches and tracking of "single- vs. multi-word" in the
649 // database.
650 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47651 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
652 if (default_url) {
653 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
654 num_matches, &default_history_results_);
[email protected]257ab712009-04-14 17:16:24655 }
[email protected]85b8d6f2012-05-08 20:53:47656 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
657 if (keyword_url) {
658 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]14710852013-02-05 23:45:41659 keyword_input_.text(), num_matches, &keyword_history_results_);
[email protected]3954c3a2012-04-10 20:17:55660 }
initial.commit09911bf2008-07-26 23:55:29661}
662
[email protected]6dc950f2012-07-16 19:49:08663void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]83c726482008-09-10 06:36:34664 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29665 StopSuggest();
[email protected]71b46152013-05-03 16:39:20666 ClearAllResults();
initial.commit09911bf2008-07-26 23:55:29667 return;
668 }
669
670 // For the minimal_changes case, if we finished the previous query and still
671 // have its results, or are allowed to keep running it, just do that, rather
672 // than starting a new query.
673 if (minimal_changes &&
[email protected]cc1526e2013-05-17 04:04:24674 (!default_results_.suggest_results.empty() ||
675 !default_results_.navigation_results.empty() ||
676 !keyword_results_.suggest_results.empty() ||
677 !keyword_results_.navigation_results.empty() ||
[email protected]ea3b9a502011-04-04 14:19:37678 (!done_ &&
679 input_.matches_requested() == AutocompleteInput::ALL_MATCHES)))
initial.commit09911bf2008-07-26 23:55:29680 return;
681
682 // We can't keep running any previous query, so halt it.
683 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42684
685 // Remove existing results that cannot inline autocomplete the new input.
[email protected]71b46152013-05-03 16:39:20686 RemoveAllStaleResults();
initial.commit09911bf2008-07-26 23:55:29687
688 // We can't start a new query if we're only allowed synchronous results.
[email protected]ea3b9a502011-04-04 14:19:37689 if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES)
initial.commit09911bf2008-07-26 23:55:29690 return;
691
[email protected]24dbf2302013-04-05 16:59:25692 // To avoid flooding the suggest server, don't send a query until at
693 // least 100 ms since the last query.
[email protected]515ffa942012-11-27 20:18:24694 base::TimeTicks next_suggest_time(time_suggest_request_sent_ +
[email protected]bc8bb0cd2013-06-24 21:50:23695 base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs));
[email protected]515ffa942012-11-27 20:18:24696 base::TimeTicks now(base::TimeTicks::Now());
697 if (now >= next_suggest_time) {
698 Run();
699 return;
700 }
701 timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29702}
703
[email protected]83c726482008-09-10 06:36:34704bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55705 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
706 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47707 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
708 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34709 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47710 ((!default_url || default_url->suggestions_url().empty()) &&
711 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34712 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
713 return false;
714
[email protected]cac59d32010-08-09 23:23:14715 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34716 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34717
[email protected]cac59d32010-08-09 23:23:14718 // FORCED_QUERY means the user is explicitly asking us to search for this, so
719 // we assume it isn't a URL and/or there isn't private data.
720 if (input_.type() == AutocompleteInput::FORCED_QUERY)
721 return true;
[email protected]83c726482008-09-10 06:36:34722
[email protected]f608ea102013-03-18 15:08:09723 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't
724 // http/https/ftp, we shouldn't send it. Sending things like file: and data:
725 // is both a waste of time and a disclosure of potentially private, local
726 // data. Other "schemes" may actually be usernames, and we don't want to send
727 // passwords. If the scheme is OK, we still need to check other cases below.
728 // If this is QUERY, then the presence of these schemes means the user
729 // explicitly typed one, and thus this is probably a URL that's being entered
730 // and happens to currently be invalid -- in which case we again want to run
731 // our checks below. Other QUERY cases are less likely to be URLs and thus we
732 // assume we're OK.
[email protected]a2fedb1e2011-01-25 15:23:36733 if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) &&
734 !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
735 !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme))
[email protected]cac59d32010-08-09 23:23:14736 return (input_.type() == AutocompleteInput::QUERY);
737
738 // Don't send URLs with usernames, queries or refs. Some of these are
739 // private, and the Suggest server is unlikely to have any useful results
740 // for any of them. Also don't send URLs with ports, as we may initially
741 // think that a username + password is a host + port (and we don't want to
742 // send usernames/passwords), and even if the port really is a port, the
743 // server is once again unlikely to have and useful results.
744 const url_parse::Parsed& parts = input_.parts();
745 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
746 parts.query.is_nonempty() || parts.ref.is_nonempty())
747 return false;
748
749 // Don't send anything for https except the hostname. Hostnames are OK
750 // because they are visible when the TCP connection is established, but the
751 // specific path may reveal private information.
[email protected]a2fedb1e2011-01-25 15:23:36752 if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
753 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14754 return false;
[email protected]83c726482008-09-10 06:36:34755
756 return true;
757}
758
initial.commit09911bf2008-07-26 23:55:29759void SearchProvider::StopSuggest() {
[email protected]7706a522012-08-16 17:42:25760 // Increment the appropriate field in the histogram by the number of
761 // pending requests that were invalidated.
762 for (int i = 0; i < suggest_results_pending_; i++)
763 LogOmniboxSuggestRequest(REQUEST_INVALIDATED);
[email protected]257ab712009-04-14 17:16:24764 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14765 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24766 // Stop any in-progress URL fetches.
767 keyword_fetcher_.reset();
768 default_fetcher_.reset();
[email protected]55ce8f12012-05-09 04:44:08769}
770
[email protected]71b46152013-05-03 16:39:20771void SearchProvider::ClearAllResults() {
[email protected]cc1526e2013-05-17 04:04:24772 keyword_results_.Clear();
773 default_results_.Clear();
[email protected]1e1550e2013-05-02 17:37:51774}
775
[email protected]71b46152013-05-03 16:39:20776void SearchProvider::RemoveAllStaleResults() {
[email protected]1e1550e2013-05-02 17:37:51777 // In theory it would be better to run an algorithm like that in
778 // RemoveStaleResults(...) below that uses all four results lists
779 // and both verbatim scores at once. However, that will be much
780 // more complicated for little obvious gain. For code simplicity
781 // and ease in reasoning about the invariants involved, this code
782 // removes stales results from the keyword provider and default
783 // provider independently.
[email protected]d30268a2013-06-25 22:31:07784 RemoveStaleResults(input_.text(), GetVerbatimRelevance(NULL),
[email protected]cc1526e2013-05-17 04:04:24785 &default_results_.suggest_results,
786 &default_results_.navigation_results);
[email protected]1e1550e2013-05-02 17:37:51787 if (!keyword_input_.text().empty()) {
[email protected]d30268a2013-06-25 22:31:07788 RemoveStaleResults(keyword_input_.text(), GetKeywordVerbatimRelevance(NULL),
[email protected]cc1526e2013-05-17 04:04:24789 &keyword_results_.suggest_results,
790 &keyword_results_.navigation_results);
[email protected]1e1550e2013-05-02 17:37:51791 } else {
792 // User is either in keyword mode with a blank input or out of
793 // keyword mode entirely.
[email protected]cc1526e2013-05-17 04:04:24794 keyword_results_.Clear();
[email protected]1e1550e2013-05-02 17:37:51795 }
[email protected]d1f0a7f2012-06-05 10:26:42796}
797
[email protected]d1f0a7f2012-06-05 10:26:42798void SearchProvider::ApplyCalculatedRelevance() {
[email protected]cc1526e2013-05-17 04:04:24799 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
800 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
801 ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results);
802 ApplyCalculatedNavigationRelevance(&default_results_.navigation_results);
[email protected]cc1526e2013-05-17 04:04:24803 default_results_.verbatim_relevance = -1;
804 keyword_results_.verbatim_relevance = -1;
[email protected]d1f0a7f2012-06-05 10:26:42805}
806
[email protected]188b50c2013-03-28 07:19:42807void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42808 for (size_t i = 0; i < list->size(); ++i) {
[email protected]188b50c2013-03-28 07:19:42809 SuggestResult& result = (*list)[i];
810 result.set_relevance(
811 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
812 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07813 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42814 }
815}
816
[email protected]188b50c2013-03-28 07:19:42817void SearchProvider::ApplyCalculatedNavigationRelevance(
818 NavigationResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42819 for (size_t i = 0; i < list->size(); ++i) {
[email protected]188b50c2013-03-28 07:19:42820 NavigationResult& result = (*list)[i];
821 result.set_relevance(
822 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
823 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07824 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42825 }
826}
827
[email protected]15fb2aa2012-05-22 22:52:59828net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12829 int id,
[email protected]9ff91722012-09-07 05:29:12830 const TemplateURL* template_url,
[email protected]14710852013-02-05 23:45:41831 const AutocompleteInput& input) {
[email protected]9ff91722012-09-07 05:29:12832 if (!template_url || template_url->suggestions_url().empty())
833 return NULL;
834
835 // Bail if the suggestion URL is invalid with the given replacements.
[email protected]14710852013-02-05 23:45:41836 TemplateURLRef::SearchTermsArgs search_term_args(input.text());
837 search_term_args.cursor_position = input.cursor_position();
[email protected]d5015ca2013-08-08 22:04:18838 search_term_args.page_classification = input.current_page_classification();
[email protected]9ff91722012-09-07 05:29:12839 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]00790562012-12-14 09:57:16840 search_term_args));
[email protected]9ff91722012-09-07 05:29:12841 if (!suggest_url.is_valid())
842 return NULL;
843
844 suggest_results_pending_++;
845 LogOmniboxSuggestRequest(REQUEST_SENT);
846
847 net::URLFetcher* fetcher =
848 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12849 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34850 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]bd3b4712012-12-18 17:01:30851 // Add Chrome experiment state to the request headers.
852 net::HttpRequestHeaders headers;
[email protected]ab7780792013-01-10 01:26:09853 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
[email protected]bd3b4712012-12-18 17:01:30854 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
855 fetcher->SetExtraRequestHeaders(headers.ToString());
[email protected]257ab712009-04-14 17:16:24856 fetcher->Start();
857 return fetcher;
858}
859
[email protected]d7ad4772012-06-01 03:12:54860bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) {
[email protected]d7ad4772012-06-01 03:12:54861 string16 query;
862 ListValue* root_list = NULL;
[email protected]cc1526e2013-05-17 04:04:24863 ListValue* results_list = NULL;
[email protected]14710852013-02-05 23:45:41864 const string16& input_text =
865 is_keyword ? keyword_input_.text() : input_.text();
[email protected]d7ad4772012-06-01 03:12:54866 if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) ||
[email protected]cc1526e2013-05-17 04:04:24867 (query != input_text) || !root_list->GetList(1, &results_list))
initial.commit09911bf2008-07-26 23:55:29868 return false;
869
[email protected]55ce8f12012-05-09 04:44:08870 // 3rd element: Description list.
[email protected]d7ad4772012-06-01 03:12:54871 ListValue* descriptions = NULL;
872 root_list->GetList(2, &descriptions);
initial.commit09911bf2008-07-26 23:55:29873
[email protected]55ce8f12012-05-09 04:44:08874 // 4th element: Disregard the query URL list for now.
initial.commit09911bf2008-07-26 23:55:29875
[email protected]d1f0a7f2012-06-05 10:26:42876 // Reset suggested relevance information from the default provider.
[email protected]cc1526e2013-05-17 04:04:24877 Results* results = is_keyword ? &keyword_results_ : &default_results_;
[email protected]cc1526e2013-05-17 04:04:24878 results->verbatim_relevance = -1;
[email protected]d1f0a7f2012-06-05 10:26:42879
[email protected]55ce8f12012-05-09 04:44:08880 // 5th element: Optional key-value pairs from the Suggest server.
[email protected]d7ad4772012-06-01 03:12:54881 ListValue* types = NULL;
[email protected]d1f0a7f2012-06-05 10:26:42882 ListValue* relevances = NULL;
883 DictionaryValue* extras = NULL;
884 if (root_list->GetDictionary(4, &extras)) {
885 extras->GetList("google:suggesttype", &types);
886
[email protected]eb8330d2013-06-15 00:55:08887 // Discard this list if its size does not match that of the suggestions.
888 if (extras->GetList("google:suggestrelevance", &relevances) &&
889 relevances->GetSize() != results_list->GetSize())
890 relevances = NULL;
891 extras->GetInteger("google:verbatimrelevance",
892 &results->verbatim_relevance);
[email protected]618d6e62012-12-16 05:55:57893
894 // Check if the active suggest field trial (if any) has triggered either
895 // for the default provider or keyword provider.
896 bool triggered = false;
897 extras->GetBoolean("google:fieldtrialtriggered", &triggered);
898 field_trial_triggered_ |= triggered;
899 field_trial_triggered_in_session_ |= triggered;
initial.commit09911bf2008-07-26 23:55:29900 }
901
[email protected]d1f0a7f2012-06-05 10:26:42902 // Clear the previous results now that new results are available.
[email protected]cc1526e2013-05-17 04:04:24903 results->suggest_results.clear();
904 results->navigation_results.clear();
[email protected]d1f0a7f2012-06-05 10:26:42905
906 string16 result, title;
907 std::string type;
908 int relevance = -1;
[email protected]cc1526e2013-05-17 04:04:24909 for (size_t index = 0; results_list->GetString(index, &result); ++index) {
[email protected]8e81f5092010-09-29 23:19:40910 // Google search may return empty suggestions for weird input characters,
[email protected]55ce8f12012-05-09 04:44:08911 // they make no sense at all and can cause problems in our code.
[email protected]d7ad4772012-06-01 03:12:54912 if (result.empty())
[email protected]8e81f5092010-09-29 23:19:40913 continue;
914
[email protected]d1f0a7f2012-06-05 10:26:42915 // Apply valid suggested relevance scores; discard invalid lists.
916 if (relevances != NULL && !relevances->GetInteger(index, &relevance))
917 relevances = NULL;
[email protected]d7ad4772012-06-01 03:12:54918 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) {
[email protected]d1f0a7f2012-06-05 10:26:42919 // Do not blindly trust the URL coming from the server to be valid.
920 GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(result), std::string()));
921 if (url.is_valid()) {
922 if (descriptions != NULL)
923 descriptions->GetString(index, &title);
[email protected]d30268a2013-06-25 22:31:07924 results->navigation_results.push_back(NavigationResult(
925 *this, url, title, is_keyword, relevance, true));
initial.commit09911bf2008-07-26 23:55:29926 }
927 } else {
[email protected]d1f0a7f2012-06-05 10:26:42928 // TODO(kochi): Improve calculator result presentation.
[email protected]cc1526e2013-05-17 04:04:24929 results->suggest_results.push_back(
[email protected]d30268a2013-06-25 22:31:07930 SuggestResult(result, is_keyword, relevance, true));
initial.commit09911bf2008-07-26 23:55:29931 }
932 }
933
[email protected]d1f0a7f2012-06-05 10:26:42934 // Apply calculated relevance scores if a valid list was not provided.
935 if (relevances == NULL) {
[email protected]cc1526e2013-05-17 04:04:24936 ApplyCalculatedSuggestRelevance(&results->suggest_results);
937 ApplyCalculatedNavigationRelevance(&results->navigation_results);
[email protected]d1f0a7f2012-06-05 10:26:42938 }
[email protected]9dfb4d362013-04-05 02:15:12939 // Keep the result lists sorted.
940 const CompareScoredResults comparator = CompareScoredResults();
[email protected]cc1526e2013-05-17 04:04:24941 std::stable_sort(results->suggest_results.begin(),
942 results->suggest_results.end(),
[email protected]9dfb4d362013-04-05 02:15:12943 comparator);
[email protected]cc1526e2013-05-17 04:04:24944 std::stable_sort(results->navigation_results.begin(),
945 results->navigation_results.end(),
[email protected]9dfb4d362013-04-05 02:15:12946 comparator);
[email protected]d1f0a7f2012-06-05 10:26:42947 return true;
initial.commit09911bf2008-07-26 23:55:29948}
949
[email protected]344946a12012-12-20 12:03:42950void SearchProvider::ConvertResultsToAutocompleteMatches() {
initial.commit09911bf2008-07-26 23:55:29951 // Convert all the results to matches and add them to a map, so we can keep
952 // the most relevant match for each result.
953 MatchMap map;
[email protected]bc8bb0cd2013-06-24 21:50:23954 const base::Time no_time;
[email protected]cc1526e2013-05-17 04:04:24955 int did_not_accept_keyword_suggestion =
956 keyword_results_.suggest_results.empty() ?
initial.commit09911bf2008-07-26 23:55:29957 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
958 TemplateURLRef::NO_SUGGESTION_CHOSEN;
initial.commit09911bf2008-07-26 23:55:29959
[email protected]d30268a2013-06-25 22:31:07960 bool relevance_from_server;
961 int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server);
[email protected]cc1526e2013-05-17 04:04:24962 int did_not_accept_default_suggestion =
963 default_results_.suggest_results.empty() ?
[email protected]55ce8f12012-05-09 04:44:08964 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
965 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]d1f0a7f2012-06-05 10:26:42966 if (verbatim_relevance > 0) {
967 AddMatchToMap(input_.text(), input_.text(), verbatim_relevance,
[email protected]d30268a2013-06-25 22:31:07968 relevance_from_server,
[email protected]b7f64d742013-05-21 04:04:04969 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
[email protected]d1f0a7f2012-06-05 10:26:42970 did_not_accept_default_suggestion, false, &map);
971 }
[email protected]5423e562013-02-07 03:58:45972 if (!keyword_input_.text().empty()) {
973 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
974 // We only create the verbatim search query match for a keyword
975 // if it's not an extension keyword. Extension keywords are handled
976 // in KeywordProvider::Start(). (Extensions are complicated...)
977 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond
978 // to the keyword verbatim search query. Do not create other matches
979 // of type SEARCH_OTHER_ENGINE.
980 if (keyword_url && !keyword_url->IsExtensionKeyword()) {
[email protected]d30268a2013-06-25 22:31:07981 bool keyword_relevance_from_server;
982 const int keyword_verbatim_relevance =
983 GetKeywordVerbatimRelevance(&keyword_relevance_from_server);
[email protected]dab8d52d2013-03-05 07:35:28984 if (keyword_verbatim_relevance > 0) {
985 AddMatchToMap(keyword_input_.text(), keyword_input_.text(),
[email protected]d30268a2013-06-25 22:31:07986 keyword_verbatim_relevance, keyword_relevance_from_server,
[email protected]b7f64d742013-05-21 04:04:04987 AutocompleteMatchType::SEARCH_OTHER_ENGINE,
[email protected]dab8d52d2013-03-05 07:35:28988 did_not_accept_keyword_suggestion, true, &map);
989 }
[email protected]5423e562013-02-07 03:58:45990 }
991 }
[email protected]257ab712009-04-14 17:16:24992 AddHistoryResultsToMap(keyword_history_results_, true,
993 did_not_accept_keyword_suggestion, &map);
994 AddHistoryResultsToMap(default_history_results_, false,
995 did_not_accept_default_suggestion, &map);
996
[email protected]cc1526e2013-05-17 04:04:24997 AddSuggestResultsToMap(keyword_results_.suggest_results, &map);
998 AddSuggestResultsToMap(default_results_.suggest_results, &map);
initial.commit09911bf2008-07-26 23:55:29999
[email protected]d30268a2013-06-25 22:31:071000 ACMatches matches;
initial.commit09911bf2008-07-26 23:55:291001 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
[email protected]d30268a2013-06-25 22:31:071002 matches.push_back(i->second);
initial.commit09911bf2008-07-26 23:55:291003
[email protected]d30268a2013-06-25 22:31:071004 AddNavigationResultsToMatches(keyword_results_.navigation_results, &matches);
1005 AddNavigationResultsToMatches(default_results_.navigation_results, &matches);
initial.commit09911bf2008-07-26 23:55:291006
[email protected]d30268a2013-06-25 22:31:071007 // Now add the most relevant matches to |matches_|. We take up to kMaxMatches
1008 // suggest/navsuggest matches, regardless of origin. If Instant Extended is
1009 // enabled and we have server-provided (and thus hopefully more accurate)
1010 // scores for some suggestions, we allow more of those, until we reach
1011 // AutocompleteResult::kMaxMatches total matches (that is, enough to fill the
1012 // whole popup).
1013 //
1014 // We will always return any verbatim matches, no matter how we obtained their
1015 // scores, unless we have already accepted AutocompleteResult::kMaxMatches
1016 // higher-scoring matches under the conditions above.
1017 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant);
1018 matches_.clear();
[email protected]3723e6e2012-06-11 21:06:561019
[email protected]d30268a2013-06-25 22:31:071020 size_t num_suggestions = 0;
1021 for (ACMatches::const_iterator i(matches.begin());
1022 (i != matches.end()) &&
1023 (matches_.size() < AutocompleteResult::kMaxMatches);
1024 ++i) {
1025 // SEARCH_OTHER_ENGINE is only used in the SearchProvider for the keyword
1026 // verbatim result, so this condition basically means "if this match is a
1027 // suggestion of some sort".
1028 if ((i->type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED) &&
1029 (i->type != AutocompleteMatchType::SEARCH_OTHER_ENGINE)) {
1030 // If we've already hit the limit on non-server-scored suggestions, and
1031 // this isn't a server-scored suggestion we can add, skip it.
1032 if ((num_suggestions >= kMaxMatches) &&
1033 (!chrome::IsInstantExtendedAPIEnabled() ||
1034 (i->GetAdditionalInfo(kRelevanceFromServerKey) != kTrue))) {
1035 continue;
1036 }
1037
1038 ++num_suggestions;
1039 }
1040
1041 matches_.push_back(*i);
1042 }
[email protected]344946a12012-12-20 12:03:421043}
1044
[email protected]9dfb4d362013-04-05 02:15:121045bool SearchProvider::IsTopMatchNavigationInKeywordMode() const {
1046 return (!providers_.keyword_provider().empty() &&
[email protected]b7f64d742013-05-21 04:04:041047 (matches_.front().type == AutocompleteMatchType::NAVSUGGEST));
[email protected]9dfb4d362013-04-05 02:15:121048}
1049
[email protected]344946a12012-12-20 12:03:421050bool SearchProvider::IsTopMatchScoreTooLow() const {
[email protected]dab8d52d2013-03-05 07:35:281051 // Here we use CalculateRelevanceForVerbatimIgnoringKeywordModeState()
1052 // rather than CalculateRelevanceForVerbatim() because the latter returns
1053 // a very low score (250) if keyword mode is active. This is because
1054 // when keyword mode is active the user probably wants the keyword matches,
1055 // not matches from the default provider. Hence, we use the version of
1056 // the function that ignores whether keyword mode is active. This allows
1057 // SearchProvider to maintain its contract with the AutocompleteController
1058 // that it will always provide an inlineable match with a reasonable
1059 // score.
1060 return matches_.front().relevance <
1061 CalculateRelevanceForVerbatimIgnoringKeywordModeState();
[email protected]344946a12012-12-20 12:03:421062}
1063
[email protected]fb95251e2013-07-17 14:52:241064bool SearchProvider::IsTopMatchSearchWithURLInput() const {
[email protected]344946a12012-12-20 12:03:421065 return input_.type() == AutocompleteInput::URL &&
1066 matches_.front().relevance > CalculateRelevanceForVerbatim() &&
[email protected]fb95251e2013-07-17 14:52:241067 matches_.front().type != AutocompleteMatchType::NAVSUGGEST;
[email protected]344946a12012-12-20 12:03:421068}
1069
[email protected]45f89a92013-08-12 13:41:361070bool SearchProvider::HasValidDefaultMatch(
1071 bool autocomplete_result_will_reorder_for_default_match) const {
1072 // One of the SearchProvider matches may need to be the overall default. If
1073 // AutocompleteResult is allowed to reorder matches, this means we simply
1074 // need at least one match in the list to be |allowed_to_be_default_match|.
1075 // If no reordering is possible, however, then our first match needs to have
1076 // this flag.
1077 for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end();
1078 ++it) {
1079 if (it->allowed_to_be_default_match)
1080 return true;
1081 if (!autocomplete_result_will_reorder_for_default_match)
1082 return false;
1083 }
1084 return false;
[email protected]344946a12012-12-20 12:03:421085}
1086
1087void SearchProvider::UpdateMatches() {
1088 ConvertResultsToAutocompleteMatches();
initial.commit09911bf2008-07-26 23:55:291089
[email protected]382a0642012-06-06 06:13:521090 // Check constraints that may be violated by suggested relevances.
[email protected]344946a12012-12-20 12:03:421091 if (!matches_.empty() &&
[email protected]bc8bb0cd2013-06-24 21:50:231092 (default_results_.HasServerProvidedScores() ||
1093 keyword_results_.HasServerProvidedScores())) {
[email protected]9dfb4d362013-04-05 02:15:121094 // These blocks attempt to repair undesirable behavior by suggested
[email protected]344946a12012-12-20 12:03:421095 // relevances with minimal impact, preserving other suggested relevances.
[email protected]9dfb4d362013-04-05 02:15:121096 if (IsTopMatchNavigationInKeywordMode()) {
1097 // Correct the suggested relevance scores if the top match is a
1098 // navigation in keyword mode, since inlining a navigation match
1099 // would break the user out of keyword mode. By the way, if the top
1100 // match is a non-keyword match (query or navsuggestion) in keyword
1101 // mode, the user would also break out of keyword mode. However,
1102 // that situation is impossible given the current scoring paradigm
1103 // and the fact that only one search engine (Google) provides suggested
1104 // relevance scores at this time.
1105 DemoteKeywordNavigationMatchesPastTopQuery();
1106 ConvertResultsToAutocompleteMatches();
1107 DCHECK(!IsTopMatchNavigationInKeywordMode());
1108 }
[email protected]45f89a92013-08-12 13:41:361109 // True if the omnibox will reorder matches as necessary to make the top
1110 // one something that is allowed to be the default match.
1111 const bool omnibox_will_reorder_for_legal_default_match =
1112 OmniboxFieldTrial::ReorderForLegalDefaultMatch(
1113 input_.current_page_classification());
1114 if (!omnibox_will_reorder_for_legal_default_match &&
1115 IsTopMatchScoreTooLow()) {
[email protected]1beee342012-06-19 22:22:281116 // Disregard the suggested verbatim relevance if the top score is below
1117 // the usual verbatim value. For example, a BarProvider may rely on
[email protected]45f89a92013-08-12 13:41:361118 // SearchProvider's verbatim or inlineable matches for input "foo" (all
1119 // allowed to be default match) to always outrank its own lowly-ranked
1120 // "bar" matches that shouldn't be the default match. This only needs
1121 // to be enforced when the omnibox will not reorder results to make a
1122 // legal default match first.
[email protected]cc1526e2013-05-17 04:04:241123 default_results_.verbatim_relevance = -1;
1124 keyword_results_.verbatim_relevance = -1;
[email protected]344946a12012-12-20 12:03:421125 ConvertResultsToAutocompleteMatches();
[email protected]e6acd002012-06-16 22:27:471126 }
[email protected]fb95251e2013-07-17 14:52:241127 if (IsTopMatchSearchWithURLInput()) {
[email protected]e6acd002012-06-16 22:27:471128 // Disregard the suggested search and verbatim relevances if the input
1129 // type is URL and the top match is a highly-ranked search suggestion.
1130 // For example, prevent a search for "foo.com" from outranking another
1131 // provider's navigation for "foo.com" or "foo.com/url_from_history".
[email protected]cc1526e2013-05-17 04:04:241132 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
1133 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
1134 default_results_.verbatim_relevance = -1;
1135 keyword_results_.verbatim_relevance = -1;
[email protected]344946a12012-12-20 12:03:421136 ConvertResultsToAutocompleteMatches();
[email protected]382a0642012-06-06 06:13:521137 }
[email protected]45f89a92013-08-12 13:41:361138 if (!HasValidDefaultMatch(omnibox_will_reorder_for_legal_default_match)) {
1139 // If the omnibox is not going to reorder results to put a legal default
1140 // match at the top, then this provider needs to guarantee that its top
1141 // scoring result is a legal default match (i.e., it's either a verbatim
1142 // match or inlinable). For example, input "foo" should not invoke a
1143 // search for "bar", which would happen if the "bar" search match
1144 // outranked all other matches. On the other hand, if the omnibox will
1145 // reorder matches as necessary to put a legal default match at the top,
1146 // all we need to guarantee is that SearchProvider returns a legal
1147 // default match. (The omnibox always needs at least one legal default
1148 // match, and it relies on SearchProvider to always return one.)
[email protected]344946a12012-12-20 12:03:421149 ApplyCalculatedRelevance();
1150 ConvertResultsToAutocompleteMatches();
[email protected]382a0642012-06-06 06:13:521151 }
[email protected]9dfb4d362013-04-05 02:15:121152 DCHECK(!IsTopMatchNavigationInKeywordMode());
[email protected]45f89a92013-08-12 13:41:361153 DCHECK(omnibox_will_reorder_for_legal_default_match ||
1154 !IsTopMatchScoreTooLow());
[email protected]fb95251e2013-07-17 14:52:241155 DCHECK(!IsTopMatchSearchWithURLInput());
[email protected]45f89a92013-08-12 13:41:361156 DCHECK(HasValidDefaultMatch(omnibox_will_reorder_for_legal_default_match));
[email protected]d1f0a7f2012-06-05 10:26:421157 }
1158
[email protected]cc63dea2008-08-21 20:56:311159 UpdateStarredStateOfMatches();
[email protected]4ab4c7c2010-11-24 04:49:341160 UpdateDone();
[email protected]257ab712009-04-14 17:16:241161}
1162
1163void SearchProvider::AddNavigationResultsToMatches(
[email protected]d30268a2013-06-25 22:31:071164 const NavigationResults& navigation_results,
1165 ACMatches* matches) {
[email protected]bc8bb0cd2013-06-24 21:50:231166 for (NavigationResults::const_iterator it = navigation_results.begin();
1167 it != navigation_results.end(); ++it) {
[email protected]d30268a2013-06-25 22:31:071168 matches->push_back(NavigationToMatch(*it));
[email protected]bc8bb0cd2013-06-24 21:50:231169 // In the absence of suggested relevance scores, use only the single
1170 // highest-scoring result. (The results are already sorted by relevance.)
[email protected]d30268a2013-06-25 22:31:071171 if (!it->relevance_from_server())
[email protected]bc8bb0cd2013-06-24 21:50:231172 return;
[email protected]257ab712009-04-14 17:16:241173 }
1174}
1175
1176void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
1177 bool is_keyword,
1178 int did_not_accept_suggestion,
1179 MatchMap* map) {
[email protected]51124552011-07-16 01:37:101180 if (results.empty())
1181 return;
1182
[email protected]d7ad4772012-06-01 03:12:541183 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
1184 (input_.type() == AutocompleteInput::URL);
[email protected]14710852013-02-05 23:45:411185 const string16& input_text =
1186 is_keyword ? keyword_input_.text() : input_.text();
[email protected]51124552011-07-16 01:37:101187 bool input_multiple_words = HasMultipleWords(input_text);
1188
[email protected]55ce8f12012-05-09 04:44:081189 SuggestResults scored_results;
1190 if (!prevent_inline_autocomplete && input_multiple_words) {
1191 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
1192 // queries if the input also has multiple words. But if we were already
[email protected]51124552011-07-16 01:37:101193 // autocompleting a multi-word, multi-visit query, and the current input is
1194 // still a prefix of it, then changing the autocompletion suddenly feels
1195 // wrong. To detect this case, first score as if only one word has been
1196 // typed, then check for a best result that is an autocompleted, multi-word
1197 // query. If we find one, then just keep that score set.
[email protected]55ce8f12012-05-09 04:44:081198 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
1199 false, input_text, is_keyword);
[email protected]9dfb4d362013-04-05 02:15:121200 if ((scored_results.front().relevance() <
[email protected]55ce8f12012-05-09 04:44:081201 AutocompleteResult::kLowestDefaultScore) ||
[email protected]9dfb4d362013-04-05 02:15:121202 !HasMultipleWords(scored_results.front().suggestion()))
[email protected]55ce8f12012-05-09 04:44:081203 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:101204 }
[email protected]55ce8f12012-05-09 04:44:081205 if (scored_results.empty())
1206 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
1207 input_multiple_words, input_text,
1208 is_keyword);
1209 for (SuggestResults::const_iterator i(scored_results.begin());
1210 i != scored_results.end(); ++i) {
[email protected]d30268a2013-06-25 22:31:071211 AddMatchToMap(i->suggestion(), input_text, i->relevance(), false,
[email protected]b7f64d742013-05-21 04:04:041212 AutocompleteMatchType::SEARCH_HISTORY,
1213 did_not_accept_suggestion,
[email protected]55ce8f12012-05-09 04:44:081214 is_keyword, map);
[email protected]51124552011-07-16 01:37:101215 }
1216}
1217
[email protected]55ce8f12012-05-09 04:44:081218SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:101219 const HistoryResults& results,
1220 bool base_prevent_inline_autocomplete,
1221 bool input_multiple_words,
1222 const string16& input_text,
1223 bool is_keyword) {
[email protected]810ffba2012-06-12 01:07:481224 AutocompleteClassifier* classifier =
1225 AutocompleteClassifierFactory::GetForProfile(profile_);
[email protected]55ce8f12012-05-09 04:44:081226 SuggestResults scored_results;
[email protected]78e5e432013-08-03 02:10:101227 const bool prevent_search_history_inlining =
1228 OmniboxFieldTrial::SearchHistoryPreventInlining(
1229 input_.current_page_classification());
[email protected]257ab712009-04-14 17:16:241230 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
1231 ++i) {
[email protected]51124552011-07-16 01:37:101232 // Don't autocomplete multi-word queries that have only been seen once
1233 // unless the user has typed more than one word.
1234 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
1235 (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term));
1236
[email protected]ea3b9a502011-04-04 14:19:371237 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:101238 // when typed. For example, if the user searched for "google.com" and types
1239 // "goog", don't autocomplete to the search term "google.com". Otherwise,
1240 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:481241 // NOTE: We don't check this in the following cases:
1242 // * When inline autocomplete is disabled, we won't be inline
1243 // autocompleting this term, so we don't need to worry about confusion as
1244 // much. This also prevents calling Classify() again from inside the
1245 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:101246 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:481247 // * When the user has typed the whole term, the "what you typed" history
1248 // match will outrank us for URL-like inputs anyway, so we need not do
1249 // anything special.
[email protected]51124552011-07-16 01:37:101250 if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) {
[email protected]ea3b9a502011-04-04 14:19:371251 AutocompleteMatch match;
[email protected]f608ea102013-03-18 15:08:091252 classifier->Classify(i->term, false, false, &match, NULL);
[email protected]2905f742011-10-13 03:51:581253 prevent_inline_autocomplete =
[email protected]749e7ae02012-09-05 18:47:461254 !AutocompleteMatch::IsSearchType(match.type);
[email protected]ea3b9a502011-04-04 14:19:371255 }
[email protected]51124552011-07-16 01:37:101256
[email protected]78e5e432013-08-03 02:10:101257 int relevance = CalculateRelevanceForHistory(
1258 i->time, is_keyword, !prevent_inline_autocomplete,
1259 prevent_search_history_inlining);
[email protected]d30268a2013-06-25 22:31:071260 scored_results.push_back(
1261 SuggestResult(i->term, is_keyword, relevance, false));
[email protected]257ab712009-04-14 17:16:241262 }
[email protected]51124552011-07-16 01:37:101263
1264 // History returns results sorted for us. However, we may have docked some
1265 // results' scores, so things are no longer in order. Do a stable sort to get
1266 // things back in order without otherwise disturbing results with equal
1267 // scores, then force the scores to be unique, so that the order in which
1268 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:081269 std::stable_sort(scored_results.begin(), scored_results.end(),
1270 CompareScoredResults());
[email protected]51124552011-07-16 01:37:101271 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:081272 for (SuggestResults::iterator i(scored_results.begin());
1273 i != scored_results.end(); ++i) {
1274 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
1275 i->set_relevance(last_relevance - 1);
1276 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:101277 }
1278
[email protected]55ce8f12012-05-09 04:44:081279 return scored_results;
[email protected]257ab712009-04-14 17:16:241280}
1281
[email protected]55ce8f12012-05-09 04:44:081282void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
[email protected]55ce8f12012-05-09 04:44:081283 MatchMap* map) {
[email protected]55ce8f12012-05-09 04:44:081284 for (size_t i = 0; i < results.size(); ++i) {
[email protected]188b50c2013-03-28 07:19:421285 const bool is_keyword = results[i].from_keyword_provider();
1286 const string16& input = is_keyword ? keyword_input_.text() : input_.text();
1287 AddMatchToMap(results[i].suggestion(), input, results[i].relevance(),
[email protected]d30268a2013-06-25 22:31:071288 results[i].relevance_from_server(),
[email protected]b7f64d742013-05-21 04:04:041289 AutocompleteMatchType::SEARCH_SUGGEST, i, is_keyword, map);
[email protected]257ab712009-04-14 17:16:241290 }
initial.commit09911bf2008-07-26 23:55:291291}
1292
[email protected]d30268a2013-06-25 22:31:071293int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const {
[email protected]dc6943b2012-06-19 06:39:561294 // Use the suggested verbatim relevance score if it is non-negative (valid),
1295 // if inline autocomplete isn't prevented (always show verbatim on backspace),
[email protected]1beee342012-06-19 22:22:281296 // and if it won't suppress verbatim, leaving no default provider matches.
1297 // Otherwise, if the default provider returned no matches and was still able
[email protected]dc6943b2012-06-19 06:39:561298 // to suppress verbatim, the user would have no search/nav matches and may be
[email protected]1beee342012-06-19 22:22:281299 // left unable to search using their default provider from the omnibox.
[email protected]dc6943b2012-06-19 06:39:561300 // Check for results on each verbatim calculation, as results from older
1301 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:231302 bool use_server_relevance =
1303 (default_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:281304 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:231305 ((default_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:241306 !default_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:231307 !default_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:071308 if (relevance_from_server)
1309 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:231310 return use_server_relevance ?
1311 default_results_.verbatim_relevance : CalculateRelevanceForVerbatim();
[email protected]382a0642012-06-06 06:13:521312}
[email protected]d1f0a7f2012-06-05 10:26:421313
[email protected]382a0642012-06-06 06:13:521314int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:471315 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:361316 return 250;
[email protected]dab8d52d2013-03-05 07:35:281317 return CalculateRelevanceForVerbatimIgnoringKeywordModeState();
1318}
[email protected]52d08b12009-10-19 18:42:361319
[email protected]dab8d52d2013-03-05 07:35:281320int SearchProvider::
1321 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const {
initial.commit09911bf2008-07-26 23:55:291322 switch (input_.type()) {
1323 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:361324 case AutocompleteInput::QUERY:
1325 case AutocompleteInput::FORCED_QUERY:
[email protected]90fe2bb2013-01-15 03:42:131326 return kNonURLVerbatimRelevance;
initial.commit09911bf2008-07-26 23:55:291327
initial.commit09911bf2008-07-26 23:55:291328 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:361329 return 850;
initial.commit09911bf2008-07-26 23:55:291330
1331 default:
1332 NOTREACHED();
1333 return 0;
1334 }
1335}
1336
[email protected]d30268a2013-06-25 22:31:071337int SearchProvider::GetKeywordVerbatimRelevance(
1338 bool* relevance_from_server) const {
[email protected]dab8d52d2013-03-05 07:35:281339 // Use the suggested verbatim relevance score if it is non-negative (valid),
1340 // if inline autocomplete isn't prevented (always show verbatim on backspace),
1341 // and if it won't suppress verbatim, leaving no keyword provider matches.
1342 // Otherwise, if the keyword provider returned no matches and was still able
1343 // to suppress verbatim, the user would have no search/nav matches and may be
1344 // left unable to search using their keyword provider from the omnibox.
1345 // Check for results on each verbatim calculation, as results from older
1346 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:231347 bool use_server_relevance =
1348 (keyword_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:281349 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:231350 ((keyword_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:241351 !keyword_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:231352 !keyword_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:071353 if (relevance_from_server)
1354 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:231355 return use_server_relevance ?
1356 keyword_results_.verbatim_relevance :
1357 CalculateRelevanceForKeywordVerbatim(keyword_input_.type(),
1358 keyword_input_.prefer_keyword());
[email protected]5423e562013-02-07 03:58:451359}
1360
[email protected]51124552011-07-16 01:37:101361int SearchProvider::CalculateRelevanceForHistory(
[email protected]bc8bb0cd2013-06-24 21:50:231362 const base::Time& time,
[email protected]51124552011-07-16 01:37:101363 bool is_keyword,
[email protected]78e5e432013-08-03 02:10:101364 bool use_aggressive_method,
1365 bool prevent_search_history_inlining) const {
[email protected]aa613d62010-11-09 20:40:181366 // The relevance of past searches falls off over time. There are two distinct
1367 // equations used. If the first equation is used (searches to the primary
[email protected]78e5e432013-08-03 02:10:101368 // provider that we want to score aggressively), the score is in the range
1369 // 1300-1599 (unless |prevent_search_history_inlining|, in which case
[email protected]d8cd76b2013-07-10 09:46:161370 // it's in the range 1200-1299). If the second equation is used the
1371 // relevance of a search 15 minutes ago is discounted 50 points, while the
1372 // relevance of a search two weeks ago is discounted 450 points.
[email protected]bc8bb0cd2013-06-24 21:50:231373 double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0);
[email protected]188b50c2013-03-28 07:19:421374 bool is_primary_provider = is_keyword || !providers_.has_keyword_provider();
[email protected]78e5e432013-08-03 02:10:101375 if (is_primary_provider && use_aggressive_method) {
[email protected]aa613d62010-11-09 20:40:181376 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:101377 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:181378 if (elapsed_time < autocomplete_time) {
[email protected]d8cd76b2013-07-10 09:46:161379 int max_score = is_keyword ? 1599 : 1399;
[email protected]78e5e432013-08-03 02:10:101380 if (prevent_search_history_inlining)
[email protected]d8cd76b2013-07-10 09:46:161381 max_score = 1299;
1382 return max_score - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:181383 std::pow(elapsed_time / autocomplete_time, 2.5));
1384 }
1385 elapsed_time -= autocomplete_time;
1386 }
1387
[email protected]c3a4bd992010-08-18 20:25:011388 const int score_discount =
1389 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:291390
[email protected]6c85aa02009-02-27 12:08:091391 // Don't let scores go below 0. Negative relevance scores are meaningful in
1392 // a different way.
initial.commit09911bf2008-07-26 23:55:291393 int base_score;
[email protected]51124552011-07-16 01:37:101394 if (is_primary_provider)
[email protected]52d08b12009-10-19 18:42:361395 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:101396 else
1397 base_score = 200;
initial.commit09911bf2008-07-26 23:55:291398 return std::max(0, base_score - score_discount);
1399}
1400
[email protected]a2fedb1e2011-01-25 15:23:361401void SearchProvider::AddMatchToMap(const string16& query_string,
1402 const string16& input_text,
initial.commit09911bf2008-07-26 23:55:291403 int relevance,
[email protected]d30268a2013-06-25 22:31:071404 bool relevance_from_server,
[email protected]4c1fb7ec2008-11-13 00:19:001405 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:291406 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:241407 bool is_keyword,
initial.commit09911bf2008-07-26 23:55:291408 MatchMap* map) {
[email protected]c4a9d4c2013-08-10 07:06:271409 // On non-mobile, ask the instant controller for the appropriate start margin.
1410 // On mobile the start margin is unused, so leave the value as default there.
1411 int omnibox_start_margin = chrome::kDisableStartMargin;
1412#if !defined(OS_ANDROID) && !defined(IOS)
1413 if (chrome::IsInstantExtendedAPIEnabled()) {
1414 Browser* browser =
1415 chrome::FindBrowserWithProfile(profile_, chrome::GetActiveDesktop());
1416 if (browser && browser->instant_controller() &&
1417 browser->instant_controller()->instant()) {
1418 omnibox_start_margin =
1419 browser->instant_controller()->instant()->omnibox_bounds().x();
1420 }
1421 }
1422#endif // !defined(OS_ANDROID) && !defined(IOS)
1423
[email protected]bba9e632013-06-28 22:52:191424 const TemplateURL* template_url = is_keyword ?
1425 providers_.GetKeywordProviderURL() : providers_.GetDefaultProviderURL();
1426 AutocompleteMatch match = CreateSearchSuggestion(this, relevance, type,
1427 template_url, query_string, input_text, input_, is_keyword,
[email protected]c4a9d4c2013-08-10 07:06:271428 accepted_suggestion, omnibox_start_margin,
[email protected]56fa29592013-07-02 20:25:531429 !is_keyword || providers_.default_provider().empty());
[email protected]cb86ee6f2013-04-28 16:58:151430 if (!match.destination_url.is_valid())
[email protected]85b8d6f2012-05-08 20:53:471431 return;
[email protected]d30268a2013-06-25 22:31:071432 match.RecordAdditionalInfo(kRelevanceFromServerKey,
1433 relevance_from_server ? kTrue : kFalse);
[email protected]85b8d6f2012-05-08 20:53:471434
initial.commit09911bf2008-07-26 23:55:291435 // Try to add |match| to |map|. If a match for |query_string| is already in
1436 // |map|, replace it if |match| is more relevant.
1437 // NOTE: Keep this ToLower() call in sync with url_database.cc.
[email protected]bc8bb0cd2013-06-24 21:50:231438 const std::pair<MatchMap::iterator, bool> i(
1439 map->insert(std::make_pair(base::i18n::ToLower(query_string), match)));
initial.commit09911bf2008-07-26 23:55:291440 // NOTE: We purposefully do a direct relevance comparison here instead of
1441 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
1442 // first" rather than "items alphabetically first" when the scores are equal.
1443 // The only case this matters is when a user has results with the same score
1444 // that differ only by capitalization; because the history system returns
1445 // results sorted by recency, this means we'll pick the most recent such
1446 // result even if the precision of our relevance score is too low to
1447 // distinguish the two.
1448 if (!i.second && (match.relevance > i.first->second.relevance))
1449 i.first->second = match;
1450}
1451
1452AutocompleteMatch SearchProvider::NavigationToMatch(
[email protected]188b50c2013-03-28 07:19:421453 const NavigationResult& navigation) {
1454 const string16& input = navigation.from_keyword_provider() ?
1455 keyword_input_.text() : input_.text();
[email protected]55ce8f12012-05-09 04:44:081456 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]b7f64d742013-05-21 04:04:041457 AutocompleteMatchType::NAVSUGGEST);
[email protected]55ce8f12012-05-09 04:44:081458 match.destination_url = navigation.url();
[email protected]371dab12012-06-01 03:23:551459
1460 // First look for the user's input inside the fill_into_edit as it would be
1461 // without trimming the scheme, so we can find matches at the beginning of the
1462 // scheme.
[email protected]a1b5e862013-03-29 06:38:441463 const string16& untrimmed_fill_into_edit = navigation.formatted_url();
[email protected]371dab12012-06-01 03:23:551464 const URLPrefix* prefix =
1465 URLPrefix::BestURLPrefix(untrimmed_fill_into_edit, input);
1466 size_t match_start = (prefix == NULL) ?
1467 untrimmed_fill_into_edit.find(input) : prefix->prefix.length();
1468 size_t inline_autocomplete_offset = (prefix == NULL) ?
1469 string16::npos : (match_start + input.length());
1470 bool trim_http = !HasHTTPScheme(input) && (!prefix || (match_start != 0));
1471
1472 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1473 // Otherwise, user edits to a suggestion would show non-Search results.
1474 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
1475 match.fill_into_edit = ASCIIToUTF16("?");
1476 if (inline_autocomplete_offset != string16::npos)
1477 ++inline_autocomplete_offset;
1478 }
1479
1480 const std::string languages(
1481 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
1482 const net::FormatUrlTypes format_types =
1483 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
1484 match.fill_into_edit +=
1485 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1486 net::FormatUrl(navigation.url(), languages, format_types,
1487 net::UnescapeRule::SPACES, NULL, NULL,
1488 &inline_autocomplete_offset));
[email protected]518024c2013-07-19 23:40:251489 if (!input_.prevent_inline_autocomplete() &&
1490 (inline_autocomplete_offset != string16::npos)) {
1491 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
[email protected]45f89a92013-08-12 13:41:361492 match.allowed_to_be_default_match = true;
[email protected]518024c2013-07-19 23:40:251493 match.inline_autocompletion =
1494 match.fill_into_edit.substr(inline_autocomplete_offset);
1495 }
[email protected]371dab12012-06-01 03:23:551496
1497 match.contents = net::FormatUrl(navigation.url(), languages,
1498 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start);
1499 // If the first match in the untrimmed string was inside a scheme that we
1500 // trimmed, look for a subsequent match.
1501 if (match_start == string16::npos)
1502 match_start = match.contents.find(input);
1503 // Safe if |match_start| is npos; also safe if the input is longer than the
1504 // remaining contents after |match_start|.
1505 AutocompleteMatch::ClassifyLocationInString(match_start, input.length(),
1506 match.contents.length(), ACMatchClassification::URL,
1507 &match.contents_class);
initial.commit09911bf2008-07-26 23:55:291508
[email protected]55ce8f12012-05-09 04:44:081509 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551510 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1511 ACMatchClassification::NONE, &match.description_class);
[email protected]d30268a2013-06-25 22:31:071512
1513 match.RecordAdditionalInfo(
1514 kRelevanceFromServerKey,
1515 navigation.relevance_from_server() ? kTrue : kFalse);
1516
initial.commit09911bf2008-07-26 23:55:291517 return match;
1518}
[email protected]4ab4c7c2010-11-24 04:49:341519
[email protected]9dfb4d362013-04-05 02:15:121520void SearchProvider::DemoteKeywordNavigationMatchesPastTopQuery() {
[email protected]d519d392013-03-27 09:24:461521 // First, determine the maximum score of any keyword query match (verbatim or
1522 // query suggestion).
[email protected]d30268a2013-06-25 22:31:071523 bool relevance_from_server;
1524 int max_query_relevance = GetKeywordVerbatimRelevance(&relevance_from_server);
[email protected]cc1526e2013-05-17 04:04:241525 if (!keyword_results_.suggest_results.empty()) {
[email protected]d30268a2013-06-25 22:31:071526 const SuggestResult& top_keyword = keyword_results_.suggest_results.front();
1527 const int suggest_relevance = top_keyword.relevance();
1528 if (suggest_relevance > max_query_relevance) {
1529 max_query_relevance = suggest_relevance;
1530 relevance_from_server = top_keyword.relevance_from_server();
1531 } else if (suggest_relevance == max_query_relevance) {
1532 relevance_from_server |= top_keyword.relevance_from_server();
1533 }
[email protected]d519d392013-03-27 09:24:461534 }
1535 // If no query is supposed to appear, then navigational matches cannot
[email protected]9dfb4d362013-04-05 02:15:121536 // be demoted past it. Get rid of suggested relevance scores for
1537 // navsuggestions and introduce the verbatim results again. The keyword
1538 // verbatim match will outscore the navsuggest matches.
1539 if (max_query_relevance == 0) {
[email protected]cc1526e2013-05-17 04:04:241540 ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results);
1541 ApplyCalculatedNavigationRelevance(&default_results_.navigation_results);
1542 keyword_results_.verbatim_relevance = -1;
1543 default_results_.verbatim_relevance = -1;
[email protected]9dfb4d362013-04-05 02:15:121544 return;
1545 }
[email protected]d519d392013-03-27 09:24:461546 // Now we know we can enforce the minimum score constraint even after
1547 // the navigation matches are demoted. Proceed to demote the navigation
1548 // matches to enforce the query-must-come-first constraint.
[email protected]d519d392013-03-27 09:24:461549 // Cap the relevance score of all results.
[email protected]cc1526e2013-05-17 04:04:241550 for (NavigationResults::iterator it =
1551 keyword_results_.navigation_results.begin();
1552 it != keyword_results_.navigation_results.end(); ++it) {
[email protected]9dfb4d362013-04-05 02:15:121553 if (it->relevance() < max_query_relevance)
1554 return;
1555 max_query_relevance = std::max(max_query_relevance - 1, 0);
1556 it->set_relevance(max_query_relevance);
[email protected]d30268a2013-06-25 22:31:071557 it->set_relevance_from_server(relevance_from_server);
[email protected]d519d392013-03-27 09:24:461558 }
[email protected]d519d392013-03-27 09:24:461559}
1560
[email protected]4ab4c7c2010-11-24 04:49:341561void SearchProvider::UpdateDone() {
[email protected]2cdf1172012-08-26 12:21:331562 // We're done when the timer isn't running, there are no suggest queries
[email protected]5fac3782013-03-06 09:32:311563 // pending, and we're not waiting on Instant.
[email protected]e1290ee62013-06-26 18:31:151564 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0);
[email protected]4ab4c7c2010-11-24 04:49:341565}