blob: f2d2a59033541a0c948d16a6e2ef33e20193b244 [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
hashimoto5f7db4b2014-08-27 02:46:205#include "components/omnibox/search_provider.h"
initial.commit09911bf2008-07-26 23:55:296
[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]51124552011-07-16 01:37:1012#include "base/i18n/break_iterator.h"
[email protected]503d03872011-05-06 08:36:2613#include "base/i18n/case_conversion.h"
[email protected]ffbec692012-02-26 20:26:4214#include "base/json/json_string_value_serializer.h"
[email protected]f5b95ba92012-03-27 14:05:1915#include "base/metrics/histogram.h"
[email protected]f7f41c0e2014-08-11 04:22:2316#include "base/metrics/user_metrics.h"
[email protected]20184242014-05-14 02:57:4217#include "base/rand_util.h"
[email protected]5889bfb2014-03-19 00:26:4818#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2019#include "base/strings/utf_string_conversions.h"
[email protected]e3ce70ac2014-06-26 18:34:5620#include "components/history/core/browser/in_memory_database.h"
[email protected]73b2d1e72014-06-25 23:45:3621#include "components/history/core/browser/keyword_search_term.h"
[email protected]3dc75b12014-06-08 00:02:2222#include "components/metrics/proto/omnibox_input_type.pb.h"
Ryo Hashimoto884ad192014-08-28 05:54:3023#include "components/omnibox/autocomplete_provider_client.h"
[email protected]b1c5ab682014-08-07 11:53:1724#include "components/omnibox/autocomplete_provider_listener.h"
[email protected]4b56c602014-08-14 17:02:3125#include "components/omnibox/autocomplete_result.h"
[email protected]02f5e32d2014-08-22 07:44:4626#include "components/omnibox/keyword_provider.h"
[email protected]4c583b62014-08-08 10:37:2327#include "components/omnibox/omnibox_field_trial.h"
[email protected]b1c5ab682014-08-07 11:53:1728#include "components/omnibox/url_prefix.h"
[email protected]720b10492014-07-23 08:48:4029#include "components/search/search.h"
[email protected]0915b352014-06-25 19:58:1430#include "components/search_engines/template_url_prepopulate_data.h"
[email protected]bf5c532d2014-07-05 00:29:5331#include "components/search_engines/template_url_service.h"
isherman3be67db2014-10-24 05:57:4432#include "components/variations/net/variations_http_header_provider.h"
[email protected]53f0cab2014-08-18 09:52:2733#include "grit/components_strings.h"
initial.commit09911bf2008-07-26 23:55:2934#include "net/base/escape.h"
[email protected]d3cf8682f02012-02-29 23:29:3435#include "net/base/load_flags.h"
[email protected]371dab12012-06-01 03:23:5536#include "net/base/net_util.h"
[email protected]bd3b4712012-12-18 17:01:3037#include "net/http/http_request_headers.h"
[email protected]3dc1bc42012-06-19 08:20:5338#include "net/url_request/url_fetcher.h"
[email protected]319d9e6f2009-02-18 19:47:2139#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1740#include "ui/base/l10n/l10n_util.h"
[email protected]cca6f392014-05-28 21:32:2641#include "url/url_constants.h"
[email protected]761fa4702013-07-02 15:25:1542#include "url/url_util.h"
initial.commit09911bf2008-07-26 23:55:2943
[email protected]bc8bb0cd2013-06-24 21:50:2344// Helpers --------------------------------------------------------------------
[email protected]e1acf6f2008-10-27 20:43:3345
[email protected]51124552011-07-16 01:37:1046namespace {
47
[email protected]7706a522012-08-16 17:42:2548// We keep track in a histogram how many suggest requests we send, how
49// many suggest requests we invalidate (e.g., due to a user typing
50// another character), and how many replies we receive.
51// *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
52// (excluding the end-of-list enum value)
53// We do not want values of existing enums to change or else it screws
54// up the statistics.
55enum SuggestRequestsHistogramValue {
56 REQUEST_SENT = 1,
57 REQUEST_INVALIDATED,
58 REPLY_RECEIVED,
59 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE
60};
61
[email protected]90fe2bb2013-01-15 03:42:1362// The verbatim score for an input which is not an URL.
63const int kNonURLVerbatimRelevance = 1300;
64
[email protected]7706a522012-08-16 17:42:2565// Increments the appropriate value in the histogram by one.
66void LogOmniboxSuggestRequest(
67 SuggestRequestsHistogramValue request_value) {
68 UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value,
69 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE);
70}
71
[email protected]0085863a2013-12-06 21:19:0372bool HasMultipleWords(const base::string16& text) {
[email protected]51124552011-07-16 01:37:1073 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
74 bool found_word = false;
75 if (i.Init()) {
76 while (i.Advance()) {
77 if (i.IsWord()) {
78 if (found_word)
79 return true;
80 found_word = true;
81 }
82 }
83 }
84 return false;
85}
86
[email protected]d1f0a7f2012-06-05 10:26:4287} // namespace
[email protected]51124552011-07-16 01:37:1088
[email protected]3954c3a2012-04-10 20:17:5589// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:5890
[email protected]85b8d6f2012-05-08 20:53:4791SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
[email protected]02346202014-02-05 05:18:3092 : template_url_service_(template_url_service) {}
[email protected]85b8d6f2012-05-08 20:53:4793
94const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
95 return default_provider_.empty() ? NULL :
96 template_url_service_->GetTemplateURLForKeyword(default_provider_);
97}
98
99const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
100 return keyword_provider_.empty() ? NULL :
101 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:24102}
103
[email protected]3954c3a2012-04-10 20:17:55104
[email protected]bc8bb0cd2013-06-24 21:50:23105// SearchProvider::CompareScoredResults ---------------------------------------
106
107class SearchProvider::CompareScoredResults {
108 public:
[email protected]0b9575f2014-07-30 11:58:37109 bool operator()(const SearchSuggestionParser::Result& a,
110 const SearchSuggestionParser::Result& b) {
[email protected]bc8bb0cd2013-06-24 21:50:23111 // Sort in descending relevance order.
112 return a.relevance() > b.relevance();
113 }
114};
115
116
[email protected]3954c3a2012-04-10 20:17:55117// SearchProvider -------------------------------------------------------------
118
hashimoto663b9f42014-08-26 04:29:20119SearchProvider::SearchProvider(
120 AutocompleteProviderListener* listener,
121 TemplateURLService* template_url_service,
Ryo Hashimoto884ad192014-08-28 05:54:30122 scoped_ptr<AutocompleteProviderClient> client)
123 : BaseSearchProvider(template_url_service, client.Pass(),
[email protected]e6477f12014-08-05 07:59:54124 AutocompleteProvider::TYPE_SEARCH),
[email protected]776ee5902014-08-11 09:15:19125 listener_(listener),
126 suggest_results_pending_(0),
[email protected]ebbac63e2014-08-22 01:43:06127 providers_(template_url_service),
groby1dbb8e22014-09-23 21:50:26128 answers_cache_(10) {
[email protected]bc8bb0cd2013-06-24 21:50:23129}
130
[email protected]cb86ee6f2013-04-28 16:58:15131// static
[email protected]987fad782013-08-28 06:23:18132std::string SearchProvider::GetSuggestMetadata(const AutocompleteMatch& match) {
133 return match.GetAdditionalInfo(kSuggestMetadataKey);
134}
135
[email protected]bc8bb0cd2013-06-24 21:50:23136void SearchProvider::ResetSession() {
137 field_trial_triggered_in_session_ = false;
[email protected]4ab4c7c2010-11-24 04:49:34138}
139
[email protected]bc8bb0cd2013-06-24 21:50:23140SearchProvider::~SearchProvider() {
141}
142
[email protected]ee6110b2014-01-09 22:26:31143// static
[email protected]bc8bb0cd2013-06-24 21:50:23144int SearchProvider::CalculateRelevanceForKeywordVerbatim(
[email protected]332d17d22014-06-20 16:56:03145 metrics::OmniboxInputType::Type type,
[email protected]bc8bb0cd2013-06-24 21:50:23146 bool prefer_keyword) {
147 // This function is responsible for scoring verbatim query matches
148 // for non-extension keywords. KeywordProvider::CalculateRelevance()
149 // scores verbatim query matches for extension keywords, as well as
150 // for keyword matches (i.e., suggestions of a keyword itself, not a
151 // suggestion of a query on a keyword search engine). These two
152 // functions are currently in sync, but there's no reason we
153 // couldn't decide in the future to score verbatim matches
154 // differently for extension and non-extension keywords. If you
155 // make such a change, however, you should update this comment to
156 // describe it, so it's clear why the functions diverge.
157 if (prefer_keyword)
158 return 1500;
[email protected]3dc75b12014-06-08 00:02:22159 return (type == metrics::OmniboxInputType::QUERY) ? 1450 : 1100;
[email protected]bc8bb0cd2013-06-24 21:50:23160}
161
mpearson6c183672014-09-03 02:09:42162// static
163void SearchProvider::UpdateOldResults(
164 bool minimal_changes,
165 SearchSuggestionParser::Results* results) {
166 // When called without |minimal_changes|, it likely means the user has
167 // pressed a key. Revise the cached results appropriately.
168 if (!minimal_changes) {
169 for (SearchSuggestionParser::SuggestResults::iterator sug_it =
170 results->suggest_results.begin();
171 sug_it != results->suggest_results.end(); ++sug_it) {
172 sug_it->set_received_after_last_keystroke(false);
173 }
174 for (SearchSuggestionParser::NavigationResults::iterator nav_it =
175 results->navigation_results.begin();
176 nav_it != results->navigation_results.end(); ++nav_it) {
177 nav_it->set_received_after_last_keystroke(false);
178 }
179 }
180}
181
182// static
183ACMatches::iterator SearchProvider::FindTopMatch(ACMatches* matches) {
184 ACMatches::iterator it = matches->begin();
185 while ((it != matches->end()) && !it->allowed_to_be_default_match)
186 ++it;
187 return it;
188}
189
initial.commit09911bf2008-07-26 23:55:29190void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27191 bool minimal_changes) {
[email protected]04504c242013-01-22 21:08:55192 // Do our best to load the model as early as possible. This will reduce
193 // odds of having the model not ready when really needed (a non-empty input).
194 TemplateURLService* model = providers_.template_url_service();
195 DCHECK(model);
196 model->Load();
197
initial.commit09911bf2008-07-26 23:55:29198 matches_.clear();
[email protected]618d6e62012-12-16 05:55:57199 field_trial_triggered_ = false;
initial.commit09911bf2008-07-26 23:55:29200
hashimoto663b9f42014-08-26 04:29:20201 // Can't return search/suggest results for bogus input.
202 if (input.type() == metrics::OmniboxInputType::INVALID) {
[email protected]9950e5d52014-02-25 23:34:19203 Stop(true);
initial.commit09911bf2008-07-26 23:55:29204 return;
205 }
206
[email protected]14710852013-02-05 23:45:41207 keyword_input_ = input;
[email protected]257ab712009-04-14 17:16:24208 const TemplateURL* keyword_provider =
[email protected]14710852013-02-05 23:45:41209 KeywordProvider::GetSubstitutingTemplateURLForInput(model,
210 &keyword_input_);
211 if (keyword_provider == NULL)
212 keyword_input_.Clear();
213 else if (keyword_input_.text().empty())
[email protected]257ab712009-04-14 17:16:24214 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24215
[email protected]85b8d6f2012-05-08 20:53:47216 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]ce7ee5f2014-06-16 23:41:19217 if (default_provider &&
218 !default_provider->SupportsReplacement(model->search_terms_data()))
[email protected]257ab712009-04-14 17:16:24219 default_provider = NULL;
220
221 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18222 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24223
224 if (!default_provider && !keyword_provider) {
225 // No valid providers.
[email protected]9950e5d52014-02-25 23:34:19226 Stop(true);
initial.commit09911bf2008-07-26 23:55:29227 return;
228 }
229
230 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24231 // or the providers, abort the query.
[email protected]0085863a2013-12-06 21:19:03232 base::string16 default_provider_keyword(default_provider ?
233 default_provider->keyword() : base::string16());
234 base::string16 keyword_provider_keyword(keyword_provider ?
235 keyword_provider->keyword() : base::string16());
[email protected]9e789742011-01-10 23:27:32236 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47237 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]bb900e02013-03-14 14:15:29238 // Cancel any in-flight suggest requests.
[email protected]e1290ee62013-06-26 18:31:15239 if (!done_)
[email protected]e29249dc52012-07-19 17:33:50240 Stop(false);
[email protected]257ab712009-04-14 17:16:24241 }
initial.commit09911bf2008-07-26 23:55:29242
[email protected]85b8d6f2012-05-08 20:53:47243 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29244
245 if (input.text().empty()) {
246 // User typed "?" alone. Give them a placeholder result indicating what
247 // this syntax does.
[email protected]257ab712009-04-14 17:16:24248 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00249 AutocompleteMatch match;
250 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36251 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24252 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35253 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47254 match.keyword = providers_.default_provider();
[email protected]45f89a92013-08-12 13:41:36255 match.allowed_to_be_default_match = true;
[email protected]257ab712009-04-14 17:16:24256 matches_.push_back(match);
257 }
[email protected]9950e5d52014-02-25 23:34:19258 Stop(true);
initial.commit09911bf2008-07-26 23:55:29259 return;
260 }
261
262 input_ = input;
263
[email protected]e1290ee62013-06-26 18:31:15264 DoHistoryQuery(minimal_changes);
grobye5fcee42014-09-26 03:36:46265 // Answers needs scored history results before any suggest query has been
266 // started, since the query for answer-bearing results needs additional
267 // prefetch information based on the highest-scored local history result.
268 if (OmniboxFieldTrial::EnableAnswersInSuggest()) {
269 ScoreHistoryResults(raw_default_history_results_,
270 false,
271 &transformed_default_history_results_);
272 ScoreHistoryResults(raw_keyword_history_results_,
273 true,
274 &transformed_keyword_history_results_);
275 prefetch_data_ = FindAnswersPrefetchData();
276
277 // Raw results are not needed any more.
278 raw_default_history_results_.clear();
279 raw_keyword_history_results_.clear();
280 } else {
281 transformed_default_history_results_.clear();
282 transformed_keyword_history_results_.clear();
283 }
284
[email protected]e1290ee62013-06-26 18:31:15285 StartOrStopSuggestQuery(minimal_changes);
[email protected]344946a12012-12-20 12:03:42286 UpdateMatches();
initial.commit09911bf2008-07-26 23:55:29287}
288
[email protected]ec3f679b2014-08-18 07:45:13289void SearchProvider::Stop(bool clear_cached_results) {
290 StopSuggest();
291 done_ = true;
292
293 if (clear_cached_results)
294 ClearAllResults();
295}
296
[email protected]776ee5902014-08-11 09:15:19297const TemplateURL* SearchProvider::GetTemplateURL(bool is_keyword) const {
298 return is_keyword ? providers_.GetKeywordProviderURL()
299 : providers_.GetDefaultProviderURL();
300}
301
302const AutocompleteInput SearchProvider::GetInput(bool is_keyword) const {
303 return is_keyword ? keyword_input_ : input_;
304}
305
306bool SearchProvider::ShouldAppendExtraParams(
307 const SearchSuggestionParser::SuggestResult& result) const {
308 return !result.from_keyword_provider() ||
309 providers_.default_provider().empty();
310}
311
[email protected]776ee5902014-08-11 09:15:19312void SearchProvider::RecordDeletionResult(bool success) {
313 if (success) {
314 base::RecordAction(
315 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Success"));
316 } else {
317 base::RecordAction(
318 base::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure"));
319 }
320}
321
322void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) {
323 DCHECK(!done_);
324 --suggest_results_pending_;
325 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
326
327 const bool is_keyword = source == keyword_fetcher_.get();
328
329 // Ensure the request succeeded and that the provider used is still available.
330 // A verbatim match cannot be generated without this provider, causing errors.
331 const bool request_succeeded =
332 source->GetStatus().is_success() && (source->GetResponseCode() == 200) &&
333 GetTemplateURL(is_keyword);
334
335 LogFetchComplete(request_succeeded, is_keyword);
336
337 bool results_updated = false;
338 if (request_succeeded) {
339 scoped_ptr<base::Value> data(SearchSuggestionParser::DeserializeJsonData(
340 SearchSuggestionParser::ExtractJsonData(source)));
341 if (data) {
342 SearchSuggestionParser::Results* results =
343 is_keyword ? &keyword_results_ : &default_results_;
344 results_updated = ParseSuggestResults(*data, -1, is_keyword, results);
345 if (results_updated)
346 SortResults(is_keyword, results);
347 }
348 }
349 UpdateMatches();
350 if (done_ || results_updated)
351 listener_->OnProviderUpdate(results_updated);
352}
353
[email protected]ec3f679b2014-08-18 07:45:13354void SearchProvider::StopSuggest() {
355 // Increment the appropriate field in the histogram by the number of
356 // pending requests that were invalidated.
357 for (int i = 0; i < suggest_results_pending_; ++i)
358 LogOmniboxSuggestRequest(REQUEST_INVALIDATED);
359 suggest_results_pending_ = 0;
360 timer_.Stop();
361 // Stop any in-progress URL fetches.
362 keyword_fetcher_.reset();
363 default_fetcher_.reset();
364}
365
366void SearchProvider::ClearAllResults() {
367 keyword_results_.Clear();
368 default_results_.Clear();
369}
370
[email protected]776ee5902014-08-11 09:15:19371void SearchProvider::UpdateMatchContentsClass(
372 const base::string16& input_text,
373 SearchSuggestionParser::Results* results) {
374 for (SearchSuggestionParser::SuggestResults::iterator sug_it =
375 results->suggest_results.begin();
376 sug_it != results->suggest_results.end(); ++sug_it) {
377 sug_it->ClassifyMatchContents(false, input_text);
378 }
Ryo Hashimoto884ad192014-08-28 05:54:30379 const std::string languages(client_->AcceptLanguages());
[email protected]776ee5902014-08-11 09:15:19380 for (SearchSuggestionParser::NavigationResults::iterator nav_it =
381 results->navigation_results.begin();
382 nav_it != results->navigation_results.end(); ++nav_it) {
383 nav_it->CalculateAndClassifyMatchContents(false, input_text, languages);
384 }
385}
386
[email protected]d4a94b92014-03-04 01:35:22387void SearchProvider::SortResults(bool is_keyword,
[email protected]0b9575f2014-07-30 11:58:37388 SearchSuggestionParser::Results* results) {
[email protected]d4a94b92014-03-04 01:35:22389 // Ignore suggested scores for non-keyword matches in keyword mode; if the
390 // server is allowed to score these, it could interfere with the user's
391 // ability to get good keyword results.
392 const bool abandon_suggested_scores =
393 !is_keyword && !providers_.keyword_provider().empty();
[email protected]0b9575f2014-07-30 11:58:37394 // Apply calculated relevance scores to suggestions if valid relevances were
[email protected]d4a94b92014-03-04 01:35:22395 // not provided or we're abandoning suggested scores entirely.
[email protected]2c802d12014-07-31 12:57:14396 if (!results->relevances_from_server || abandon_suggested_scores) {
[email protected]d4a94b92014-03-04 01:35:22397 ApplyCalculatedSuggestRelevance(&results->suggest_results);
398 ApplyCalculatedNavigationRelevance(&results->navigation_results);
399 // If abandoning scores entirely, also abandon the verbatim score.
400 if (abandon_suggested_scores)
401 results->verbatim_relevance = -1;
402 }
403
404 // Keep the result lists sorted.
405 const CompareScoredResults comparator = CompareScoredResults();
406 std::stable_sort(results->suggest_results.begin(),
407 results->suggest_results.end(),
408 comparator);
409 std::stable_sort(results->navigation_results.begin(),
410 results->navigation_results.end(),
411 comparator);
412}
413
[email protected]cfa164bf2014-03-19 11:51:15414void SearchProvider::LogFetchComplete(bool success, bool is_keyword) {
415 LogOmniboxSuggestRequest(REPLY_RECEIVED);
416 // Record response time for suggest requests sent to Google. We care
417 // only about the common case: the Google default provider used in
418 // non-keyword mode.
419 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
420 if (!is_keyword && default_url &&
[email protected]ce7ee5f2014-06-16 23:41:19421 (TemplateURLPrepopulateData::GetEngineType(
422 *default_url,
423 providers_.template_url_service()->search_terms_data()) ==
[email protected]cfa164bf2014-03-19 11:51:15424 SEARCH_ENGINE_GOOGLE)) {
425 const base::TimeDelta elapsed_time =
426 base::TimeTicks::Now() - time_suggest_request_sent_;
427 if (success) {
428 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
429 elapsed_time);
430 } else {
431 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
432 elapsed_time);
433 }
434 }
435}
436
[email protected]cfa164bf2014-03-19 11:51:15437void SearchProvider::UpdateMatches() {
mpearson6c183672014-09-03 02:09:42438 PersistTopSuggestions(&default_results_);
439 PersistTopSuggestions(&keyword_results_);
[email protected]cfa164bf2014-03-19 11:51:15440 ConvertResultsToAutocompleteMatches();
441
442 // Check constraints that may be violated by suggested relevances.
443 if (!matches_.empty() &&
444 (default_results_.HasServerProvidedScores() ||
445 keyword_results_.HasServerProvidedScores())) {
446 // These blocks attempt to repair undesirable behavior by suggested
447 // relevances with minimal impact, preserving other suggested relevances.
448
[email protected]d0e4ad02014-08-22 18:58:33449 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
450 const bool is_extension_keyword = (keyword_url != NULL) &&
451 (keyword_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION);
452 if ((keyword_url != NULL) && !is_extension_keyword &&
[email protected]7bc5e162014-08-15 19:41:11453 (FindTopMatch() == matches_.end())) {
[email protected]d0e4ad02014-08-22 18:58:33454 // In non-extension keyword mode, disregard the keyword verbatim suggested
455 // relevance if necessary, so at least one match is allowed to be default.
456 // (In extension keyword mode this is not necessary because the extension
mpearson6c183672014-09-03 02:09:42457 // will return a default match.) Give keyword verbatim the lowest
458 // non-zero score to best reflect what the server desired.
459 DCHECK_EQ(0, keyword_results_.verbatim_relevance);
460 keyword_results_.verbatim_relevance = 1;
[email protected]cfa164bf2014-03-19 11:51:15461 ConvertResultsToAutocompleteMatches();
462 }
[email protected]89bd27d12014-04-12 17:36:23463 if (IsTopMatchSearchWithURLInput()) {
[email protected]cfa164bf2014-03-19 11:51:15464 // Disregard the suggested search and verbatim relevances if the input
465 // type is URL and the top match is a highly-ranked search suggestion.
466 // For example, prevent a search for "foo.com" from outranking another
467 // provider's navigation for "foo.com" or "foo.com/url_from_history".
468 ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results);
469 ApplyCalculatedSuggestRelevance(&default_results_.suggest_results);
470 default_results_.verbatim_relevance = -1;
471 keyword_results_.verbatim_relevance = -1;
472 ConvertResultsToAutocompleteMatches();
473 }
[email protected]d0e4ad02014-08-22 18:58:33474 if (!is_extension_keyword && (FindTopMatch() == matches_.end())) {
475 // Guarantee that SearchProvider returns a legal default match (except
476 // when in extension-based keyword mode). The omnibox always needs at
477 // least one legal default match, and it relies on SearchProvider in
478 // combination with KeywordProvider (for extension-based keywords) to
mpearson6c183672014-09-03 02:09:42479 // always return one. Give the verbatim suggestion the lowest non-zero
480 // scores to best reflect what the server desired.
481 DCHECK_EQ(0, default_results_.verbatim_relevance);
482 default_results_.verbatim_relevance = 1;
483 // We do not have to alter keyword_results_.verbatim_relevance here.
484 // If the user is in keyword mode, we already reverted (earlier in this
485 // function) the instructions to suppress keyword verbatim.
[email protected]cfa164bf2014-03-19 11:51:15486 ConvertResultsToAutocompleteMatches();
487 }
[email protected]89bd27d12014-04-12 17:36:23488 DCHECK(!IsTopMatchSearchWithURLInput());
[email protected]d0e4ad02014-08-22 18:58:33489 DCHECK(is_extension_keyword || (FindTopMatch() != matches_.end()));
[email protected]cfa164bf2014-03-19 11:51:15490 }
491 UMA_HISTOGRAM_CUSTOM_COUNTS(
492 "Omnibox.SearchProviderMatches", matches_.size(), 1, 6, 7);
mpearson6c183672014-09-03 02:09:42493
494 // Record the top suggestion (if any) for future use.
495 top_query_suggestion_match_contents_ = base::string16();
496 top_navigation_suggestion_ = GURL();
497 ACMatches::const_iterator first_match = FindTopMatch();
498 if ((first_match != matches_.end()) &&
499 !first_match->inline_autocompletion.empty()) {
500 // Identify if this match came from a query suggestion or a navsuggestion.
501 // In either case, extracts the identifying feature of the suggestion
502 // (query string or navigation url).
503 if (AutocompleteMatch::IsSearchType(first_match->type))
504 top_query_suggestion_match_contents_ = first_match->contents;
505 else
506 top_navigation_suggestion_ = first_match->destination_url;
507 }
508
[email protected]cfa164bf2014-03-19 11:51:15509 UpdateDone();
[email protected]cfa164bf2014-03-19 11:51:15510}
511
[email protected]bc8bb0cd2013-06-24 21:50:23512void SearchProvider::Run() {
513 // Start a new request with the current input.
514 suggest_results_pending_ = 0;
515 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]abe441e2013-05-06 12:35:05516
[email protected]bc8bb0cd2013-06-24 21:50:23517 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
518 providers_.GetDefaultProviderURL(), input_));
519 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
520 providers_.GetKeywordProviderURL(), keyword_input_));
521
522 // Both the above can fail if the providers have been modified or deleted
523 // since the query began.
524 if (suggest_results_pending_ == 0) {
525 UpdateDone();
526 // We only need to update the listener if we're actually done.
527 if (done_)
528 listener_->OnProviderUpdate(false);
529 }
[email protected]601858c02010-09-01 17:08:20530}
531
[email protected]8d457132010-11-04 18:13:40532void SearchProvider::DoHistoryQuery(bool minimal_changes) {
533 // The history query results are synchronous, so if minimal_changes is true,
534 // we still have the last results and don't need to do anything.
535 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29536 return;
537
grobye5fcee42014-09-26 03:36:46538 raw_keyword_history_results_.clear();
539 raw_default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29540
[email protected]78e5e432013-08-03 02:10:10541 if (OmniboxFieldTrial::SearchHistoryDisable(
542 input_.current_page_classification()))
[email protected]d8cd76b2013-07-10 09:46:16543 return;
544
Ryo Hashimoto884ad192014-08-28 05:54:30545 history::URLDatabase* url_db = client_->InMemoryDatabase();
[email protected]8d457132010-11-04 18:13:40546 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29547 return;
548
[email protected]51124552011-07-16 01:37:10549 // Request history for both the keyword and default provider. We grab many
550 // more matches than we'll ultimately clamp to so that if there are several
551 // recent multi-word matches who scores are lowered (see
grobye5fcee42014-09-26 03:36:46552 // ScoreHistoryResults()), they won't crowd out older, higher-scoring
[email protected]51124552011-07-16 01:37:10553 // matches. Note that this doesn't fix the problem entirely, but merely
554 // limits it to cases with a very large number of such multi-word matches; for
555 // now, this seems OK compared with the complexity of a real fix, which would
556 // require multiple searches and tracking of "single- vs. multi-word" in the
557 // database.
558 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47559 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
560 if (default_url) {
[email protected]b4bec972014-04-05 18:07:15561 const base::TimeTicks start_time = base::TimeTicks::Now();
grobye5fcee42014-09-26 03:36:46562 url_db->GetMostRecentKeywordSearchTerms(default_url->id(),
563 input_.text(),
564 num_matches,
565 &raw_default_history_results_);
[email protected]31afdf72013-09-26 04:29:36566 UMA_HISTOGRAM_TIMES(
567 "Omnibox.SearchProvider.GetMostRecentKeywordTermsDefaultProviderTime",
568 base::TimeTicks::Now() - start_time);
[email protected]257ab712009-04-14 17:16:24569 }
[email protected]85b8d6f2012-05-08 20:53:47570 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
571 if (keyword_url) {
572 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
grobye5fcee42014-09-26 03:36:46573 keyword_input_.text(),
574 num_matches,
575 &raw_keyword_history_results_);
[email protected]3954c3a2012-04-10 20:17:55576 }
initial.commit09911bf2008-07-26 23:55:29577}
578
bartn1c07e722014-10-27 19:34:24579base::TimeDelta SearchProvider::GetSuggestQueryDelay() const {
580 bool from_last_keystroke;
581 int polling_delay_ms;
582 OmniboxFieldTrial::GetSuggestPollingStrategy(&from_last_keystroke,
583 &polling_delay_ms);
584
585 base::TimeDelta delay(base::TimeDelta::FromMilliseconds(polling_delay_ms));
586 if (from_last_keystroke)
587 return delay;
588
589 base::TimeDelta time_since_last_suggest_request =
590 base::TimeTicks::Now() - time_suggest_request_sent_;
591 return std::max(base::TimeDelta(), delay - time_since_last_suggest_request);
592}
593
[email protected]6dc950f2012-07-16 19:49:08594void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]83c726482008-09-10 06:36:34595 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29596 StopSuggest();
[email protected]71b46152013-05-03 16:39:20597 ClearAllResults();
initial.commit09911bf2008-07-26 23:55:29598 return;
599 }
600
bartn1c07e722014-10-27 19:34:24601 if (OmniboxFieldTrial::DisableResultsCaching())
602 ClearAllResults();
603
initial.commit09911bf2008-07-26 23:55:29604 // For the minimal_changes case, if we finished the previous query and still
605 // have its results, or are allowed to keep running it, just do that, rather
606 // than starting a new query.
607 if (minimal_changes &&
[email protected]cc1526e2013-05-17 04:04:24608 (!default_results_.suggest_results.empty() ||
609 !default_results_.navigation_results.empty() ||
610 !keyword_results_.suggest_results.empty() ||
611 !keyword_results_.navigation_results.empty() ||
[email protected]a2770a7d2014-04-22 19:33:35612 (!done_ && input_.want_asynchronous_matches())))
initial.commit09911bf2008-07-26 23:55:29613 return;
614
615 // We can't keep running any previous query, so halt it.
616 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42617
mpearson6c183672014-09-03 02:09:42618 UpdateAllOldResults(minimal_changes);
initial.commit09911bf2008-07-26 23:55:29619
[email protected]ee6110b2014-01-09 22:26:31620 // Update the content classifications of remaining results so they look good
621 // against the current input.
[email protected]23db6492014-01-16 02:35:30622 UpdateMatchContentsClass(input_.text(), &default_results_);
623 if (!keyword_input_.text().empty())
624 UpdateMatchContentsClass(keyword_input_.text(), &keyword_results_);
[email protected]ee6110b2014-01-09 22:26:31625
initial.commit09911bf2008-07-26 23:55:29626 // We can't start a new query if we're only allowed synchronous results.
[email protected]a2770a7d2014-04-22 19:33:35627 if (!input_.want_asynchronous_matches())
initial.commit09911bf2008-07-26 23:55:29628 return;
629
bartn1c07e722014-10-27 19:34:24630 // Kick off a timer that will start the URL fetch if it completes before
631 // the user types another character. Requests may be delayed to avoid
632 // flooding the server with requests that are likely to be thrown away later
633 // anyway.
634 const base::TimeDelta delay = GetSuggestQueryDelay();
635 if (delay <= base::TimeDelta()) {
[email protected]515ffa942012-11-27 20:18:24636 Run();
637 return;
638 }
bartn1c07e722014-10-27 19:34:24639 timer_.Start(FROM_HERE, delay, this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29640}
641
[email protected]83c726482008-09-10 06:36:34642bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55643 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
644 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47645 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
646 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
Ryo Hashimoto884ad192014-08-28 05:54:30647 if (client_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47648 ((!default_url || default_url->suggestions_url().empty()) &&
649 (!keyword_url || keyword_url->suggestions_url().empty())) ||
Ryo Hashimoto884ad192014-08-28 05:54:30650 !client_->SearchSuggestEnabled())
[email protected]83c726482008-09-10 06:36:34651 return false;
652
[email protected]cac59d32010-08-09 23:23:14653 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34654 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34655
[email protected]cac59d32010-08-09 23:23:14656 // FORCED_QUERY means the user is explicitly asking us to search for this, so
657 // we assume it isn't a URL and/or there isn't private data.
[email protected]3dc75b12014-06-08 00:02:22658 if (input_.type() == metrics::OmniboxInputType::FORCED_QUERY)
[email protected]cac59d32010-08-09 23:23:14659 return true;
[email protected]83c726482008-09-10 06:36:34660
[email protected]f608ea102013-03-18 15:08:09661 // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't
662 // http/https/ftp, we shouldn't send it. Sending things like file: and data:
663 // is both a waste of time and a disclosure of potentially private, local
664 // data. Other "schemes" may actually be usernames, and we don't want to send
665 // passwords. If the scheme is OK, we still need to check other cases below.
666 // If this is QUERY, then the presence of these schemes means the user
667 // explicitly typed one, and thus this is probably a URL that's being entered
668 // and happens to currently be invalid -- in which case we again want to run
669 // our checks below. Other QUERY cases are less likely to be URLs and thus we
670 // assume we're OK.
[email protected]df807042014-08-13 16:48:41671 if (!LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) &&
672 !LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
673 !LowerCaseEqualsASCII(input_.scheme(), url::kFtpScheme))
[email protected]3dc75b12014-06-08 00:02:22674 return (input_.type() == metrics::OmniboxInputType::QUERY);
[email protected]cac59d32010-08-09 23:23:14675
676 // Don't send URLs with usernames, queries or refs. Some of these are
677 // private, and the Suggest server is unlikely to have any useful results
678 // for any of them. Also don't send URLs with ports, as we may initially
679 // think that a username + password is a host + port (and we don't want to
680 // send usernames/passwords), and even if the port really is a port, the
681 // server is once again unlikely to have and useful results.
[email protected]825e16f2013-09-30 23:52:58682 // Note that we only block based on refs if the input is URL-typed, as search
683 // queries can legitimately have #s in them which the URL parser
684 // overaggressively categorizes as a url with a ref.
[email protected]b45334502014-04-30 19:44:05685 const url::Parsed& parts = input_.parts();
[email protected]cac59d32010-08-09 23:23:14686 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
[email protected]825e16f2013-09-30 23:52:58687 parts.query.is_nonempty() ||
[email protected]3dc75b12014-06-08 00:02:22688 (parts.ref.is_nonempty() &&
689 (input_.type() == metrics::OmniboxInputType::URL)))
[email protected]cac59d32010-08-09 23:23:14690 return false;
691
692 // Don't send anything for https except the hostname. Hostnames are OK
693 // because they are visible when the TCP connection is established, but the
694 // specific path may reveal private information.
[email protected]df807042014-08-13 16:48:41695 if (LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) &&
[email protected]a2fedb1e2011-01-25 15:23:36696 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14697 return false;
[email protected]83c726482008-09-10 06:36:34698
699 return true;
700}
701
mpearson6c183672014-09-03 02:09:42702void SearchProvider::UpdateAllOldResults(bool minimal_changes) {
[email protected]dc735c02013-11-12 23:23:41703 if (keyword_input_.text().empty()) {
[email protected]1e1550e2013-05-02 17:37:51704 // User is either in keyword mode with a blank input or out of
705 // keyword mode entirely.
[email protected]cc1526e2013-05-17 04:04:24706 keyword_results_.Clear();
[email protected]1e1550e2013-05-02 17:37:51707 }
mpearson6c183672014-09-03 02:09:42708 UpdateOldResults(minimal_changes, &default_results_);
709 UpdateOldResults(minimal_changes, &keyword_results_);
[email protected]d1f0a7f2012-06-05 10:26:42710}
711
mpearson6c183672014-09-03 02:09:42712void SearchProvider::PersistTopSuggestions(
713 SearchSuggestionParser::Results* results) {
714 // Mark any results matching the current top results as having been received
715 // prior to the last keystroke. That prevents asynchronous updates from
716 // clobbering top results, which may be used for inline autocompletion.
717 // Other results don't need similar changes, because they shouldn't be
718 // displayed asynchronously anyway.
719 if (!top_query_suggestion_match_contents_.empty()) {
720 for (SearchSuggestionParser::SuggestResults::iterator sug_it =
721 results->suggest_results.begin();
722 sug_it != results->suggest_results.end(); ++sug_it) {
723 if (sug_it->match_contents() == top_query_suggestion_match_contents_)
724 sug_it->set_received_after_last_keystroke(false);
725 }
726 }
727 if (top_navigation_suggestion_.is_valid()) {
728 for (SearchSuggestionParser::NavigationResults::iterator nav_it =
729 results->navigation_results.begin();
730 nav_it != results->navigation_results.end(); ++nav_it) {
731 if (nav_it->url() == top_navigation_suggestion_)
732 nav_it->set_received_after_last_keystroke(false);
733 }
734 }
[email protected]d1f0a7f2012-06-05 10:26:42735}
736
[email protected]0b9575f2014-07-30 11:58:37737void SearchProvider::ApplyCalculatedSuggestRelevance(
738 SearchSuggestionParser::SuggestResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42739 for (size_t i = 0; i < list->size(); ++i) {
[email protected]0b9575f2014-07-30 11:58:37740 SearchSuggestionParser::SuggestResult& result = (*list)[i];
[email protected]188b50c2013-03-28 07:19:42741 result.set_relevance(
742 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
743 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07744 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42745 }
746}
747
[email protected]188b50c2013-03-28 07:19:42748void SearchProvider::ApplyCalculatedNavigationRelevance(
[email protected]0b9575f2014-07-30 11:58:37749 SearchSuggestionParser::NavigationResults* list) {
[email protected]d1f0a7f2012-06-05 10:26:42750 for (size_t i = 0; i < list->size(); ++i) {
[email protected]0b9575f2014-07-30 11:58:37751 SearchSuggestionParser::NavigationResult& result = (*list)[i];
[email protected]188b50c2013-03-28 07:19:42752 result.set_relevance(
753 result.CalculateRelevance(input_, providers_.has_keyword_provider()) +
754 (list->size() - i - 1));
[email protected]d30268a2013-06-25 22:31:07755 result.set_relevance_from_server(false);
[email protected]d1f0a7f2012-06-05 10:26:42756 }
757}
758
[email protected]15fb2aa2012-05-22 22:52:59759net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12760 int id,
[email protected]9ff91722012-09-07 05:29:12761 const TemplateURL* template_url,
[email protected]14710852013-02-05 23:45:41762 const AutocompleteInput& input) {
[email protected]9ff91722012-09-07 05:29:12763 if (!template_url || template_url->suggestions_url().empty())
764 return NULL;
765
766 // Bail if the suggestion URL is invalid with the given replacements.
[email protected]14710852013-02-05 23:45:41767 TemplateURLRef::SearchTermsArgs search_term_args(input.text());
[email protected]420472b22014-06-10 13:34:43768 search_term_args.input_type = input.type();
[email protected]14710852013-02-05 23:45:41769 search_term_args.cursor_position = input.cursor_position();
[email protected]d5015ca2013-08-08 22:04:18770 search_term_args.page_classification = input.current_page_classification();
[email protected]2ef2a6642014-07-30 05:50:29771 if (OmniboxFieldTrial::EnableAnswersInSuggest()) {
[email protected]20184242014-05-14 02:57:42772 search_term_args.session_token = GetSessionToken();
[email protected]2ef2a6642014-07-30 05:50:29773 if (!prefetch_data_.full_query_text.empty()) {
774 search_term_args.prefetch_query =
[email protected]ebbac63e2014-08-22 01:43:06775 base::UTF16ToUTF8(prefetch_data_.full_query_text);
[email protected]2ef2a6642014-07-30 05:50:29776 search_term_args.prefetch_query_type =
[email protected]ebbac63e2014-08-22 01:43:06777 base::UTF16ToUTF8(prefetch_data_.query_type);
[email protected]2ef2a6642014-07-30 05:50:29778 }
779 }
[email protected]9ff91722012-09-07 05:29:12780 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19781 search_term_args,
782 providers_.template_url_service()->search_terms_data()));
[email protected]9ff91722012-09-07 05:29:12783 if (!suggest_url.is_valid())
784 return NULL;
[email protected]9b9fa672013-11-07 06:04:52785 // Send the current page URL if user setting and URL requirements are met and
786 // the user is in the field trial.
787 if (CanSendURL(current_page_url_, suggest_url, template_url,
[email protected]e6477f12014-08-05 07:59:54788 input.current_page_classification(),
Ryo Hashimoto884ad192014-08-28 05:54:30789 template_url_service_->search_terms_data(), client_.get()) &&
[email protected]9b9fa672013-11-07 06:04:52790 OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) {
791 search_term_args.current_page_url = current_page_url_.spec();
792 // Create the suggest URL again with the current page URL.
793 suggest_url = GURL(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19794 search_term_args,
795 providers_.template_url_service()->search_terms_data()));
[email protected]9b9fa672013-11-07 06:04:52796 }
[email protected]9ff91722012-09-07 05:29:12797
798 suggest_results_pending_++;
799 LogOmniboxSuggestRequest(REQUEST_SENT);
800
801 net::URLFetcher* fetcher =
802 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this);
Ryo Hashimoto884ad192014-08-28 05:54:30803 fetcher->SetRequestContext(client_->RequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34804 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]bd3b4712012-12-18 17:01:30805 // Add Chrome experiment state to the request headers.
806 net::HttpRequestHeaders headers;
[email protected]71011c1682014-07-09 17:19:16807 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
Ryo Hashimoto884ad192014-08-28 05:54:30808 fetcher->GetOriginalURL(), client_->IsOffTheRecord(), false, &headers);
[email protected]bd3b4712012-12-18 17:01:30809 fetcher->SetExtraRequestHeaders(headers.ToString());
[email protected]257ab712009-04-14 17:16:24810 fetcher->Start();
811 return fetcher;
812}
813
[email protected]344946a12012-12-20 12:03:42814void SearchProvider::ConvertResultsToAutocompleteMatches() {
initial.commit09911bf2008-07-26 23:55:29815 // Convert all the results to matches and add them to a map, so we can keep
816 // the most relevant match for each result.
[email protected]31afdf72013-09-26 04:29:36817 base::TimeTicks start_time(base::TimeTicks::Now());
initial.commit09911bf2008-07-26 23:55:29818 MatchMap map;
[email protected]bc8bb0cd2013-06-24 21:50:23819 const base::Time no_time;
[email protected]cc1526e2013-05-17 04:04:24820 int did_not_accept_keyword_suggestion =
821 keyword_results_.suggest_results.empty() ?
initial.commit09911bf2008-07-26 23:55:29822 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
823 TemplateURLRef::NO_SUGGESTION_CHOSEN;
initial.commit09911bf2008-07-26 23:55:29824
[email protected]d30268a2013-06-25 22:31:07825 bool relevance_from_server;
826 int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server);
[email protected]cc1526e2013-05-17 04:04:24827 int did_not_accept_default_suggestion =
828 default_results_.suggest_results.empty() ?
[email protected]55ce8f12012-05-09 04:44:08829 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
830 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]7bc5e162014-08-15 19:41:11831 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]d1f0a7f2012-06-05 10:26:42832 if (verbatim_relevance > 0) {
[email protected]c2ca3fd2014-03-22 03:07:44833 const base::string16& trimmed_verbatim =
834 base::CollapseWhitespace(input_.text(), false);
[email protected]716cd372014-08-15 18:56:03835
836 // Verbatim results don't get suggestions and hence, answers.
837 // Scan previous matches if the last answer-bearing suggestion matches
838 // verbatim, and if so, copy over answer contents.
839 base::string16 answer_contents;
840 base::string16 answer_type;
841 for (ACMatches::iterator it = matches_.begin(); it != matches_.end();
842 ++it) {
843 if (!it->answer_contents.empty() &&
844 it->fill_into_edit == trimmed_verbatim) {
845 answer_contents = it->answer_contents;
846 answer_type = it->answer_type;
847 break;
848 }
849 }
850
[email protected]0b9575f2014-07-30 11:58:37851 SearchSuggestionParser::SuggestResult verbatim(
[email protected]c2ca3fd2014-03-22 03:07:44852 trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
[email protected]716cd372014-08-15 18:56:03853 trimmed_verbatim, base::string16(), base::string16(), answer_contents,
854 answer_type, std::string(), std::string(), false, verbatim_relevance,
855 relevance_from_server, false, trimmed_verbatim);
[email protected]57482a72014-03-14 22:27:37856 AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion,
[email protected]7bc5e162014-08-15 19:41:11857 false, keyword_url != NULL, &map);
[email protected]d1f0a7f2012-06-05 10:26:42858 }
[email protected]5423e562013-02-07 03:58:45859 if (!keyword_input_.text().empty()) {
[email protected]5423e562013-02-07 03:58:45860 // We only create the verbatim search query match for a keyword
861 // if it's not an extension keyword. Extension keywords are handled
862 // in KeywordProvider::Start(). (Extensions are complicated...)
863 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond
864 // to the keyword verbatim search query. Do not create other matches
865 // of type SEARCH_OTHER_ENGINE.
[email protected]bdcbcd82013-10-28 13:40:25866 if (keyword_url &&
867 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) {
[email protected]d30268a2013-06-25 22:31:07868 bool keyword_relevance_from_server;
869 const int keyword_verbatim_relevance =
870 GetKeywordVerbatimRelevance(&keyword_relevance_from_server);
[email protected]dab8d52d2013-03-05 07:35:28871 if (keyword_verbatim_relevance > 0) {
[email protected]c2ca3fd2014-03-22 03:07:44872 const base::string16& trimmed_verbatim =
873 base::CollapseWhitespace(keyword_input_.text(), false);
[email protected]0b9575f2014-07-30 11:58:37874 SearchSuggestionParser::SuggestResult verbatim(
[email protected]c2ca3fd2014-03-22 03:07:44875 trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE,
876 trimmed_verbatim, base::string16(), base::string16(),
[email protected]7ec287e2014-05-16 02:50:08877 base::string16(), base::string16(), std::string(), std::string(),
878 true, keyword_verbatim_relevance, keyword_relevance_from_server,
879 false, trimmed_verbatim);
[email protected]57482a72014-03-14 22:27:37880 AddMatchToMap(verbatim, std::string(),
[email protected]7bc5e162014-08-15 19:41:11881 did_not_accept_keyword_suggestion, false, true, &map);
[email protected]dab8d52d2013-03-05 07:35:28882 }
[email protected]5423e562013-02-07 03:58:45883 }
884 }
grobye5fcee42014-09-26 03:36:46885 AddRawHistoryResultsToMap(true, did_not_accept_keyword_suggestion, &map);
886 AddRawHistoryResultsToMap(false, did_not_accept_default_suggestion, &map);
[email protected]257ab712009-04-14 17:16:24887
[email protected]d1cb6a822013-09-18 19:43:00888 AddSuggestResultsToMap(keyword_results_.suggest_results,
889 keyword_results_.metadata, &map);
[email protected]987fad782013-08-28 06:23:18890 AddSuggestResultsToMap(default_results_.suggest_results,
891 default_results_.metadata, &map);
initial.commit09911bf2008-07-26 23:55:29892
[email protected]d30268a2013-06-25 22:31:07893 ACMatches matches;
initial.commit09911bf2008-07-26 23:55:29894 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
[email protected]d30268a2013-06-25 22:31:07895 matches.push_back(i->second);
initial.commit09911bf2008-07-26 23:55:29896
[email protected]d30268a2013-06-25 22:31:07897 AddNavigationResultsToMatches(keyword_results_.navigation_results, &matches);
898 AddNavigationResultsToMatches(default_results_.navigation_results, &matches);
initial.commit09911bf2008-07-26 23:55:29899
[email protected]d30268a2013-06-25 22:31:07900 // Now add the most relevant matches to |matches_|. We take up to kMaxMatches
mpearson6c183672014-09-03 02:09:42901 // suggest/navsuggest matches, regardless of origin. We always include in
902 // that set a legal default match if possible. If Instant Extended is enabled
903 // and we have server-provided (and thus hopefully more accurate) scores for
904 // some suggestions, we allow more of those, until we reach
[email protected]d30268a2013-06-25 22:31:07905 // AutocompleteResult::kMaxMatches total matches (that is, enough to fill the
906 // whole popup).
907 //
908 // We will always return any verbatim matches, no matter how we obtained their
909 // scores, unless we have already accepted AutocompleteResult::kMaxMatches
910 // higher-scoring matches under the conditions above.
911 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant);
jdonnelly55f66142014-09-25 23:08:11912
mpearson6c183672014-09-03 02:09:42913 // Guarantee that if there's a legal default match anywhere in the result
914 // set that it'll get returned. The rotate() call does this by moving the
915 // default match to the front of the list.
916 ACMatches::iterator default_match = FindTopMatch(&matches);
917 if (default_match != matches.end())
918 std::rotate(matches.begin(), default_match, default_match + 1);
[email protected]3723e6e2012-06-11 21:06:56919
jdonnelly55f66142014-09-25 23:08:11920 // It's possible to get a copy of an answer from previous matches and get the
921 // same or a different answer to another server-provided suggestion. In the
922 // future we may decide that we want to have answers attached to multiple
923 // suggestions, but the current assumption is that there should only ever be
924 // one suggestion with an answer. To maintain this assumption, remove any
925 // answers after the first.
926 RemoveExtraAnswers(&matches);
927
928 matches_.clear();
[email protected]d30268a2013-06-25 22:31:07929 size_t num_suggestions = 0;
930 for (ACMatches::const_iterator i(matches.begin());
931 (i != matches.end()) &&
932 (matches_.size() < AutocompleteResult::kMaxMatches);
933 ++i) {
934 // SEARCH_OTHER_ENGINE is only used in the SearchProvider for the keyword
935 // verbatim result, so this condition basically means "if this match is a
936 // suggestion of some sort".
937 if ((i->type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED) &&
938 (i->type != AutocompleteMatchType::SEARCH_OTHER_ENGINE)) {
939 // If we've already hit the limit on non-server-scored suggestions, and
940 // this isn't a server-scored suggestion we can add, skip it.
941 if ((num_suggestions >= kMaxMatches) &&
942 (!chrome::IsInstantExtendedAPIEnabled() ||
943 (i->GetAdditionalInfo(kRelevanceFromServerKey) != kTrue))) {
944 continue;
945 }
946
947 ++num_suggestions;
948 }
949
950 matches_.push_back(*i);
951 }
[email protected]31afdf72013-09-26 04:29:36952 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime",
953 base::TimeTicks::Now() - start_time);
[email protected]344946a12012-12-20 12:03:42954}
955
jdonnelly55f66142014-09-25 23:08:11956void SearchProvider::RemoveExtraAnswers(ACMatches* matches) {
957 bool answer_seen = false;
958 for (ACMatches::iterator it = matches->begin(); it != matches->end(); ++it) {
959 if (!it->answer_contents.empty()) {
960 if (!answer_seen) {
961 answer_seen = true;
962 } else {
963 it->answer_contents.clear();
964 it->answer_type.clear();
965 }
966 }
967 }
968}
969
[email protected]89bd27d12014-04-12 17:36:23970ACMatches::const_iterator SearchProvider::FindTopMatch() const {
[email protected]0a8718b12013-11-13 18:41:31971 ACMatches::const_iterator it = matches_.begin();
972 while ((it != matches_.end()) && !it->allowed_to_be_default_match)
973 ++it;
974 return it;
[email protected]9dfb4d362013-04-05 02:15:12975}
976
[email protected]89bd27d12014-04-12 17:36:23977bool SearchProvider::IsTopMatchSearchWithURLInput() const {
978 ACMatches::const_iterator first_match = FindTopMatch();
[email protected]3dc75b12014-06-08 00:02:22979 return (input_.type() == metrics::OmniboxInputType::URL) &&
[email protected]0a8718b12013-11-13 18:41:31980 (first_match != matches_.end()) &&
981 (first_match->relevance > CalculateRelevanceForVerbatim()) &&
[email protected]78981d8c2014-05-09 15:05:47982 (first_match->type != AutocompleteMatchType::NAVSUGGEST) &&
983 (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED);
[email protected]344946a12012-12-20 12:03:42984}
985
[email protected]257ab712009-04-14 17:16:24986void SearchProvider::AddNavigationResultsToMatches(
[email protected]0b9575f2014-07-30 11:58:37987 const SearchSuggestionParser::NavigationResults& navigation_results,
[email protected]d30268a2013-06-25 22:31:07988 ACMatches* matches) {
[email protected]0b9575f2014-07-30 11:58:37989 for (SearchSuggestionParser::NavigationResults::const_iterator it =
990 navigation_results.begin(); it != navigation_results.end(); ++it) {
[email protected]d30268a2013-06-25 22:31:07991 matches->push_back(NavigationToMatch(*it));
[email protected]bc8bb0cd2013-06-24 21:50:23992 // In the absence of suggested relevance scores, use only the single
993 // highest-scoring result. (The results are already sorted by relevance.)
[email protected]d30268a2013-06-25 22:31:07994 if (!it->relevance_from_server())
[email protected]bc8bb0cd2013-06-24 21:50:23995 return;
[email protected]257ab712009-04-14 17:16:24996 }
997}
998
grobye5fcee42014-09-26 03:36:46999void SearchProvider::AddRawHistoryResultsToMap(bool is_keyword,
1000 int did_not_accept_suggestion,
1001 MatchMap* map) {
1002 const HistoryResults& raw_results =
1003 is_keyword ? raw_keyword_history_results_ : raw_default_history_results_;
1004 if (!OmniboxFieldTrial::EnableAnswersInSuggest() && raw_results.empty())
[email protected]51124552011-07-16 01:37:101005 return;
1006
[email protected]31afdf72013-09-26 04:29:361007 base::TimeTicks start_time(base::TimeTicks::Now());
[email protected]51124552011-07-16 01:37:101008
grobye5fcee42014-09-26 03:36:461009 // Until Answers becomes default, scoring of history results will still happen
1010 // here for non-Answers Chrome, to prevent scoring performance regressions
1011 // resulting from moving the scoring code before the suggest request is sent.
1012 // For users with Answers enabled, the history results have already been
1013 // scored earlier, right after calling DoHistoryQuery().
1014 SearchSuggestionParser::SuggestResults local_transformed_results;
1015 const SearchSuggestionParser::SuggestResults* transformed_results = NULL;
1016 if (!OmniboxFieldTrial::EnableAnswersInSuggest()) {
1017 ScoreHistoryResults(raw_results, is_keyword, &local_transformed_results);
1018 transformed_results = &local_transformed_results;
1019 } else {
1020 transformed_results = is_keyword ? &transformed_keyword_history_results_
1021 : &transformed_default_history_results_;
[email protected]51124552011-07-16 01:37:101022 }
grobye5fcee42014-09-26 03:36:461023 DCHECK(transformed_results);
1024 AddTransformedHistoryResultsToMap(
1025 *transformed_results, did_not_accept_suggestion, map);
[email protected]31afdf72013-09-26 04:29:361026 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime",
1027 base::TimeTicks::Now() - start_time);
[email protected]51124552011-07-16 01:37:101028}
1029
grobye5fcee42014-09-26 03:36:461030void SearchProvider::AddTransformedHistoryResultsToMap(
1031 const SearchSuggestionParser::SuggestResults& transformed_results,
1032 int did_not_accept_suggestion,
1033 MatchMap* map) {
1034 for (SearchSuggestionParser::SuggestResults::const_iterator i(
1035 transformed_results.begin());
1036 i != transformed_results.end();
1037 ++i) {
1038 AddMatchToMap(*i, std::string(), did_not_accept_suggestion, true,
1039 providers_.GetKeywordProviderURL() != NULL, map);
1040 }
1041}
1042
1043SearchSuggestionParser::SuggestResults
1044SearchProvider::ScoreHistoryResultsHelper(const HistoryResults& results,
1045 bool base_prevent_inline_autocomplete,
1046 bool input_multiple_words,
1047 const base::string16& input_text,
1048 bool is_keyword) {
[email protected]0b9575f2014-07-30 11:58:371049 SearchSuggestionParser::SuggestResults scored_results;
[email protected]ab5fd2f2014-07-17 19:18:521050 // True if the user has asked this exact query previously.
1051 bool found_what_you_typed_match = false;
[email protected]78e5e432013-08-03 02:10:101052 const bool prevent_search_history_inlining =
1053 OmniboxFieldTrial::SearchHistoryPreventInlining(
1054 input_.current_page_classification());
[email protected]c2ca3fd2014-03-22 03:07:441055 const base::string16& trimmed_input =
1056 base::CollapseWhitespace(input_text, false);
[email protected]257ab712009-04-14 17:16:241057 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
1058 ++i) {
[email protected]c2ca3fd2014-03-22 03:07:441059 const base::string16& trimmed_suggestion =
1060 base::CollapseWhitespace(i->term, false);
1061
[email protected]51124552011-07-16 01:37:101062 // Don't autocomplete multi-word queries that have only been seen once
1063 // unless the user has typed more than one word.
1064 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
[email protected]c2ca3fd2014-03-22 03:07:441065 (!input_multiple_words && (i->visits < 2) &&
1066 HasMultipleWords(trimmed_suggestion));
[email protected]51124552011-07-16 01:37:101067
[email protected]78e5e432013-08-03 02:10:101068 int relevance = CalculateRelevanceForHistory(
1069 i->time, is_keyword, !prevent_inline_autocomplete,
1070 prevent_search_history_inlining);
[email protected]ab5fd2f2014-07-17 19:18:521071 // Add the match to |scored_results| by putting the what-you-typed match
1072 // on the front and appending all other matches. We want the what-you-
1073 // typed match to always be first.
[email protected]0b9575f2014-07-30 11:58:371074 SearchSuggestionParser::SuggestResults::iterator insertion_position =
1075 scored_results.end();
[email protected]ab5fd2f2014-07-17 19:18:521076 if (trimmed_suggestion == trimmed_input) {
1077 found_what_you_typed_match = true;
1078 insertion_position = scored_results.begin();
1079 }
mpearson6c183672014-09-03 02:09:421080 SearchSuggestionParser::SuggestResult history_suggestion(
1081 trimmed_suggestion, AutocompleteMatchType::SEARCH_HISTORY,
1082 trimmed_suggestion, base::string16(), base::string16(),
1083 base::string16(), base::string16(), std::string(), std::string(),
1084 is_keyword, relevance, false, false, trimmed_input);
1085 // History results are synchronous; they are received on the last keystroke.
1086 history_suggestion.set_received_after_last_keystroke(false);
1087 scored_results.insert(insertion_position, history_suggestion);
[email protected]257ab712009-04-14 17:16:241088 }
[email protected]51124552011-07-16 01:37:101089
1090 // History returns results sorted for us. However, we may have docked some
[email protected]ab5fd2f2014-07-17 19:18:521091 // results' scores, so things are no longer in order. While keeping the
1092 // what-you-typed match at the front (if it exists), do a stable sort to get
[email protected]51124552011-07-16 01:37:101093 // things back in order without otherwise disturbing results with equal
1094 // scores, then force the scores to be unique, so that the order in which
1095 // they're shown is deterministic.
[email protected]ab5fd2f2014-07-17 19:18:521096 std::stable_sort(scored_results.begin() +
1097 (found_what_you_typed_match ? 1 : 0),
1098 scored_results.end(),
[email protected]55ce8f12012-05-09 04:44:081099 CompareScoredResults());
[email protected]7e3b77f2014-07-25 02:29:441100
1101 // Don't autocomplete to search terms that would normally be treated as URLs
1102 // when typed. For example, if the user searched for "google.com" and types
1103 // "goog", don't autocomplete to the search term "google.com". Otherwise,
1104 // the input will look like a URL but act like a search, which is confusing.
1105 // The 1200 relevance score threshold in the test below is the lowest
1106 // possible score in CalculateRelevanceForHistory()'s aggressive-scoring
1107 // curve. This is an appropriate threshold to use to decide if we're overly
1108 // aggressively inlining because, if we decide the answer is yes, the
1109 // way we resolve it it to not use the aggressive-scoring curve.
1110 // NOTE: We don't check for autocompleting to URLs in the following cases:
1111 // * When inline autocomplete is disabled, we won't be inline autocompleting
1112 // this term, so we don't need to worry about confusion as much. This
1113 // also prevents calling Classify() again from inside the classifier
1114 // (which will corrupt state and likely crash), since the classifier
1115 // always disables inline autocomplete.
1116 // * When the user has typed the whole string before as a query, then it's
1117 // likely the user has no expectation that term should be interpreted as
1118 // as a URL, so we need not do anything special to preserve user
1119 // expectation.
[email protected]51124552011-07-16 01:37:101120 int last_relevance = 0;
[email protected]7e3b77f2014-07-25 02:29:441121 if (!base_prevent_inline_autocomplete && !found_what_you_typed_match &&
hashimoto663b9f42014-08-26 04:29:201122 scored_results.front().relevance() >= 1200) {
[email protected]7e3b77f2014-07-25 02:29:441123 AutocompleteMatch match;
Ryo Hashimoto884ad192014-08-28 05:54:301124 client_->Classify(scored_results.front().suggestion(), false, false,
1125 input_.current_page_classification(), &match, NULL);
[email protected]7e3b77f2014-07-25 02:29:441126 // Demote this match that would normally be interpreted as a URL to have
1127 // the highest score a previously-issued search query could have when
1128 // scoring with the non-aggressive method. A consequence of demoting
1129 // by revising |last_relevance| is that this match and all following
1130 // matches get demoted; the relative order of matches is preserved.
1131 // One could imagine demoting only those matches that might cause
1132 // confusion (which, by the way, might change the relative order of
1133 // matches. We have decided to go with the simple demote-all approach
1134 // because selective demotion requires multiple Classify() calls and
1135 // such calls can be expensive (as expensive as running the whole
1136 // autocomplete system).
1137 if (!AutocompleteMatch::IsSearchType(match.type)) {
1138 last_relevance = CalculateRelevanceForHistory(
1139 base::Time::Now(), is_keyword, false,
1140 prevent_search_history_inlining);
1141 }
1142 }
1143
[email protected]0b9575f2014-07-30 11:58:371144 for (SearchSuggestionParser::SuggestResults::iterator i(
1145 scored_results.begin()); i != scored_results.end(); ++i) {
[email protected]7e3b77f2014-07-25 02:29:441146 if ((last_relevance != 0) && (i->relevance() >= last_relevance))
[email protected]55ce8f12012-05-09 04:44:081147 i->set_relevance(last_relevance - 1);
1148 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:101149 }
1150
[email protected]55ce8f12012-05-09 04:44:081151 return scored_results;
[email protected]257ab712009-04-14 17:16:241152}
1153
grobye5fcee42014-09-26 03:36:461154void SearchProvider::ScoreHistoryResults(
1155 const HistoryResults& results,
1156 bool is_keyword,
1157 SearchSuggestionParser::SuggestResults* scored_results) {
1158 DCHECK(scored_results);
1159 if (results.empty()) {
1160 scored_results->clear();
1161 return;
1162 }
1163
1164 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
1165 (input_.type() == metrics::OmniboxInputType::URL);
1166 const base::string16 input_text = GetInput(is_keyword).text();
1167 bool input_multiple_words = HasMultipleWords(input_text);
1168
1169 if (!prevent_inline_autocomplete && input_multiple_words) {
1170 // ScoreHistoryResultsHelper() allows autocompletion of multi-word, 1-visit
1171 // queries if the input also has multiple words. But if we were already
1172 // scoring a multi-word, multi-visit query aggressively, and the current
1173 // input is still a prefix of it, then changing the suggestion suddenly
1174 // feels wrong. To detect this case, first score as if only one word has
1175 // been typed, then check if the best result came from aggressive search
1176 // history scoring. If it did, then just keep that score set. This
1177 // 1200 the lowest possible score in CalculateRelevanceForHistory()'s
1178 // aggressive-scoring curve.
1179 *scored_results = ScoreHistoryResultsHelper(
1180 results, prevent_inline_autocomplete, false, input_text, is_keyword);
1181 if ((scored_results->front().relevance() < 1200) ||
1182 !HasMultipleWords(scored_results->front().suggestion()))
1183 scored_results->clear(); // Didn't detect the case above, score normally.
1184 }
1185 if (scored_results->empty()) {
1186 *scored_results = ScoreHistoryResultsHelper(results,
1187 prevent_inline_autocomplete,
1188 input_multiple_words,
1189 input_text,
1190 is_keyword);
1191 }
1192}
1193
[email protected]0b9575f2014-07-30 11:58:371194void SearchProvider::AddSuggestResultsToMap(
1195 const SearchSuggestionParser::SuggestResults& results,
1196 const std::string& metadata,
1197 MatchMap* map) {
[email protected]7bc5e162014-08-15 19:41:111198 for (size_t i = 0; i < results.size(); ++i) {
1199 AddMatchToMap(results[i], metadata, i, false,
1200 providers_.GetKeywordProviderURL() != NULL, map);
1201 }
initial.commit09911bf2008-07-26 23:55:291202}
1203
[email protected]d30268a2013-06-25 22:31:071204int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const {
[email protected]dc6943b2012-06-19 06:39:561205 // Use the suggested verbatim relevance score if it is non-negative (valid),
1206 // if inline autocomplete isn't prevented (always show verbatim on backspace),
[email protected]1beee342012-06-19 22:22:281207 // and if it won't suppress verbatim, leaving no default provider matches.
1208 // Otherwise, if the default provider returned no matches and was still able
[email protected]dc6943b2012-06-19 06:39:561209 // to suppress verbatim, the user would have no search/nav matches and may be
[email protected]1beee342012-06-19 22:22:281210 // left unable to search using their default provider from the omnibox.
[email protected]dc6943b2012-06-19 06:39:561211 // Check for results on each verbatim calculation, as results from older
1212 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:231213 bool use_server_relevance =
1214 (default_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:281215 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:231216 ((default_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:241217 !default_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:231218 !default_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:071219 if (relevance_from_server)
1220 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:231221 return use_server_relevance ?
1222 default_results_.verbatim_relevance : CalculateRelevanceForVerbatim();
[email protected]382a0642012-06-06 06:13:521223}
[email protected]d1f0a7f2012-06-05 10:26:421224
[email protected]382a0642012-06-06 06:13:521225int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:471226 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:361227 return 250;
[email protected]dab8d52d2013-03-05 07:35:281228 return CalculateRelevanceForVerbatimIgnoringKeywordModeState();
1229}
[email protected]52d08b12009-10-19 18:42:361230
[email protected]dab8d52d2013-03-05 07:35:281231int SearchProvider::
1232 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const {
initial.commit09911bf2008-07-26 23:55:291233 switch (input_.type()) {
[email protected]3dc75b12014-06-08 00:02:221234 case metrics::OmniboxInputType::UNKNOWN:
1235 case metrics::OmniboxInputType::QUERY:
1236 case metrics::OmniboxInputType::FORCED_QUERY:
[email protected]90fe2bb2013-01-15 03:42:131237 return kNonURLVerbatimRelevance;
initial.commit09911bf2008-07-26 23:55:291238
[email protected]3dc75b12014-06-08 00:02:221239 case metrics::OmniboxInputType::URL:
[email protected]52d08b12009-10-19 18:42:361240 return 850;
initial.commit09911bf2008-07-26 23:55:291241
1242 default:
1243 NOTREACHED();
1244 return 0;
1245 }
1246}
1247
[email protected]d30268a2013-06-25 22:31:071248int SearchProvider::GetKeywordVerbatimRelevance(
1249 bool* relevance_from_server) const {
[email protected]dab8d52d2013-03-05 07:35:281250 // Use the suggested verbatim relevance score if it is non-negative (valid),
1251 // if inline autocomplete isn't prevented (always show verbatim on backspace),
1252 // and if it won't suppress verbatim, leaving no keyword provider matches.
1253 // Otherwise, if the keyword provider returned no matches and was still able
1254 // to suppress verbatim, the user would have no search/nav matches and may be
1255 // left unable to search using their keyword provider from the omnibox.
1256 // Check for results on each verbatim calculation, as results from older
1257 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]bc8bb0cd2013-06-24 21:50:231258 bool use_server_relevance =
1259 (keyword_results_.verbatim_relevance >= 0) &&
[email protected]dab8d52d2013-03-05 07:35:281260 !input_.prevent_inline_autocomplete() &&
[email protected]bc8bb0cd2013-06-24 21:50:231261 ((keyword_results_.verbatim_relevance > 0) ||
[email protected]cc1526e2013-05-17 04:04:241262 !keyword_results_.suggest_results.empty() ||
[email protected]bc8bb0cd2013-06-24 21:50:231263 !keyword_results_.navigation_results.empty());
[email protected]d30268a2013-06-25 22:31:071264 if (relevance_from_server)
1265 *relevance_from_server = use_server_relevance;
[email protected]bc8bb0cd2013-06-24 21:50:231266 return use_server_relevance ?
1267 keyword_results_.verbatim_relevance :
1268 CalculateRelevanceForKeywordVerbatim(keyword_input_.type(),
1269 keyword_input_.prefer_keyword());
[email protected]5423e562013-02-07 03:58:451270}
1271
[email protected]51124552011-07-16 01:37:101272int SearchProvider::CalculateRelevanceForHistory(
[email protected]bc8bb0cd2013-06-24 21:50:231273 const base::Time& time,
[email protected]51124552011-07-16 01:37:101274 bool is_keyword,
[email protected]78e5e432013-08-03 02:10:101275 bool use_aggressive_method,
1276 bool prevent_search_history_inlining) const {
[email protected]aa613d62010-11-09 20:40:181277 // The relevance of past searches falls off over time. There are two distinct
1278 // equations used. If the first equation is used (searches to the primary
[email protected]78e5e432013-08-03 02:10:101279 // provider that we want to score aggressively), the score is in the range
1280 // 1300-1599 (unless |prevent_search_history_inlining|, in which case
[email protected]d8cd76b2013-07-10 09:46:161281 // it's in the range 1200-1299). If the second equation is used the
1282 // relevance of a search 15 minutes ago is discounted 50 points, while the
1283 // relevance of a search two weeks ago is discounted 450 points.
[email protected]bc8bb0cd2013-06-24 21:50:231284 double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0);
[email protected]188b50c2013-03-28 07:19:421285 bool is_primary_provider = is_keyword || !providers_.has_keyword_provider();
[email protected]78e5e432013-08-03 02:10:101286 if (is_primary_provider && use_aggressive_method) {
[email protected]aa613d62010-11-09 20:40:181287 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:101288 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:181289 if (elapsed_time < autocomplete_time) {
[email protected]d8cd76b2013-07-10 09:46:161290 int max_score = is_keyword ? 1599 : 1399;
[email protected]78e5e432013-08-03 02:10:101291 if (prevent_search_history_inlining)
[email protected]d8cd76b2013-07-10 09:46:161292 max_score = 1299;
1293 return max_score - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:181294 std::pow(elapsed_time / autocomplete_time, 2.5));
1295 }
1296 elapsed_time -= autocomplete_time;
1297 }
1298
[email protected]c3a4bd992010-08-18 20:25:011299 const int score_discount =
1300 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:291301
[email protected]6c85aa02009-02-27 12:08:091302 // Don't let scores go below 0. Negative relevance scores are meaningful in
1303 // a different way.
initial.commit09911bf2008-07-26 23:55:291304 int base_score;
[email protected]51124552011-07-16 01:37:101305 if (is_primary_provider)
[email protected]3dc75b12014-06-08 00:02:221306 base_score = (input_.type() == metrics::OmniboxInputType::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:101307 else
1308 base_score = 200;
initial.commit09911bf2008-07-26 23:55:291309 return std::max(0, base_score - score_discount);
1310}
1311
initial.commit09911bf2008-07-26 23:55:291312AutocompleteMatch SearchProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:371313 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]5889bfb2014-03-19 00:26:481314 base::string16 input;
1315 const bool trimmed_whitespace = base::TrimWhitespace(
1316 navigation.from_keyword_provider() ?
1317 keyword_input_.text() : input_.text(),
1318 base::TRIM_TRAILING, &input) != base::TRIM_NONE;
[email protected]55ce8f12012-05-09 04:44:081319 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:471320 navigation.type());
[email protected]55ce8f12012-05-09 04:44:081321 match.destination_url = navigation.url();
[email protected]78981d8c2014-05-09 15:05:471322 BaseSearchProvider::SetDeletionURL(navigation.deletion_url(), &match);
[email protected]23db6492014-01-16 02:35:301323 // First look for the user's input inside the formatted url as it would be
[email protected]371dab12012-06-01 03:23:551324 // without trimming the scheme, so we can find matches at the beginning of the
1325 // scheme.
[email protected]371dab12012-06-01 03:23:551326 const URLPrefix* prefix =
[email protected]23db6492014-01-16 02:35:301327 URLPrefix::BestURLPrefix(navigation.formatted_url(), input);
[email protected]371dab12012-06-01 03:23:551328 size_t match_start = (prefix == NULL) ?
[email protected]23db6492014-01-16 02:35:301329 navigation.formatted_url().find(input) : prefix->prefix.length();
[email protected]d2445c82013-11-04 22:28:351330 bool trim_http = !AutocompleteInput::HasHTTPScheme(input) &&
1331 (!prefix || (match_start != 0));
[email protected]23db6492014-01-16 02:35:301332 const net::FormatUrlTypes format_types =
1333 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
[email protected]371dab12012-06-01 03:23:551334
Ryo Hashimoto884ad192014-08-28 05:54:301335 const std::string languages(client_->AcceptLanguages());
[email protected]23db6492014-01-16 02:35:301336 size_t inline_autocomplete_offset = (prefix == NULL) ?
1337 base::string16::npos : (match_start + input.length());
[email protected]371dab12012-06-01 03:23:551338 match.fill_into_edit +=
[email protected]5655ea32014-06-21 05:28:081339 AutocompleteInput::FormattedStringWithEquivalentMeaning(
1340 navigation.url(),
[email protected]371dab12012-06-01 03:23:551341 net::FormatUrl(navigation.url(), languages, format_types,
1342 net::UnescapeRule::SPACES, NULL, NULL,
[email protected]5655ea32014-06-21 05:28:081343 &inline_autocomplete_offset),
Ryo Hashimoto884ad192014-08-28 05:54:301344 client_->SchemeClassifier());
[email protected]14119032013-11-07 08:14:261345 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1346 // Otherwise, user edits to a suggestion would show non-Search results.
[email protected]3dc75b12014-06-08 00:02:221347 if (input_.type() == metrics::OmniboxInputType::FORCED_QUERY) {
[email protected]670d3232013-12-24 17:58:581348 match.fill_into_edit.insert(0, base::ASCIIToUTF16("?"));
[email protected]0085863a2013-12-06 21:19:031349 if (inline_autocomplete_offset != base::string16::npos)
[email protected]14119032013-11-07 08:14:261350 ++inline_autocomplete_offset;
1351 }
[email protected]6c94a1022014-02-21 03:48:041352 if (inline_autocomplete_offset != base::string16::npos) {
[email protected]518024c2013-07-19 23:40:251353 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
1354 match.inline_autocompletion =
1355 match.fill_into_edit.substr(inline_autocomplete_offset);
1356 }
[email protected]6c94a1022014-02-21 03:48:041357 // An inlineable navsuggestion can only be the default match when there
1358 // is no keyword provider active, lest it appear first and break the user
mpearson6c183672014-09-03 02:09:421359 // out of keyword mode. We also must have received the navsuggestion before
1360 // the last keystroke, to prevent asynchronous inline autocompletions changes.
1361 // The navsuggestion can also only be default if either the inline
[email protected]5889bfb2014-03-19 00:26:481362 // autocompletion is empty or we're not preventing inline autocompletion.
1363 // Finally, if we have an inlineable navsuggestion with an inline completion
1364 // that we're not preventing, make sure we didn't trim any whitespace.
1365 // We don't want to claim http://foo.com/bar is inlineable against the
1366 // input "foo.com/b ".
mpearson6c183672014-09-03 02:09:421367 match.allowed_to_be_default_match =
1368 (prefix != NULL) &&
[email protected]6c94a1022014-02-21 03:48:041369 (providers_.GetKeywordProviderURL() == NULL) &&
mpearson6c183672014-09-03 02:09:421370 !navigation.received_after_last_keystroke() &&
[email protected]5889bfb2014-03-19 00:26:481371 (match.inline_autocompletion.empty() ||
[email protected]78981d8c2014-05-09 15:05:471372 (!input_.prevent_inline_autocomplete() && !trimmed_whitespace));
[email protected]c7b8be02014-07-11 19:46:341373 match.EnsureUWYTIsAllowedToBeDefault(
1374 input_.canonicalized_url(), providers_.template_url_service());
[email protected]371dab12012-06-01 03:23:551375
[email protected]23db6492014-01-16 02:35:301376 match.contents = navigation.match_contents();
1377 match.contents_class = navigation.match_contents_class();
[email protected]55ce8f12012-05-09 04:44:081378 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551379 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1380 ACMatchClassification::NONE, &match.description_class);
[email protected]d30268a2013-06-25 22:31:071381
1382 match.RecordAdditionalInfo(
1383 kRelevanceFromServerKey,
1384 navigation.relevance_from_server() ? kTrue : kFalse);
[email protected]987fad782013-08-28 06:23:181385 match.RecordAdditionalInfo(kShouldPrefetchKey, kFalse);
[email protected]d30268a2013-06-25 22:31:071386
initial.commit09911bf2008-07-26 23:55:291387 return match;
1388}
[email protected]4ab4c7c2010-11-24 04:49:341389
1390void SearchProvider::UpdateDone() {
[email protected]2cdf1172012-08-26 12:21:331391 // We're done when the timer isn't running, there are no suggest queries
[email protected]5fac3782013-03-06 09:32:311392 // pending, and we're not waiting on Instant.
[email protected]e1290ee62013-06-26 18:31:151393 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0);
[email protected]4ab4c7c2010-11-24 04:49:341394}
[email protected]20184242014-05-14 02:57:421395
1396std::string SearchProvider::GetSessionToken() {
1397 base::TimeTicks current_time(base::TimeTicks::Now());
1398 // Renew token if it expired.
1399 if (current_time > token_expiration_time_) {
1400 const size_t kTokenBytes = 12;
1401 std::string raw_data;
1402 base::RandBytes(WriteInto(&raw_data, kTokenBytes + 1), kTokenBytes);
1403 base::Base64Encode(raw_data, &current_token_);
[email protected]ab2c31f72014-05-17 17:03:531404
1405 // Make the base64 encoded value URL and filename safe(see RFC 3548).
1406 std::replace(current_token_.begin(), current_token_.end(), '+', '-');
1407 std::replace(current_token_.begin(), current_token_.end(), '/', '_');
[email protected]20184242014-05-14 02:57:421408 }
1409
1410 // Extend expiration time another 60 seconds.
1411 token_expiration_time_ = current_time + base::TimeDelta::FromSeconds(60);
1412
1413 return current_token_;
1414}
[email protected]2ef2a6642014-07-30 05:50:291415
1416void SearchProvider::RegisterDisplayedAnswers(
1417 const AutocompleteResult& result) {
1418 if (result.empty())
1419 return;
1420
1421 // The answer must be in the first or second slot to be considered. It should
1422 // only be in the second slot if AutocompleteController ranked a local search
1423 // history or a verbatim item higher than the answer.
1424 AutocompleteResult::const_iterator match = result.begin();
1425 if (match->answer_contents.empty() && result.size() > 1)
1426 ++match;
1427 if (match->answer_contents.empty() || match->answer_type.empty() ||
1428 match->fill_into_edit.empty())
1429 return;
1430
1431 // Valid answer encountered, cache it for further queries.
[email protected]ebbac63e2014-08-22 01:43:061432 answers_cache_.UpdateRecentAnswers(match->fill_into_edit, match->answer_type);
[email protected]2ef2a6642014-07-30 05:50:291433}
1434
grobye5fcee42014-09-26 03:36:461435AnswersQueryData SearchProvider::FindAnswersPrefetchData() {
1436 // Retrieve the top entry from scored history results.
1437 MatchMap map;
1438 AddTransformedHistoryResultsToMap(transformed_keyword_history_results_,
1439 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
1440 &map);
1441 AddTransformedHistoryResultsToMap(transformed_default_history_results_,
1442 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
1443 &map);
1444
1445 ACMatches matches;
1446 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
1447 matches.push_back(i->second);
1448 std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant);
1449
1450 // If there is a top scoring entry, find the corresponding answer.
1451 if (!matches.empty())
1452 return answers_cache_.GetTopAnswerEntry(matches[0].contents);
1453
1454 return AnswersQueryData();
[email protected]2ef2a6642014-07-30 05:50:291455}