blob: d6979a84f22ccc118ebf23cbd2104ee1af271486 [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]20184242014-05-14 02:57:4210#include "base/base64.h"
[email protected]2041cf342010-02-19 03:15:5911#include "base/callback.h"
[email protected]20184242014-05-14 02:57:4212#include "base/command_line.h"
[email protected]51124552011-07-16 01:37:1013#include "base/i18n/break_iterator.h"
[email protected]503d03872011-05-06 08:36:2614#include "base/i18n/case_conversion.h"
[email protected]ffbec692012-02-26 20:26:4215#include "base/json/json_string_value_serializer.h"
[email protected]fa1e0e12013-07-18 00:10:1416#include "base/message_loop/message_loop.h"
[email protected]f5b95ba92012-03-27 14:05:1917#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5718#include "base/prefs/pref_service.h"
[email protected]20184242014-05-14 02:57:4219#include "base/rand_util.h"
[email protected]5889bfb2014-03-19 00:26:4820#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2021#include "base/strings/utf_string_conversions.h"
[email protected]ea3b9a502011-04-04 14:19:3722#include "chrome/browser/autocomplete/autocomplete_classifier.h"
[email protected]810ffba2012-06-12 01:07:4823#include "chrome/browser/autocomplete/autocomplete_classifier_factory.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]9b9fa672013-11-07 06:04:5228#include "chrome/browser/google/google_util.h"
[email protected]6a2c09f2013-01-25 04:50:0729#include "chrome/browser/history/history_service.h"
[email protected]9d2db762012-06-19 00:01:1030#include "chrome/browser/history/history_service_factory.h"
[email protected]10c2d692012-05-11 05:32:2331#include "chrome/browser/history/in_memory_database.h"
[email protected]ab7780792013-01-10 01:26:0932#include "chrome/browser/metrics/variations/variations_http_header_provider.h"
[email protected]684fcc12013-03-08 22:50:0233#include "chrome/browser/omnibox/omnibox_field_trial.h"
[email protected]8ecad5e2010-12-02 21:18:3334#include "chrome/browser/profiles/profile.h"
[email protected]a7b8e43d2013-03-18 18:52:4335#include "chrome/browser/search/search.h"
[email protected]9899a612012-08-21 23:50:0436#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
[email protected]8e5c89a2011-06-07 18:13:3337#include "chrome/browser/search_engines/template_url_service.h"
38#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]c4a9d4c2013-08-10 07:06:2739#include "chrome/browser/ui/search/instant_controller.h"
[email protected]20184242014-05-14 02:57:4240#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0242#include "chrome/common/url_constants.h"
[email protected]7267ff82013-12-03 17:20:3043#include "content/public/browser/user_metrics.h"
[email protected]34ac8f32009-02-22 23:03:2744#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2945#include "net/base/escape.h"
[email protected]d3cf8682f02012-02-29 23:29:3446#include "net/base/load_flags.h"
[email protected]371dab12012-06-01 03:23:5547#include "net/base/net_util.h"
[email protected]bd3b4712012-12-18 17:01:3048#include "net/http/http_request_headers.h"
[email protected]3dc1bc42012-06-19 08:20:5349#include "net/url_request/url_fetcher.h"
[email protected]319d9e6f2009-02-18 19:47:2150#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1751#include "ui/base/l10n/l10n_util.h"
[email protected]761fa4702013-07-02 15:25:1552#include "url/url_util.h"
initial.commit09911bf2008-07-26 23:55:2953
[email protected]bc8bb0cd2013-06-24 21:50:2354// Helpers --------------------------------------------------------------------
[email protected]e1acf6f2008-10-27 20:43:3355
[email protected]51124552011-07-16 01:37:1056namespace {
57
[email protected]7706a522012-08-16 17:42:2558// We keep track in a histogram how many suggest requests we send, how
59// many suggest requests we invalidate (e.g., due to a user typing
60// another character), and how many replies we receive.
61// *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
62// (excluding the end-of-list enum value)
63// We do not want values of existing enums to change or else it screws
64// up the statistics.
65enum SuggestRequestsHistogramValue {
66 REQUEST_SENT = 1,
67 REQUEST_INVALIDATED,
68 REPLY_RECEIVED,
69 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE
70};
71
[email protected]90fe2bb2013-01-15 03:42:1372// The verbatim score for an input which is not an URL.
73const int kNonURLVerbatimRelevance = 1300;
74
[email protected]7706a522012-08-16 17:42:2575// Increments the appropriate value in the histogram by one.
76void LogOmniboxSuggestRequest(
77 SuggestRequestsHistogramValue request_value) {
78 UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value,
79 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE);
80}
81
[email protected]0085863a2013-12-06 21:19:0382bool HasMultipleWords(const base::string16& text) {
[email protected]51124552011-07-16 01:37:1083 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
84 bool found_word = false;
85 if (i.Init()) {
86 while (i.Advance()) {
87 if (i.IsWord()) {
88 if (found_word)
89 return true;
90 found_word = true;
91 }
92 }
93 }
94 return false;
95}
96
[email protected]d1f0a7f2012-06-05 10:26:4297} // namespace
[email protected]51124552011-07-16 01:37:1098
[email protected]3954c3a2012-04-10 20:17:5599// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:58100
[email protected]85b8d6f2012-05-08 20:53:47101SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
[email protected]02346202014-02-05 05:18:30102 : template_url_service_(template_url_service) {}
[email protected]85b8d6f2012-05-08 20:53:47103
104const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
105 return default_provider_.empty() ? NULL :
106 template_url_service_->GetTemplateURLForKeyword(default_provider_);
107}
108
109const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
110 return keyword_provider_.empty() ? NULL :
111 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:24112}
113
[email protected]3954c3a2012-04-10 20:17:55114
[email protected]bc8bb0cd2013-06-24 21:50:23115// SearchProvider::CompareScoredResults ---------------------------------------
116
117class SearchProvider::CompareScoredResults {
118 public:
119 bool operator()(const Result& a, const Result& b) {
120 // Sort in descending relevance order.
121 return a.relevance() > b.relevance();
122 }
123};
124
125
[email protected]3954c3a2012-04-10 20:17:55126// SearchProvider -------------------------------------------------------------
127
128// static
[email protected]24dbf2302013-04-05 16:59:25129int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100;
[email protected]3954c3a2012-04-10 20:17:55130
[email protected]bc8bb0cd2013-06-24 21:50:23131SearchProvider::SearchProvider(AutocompleteProviderListener* listener,
132 Profile* profile)
[email protected]02346202014-02-05 05:18:30133 : BaseSearchProvider(listener, profile, AutocompleteProvider::TYPE_SEARCH),
[email protected]cfa164bf2014-03-19 11:51:15134 providers_(TemplateURLServiceFactory::GetForProfile(profile)) {
[email protected]bc8bb0cd2013-06-24 21:50:23135}
136
[email protected]cb86ee6f2013-04-28 16:58:15137// static
[email protected]987fad782013-08-28 06:23:18138std::string SearchProvider::GetSuggestMetadata(const AutocompleteMatch& match) {
139 return match.GetAdditionalInfo(kSuggestMetadataKey);
140}
141
[email protected]bc8bb0cd2013-06-24 21:50:23142void SearchProvider::ResetSession() {
143 field_trial_triggered_in_session_ = false;
[email protected]4ab4c7c2010-11-24 04:49:34144}
145
[email protected]bc8bb0cd2013-06-24 21:50:23146SearchProvider::~SearchProvider() {
147}
148
[email protected]ee6110b2014-01-09 22:26:31149void SearchProvider::UpdateMatchContentsClass(const base::string16& input_text,
[email protected]23db6492014-01-16 02:35:30150 Results* results) {
151 for (SuggestResults::iterator sug_it = results->suggest_results.begin();
152 sug_it != results->suggest_results.end(); ++sug_it) {
[email protected]ee6110b2014-01-09 22:26:31153 sug_it->ClassifyMatchContents(false, input_text);
154 }
[email protected]23db6492014-01-16 02:35:30155 const std::string languages(
156 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
157 for (NavigationResults::iterator nav_it = results->navigation_results.begin();
158 nav_it != results->navigation_results.end(); ++nav_it) {
159 nav_it->CalculateAndClassifyMatchContents(false, input_text, languages);
160 }
[email protected]ee6110b2014-01-09 22:26:31161}
162
163// static
[email protected]bc8bb0cd2013-06-24 21:50:23164int SearchProvider::CalculateRelevanceForKeywordVerbatim(
165 AutocompleteInput::Type type,
166 bool prefer_keyword) {
167 // This function is responsible for scoring verbatim query matches
168 // for non-extension keywords. KeywordProvider::CalculateRelevance()
169 // scores verbatim query matches for extension keywords, as well as
170 // for keyword matches (i.e., suggestions of a keyword itself, not a
171 // suggestion of a query on a keyword search engine). These two
172 // functions are currently in sync, but there's no reason we
173 // couldn't decide in the future to score verbatim matches
174 // differently for extension and non-extension keywords. If you
175 // make such a change, however, you should update this comment to
176 // describe it, so it's clear why the functions diverge.
177 if (prefer_keyword)
178 return 1500;
179 return (type == AutocompleteInput::QUERY) ? 1450 : 1100;
180}
181
initial.commit09911bf2008-07-26 23:55:29182void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27183 bool minimal_changes) {
[email protected]04504c242013-01-22 21:08:55184 // Do our best to load the model as early as possible. This will reduce
185 // odds of having the model not ready when really needed (a non-empty input).
186 TemplateURLService* model = providers_.template_url_service();
187 DCHECK(model);
188 model->Load();
189
initial.commit09911bf2008-07-26 23:55:29190 matches_.clear();
[email protected]618d6e62012-12-16 05:55:57191 field_trial_triggered_ = false;
initial.commit09911bf2008-07-26 23:55:29192
[email protected]6c85aa02009-02-27 12:08:09193 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29194 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
[email protected]9950e5d52014-02-25 23:34:19195 Stop(true);
initial.commit09911bf2008-07-26 23:55:29196 return;
197 }
198
[email protected]14710852013-02-05 23:45:41199 keyword_input_ = input;
[email protected]257ab712009-04-14 17:16:24200 const TemplateURL* keyword_provider =
[email protected]14710852013-02-05 23:45:41201 KeywordProvider::GetSubstitutingTemplateURLForInput(model,
202 &keyword_input_);
203 if (keyword_provider == NULL)
204 keyword_input_.Clear();
205 else if (keyword_input_.text().empty())
[email protected]257ab712009-04-14 17:16:24206 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24207
[email protected]85b8d6f2012-05-08 20:53:47208 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]9b74ab52012-03-30 16:08:07209 if (default_provider && !default_provider->SupportsReplacement())
[email protected]257ab712009-04-14 17:16:24210 default_provider = NULL;
211
212 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18213 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24214
215 if (!default_provider && !keyword_provider) {
216 // No valid providers.
[email protected]9950e5d52014-02-25 23:34:19217 Stop(true);
initial.commit09911bf2008-07-26 23:55:29218 return;
219 }
220
221 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24222 // or the providers, abort the query.
[email protected]0085863a2013-12-06 21:19:03223 base::string16 default_provider_keyword(default_provider ?
224 default_provider->keyword() : base::string16());
225 base::string16 keyword_provider_keyword(keyword_provider ?
226 keyword_provider->keyword() : base::string16());
[email protected]9e789742011-01-10 23:27:32227 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47228 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]bb900e02013-03-14 14:15:29229 // Cancel any in-flight suggest requests.
[email protected]e1290ee62013-06-26 18:31:15230 if (!done_)
[email protected]e29249dc52012-07-19 17:33:50231 Stop(false);
[email protected]257ab712009-04-14 17:16:24232 }
initial.commit09911bf2008-07-26 23:55:29233
[email protected]85b8d6f2012-05-08 20:53:47234 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29235
236 if (input.text().empty()) {
237 // User typed "?" alone. Give them a placeholder result indicating what
238 // this syntax does.
[email protected]257ab712009-04-14 17:16:24239 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00240 AutocompleteMatch match;
241 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36242 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24243 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35244 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47245 match.keyword = providers_.default_provider();
[email protected]45f89a92013-08-12 13:41:36246 match.allowed_to_be_default_match = true;
[email protected]257ab712009-04-14 17:16:24247 matches_.push_back(match);
248 }
[email protected]9950e5d52014-02-25 23:34:19249 Stop(true);
initial.commit09911bf2008-07-26 23:55:29250 return;
251 }
252
253 input_ = input;
254
[email protected]e1290ee62013-06-26 18:31:15255 DoHistoryQuery(minimal_changes);
256 StartOrStopSuggestQuery(minimal_changes);
[email protected]344946a12012-12-20 12:03:42257 UpdateMatches();
initial.commit09911bf2008-07-26 23:55:29258}
259
[email protected]d4a94b92014-03-04 01:35:22260void SearchProvider::SortResults(bool is_keyword,
261 const base::ListValue* relevances,
262 Results* results) {
263 // Ignore suggested scores for non-keyword matches in keyword mode; if the
264 // server is allowed to score these, it could interfere with the user's
265 // ability to get good keyword results.
266 const bool abandon_suggested_scores =
267 !is_keyword && !providers_.keyword_provider().empty();
268 // Apply calculated relevance scores to suggestions if a valid list was
269 // not provided or we're abandoning suggested scores entirely.
270 if ((relevances == NULL) || abandon_suggested_scores) {
271 ApplyCalculatedSuggestRelevance(&results->suggest_results);
272 ApplyCalculatedNavigationRelevance(&results->navigation_results);
273 // If abandoning scores entirely, also abandon the verbatim score.
274 if (abandon_suggested_scores)
275 results->verbatim_relevance = -1;
276 }
277
278 // Keep the result lists sorted.
279 const CompareScoredResults comparator = CompareScoredResults();
280 std::stable_sort(results->suggest_results.begin(),
281 results->suggest_results.end(),
282 comparator);
283 std::stable_sort(results->navigation_results.begin(),
284 results->navigation_results.end(),
285 comparator);
286}
287
[email protected]cfa164bf2014-03-19 11:51:15288const TemplateURL* SearchProvider::GetTemplateURL(bool is_keyword) const {
289 return is_keyword ? providers_.GetKeywordProviderURL()
290 : providers_.GetDefaultProviderURL();
[email protected]9487b392014-02-14 02:48:18291}
292
[email protected]d4a94b92014-03-04 01:35:22293const AutocompleteInput SearchProvider::GetInput(bool is_keyword) const {
294 return is_keyword ? keyword_input_ : input_;
[email protected]9487b392014-02-14 02:48:18295}
296
[email protected]cfa164bf2014-03-19 11:51:15297BaseSearchProvider::Results* SearchProvider::GetResultsToFill(bool is_keyword) {
298 return is_keyword ? &keyword_results_ : &default_results_;
299}
300
[email protected]9487b392014-02-14 02:48:18301bool SearchProvider::ShouldAppendExtraParams(
302 const SuggestResult& result) const {
303 return !result.from_keyword_provider() ||
304 providers_.default_provider().empty();
305}
306
[email protected]ef6866f2014-02-18 08:26:34307void SearchProvider::StopSuggest() {
308 // Increment the appropriate field in the histogram by the number of
309 // pending requests that were invalidated.
310 for (int i = 0; i < suggest_results_pending_; ++i)
311 LogOmniboxSuggestRequest(REQUEST_INVALIDATED);
312 suggest_results_pending_ = 0;
313 timer_.Stop();
314 // Stop any in-progress URL fetches.
315 keyword_fetcher_.reset();
316 default_fetcher_.reset();
317}
318
319void SearchProvider::ClearAllResults() {
320 keyword_results_.Clear();
321 default_results_.Clear();
322}
323
[email protected]d4a94b92014-03-04 01:35:22324int SearchProvider::GetDefaultResultRelevance() const {
325 return -1;
326}
327
[email protected]7267ff82013-12-03 17:20:30328void SearchProvider::RecordDeletionResult(bool success) {
329 if (success) {
330 content::RecordAction(
[email protected]e6e30ac2014-01-13 21:24:39331 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Success"));
[email protected]7267ff82013-12-03 17:20:30332 } else {
333 content::RecordAction(
[email protected]e6e30ac2014-01-13 21:24:39334 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure"));
[email protected]7267ff82013-12-03 17:20:30335 }
336}
337
[email protected]cfa164bf2014-03-19 11:51:15338void SearchProvider::LogFetchComplete(bool success, bool is_keyword) {
339 LogOmniboxSuggestRequest(REPLY_RECEIVED);
340 // Record response time for suggest requests sent to Google. We care
341 // only about the common case: the Google default provider used in
342 // non-keyword mode.
343 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
344 if (!is_keyword && default_url &&
345 (TemplateURLPrepopulateData::GetEngineType(*default_url) ==
346 SEARCH_ENGINE_GOOGLE)) {
347 const base::TimeDelta elapsed_time =
348 base::TimeTicks::Now() - time_suggest_request_sent_;
349 if (success) {
350 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
351 elapsed_time);
352 } else {
353 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
354 elapsed_time);
355 }
356 }
357}
358
359bool SearchProvider::IsKeywordFetcher(const net::URLFetcher* fetcher) const {
360 return fetcher == keyword_fetcher_.get();
361}
362
363void SearchProvider::UpdateMatches() {
[email protected]cfa164bf2014-03-19 11:51:15364 ConvertResultsToAutocompleteMatches();
365
366 // Check constraints that may be violated by suggested relevances.
367 if (!matches_.empty() &&
368 (default_results_.HasServerProvidedScores() ||
369 keyword_results_.HasServerProvidedScores())) {
370 // These blocks attempt to repair undesirable behavior by suggested
371 // relevances with minimal impact, preserving other suggested relevances.
372
[email protected]cfa164bf2014-03-19 11:51:15373 if (!HasKeywordDefaultMatchInKeywordMode()) {
374 // In keyword mode, disregard the keyword verbatim suggested relevance
375 // if necessary so there at least one keyword match that's allowed to
376 // be the default match.
377 keyword_results_.verbatim_relevance = -1;
378 ConvertResultsToAutocompleteMatches();
379 }
[email protected]89bd27d12014-04-12 17:36:23380 if (IsTopMatchSearchWithURLInput()) {
[email protected]cfa164bf2014-03-19 11:51:15381 // Disregard the suggested search and verbatim relevances if the input
382 // type is URL and the top match is a highly-ranked search suggestion.
383 // For example, prevent a search for "foo.com" from outranking another
384 // provider's navigation for "foo.com" or "foo.com/url_from_history".
385 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
386 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
387 default_results_.verbatim_relevance = -1;
388 keyword_results_.verbatim_relevance = -1;
389 ConvertResultsToAutocompleteMatches();
390 }
[email protected]89bd27d12014-04-12 17:36:23391 if (FindTopMatch() == matches_.end()) {
392 // Guarantee that SearchProvider returns a legal default match. (The
393 // omnibox always needs at least one legal default match, and it relies
394 // on SearchProvider to always return one.)
[email protected]cfa164bf2014-03-19 11:51:15395 ApplyCalculatedRelevance();
396 ConvertResultsToAutocompleteMatches();
397 }
[email protected]cfa164bf2014-03-19 11:51:15398 DCHECK(HasKeywordDefaultMatchInKeywordMode());
[email protected]89bd27d12014-04-12 17:36:23399 DCHECK(!IsTopMatchSearchWithURLInput());
400 DCHECK(FindTopMatch() != matches_.end());
[email protected]cfa164bf2014-03-19 11:51:15401 }
402 UMA_HISTOGRAM_CUSTOM_COUNTS(
403 "Omnibox.SearchProviderMatches", matches_.size(), 1, 6, 7);
404
405 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
406 if ((keyword_url != NULL) && HasKeywordDefaultMatchInKeywordMode()) {
407 // If there is a keyword match that is allowed to be the default match,
408 // then prohibit default provider matches from being the default match lest
409 // such matches cause the user to break out of keyword mode.
410 for (ACMatches::iterator it = matches_.begin(); it != matches_.end();
411 ++it) {
412 if (it->keyword != keyword_url->keyword())
413 it->allowed_to_be_default_match = false;
414 }
415 }
416
417 base::TimeTicks update_starred_start_time(base::TimeTicks::Now());
418 UpdateStarredStateOfMatches();
419 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateStarredTime",
420 base::TimeTicks::Now() - update_starred_start_time);
421 UpdateDone();
[email protected]cfa164bf2014-03-19 11:51:15422}
423
[email protected]bc8bb0cd2013-06-24 21:50:23424void SearchProvider::Run() {
425 // Start a new request with the current input.
426 suggest_results_pending_ = 0;
427 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]abe441e2013-05-06 12:35:05428
[email protected]bc8bb0cd2013-06-24 21:50:23429 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
430 providers_.GetDefaultProviderURL(), input_));
431 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
432 providers_.GetKeywordProviderURL(), keyword_input_));
433
434 // Both the above can fail if the providers have been modified or deleted
435 // since the query began.
436 if (suggest_results_pending_ == 0) {
437 UpdateDone();
438 // We only need to update the listener if we're actually done.
439 if (done_)
440 listener_->OnProviderUpdate(false);
441 }
[email protected]601858c02010-09-01 17:08:20442}
443
[email protected]8d457132010-11-04 18:13:40444void SearchProvider::DoHistoryQuery(bool minimal_changes) {
445 // The history query results are synchronous, so if minimal_changes is true,
446 // we still have the last results and don't need to do anything.
447 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29448 return;
449
[email protected]8d457132010-11-04 18:13:40450 keyword_history_results_.clear();
451 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29452
[email protected]78e5e432013-08-03 02:10:10453 if (OmniboxFieldTrial::SearchHistoryDisable(
454 input_.current_page_classification()))
[email protected]d8cd76b2013-07-10 09:46:16455 return;
456
[email protected]8d457132010-11-04 18:13:40457 HistoryService* const history_service =
[email protected]9d2db762012-06-19 00:01:10458 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
[email protected]8d457132010-11-04 18:13:40459 history::URLDatabase* url_db = history_service ?
460 history_service->InMemoryDatabase() : NULL;
[email protected]8d457132010-11-04 18:13:40461 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29462 return;
463
[email protected]51124552011-07-16 01:37:10464 // Request history for both the keyword and default provider. We grab many
465 // more matches than we'll ultimately clamp to so that if there are several
466 // recent multi-word matches who scores are lowered (see
467 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
468 // matches. Note that this doesn't fix the problem entirely, but merely
469 // limits it to cases with a very large number of such multi-word matches; for
470 // now, this seems OK compared with the complexity of a real fix, which would
471 // require multiple searches and tracking of "single- vs. multi-word" in the
472 // database.
473 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47474 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
475 if (default_url) {
[email protected]b4bec972014-04-05 18:07:15476 const base::TimeTicks start_time = base::TimeTicks::Now();
[email protected]85b8d6f2012-05-08 20:53:47477 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
478 num_matches, &default_history_results_);
[email protected]31afdf72013-09-26 04:29:36479 UMA_HISTOGRAM_TIMES(
480 "Omnibox.SearchProvider.GetMostRecentKeywordTermsDefaultProviderTime",
481 base::TimeTicks::Now() - start_time);
[email protected]257ab712009-04-14 17:16:24482 }
[email protected]85b8d6f2012-05-08 20:53:47483 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
484 if (keyword_url) {
485 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]14710852013-02-05 23:45:41486 keyword_input_.text(), num_matches, &keyword_history_results_);
[email protected]3954c3a2012-04-10 20:17:55487 }
initial.commit09911bf2008-07-26 23:55:29488}
489
[email protected]6dc950f2012-07-16 19:49:08490void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]83c726482008-09-10 06:36:34491 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29492 StopSuggest();
[email protected]71b46152013-05-03 16:39:20493 ClearAllResults();
initial.commit09911bf2008-07-26 23:55:29494 return;
495 }
496
497 // For the minimal_changes case, if we finished the previous query and still
498 // have its results, or are allowed to keep running it, just do that, rather
499 // than starting a new query.
500 if (minimal_changes &&
[email protected]cc1526e2013-05-17 04:04:24501 (!default_results_.suggest_results.empty() ||
502 !default_results_.navigation_results.empty() ||
503 !keyword_results_.suggest_results.empty() ||
504 !keyword_results_.navigation_results.empty() ||
[email protected]a2770a7d2014-04-22 19:33:35505 (!done_ && input_.want_asynchronous_matches())))
initial.commit09911bf2008-07-26 23:55:29506 return;
507
508 // We can't keep running any previous query, so halt it.
509 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42510
511 // Remove existing results that cannot inline autocomplete the new input.
[email protected]71b46152013-05-03 16:39:20512 RemoveAllStaleResults();
initial.commit09911bf2008-07-26 23:55:29513
[email protected]ee6110b2014-01-09 22:26:31514 // Update the content classifications of remaining results so they look good
515 // against the current input.
[email protected]23db6492014-01-16 02:35:30516 UpdateMatchContentsClass(input_.text(), &default_results_);
517 if (!keyword_input_.text().empty())
518 UpdateMatchContentsClass(keyword_input_.text(), &keyword_results_);
[email protected]ee6110b2014-01-09 22:26:31519
initial.commit09911bf2008-07-26 23:55:29520 // We can't start a new query if we're only allowed synchronous results.
[email protected]a2770a7d2014-04-22 19:33:35521 if (!input_.want_asynchronous_matches())
initial.commit09911bf2008-07-26 23:55:29522 return;
523
[email protected]24dbf2302013-04-05 16:59:25524 // To avoid flooding the suggest server, don't send a query until at
525 // least 100 ms since the last query.
[email protected]515ffa942012-11-27 20:18:24526 base::TimeTicks next_suggest_time(time_suggest_request_sent_ +
[email protected]bc8bb0cd2013-06-24 21:50:23527 base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs));
[email protected]515ffa942012-11-27 20:18:24528 base::TimeTicks now(base::TimeTicks::Now());
529 if (now >= next_suggest_time) {
530 Run();
531 return;
532 }
533 timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29534}
535
[email protected]83c726482008-09-10 06:36:34536bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55537 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
538 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47539 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
540 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34541 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47542 ((!default_url || default_url->suggestions_url().empty()) &&
543 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34544 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
545 return false;
546
[email protected]cac59d32010-08-09 23:23:14547 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34548 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34549
[email protected]cac59d32010-08-09 23:23:14550 // FORCED_QUERY means the user is explicitly asking us to search for this, so
551 // we assume it isn't a URL and/or there isn't private data.
552 if (input_.type() == AutocompleteInput::FORCED_QUERY)
553 return true;
[email protected]83c726482008-09-10 06:36:34554
[email protected]f608ea102013-03-18 15:08:09555 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't
556 // http/https/ftp, we shouldn't send it. Sending things like file: and data:
557 // is both a waste of time and a disclosure of potentially private, local
558 // data. Other "schemes" may actually be usernames, and we don't want to send
559 // passwords. If the scheme is OK, we still need to check other cases below.
560 // If this is QUERY, then the presence of these schemes means the user
561 // explicitly typed one, and thus this is probably a URL that's being entered
562 // and happens to currently be invalid -- in which case we again want to run
563 // our checks below. Other QUERY cases are less likely to be URLs and thus we
564 // assume we're OK.
[email protected]e8ca69c2014-05-07 15:31:19565 if (!LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) &&
566 !LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
[email protected]72aa4c22013-12-01 15:09:35567 !LowerCaseEqualsASCII(input_.scheme(), content::kFtpScheme))
[email protected]cac59d32010-08-09 23:23:14568 return (input_.type() == AutocompleteInput::QUERY);
569
570 // Don't send URLs with usernames, queries or refs. Some of these are
571 // private, and the Suggest server is unlikely to have any useful results
572 // for any of them. Also don't send URLs with ports, as we may initially
573 // think that a username + password is a host + port (and we don't want to
574 // send usernames/passwords), and even if the port really is a port, the
575 // server is once again unlikely to have and useful results.
[email protected]825e16f2013-09-30 23:52:58576 // Note that we only block based on refs if the input is URL-typed, as search
577 // queries can legitimately have #s in them which the URL parser
578 // overaggressively categorizes as a url with a ref.
[email protected]b45334502014-04-30 19:44:05579 const url::Parsed& parts = input_.parts();
[email protected]cac59d32010-08-09 23:23:14580 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
[email protected]825e16f2013-09-30 23:52:58581 parts.query.is_nonempty() ||
582 (parts.ref.is_nonempty() && (input_.type() == AutocompleteInput::URL)))
[email protected]cac59d32010-08-09 23:23:14583 return false;
584
585 // Don't send anything for https except the hostname. Hostnames are OK
586 // because they are visible when the TCP connection is established, but the
587 // specific path may reveal private information.
[email protected]e8ca69c2014-05-07 15:31:19588 if (LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
[email protected]a2fedb1e2011-01-25 15:23:36589 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14590 return false;
[email protected]83c726482008-09-10 06:36:34591
592 return true;
593}
594
[email protected]71b46152013-05-03 16:39:20595void SearchProvider::RemoveAllStaleResults() {
[email protected]dc735c02013-11-12 23:23:41596 if (keyword_input_.text().empty()) {
[email protected]1e1550e2013-05-02 17:37:51597 // User is either in keyword mode with a blank input or out of
598 // keyword mode entirely.
[email protected]cc1526e2013-05-17 04:04:24599 keyword_results_.Clear();
[email protected]1e1550e2013-05-02 17:37:51600 }
[email protected]d1f0a7f2012-06-05 10:26:42601}
602
[email protected]d1f0a7f2012-06-05 10:26:42603void SearchProvider::ApplyCalculatedRelevance() {
[email protected]cc1526e2013-05-17 04:04:24604 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
605 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
606 ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results);
607 ApplyCalculatedNavigationRelevance(&default_results_.navigation_results);
[email protected]cc1526e2013-05-17 04:04:24608 default_results_.verbatim_relevance = -1;
609 keyword_results_.verbatim_relevance = -1;
[email protected]d1f0a7f2012-06-05 10:26:42610}
611
[email protected]188b50c2013-03-28 07:19:42612void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42613 for (size_t i = 0; i < list->size(); ++i) {
[email protected]188b50c2013-03-28 07:19:42614 SuggestResult& result = (*list)[i];
615 result.set_relevance(
616 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
617 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07618 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42619 }
620}
621
[email protected]188b50c2013-03-28 07:19:42622void SearchProvider::ApplyCalculatedNavigationRelevance(
623 NavigationResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42624 for (size_t i = 0; i < list->size(); ++i) {
[email protected]188b50c2013-03-28 07:19:42625 NavigationResult& result = (*list)[i];
626 result.set_relevance(
627 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
628 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07629 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42630 }
631}
632
[email protected]15fb2aa2012-05-22 22:52:59633net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12634 int id,
[email protected]9ff91722012-09-07 05:29:12635 const TemplateURL* template_url,
[email protected]14710852013-02-05 23:45:41636 const AutocompleteInput& input) {
[email protected]9ff91722012-09-07 05:29:12637 if (!template_url || template_url->suggestions_url().empty())
638 return NULL;
639
640 // Bail if the suggestion URL is invalid with the given replacements.
[email protected]14710852013-02-05 23:45:41641 TemplateURLRef::SearchTermsArgs search_term_args(input.text());
642 search_term_args.cursor_position = input.cursor_position();
[email protected]d5015ca2013-08-08 22:04:18643 search_term_args.page_classification = input.current_page_classification();
[email protected]20184242014-05-14 02:57:42644#if defined(OS_ANDROID)
645 if (CommandLine::ForCurrentProcess()->HasSwitch(
646 switches::kEnableAnswersInSuggest))
647 search_term_args.session_token = GetSessionToken();
648#endif
[email protected]9ff91722012-09-07 05:29:12649 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]00790562012-12-14 09:57:16650 search_term_args));
[email protected]9ff91722012-09-07 05:29:12651 if (!suggest_url.is_valid())
652 return NULL;
[email protected]9b9fa672013-11-07 06:04:52653 // Send the current page URL if user setting and URL requirements are met and
654 // the user is in the field trial.
655 if (CanSendURL(current_page_url_, suggest_url, template_url,
656 input.current_page_classification(), profile_) &&
657 OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) {
658 search_term_args.current_page_url = current_page_url_.spec();
659 // Create the suggest URL again with the current page URL.
660 suggest_url = GURL(template_url->suggestions_url_ref().ReplaceSearchTerms(
661 search_term_args));
662 }
[email protected]9ff91722012-09-07 05:29:12663
664 suggest_results_pending_++;
665 LogOmniboxSuggestRequest(REQUEST_SENT);
666
667 net::URLFetcher* fetcher =
668 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12669 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34670 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]bd3b4712012-12-18 17:01:30671 // Add Chrome experiment state to the request headers.
672 net::HttpRequestHeaders headers;
[email protected]ab7780792013-01-10 01:26:09673 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
[email protected]bd3b4712012-12-18 17:01:30674 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
675 fetcher->SetExtraRequestHeaders(headers.ToString());
[email protected]257ab712009-04-14 17:16:24676 fetcher->Start();
677 return fetcher;
678}
679
[email protected]344946a12012-12-20 12:03:42680void SearchProvider::ConvertResultsToAutocompleteMatches() {
initial.commit09911bf2008-07-26 23:55:29681 // Convert all the results to matches and add them to a map, so we can keep
682 // the most relevant match for each result.
[email protected]31afdf72013-09-26 04:29:36683 base::TimeTicks start_time(base::TimeTicks::Now());
initial.commit09911bf2008-07-26 23:55:29684 MatchMap map;
[email protected]bc8bb0cd2013-06-24 21:50:23685 const base::Time no_time;
[email protected]cc1526e2013-05-17 04:04:24686 int did_not_accept_keyword_suggestion =
687 keyword_results_.suggest_results.empty() ?
initial.commit09911bf2008-07-26 23:55:29688 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
689 TemplateURLRef::NO_SUGGESTION_CHOSEN;
initial.commit09911bf2008-07-26 23:55:29690
[email protected]d30268a2013-06-25 22:31:07691 bool relevance_from_server;
692 int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server);
[email protected]cc1526e2013-05-17 04:04:24693 int did_not_accept_default_suggestion =
694 default_results_.suggest_results.empty() ?
[email protected]55ce8f12012-05-09 04:44:08695 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
696 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]d1f0a7f2012-06-05 10:26:42697 if (verbatim_relevance > 0) {
[email protected]c2ca3fd2014-03-22 03:07:44698 const base::string16& trimmed_verbatim =
699 base::CollapseWhitespace(input_.text(), false);
[email protected]2c03c06b2013-12-11 20:45:02700 SuggestResult verbatim(
[email protected]c2ca3fd2014-03-22 03:07:44701 trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
702 trimmed_verbatim, base::string16(), base::string16(), std::string(),
[email protected]61f99082014-02-24 02:17:49703 std::string(), false, verbatim_relevance, relevance_from_server, false,
[email protected]c2ca3fd2014-03-22 03:07:44704 trimmed_verbatim);
[email protected]57482a72014-03-14 22:27:37705 AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion,
706 false, &map);
[email protected]d1f0a7f2012-06-05 10:26:42707 }
[email protected]5423e562013-02-07 03:58:45708 if (!keyword_input_.text().empty()) {
709 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
710 // We only create the verbatim search query match for a keyword
711 // if it's not an extension keyword. Extension keywords are handled
712 // in KeywordProvider::Start(). (Extensions are complicated...)
713 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond
714 // to the keyword verbatim search query. Do not create other matches
715 // of type SEARCH_OTHER_ENGINE.
[email protected]bdcbcd82013-10-28 13:40:25716 if (keyword_url &&
717 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) {
[email protected]d30268a2013-06-25 22:31:07718 bool keyword_relevance_from_server;
719 const int keyword_verbatim_relevance =
720 GetKeywordVerbatimRelevance(&keyword_relevance_from_server);
[email protected]dab8d52d2013-03-05 07:35:28721 if (keyword_verbatim_relevance > 0) {
[email protected]c2ca3fd2014-03-22 03:07:44722 const base::string16& trimmed_verbatim =
723 base::CollapseWhitespace(keyword_input_.text(), false);
[email protected]2c03c06b2013-12-11 20:45:02724 SuggestResult verbatim(
[email protected]c2ca3fd2014-03-22 03:07:44725 trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE,
726 trimmed_verbatim, base::string16(), base::string16(),
[email protected]61f99082014-02-24 02:17:49727 std::string(), std::string(), true, keyword_verbatim_relevance,
[email protected]c2ca3fd2014-03-22 03:07:44728 keyword_relevance_from_server, false, trimmed_verbatim);
[email protected]57482a72014-03-14 22:27:37729 AddMatchToMap(verbatim, std::string(),
730 did_not_accept_keyword_suggestion, false, &map);
[email protected]dab8d52d2013-03-05 07:35:28731 }
[email protected]5423e562013-02-07 03:58:45732 }
733 }
[email protected]257ab712009-04-14 17:16:24734 AddHistoryResultsToMap(keyword_history_results_, true,
735 did_not_accept_keyword_suggestion, &map);
736 AddHistoryResultsToMap(default_history_results_, false,
737 did_not_accept_default_suggestion, &map);
738
[email protected]d1cb6a822013-09-18 19:43:00739 AddSuggestResultsToMap(keyword_results_.suggest_results,
740 keyword_results_.metadata, &map);
[email protected]987fad782013-08-28 06:23:18741 AddSuggestResultsToMap(default_results_.suggest_results,
742 default_results_.metadata, &map);
initial.commit09911bf2008-07-26 23:55:29743
[email protected]d30268a2013-06-25 22:31:07744 ACMatches matches;
initial.commit09911bf2008-07-26 23:55:29745 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
[email protected]d30268a2013-06-25 22:31:07746 matches.push_back(i->second);
initial.commit09911bf2008-07-26 23:55:29747
[email protected]d30268a2013-06-25 22:31:07748 AddNavigationResultsToMatches(keyword_results_.navigation_results, &matches);
749 AddNavigationResultsToMatches(default_results_.navigation_results, &matches);
initial.commit09911bf2008-07-26 23:55:29750
[email protected]d30268a2013-06-25 22:31:07751 // Now add the most relevant matches to |matches_|. We take up to kMaxMatches
752 // suggest/navsuggest matches, regardless of origin. If Instant Extended is
753 // enabled and we have server-provided (and thus hopefully more accurate)
754 // scores for some suggestions, we allow more of those, until we reach
755 // AutocompleteResult::kMaxMatches total matches (that is, enough to fill the
756 // whole popup).
757 //
758 // We will always return any verbatim matches, no matter how we obtained their
759 // scores, unless we have already accepted AutocompleteResult::kMaxMatches
760 // higher-scoring matches under the conditions above.
[email protected]d30268a2013-06-25 22:31:07761 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant);
762 matches_.clear();
[email protected]3723e6e2012-06-11 21:06:56763
[email protected]d30268a2013-06-25 22:31:07764 size_t num_suggestions = 0;
765 for (ACMatches::const_iterator i(matches.begin());
766 (i != matches.end()) &&
767 (matches_.size() < AutocompleteResult::kMaxMatches);
768 ++i) {
769 // SEARCH_OTHER_ENGINE is only used in the SearchProvider for the keyword
770 // verbatim result, so this condition basically means "if this match is a
771 // suggestion of some sort".
772 if ((i->type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED) &&
773 (i->type != AutocompleteMatchType::SEARCH_OTHER_ENGINE)) {
774 // If we've already hit the limit on non-server-scored suggestions, and
775 // this isn't a server-scored suggestion we can add, skip it.
776 if ((num_suggestions >= kMaxMatches) &&
777 (!chrome::IsInstantExtendedAPIEnabled() ||
778 (i->GetAdditionalInfo(kRelevanceFromServerKey) != kTrue))) {
779 continue;
780 }
781
782 ++num_suggestions;
783 }
784
785 matches_.push_back(*i);
786 }
[email protected]31afdf72013-09-26 04:29:36787 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime",
788 base::TimeTicks::Now() - start_time);
[email protected]344946a12012-12-20 12:03:42789}
790
[email protected]89bd27d12014-04-12 17:36:23791ACMatches::const_iterator SearchProvider::FindTopMatch() const {
[email protected]0a8718b12013-11-13 18:41:31792 ACMatches::const_iterator it = matches_.begin();
793 while ((it != matches_.end()) && !it->allowed_to_be_default_match)
794 ++it;
795 return it;
[email protected]9dfb4d362013-04-05 02:15:12796}
797
[email protected]1c1e7842013-11-22 16:28:16798bool SearchProvider::HasKeywordDefaultMatchInKeywordMode() const {
799 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
800 // If the user is not in keyword mode, return true to say that this
801 // constraint is not violated.
802 if (keyword_url == NULL)
803 return true;
804 for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end();
805 ++it) {
806 if ((it->keyword == keyword_url->keyword()) &&
807 it->allowed_to_be_default_match)
808 return true;
809 }
810 return false;
811}
812
[email protected]89bd27d12014-04-12 17:36:23813bool SearchProvider::IsTopMatchSearchWithURLInput() const {
814 ACMatches::const_iterator first_match = FindTopMatch();
[email protected]0a8718b12013-11-13 18:41:31815 return (input_.type() == AutocompleteInput::URL) &&
816 (first_match != matches_.end()) &&
817 (first_match->relevance > CalculateRelevanceForVerbatim()) &&
[email protected]78981d8c2014-05-09 15:05:47818 (first_match->type != AutocompleteMatchType::NAVSUGGEST) &&
819 (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED);
[email protected]344946a12012-12-20 12:03:42820}
821
[email protected]257ab712009-04-14 17:16:24822void SearchProvider::AddNavigationResultsToMatches(
[email protected]d30268a2013-06-25 22:31:07823 const NavigationResults& navigation_results,
824 ACMatches* matches) {
[email protected]bc8bb0cd2013-06-24 21:50:23825 for (NavigationResults::const_iterator it = navigation_results.begin();
826 it != navigation_results.end(); ++it) {
[email protected]d30268a2013-06-25 22:31:07827 matches->push_back(NavigationToMatch(*it));
[email protected]bc8bb0cd2013-06-24 21:50:23828 // In the absence of suggested relevance scores, use only the single
829 // highest-scoring result. (The results are already sorted by relevance.)
[email protected]d30268a2013-06-25 22:31:07830 if (!it->relevance_from_server())
[email protected]bc8bb0cd2013-06-24 21:50:23831 return;
[email protected]257ab712009-04-14 17:16:24832 }
833}
834
835void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
836 bool is_keyword,
837 int did_not_accept_suggestion,
838 MatchMap* map) {
[email protected]51124552011-07-16 01:37:10839 if (results.empty())
840 return;
841
[email protected]31afdf72013-09-26 04:29:36842 base::TimeTicks start_time(base::TimeTicks::Now());
[email protected]d7ad4772012-06-01 03:12:54843 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
844 (input_.type() == AutocompleteInput::URL);
[email protected]0085863a2013-12-06 21:19:03845 const base::string16& input_text =
[email protected]14710852013-02-05 23:45:41846 is_keyword ? keyword_input_.text() : input_.text();
[email protected]51124552011-07-16 01:37:10847 bool input_multiple_words = HasMultipleWords(input_text);
848
[email protected]55ce8f12012-05-09 04:44:08849 SuggestResults scored_results;
850 if (!prevent_inline_autocomplete && input_multiple_words) {
851 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
852 // queries if the input also has multiple words. But if we were already
[email protected]f13a2132014-04-22 08:13:52853 // scoring a multi-word, multi-visit query aggressively, and the current
854 // input is still a prefix of it, then changing the suggestion suddenly
855 // feels wrong. To detect this case, first score as if only one word has
856 // been typed, then check if the best result came from aggressive search
857 // history scoring. If it did, then just keep that score set. This
858 // 1200 the lowest possible score in CalculateRelevanceForHistory()'s
859 // aggressive-scoring curve.
[email protected]55ce8f12012-05-09 04:44:08860 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
861 false, input_text, is_keyword);
[email protected]f13a2132014-04-22 08:13:52862 if ((scored_results.front().relevance() < 1200) ||
[email protected]9dfb4d362013-04-05 02:15:12863 !HasMultipleWords(scored_results.front().suggestion()))
[email protected]55ce8f12012-05-09 04:44:08864 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:10865 }
[email protected]55ce8f12012-05-09 04:44:08866 if (scored_results.empty())
867 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
868 input_multiple_words, input_text,
869 is_keyword);
870 for (SuggestResults::const_iterator i(scored_results.begin());
871 i != scored_results.end(); ++i) {
[email protected]57482a72014-03-14 22:27:37872 AddMatchToMap(*i, std::string(), did_not_accept_suggestion, true, map);
[email protected]51124552011-07-16 01:37:10873 }
[email protected]31afdf72013-09-26 04:29:36874 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime",
875 base::TimeTicks::Now() - start_time);
[email protected]51124552011-07-16 01:37:10876}
877
[email protected]55ce8f12012-05-09 04:44:08878SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:10879 const HistoryResults& results,
880 bool base_prevent_inline_autocomplete,
881 bool input_multiple_words,
[email protected]0085863a2013-12-06 21:19:03882 const base::string16& input_text,
[email protected]51124552011-07-16 01:37:10883 bool is_keyword) {
[email protected]810ffba2012-06-12 01:07:48884 AutocompleteClassifier* classifier =
885 AutocompleteClassifierFactory::GetForProfile(profile_);
[email protected]55ce8f12012-05-09 04:44:08886 SuggestResults scored_results;
[email protected]78e5e432013-08-03 02:10:10887 const bool prevent_search_history_inlining =
888 OmniboxFieldTrial::SearchHistoryPreventInlining(
889 input_.current_page_classification());
[email protected]c2ca3fd2014-03-22 03:07:44890 const base::string16& trimmed_input =
891 base::CollapseWhitespace(input_text, false);
[email protected]257ab712009-04-14 17:16:24892 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
893 ++i) {
[email protected]c2ca3fd2014-03-22 03:07:44894 const base::string16& trimmed_suggestion =
895 base::CollapseWhitespace(i->term, false);
896
[email protected]51124552011-07-16 01:37:10897 // Don't autocomplete multi-word queries that have only been seen once
898 // unless the user has typed more than one word.
899 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
[email protected]c2ca3fd2014-03-22 03:07:44900 (!input_multiple_words && (i->visits < 2) &&
901 HasMultipleWords(trimmed_suggestion));
[email protected]51124552011-07-16 01:37:10902
[email protected]ea3b9a502011-04-04 14:19:37903 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:10904 // when typed. For example, if the user searched for "google.com" and types
905 // "goog", don't autocomplete to the search term "google.com". Otherwise,
906 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:48907 // NOTE: We don't check this in the following cases:
908 // * When inline autocomplete is disabled, we won't be inline
909 // autocompleting this term, so we don't need to worry about confusion as
910 // much. This also prevents calling Classify() again from inside the
911 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:10912 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:48913 // * When the user has typed the whole term, the "what you typed" history
914 // match will outrank us for URL-like inputs anyway, so we need not do
915 // anything special.
[email protected]c2ca3fd2014-03-22 03:07:44916 if (!prevent_inline_autocomplete && classifier &&
917 (trimmed_suggestion != trimmed_input)) {
[email protected]ea3b9a502011-04-04 14:19:37918 AutocompleteMatch match;
[email protected]c2ca3fd2014-03-22 03:07:44919 classifier->Classify(trimmed_suggestion, false, false,
[email protected]51abb7b2014-02-09 23:00:08920 input_.current_page_classification(), &match, NULL);
[email protected]2905f742011-10-13 03:51:58921 prevent_inline_autocomplete =
[email protected]749e7ae02012-09-05 18:47:46922 !AutocompleteMatch::IsSearchType(match.type);
[email protected]ea3b9a502011-04-04 14:19:37923 }
[email protected]51124552011-07-16 01:37:10924
[email protected]78e5e432013-08-03 02:10:10925 int relevance = CalculateRelevanceForHistory(
926 i->time, is_keyword, !prevent_inline_autocomplete,
927 prevent_search_history_inlining);
[email protected]2c03c06b2013-12-11 20:45:02928 scored_results.push_back(SuggestResult(
[email protected]c2ca3fd2014-03-22 03:07:44929 trimmed_suggestion, AutocompleteMatchType::SEARCH_HISTORY,
930 trimmed_suggestion, base::string16(), base::string16(), std::string(),
931 std::string(), is_keyword, relevance, false, false, trimmed_input));
[email protected]257ab712009-04-14 17:16:24932 }
[email protected]51124552011-07-16 01:37:10933
934 // History returns results sorted for us. However, we may have docked some
935 // results' scores, so things are no longer in order. Do a stable sort to get
936 // things back in order without otherwise disturbing results with equal
937 // scores, then force the scores to be unique, so that the order in which
938 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:08939 std::stable_sort(scored_results.begin(), scored_results.end(),
940 CompareScoredResults());
[email protected]51124552011-07-16 01:37:10941 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:08942 for (SuggestResults::iterator i(scored_results.begin());
943 i != scored_results.end(); ++i) {
944 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
945 i->set_relevance(last_relevance - 1);
946 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:10947 }
948
[email protected]55ce8f12012-05-09 04:44:08949 return scored_results;
[email protected]257ab712009-04-14 17:16:24950}
951
[email protected]55ce8f12012-05-09 04:44:08952void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
[email protected]987fad782013-08-28 06:23:18953 const std::string& metadata,
[email protected]55ce8f12012-05-09 04:44:08954 MatchMap* map) {
[email protected]9487b392014-02-14 02:48:18955 for (size_t i = 0; i < results.size(); ++i)
[email protected]57482a72014-03-14 22:27:37956 AddMatchToMap(results[i], metadata, i, false, map);
initial.commit09911bf2008-07-26 23:55:29957}
958
[email protected]d30268a2013-06-25 22:31:07959int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const {
[email protected]dc6943b2012-06-19 06:39:56960 // Use the suggested verbatim relevance score if it is non-negative (valid),
961 // if inline autocomplete isn't prevented (always show verbatim on backspace),
[email protected]1beee342012-06-19 22:22:28962 // and if it won't suppress verbatim, leaving no default provider matches.
963 // Otherwise, if the default provider returned no matches and was still able
[email protected]dc6943b2012-06-19 06:39:56964 // to suppress verbatim, the user would have no search/nav matches and may be
[email protected]1beee342012-06-19 22:22:28965 // left unable to search using their default provider from the omnibox.
[email protected]dc6943b2012-06-19 06:39:56966 // Check for results on each verbatim calculation, as results from older
967 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:23968 bool use_server_relevance =
969 (default_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:28970 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:23971 ((default_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:24972 !default_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:23973 !default_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:07974 if (relevance_from_server)
975 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:23976 return use_server_relevance ?
977 default_results_.verbatim_relevance : CalculateRelevanceForVerbatim();
[email protected]382a0642012-06-06 06:13:52978}
[email protected]d1f0a7f2012-06-05 10:26:42979
[email protected]382a0642012-06-06 06:13:52980int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:47981 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:36982 return 250;
[email protected]dab8d52d2013-03-05 07:35:28983 return CalculateRelevanceForVerbatimIgnoringKeywordModeState();
984}
[email protected]52d08b12009-10-19 18:42:36985
[email protected]dab8d52d2013-03-05 07:35:28986int SearchProvider::
987 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const {
initial.commit09911bf2008-07-26 23:55:29988 switch (input_.type()) {
989 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:36990 case AutocompleteInput::QUERY:
991 case AutocompleteInput::FORCED_QUERY:
[email protected]90fe2bb2013-01-15 03:42:13992 return kNonURLVerbatimRelevance;
initial.commit09911bf2008-07-26 23:55:29993
initial.commit09911bf2008-07-26 23:55:29994 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:36995 return 850;
initial.commit09911bf2008-07-26 23:55:29996
997 default:
998 NOTREACHED();
999 return 0;
1000 }
1001}
1002
[email protected]d30268a2013-06-25 22:31:071003int SearchProvider::GetKeywordVerbatimRelevance(
1004 bool* relevance_from_server) const {
[email protected]dab8d52d2013-03-05 07:35:281005 // Use the suggested verbatim relevance score if it is non-negative (valid),
1006 // if inline autocomplete isn't prevented (always show verbatim on backspace),
1007 // and if it won't suppress verbatim, leaving no keyword provider matches.
1008 // Otherwise, if the keyword provider returned no matches and was still able
1009 // to suppress verbatim, the user would have no search/nav matches and may be
1010 // left unable to search using their keyword provider from the omnibox.
1011 // Check for results on each verbatim calculation, as results from older
1012 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:231013 bool use_server_relevance =
1014 (keyword_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:281015 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:231016 ((keyword_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:241017 !keyword_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:231018 !keyword_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:071019 if (relevance_from_server)
1020 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:231021 return use_server_relevance ?
1022 keyword_results_.verbatim_relevance :
1023 CalculateRelevanceForKeywordVerbatim(keyword_input_.type(),
1024 keyword_input_.prefer_keyword());
[email protected]5423e562013-02-07 03:58:451025}
1026
[email protected]51124552011-07-16 01:37:101027int SearchProvider::CalculateRelevanceForHistory(
[email protected]bc8bb0cd2013-06-24 21:50:231028 const base::Time& time,
[email protected]51124552011-07-16 01:37:101029 bool is_keyword,
[email protected]78e5e432013-08-03 02:10:101030 bool use_aggressive_method,
1031 bool prevent_search_history_inlining) const {
[email protected]aa613d62010-11-09 20:40:181032 // The relevance of past searches falls off over time. There are two distinct
1033 // equations used. If the first equation is used (searches to the primary
[email protected]78e5e432013-08-03 02:10:101034 // provider that we want to score aggressively), the score is in the range
1035 // 1300-1599 (unless |prevent_search_history_inlining|, in which case
[email protected]d8cd76b2013-07-10 09:46:161036 // it's in the range 1200-1299). If the second equation is used the
1037 // relevance of a search 15 minutes ago is discounted 50 points, while the
1038 // relevance of a search two weeks ago is discounted 450 points.
[email protected]bc8bb0cd2013-06-24 21:50:231039 double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0);
[email protected]188b50c2013-03-28 07:19:421040 bool is_primary_provider = is_keyword || !providers_.has_keyword_provider();
[email protected]78e5e432013-08-03 02:10:101041 if (is_primary_provider && use_aggressive_method) {
[email protected]aa613d62010-11-09 20:40:181042 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:101043 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:181044 if (elapsed_time < autocomplete_time) {
[email protected]d8cd76b2013-07-10 09:46:161045 int max_score = is_keyword ? 1599 : 1399;
[email protected]78e5e432013-08-03 02:10:101046 if (prevent_search_history_inlining)
[email protected]d8cd76b2013-07-10 09:46:161047 max_score = 1299;
1048 return max_score - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:181049 std::pow(elapsed_time / autocomplete_time, 2.5));
1050 }
1051 elapsed_time -= autocomplete_time;
1052 }
1053
[email protected]c3a4bd992010-08-18 20:25:011054 const int score_discount =
1055 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:291056
[email protected]6c85aa02009-02-27 12:08:091057 // Don't let scores go below 0. Negative relevance scores are meaningful in
1058 // a different way.
initial.commit09911bf2008-07-26 23:55:291059 int base_score;
[email protected]51124552011-07-16 01:37:101060 if (is_primary_provider)
[email protected]52d08b12009-10-19 18:42:361061 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:101062 else
1063 base_score = 200;
initial.commit09911bf2008-07-26 23:55:291064 return std::max(0, base_score - score_discount);
1065}
1066
initial.commit09911bf2008-07-26 23:55:291067AutocompleteMatch SearchProvider::NavigationToMatch(
[email protected]188b50c2013-03-28 07:19:421068 const NavigationResult& navigation) {
[email protected]5889bfb2014-03-19 00:26:481069 base::string16 input;
1070 const bool trimmed_whitespace = base::TrimWhitespace(
1071 navigation.from_keyword_provider() ?
1072 keyword_input_.text() : input_.text(),
1073 base::TRIM_TRAILING, &input) != base::TRIM_NONE;
[email protected]55ce8f12012-05-09 04:44:081074 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:471075 navigation.type());
[email protected]55ce8f12012-05-09 04:44:081076 match.destination_url = navigation.url();
[email protected]78981d8c2014-05-09 15:05:471077 BaseSearchProvider::SetDeletionURL(navigation.deletion_url(), &match);
[email protected]23db6492014-01-16 02:35:301078 // First look for the user's input inside the formatted url as it would be
[email protected]371dab12012-06-01 03:23:551079 // without trimming the scheme, so we can find matches at the beginning of the
1080 // scheme.
[email protected]371dab12012-06-01 03:23:551081 const URLPrefix* prefix =
[email protected]23db6492014-01-16 02:35:301082 URLPrefix::BestURLPrefix(navigation.formatted_url(), input);
[email protected]371dab12012-06-01 03:23:551083 size_t match_start = (prefix == NULL) ?
[email protected]23db6492014-01-16 02:35:301084 navigation.formatted_url().find(input) : prefix->prefix.length();
[email protected]d2445c82013-11-04 22:28:351085 bool trim_http = !AutocompleteInput::HasHTTPScheme(input) &&
1086 (!prefix || (match_start != 0));
[email protected]23db6492014-01-16 02:35:301087 const net::FormatUrlTypes format_types =
1088 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
[email protected]371dab12012-06-01 03:23:551089
[email protected]371dab12012-06-01 03:23:551090 const std::string languages(
1091 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]23db6492014-01-16 02:35:301092 size_t inline_autocomplete_offset = (prefix == NULL) ?
1093 base::string16::npos : (match_start + input.length());
[email protected]371dab12012-06-01 03:23:551094 match.fill_into_edit +=
1095 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1096 net::FormatUrl(navigation.url(), languages, format_types,
1097 net::UnescapeRule::SPACES, NULL, NULL,
1098 &inline_autocomplete_offset));
[email protected]14119032013-11-07 08:14:261099 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1100 // Otherwise, user edits to a suggestion would show non-Search results.
1101 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
[email protected]670d3232013-12-24 17:58:581102 match.fill_into_edit.insert(0, base::ASCIIToUTF16("?"));
[email protected]0085863a2013-12-06 21:19:031103 if (inline_autocomplete_offset != base::string16::npos)
[email protected]14119032013-11-07 08:14:261104 ++inline_autocomplete_offset;
1105 }
[email protected]6c94a1022014-02-21 03:48:041106 if (inline_autocomplete_offset != base::string16::npos) {
[email protected]518024c2013-07-19 23:40:251107 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
1108 match.inline_autocompletion =
1109 match.fill_into_edit.substr(inline_autocomplete_offset);
1110 }
[email protected]6c94a1022014-02-21 03:48:041111 // An inlineable navsuggestion can only be the default match when there
1112 // is no keyword provider active, lest it appear first and break the user
[email protected]5889bfb2014-03-19 00:26:481113 // out of keyword mode. It can also only be default if either the inline
1114 // autocompletion is empty or we're not preventing inline autocompletion.
1115 // Finally, if we have an inlineable navsuggestion with an inline completion
1116 // that we're not preventing, make sure we didn't trim any whitespace.
1117 // We don't want to claim http://foo.com/bar is inlineable against the
1118 // input "foo.com/b ".
[email protected]6c94a1022014-02-21 03:48:041119 match.allowed_to_be_default_match = navigation.IsInlineable(input) &&
1120 (providers_.GetKeywordProviderURL() == NULL) &&
[email protected]5889bfb2014-03-19 00:26:481121 (match.inline_autocompletion.empty() ||
[email protected]78981d8c2014-05-09 15:05:471122 (!input_.prevent_inline_autocomplete() && !trimmed_whitespace));
[email protected]371dab12012-06-01 03:23:551123
[email protected]23db6492014-01-16 02:35:301124 match.contents = navigation.match_contents();
1125 match.contents_class = navigation.match_contents_class();
[email protected]55ce8f12012-05-09 04:44:081126 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551127 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1128 ACMatchClassification::NONE, &match.description_class);
[email protected]d30268a2013-06-25 22:31:071129
1130 match.RecordAdditionalInfo(
1131 kRelevanceFromServerKey,
1132 navigation.relevance_from_server() ? kTrue : kFalse);
[email protected]987fad782013-08-28 06:23:181133 match.RecordAdditionalInfo(kShouldPrefetchKey, kFalse);
[email protected]d30268a2013-06-25 22:31:071134
initial.commit09911bf2008-07-26 23:55:291135 return match;
1136}
[email protected]4ab4c7c2010-11-24 04:49:341137
[email protected]4ab4c7c2010-11-24 04:49:341138void SearchProvider::UpdateDone() {
[email protected]2cdf1172012-08-26 12:21:331139 // We're done when the timer isn't running, there are no suggest queries
[email protected]5fac3782013-03-06 09:32:311140 // pending, and we're not waiting on Instant.
[email protected]e1290ee62013-06-26 18:31:151141 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0);
[email protected]4ab4c7c2010-11-24 04:49:341142}
[email protected]20184242014-05-14 02:57:421143
1144std::string SearchProvider::GetSessionToken() {
1145 base::TimeTicks current_time(base::TimeTicks::Now());
1146 // Renew token if it expired.
1147 if (current_time > token_expiration_time_) {
1148 const size_t kTokenBytes = 12;
1149 std::string raw_data;
1150 base::RandBytes(WriteInto(&raw_data, kTokenBytes + 1), kTokenBytes);
1151 base::Base64Encode(raw_data, &current_token_);
1152 }
1153
1154 // Extend expiration time another 60 seconds.
1155 token_expiration_time_ = current_time + base::TimeDelta::FromSeconds(60);
1156
1157 return current_token_;
1158}