blob: 75b4b0ab0ae63a8e059b81663354a1ef4e98d0a4 [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]3dc75b12014-06-08 00:02:2242#include "components/metrics/proto/omnibox_input_type.pb.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]cca6f392014-05-28 21:32:2652#include "url/url_constants.h"
[email protected]761fa4702013-07-02 15:25:1553#include "url/url_util.h"
initial.commit09911bf2008-07-26 23:55:2954
[email protected]bc8bb0cd2013-06-24 21:50:2355// Helpers --------------------------------------------------------------------
[email protected]e1acf6f2008-10-27 20:43:3356
[email protected]51124552011-07-16 01:37:1057namespace {
58
[email protected]7706a522012-08-16 17:42:2559// We keep track in a histogram how many suggest requests we send, how
60// many suggest requests we invalidate (e.g., due to a user typing
61// another character), and how many replies we receive.
62// *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
63// (excluding the end-of-list enum value)
64// We do not want values of existing enums to change or else it screws
65// up the statistics.
66enum SuggestRequestsHistogramValue {
67 REQUEST_SENT = 1,
68 REQUEST_INVALIDATED,
69 REPLY_RECEIVED,
70 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE
71};
72
[email protected]90fe2bb2013-01-15 03:42:1373// The verbatim score for an input which is not an URL.
74const int kNonURLVerbatimRelevance = 1300;
75
[email protected]7706a522012-08-16 17:42:2576// Increments the appropriate value in the histogram by one.
77void LogOmniboxSuggestRequest(
78 SuggestRequestsHistogramValue request_value) {
79 UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value,
80 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE);
81}
82
[email protected]0085863a2013-12-06 21:19:0383bool HasMultipleWords(const base::string16& text) {
[email protected]51124552011-07-16 01:37:1084 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
85 bool found_word = false;
86 if (i.Init()) {
87 while (i.Advance()) {
88 if (i.IsWord()) {
89 if (found_word)
90 return true;
91 found_word = true;
92 }
93 }
94 }
95 return false;
96}
97
[email protected]d1f0a7f2012-06-05 10:26:4298} // namespace
[email protected]51124552011-07-16 01:37:1099
[email protected]3954c3a2012-04-10 20:17:55100// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:58101
[email protected]85b8d6f2012-05-08 20:53:47102SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
[email protected]02346202014-02-05 05:18:30103 : template_url_service_(template_url_service) {}
[email protected]85b8d6f2012-05-08 20:53:47104
105const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
106 return default_provider_.empty() ? NULL :
107 template_url_service_->GetTemplateURLForKeyword(default_provider_);
108}
109
110const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
111 return keyword_provider_.empty() ? NULL :
112 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:24113}
114
[email protected]3954c3a2012-04-10 20:17:55115
[email protected]bc8bb0cd2013-06-24 21:50:23116// SearchProvider::CompareScoredResults ---------------------------------------
117
118class SearchProvider::CompareScoredResults {
119 public:
120 bool operator()(const Result& a, const Result& b) {
121 // Sort in descending relevance order.
122 return a.relevance() > b.relevance();
123 }
124};
125
126
[email protected]3954c3a2012-04-10 20:17:55127// SearchProvider -------------------------------------------------------------
128
129// static
[email protected]24dbf2302013-04-05 16:59:25130int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100;
[email protected]3954c3a2012-04-10 20:17:55131
[email protected]bc8bb0cd2013-06-24 21:50:23132SearchProvider::SearchProvider(AutocompleteProviderListener* listener,
133 Profile* profile)
[email protected]02346202014-02-05 05:18:30134 : BaseSearchProvider(listener, profile, AutocompleteProvider::TYPE_SEARCH),
[email protected]cfa164bf2014-03-19 11:51:15135 providers_(TemplateURLServiceFactory::GetForProfile(profile)) {
[email protected]bc8bb0cd2013-06-24 21:50:23136}
137
[email protected]cb86ee6f2013-04-28 16:58:15138// static
[email protected]987fad782013-08-28 06:23:18139std::string SearchProvider::GetSuggestMetadata(const AutocompleteMatch& match) {
140 return match.GetAdditionalInfo(kSuggestMetadataKey);
141}
142
[email protected]bc8bb0cd2013-06-24 21:50:23143void SearchProvider::ResetSession() {
144 field_trial_triggered_in_session_ = false;
[email protected]4ab4c7c2010-11-24 04:49:34145}
146
[email protected]bc8bb0cd2013-06-24 21:50:23147SearchProvider::~SearchProvider() {
148}
149
[email protected]ee6110b2014-01-09 22:26:31150void SearchProvider::UpdateMatchContentsClass(const base::string16& input_text,
[email protected]23db6492014-01-16 02:35:30151 Results* results) {
152 for (SuggestResults::iterator sug_it = results->suggest_results.begin();
153 sug_it != results->suggest_results.end(); ++sug_it) {
[email protected]ee6110b2014-01-09 22:26:31154 sug_it->ClassifyMatchContents(false, input_text);
155 }
[email protected]23db6492014-01-16 02:35:30156 const std::string languages(
157 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
158 for (NavigationResults::iterator nav_it = results->navigation_results.begin();
159 nav_it != results->navigation_results.end(); ++nav_it) {
160 nav_it->CalculateAndClassifyMatchContents(false, input_text, languages);
161 }
[email protected]ee6110b2014-01-09 22:26:31162}
163
164// static
[email protected]bc8bb0cd2013-06-24 21:50:23165int SearchProvider::CalculateRelevanceForKeywordVerbatim(
166 AutocompleteInput::Type type,
167 bool prefer_keyword) {
168 // This function is responsible for scoring verbatim query matches
169 // for non-extension keywords. KeywordProvider::CalculateRelevance()
170 // scores verbatim query matches for extension keywords, as well as
171 // for keyword matches (i.e., suggestions of a keyword itself, not a
172 // suggestion of a query on a keyword search engine). These two
173 // functions are currently in sync, but there's no reason we
174 // couldn't decide in the future to score verbatim matches
175 // differently for extension and non-extension keywords. If you
176 // make such a change, however, you should update this comment to
177 // describe it, so it's clear why the functions diverge.
178 if (prefer_keyword)
179 return 1500;
[email protected]3dc75b12014-06-08 00:02:22180 return (type == metrics::OmniboxInputType::QUERY) ? 1450 : 1100;
[email protected]bc8bb0cd2013-06-24 21:50:23181}
182
initial.commit09911bf2008-07-26 23:55:29183void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27184 bool minimal_changes) {
[email protected]04504c242013-01-22 21:08:55185 // Do our best to load the model as early as possible. This will reduce
186 // odds of having the model not ready when really needed (a non-empty input).
187 TemplateURLService* model = providers_.template_url_service();
188 DCHECK(model);
189 model->Load();
190
initial.commit09911bf2008-07-26 23:55:29191 matches_.clear();
[email protected]618d6e62012-12-16 05:55:57192 field_trial_triggered_ = false;
initial.commit09911bf2008-07-26 23:55:29193
[email protected]6c85aa02009-02-27 12:08:09194 // Can't return search/suggest results for bogus input or without a profile.
[email protected]3dc75b12014-06-08 00:02:22195 if (!profile_ || (input.type() == metrics::OmniboxInputType::INVALID)) {
[email protected]9950e5d52014-02-25 23:34:19196 Stop(true);
initial.commit09911bf2008-07-26 23:55:29197 return;
198 }
199
[email protected]14710852013-02-05 23:45:41200 keyword_input_ = input;
[email protected]257ab712009-04-14 17:16:24201 const TemplateURL* keyword_provider =
[email protected]14710852013-02-05 23:45:41202 KeywordProvider::GetSubstitutingTemplateURLForInput(model,
203 &keyword_input_);
204 if (keyword_provider == NULL)
205 keyword_input_.Clear();
206 else if (keyword_input_.text().empty())
[email protected]257ab712009-04-14 17:16:24207 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24208
[email protected]85b8d6f2012-05-08 20:53:47209 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]ce7ee5f2014-06-16 23:41:19210 if (default_provider &&
211 !default_provider->SupportsReplacement(model->search_terms_data()))
[email protected]257ab712009-04-14 17:16:24212 default_provider = NULL;
213
214 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18215 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24216
217 if (!default_provider && !keyword_provider) {
218 // No valid providers.
[email protected]9950e5d52014-02-25 23:34:19219 Stop(true);
initial.commit09911bf2008-07-26 23:55:29220 return;
221 }
222
223 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24224 // or the providers, abort the query.
[email protected]0085863a2013-12-06 21:19:03225 base::string16 default_provider_keyword(default_provider ?
226 default_provider->keyword() : base::string16());
227 base::string16 keyword_provider_keyword(keyword_provider ?
228 keyword_provider->keyword() : base::string16());
[email protected]9e789742011-01-10 23:27:32229 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47230 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]bb900e02013-03-14 14:15:29231 // Cancel any in-flight suggest requests.
[email protected]e1290ee62013-06-26 18:31:15232 if (!done_)
[email protected]e29249dc52012-07-19 17:33:50233 Stop(false);
[email protected]257ab712009-04-14 17:16:24234 }
initial.commit09911bf2008-07-26 23:55:29235
[email protected]85b8d6f2012-05-08 20:53:47236 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29237
238 if (input.text().empty()) {
239 // User typed "?" alone. Give them a placeholder result indicating what
240 // this syntax does.
[email protected]257ab712009-04-14 17:16:24241 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00242 AutocompleteMatch match;
243 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36244 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24245 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35246 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47247 match.keyword = providers_.default_provider();
[email protected]45f89a92013-08-12 13:41:36248 match.allowed_to_be_default_match = true;
[email protected]257ab712009-04-14 17:16:24249 matches_.push_back(match);
250 }
[email protected]9950e5d52014-02-25 23:34:19251 Stop(true);
initial.commit09911bf2008-07-26 23:55:29252 return;
253 }
254
255 input_ = input;
256
[email protected]e1290ee62013-06-26 18:31:15257 DoHistoryQuery(minimal_changes);
258 StartOrStopSuggestQuery(minimal_changes);
[email protected]344946a12012-12-20 12:03:42259 UpdateMatches();
initial.commit09911bf2008-07-26 23:55:29260}
261
[email protected]d4a94b92014-03-04 01:35:22262void SearchProvider::SortResults(bool is_keyword,
263 const base::ListValue* relevances,
264 Results* results) {
265 // Ignore suggested scores for non-keyword matches in keyword mode; if the
266 // server is allowed to score these, it could interfere with the user's
267 // ability to get good keyword results.
268 const bool abandon_suggested_scores =
269 !is_keyword && !providers_.keyword_provider().empty();
270 // Apply calculated relevance scores to suggestions if a valid list was
271 // not provided or we're abandoning suggested scores entirely.
272 if ((relevances == NULL) || abandon_suggested_scores) {
273 ApplyCalculatedSuggestRelevance(&results->suggest_results);
274 ApplyCalculatedNavigationRelevance(&results->navigation_results);
275 // If abandoning scores entirely, also abandon the verbatim score.
276 if (abandon_suggested_scores)
277 results->verbatim_relevance = -1;
278 }
279
280 // Keep the result lists sorted.
281 const CompareScoredResults comparator = CompareScoredResults();
282 std::stable_sort(results->suggest_results.begin(),
283 results->suggest_results.end(),
284 comparator);
285 std::stable_sort(results->navigation_results.begin(),
286 results->navigation_results.end(),
287 comparator);
288}
289
[email protected]cfa164bf2014-03-19 11:51:15290const TemplateURL* SearchProvider::GetTemplateURL(bool is_keyword) const {
291 return is_keyword ? providers_.GetKeywordProviderURL()
292 : providers_.GetDefaultProviderURL();
[email protected]9487b392014-02-14 02:48:18293}
294
[email protected]d4a94b92014-03-04 01:35:22295const AutocompleteInput SearchProvider::GetInput(bool is_keyword) const {
296 return is_keyword ? keyword_input_ : input_;
[email protected]9487b392014-02-14 02:48:18297}
298
[email protected]cfa164bf2014-03-19 11:51:15299BaseSearchProvider::Results* SearchProvider::GetResultsToFill(bool is_keyword) {
300 return is_keyword ? &keyword_results_ : &default_results_;
301}
302
[email protected]9487b392014-02-14 02:48:18303bool SearchProvider::ShouldAppendExtraParams(
304 const SuggestResult& result) const {
305 return !result.from_keyword_provider() ||
306 providers_.default_provider().empty();
307}
308
[email protected]ef6866f2014-02-18 08:26:34309void SearchProvider::StopSuggest() {
310 // Increment the appropriate field in the histogram by the number of
311 // pending requests that were invalidated.
312 for (int i = 0; i < suggest_results_pending_; ++i)
313 LogOmniboxSuggestRequest(REQUEST_INVALIDATED);
314 suggest_results_pending_ = 0;
315 timer_.Stop();
316 // Stop any in-progress URL fetches.
317 keyword_fetcher_.reset();
318 default_fetcher_.reset();
319}
320
321void SearchProvider::ClearAllResults() {
322 keyword_results_.Clear();
323 default_results_.Clear();
324}
325
[email protected]d4a94b92014-03-04 01:35:22326int SearchProvider::GetDefaultResultRelevance() const {
327 return -1;
328}
329
[email protected]7267ff82013-12-03 17:20:30330void SearchProvider::RecordDeletionResult(bool success) {
331 if (success) {
332 content::RecordAction(
[email protected]e6e30ac2014-01-13 21:24:39333 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Success"));
[email protected]7267ff82013-12-03 17:20:30334 } else {
335 content::RecordAction(
[email protected]e6e30ac2014-01-13 21:24:39336 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure"));
[email protected]7267ff82013-12-03 17:20:30337 }
338}
339
[email protected]cfa164bf2014-03-19 11:51:15340void SearchProvider::LogFetchComplete(bool success, bool is_keyword) {
341 LogOmniboxSuggestRequest(REPLY_RECEIVED);
342 // Record response time for suggest requests sent to Google. We care
343 // only about the common case: the Google default provider used in
344 // non-keyword mode.
345 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
346 if (!is_keyword && default_url &&
[email protected]ce7ee5f2014-06-16 23:41:19347 (TemplateURLPrepopulateData::GetEngineType(
348 *default_url,
349 providers_.template_url_service()->search_terms_data()) ==
[email protected]cfa164bf2014-03-19 11:51:15350 SEARCH_ENGINE_GOOGLE)) {
351 const base::TimeDelta elapsed_time =
352 base::TimeTicks::Now() - time_suggest_request_sent_;
353 if (success) {
354 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
355 elapsed_time);
356 } else {
357 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
358 elapsed_time);
359 }
360 }
361}
362
363bool SearchProvider::IsKeywordFetcher(const net::URLFetcher* fetcher) const {
364 return fetcher == keyword_fetcher_.get();
365}
366
367void SearchProvider::UpdateMatches() {
[email protected]cfa164bf2014-03-19 11:51:15368 ConvertResultsToAutocompleteMatches();
369
370 // Check constraints that may be violated by suggested relevances.
371 if (!matches_.empty() &&
372 (default_results_.HasServerProvidedScores() ||
373 keyword_results_.HasServerProvidedScores())) {
374 // These blocks attempt to repair undesirable behavior by suggested
375 // relevances with minimal impact, preserving other suggested relevances.
376
[email protected]cfa164bf2014-03-19 11:51:15377 if (!HasKeywordDefaultMatchInKeywordMode()) {
378 // In keyword mode, disregard the keyword verbatim suggested relevance
379 // if necessary so there at least one keyword match that's allowed to
380 // be the default match.
381 keyword_results_.verbatim_relevance = -1;
382 ConvertResultsToAutocompleteMatches();
383 }
[email protected]89bd27d12014-04-12 17:36:23384 if (IsTopMatchSearchWithURLInput()) {
[email protected]cfa164bf2014-03-19 11:51:15385 // Disregard the suggested search and verbatim relevances if the input
386 // type is URL and the top match is a highly-ranked search suggestion.
387 // For example, prevent a search for "foo.com" from outranking another
388 // provider's navigation for "foo.com" or "foo.com/url_from_history".
389 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
390 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
391 default_results_.verbatim_relevance = -1;
392 keyword_results_.verbatim_relevance = -1;
393 ConvertResultsToAutocompleteMatches();
394 }
[email protected]89bd27d12014-04-12 17:36:23395 if (FindTopMatch() == matches_.end()) {
396 // Guarantee that SearchProvider returns a legal default match. (The
397 // omnibox always needs at least one legal default match, and it relies
398 // on SearchProvider to always return one.)
[email protected]cfa164bf2014-03-19 11:51:15399 ApplyCalculatedRelevance();
400 ConvertResultsToAutocompleteMatches();
401 }
[email protected]cfa164bf2014-03-19 11:51:15402 DCHECK(HasKeywordDefaultMatchInKeywordMode());
[email protected]89bd27d12014-04-12 17:36:23403 DCHECK(!IsTopMatchSearchWithURLInput());
404 DCHECK(FindTopMatch() != matches_.end());
[email protected]cfa164bf2014-03-19 11:51:15405 }
406 UMA_HISTOGRAM_CUSTOM_COUNTS(
407 "Omnibox.SearchProviderMatches", matches_.size(), 1, 6, 7);
408
409 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
410 if ((keyword_url != NULL) && HasKeywordDefaultMatchInKeywordMode()) {
411 // If there is a keyword match that is allowed to be the default match,
412 // then prohibit default provider matches from being the default match lest
413 // such matches cause the user to break out of keyword mode.
414 for (ACMatches::iterator it = matches_.begin(); it != matches_.end();
415 ++it) {
416 if (it->keyword != keyword_url->keyword())
417 it->allowed_to_be_default_match = false;
418 }
419 }
420
421 base::TimeTicks update_starred_start_time(base::TimeTicks::Now());
422 UpdateStarredStateOfMatches();
423 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateStarredTime",
424 base::TimeTicks::Now() - update_starred_start_time);
425 UpdateDone();
[email protected]cfa164bf2014-03-19 11:51:15426}
427
[email protected]bc8bb0cd2013-06-24 21:50:23428void SearchProvider::Run() {
429 // Start a new request with the current input.
430 suggest_results_pending_ = 0;
431 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]abe441e2013-05-06 12:35:05432
[email protected]bc8bb0cd2013-06-24 21:50:23433 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
434 providers_.GetDefaultProviderURL(), input_));
435 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
436 providers_.GetKeywordProviderURL(), keyword_input_));
437
438 // Both the above can fail if the providers have been modified or deleted
439 // since the query began.
440 if (suggest_results_pending_ == 0) {
441 UpdateDone();
442 // We only need to update the listener if we're actually done.
443 if (done_)
444 listener_->OnProviderUpdate(false);
445 }
[email protected]601858c02010-09-01 17:08:20446}
447
[email protected]8d457132010-11-04 18:13:40448void SearchProvider::DoHistoryQuery(bool minimal_changes) {
449 // The history query results are synchronous, so if minimal_changes is true,
450 // we still have the last results and don't need to do anything.
451 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29452 return;
453
[email protected]8d457132010-11-04 18:13:40454 keyword_history_results_.clear();
455 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29456
[email protected]78e5e432013-08-03 02:10:10457 if (OmniboxFieldTrial::SearchHistoryDisable(
458 input_.current_page_classification()))
[email protected]d8cd76b2013-07-10 09:46:16459 return;
460
[email protected]8d457132010-11-04 18:13:40461 HistoryService* const history_service =
[email protected]9d2db762012-06-19 00:01:10462 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
[email protected]8d457132010-11-04 18:13:40463 history::URLDatabase* url_db = history_service ?
464 history_service->InMemoryDatabase() : NULL;
[email protected]8d457132010-11-04 18:13:40465 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29466 return;
467
[email protected]51124552011-07-16 01:37:10468 // Request history for both the keyword and default provider. We grab many
469 // more matches than we'll ultimately clamp to so that if there are several
470 // recent multi-word matches who scores are lowered (see
471 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
472 // matches. Note that this doesn't fix the problem entirely, but merely
473 // limits it to cases with a very large number of such multi-word matches; for
474 // now, this seems OK compared with the complexity of a real fix, which would
475 // require multiple searches and tracking of "single- vs. multi-word" in the
476 // database.
477 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47478 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
479 if (default_url) {
[email protected]b4bec972014-04-05 18:07:15480 const base::TimeTicks start_time = base::TimeTicks::Now();
[email protected]85b8d6f2012-05-08 20:53:47481 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
482 num_matches, &default_history_results_);
[email protected]31afdf72013-09-26 04:29:36483 UMA_HISTOGRAM_TIMES(
484 "Omnibox.SearchProvider.GetMostRecentKeywordTermsDefaultProviderTime",
485 base::TimeTicks::Now() - start_time);
[email protected]257ab712009-04-14 17:16:24486 }
[email protected]85b8d6f2012-05-08 20:53:47487 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
488 if (keyword_url) {
489 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]14710852013-02-05 23:45:41490 keyword_input_.text(), num_matches, &keyword_history_results_);
[email protected]3954c3a2012-04-10 20:17:55491 }
initial.commit09911bf2008-07-26 23:55:29492}
493
[email protected]6dc950f2012-07-16 19:49:08494void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]83c726482008-09-10 06:36:34495 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29496 StopSuggest();
[email protected]71b46152013-05-03 16:39:20497 ClearAllResults();
initial.commit09911bf2008-07-26 23:55:29498 return;
499 }
500
501 // For the minimal_changes case, if we finished the previous query and still
502 // have its results, or are allowed to keep running it, just do that, rather
503 // than starting a new query.
504 if (minimal_changes &&
[email protected]cc1526e2013-05-17 04:04:24505 (!default_results_.suggest_results.empty() ||
506 !default_results_.navigation_results.empty() ||
507 !keyword_results_.suggest_results.empty() ||
508 !keyword_results_.navigation_results.empty() ||
[email protected]a2770a7d2014-04-22 19:33:35509 (!done_ && input_.want_asynchronous_matches())))
initial.commit09911bf2008-07-26 23:55:29510 return;
511
512 // We can't keep running any previous query, so halt it.
513 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42514
515 // Remove existing results that cannot inline autocomplete the new input.
[email protected]71b46152013-05-03 16:39:20516 RemoveAllStaleResults();
initial.commit09911bf2008-07-26 23:55:29517
[email protected]ee6110b2014-01-09 22:26:31518 // Update the content classifications of remaining results so they look good
519 // against the current input.
[email protected]23db6492014-01-16 02:35:30520 UpdateMatchContentsClass(input_.text(), &default_results_);
521 if (!keyword_input_.text().empty())
522 UpdateMatchContentsClass(keyword_input_.text(), &keyword_results_);
[email protected]ee6110b2014-01-09 22:26:31523
initial.commit09911bf2008-07-26 23:55:29524 // We can't start a new query if we're only allowed synchronous results.
[email protected]a2770a7d2014-04-22 19:33:35525 if (!input_.want_asynchronous_matches())
initial.commit09911bf2008-07-26 23:55:29526 return;
527
[email protected]24dbf2302013-04-05 16:59:25528 // To avoid flooding the suggest server, don't send a query until at
529 // least 100 ms since the last query.
[email protected]515ffa942012-11-27 20:18:24530 base::TimeTicks next_suggest_time(time_suggest_request_sent_ +
[email protected]bc8bb0cd2013-06-24 21:50:23531 base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs));
[email protected]515ffa942012-11-27 20:18:24532 base::TimeTicks now(base::TimeTicks::Now());
533 if (now >= next_suggest_time) {
534 Run();
535 return;
536 }
537 timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29538}
539
[email protected]83c726482008-09-10 06:36:34540bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55541 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
542 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47543 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
544 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34545 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47546 ((!default_url || default_url->suggestions_url().empty()) &&
547 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34548 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
549 return false;
550
[email protected]cac59d32010-08-09 23:23:14551 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34552 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34553
[email protected]cac59d32010-08-09 23:23:14554 // FORCED_QUERY means the user is explicitly asking us to search for this, so
555 // we assume it isn't a URL and/or there isn't private data.
[email protected]3dc75b12014-06-08 00:02:22556 if (input_.type() == metrics::OmniboxInputType::FORCED_QUERY)
[email protected]cac59d32010-08-09 23:23:14557 return true;
[email protected]83c726482008-09-10 06:36:34558
[email protected]f608ea102013-03-18 15:08:09559 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't
560 // http/https/ftp, we shouldn't send it. Sending things like file: and data:
561 // is both a waste of time and a disclosure of potentially private, local
562 // data. Other "schemes" may actually be usernames, and we don't want to send
563 // passwords. If the scheme is OK, we still need to check other cases below.
564 // If this is QUERY, then the presence of these schemes means the user
565 // explicitly typed one, and thus this is probably a URL that's being entered
566 // and happens to currently be invalid -- in which case we again want to run
567 // our checks below. Other QUERY cases are less likely to be URLs and thus we
568 // assume we're OK.
[email protected]e8ca69c2014-05-07 15:31:19569 if (!LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) &&
570 !LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
[email protected]cca6f392014-05-28 21:32:26571 !LowerCaseEqualsASCII(input_.scheme(), url::kFtpScheme))
[email protected]3dc75b12014-06-08 00:02:22572 return (input_.type() == metrics::OmniboxInputType::QUERY);
[email protected]cac59d32010-08-09 23:23:14573
574 // Don't send URLs with usernames, queries or refs. Some of these are
575 // private, and the Suggest server is unlikely to have any useful results
576 // for any of them. Also don't send URLs with ports, as we may initially
577 // think that a username + password is a host + port (and we don't want to
578 // send usernames/passwords), and even if the port really is a port, the
579 // server is once again unlikely to have and useful results.
[email protected]825e16f2013-09-30 23:52:58580 // Note that we only block based on refs if the input is URL-typed, as search
581 // queries can legitimately have #s in them which the URL parser
582 // overaggressively categorizes as a url with a ref.
[email protected]b45334502014-04-30 19:44:05583 const url::Parsed& parts = input_.parts();
[email protected]cac59d32010-08-09 23:23:14584 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
[email protected]825e16f2013-09-30 23:52:58585 parts.query.is_nonempty() ||
[email protected]3dc75b12014-06-08 00:02:22586 (parts.ref.is_nonempty() &&
587 (input_.type() == metrics::OmniboxInputType::URL)))
[email protected]cac59d32010-08-09 23:23:14588 return false;
589
590 // Don't send anything for https except the hostname. Hostnames are OK
591 // because they are visible when the TCP connection is established, but the
592 // specific path may reveal private information.
[email protected]e8ca69c2014-05-07 15:31:19593 if (LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
[email protected]a2fedb1e2011-01-25 15:23:36594 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14595 return false;
[email protected]83c726482008-09-10 06:36:34596
597 return true;
598}
599
[email protected]71b46152013-05-03 16:39:20600void SearchProvider::RemoveAllStaleResults() {
[email protected]dc735c02013-11-12 23:23:41601 if (keyword_input_.text().empty()) {
[email protected]1e1550e2013-05-02 17:37:51602 // User is either in keyword mode with a blank input or out of
603 // keyword mode entirely.
[email protected]cc1526e2013-05-17 04:04:24604 keyword_results_.Clear();
[email protected]1e1550e2013-05-02 17:37:51605 }
[email protected]d1f0a7f2012-06-05 10:26:42606}
607
[email protected]d1f0a7f2012-06-05 10:26:42608void SearchProvider::ApplyCalculatedRelevance() {
[email protected]cc1526e2013-05-17 04:04:24609 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
610 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
611 ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results);
612 ApplyCalculatedNavigationRelevance(&default_results_.navigation_results);
[email protected]cc1526e2013-05-17 04:04:24613 default_results_.verbatim_relevance = -1;
614 keyword_results_.verbatim_relevance = -1;
[email protected]d1f0a7f2012-06-05 10:26:42615}
616
[email protected]188b50c2013-03-28 07:19:42617void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42618 for (size_t i = 0; i < list->size(); ++i) {
[email protected]188b50c2013-03-28 07:19:42619 SuggestResult& result = (*list)[i];
620 result.set_relevance(
621 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
622 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07623 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42624 }
625}
626
[email protected]188b50c2013-03-28 07:19:42627void SearchProvider::ApplyCalculatedNavigationRelevance(
628 NavigationResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42629 for (size_t i = 0; i < list->size(); ++i) {
[email protected]188b50c2013-03-28 07:19:42630 NavigationResult& result = (*list)[i];
631 result.set_relevance(
632 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
633 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07634 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42635 }
636}
637
[email protected]15fb2aa2012-05-22 22:52:59638net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12639 int id,
[email protected]9ff91722012-09-07 05:29:12640 const TemplateURL* template_url,
[email protected]14710852013-02-05 23:45:41641 const AutocompleteInput& input) {
[email protected]9ff91722012-09-07 05:29:12642 if (!template_url || template_url->suggestions_url().empty())
643 return NULL;
644
645 // Bail if the suggestion URL is invalid with the given replacements.
[email protected]14710852013-02-05 23:45:41646 TemplateURLRef::SearchTermsArgs search_term_args(input.text());
[email protected]420472b22014-06-10 13:34:43647 search_term_args.input_type = input.type();
[email protected]14710852013-02-05 23:45:41648 search_term_args.cursor_position = input.cursor_position();
[email protected]d5015ca2013-08-08 22:04:18649 search_term_args.page_classification = input.current_page_classification();
[email protected]20184242014-05-14 02:57:42650 if (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]25e68012014-05-17 15:53:24651 switches::kEnableAnswersInSuggest))
[email protected]20184242014-05-14 02:57:42652 search_term_args.session_token = GetSessionToken();
[email protected]9ff91722012-09-07 05:29:12653 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19654 search_term_args,
655 providers_.template_url_service()->search_terms_data()));
[email protected]9ff91722012-09-07 05:29:12656 if (!suggest_url.is_valid())
657 return NULL;
[email protected]9b9fa672013-11-07 06:04:52658 // Send the current page URL if user setting and URL requirements are met and
659 // the user is in the field trial.
660 if (CanSendURL(current_page_url_, suggest_url, template_url,
661 input.current_page_classification(), profile_) &&
662 OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) {
663 search_term_args.current_page_url = current_page_url_.spec();
664 // Create the suggest URL again with the current page URL.
665 suggest_url = GURL(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19666 search_term_args,
667 providers_.template_url_service()->search_terms_data()));
[email protected]9b9fa672013-11-07 06:04:52668 }
[email protected]9ff91722012-09-07 05:29:12669
670 suggest_results_pending_++;
671 LogOmniboxSuggestRequest(REQUEST_SENT);
672
673 net::URLFetcher* fetcher =
674 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12675 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34676 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]bd3b4712012-12-18 17:01:30677 // Add Chrome experiment state to the request headers.
678 net::HttpRequestHeaders headers;
[email protected]ab7780792013-01-10 01:26:09679 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
[email protected]bd3b4712012-12-18 17:01:30680 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
681 fetcher->SetExtraRequestHeaders(headers.ToString());
[email protected]257ab712009-04-14 17:16:24682 fetcher->Start();
683 return fetcher;
684}
685
[email protected]344946a12012-12-20 12:03:42686void SearchProvider::ConvertResultsToAutocompleteMatches() {
initial.commit09911bf2008-07-26 23:55:29687 // Convert all the results to matches and add them to a map, so we can keep
688 // the most relevant match for each result.
[email protected]31afdf72013-09-26 04:29:36689 base::TimeTicks start_time(base::TimeTicks::Now());
initial.commit09911bf2008-07-26 23:55:29690 MatchMap map;
[email protected]bc8bb0cd2013-06-24 21:50:23691 const base::Time no_time;
[email protected]cc1526e2013-05-17 04:04:24692 int did_not_accept_keyword_suggestion =
693 keyword_results_.suggest_results.empty() ?
initial.commit09911bf2008-07-26 23:55:29694 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
695 TemplateURLRef::NO_SUGGESTION_CHOSEN;
initial.commit09911bf2008-07-26 23:55:29696
[email protected]d30268a2013-06-25 22:31:07697 bool relevance_from_server;
698 int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server);
[email protected]cc1526e2013-05-17 04:04:24699 int did_not_accept_default_suggestion =
700 default_results_.suggest_results.empty() ?
[email protected]55ce8f12012-05-09 04:44:08701 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
702 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]d1f0a7f2012-06-05 10:26:42703 if (verbatim_relevance > 0) {
[email protected]c2ca3fd2014-03-22 03:07:44704 const base::string16& trimmed_verbatim =
705 base::CollapseWhitespace(input_.text(), false);
[email protected]2c03c06b2013-12-11 20:45:02706 SuggestResult verbatim(
[email protected]c2ca3fd2014-03-22 03:07:44707 trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
[email protected]7ec287e2014-05-16 02:50:08708 trimmed_verbatim, base::string16(), base::string16(), base::string16(),
709 base::string16(), std::string(), std::string(), false,
710 verbatim_relevance, relevance_from_server, false,
[email protected]c2ca3fd2014-03-22 03:07:44711 trimmed_verbatim);
[email protected]57482a72014-03-14 22:27:37712 AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion,
713 false, &map);
[email protected]d1f0a7f2012-06-05 10:26:42714 }
[email protected]5423e562013-02-07 03:58:45715 if (!keyword_input_.text().empty()) {
716 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
717 // We only create the verbatim search query match for a keyword
718 // if it's not an extension keyword. Extension keywords are handled
719 // in KeywordProvider::Start(). (Extensions are complicated...)
720 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond
721 // to the keyword verbatim search query. Do not create other matches
722 // of type SEARCH_OTHER_ENGINE.
[email protected]bdcbcd82013-10-28 13:40:25723 if (keyword_url &&
724 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) {
[email protected]d30268a2013-06-25 22:31:07725 bool keyword_relevance_from_server;
726 const int keyword_verbatim_relevance =
727 GetKeywordVerbatimRelevance(&keyword_relevance_from_server);
[email protected]dab8d52d2013-03-05 07:35:28728 if (keyword_verbatim_relevance > 0) {
[email protected]c2ca3fd2014-03-22 03:07:44729 const base::string16& trimmed_verbatim =
730 base::CollapseWhitespace(keyword_input_.text(), false);
[email protected]2c03c06b2013-12-11 20:45:02731 SuggestResult verbatim(
[email protected]c2ca3fd2014-03-22 03:07:44732 trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE,
733 trimmed_verbatim, base::string16(), base::string16(),
[email protected]7ec287e2014-05-16 02:50:08734 base::string16(), base::string16(), std::string(), std::string(),
735 true, keyword_verbatim_relevance, keyword_relevance_from_server,
736 false, trimmed_verbatim);
[email protected]57482a72014-03-14 22:27:37737 AddMatchToMap(verbatim, std::string(),
738 did_not_accept_keyword_suggestion, false, &map);
[email protected]dab8d52d2013-03-05 07:35:28739 }
[email protected]5423e562013-02-07 03:58:45740 }
741 }
[email protected]257ab712009-04-14 17:16:24742 AddHistoryResultsToMap(keyword_history_results_, true,
743 did_not_accept_keyword_suggestion, &map);
744 AddHistoryResultsToMap(default_history_results_, false,
745 did_not_accept_default_suggestion, &map);
746
[email protected]d1cb6a822013-09-18 19:43:00747 AddSuggestResultsToMap(keyword_results_.suggest_results,
748 keyword_results_.metadata, &map);
[email protected]987fad782013-08-28 06:23:18749 AddSuggestResultsToMap(default_results_.suggest_results,
750 default_results_.metadata, &map);
initial.commit09911bf2008-07-26 23:55:29751
[email protected]d30268a2013-06-25 22:31:07752 ACMatches matches;
initial.commit09911bf2008-07-26 23:55:29753 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
[email protected]d30268a2013-06-25 22:31:07754 matches.push_back(i->second);
initial.commit09911bf2008-07-26 23:55:29755
[email protected]d30268a2013-06-25 22:31:07756 AddNavigationResultsToMatches(keyword_results_.navigation_results, &matches);
757 AddNavigationResultsToMatches(default_results_.navigation_results, &matches);
initial.commit09911bf2008-07-26 23:55:29758
[email protected]d30268a2013-06-25 22:31:07759 // Now add the most relevant matches to |matches_|. We take up to kMaxMatches
760 // suggest/navsuggest matches, regardless of origin. If Instant Extended is
761 // enabled and we have server-provided (and thus hopefully more accurate)
762 // scores for some suggestions, we allow more of those, until we reach
763 // AutocompleteResult::kMaxMatches total matches (that is, enough to fill the
764 // whole popup).
765 //
766 // We will always return any verbatim matches, no matter how we obtained their
767 // scores, unless we have already accepted AutocompleteResult::kMaxMatches
768 // higher-scoring matches under the conditions above.
[email protected]d30268a2013-06-25 22:31:07769 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant);
770 matches_.clear();
[email protected]3723e6e2012-06-11 21:06:56771
[email protected]d30268a2013-06-25 22:31:07772 size_t num_suggestions = 0;
773 for (ACMatches::const_iterator i(matches.begin());
774 (i != matches.end()) &&
775 (matches_.size() < AutocompleteResult::kMaxMatches);
776 ++i) {
777 // SEARCH_OTHER_ENGINE is only used in the SearchProvider for the keyword
778 // verbatim result, so this condition basically means "if this match is a
779 // suggestion of some sort".
780 if ((i->type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED) &&
781 (i->type != AutocompleteMatchType::SEARCH_OTHER_ENGINE)) {
782 // If we've already hit the limit on non-server-scored suggestions, and
783 // this isn't a server-scored suggestion we can add, skip it.
784 if ((num_suggestions >= kMaxMatches) &&
785 (!chrome::IsInstantExtendedAPIEnabled() ||
786 (i->GetAdditionalInfo(kRelevanceFromServerKey) != kTrue))) {
787 continue;
788 }
789
790 ++num_suggestions;
791 }
792
793 matches_.push_back(*i);
794 }
[email protected]31afdf72013-09-26 04:29:36795 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime",
796 base::TimeTicks::Now() - start_time);
[email protected]344946a12012-12-20 12:03:42797}
798
[email protected]89bd27d12014-04-12 17:36:23799ACMatches::const_iterator SearchProvider::FindTopMatch() const {
[email protected]0a8718b12013-11-13 18:41:31800 ACMatches::const_iterator it = matches_.begin();
801 while ((it != matches_.end()) && !it->allowed_to_be_default_match)
802 ++it;
803 return it;
[email protected]9dfb4d362013-04-05 02:15:12804}
805
[email protected]1c1e7842013-11-22 16:28:16806bool SearchProvider::HasKeywordDefaultMatchInKeywordMode() const {
807 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
808 // If the user is not in keyword mode, return true to say that this
809 // constraint is not violated.
810 if (keyword_url == NULL)
811 return true;
812 for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end();
813 ++it) {
814 if ((it->keyword == keyword_url->keyword()) &&
815 it->allowed_to_be_default_match)
816 return true;
817 }
818 return false;
819}
820
[email protected]89bd27d12014-04-12 17:36:23821bool SearchProvider::IsTopMatchSearchWithURLInput() const {
822 ACMatches::const_iterator first_match = FindTopMatch();
[email protected]3dc75b12014-06-08 00:02:22823 return (input_.type() == metrics::OmniboxInputType::URL) &&
[email protected]0a8718b12013-11-13 18:41:31824 (first_match != matches_.end()) &&
825 (first_match->relevance > CalculateRelevanceForVerbatim()) &&
[email protected]78981d8c2014-05-09 15:05:47826 (first_match->type != AutocompleteMatchType::NAVSUGGEST) &&
827 (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED);
[email protected]344946a12012-12-20 12:03:42828}
829
[email protected]257ab712009-04-14 17:16:24830void SearchProvider::AddNavigationResultsToMatches(
[email protected]d30268a2013-06-25 22:31:07831 const NavigationResults& navigation_results,
832 ACMatches* matches) {
[email protected]bc8bb0cd2013-06-24 21:50:23833 for (NavigationResults::const_iterator it = navigation_results.begin();
834 it != navigation_results.end(); ++it) {
[email protected]d30268a2013-06-25 22:31:07835 matches->push_back(NavigationToMatch(*it));
[email protected]bc8bb0cd2013-06-24 21:50:23836 // In the absence of suggested relevance scores, use only the single
837 // highest-scoring result. (The results are already sorted by relevance.)
[email protected]d30268a2013-06-25 22:31:07838 if (!it->relevance_from_server())
[email protected]bc8bb0cd2013-06-24 21:50:23839 return;
[email protected]257ab712009-04-14 17:16:24840 }
841}
842
843void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
844 bool is_keyword,
845 int did_not_accept_suggestion,
846 MatchMap* map) {
[email protected]51124552011-07-16 01:37:10847 if (results.empty())
848 return;
849
[email protected]31afdf72013-09-26 04:29:36850 base::TimeTicks start_time(base::TimeTicks::Now());
[email protected]d7ad4772012-06-01 03:12:54851 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
[email protected]3dc75b12014-06-08 00:02:22852 (input_.type() == metrics::OmniboxInputType::URL);
[email protected]0085863a2013-12-06 21:19:03853 const base::string16& input_text =
[email protected]14710852013-02-05 23:45:41854 is_keyword ? keyword_input_.text() : input_.text();
[email protected]51124552011-07-16 01:37:10855 bool input_multiple_words = HasMultipleWords(input_text);
856
[email protected]55ce8f12012-05-09 04:44:08857 SuggestResults scored_results;
858 if (!prevent_inline_autocomplete && input_multiple_words) {
859 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
860 // queries if the input also has multiple words. But if we were already
[email protected]f13a2132014-04-22 08:13:52861 // scoring a multi-word, multi-visit query aggressively, and the current
862 // input is still a prefix of it, then changing the suggestion suddenly
863 // feels wrong. To detect this case, first score as if only one word has
864 // been typed, then check if the best result came from aggressive search
865 // history scoring. If it did, then just keep that score set. This
866 // 1200 the lowest possible score in CalculateRelevanceForHistory()'s
867 // aggressive-scoring curve.
[email protected]55ce8f12012-05-09 04:44:08868 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
869 false, input_text, is_keyword);
[email protected]f13a2132014-04-22 08:13:52870 if ((scored_results.front().relevance() < 1200) ||
[email protected]9dfb4d362013-04-05 02:15:12871 !HasMultipleWords(scored_results.front().suggestion()))
[email protected]55ce8f12012-05-09 04:44:08872 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:10873 }
[email protected]55ce8f12012-05-09 04:44:08874 if (scored_results.empty())
875 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
876 input_multiple_words, input_text,
877 is_keyword);
878 for (SuggestResults::const_iterator i(scored_results.begin());
879 i != scored_results.end(); ++i) {
[email protected]57482a72014-03-14 22:27:37880 AddMatchToMap(*i, std::string(), did_not_accept_suggestion, true, map);
[email protected]51124552011-07-16 01:37:10881 }
[email protected]31afdf72013-09-26 04:29:36882 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime",
883 base::TimeTicks::Now() - start_time);
[email protected]51124552011-07-16 01:37:10884}
885
[email protected]55ce8f12012-05-09 04:44:08886SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:10887 const HistoryResults& results,
888 bool base_prevent_inline_autocomplete,
889 bool input_multiple_words,
[email protected]0085863a2013-12-06 21:19:03890 const base::string16& input_text,
[email protected]51124552011-07-16 01:37:10891 bool is_keyword) {
[email protected]810ffba2012-06-12 01:07:48892 AutocompleteClassifier* classifier =
893 AutocompleteClassifierFactory::GetForProfile(profile_);
[email protected]55ce8f12012-05-09 04:44:08894 SuggestResults scored_results;
[email protected]78e5e432013-08-03 02:10:10895 const bool prevent_search_history_inlining =
896 OmniboxFieldTrial::SearchHistoryPreventInlining(
897 input_.current_page_classification());
[email protected]c2ca3fd2014-03-22 03:07:44898 const base::string16& trimmed_input =
899 base::CollapseWhitespace(input_text, false);
[email protected]257ab712009-04-14 17:16:24900 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
901 ++i) {
[email protected]c2ca3fd2014-03-22 03:07:44902 const base::string16& trimmed_suggestion =
903 base::CollapseWhitespace(i->term, false);
904
[email protected]51124552011-07-16 01:37:10905 // Don't autocomplete multi-word queries that have only been seen once
906 // unless the user has typed more than one word.
907 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
[email protected]c2ca3fd2014-03-22 03:07:44908 (!input_multiple_words && (i->visits < 2) &&
909 HasMultipleWords(trimmed_suggestion));
[email protected]51124552011-07-16 01:37:10910
[email protected]ea3b9a502011-04-04 14:19:37911 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:10912 // when typed. For example, if the user searched for "google.com" and types
913 // "goog", don't autocomplete to the search term "google.com". Otherwise,
914 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:48915 // NOTE: We don't check this in the following cases:
916 // * When inline autocomplete is disabled, we won't be inline
917 // autocompleting this term, so we don't need to worry about confusion as
918 // much. This also prevents calling Classify() again from inside the
919 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:10920 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:48921 // * When the user has typed the whole term, the "what you typed" history
922 // match will outrank us for URL-like inputs anyway, so we need not do
923 // anything special.
[email protected]c2ca3fd2014-03-22 03:07:44924 if (!prevent_inline_autocomplete && classifier &&
925 (trimmed_suggestion != trimmed_input)) {
[email protected]ea3b9a502011-04-04 14:19:37926 AutocompleteMatch match;
[email protected]c2ca3fd2014-03-22 03:07:44927 classifier->Classify(trimmed_suggestion, false, false,
[email protected]51abb7b2014-02-09 23:00:08928 input_.current_page_classification(), &match, NULL);
[email protected]2905f742011-10-13 03:51:58929 prevent_inline_autocomplete =
[email protected]749e7ae02012-09-05 18:47:46930 !AutocompleteMatch::IsSearchType(match.type);
[email protected]ea3b9a502011-04-04 14:19:37931 }
[email protected]51124552011-07-16 01:37:10932
[email protected]78e5e432013-08-03 02:10:10933 int relevance = CalculateRelevanceForHistory(
934 i->time, is_keyword, !prevent_inline_autocomplete,
935 prevent_search_history_inlining);
[email protected]2c03c06b2013-12-11 20:45:02936 scored_results.push_back(SuggestResult(
[email protected]c2ca3fd2014-03-22 03:07:44937 trimmed_suggestion, AutocompleteMatchType::SEARCH_HISTORY,
[email protected]7ec287e2014-05-16 02:50:08938 trimmed_suggestion, base::string16(), base::string16(),
939 base::string16(), base::string16(), std::string(), std::string(),
940 is_keyword, relevance, false, false, trimmed_input));
[email protected]257ab712009-04-14 17:16:24941 }
[email protected]51124552011-07-16 01:37:10942
943 // History returns results sorted for us. However, we may have docked some
944 // results' scores, so things are no longer in order. Do a stable sort to get
945 // things back in order without otherwise disturbing results with equal
946 // scores, then force the scores to be unique, so that the order in which
947 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:08948 std::stable_sort(scored_results.begin(), scored_results.end(),
949 CompareScoredResults());
[email protected]51124552011-07-16 01:37:10950 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:08951 for (SuggestResults::iterator i(scored_results.begin());
952 i != scored_results.end(); ++i) {
953 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
954 i->set_relevance(last_relevance - 1);
955 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:10956 }
957
[email protected]55ce8f12012-05-09 04:44:08958 return scored_results;
[email protected]257ab712009-04-14 17:16:24959}
960
[email protected]55ce8f12012-05-09 04:44:08961void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
[email protected]987fad782013-08-28 06:23:18962 const std::string& metadata,
[email protected]55ce8f12012-05-09 04:44:08963 MatchMap* map) {
[email protected]9487b392014-02-14 02:48:18964 for (size_t i = 0; i < results.size(); ++i)
[email protected]57482a72014-03-14 22:27:37965 AddMatchToMap(results[i], metadata, i, false, map);
initial.commit09911bf2008-07-26 23:55:29966}
967
[email protected]d30268a2013-06-25 22:31:07968int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const {
[email protected]dc6943b2012-06-19 06:39:56969 // Use the suggested verbatim relevance score if it is non-negative (valid),
970 // if inline autocomplete isn't prevented (always show verbatim on backspace),
[email protected]1beee342012-06-19 22:22:28971 // and if it won't suppress verbatim, leaving no default provider matches.
972 // Otherwise, if the default provider returned no matches and was still able
[email protected]dc6943b2012-06-19 06:39:56973 // to suppress verbatim, the user would have no search/nav matches and may be
[email protected]1beee342012-06-19 22:22:28974 // left unable to search using their default provider from the omnibox.
[email protected]dc6943b2012-06-19 06:39:56975 // Check for results on each verbatim calculation, as results from older
976 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:23977 bool use_server_relevance =
978 (default_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:28979 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:23980 ((default_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:24981 !default_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:23982 !default_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:07983 if (relevance_from_server)
984 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:23985 return use_server_relevance ?
986 default_results_.verbatim_relevance : CalculateRelevanceForVerbatim();
[email protected]382a0642012-06-06 06:13:52987}
[email protected]d1f0a7f2012-06-05 10:26:42988
[email protected]382a0642012-06-06 06:13:52989int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:47990 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:36991 return 250;
[email protected]dab8d52d2013-03-05 07:35:28992 return CalculateRelevanceForVerbatimIgnoringKeywordModeState();
993}
[email protected]52d08b12009-10-19 18:42:36994
[email protected]dab8d52d2013-03-05 07:35:28995int SearchProvider::
996 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const {
initial.commit09911bf2008-07-26 23:55:29997 switch (input_.type()) {
[email protected]3dc75b12014-06-08 00:02:22998 case metrics::OmniboxInputType::UNKNOWN:
999 case metrics::OmniboxInputType::QUERY:
1000 case metrics::OmniboxInputType::FORCED_QUERY:
[email protected]90fe2bb2013-01-15 03:42:131001 return kNonURLVerbatimRelevance;
initial.commit09911bf2008-07-26 23:55:291002
[email protected]3dc75b12014-06-08 00:02:221003 case metrics::OmniboxInputType::URL:
[email protected]52d08b12009-10-19 18:42:361004 return 850;
initial.commit09911bf2008-07-26 23:55:291005
1006 default:
1007 NOTREACHED();
1008 return 0;
1009 }
1010}
1011
[email protected]d30268a2013-06-25 22:31:071012int SearchProvider::GetKeywordVerbatimRelevance(
1013 bool* relevance_from_server) const {
[email protected]dab8d52d2013-03-05 07:35:281014 // Use the suggested verbatim relevance score if it is non-negative (valid),
1015 // if inline autocomplete isn't prevented (always show verbatim on backspace),
1016 // and if it won't suppress verbatim, leaving no keyword provider matches.
1017 // Otherwise, if the keyword provider returned no matches and was still able
1018 // to suppress verbatim, the user would have no search/nav matches and may be
1019 // left unable to search using their keyword provider from the omnibox.
1020 // Check for results on each verbatim calculation, as results from older
1021 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:231022 bool use_server_relevance =
1023 (keyword_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:281024 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:231025 ((keyword_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:241026 !keyword_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:231027 !keyword_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:071028 if (relevance_from_server)
1029 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:231030 return use_server_relevance ?
1031 keyword_results_.verbatim_relevance :
1032 CalculateRelevanceForKeywordVerbatim(keyword_input_.type(),
1033 keyword_input_.prefer_keyword());
[email protected]5423e562013-02-07 03:58:451034}
1035
[email protected]51124552011-07-16 01:37:101036int SearchProvider::CalculateRelevanceForHistory(
[email protected]bc8bb0cd2013-06-24 21:50:231037 const base::Time& time,
[email protected]51124552011-07-16 01:37:101038 bool is_keyword,
[email protected]78e5e432013-08-03 02:10:101039 bool use_aggressive_method,
1040 bool prevent_search_history_inlining) const {
[email protected]aa613d62010-11-09 20:40:181041 // The relevance of past searches falls off over time. There are two distinct
1042 // equations used. If the first equation is used (searches to the primary
[email protected]78e5e432013-08-03 02:10:101043 // provider that we want to score aggressively), the score is in the range
1044 // 1300-1599 (unless |prevent_search_history_inlining|, in which case
[email protected]d8cd76b2013-07-10 09:46:161045 // it's in the range 1200-1299). If the second equation is used the
1046 // relevance of a search 15 minutes ago is discounted 50 points, while the
1047 // relevance of a search two weeks ago is discounted 450 points.
[email protected]bc8bb0cd2013-06-24 21:50:231048 double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0);
[email protected]188b50c2013-03-28 07:19:421049 bool is_primary_provider = is_keyword || !providers_.has_keyword_provider();
[email protected]78e5e432013-08-03 02:10:101050 if (is_primary_provider && use_aggressive_method) {
[email protected]aa613d62010-11-09 20:40:181051 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:101052 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:181053 if (elapsed_time < autocomplete_time) {
[email protected]d8cd76b2013-07-10 09:46:161054 int max_score = is_keyword ? 1599 : 1399;
[email protected]78e5e432013-08-03 02:10:101055 if (prevent_search_history_inlining)
[email protected]d8cd76b2013-07-10 09:46:161056 max_score = 1299;
1057 return max_score - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:181058 std::pow(elapsed_time / autocomplete_time, 2.5));
1059 }
1060 elapsed_time -= autocomplete_time;
1061 }
1062
[email protected]c3a4bd992010-08-18 20:25:011063 const int score_discount =
1064 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:291065
[email protected]6c85aa02009-02-27 12:08:091066 // Don't let scores go below 0. Negative relevance scores are meaningful in
1067 // a different way.
initial.commit09911bf2008-07-26 23:55:291068 int base_score;
[email protected]51124552011-07-16 01:37:101069 if (is_primary_provider)
[email protected]3dc75b12014-06-08 00:02:221070 base_score = (input_.type() == metrics::OmniboxInputType::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:101071 else
1072 base_score = 200;
initial.commit09911bf2008-07-26 23:55:291073 return std::max(0, base_score - score_discount);
1074}
1075
initial.commit09911bf2008-07-26 23:55:291076AutocompleteMatch SearchProvider::NavigationToMatch(
[email protected]188b50c2013-03-28 07:19:421077 const NavigationResult& navigation) {
[email protected]5889bfb2014-03-19 00:26:481078 base::string16 input;
1079 const bool trimmed_whitespace = base::TrimWhitespace(
1080 navigation.from_keyword_provider() ?
1081 keyword_input_.text() : input_.text(),
1082 base::TRIM_TRAILING, &input) != base::TRIM_NONE;
[email protected]55ce8f12012-05-09 04:44:081083 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:471084 navigation.type());
[email protected]55ce8f12012-05-09 04:44:081085 match.destination_url = navigation.url();
[email protected]78981d8c2014-05-09 15:05:471086 BaseSearchProvider::SetDeletionURL(navigation.deletion_url(), &match);
[email protected]23db6492014-01-16 02:35:301087 // First look for the user's input inside the formatted url as it would be
[email protected]371dab12012-06-01 03:23:551088 // without trimming the scheme, so we can find matches at the beginning of the
1089 // scheme.
[email protected]371dab12012-06-01 03:23:551090 const URLPrefix* prefix =
[email protected]23db6492014-01-16 02:35:301091 URLPrefix::BestURLPrefix(navigation.formatted_url(), input);
[email protected]371dab12012-06-01 03:23:551092 size_t match_start = (prefix == NULL) ?
[email protected]23db6492014-01-16 02:35:301093 navigation.formatted_url().find(input) : prefix->prefix.length();
[email protected]d2445c82013-11-04 22:28:351094 bool trim_http = !AutocompleteInput::HasHTTPScheme(input) &&
1095 (!prefix || (match_start != 0));
[email protected]23db6492014-01-16 02:35:301096 const net::FormatUrlTypes format_types =
1097 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
[email protected]371dab12012-06-01 03:23:551098
[email protected]371dab12012-06-01 03:23:551099 const std::string languages(
1100 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]23db6492014-01-16 02:35:301101 size_t inline_autocomplete_offset = (prefix == NULL) ?
1102 base::string16::npos : (match_start + input.length());
[email protected]371dab12012-06-01 03:23:551103 match.fill_into_edit +=
1104 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1105 net::FormatUrl(navigation.url(), languages, format_types,
1106 net::UnescapeRule::SPACES, NULL, NULL,
1107 &inline_autocomplete_offset));
[email protected]14119032013-11-07 08:14:261108 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1109 // Otherwise, user edits to a suggestion would show non-Search results.
[email protected]3dc75b12014-06-08 00:02:221110 if (input_.type() == metrics::OmniboxInputType::FORCED_QUERY) {
[email protected]670d3232013-12-24 17:58:581111 match.fill_into_edit.insert(0, base::ASCIIToUTF16("?"));
[email protected]0085863a2013-12-06 21:19:031112 if (inline_autocomplete_offset != base::string16::npos)
[email protected]14119032013-11-07 08:14:261113 ++inline_autocomplete_offset;
1114 }
[email protected]6c94a1022014-02-21 03:48:041115 if (inline_autocomplete_offset != base::string16::npos) {
[email protected]518024c2013-07-19 23:40:251116 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
1117 match.inline_autocompletion =
1118 match.fill_into_edit.substr(inline_autocomplete_offset);
1119 }
[email protected]6c94a1022014-02-21 03:48:041120 // An inlineable navsuggestion can only be the default match when there
1121 // is no keyword provider active, lest it appear first and break the user
[email protected]5889bfb2014-03-19 00:26:481122 // out of keyword mode. It can also only be default if either the inline
1123 // autocompletion is empty or we're not preventing inline autocompletion.
1124 // Finally, if we have an inlineable navsuggestion with an inline completion
1125 // that we're not preventing, make sure we didn't trim any whitespace.
1126 // We don't want to claim http://foo.com/bar is inlineable against the
1127 // input "foo.com/b ".
[email protected]6c94a1022014-02-21 03:48:041128 match.allowed_to_be_default_match = navigation.IsInlineable(input) &&
1129 (providers_.GetKeywordProviderURL() == NULL) &&
[email protected]5889bfb2014-03-19 00:26:481130 (match.inline_autocompletion.empty() ||
[email protected]78981d8c2014-05-09 15:05:471131 (!input_.prevent_inline_autocomplete() && !trimmed_whitespace));
[email protected]371dab12012-06-01 03:23:551132
[email protected]23db6492014-01-16 02:35:301133 match.contents = navigation.match_contents();
1134 match.contents_class = navigation.match_contents_class();
[email protected]55ce8f12012-05-09 04:44:081135 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551136 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1137 ACMatchClassification::NONE, &match.description_class);
[email protected]d30268a2013-06-25 22:31:071138
1139 match.RecordAdditionalInfo(
1140 kRelevanceFromServerKey,
1141 navigation.relevance_from_server() ? kTrue : kFalse);
[email protected]987fad782013-08-28 06:23:181142 match.RecordAdditionalInfo(kShouldPrefetchKey, kFalse);
[email protected]d30268a2013-06-25 22:31:071143
initial.commit09911bf2008-07-26 23:55:291144 return match;
1145}
[email protected]4ab4c7c2010-11-24 04:49:341146
[email protected]4ab4c7c2010-11-24 04:49:341147void SearchProvider::UpdateDone() {
[email protected]2cdf1172012-08-26 12:21:331148 // We're done when the timer isn't running, there are no suggest queries
[email protected]5fac3782013-03-06 09:32:311149 // pending, and we're not waiting on Instant.
[email protected]e1290ee62013-06-26 18:31:151150 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0);
[email protected]4ab4c7c2010-11-24 04:49:341151}
[email protected]20184242014-05-14 02:57:421152
1153std::string SearchProvider::GetSessionToken() {
1154 base::TimeTicks current_time(base::TimeTicks::Now());
1155 // Renew token if it expired.
1156 if (current_time > token_expiration_time_) {
1157 const size_t kTokenBytes = 12;
1158 std::string raw_data;
1159 base::RandBytes(WriteInto(&raw_data, kTokenBytes + 1), kTokenBytes);
1160 base::Base64Encode(raw_data, &current_token_);
[email protected]ab2c31f72014-05-17 17:03:531161
1162 // Make the base64 encoded value URL and filename safe(see RFC 3548).
1163 std::replace(current_token_.begin(), current_token_.end(), '+', '-');
1164 std::replace(current_token_.begin(), current_token_.end(), '/', '_');
[email protected]20184242014-05-14 02:57:421165 }
1166
1167 // Extend expiration time another 60 seconds.
1168 token_expiration_time_ = current_time + base::TimeDelta::FromSeconds(60);
1169
1170 return current_token_;
1171}