blob: df2b04a6720388e22b1472e94ff58285e0418ca3 [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]a817ed392014-06-27 05:03:0026#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
[email protected]2c812ba02011-07-14 00:23:1527#include "chrome/browser/autocomplete/keyword_provider.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]ab7780792013-01-10 01:26:0930#include "chrome/browser/metrics/variations/variations_http_header_provider.h"
[email protected]684fcc12013-03-08 22:50:0231#include "chrome/browser/omnibox/omnibox_field_trial.h"
[email protected]8ecad5e2010-12-02 21:18:3332#include "chrome/browser/profiles/profile.h"
[email protected]a7b8e43d2013-03-18 18:52:4333#include "chrome/browser/search/search.h"
[email protected]8e5c89a2011-06-07 18:13:3334#include "chrome/browser/search_engines/template_url_service.h"
35#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]c4a9d4c2013-08-10 07:06:2736#include "chrome/browser/ui/search/instant_controller.h"
[email protected]20184242014-05-14 02:57:4237#include "chrome/common/chrome_switches.h"
initial.commit09911bf2008-07-26 23:55:2938#include "chrome/common/pref_names.h"
[email protected]ebeb8612014-06-17 01:54:5039#include "components/autocomplete/url_prefix.h"
[email protected]8e44a5b02014-06-19 19:03:2440#include "components/google/core/browser/google_util.h"
[email protected]e3ce70ac2014-06-26 18:34:5641#include "components/history/core/browser/in_memory_database.h"
[email protected]73b2d1e72014-06-25 23:45:3642#include "components/history/core/browser/keyword_search_term.h"
[email protected]3dc75b12014-06-08 00:02:2243#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]0915b352014-06-25 19:58:1444#include "components/search_engines/template_url_prepopulate_data.h"
[email protected]7267ff82013-12-03 17:20:3045#include "content/public/browser/user_metrics.h"
[email protected]34ac8f32009-02-22 23:03:2746#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2947#include "net/base/escape.h"
[email protected]d3cf8682f02012-02-29 23:29:3448#include "net/base/load_flags.h"
[email protected]371dab12012-06-01 03:23:5549#include "net/base/net_util.h"
[email protected]bd3b4712012-12-18 17:01:3050#include "net/http/http_request_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]cca6f392014-05-28 21:32:2654#include "url/url_constants.h"
[email protected]761fa4702013-07-02 15:25:1555#include "url/url_util.h"
initial.commit09911bf2008-07-26 23:55:2956
[email protected]bc8bb0cd2013-06-24 21:50:2357// 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]0085863a2013-12-06 21:19:0385bool HasMultipleWords(const base::string16& text) {
[email protected]51124552011-07-16 01:37:1086 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]3954c3a2012-04-10 20:17:55102// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:58103
[email protected]85b8d6f2012-05-08 20:53:47104SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
[email protected]02346202014-02-05 05:18:30105 : template_url_service_(template_url_service) {}
[email protected]85b8d6f2012-05-08 20:53:47106
107const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
108 return default_provider_.empty() ? NULL :
109 template_url_service_->GetTemplateURLForKeyword(default_provider_);
110}
111
112const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
113 return keyword_provider_.empty() ? NULL :
114 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:24115}
116
[email protected]3954c3a2012-04-10 20:17:55117
[email protected]bc8bb0cd2013-06-24 21:50:23118// SearchProvider::CompareScoredResults ---------------------------------------
119
120class SearchProvider::CompareScoredResults {
121 public:
122 bool operator()(const Result& a, const Result& b) {
123 // Sort in descending relevance order.
124 return a.relevance() > b.relevance();
125 }
126};
127
128
[email protected]3954c3a2012-04-10 20:17:55129// SearchProvider -------------------------------------------------------------
130
131// static
[email protected]24dbf2302013-04-05 16:59:25132int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100;
[email protected]3954c3a2012-04-10 20:17:55133
[email protected]bc8bb0cd2013-06-24 21:50:23134SearchProvider::SearchProvider(AutocompleteProviderListener* listener,
135 Profile* profile)
[email protected]02346202014-02-05 05:18:30136 : BaseSearchProvider(listener, profile, AutocompleteProvider::TYPE_SEARCH),
[email protected]cfa164bf2014-03-19 11:51:15137 providers_(TemplateURLServiceFactory::GetForProfile(profile)) {
[email protected]bc8bb0cd2013-06-24 21:50:23138}
139
[email protected]cb86ee6f2013-04-28 16:58:15140// static
[email protected]987fad782013-08-28 06:23:18141std::string SearchProvider::GetSuggestMetadata(const AutocompleteMatch& match) {
142 return match.GetAdditionalInfo(kSuggestMetadataKey);
143}
144
[email protected]bc8bb0cd2013-06-24 21:50:23145void SearchProvider::ResetSession() {
146 field_trial_triggered_in_session_ = false;
[email protected]4ab4c7c2010-11-24 04:49:34147}
148
[email protected]bc8bb0cd2013-06-24 21:50:23149SearchProvider::~SearchProvider() {
150}
151
[email protected]ee6110b2014-01-09 22:26:31152void SearchProvider::UpdateMatchContentsClass(const base::string16& input_text,
[email protected]23db6492014-01-16 02:35:30153 Results* results) {
154 for (SuggestResults::iterator sug_it = results->suggest_results.begin();
155 sug_it != results->suggest_results.end(); ++sug_it) {
[email protected]ee6110b2014-01-09 22:26:31156 sug_it->ClassifyMatchContents(false, input_text);
157 }
[email protected]23db6492014-01-16 02:35:30158 const std::string languages(
159 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
160 for (NavigationResults::iterator nav_it = results->navigation_results.begin();
161 nav_it != results->navigation_results.end(); ++nav_it) {
162 nav_it->CalculateAndClassifyMatchContents(false, input_text, languages);
163 }
[email protected]ee6110b2014-01-09 22:26:31164}
165
166// static
[email protected]bc8bb0cd2013-06-24 21:50:23167int SearchProvider::CalculateRelevanceForKeywordVerbatim(
[email protected]332d17d22014-06-20 16:56:03168 metrics::OmniboxInputType::Type type,
[email protected]bc8bb0cd2013-06-24 21:50:23169 bool prefer_keyword) {
170 // This function is responsible for scoring verbatim query matches
171 // for non-extension keywords. KeywordProvider::CalculateRelevance()
172 // scores verbatim query matches for extension keywords, as well as
173 // for keyword matches (i.e., suggestions of a keyword itself, not a
174 // suggestion of a query on a keyword search engine). These two
175 // functions are currently in sync, but there's no reason we
176 // couldn't decide in the future to score verbatim matches
177 // differently for extension and non-extension keywords. If you
178 // make such a change, however, you should update this comment to
179 // describe it, so it's clear why the functions diverge.
180 if (prefer_keyword)
181 return 1500;
[email protected]3dc75b12014-06-08 00:02:22182 return (type == metrics::OmniboxInputType::QUERY) ? 1450 : 1100;
[email protected]bc8bb0cd2013-06-24 21:50:23183}
184
initial.commit09911bf2008-07-26 23:55:29185void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27186 bool minimal_changes) {
[email protected]04504c242013-01-22 21:08:55187 // Do our best to load the model as early as possible. This will reduce
188 // odds of having the model not ready when really needed (a non-empty input).
189 TemplateURLService* model = providers_.template_url_service();
190 DCHECK(model);
191 model->Load();
192
initial.commit09911bf2008-07-26 23:55:29193 matches_.clear();
[email protected]618d6e62012-12-16 05:55:57194 field_trial_triggered_ = false;
initial.commit09911bf2008-07-26 23:55:29195
[email protected]6c85aa02009-02-27 12:08:09196 // Can't return search/suggest results for bogus input or without a profile.
[email protected]3dc75b12014-06-08 00:02:22197 if (!profile_ || (input.type() == metrics::OmniboxInputType::INVALID)) {
[email protected]9950e5d52014-02-25 23:34:19198 Stop(true);
initial.commit09911bf2008-07-26 23:55:29199 return;
200 }
201
[email protected]14710852013-02-05 23:45:41202 keyword_input_ = input;
[email protected]257ab712009-04-14 17:16:24203 const TemplateURL* keyword_provider =
[email protected]14710852013-02-05 23:45:41204 KeywordProvider::GetSubstitutingTemplateURLForInput(model,
205 &keyword_input_);
206 if (keyword_provider == NULL)
207 keyword_input_.Clear();
208 else if (keyword_input_.text().empty())
[email protected]257ab712009-04-14 17:16:24209 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24210
[email protected]85b8d6f2012-05-08 20:53:47211 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]ce7ee5f2014-06-16 23:41:19212 if (default_provider &&
213 !default_provider->SupportsReplacement(model->search_terms_data()))
[email protected]257ab712009-04-14 17:16:24214 default_provider = NULL;
215
216 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18217 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24218
219 if (!default_provider && !keyword_provider) {
220 // No valid providers.
[email protected]9950e5d52014-02-25 23:34:19221 Stop(true);
initial.commit09911bf2008-07-26 23:55:29222 return;
223 }
224
225 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24226 // or the providers, abort the query.
[email protected]0085863a2013-12-06 21:19:03227 base::string16 default_provider_keyword(default_provider ?
228 default_provider->keyword() : base::string16());
229 base::string16 keyword_provider_keyword(keyword_provider ?
230 keyword_provider->keyword() : base::string16());
[email protected]9e789742011-01-10 23:27:32231 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47232 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]bb900e02013-03-14 14:15:29233 // Cancel any in-flight suggest requests.
[email protected]e1290ee62013-06-26 18:31:15234 if (!done_)
[email protected]e29249dc52012-07-19 17:33:50235 Stop(false);
[email protected]257ab712009-04-14 17:16:24236 }
initial.commit09911bf2008-07-26 23:55:29237
[email protected]85b8d6f2012-05-08 20:53:47238 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29239
240 if (input.text().empty()) {
241 // User typed "?" alone. Give them a placeholder result indicating what
242 // this syntax does.
[email protected]257ab712009-04-14 17:16:24243 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00244 AutocompleteMatch match;
245 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36246 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24247 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35248 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47249 match.keyword = providers_.default_provider();
[email protected]45f89a92013-08-12 13:41:36250 match.allowed_to_be_default_match = true;
[email protected]257ab712009-04-14 17:16:24251 matches_.push_back(match);
252 }
[email protected]9950e5d52014-02-25 23:34:19253 Stop(true);
initial.commit09911bf2008-07-26 23:55:29254 return;
255 }
256
257 input_ = input;
258
[email protected]e1290ee62013-06-26 18:31:15259 DoHistoryQuery(minimal_changes);
260 StartOrStopSuggestQuery(minimal_changes);
[email protected]344946a12012-12-20 12:03:42261 UpdateMatches();
initial.commit09911bf2008-07-26 23:55:29262}
263
[email protected]d4a94b92014-03-04 01:35:22264void SearchProvider::SortResults(bool is_keyword,
265 const base::ListValue* relevances,
266 Results* results) {
267 // Ignore suggested scores for non-keyword matches in keyword mode; if the
268 // server is allowed to score these, it could interfere with the user's
269 // ability to get good keyword results.
270 const bool abandon_suggested_scores =
271 !is_keyword && !providers_.keyword_provider().empty();
272 // Apply calculated relevance scores to suggestions if a valid list was
273 // not provided or we're abandoning suggested scores entirely.
274 if ((relevances == NULL) || abandon_suggested_scores) {
275 ApplyCalculatedSuggestRelevance(&results->suggest_results);
276 ApplyCalculatedNavigationRelevance(&results->navigation_results);
277 // If abandoning scores entirely, also abandon the verbatim score.
278 if (abandon_suggested_scores)
279 results->verbatim_relevance = -1;
280 }
281
282 // Keep the result lists sorted.
283 const CompareScoredResults comparator = CompareScoredResults();
284 std::stable_sort(results->suggest_results.begin(),
285 results->suggest_results.end(),
286 comparator);
287 std::stable_sort(results->navigation_results.begin(),
288 results->navigation_results.end(),
289 comparator);
290}
291
[email protected]cfa164bf2014-03-19 11:51:15292const TemplateURL* SearchProvider::GetTemplateURL(bool is_keyword) const {
293 return is_keyword ? providers_.GetKeywordProviderURL()
294 : providers_.GetDefaultProviderURL();
[email protected]9487b392014-02-14 02:48:18295}
296
[email protected]d4a94b92014-03-04 01:35:22297const AutocompleteInput SearchProvider::GetInput(bool is_keyword) const {
298 return is_keyword ? keyword_input_ : input_;
[email protected]9487b392014-02-14 02:48:18299}
300
[email protected]cfa164bf2014-03-19 11:51:15301BaseSearchProvider::Results* SearchProvider::GetResultsToFill(bool is_keyword) {
302 return is_keyword ? &keyword_results_ : &default_results_;
303}
304
[email protected]9487b392014-02-14 02:48:18305bool SearchProvider::ShouldAppendExtraParams(
306 const SuggestResult& result) const {
307 return !result.from_keyword_provider() ||
308 providers_.default_provider().empty();
309}
310
[email protected]ef6866f2014-02-18 08:26:34311void SearchProvider::StopSuggest() {
312 // Increment the appropriate field in the histogram by the number of
313 // pending requests that were invalidated.
314 for (int i = 0; i < suggest_results_pending_; ++i)
315 LogOmniboxSuggestRequest(REQUEST_INVALIDATED);
316 suggest_results_pending_ = 0;
317 timer_.Stop();
318 // Stop any in-progress URL fetches.
319 keyword_fetcher_.reset();
320 default_fetcher_.reset();
321}
322
323void SearchProvider::ClearAllResults() {
324 keyword_results_.Clear();
325 default_results_.Clear();
326}
327
[email protected]d4a94b92014-03-04 01:35:22328int SearchProvider::GetDefaultResultRelevance() const {
329 return -1;
330}
331
[email protected]7267ff82013-12-03 17:20:30332void SearchProvider::RecordDeletionResult(bool success) {
333 if (success) {
334 content::RecordAction(
[email protected]e6e30ac2014-01-13 21:24:39335 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Success"));
[email protected]7267ff82013-12-03 17:20:30336 } else {
337 content::RecordAction(
[email protected]e6e30ac2014-01-13 21:24:39338 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure"));
[email protected]7267ff82013-12-03 17:20:30339 }
340}
341
[email protected]cfa164bf2014-03-19 11:51:15342void SearchProvider::LogFetchComplete(bool success, bool is_keyword) {
343 LogOmniboxSuggestRequest(REPLY_RECEIVED);
344 // Record response time for suggest requests sent to Google. We care
345 // only about the common case: the Google default provider used in
346 // non-keyword mode.
347 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
348 if (!is_keyword && default_url &&
[email protected]ce7ee5f2014-06-16 23:41:19349 (TemplateURLPrepopulateData::GetEngineType(
350 *default_url,
351 providers_.template_url_service()->search_terms_data()) ==
[email protected]cfa164bf2014-03-19 11:51:15352 SEARCH_ENGINE_GOOGLE)) {
353 const base::TimeDelta elapsed_time =
354 base::TimeTicks::Now() - time_suggest_request_sent_;
355 if (success) {
356 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
357 elapsed_time);
358 } else {
359 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
360 elapsed_time);
361 }
362 }
363}
364
365bool SearchProvider::IsKeywordFetcher(const net::URLFetcher* fetcher) const {
366 return fetcher == keyword_fetcher_.get();
367}
368
369void SearchProvider::UpdateMatches() {
[email protected]cfa164bf2014-03-19 11:51:15370 ConvertResultsToAutocompleteMatches();
371
372 // Check constraints that may be violated by suggested relevances.
373 if (!matches_.empty() &&
374 (default_results_.HasServerProvidedScores() ||
375 keyword_results_.HasServerProvidedScores())) {
376 // These blocks attempt to repair undesirable behavior by suggested
377 // relevances with minimal impact, preserving other suggested relevances.
378
[email protected]cfa164bf2014-03-19 11:51:15379 if (!HasKeywordDefaultMatchInKeywordMode()) {
380 // In keyword mode, disregard the keyword verbatim suggested relevance
381 // if necessary so there at least one keyword match that's allowed to
382 // be the default match.
383 keyword_results_.verbatim_relevance = -1;
384 ConvertResultsToAutocompleteMatches();
385 }
[email protected]89bd27d12014-04-12 17:36:23386 if (IsTopMatchSearchWithURLInput()) {
[email protected]cfa164bf2014-03-19 11:51:15387 // Disregard the suggested search and verbatim relevances if the input
388 // type is URL and the top match is a highly-ranked search suggestion.
389 // For example, prevent a search for "foo.com" from outranking another
390 // provider's navigation for "foo.com" or "foo.com/url_from_history".
391 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
392 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
393 default_results_.verbatim_relevance = -1;
394 keyword_results_.verbatim_relevance = -1;
395 ConvertResultsToAutocompleteMatches();
396 }
[email protected]89bd27d12014-04-12 17:36:23397 if (FindTopMatch() == matches_.end()) {
398 // Guarantee that SearchProvider returns a legal default match. (The
399 // omnibox always needs at least one legal default match, and it relies
400 // on SearchProvider to always return one.)
[email protected]cfa164bf2014-03-19 11:51:15401 ApplyCalculatedRelevance();
402 ConvertResultsToAutocompleteMatches();
403 }
[email protected]cfa164bf2014-03-19 11:51:15404 DCHECK(HasKeywordDefaultMatchInKeywordMode());
[email protected]89bd27d12014-04-12 17:36:23405 DCHECK(!IsTopMatchSearchWithURLInput());
406 DCHECK(FindTopMatch() != matches_.end());
[email protected]cfa164bf2014-03-19 11:51:15407 }
408 UMA_HISTOGRAM_CUSTOM_COUNTS(
409 "Omnibox.SearchProviderMatches", matches_.size(), 1, 6, 7);
410
411 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
412 if ((keyword_url != NULL) && HasKeywordDefaultMatchInKeywordMode()) {
413 // If there is a keyword match that is allowed to be the default match,
414 // then prohibit default provider matches from being the default match lest
415 // such matches cause the user to break out of keyword mode.
416 for (ACMatches::iterator it = matches_.begin(); it != matches_.end();
417 ++it) {
418 if (it->keyword != keyword_url->keyword())
419 it->allowed_to_be_default_match = false;
420 }
421 }
422
423 base::TimeTicks update_starred_start_time(base::TimeTicks::Now());
424 UpdateStarredStateOfMatches();
425 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateStarredTime",
426 base::TimeTicks::Now() - update_starred_start_time);
427 UpdateDone();
[email protected]cfa164bf2014-03-19 11:51:15428}
429
[email protected]bc8bb0cd2013-06-24 21:50:23430void SearchProvider::Run() {
431 // Start a new request with the current input.
432 suggest_results_pending_ = 0;
433 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]abe441e2013-05-06 12:35:05434
[email protected]bc8bb0cd2013-06-24 21:50:23435 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
436 providers_.GetDefaultProviderURL(), input_));
437 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
438 providers_.GetKeywordProviderURL(), keyword_input_));
439
440 // Both the above can fail if the providers have been modified or deleted
441 // since the query began.
442 if (suggest_results_pending_ == 0) {
443 UpdateDone();
444 // We only need to update the listener if we're actually done.
445 if (done_)
446 listener_->OnProviderUpdate(false);
447 }
[email protected]601858c02010-09-01 17:08:20448}
449
[email protected]8d457132010-11-04 18:13:40450void SearchProvider::DoHistoryQuery(bool minimal_changes) {
451 // The history query results are synchronous, so if minimal_changes is true,
452 // we still have the last results and don't need to do anything.
453 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29454 return;
455
[email protected]8d457132010-11-04 18:13:40456 keyword_history_results_.clear();
457 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29458
[email protected]78e5e432013-08-03 02:10:10459 if (OmniboxFieldTrial::SearchHistoryDisable(
460 input_.current_page_classification()))
[email protected]d8cd76b2013-07-10 09:46:16461 return;
462
[email protected]8d457132010-11-04 18:13:40463 HistoryService* const history_service =
[email protected]9d2db762012-06-19 00:01:10464 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
[email protected]8d457132010-11-04 18:13:40465 history::URLDatabase* url_db = history_service ?
466 history_service->InMemoryDatabase() : NULL;
[email protected]8d457132010-11-04 18:13:40467 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29468 return;
469
[email protected]51124552011-07-16 01:37:10470 // Request history for both the keyword and default provider. We grab many
471 // more matches than we'll ultimately clamp to so that if there are several
472 // recent multi-word matches who scores are lowered (see
473 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
474 // matches. Note that this doesn't fix the problem entirely, but merely
475 // limits it to cases with a very large number of such multi-word matches; for
476 // now, this seems OK compared with the complexity of a real fix, which would
477 // require multiple searches and tracking of "single- vs. multi-word" in the
478 // database.
479 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47480 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
481 if (default_url) {
[email protected]b4bec972014-04-05 18:07:15482 const base::TimeTicks start_time = base::TimeTicks::Now();
[email protected]85b8d6f2012-05-08 20:53:47483 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
484 num_matches, &default_history_results_);
[email protected]31afdf72013-09-26 04:29:36485 UMA_HISTOGRAM_TIMES(
486 "Omnibox.SearchProvider.GetMostRecentKeywordTermsDefaultProviderTime",
487 base::TimeTicks::Now() - start_time);
[email protected]257ab712009-04-14 17:16:24488 }
[email protected]85b8d6f2012-05-08 20:53:47489 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
490 if (keyword_url) {
491 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]14710852013-02-05 23:45:41492 keyword_input_.text(), num_matches, &keyword_history_results_);
[email protected]3954c3a2012-04-10 20:17:55493 }
initial.commit09911bf2008-07-26 23:55:29494}
495
[email protected]6dc950f2012-07-16 19:49:08496void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]83c726482008-09-10 06:36:34497 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29498 StopSuggest();
[email protected]71b46152013-05-03 16:39:20499 ClearAllResults();
initial.commit09911bf2008-07-26 23:55:29500 return;
501 }
502
503 // For the minimal_changes case, if we finished the previous query and still
504 // have its results, or are allowed to keep running it, just do that, rather
505 // than starting a new query.
506 if (minimal_changes &&
[email protected]cc1526e2013-05-17 04:04:24507 (!default_results_.suggest_results.empty() ||
508 !default_results_.navigation_results.empty() ||
509 !keyword_results_.suggest_results.empty() ||
510 !keyword_results_.navigation_results.empty() ||
[email protected]a2770a7d2014-04-22 19:33:35511 (!done_ && input_.want_asynchronous_matches())))
initial.commit09911bf2008-07-26 23:55:29512 return;
513
514 // We can't keep running any previous query, so halt it.
515 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42516
517 // Remove existing results that cannot inline autocomplete the new input.
[email protected]71b46152013-05-03 16:39:20518 RemoveAllStaleResults();
initial.commit09911bf2008-07-26 23:55:29519
[email protected]ee6110b2014-01-09 22:26:31520 // Update the content classifications of remaining results so they look good
521 // against the current input.
[email protected]23db6492014-01-16 02:35:30522 UpdateMatchContentsClass(input_.text(), &default_results_);
523 if (!keyword_input_.text().empty())
524 UpdateMatchContentsClass(keyword_input_.text(), &keyword_results_);
[email protected]ee6110b2014-01-09 22:26:31525
initial.commit09911bf2008-07-26 23:55:29526 // We can't start a new query if we're only allowed synchronous results.
[email protected]a2770a7d2014-04-22 19:33:35527 if (!input_.want_asynchronous_matches())
initial.commit09911bf2008-07-26 23:55:29528 return;
529
[email protected]24dbf2302013-04-05 16:59:25530 // To avoid flooding the suggest server, don't send a query until at
531 // least 100 ms since the last query.
[email protected]515ffa942012-11-27 20:18:24532 base::TimeTicks next_suggest_time(time_suggest_request_sent_ +
[email protected]bc8bb0cd2013-06-24 21:50:23533 base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs));
[email protected]515ffa942012-11-27 20:18:24534 base::TimeTicks now(base::TimeTicks::Now());
535 if (now >= next_suggest_time) {
536 Run();
537 return;
538 }
539 timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29540}
541
[email protected]83c726482008-09-10 06:36:34542bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55543 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
544 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47545 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
546 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34547 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47548 ((!default_url || default_url->suggestions_url().empty()) &&
549 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34550 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
551 return false;
552
[email protected]cac59d32010-08-09 23:23:14553 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34554 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34555
[email protected]cac59d32010-08-09 23:23:14556 // FORCED_QUERY means the user is explicitly asking us to search for this, so
557 // we assume it isn't a URL and/or there isn't private data.
[email protected]3dc75b12014-06-08 00:02:22558 if (input_.type() == metrics::OmniboxInputType::FORCED_QUERY)
[email protected]cac59d32010-08-09 23:23:14559 return true;
[email protected]83c726482008-09-10 06:36:34560
[email protected]f608ea102013-03-18 15:08:09561 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't
562 // http/https/ftp, we shouldn't send it. Sending things like file: and data:
563 // is both a waste of time and a disclosure of potentially private, local
564 // data. Other "schemes" may actually be usernames, and we don't want to send
565 // passwords. If the scheme is OK, we still need to check other cases below.
566 // If this is QUERY, then the presence of these schemes means the user
567 // explicitly typed one, and thus this is probably a URL that's being entered
568 // and happens to currently be invalid -- in which case we again want to run
569 // our checks below. Other QUERY cases are less likely to be URLs and thus we
570 // assume we're OK.
[email protected]e8ca69c2014-05-07 15:31:19571 if (!LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) &&
572 !LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
[email protected]cca6f392014-05-28 21:32:26573 !LowerCaseEqualsASCII(input_.scheme(), url::kFtpScheme))
[email protected]3dc75b12014-06-08 00:02:22574 return (input_.type() == metrics::OmniboxInputType::QUERY);
[email protected]cac59d32010-08-09 23:23:14575
576 // Don't send URLs with usernames, queries or refs. Some of these are
577 // private, and the Suggest server is unlikely to have any useful results
578 // for any of them. Also don't send URLs with ports, as we may initially
579 // think that a username + password is a host + port (and we don't want to
580 // send usernames/passwords), and even if the port really is a port, the
581 // server is once again unlikely to have and useful results.
[email protected]825e16f2013-09-30 23:52:58582 // Note that we only block based on refs if the input is URL-typed, as search
583 // queries can legitimately have #s in them which the URL parser
584 // overaggressively categorizes as a url with a ref.
[email protected]b45334502014-04-30 19:44:05585 const url::Parsed& parts = input_.parts();
[email protected]cac59d32010-08-09 23:23:14586 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
[email protected]825e16f2013-09-30 23:52:58587 parts.query.is_nonempty() ||
[email protected]3dc75b12014-06-08 00:02:22588 (parts.ref.is_nonempty() &&
589 (input_.type() == metrics::OmniboxInputType::URL)))
[email protected]cac59d32010-08-09 23:23:14590 return false;
591
592 // Don't send anything for https except the hostname. Hostnames are OK
593 // because they are visible when the TCP connection is established, but the
594 // specific path may reveal private information.
[email protected]e8ca69c2014-05-07 15:31:19595 if (LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
[email protected]a2fedb1e2011-01-25 15:23:36596 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14597 return false;
[email protected]83c726482008-09-10 06:36:34598
599 return true;
600}
601
[email protected]71b46152013-05-03 16:39:20602void SearchProvider::RemoveAllStaleResults() {
[email protected]dc735c02013-11-12 23:23:41603 if (keyword_input_.text().empty()) {
[email protected]1e1550e2013-05-02 17:37:51604 // User is either in keyword mode with a blank input or out of
605 // keyword mode entirely.
[email protected]cc1526e2013-05-17 04:04:24606 keyword_results_.Clear();
[email protected]1e1550e2013-05-02 17:37:51607 }
[email protected]d1f0a7f2012-06-05 10:26:42608}
609
[email protected]d1f0a7f2012-06-05 10:26:42610void SearchProvider::ApplyCalculatedRelevance() {
[email protected]cc1526e2013-05-17 04:04:24611 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
612 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
613 ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results);
614 ApplyCalculatedNavigationRelevance(&default_results_.navigation_results);
[email protected]cc1526e2013-05-17 04:04:24615 default_results_.verbatim_relevance = -1;
616 keyword_results_.verbatim_relevance = -1;
[email protected]d1f0a7f2012-06-05 10:26:42617}
618
[email protected]188b50c2013-03-28 07:19:42619void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42620 for (size_t i = 0; i < list->size(); ++i) {
[email protected]188b50c2013-03-28 07:19:42621 SuggestResult& result = (*list)[i];
622 result.set_relevance(
623 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
624 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07625 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42626 }
627}
628
[email protected]188b50c2013-03-28 07:19:42629void SearchProvider::ApplyCalculatedNavigationRelevance(
630 NavigationResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42631 for (size_t i = 0; i < list->size(); ++i) {
[email protected]188b50c2013-03-28 07:19:42632 NavigationResult& result = (*list)[i];
633 result.set_relevance(
634 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
635 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07636 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42637 }
638}
639
[email protected]15fb2aa2012-05-22 22:52:59640net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12641 int id,
[email protected]9ff91722012-09-07 05:29:12642 const TemplateURL* template_url,
[email protected]14710852013-02-05 23:45:41643 const AutocompleteInput& input) {
[email protected]9ff91722012-09-07 05:29:12644 if (!template_url || template_url->suggestions_url().empty())
645 return NULL;
646
647 // Bail if the suggestion URL is invalid with the given replacements.
[email protected]14710852013-02-05 23:45:41648 TemplateURLRef::SearchTermsArgs search_term_args(input.text());
[email protected]420472b22014-06-10 13:34:43649 search_term_args.input_type = input.type();
[email protected]14710852013-02-05 23:45:41650 search_term_args.cursor_position = input.cursor_position();
[email protected]d5015ca2013-08-08 22:04:18651 search_term_args.page_classification = input.current_page_classification();
[email protected]7aa1fd92014-06-20 10:27:55652 if (OmniboxFieldTrial::EnableAnswersInSuggest())
[email protected]20184242014-05-14 02:57:42653 search_term_args.session_token = GetSessionToken();
[email protected]9ff91722012-09-07 05:29:12654 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19655 search_term_args,
656 providers_.template_url_service()->search_terms_data()));
[email protected]9ff91722012-09-07 05:29:12657 if (!suggest_url.is_valid())
658 return NULL;
[email protected]9b9fa672013-11-07 06:04:52659 // Send the current page URL if user setting and URL requirements are met and
660 // the user is in the field trial.
661 if (CanSendURL(current_page_url_, suggest_url, template_url,
662 input.current_page_classification(), profile_) &&
663 OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) {
664 search_term_args.current_page_url = current_page_url_.spec();
665 // Create the suggest URL again with the current page URL.
666 suggest_url = GURL(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19667 search_term_args,
668 providers_.template_url_service()->search_terms_data()));
[email protected]9b9fa672013-11-07 06:04:52669 }
[email protected]9ff91722012-09-07 05:29:12670
671 suggest_results_pending_++;
672 LogOmniboxSuggestRequest(REQUEST_SENT);
673
674 net::URLFetcher* fetcher =
675 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12676 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34677 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]bd3b4712012-12-18 17:01:30678 // Add Chrome experiment state to the request headers.
679 net::HttpRequestHeaders headers;
[email protected]ab7780792013-01-10 01:26:09680 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
[email protected]bd3b4712012-12-18 17:01:30681 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
682 fetcher->SetExtraRequestHeaders(headers.ToString());
[email protected]257ab712009-04-14 17:16:24683 fetcher->Start();
684 return fetcher;
685}
686
[email protected]344946a12012-12-20 12:03:42687void SearchProvider::ConvertResultsToAutocompleteMatches() {
initial.commit09911bf2008-07-26 23:55:29688 // Convert all the results to matches and add them to a map, so we can keep
689 // the most relevant match for each result.
[email protected]31afdf72013-09-26 04:29:36690 base::TimeTicks start_time(base::TimeTicks::Now());
initial.commit09911bf2008-07-26 23:55:29691 MatchMap map;
[email protected]bc8bb0cd2013-06-24 21:50:23692 const base::Time no_time;
[email protected]cc1526e2013-05-17 04:04:24693 int did_not_accept_keyword_suggestion =
694 keyword_results_.suggest_results.empty() ?
initial.commit09911bf2008-07-26 23:55:29695 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
696 TemplateURLRef::NO_SUGGESTION_CHOSEN;
initial.commit09911bf2008-07-26 23:55:29697
[email protected]d30268a2013-06-25 22:31:07698 bool relevance_from_server;
699 int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server);
[email protected]cc1526e2013-05-17 04:04:24700 int did_not_accept_default_suggestion =
701 default_results_.suggest_results.empty() ?
[email protected]55ce8f12012-05-09 04:44:08702 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
703 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]d1f0a7f2012-06-05 10:26:42704 if (verbatim_relevance > 0) {
[email protected]c2ca3fd2014-03-22 03:07:44705 const base::string16& trimmed_verbatim =
706 base::CollapseWhitespace(input_.text(), false);
[email protected]2c03c06b2013-12-11 20:45:02707 SuggestResult verbatim(
[email protected]c2ca3fd2014-03-22 03:07:44708 trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
[email protected]7ec287e2014-05-16 02:50:08709 trimmed_verbatim, base::string16(), base::string16(), base::string16(),
710 base::string16(), std::string(), std::string(), false,
711 verbatim_relevance, relevance_from_server, false,
[email protected]c2ca3fd2014-03-22 03:07:44712 trimmed_verbatim);
[email protected]57482a72014-03-14 22:27:37713 AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion,
714 false, &map);
[email protected]d1f0a7f2012-06-05 10:26:42715 }
[email protected]5423e562013-02-07 03:58:45716 if (!keyword_input_.text().empty()) {
717 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
718 // We only create the verbatim search query match for a keyword
719 // if it's not an extension keyword. Extension keywords are handled
720 // in KeywordProvider::Start(). (Extensions are complicated...)
721 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond
722 // to the keyword verbatim search query. Do not create other matches
723 // of type SEARCH_OTHER_ENGINE.
[email protected]bdcbcd82013-10-28 13:40:25724 if (keyword_url &&
725 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) {
[email protected]d30268a2013-06-25 22:31:07726 bool keyword_relevance_from_server;
727 const int keyword_verbatim_relevance =
728 GetKeywordVerbatimRelevance(&keyword_relevance_from_server);
[email protected]dab8d52d2013-03-05 07:35:28729 if (keyword_verbatim_relevance > 0) {
[email protected]c2ca3fd2014-03-22 03:07:44730 const base::string16& trimmed_verbatim =
731 base::CollapseWhitespace(keyword_input_.text(), false);
[email protected]2c03c06b2013-12-11 20:45:02732 SuggestResult verbatim(
[email protected]c2ca3fd2014-03-22 03:07:44733 trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE,
734 trimmed_verbatim, base::string16(), base::string16(),
[email protected]7ec287e2014-05-16 02:50:08735 base::string16(), base::string16(), std::string(), std::string(),
736 true, keyword_verbatim_relevance, keyword_relevance_from_server,
737 false, trimmed_verbatim);
[email protected]57482a72014-03-14 22:27:37738 AddMatchToMap(verbatim, std::string(),
739 did_not_accept_keyword_suggestion, false, &map);
[email protected]dab8d52d2013-03-05 07:35:28740 }
[email protected]5423e562013-02-07 03:58:45741 }
742 }
[email protected]257ab712009-04-14 17:16:24743 AddHistoryResultsToMap(keyword_history_results_, true,
744 did_not_accept_keyword_suggestion, &map);
745 AddHistoryResultsToMap(default_history_results_, false,
746 did_not_accept_default_suggestion, &map);
747
[email protected]d1cb6a822013-09-18 19:43:00748 AddSuggestResultsToMap(keyword_results_.suggest_results,
749 keyword_results_.metadata, &map);
[email protected]987fad782013-08-28 06:23:18750 AddSuggestResultsToMap(default_results_.suggest_results,
751 default_results_.metadata, &map);
initial.commit09911bf2008-07-26 23:55:29752
[email protected]d30268a2013-06-25 22:31:07753 ACMatches matches;
initial.commit09911bf2008-07-26 23:55:29754 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
[email protected]d30268a2013-06-25 22:31:07755 matches.push_back(i->second);
initial.commit09911bf2008-07-26 23:55:29756
[email protected]d30268a2013-06-25 22:31:07757 AddNavigationResultsToMatches(keyword_results_.navigation_results, &matches);
758 AddNavigationResultsToMatches(default_results_.navigation_results, &matches);
initial.commit09911bf2008-07-26 23:55:29759
[email protected]d30268a2013-06-25 22:31:07760 // Now add the most relevant matches to |matches_|. We take up to kMaxMatches
761 // suggest/navsuggest matches, regardless of origin. If Instant Extended is
762 // enabled and we have server-provided (and thus hopefully more accurate)
763 // scores for some suggestions, we allow more of those, until we reach
764 // AutocompleteResult::kMaxMatches total matches (that is, enough to fill the
765 // whole popup).
766 //
767 // We will always return any verbatim matches, no matter how we obtained their
768 // scores, unless we have already accepted AutocompleteResult::kMaxMatches
769 // higher-scoring matches under the conditions above.
[email protected]d30268a2013-06-25 22:31:07770 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant);
771 matches_.clear();
[email protected]3723e6e2012-06-11 21:06:56772
[email protected]d30268a2013-06-25 22:31:07773 size_t num_suggestions = 0;
774 for (ACMatches::const_iterator i(matches.begin());
775 (i != matches.end()) &&
776 (matches_.size() < AutocompleteResult::kMaxMatches);
777 ++i) {
778 // SEARCH_OTHER_ENGINE is only used in the SearchProvider for the keyword
779 // verbatim result, so this condition basically means "if this match is a
780 // suggestion of some sort".
781 if ((i->type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED) &&
782 (i->type != AutocompleteMatchType::SEARCH_OTHER_ENGINE)) {
783 // If we've already hit the limit on non-server-scored suggestions, and
784 // this isn't a server-scored suggestion we can add, skip it.
785 if ((num_suggestions >= kMaxMatches) &&
786 (!chrome::IsInstantExtendedAPIEnabled() ||
787 (i->GetAdditionalInfo(kRelevanceFromServerKey) != kTrue))) {
788 continue;
789 }
790
791 ++num_suggestions;
792 }
793
794 matches_.push_back(*i);
795 }
[email protected]31afdf72013-09-26 04:29:36796 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime",
797 base::TimeTicks::Now() - start_time);
[email protected]344946a12012-12-20 12:03:42798}
799
[email protected]89bd27d12014-04-12 17:36:23800ACMatches::const_iterator SearchProvider::FindTopMatch() const {
[email protected]0a8718b12013-11-13 18:41:31801 ACMatches::const_iterator it = matches_.begin();
802 while ((it != matches_.end()) && !it->allowed_to_be_default_match)
803 ++it;
804 return it;
[email protected]9dfb4d362013-04-05 02:15:12805}
806
[email protected]1c1e7842013-11-22 16:28:16807bool SearchProvider::HasKeywordDefaultMatchInKeywordMode() const {
808 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
809 // If the user is not in keyword mode, return true to say that this
810 // constraint is not violated.
811 if (keyword_url == NULL)
812 return true;
813 for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end();
814 ++it) {
815 if ((it->keyword == keyword_url->keyword()) &&
816 it->allowed_to_be_default_match)
817 return true;
818 }
819 return false;
820}
821
[email protected]89bd27d12014-04-12 17:36:23822bool SearchProvider::IsTopMatchSearchWithURLInput() const {
823 ACMatches::const_iterator first_match = FindTopMatch();
[email protected]3dc75b12014-06-08 00:02:22824 return (input_.type() == metrics::OmniboxInputType::URL) &&
[email protected]0a8718b12013-11-13 18:41:31825 (first_match != matches_.end()) &&
826 (first_match->relevance > CalculateRelevanceForVerbatim()) &&
[email protected]78981d8c2014-05-09 15:05:47827 (first_match->type != AutocompleteMatchType::NAVSUGGEST) &&
828 (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED);
[email protected]344946a12012-12-20 12:03:42829}
830
[email protected]257ab712009-04-14 17:16:24831void SearchProvider::AddNavigationResultsToMatches(
[email protected]d30268a2013-06-25 22:31:07832 const NavigationResults& navigation_results,
833 ACMatches* matches) {
[email protected]bc8bb0cd2013-06-24 21:50:23834 for (NavigationResults::const_iterator it = navigation_results.begin();
835 it != navigation_results.end(); ++it) {
[email protected]d30268a2013-06-25 22:31:07836 matches->push_back(NavigationToMatch(*it));
[email protected]bc8bb0cd2013-06-24 21:50:23837 // In the absence of suggested relevance scores, use only the single
838 // highest-scoring result. (The results are already sorted by relevance.)
[email protected]d30268a2013-06-25 22:31:07839 if (!it->relevance_from_server())
[email protected]bc8bb0cd2013-06-24 21:50:23840 return;
[email protected]257ab712009-04-14 17:16:24841 }
842}
843
844void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
845 bool is_keyword,
846 int did_not_accept_suggestion,
847 MatchMap* map) {
[email protected]51124552011-07-16 01:37:10848 if (results.empty())
849 return;
850
[email protected]31afdf72013-09-26 04:29:36851 base::TimeTicks start_time(base::TimeTicks::Now());
[email protected]d7ad4772012-06-01 03:12:54852 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
[email protected]3dc75b12014-06-08 00:02:22853 (input_.type() == metrics::OmniboxInputType::URL);
[email protected]0085863a2013-12-06 21:19:03854 const base::string16& input_text =
[email protected]14710852013-02-05 23:45:41855 is_keyword ? keyword_input_.text() : input_.text();
[email protected]51124552011-07-16 01:37:10856 bool input_multiple_words = HasMultipleWords(input_text);
857
[email protected]55ce8f12012-05-09 04:44:08858 SuggestResults scored_results;
859 if (!prevent_inline_autocomplete && input_multiple_words) {
860 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
861 // queries if the input also has multiple words. But if we were already
[email protected]f13a2132014-04-22 08:13:52862 // scoring a multi-word, multi-visit query aggressively, and the current
863 // input is still a prefix of it, then changing the suggestion suddenly
864 // feels wrong. To detect this case, first score as if only one word has
865 // been typed, then check if the best result came from aggressive search
866 // history scoring. If it did, then just keep that score set. This
867 // 1200 the lowest possible score in CalculateRelevanceForHistory()'s
868 // aggressive-scoring curve.
[email protected]55ce8f12012-05-09 04:44:08869 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
870 false, input_text, is_keyword);
[email protected]f13a2132014-04-22 08:13:52871 if ((scored_results.front().relevance() < 1200) ||
[email protected]9dfb4d362013-04-05 02:15:12872 !HasMultipleWords(scored_results.front().suggestion()))
[email protected]55ce8f12012-05-09 04:44:08873 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:10874 }
[email protected]55ce8f12012-05-09 04:44:08875 if (scored_results.empty())
876 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
877 input_multiple_words, input_text,
878 is_keyword);
879 for (SuggestResults::const_iterator i(scored_results.begin());
880 i != scored_results.end(); ++i) {
[email protected]57482a72014-03-14 22:27:37881 AddMatchToMap(*i, std::string(), did_not_accept_suggestion, true, map);
[email protected]51124552011-07-16 01:37:10882 }
[email protected]31afdf72013-09-26 04:29:36883 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime",
884 base::TimeTicks::Now() - start_time);
[email protected]51124552011-07-16 01:37:10885}
886
[email protected]55ce8f12012-05-09 04:44:08887SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:10888 const HistoryResults& results,
889 bool base_prevent_inline_autocomplete,
890 bool input_multiple_words,
[email protected]0085863a2013-12-06 21:19:03891 const base::string16& input_text,
[email protected]51124552011-07-16 01:37:10892 bool is_keyword) {
[email protected]810ffba2012-06-12 01:07:48893 AutocompleteClassifier* classifier =
894 AutocompleteClassifierFactory::GetForProfile(profile_);
[email protected]55ce8f12012-05-09 04:44:08895 SuggestResults scored_results;
[email protected]78e5e432013-08-03 02:10:10896 const bool prevent_search_history_inlining =
897 OmniboxFieldTrial::SearchHistoryPreventInlining(
898 input_.current_page_classification());
[email protected]c2ca3fd2014-03-22 03:07:44899 const base::string16& trimmed_input =
900 base::CollapseWhitespace(input_text, false);
[email protected]257ab712009-04-14 17:16:24901 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
902 ++i) {
[email protected]c2ca3fd2014-03-22 03:07:44903 const base::string16& trimmed_suggestion =
904 base::CollapseWhitespace(i->term, false);
905
[email protected]51124552011-07-16 01:37:10906 // Don't autocomplete multi-word queries that have only been seen once
907 // unless the user has typed more than one word.
908 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
[email protected]c2ca3fd2014-03-22 03:07:44909 (!input_multiple_words && (i->visits < 2) &&
910 HasMultipleWords(trimmed_suggestion));
[email protected]51124552011-07-16 01:37:10911
[email protected]ea3b9a502011-04-04 14:19:37912 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:10913 // when typed. For example, if the user searched for "google.com" and types
914 // "goog", don't autocomplete to the search term "google.com". Otherwise,
915 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:48916 // NOTE: We don't check this in the following cases:
917 // * When inline autocomplete is disabled, we won't be inline
918 // autocompleting this term, so we don't need to worry about confusion as
919 // much. This also prevents calling Classify() again from inside the
920 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:10921 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:48922 // * When the user has typed the whole term, the "what you typed" history
923 // match will outrank us for URL-like inputs anyway, so we need not do
924 // anything special.
[email protected]c2ca3fd2014-03-22 03:07:44925 if (!prevent_inline_autocomplete && classifier &&
926 (trimmed_suggestion != trimmed_input)) {
[email protected]ea3b9a502011-04-04 14:19:37927 AutocompleteMatch match;
[email protected]c2ca3fd2014-03-22 03:07:44928 classifier->Classify(trimmed_suggestion, false, false,
[email protected]51abb7b2014-02-09 23:00:08929 input_.current_page_classification(), &match, NULL);
[email protected]2905f742011-10-13 03:51:58930 prevent_inline_autocomplete =
[email protected]749e7ae02012-09-05 18:47:46931 !AutocompleteMatch::IsSearchType(match.type);
[email protected]ea3b9a502011-04-04 14:19:37932 }
[email protected]51124552011-07-16 01:37:10933
[email protected]78e5e432013-08-03 02:10:10934 int relevance = CalculateRelevanceForHistory(
935 i->time, is_keyword, !prevent_inline_autocomplete,
936 prevent_search_history_inlining);
[email protected]2c03c06b2013-12-11 20:45:02937 scored_results.push_back(SuggestResult(
[email protected]c2ca3fd2014-03-22 03:07:44938 trimmed_suggestion, AutocompleteMatchType::SEARCH_HISTORY,
[email protected]7ec287e2014-05-16 02:50:08939 trimmed_suggestion, base::string16(), base::string16(),
940 base::string16(), base::string16(), std::string(), std::string(),
941 is_keyword, relevance, false, false, trimmed_input));
[email protected]257ab712009-04-14 17:16:24942 }
[email protected]51124552011-07-16 01:37:10943
944 // History returns results sorted for us. However, we may have docked some
945 // results' scores, so things are no longer in order. Do a stable sort to get
946 // things back in order without otherwise disturbing results with equal
947 // scores, then force the scores to be unique, so that the order in which
948 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:08949 std::stable_sort(scored_results.begin(), scored_results.end(),
950 CompareScoredResults());
[email protected]51124552011-07-16 01:37:10951 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:08952 for (SuggestResults::iterator i(scored_results.begin());
953 i != scored_results.end(); ++i) {
954 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
955 i->set_relevance(last_relevance - 1);
956 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:10957 }
958
[email protected]55ce8f12012-05-09 04:44:08959 return scored_results;
[email protected]257ab712009-04-14 17:16:24960}
961
[email protected]55ce8f12012-05-09 04:44:08962void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
[email protected]987fad782013-08-28 06:23:18963 const std::string& metadata,
[email protected]55ce8f12012-05-09 04:44:08964 MatchMap* map) {
[email protected]9487b392014-02-14 02:48:18965 for (size_t i = 0; i < results.size(); ++i)
[email protected]57482a72014-03-14 22:27:37966 AddMatchToMap(results[i], metadata, i, false, map);
initial.commit09911bf2008-07-26 23:55:29967}
968
[email protected]d30268a2013-06-25 22:31:07969int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const {
[email protected]dc6943b2012-06-19 06:39:56970 // Use the suggested verbatim relevance score if it is non-negative (valid),
971 // if inline autocomplete isn't prevented (always show verbatim on backspace),
[email protected]1beee342012-06-19 22:22:28972 // and if it won't suppress verbatim, leaving no default provider matches.
973 // Otherwise, if the default provider returned no matches and was still able
[email protected]dc6943b2012-06-19 06:39:56974 // to suppress verbatim, the user would have no search/nav matches and may be
[email protected]1beee342012-06-19 22:22:28975 // left unable to search using their default provider from the omnibox.
[email protected]dc6943b2012-06-19 06:39:56976 // Check for results on each verbatim calculation, as results from older
977 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:23978 bool use_server_relevance =
979 (default_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:28980 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:23981 ((default_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:24982 !default_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:23983 !default_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:07984 if (relevance_from_server)
985 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:23986 return use_server_relevance ?
987 default_results_.verbatim_relevance : CalculateRelevanceForVerbatim();
[email protected]382a0642012-06-06 06:13:52988}
[email protected]d1f0a7f2012-06-05 10:26:42989
[email protected]382a0642012-06-06 06:13:52990int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:47991 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:36992 return 250;
[email protected]dab8d52d2013-03-05 07:35:28993 return CalculateRelevanceForVerbatimIgnoringKeywordModeState();
994}
[email protected]52d08b12009-10-19 18:42:36995
[email protected]dab8d52d2013-03-05 07:35:28996int SearchProvider::
997 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const {
initial.commit09911bf2008-07-26 23:55:29998 switch (input_.type()) {
[email protected]3dc75b12014-06-08 00:02:22999 case metrics::OmniboxInputType::UNKNOWN:
1000 case metrics::OmniboxInputType::QUERY:
1001 case metrics::OmniboxInputType::FORCED_QUERY:
[email protected]90fe2bb2013-01-15 03:42:131002 return kNonURLVerbatimRelevance;
initial.commit09911bf2008-07-26 23:55:291003
[email protected]3dc75b12014-06-08 00:02:221004 case metrics::OmniboxInputType::URL:
[email protected]52d08b12009-10-19 18:42:361005 return 850;
initial.commit09911bf2008-07-26 23:55:291006
1007 default:
1008 NOTREACHED();
1009 return 0;
1010 }
1011}
1012
[email protected]d30268a2013-06-25 22:31:071013int SearchProvider::GetKeywordVerbatimRelevance(
1014 bool* relevance_from_server) const {
[email protected]dab8d52d2013-03-05 07:35:281015 // Use the suggested verbatim relevance score if it is non-negative (valid),
1016 // if inline autocomplete isn't prevented (always show verbatim on backspace),
1017 // and if it won't suppress verbatim, leaving no keyword provider matches.
1018 // Otherwise, if the keyword provider returned no matches and was still able
1019 // to suppress verbatim, the user would have no search/nav matches and may be
1020 // left unable to search using their keyword provider from the omnibox.
1021 // Check for results on each verbatim calculation, as results from older
1022 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:231023 bool use_server_relevance =
1024 (keyword_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:281025 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:231026 ((keyword_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:241027 !keyword_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:231028 !keyword_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:071029 if (relevance_from_server)
1030 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:231031 return use_server_relevance ?
1032 keyword_results_.verbatim_relevance :
1033 CalculateRelevanceForKeywordVerbatim(keyword_input_.type(),
1034 keyword_input_.prefer_keyword());
[email protected]5423e562013-02-07 03:58:451035}
1036
[email protected]51124552011-07-16 01:37:101037int SearchProvider::CalculateRelevanceForHistory(
[email protected]bc8bb0cd2013-06-24 21:50:231038 const base::Time& time,
[email protected]51124552011-07-16 01:37:101039 bool is_keyword,
[email protected]78e5e432013-08-03 02:10:101040 bool use_aggressive_method,
1041 bool prevent_search_history_inlining) const {
[email protected]aa613d62010-11-09 20:40:181042 // The relevance of past searches falls off over time. There are two distinct
1043 // equations used. If the first equation is used (searches to the primary
[email protected]78e5e432013-08-03 02:10:101044 // provider that we want to score aggressively), the score is in the range
1045 // 1300-1599 (unless |prevent_search_history_inlining|, in which case
[email protected]d8cd76b2013-07-10 09:46:161046 // it's in the range 1200-1299). If the second equation is used the
1047 // relevance of a search 15 minutes ago is discounted 50 points, while the
1048 // relevance of a search two weeks ago is discounted 450 points.
[email protected]bc8bb0cd2013-06-24 21:50:231049 double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0);
[email protected]188b50c2013-03-28 07:19:421050 bool is_primary_provider = is_keyword || !providers_.has_keyword_provider();
[email protected]78e5e432013-08-03 02:10:101051 if (is_primary_provider && use_aggressive_method) {
[email protected]aa613d62010-11-09 20:40:181052 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:101053 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:181054 if (elapsed_time < autocomplete_time) {
[email protected]d8cd76b2013-07-10 09:46:161055 int max_score = is_keyword ? 1599 : 1399;
[email protected]78e5e432013-08-03 02:10:101056 if (prevent_search_history_inlining)
[email protected]d8cd76b2013-07-10 09:46:161057 max_score = 1299;
1058 return max_score - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:181059 std::pow(elapsed_time / autocomplete_time, 2.5));
1060 }
1061 elapsed_time -= autocomplete_time;
1062 }
1063
[email protected]c3a4bd992010-08-18 20:25:011064 const int score_discount =
1065 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:291066
[email protected]6c85aa02009-02-27 12:08:091067 // Don't let scores go below 0. Negative relevance scores are meaningful in
1068 // a different way.
initial.commit09911bf2008-07-26 23:55:291069 int base_score;
[email protected]51124552011-07-16 01:37:101070 if (is_primary_provider)
[email protected]3dc75b12014-06-08 00:02:221071 base_score = (input_.type() == metrics::OmniboxInputType::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:101072 else
1073 base_score = 200;
initial.commit09911bf2008-07-26 23:55:291074 return std::max(0, base_score - score_discount);
1075}
1076
initial.commit09911bf2008-07-26 23:55:291077AutocompleteMatch SearchProvider::NavigationToMatch(
[email protected]188b50c2013-03-28 07:19:421078 const NavigationResult& navigation) {
[email protected]5889bfb2014-03-19 00:26:481079 base::string16 input;
1080 const bool trimmed_whitespace = base::TrimWhitespace(
1081 navigation.from_keyword_provider() ?
1082 keyword_input_.text() : input_.text(),
1083 base::TRIM_TRAILING, &input) != base::TRIM_NONE;
[email protected]55ce8f12012-05-09 04:44:081084 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:471085 navigation.type());
[email protected]55ce8f12012-05-09 04:44:081086 match.destination_url = navigation.url();
[email protected]78981d8c2014-05-09 15:05:471087 BaseSearchProvider::SetDeletionURL(navigation.deletion_url(), &match);
[email protected]23db6492014-01-16 02:35:301088 // First look for the user's input inside the formatted url as it would be
[email protected]371dab12012-06-01 03:23:551089 // without trimming the scheme, so we can find matches at the beginning of the
1090 // scheme.
[email protected]371dab12012-06-01 03:23:551091 const URLPrefix* prefix =
[email protected]23db6492014-01-16 02:35:301092 URLPrefix::BestURLPrefix(navigation.formatted_url(), input);
[email protected]371dab12012-06-01 03:23:551093 size_t match_start = (prefix == NULL) ?
[email protected]23db6492014-01-16 02:35:301094 navigation.formatted_url().find(input) : prefix->prefix.length();
[email protected]d2445c82013-11-04 22:28:351095 bool trim_http = !AutocompleteInput::HasHTTPScheme(input) &&
1096 (!prefix || (match_start != 0));
[email protected]23db6492014-01-16 02:35:301097 const net::FormatUrlTypes format_types =
1098 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
[email protected]371dab12012-06-01 03:23:551099
[email protected]371dab12012-06-01 03:23:551100 const std::string languages(
1101 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]23db6492014-01-16 02:35:301102 size_t inline_autocomplete_offset = (prefix == NULL) ?
1103 base::string16::npos : (match_start + input.length());
[email protected]371dab12012-06-01 03:23:551104 match.fill_into_edit +=
[email protected]5655ea32014-06-21 05:28:081105 AutocompleteInput::FormattedStringWithEquivalentMeaning(
1106 navigation.url(),
[email protected]371dab12012-06-01 03:23:551107 net::FormatUrl(navigation.url(), languages, format_types,
1108 net::UnescapeRule::SPACES, NULL, NULL,
[email protected]5655ea32014-06-21 05:28:081109 &inline_autocomplete_offset),
[email protected]a817ed392014-06-27 05:03:001110 ChromeAutocompleteSchemeClassifier(profile_));
[email protected]14119032013-11-07 08:14:261111 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1112 // Otherwise, user edits to a suggestion would show non-Search results.
[email protected]3dc75b12014-06-08 00:02:221113 if (input_.type() == metrics::OmniboxInputType::FORCED_QUERY) {
[email protected]670d3232013-12-24 17:58:581114 match.fill_into_edit.insert(0, base::ASCIIToUTF16("?"));
[email protected]0085863a2013-12-06 21:19:031115 if (inline_autocomplete_offset != base::string16::npos)
[email protected]14119032013-11-07 08:14:261116 ++inline_autocomplete_offset;
1117 }
[email protected]6c94a1022014-02-21 03:48:041118 if (inline_autocomplete_offset != base::string16::npos) {
[email protected]518024c2013-07-19 23:40:251119 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
1120 match.inline_autocompletion =
1121 match.fill_into_edit.substr(inline_autocomplete_offset);
1122 }
[email protected]6c94a1022014-02-21 03:48:041123 // An inlineable navsuggestion can only be the default match when there
1124 // is no keyword provider active, lest it appear first and break the user
[email protected]5889bfb2014-03-19 00:26:481125 // out of keyword mode. It can also only be default if either the inline
1126 // autocompletion is empty or we're not preventing inline autocompletion.
1127 // Finally, if we have an inlineable navsuggestion with an inline completion
1128 // that we're not preventing, make sure we didn't trim any whitespace.
1129 // We don't want to claim http://foo.com/bar is inlineable against the
1130 // input "foo.com/b ".
[email protected]6c94a1022014-02-21 03:48:041131 match.allowed_to_be_default_match = navigation.IsInlineable(input) &&
1132 (providers_.GetKeywordProviderURL() == NULL) &&
[email protected]5889bfb2014-03-19 00:26:481133 (match.inline_autocompletion.empty() ||
[email protected]78981d8c2014-05-09 15:05:471134 (!input_.prevent_inline_autocomplete() && !trimmed_whitespace));
[email protected]371dab12012-06-01 03:23:551135
[email protected]23db6492014-01-16 02:35:301136 match.contents = navigation.match_contents();
1137 match.contents_class = navigation.match_contents_class();
[email protected]55ce8f12012-05-09 04:44:081138 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551139 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1140 ACMatchClassification::NONE, &match.description_class);
[email protected]d30268a2013-06-25 22:31:071141
1142 match.RecordAdditionalInfo(
1143 kRelevanceFromServerKey,
1144 navigation.relevance_from_server() ? kTrue : kFalse);
[email protected]987fad782013-08-28 06:23:181145 match.RecordAdditionalInfo(kShouldPrefetchKey, kFalse);
[email protected]d30268a2013-06-25 22:31:071146
initial.commit09911bf2008-07-26 23:55:291147 return match;
1148}
[email protected]4ab4c7c2010-11-24 04:49:341149
[email protected]4ab4c7c2010-11-24 04:49:341150void SearchProvider::UpdateDone() {
[email protected]2cdf1172012-08-26 12:21:331151 // We're done when the timer isn't running, there are no suggest queries
[email protected]5fac3782013-03-06 09:32:311152 // pending, and we're not waiting on Instant.
[email protected]e1290ee62013-06-26 18:31:151153 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0);
[email protected]4ab4c7c2010-11-24 04:49:341154}
[email protected]20184242014-05-14 02:57:421155
1156std::string SearchProvider::GetSessionToken() {
1157 base::TimeTicks current_time(base::TimeTicks::Now());
1158 // Renew token if it expired.
1159 if (current_time > token_expiration_time_) {
1160 const size_t kTokenBytes = 12;
1161 std::string raw_data;
1162 base::RandBytes(WriteInto(&raw_data, kTokenBytes + 1), kTokenBytes);
1163 base::Base64Encode(raw_data, &current_token_);
[email protected]ab2c31f72014-05-17 17:03:531164
1165 // Make the base64 encoded value URL and filename safe(see RFC 3548).
1166 std::replace(current_token_.begin(), current_token_.end(), '+', '-');
1167 std::replace(current_token_.begin(), current_token_.end(), '/', '_');
[email protected]20184242014-05-14 02:57:421168 }
1169
1170 // Extend expiration time another 60 seconds.
1171 token_expiration_time_ = current_time + base::TimeDelta::FromSeconds(60);
1172
1173 return current_token_;
1174}