blob: 429e682a3aefbecb8e4f726c4354d84c24dec265 [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/autocomplete/search_provider.h"
6
[email protected]1cb2dac2010-03-08 21:49:157#include <algorithm>
[email protected]c3a4bd992010-08-18 20:25:018#include <cmath>
[email protected]1cb2dac2010-03-08 21:49:159
[email protected]2041cf342010-02-19 03:15:5910#include "base/callback.h"
[email protected]51124552011-07-16 01:37:1011#include "base/i18n/break_iterator.h"
[email protected]503d03872011-05-06 08:36:2612#include "base/i18n/case_conversion.h"
[email protected]d6e58c6e2009-10-10 20:40:5013#include "base/i18n/icu_string_conversions.h"
[email protected]ffbec692012-02-26 20:26:4214#include "base/json/json_string_value_serializer.h"
initial.commit09911bf2008-07-26 23:55:2915#include "base/message_loop.h"
[email protected]f5b95ba92012-03-27 14:05:1916#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5717#include "base/prefs/pref_service.h"
[email protected]dc9a6762010-08-16 07:13:5318#include "base/string16.h"
[email protected]371dab12012-06-01 03:23:5519#include "base/string_util.h"
[email protected]1cb2dac2010-03-08 21:49:1520#include "base/utf_string_conversions.h"
[email protected]ea3b9a502011-04-04 14:19:3721#include "chrome/browser/autocomplete/autocomplete_classifier.h"
[email protected]810ffba2012-06-12 01:07:4822#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
[email protected]618d6e62012-12-16 05:55:5723#include "chrome/browser/autocomplete/autocomplete_field_trial.h"
[email protected]9ac40092010-10-27 23:05:2624#include "chrome/browser/autocomplete/autocomplete_match.h"
[email protected]5af9bc82012-06-29 00:53:4825#include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
[email protected]73c2b1632012-07-02 22:51:3826#include "chrome/browser/autocomplete/autocomplete_result.h"
[email protected]3723e6e2012-06-11 21:06:5627#include "chrome/browser/autocomplete/history_url_provider.h"
[email protected]2c812ba02011-07-14 00:23:1528#include "chrome/browser/autocomplete/keyword_provider.h"
[email protected]371dab12012-06-01 03:23:5529#include "chrome/browser/autocomplete/url_prefix.h"
[email protected]6a2c09f2013-01-25 04:50:0730#include "chrome/browser/history/history_service.h"
[email protected]9d2db762012-06-19 00:01:1031#include "chrome/browser/history/history_service_factory.h"
[email protected]10c2d692012-05-11 05:32:2332#include "chrome/browser/history/in_memory_database.h"
[email protected]ab7780792013-01-10 01:26:0933#include "chrome/browser/metrics/variations/variations_http_header_provider.h"
[email protected]f870a322009-01-16 21:47:2734#include "chrome/browser/net/url_fixer_upper.h"
[email protected]8ecad5e2010-12-02 21:18:3335#include "chrome/browser/profiles/profile.h"
[email protected]a0ad93ea2012-05-07 22:11:5336#include "chrome/browser/search_engines/search_engine_type.h"
[email protected]9899a612012-08-21 23:50:0437#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
[email protected]8e5c89a2011-06-07 18:13:3338#include "chrome/browser/search_engines/template_url_service.h"
39#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]e41982a72012-11-20 07:16:5140#include "chrome/browser/ui/browser_instant_controller.h"
[email protected]5cd06c3e2012-09-20 03:25:2041#include "chrome/browser/ui/search/search.h"
initial.commit09911bf2008-07-26 23:55:2942#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0243#include "chrome/common/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2944#include "googleurl/src/url_util.h"
[email protected]34ac8f32009-02-22 23:03:2745#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2946#include "net/base/escape.h"
[email protected]d3cf8682f02012-02-29 23:29:3447#include "net/base/load_flags.h"
[email protected]371dab12012-06-01 03:23:5548#include "net/base/net_util.h"
[email protected]bd3b4712012-12-18 17:01:3049#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2150#include "net/http/http_response_headers.h"
[email protected]3dc1bc42012-06-19 08:20:5351#include "net/url_request/url_fetcher.h"
[email protected]319d9e6f2009-02-18 19:47:2152#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1753#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2954
[email protected]e1acf6f2008-10-27 20:43:3355using base::Time;
56using base::TimeDelta;
57
[email protected]51124552011-07-16 01:37:1058namespace {
59
[email protected]7706a522012-08-16 17:42:2560// We keep track in a histogram how many suggest requests we send, how
61// many suggest requests we invalidate (e.g., due to a user typing
62// another character), and how many replies we receive.
63// *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
64// (excluding the end-of-list enum value)
65// We do not want values of existing enums to change or else it screws
66// up the statistics.
67enum SuggestRequestsHistogramValue {
68 REQUEST_SENT = 1,
69 REQUEST_INVALIDATED,
70 REPLY_RECEIVED,
71 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE
72};
73
[email protected]90fe2bb2013-01-15 03:42:1374// The verbatim score for an input which is not an URL.
75const int kNonURLVerbatimRelevance = 1300;
76
[email protected]7706a522012-08-16 17:42:2577// Increments the appropriate value in the histogram by one.
78void LogOmniboxSuggestRequest(
79 SuggestRequestsHistogramValue request_value) {
80 UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value,
81 MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE);
82}
83
[email protected]51124552011-07-16 01:37:1084bool HasMultipleWords(const string16& text) {
85 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
86 bool found_word = false;
87 if (i.Init()) {
88 while (i.Advance()) {
89 if (i.IsWord()) {
90 if (found_word)
91 return true;
92 found_word = true;
93 }
94 }
95 }
96 return false;
97}
98
[email protected]d1f0a7f2012-06-05 10:26:4299} // namespace
[email protected]51124552011-07-16 01:37:10100
[email protected]033f3422012-03-13 21:24:18101
[email protected]3954c3a2012-04-10 20:17:55102// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:58103
[email protected]85b8d6f2012-05-08 20:53:47104SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
105 : template_url_service_(template_url_service) {
106}
107
108const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
109 return default_provider_.empty() ? NULL :
110 template_url_service_->GetTemplateURLForKeyword(default_provider_);
111}
112
113const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
114 return keyword_provider_.empty() ? NULL :
115 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:24116}
117
[email protected]3954c3a2012-04-10 20:17:55118
119// SearchProvider -------------------------------------------------------------
120
121// static
122const int SearchProvider::kDefaultProviderURLFetcherID = 1;
123// static
124const int SearchProvider::kKeywordProviderURLFetcherID = 2;
[email protected]3954c3a2012-04-10 20:17:55125
[email protected]30f5bc92012-06-26 04:14:55126SearchProvider::SearchProvider(AutocompleteProviderListener* listener,
127 Profile* profile)
[email protected]35f1f4f02012-09-11 13:17:00128 : AutocompleteProvider(listener, profile,
129 AutocompleteProvider::TYPE_SEARCH),
[email protected]85b8d6f2012-05-08 20:53:47130 providers_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]601858c02010-09-01 17:08:20131 suggest_results_pending_(0),
[email protected]dab8d52d2013-03-05 07:35:28132 has_default_suggested_relevance_(false),
133 has_keyword_suggested_relevance_(false),
134 default_verbatim_relevance_(-1),
135 keyword_verbatim_relevance_(-1),
[email protected]8e5cc282010-12-05 18:11:39136 have_suggest_results_(false),
[email protected]618d6e62012-12-16 05:55:57137 instant_finalized_(false),
138 field_trial_triggered_(false),
139 field_trial_triggered_in_session_(false) {
[email protected]4ab4c7c2010-11-24 04:49:34140}
141
[email protected]a2fedb1e2011-01-25 15:23:36142void SearchProvider::FinalizeInstantQuery(const string16& input_text,
[email protected]93b73832012-10-18 20:18:38143 const InstantSuggestion& suggestion) {
[email protected]4ab4c7c2010-11-24 04:49:34144 if (done_ || instant_finalized_)
145 return;
146
147 instant_finalized_ = true;
148 UpdateDone();
149
[email protected]e918c112010-12-08 23:03:49150 if (input_text.empty()) {
[email protected]4ab4c7c2010-11-24 04:49:34151 // We only need to update the listener if we're actually done.
152 if (done_)
153 listener_->OnProviderUpdate(false);
154 return;
155 }
156
[email protected]93b73832012-10-18 20:18:38157 default_provider_suggestion_ = suggestion;
[email protected]9e789742011-01-10 23:27:32158
[email protected]a2fedb1e2011-01-25 15:23:36159 string16 adjusted_input_text(input_text);
[email protected]e918c112010-12-08 23:03:49160 AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(),
161 &adjusted_input_text);
162
[email protected]93b73832012-10-18 20:18:38163 const string16 text = adjusted_input_text + suggestion.text;
[email protected]9acdcdc02012-05-21 20:58:36164 bool results_updated = false;
[email protected]4ab4c7c2010-11-24 04:49:34165 // Remove any matches that are identical to |text|. We don't use the
166 // destination_url for comparison as it varies depending upon the index passed
167 // to TemplateURL::ReplaceSearchTerms.
168 for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) {
169 if (((i->type == AutocompleteMatch::SEARCH_HISTORY) ||
170 (i->type == AutocompleteMatch::SEARCH_SUGGEST)) &&
171 (i->fill_into_edit == text)) {
[email protected]e030de62010-11-24 05:41:19172 i = matches_.erase(i);
[email protected]9acdcdc02012-05-21 20:58:36173 results_updated = true;
[email protected]4ab4c7c2010-11-24 04:49:34174 } else {
175 ++i;
176 }
177 }
178
[email protected]90fe2bb2013-01-15 03:42:13179 // Add the new instant suggest result.
[email protected]93b73832012-10-18 20:18:38180 if (suggestion.type == INSTANT_SUGGESTION_SEARCH) {
[email protected]90fe2bb2013-01-15 03:42:13181 // Instant has a query suggestion. Rank it higher than SEARCH_WHAT_YOU_TYPED
182 // so that it gets autocompleted.
183 const int verbatim_relevance = GetVerbatimRelevance();
[email protected]93b73832012-10-18 20:18:38184 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
[email protected]4ab4c7c2010-11-24 04:49:34185 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
186 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]93b73832012-10-18 20:18:38187 MatchMap match_map;
188 AddMatchToMap(text, adjusted_input_text, verbatim_relevance + 1,
189 AutocompleteMatch::SEARCH_SUGGEST,
190 did_not_accept_default_suggestion, false, &match_map);
191 if (!match_map.empty()) {
192 matches_.push_back(match_map.begin()->second);
193 results_updated = true;
194 }
195 } else {
[email protected]c112f0fe72013-01-18 20:30:09196 // Instant has a URL suggestion. Rank it higher than URL_WHAT_YOU_TYPED so
[email protected]90fe2bb2013-01-15 03:42:13197 // it gets autocompleted; use kNonURLVerbatimRelevance rather than
198 // verbatim_relevance so that the score does not change if the user keeps
199 // typing and the input changes from type UNKNOWN to URL.
[email protected]93b73832012-10-18 20:18:38200 matches_.push_back(NavigationToMatch(
201 NavigationResult(GURL(UTF16ToUTF8(suggestion.text)),
202 string16(),
[email protected]90fe2bb2013-01-15 03:42:13203 kNonURLVerbatimRelevance + 1),
[email protected]93b73832012-10-18 20:18:38204 false));
[email protected]9acdcdc02012-05-21 20:58:36205 results_updated = true;
206 }
[email protected]4ab4c7c2010-11-24 04:49:34207
[email protected]9acdcdc02012-05-21 20:58:36208 if (results_updated || done_)
209 listener_->OnProviderUpdate(results_updated);
[email protected]601858c02010-09-01 17:08:20210}
211
initial.commit09911bf2008-07-26 23:55:29212void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27213 bool minimal_changes) {
[email protected]04504c242013-01-22 21:08:55214 // Do our best to load the model as early as possible. This will reduce
215 // odds of having the model not ready when really needed (a non-empty input).
216 TemplateURLService* model = providers_.template_url_service();
217 DCHECK(model);
218 model->Load();
219
initial.commit09911bf2008-07-26 23:55:29220 matches_.clear();
[email protected]618d6e62012-12-16 05:55:57221 field_trial_triggered_ = false;
initial.commit09911bf2008-07-26 23:55:29222
[email protected]ea3b9a502011-04-04 14:19:37223 instant_finalized_ =
224 (input.matches_requested() != AutocompleteInput::ALL_MATCHES);
[email protected]4ab4c7c2010-11-24 04:49:34225
[email protected]6c85aa02009-02-27 12:08:09226 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29227 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
[email protected]e29249dc52012-07-19 17:33:50228 Stop(false);
initial.commit09911bf2008-07-26 23:55:29229 return;
230 }
231
[email protected]14710852013-02-05 23:45:41232 keyword_input_ = input;
[email protected]257ab712009-04-14 17:16:24233 const TemplateURL* keyword_provider =
[email protected]14710852013-02-05 23:45:41234 KeywordProvider::GetSubstitutingTemplateURLForInput(model,
235 &keyword_input_);
236 if (keyword_provider == NULL)
237 keyword_input_.Clear();
238 else if (keyword_input_.text().empty())
[email protected]257ab712009-04-14 17:16:24239 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24240
[email protected]85b8d6f2012-05-08 20:53:47241 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]9b74ab52012-03-30 16:08:07242 if (default_provider && !default_provider->SupportsReplacement())
[email protected]257ab712009-04-14 17:16:24243 default_provider = NULL;
244
245 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18246 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24247
248 if (!default_provider && !keyword_provider) {
249 // No valid providers.
[email protected]e29249dc52012-07-19 17:33:50250 Stop(false);
initial.commit09911bf2008-07-26 23:55:29251 return;
252 }
253
254 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24255 // or the providers, abort the query.
[email protected]85b8d6f2012-05-08 20:53:47256 string16 default_provider_keyword(default_provider ?
257 default_provider->keyword() : string16());
258 string16 keyword_provider_keyword(keyword_provider ?
259 keyword_provider->keyword() : string16());
[email protected]9e789742011-01-10 23:27:32260 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47261 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]9e789742011-01-10 23:27:32262 if (done_)
[email protected]93b73832012-10-18 20:18:38263 default_provider_suggestion_ = InstantSuggestion();
[email protected]9e789742011-01-10 23:27:32264 else
[email protected]e29249dc52012-07-19 17:33:50265 Stop(false);
[email protected]257ab712009-04-14 17:16:24266 }
initial.commit09911bf2008-07-26 23:55:29267
[email protected]85b8d6f2012-05-08 20:53:47268 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29269
270 if (input.text().empty()) {
271 // User typed "?" alone. Give them a placeholder result indicating what
272 // this syntax does.
[email protected]257ab712009-04-14 17:16:24273 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00274 AutocompleteMatch match;
275 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36276 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24277 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35278 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47279 match.keyword = providers_.default_provider();
[email protected]257ab712009-04-14 17:16:24280 matches_.push_back(match);
281 }
[email protected]e29249dc52012-07-19 17:33:50282 Stop(false);
initial.commit09911bf2008-07-26 23:55:29283 return;
284 }
285
286 input_ = input;
287
[email protected]5cd06c3e2012-09-20 03:25:20288 // Don't run the normal provider flow when the Instant Extended API is
289 // enabled. (When the Extended API is enabled, the embedded page will handle
290 // all search suggestions itself.)
291 // TODO(dcblack): once we are done refactoring the omnibox so we don't need to
292 // use FinalizeInstantQuery anymore, we can take out this check and remove
293 // this provider from kInstantExtendedOmniboxProviders.
294 if (!chrome::search::IsInstantExtendedAPIEnabled(profile_)) {
295 DoHistoryQuery(minimal_changes);
296 StartOrStopSuggestQuery(minimal_changes);
297 }
[email protected]344946a12012-12-20 12:03:42298 UpdateMatches();
initial.commit09911bf2008-07-26 23:55:29299}
300
[email protected]55ce8f12012-05-09 04:44:08301SearchProvider::Result::Result(int relevance) : relevance_(relevance) {}
302SearchProvider::Result::~Result() {}
303
304SearchProvider::SuggestResult::SuggestResult(const string16& suggestion,
305 int relevance)
306 : Result(relevance),
307 suggestion_(suggestion) {
308}
309
310SearchProvider::SuggestResult::~SuggestResult() {}
311
312SearchProvider::NavigationResult::NavigationResult(const GURL& url,
313 const string16& description,
314 int relevance)
315 : Result(relevance),
316 url_(url),
317 description_(description) {
318 DCHECK(url_.is_valid());
319}
320
321SearchProvider::NavigationResult::~NavigationResult() {}
322
323class SearchProvider::CompareScoredResults {
[email protected]51124552011-07-16 01:37:10324 public:
[email protected]55ce8f12012-05-09 04:44:08325 bool operator()(const Result& a, const Result& b) {
[email protected]51124552011-07-16 01:37:10326 // Sort in descending relevance order.
[email protected]55ce8f12012-05-09 04:44:08327 return a.relevance() > b.relevance();
[email protected]51124552011-07-16 01:37:10328 }
329};
330
initial.commit09911bf2008-07-26 23:55:29331void SearchProvider::Run() {
332 // Start a new request with the current input.
[email protected]257ab712009-04-14 17:16:24333 suggest_results_pending_ = 0;
[email protected]a0ad93ea2012-05-07 22:11:53334 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]9ff91722012-09-07 05:29:12335
336 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
[email protected]14710852013-02-05 23:45:41337 providers_.GetDefaultProviderURL(), input_));
[email protected]9ff91722012-09-07 05:29:12338 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
[email protected]14710852013-02-05 23:45:41339 providers_.GetKeywordProviderURL(), keyword_input_));
[email protected]85b8d6f2012-05-08 20:53:47340
341 // Both the above can fail if the providers have been modified or deleted
342 // since the query began.
343 if (suggest_results_pending_ == 0) {
344 UpdateDone();
345 // We only need to update the listener if we're actually done.
346 if (done_)
347 listener_->OnProviderUpdate(false);
348 }
initial.commit09911bf2008-07-26 23:55:29349}
350
[email protected]e29249dc52012-07-19 17:33:50351void SearchProvider::Stop(bool clear_cached_results) {
initial.commit09911bf2008-07-26 23:55:29352 StopSuggest();
353 done_ = true;
[email protected]93b73832012-10-18 20:18:38354 default_provider_suggestion_ = InstantSuggestion();
[email protected]e29249dc52012-07-19 17:33:50355
356 if (clear_cached_results)
357 ClearResults();
initial.commit09911bf2008-07-26 23:55:29358}
359
[email protected]0e9e8782012-05-15 23:01:51360void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
361 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
362 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
363 new_entry.set_provider(AsOmniboxEventProviderType());
364 new_entry.set_provider_done(done_);
[email protected]618d6e62012-12-16 05:55:57365 uint32 field_trial_hash = 0;
366 if (AutocompleteFieldTrial::GetActiveSuggestFieldTrialHash(
[email protected]d78d7362012-12-19 18:43:28367 &field_trial_hash)) {
[email protected]618d6e62012-12-16 05:55:57368 if (field_trial_triggered_)
369 new_entry.mutable_field_trial_triggered()->Add(field_trial_hash);
[email protected]d78d7362012-12-19 18:43:28370 if (field_trial_triggered_in_session_) {
[email protected]618d6e62012-12-16 05:55:57371 new_entry.mutable_field_trial_triggered_in_session()->Add(
372 field_trial_hash);
[email protected]d78d7362012-12-19 18:43:28373 }
[email protected]618d6e62012-12-16 05:55:57374 }
375}
376
377void SearchProvider::ResetSession() {
378 field_trial_triggered_in_session_ = false;
[email protected]0e9e8782012-05-15 23:01:51379}
380
[email protected]10c2d692012-05-11 05:32:23381void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) {
initial.commit09911bf2008-07-26 23:55:29382 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24383 suggest_results_pending_--;
[email protected]7706a522012-08-16 17:42:25384 LogOmniboxSuggestRequest(REPLY_RECEIVED);
[email protected]1cb2dac2010-03-08 21:49:15385 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06386 const net::HttpResponseHeaders* const response_headers =
[email protected]7cc6e5632011-10-25 17:56:12387 source->GetResponseHeaders();
[email protected]c530c852011-10-24 18:18:34388 std::string json_data;
389 source->GetResponseAsString(&json_data);
[email protected]6c85aa02009-02-27 12:08:09390 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
391 // files in non-UTF-8 encodings. The actual encoding is usually specified in
392 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06393 if (response_headers) {
394 std::string charset;
395 if (response_headers->GetCharset(&charset)) {
[email protected]a2fedb1e2011-01-25 15:23:36396 string16 data_16;
[email protected]ec9207d32008-09-26 00:51:06397 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]c530c852011-10-24 18:18:34398 if (base::CodepageToUTF16(json_data, charset.c_str(),
[email protected]a2fedb1e2011-01-25 15:23:36399 base::OnStringConversionError::FAIL,
400 &data_16))
401 json_data = UTF16ToUTF8(data_16);
[email protected]ec9207d32008-09-26 00:51:06402 }
403 }
404
[email protected]d7ad4772012-06-01 03:12:54405 const bool is_keyword = (source == keyword_fetcher_.get());
[email protected]f2916ea2013-01-26 02:20:14406 // Ensure the request succeeded and that the provider used is still available.
407 // A verbatim match cannot be generated without this provider, causing errors.
[email protected]013e9a02012-05-18 20:27:10408 const bool request_succeeded =
[email protected]f2916ea2013-01-26 02:20:14409 source->GetStatus().is_success() && source->GetResponseCode() == 200 &&
410 ((is_keyword && providers_.GetKeywordProviderURL()) ||
411 (!is_keyword && providers_.GetDefaultProviderURL()));
[email protected]a0ad93ea2012-05-07 22:11:53412
413 // Record response time for suggest requests sent to Google. We care
414 // only about the common case: the Google default provider used in
415 // non-keyword mode.
[email protected]85b8d6f2012-05-08 20:53:47416 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
[email protected]55ce8f12012-05-09 04:44:08417 if (!is_keyword && default_url &&
[email protected]9899a612012-08-21 23:50:04418 (TemplateURLPrepopulateData::GetEngineType(default_url->url()) ==
419 SEARCH_ENGINE_GOOGLE)) {
[email protected]6dc950f2012-07-16 19:49:08420 const TimeDelta elapsed_time =
[email protected]013e9a02012-05-18 20:27:10421 base::TimeTicks::Now() - time_suggest_request_sent_;
422 if (request_succeeded) {
423 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
424 elapsed_time);
425 } else {
426 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
427 elapsed_time);
428 }
[email protected]b4cebf82008-12-29 19:59:08429 }
430
[email protected]d7ad4772012-06-01 03:12:54431 bool results_updated = false;
432 if (request_succeeded) {
433 JSONStringValueSerializer deserializer(json_data);
434 deserializer.set_allow_trailing_comma(true);
435 scoped_ptr<Value> data(deserializer.Deserialize(NULL, NULL));
436 results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword);
437 }
438
[email protected]344946a12012-12-20 12:03:42439 UpdateMatches();
[email protected]d7ad4772012-06-01 03:12:54440 if (done_ || results_updated)
441 listener_->OnProviderUpdate(results_updated);
initial.commit09911bf2008-07-26 23:55:29442}
443
[email protected]601858c02010-09-01 17:08:20444SearchProvider::~SearchProvider() {
445}
446
[email protected]8d457132010-11-04 18:13:40447void SearchProvider::DoHistoryQuery(bool minimal_changes) {
448 // The history query results are synchronous, so if minimal_changes is true,
449 // we still have the last results and don't need to do anything.
450 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29451 return;
452
[email protected]8d457132010-11-04 18:13:40453 keyword_history_results_.clear();
454 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29455
[email protected]8d457132010-11-04 18:13:40456 HistoryService* const history_service =
[email protected]9d2db762012-06-19 00:01:10457 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
[email protected]8d457132010-11-04 18:13:40458 history::URLDatabase* url_db = history_service ?
459 history_service->InMemoryDatabase() : NULL;
460 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29461 return;
462
[email protected]51124552011-07-16 01:37:10463 // Request history for both the keyword and default provider. We grab many
464 // more matches than we'll ultimately clamp to so that if there are several
465 // recent multi-word matches who scores are lowered (see
466 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
467 // matches. Note that this doesn't fix the problem entirely, but merely
468 // limits it to cases with a very large number of such multi-word matches; for
469 // now, this seems OK compared with the complexity of a real fix, which would
470 // require multiple searches and tracking of "single- vs. multi-word" in the
471 // database.
472 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47473 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
474 if (default_url) {
475 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
476 num_matches, &default_history_results_);
[email protected]257ab712009-04-14 17:16:24477 }
[email protected]85b8d6f2012-05-08 20:53:47478 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
479 if (keyword_url) {
480 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]14710852013-02-05 23:45:41481 keyword_input_.text(), num_matches, &keyword_history_results_);
[email protected]3954c3a2012-04-10 20:17:55482 }
initial.commit09911bf2008-07-26 23:55:29483}
484
[email protected]6dc950f2012-07-16 19:49:08485void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]83c726482008-09-10 06:36:34486 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29487 StopSuggest();
[email protected]55ce8f12012-05-09 04:44:08488 ClearResults();
initial.commit09911bf2008-07-26 23:55:29489 return;
490 }
491
492 // For the minimal_changes case, if we finished the previous query and still
493 // have its results, or are allowed to keep running it, just do that, rather
494 // than starting a new query.
495 if (minimal_changes &&
[email protected]ea3b9a502011-04-04 14:19:37496 (have_suggest_results_ ||
497 (!done_ &&
498 input_.matches_requested() == AutocompleteInput::ALL_MATCHES)))
initial.commit09911bf2008-07-26 23:55:29499 return;
500
501 // We can't keep running any previous query, so halt it.
502 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42503
504 // Remove existing results that cannot inline autocomplete the new input.
505 RemoveStaleResults();
initial.commit09911bf2008-07-26 23:55:29506
507 // We can't start a new query if we're only allowed synchronous results.
[email protected]ea3b9a502011-04-04 14:19:37508 if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES)
initial.commit09911bf2008-07-26 23:55:29509 return;
510
[email protected]515ffa942012-11-27 20:18:24511 // To avoid flooding the suggest server, don't send a query until at least 100
512 // ms since the last query.
513 const int kMinimumTimeBetweenSuggestQueriesMs = 100;
514 base::TimeTicks next_suggest_time(time_suggest_request_sent_ +
515 TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs));
516 base::TimeTicks now(base::TimeTicks::Now());
517 if (now >= next_suggest_time) {
518 Run();
519 return;
520 }
521 timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29522}
523
[email protected]83c726482008-09-10 06:36:34524bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55525 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
526 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47527 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
528 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34529 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47530 ((!default_url || default_url->suggestions_url().empty()) &&
531 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34532 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
533 return false;
534
[email protected]cac59d32010-08-09 23:23:14535 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34536 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34537
[email protected]cac59d32010-08-09 23:23:14538 // FORCED_QUERY means the user is explicitly asking us to search for this, so
539 // we assume it isn't a URL and/or there isn't private data.
540 if (input_.type() == AutocompleteInput::FORCED_QUERY)
541 return true;
[email protected]83c726482008-09-10 06:36:34542
[email protected]cac59d32010-08-09 23:23:14543 // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a
544 // scheme that isn't http/https/ftp, we shouldn't send it. Sending things
545 // like file: and data: is both a waste of time and a disclosure of
546 // potentially private, local data. Other "schemes" may actually be
547 // usernames, and we don't want to send passwords. If the scheme is OK, we
548 // still need to check other cases below. If this is QUERY, then the presence
549 // of these schemes means the user explicitly typed one, and thus this is
550 // probably a URL that's being entered and happens to currently be invalid --
551 // in which case we again want to run our checks below. Other QUERY cases are
552 // less likely to be URLs and thus we assume we're OK.
[email protected]a2fedb1e2011-01-25 15:23:36553 if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) &&
554 !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
555 !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme))
[email protected]cac59d32010-08-09 23:23:14556 return (input_.type() == AutocompleteInput::QUERY);
557
558 // Don't send URLs with usernames, queries or refs. Some of these are
559 // private, and the Suggest server is unlikely to have any useful results
560 // for any of them. Also don't send URLs with ports, as we may initially
561 // think that a username + password is a host + port (and we don't want to
562 // send usernames/passwords), and even if the port really is a port, the
563 // server is once again unlikely to have and useful results.
564 const url_parse::Parsed& parts = input_.parts();
565 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
566 parts.query.is_nonempty() || parts.ref.is_nonempty())
567 return false;
568
569 // Don't send anything for https except the hostname. Hostnames are OK
570 // because they are visible when the TCP connection is established, but the
571 // specific path may reveal private information.
[email protected]a2fedb1e2011-01-25 15:23:36572 if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
573 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14574 return false;
[email protected]83c726482008-09-10 06:36:34575
576 return true;
577}
578
initial.commit09911bf2008-07-26 23:55:29579void SearchProvider::StopSuggest() {
[email protected]7706a522012-08-16 17:42:25580 // Increment the appropriate field in the histogram by the number of
581 // pending requests that were invalidated.
582 for (int i = 0; i < suggest_results_pending_; i++)
583 LogOmniboxSuggestRequest(REQUEST_INVALIDATED);
[email protected]257ab712009-04-14 17:16:24584 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14585 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24586 // Stop any in-progress URL fetches.
587 keyword_fetcher_.reset();
588 default_fetcher_.reset();
[email protected]55ce8f12012-05-09 04:44:08589}
590
591void SearchProvider::ClearResults() {
[email protected]257ab712009-04-14 17:16:24592 keyword_suggest_results_.clear();
593 default_suggest_results_.clear();
594 keyword_navigation_results_.clear();
595 default_navigation_results_.clear();
[email protected]dab8d52d2013-03-05 07:35:28596 has_default_suggested_relevance_ = false;
597 has_keyword_suggested_relevance_ = false;
598 default_verbatim_relevance_ = -1;
599 keyword_verbatim_relevance_ = -1;
initial.commit09911bf2008-07-26 23:55:29600 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29601}
602
[email protected]d1f0a7f2012-06-05 10:26:42603void SearchProvider::RemoveStaleResults() {
[email protected]14710852013-02-05 23:45:41604 // Keyword provider results should match |keyword_input_.text()|, unless
[email protected]42cfd01d02013-01-23 02:40:02605 // the input was just changed to non-keyword mode; in that case, compare
606 // against |input_.text()|.
607 const string16& keyword_input =
[email protected]14710852013-02-05 23:45:41608 !keyword_input_.text().empty() ? keyword_input_.text() : input_.text();
[email protected]42cfd01d02013-01-23 02:40:02609 RemoveStaleSuggestResults(&keyword_suggest_results_, keyword_input);
610 RemoveStaleSuggestResults(&default_suggest_results_, input_.text());
611 RemoveStaleNavigationResults(&keyword_navigation_results_, keyword_input);
612 RemoveStaleNavigationResults(&default_navigation_results_, input_.text());
[email protected]d1f0a7f2012-06-05 10:26:42613}
614
[email protected]42cfd01d02013-01-23 02:40:02615// static
[email protected]d1f0a7f2012-06-05 10:26:42616void SearchProvider::RemoveStaleSuggestResults(SuggestResults* list,
[email protected]42cfd01d02013-01-23 02:40:02617 const string16& input) {
[email protected]d1f0a7f2012-06-05 10:26:42618 for (SuggestResults::iterator i = list->begin(); i < list->end();)
619 i = StartsWith(i->suggestion(), input, false) ? (i + 1) : list->erase(i);
620}
621
622void SearchProvider::RemoveStaleNavigationResults(NavigationResults* list,
[email protected]42cfd01d02013-01-23 02:40:02623 const string16& input) {
[email protected]d1f0a7f2012-06-05 10:26:42624 for (NavigationResults::iterator i = list->begin(); i < list->end();) {
625 const string16 fill(AutocompleteInput::FormattedStringWithEquivalentMeaning(
626 i->url(), StringForURLDisplay(i->url(), true, false)));
627 i = URLPrefix::BestURLPrefix(fill, input) ? (i + 1) : list->erase(i);
628 }
629}
630
631void SearchProvider::ApplyCalculatedRelevance() {
632 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true);
633 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false);
634 ApplyCalculatedNavigationRelevance(&keyword_navigation_results_, true);
635 ApplyCalculatedNavigationRelevance(&default_navigation_results_, false);
[email protected]dab8d52d2013-03-05 07:35:28636 has_default_suggested_relevance_ = false;
637 has_keyword_suggested_relevance_ = false;
638 default_verbatim_relevance_ = -1;
639 keyword_verbatim_relevance_ = -1;
[email protected]d1f0a7f2012-06-05 10:26:42640}
641
642void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list,
643 bool is_keyword) {
644 for (size_t i = 0; i < list->size(); ++i) {
645 (*list)[i].set_relevance(CalculateRelevanceForSuggestion(is_keyword) +
646 (list->size() - i - 1));
647 }
648}
649
650void SearchProvider::ApplyCalculatedNavigationRelevance(NavigationResults* list,
651 bool is_keyword) {
652 for (size_t i = 0; i < list->size(); ++i) {
653 (*list)[i].set_relevance(CalculateRelevanceForNavigation(is_keyword) +
654 (list->size() - i - 1));
655 }
656}
657
[email protected]15fb2aa2012-05-22 22:52:59658net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12659 int id,
[email protected]9ff91722012-09-07 05:29:12660 const TemplateURL* template_url,
[email protected]14710852013-02-05 23:45:41661 const AutocompleteInput& input) {
[email protected]9ff91722012-09-07 05:29:12662 if (!template_url || template_url->suggestions_url().empty())
663 return NULL;
664
665 // Bail if the suggestion URL is invalid with the given replacements.
[email protected]14710852013-02-05 23:45:41666 TemplateURLRef::SearchTermsArgs search_term_args(input.text());
667 search_term_args.cursor_position = input.cursor_position();
[email protected]9ff91722012-09-07 05:29:12668 GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms(
[email protected]00790562012-12-14 09:57:16669 search_term_args));
[email protected]9ff91722012-09-07 05:29:12670 if (!suggest_url.is_valid())
671 return NULL;
672
673 suggest_results_pending_++;
674 LogOmniboxSuggestRequest(REQUEST_SENT);
675
676 net::URLFetcher* fetcher =
677 net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12678 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34679 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]bd3b4712012-12-18 17:01:30680 // Add Chrome experiment state to the request headers.
681 net::HttpRequestHeaders headers;
[email protected]ab7780792013-01-10 01:26:09682 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
[email protected]bd3b4712012-12-18 17:01:30683 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
684 fetcher->SetExtraRequestHeaders(headers.ToString());
[email protected]257ab712009-04-14 17:16:24685 fetcher->Start();
686 return fetcher;
687}
688
[email protected]d7ad4772012-06-01 03:12:54689bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) {
690 // TODO(pkasting): Fix |have_suggest_results_|; see http://crbug.com/130631
691 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29692
[email protected]d7ad4772012-06-01 03:12:54693 string16 query;
694 ListValue* root_list = NULL;
695 ListValue* results = NULL;
[email protected]14710852013-02-05 23:45:41696 const string16& input_text =
697 is_keyword ? keyword_input_.text() : input_.text();
[email protected]d7ad4772012-06-01 03:12:54698 if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) ||
699 (query != input_text) || !root_list->GetList(1, &results))
initial.commit09911bf2008-07-26 23:55:29700 return false;
701
[email protected]55ce8f12012-05-09 04:44:08702 // 3rd element: Description list.
[email protected]d7ad4772012-06-01 03:12:54703 ListValue* descriptions = NULL;
704 root_list->GetList(2, &descriptions);
initial.commit09911bf2008-07-26 23:55:29705
[email protected]55ce8f12012-05-09 04:44:08706 // 4th element: Disregard the query URL list for now.
initial.commit09911bf2008-07-26 23:55:29707
[email protected]d1f0a7f2012-06-05 10:26:42708 // Reset suggested relevance information from the default provider.
[email protected]dab8d52d2013-03-05 07:35:28709 bool* has_suggested_relevance = is_keyword ?
710 &has_keyword_suggested_relevance_ : &has_default_suggested_relevance_;
711 *has_suggested_relevance = false;
712 int* verbatim_relevance = is_keyword ?
713 &keyword_verbatim_relevance_ : &default_verbatim_relevance_;
714 *verbatim_relevance = -1;
[email protected]d1f0a7f2012-06-05 10:26:42715
[email protected]55ce8f12012-05-09 04:44:08716 // 5th element: Optional key-value pairs from the Suggest server.
[email protected]d7ad4772012-06-01 03:12:54717 ListValue* types = NULL;
[email protected]d1f0a7f2012-06-05 10:26:42718 ListValue* relevances = NULL;
719 DictionaryValue* extras = NULL;
720 if (root_list->GetDictionary(4, &extras)) {
721 extras->GetList("google:suggesttype", &types);
722
723 // Only accept relevance suggestions if Instant is disabled.
[email protected]dab8d52d2013-03-05 07:35:28724 if (!chrome::BrowserInstantController::IsInstantEnabled(profile_)) {
[email protected]d1f0a7f2012-06-05 10:26:42725 // Discard this list if its size does not match that of the suggestions.
726 if (extras->GetList("google:suggestrelevance", &relevances) &&
727 relevances->GetSize() != results->GetSize())
728 relevances = NULL;
[email protected]dab8d52d2013-03-05 07:35:28729 extras->GetInteger("google:verbatimrelevance", verbatim_relevance);
[email protected]d1f0a7f2012-06-05 10:26:42730 }
[email protected]618d6e62012-12-16 05:55:57731
732 // Check if the active suggest field trial (if any) has triggered either
733 // for the default provider or keyword provider.
734 bool triggered = false;
735 extras->GetBoolean("google:fieldtrialtriggered", &triggered);
736 field_trial_triggered_ |= triggered;
737 field_trial_triggered_in_session_ |= triggered;
initial.commit09911bf2008-07-26 23:55:29738 }
739
[email protected]d7ad4772012-06-01 03:12:54740 SuggestResults* suggest_results =
741 is_keyword ? &keyword_suggest_results_ : &default_suggest_results_;
742 NavigationResults* navigation_results =
743 is_keyword ? &keyword_navigation_results_ : &default_navigation_results_;
initial.commit09911bf2008-07-26 23:55:29744
[email protected]d1f0a7f2012-06-05 10:26:42745 // Clear the previous results now that new results are available.
746 suggest_results->clear();
747 navigation_results->clear();
748
749 string16 result, title;
750 std::string type;
751 int relevance = -1;
[email protected]d7ad4772012-06-01 03:12:54752 for (size_t index = 0; results->GetString(index, &result); ++index) {
[email protected]8e81f5092010-09-29 23:19:40753 // Google search may return empty suggestions for weird input characters,
[email protected]55ce8f12012-05-09 04:44:08754 // they make no sense at all and can cause problems in our code.
[email protected]d7ad4772012-06-01 03:12:54755 if (result.empty())
[email protected]8e81f5092010-09-29 23:19:40756 continue;
757
[email protected]d1f0a7f2012-06-05 10:26:42758 // Apply valid suggested relevance scores; discard invalid lists.
759 if (relevances != NULL && !relevances->GetInteger(index, &relevance))
760 relevances = NULL;
[email protected]d7ad4772012-06-01 03:12:54761 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) {
[email protected]d1f0a7f2012-06-05 10:26:42762 // Do not blindly trust the URL coming from the server to be valid.
763 GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(result), std::string()));
764 if (url.is_valid()) {
765 if (descriptions != NULL)
766 descriptions->GetString(index, &title);
767 navigation_results->push_back(NavigationResult(url, title, relevance));
initial.commit09911bf2008-07-26 23:55:29768 }
769 } else {
[email protected]d1f0a7f2012-06-05 10:26:42770 // TODO(kochi): Improve calculator result presentation.
771 suggest_results->push_back(SuggestResult(result, relevance));
initial.commit09911bf2008-07-26 23:55:29772 }
773 }
774
[email protected]d1f0a7f2012-06-05 10:26:42775 // Apply calculated relevance scores if a valid list was not provided.
776 if (relevances == NULL) {
777 ApplyCalculatedSuggestRelevance(suggest_results, is_keyword);
778 ApplyCalculatedNavigationRelevance(navigation_results, is_keyword);
[email protected]dab8d52d2013-03-05 07:35:28779 } else {
780 *has_suggested_relevance = true;
[email protected]d1f0a7f2012-06-05 10:26:42781 }
782
[email protected]d7ad4772012-06-01 03:12:54783 have_suggest_results_ = true;
[email protected]d1f0a7f2012-06-05 10:26:42784 return true;
initial.commit09911bf2008-07-26 23:55:29785}
786
[email protected]344946a12012-12-20 12:03:42787void SearchProvider::ConvertResultsToAutocompleteMatches() {
initial.commit09911bf2008-07-26 23:55:29788 // Convert all the results to matches and add them to a map, so we can keep
789 // the most relevant match for each result.
790 MatchMap map;
[email protected]257ab712009-04-14 17:16:24791 const Time no_time;
792 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ?
initial.commit09911bf2008-07-26 23:55:29793 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
794 TemplateURLRef::NO_SUGGESTION_CHOSEN;
initial.commit09911bf2008-07-26 23:55:29795
[email protected]382a0642012-06-06 06:13:52796 int verbatim_relevance = GetVerbatimRelevance();
[email protected]257ab712009-04-14 17:16:24797 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
[email protected]55ce8f12012-05-09 04:44:08798 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
799 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]d1f0a7f2012-06-05 10:26:42800 if (verbatim_relevance > 0) {
801 AddMatchToMap(input_.text(), input_.text(), verbatim_relevance,
802 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
803 did_not_accept_default_suggestion, false, &map);
804 }
[email protected]5423e562013-02-07 03:58:45805 if (!keyword_input_.text().empty()) {
806 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
807 // We only create the verbatim search query match for a keyword
808 // if it's not an extension keyword. Extension keywords are handled
809 // in KeywordProvider::Start(). (Extensions are complicated...)
810 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond
811 // to the keyword verbatim search query. Do not create other matches
812 // of type SEARCH_OTHER_ENGINE.
813 if (keyword_url && !keyword_url->IsExtensionKeyword()) {
[email protected]dab8d52d2013-03-05 07:35:28814 const int keyword_verbatim_relevance = GetKeywordVerbatimRelevance();
815 if (keyword_verbatim_relevance > 0) {
816 AddMatchToMap(keyword_input_.text(), keyword_input_.text(),
817 keyword_verbatim_relevance,
818 AutocompleteMatch::SEARCH_OTHER_ENGINE,
819 did_not_accept_keyword_suggestion, true, &map);
820 }
[email protected]5423e562013-02-07 03:58:45821 }
822 }
823 const size_t verbatim_matches_size = map.size();
[email protected]93b73832012-10-18 20:18:38824 if (!default_provider_suggestion_.text.empty() &&
[email protected]2ac15ba2013-01-14 22:38:59825 default_provider_suggestion_.type == INSTANT_SUGGESTION_SEARCH &&
826 !input_.prevent_inline_autocomplete())
[email protected]93b73832012-10-18 20:18:38827 AddMatchToMap(input_.text() + default_provider_suggestion_.text,
[email protected]55ce8f12012-05-09 04:44:08828 input_.text(), verbatim_relevance + 1,
[email protected]85b8d6f2012-05-08 20:53:47829 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08830 did_not_accept_default_suggestion, false, &map);
initial.commit09911bf2008-07-26 23:55:29831
[email protected]257ab712009-04-14 17:16:24832 AddHistoryResultsToMap(keyword_history_results_, true,
833 did_not_accept_keyword_suggestion, &map);
834 AddHistoryResultsToMap(default_history_results_, false,
835 did_not_accept_default_suggestion, &map);
836
[email protected]55ce8f12012-05-09 04:44:08837 AddSuggestResultsToMap(keyword_suggest_results_, true, &map);
838 AddSuggestResultsToMap(default_suggest_results_, false, &map);
initial.commit09911bf2008-07-26 23:55:29839
840 // Now add the most relevant matches from the map to |matches_|.
841 matches_.clear();
842 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
843 matches_.push_back(i->second);
844
[email protected]93b73832012-10-18 20:18:38845 if (!default_provider_suggestion_.text.empty() &&
[email protected]2ac15ba2013-01-14 22:38:59846 default_provider_suggestion_.type == INSTANT_SUGGESTION_URL &&
[email protected]90fe2bb2013-01-15 03:42:13847 !input_.prevent_inline_autocomplete()) {
848 // See comment in FinalizeInstantQuery() for why we don't use
849 // |verbatim_relevance| here.
[email protected]93b73832012-10-18 20:18:38850 matches_.push_back(NavigationToMatch(
851 NavigationResult(GURL(UTF16ToUTF8(default_provider_suggestion_.text)),
852 string16(),
[email protected]90fe2bb2013-01-15 03:42:13853 kNonURLVerbatimRelevance + 1),
[email protected]93b73832012-10-18 20:18:38854 false));
[email protected]90fe2bb2013-01-15 03:42:13855 }
[email protected]257ab712009-04-14 17:16:24856 AddNavigationResultsToMatches(keyword_navigation_results_, true);
857 AddNavigationResultsToMatches(default_navigation_results_, false);
initial.commit09911bf2008-07-26 23:55:29858
[email protected]5423e562013-02-07 03:58:45859 // Allow additional match(es) for verbatim results if present.
860 const size_t max_total_matches = kMaxMatches + verbatim_matches_size;
initial.commit09911bf2008-07-26 23:55:29861 std::partial_sort(matches_.begin(),
862 matches_.begin() + std::min(max_total_matches, matches_.size()),
863 matches_.end(), &AutocompleteMatch::MoreRelevant);
[email protected]3723e6e2012-06-11 21:06:56864
865 // If the top match is effectively 'verbatim' but exceeds the calculated
866 // verbatim relevance, and REQUESTED_URL |input_| has a |desired_tld|
867 // (for example ".com" when the CTRL key is pressed for REQUESTED_URL input),
868 // promote a URL_WHAT_YOU_TYPED match to the top. Otherwise, these matches can
869 // stomp the HistoryURLProvider's similar transient URL_WHAT_YOU_TYPED match,
870 // and CTRL+ENTER will invoke the search instead of the expected navigation.
[email protected]dab8d52d2013-03-05 07:35:28871 if ((has_default_suggested_relevance_ || default_verbatim_relevance_ >= 0 ||
872 has_keyword_suggested_relevance_ || keyword_verbatim_relevance_ >= 0) &&
[email protected]3723e6e2012-06-11 21:06:56873 input_.type() == AutocompleteInput::REQUESTED_URL &&
874 !input_.desired_tld().empty() && !matches_.empty() &&
875 matches_.front().relevance > CalculateRelevanceForVerbatim() &&
876 matches_.front().fill_into_edit == input_.text()) {
877 AutocompleteMatch match = HistoryURLProvider::SuggestExactInput(
878 this, input_, !HasHTTPScheme(input_.text()));
879 match.relevance = matches_.front().relevance + 1;
880 matches_.insert(matches_.begin(), match);
881 }
882
initial.commit09911bf2008-07-26 23:55:29883 if (matches_.size() > max_total_matches)
[email protected]344946a12012-12-20 12:03:42884 matches_.resize(max_total_matches);
885}
886
887bool SearchProvider::IsTopMatchScoreTooLow() const {
[email protected]dab8d52d2013-03-05 07:35:28888 // Here we use CalculateRelevanceForVerbatimIgnoringKeywordModeState()
889 // rather than CalculateRelevanceForVerbatim() because the latter returns
890 // a very low score (250) if keyword mode is active. This is because
891 // when keyword mode is active the user probably wants the keyword matches,
892 // not matches from the default provider. Hence, we use the version of
893 // the function that ignores whether keyword mode is active. This allows
894 // SearchProvider to maintain its contract with the AutocompleteController
895 // that it will always provide an inlineable match with a reasonable
896 // score.
897 return matches_.front().relevance <
898 CalculateRelevanceForVerbatimIgnoringKeywordModeState();
[email protected]344946a12012-12-20 12:03:42899}
900
901bool SearchProvider::IsTopMatchHighRankSearchForURL() const {
902 return input_.type() == AutocompleteInput::URL &&
903 matches_.front().relevance > CalculateRelevanceForVerbatim() &&
904 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST ||
[email protected]5423e562013-02-07 03:58:45905 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED ||
906 matches_.front().type == AutocompleteMatch::SEARCH_OTHER_ENGINE);
[email protected]344946a12012-12-20 12:03:42907}
908
909bool SearchProvider::IsTopMatchNotInlinable() const {
[email protected]5423e562013-02-07 03:58:45910 // Note: this test assumes the SEARCH_OTHER_ENGINE match corresponds to
911 // the verbatim search query on the keyword engine. SearchProvider should
912 // not create any other match of type SEARCH_OTHER_ENGINE.
[email protected]344946a12012-12-20 12:03:42913 return matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED &&
914 matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED &&
[email protected]5423e562013-02-07 03:58:45915 matches_.front().type != AutocompleteMatch::SEARCH_OTHER_ENGINE &&
[email protected]344946a12012-12-20 12:03:42916 matches_.front().inline_autocomplete_offset == string16::npos &&
917 matches_.front().fill_into_edit != input_.text();
918}
919
920void SearchProvider::UpdateMatches() {
921 ConvertResultsToAutocompleteMatches();
initial.commit09911bf2008-07-26 23:55:29922
[email protected]382a0642012-06-06 06:13:52923 // Check constraints that may be violated by suggested relevances.
[email protected]344946a12012-12-20 12:03:42924 if (!matches_.empty() &&
[email protected]dab8d52d2013-03-05 07:35:28925 (has_default_suggested_relevance_ || default_verbatim_relevance_ >= 0 ||
926 has_keyword_suggested_relevance_ || keyword_verbatim_relevance_ >= 0)) {
927 // These two blocks attempt to repair undesirable behavior by suggested
[email protected]344946a12012-12-20 12:03:42928 // relevances with minimal impact, preserving other suggested relevances.
929 if (IsTopMatchScoreTooLow()) {
[email protected]1beee342012-06-19 22:22:28930 // Disregard the suggested verbatim relevance if the top score is below
931 // the usual verbatim value. For example, a BarProvider may rely on
932 // SearchProvider's verbatim or inlineable matches for input "foo" to
933 // always outrank its own lowly-ranked non-inlineable "bar" match.
[email protected]dab8d52d2013-03-05 07:35:28934 default_verbatim_relevance_ = -1;
935 keyword_verbatim_relevance_ = -1;
[email protected]344946a12012-12-20 12:03:42936 ConvertResultsToAutocompleteMatches();
[email protected]e6acd002012-06-16 22:27:47937 }
[email protected]344946a12012-12-20 12:03:42938 if (IsTopMatchHighRankSearchForURL()) {
[email protected]e6acd002012-06-16 22:27:47939 // Disregard the suggested search and verbatim relevances if the input
940 // type is URL and the top match is a highly-ranked search suggestion.
941 // For example, prevent a search for "foo.com" from outranking another
942 // provider's navigation for "foo.com" or "foo.com/url_from_history".
[email protected]e6acd002012-06-16 22:27:47943 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true);
944 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false);
[email protected]dab8d52d2013-03-05 07:35:28945 default_verbatim_relevance_ = -1;
946 keyword_verbatim_relevance_ = -1;
[email protected]344946a12012-12-20 12:03:42947 ConvertResultsToAutocompleteMatches();
[email protected]382a0642012-06-06 06:13:52948 }
[email protected]344946a12012-12-20 12:03:42949 if (IsTopMatchNotInlinable()) {
[email protected]5423e562013-02-07 03:58:45950 // Disregard suggested relevances if the top match is not a verbatim
951 // match, inlinable, or URL_WHAT_YOU_TYPED (which may be top match
952 // regardless of inlining). For example, input "foo" should not
953 // invoke a search for "bar", which would happen if the "bar" search
954 // match outranked all other matches.
[email protected]344946a12012-12-20 12:03:42955 ApplyCalculatedRelevance();
956 ConvertResultsToAutocompleteMatches();
[email protected]382a0642012-06-06 06:13:52957 }
[email protected]344946a12012-12-20 12:03:42958 DCHECK(!IsTopMatchScoreTooLow());
959 DCHECK(!IsTopMatchHighRankSearchForURL());
960 DCHECK(!IsTopMatchNotInlinable());
[email protected]d1f0a7f2012-06-05 10:26:42961 }
962
[email protected]cc63dea2008-08-21 20:56:31963 UpdateStarredStateOfMatches();
[email protected]4ab4c7c2010-11-24 04:49:34964 UpdateDone();
[email protected]257ab712009-04-14 17:16:24965}
966
967void SearchProvider::AddNavigationResultsToMatches(
968 const NavigationResults& navigation_results,
969 bool is_keyword) {
[email protected]c112f0fe72013-01-18 20:30:09970 if (navigation_results.empty())
971 return;
972
[email protected]dab8d52d2013-03-05 07:35:28973 if (is_keyword ?
974 has_keyword_suggested_relevance_ : has_default_suggested_relevance_) {
[email protected]c112f0fe72013-01-18 20:30:09975 for (NavigationResults::const_iterator it = navigation_results.begin();
976 it != navigation_results.end(); ++it)
977 matches_.push_back(NavigationToMatch(*it, is_keyword));
978 } else {
979 // Pick one element only in absence of the suggested relevance scores.
[email protected]6c535842012-05-15 05:20:55980 // TODO(kochi|msw): Add more navigational results if they get more
981 // meaningful relevance values; see http://b/1170574.
[email protected]d7ad4772012-06-01 03:12:54982 // CompareScoredResults sorts by descending relevance; so use min_element.
[email protected]6c535842012-05-15 05:20:55983 NavigationResults::const_iterator result(
[email protected]d7ad4772012-06-01 03:12:54984 std::min_element(navigation_results.begin(),
[email protected]6c535842012-05-15 05:20:55985 navigation_results.end(),
986 CompareScoredResults()));
987 matches_.push_back(NavigationToMatch(*result, is_keyword));
[email protected]257ab712009-04-14 17:16:24988 }
989}
990
991void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
992 bool is_keyword,
993 int did_not_accept_suggestion,
994 MatchMap* map) {
[email protected]51124552011-07-16 01:37:10995 if (results.empty())
996 return;
997
[email protected]d7ad4772012-06-01 03:12:54998 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
999 (input_.type() == AutocompleteInput::URL);
[email protected]14710852013-02-05 23:45:411000 const string16& input_text =
1001 is_keyword ? keyword_input_.text() : input_.text();
[email protected]51124552011-07-16 01:37:101002 bool input_multiple_words = HasMultipleWords(input_text);
1003
[email protected]55ce8f12012-05-09 04:44:081004 SuggestResults scored_results;
1005 if (!prevent_inline_autocomplete && input_multiple_words) {
1006 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
1007 // queries if the input also has multiple words. But if we were already
[email protected]51124552011-07-16 01:37:101008 // autocompleting a multi-word, multi-visit query, and the current input is
1009 // still a prefix of it, then changing the autocompletion suddenly feels
1010 // wrong. To detect this case, first score as if only one word has been
1011 // typed, then check for a best result that is an autocompleted, multi-word
1012 // query. If we find one, then just keep that score set.
[email protected]55ce8f12012-05-09 04:44:081013 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
1014 false, input_text, is_keyword);
1015 if ((scored_results[0].relevance() <
1016 AutocompleteResult::kLowestDefaultScore) ||
1017 !HasMultipleWords(scored_results[0].suggestion()))
1018 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:101019 }
[email protected]55ce8f12012-05-09 04:44:081020 if (scored_results.empty())
1021 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
1022 input_multiple_words, input_text,
1023 is_keyword);
1024 for (SuggestResults::const_iterator i(scored_results.begin());
1025 i != scored_results.end(); ++i) {
1026 AddMatchToMap(i->suggestion(), input_text, i->relevance(),
[email protected]51124552011-07-16 01:37:101027 AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion,
[email protected]55ce8f12012-05-09 04:44:081028 is_keyword, map);
[email protected]51124552011-07-16 01:37:101029 }
1030}
1031
[email protected]55ce8f12012-05-09 04:44:081032SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:101033 const HistoryResults& results,
1034 bool base_prevent_inline_autocomplete,
1035 bool input_multiple_words,
1036 const string16& input_text,
1037 bool is_keyword) {
[email protected]810ffba2012-06-12 01:07:481038 AutocompleteClassifier* classifier =
1039 AutocompleteClassifierFactory::GetForProfile(profile_);
[email protected]55ce8f12012-05-09 04:44:081040 SuggestResults scored_results;
[email protected]257ab712009-04-14 17:16:241041 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
1042 ++i) {
[email protected]51124552011-07-16 01:37:101043 // Don't autocomplete multi-word queries that have only been seen once
1044 // unless the user has typed more than one word.
1045 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
1046 (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term));
1047
[email protected]ea3b9a502011-04-04 14:19:371048 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:101049 // when typed. For example, if the user searched for "google.com" and types
1050 // "goog", don't autocomplete to the search term "google.com". Otherwise,
1051 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:481052 // NOTE: We don't check this in the following cases:
1053 // * When inline autocomplete is disabled, we won't be inline
1054 // autocompleting this term, so we don't need to worry about confusion as
1055 // much. This also prevents calling Classify() again from inside the
1056 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:101057 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:481058 // * When the user has typed the whole term, the "what you typed" history
1059 // match will outrank us for URL-like inputs anyway, so we need not do
1060 // anything special.
[email protected]51124552011-07-16 01:37:101061 if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) {
[email protected]ea3b9a502011-04-04 14:19:371062 AutocompleteMatch match;
[email protected]72874a8d2011-05-11 03:48:541063 classifier->Classify(i->term, string16(), false, false, &match, NULL);
[email protected]2905f742011-10-13 03:51:581064 prevent_inline_autocomplete =
[email protected]749e7ae02012-09-05 18:47:461065 !AutocompleteMatch::IsSearchType(match.type);
[email protected]ea3b9a502011-04-04 14:19:371066 }
[email protected]51124552011-07-16 01:37:101067
1068 int relevance = CalculateRelevanceForHistory(i->time, is_keyword,
1069 prevent_inline_autocomplete);
[email protected]55ce8f12012-05-09 04:44:081070 scored_results.push_back(SuggestResult(i->term, relevance));
[email protected]257ab712009-04-14 17:16:241071 }
[email protected]51124552011-07-16 01:37:101072
1073 // History returns results sorted for us. However, we may have docked some
1074 // results' scores, so things are no longer in order. Do a stable sort to get
1075 // things back in order without otherwise disturbing results with equal
1076 // scores, then force the scores to be unique, so that the order in which
1077 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:081078 std::stable_sort(scored_results.begin(), scored_results.end(),
1079 CompareScoredResults());
[email protected]51124552011-07-16 01:37:101080 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:081081 for (SuggestResults::iterator i(scored_results.begin());
1082 i != scored_results.end(); ++i) {
1083 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
1084 i->set_relevance(last_relevance - 1);
1085 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:101086 }
1087
[email protected]55ce8f12012-05-09 04:44:081088 return scored_results;
[email protected]257ab712009-04-14 17:16:241089}
1090
[email protected]55ce8f12012-05-09 04:44:081091void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
1092 bool is_keyword,
1093 MatchMap* map) {
[email protected]14710852013-02-05 23:45:411094 const string16& input_text =
1095 is_keyword ? keyword_input_.text() : input_.text();
[email protected]55ce8f12012-05-09 04:44:081096 for (size_t i = 0; i < results.size(); ++i) {
[email protected]d7ad4772012-06-01 03:12:541097 AddMatchToMap(results[i].suggestion(), input_text, results[i].relevance(),
[email protected]55ce8f12012-05-09 04:44:081098 AutocompleteMatch::SEARCH_SUGGEST, i, is_keyword, map);
[email protected]257ab712009-04-14 17:16:241099 }
initial.commit09911bf2008-07-26 23:55:291100}
1101
[email protected]382a0642012-06-06 06:13:521102int SearchProvider::GetVerbatimRelevance() const {
[email protected]dc6943b2012-06-19 06:39:561103 // Use the suggested verbatim relevance score if it is non-negative (valid),
1104 // if inline autocomplete isn't prevented (always show verbatim on backspace),
[email protected]1beee342012-06-19 22:22:281105 // and if it won't suppress verbatim, leaving no default provider matches.
1106 // Otherwise, if the default provider returned no matches and was still able
[email protected]dc6943b2012-06-19 06:39:561107 // to suppress verbatim, the user would have no search/nav matches and may be
[email protected]1beee342012-06-19 22:22:281108 // left unable to search using their default provider from the omnibox.
[email protected]dc6943b2012-06-19 06:39:561109 // Check for results on each verbatim calculation, as results from older
1110 // queries (on previous input) may be trimmed for failing to inline new input.
[email protected]dab8d52d2013-03-05 07:35:281111 if (default_verbatim_relevance_ >= 0 &&
1112 !input_.prevent_inline_autocomplete() &&
1113 (default_verbatim_relevance_ > 0 ||
[email protected]1beee342012-06-19 22:22:281114 !default_suggest_results_.empty() ||
[email protected]dc6943b2012-06-19 06:39:561115 !default_navigation_results_.empty())) {
[email protected]dab8d52d2013-03-05 07:35:281116 return default_verbatim_relevance_;
[email protected]dc6943b2012-06-19 06:39:561117 }
[email protected]382a0642012-06-06 06:13:521118 return CalculateRelevanceForVerbatim();
1119}
[email protected]d1f0a7f2012-06-05 10:26:421120
[email protected]382a0642012-06-06 06:13:521121int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:471122 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:361123 return 250;
[email protected]dab8d52d2013-03-05 07:35:281124 return CalculateRelevanceForVerbatimIgnoringKeywordModeState();
1125}
[email protected]52d08b12009-10-19 18:42:361126
[email protected]dab8d52d2013-03-05 07:35:281127int SearchProvider::
1128 CalculateRelevanceForVerbatimIgnoringKeywordModeState() const {
initial.commit09911bf2008-07-26 23:55:291129 switch (input_.type()) {
1130 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:361131 case AutocompleteInput::QUERY:
1132 case AutocompleteInput::FORCED_QUERY:
[email protected]90fe2bb2013-01-15 03:42:131133 return kNonURLVerbatimRelevance;
initial.commit09911bf2008-07-26 23:55:291134
1135 case AutocompleteInput::REQUESTED_URL:
[email protected]52d08b12009-10-19 18:42:361136 return 1150;
initial.commit09911bf2008-07-26 23:55:291137
1138 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:361139 return 850;
initial.commit09911bf2008-07-26 23:55:291140
1141 default:
1142 NOTREACHED();
1143 return 0;
1144 }
1145}
1146
[email protected]dab8d52d2013-03-05 07:35:281147int SearchProvider::GetKeywordVerbatimRelevance() const {
1148 // Use the suggested verbatim relevance score if it is non-negative (valid),
1149 // if inline autocomplete isn't prevented (always show verbatim on backspace),
1150 // and if it won't suppress verbatim, leaving no keyword provider matches.
1151 // Otherwise, if the keyword provider returned no matches and was still able
1152 // to suppress verbatim, the user would have no search/nav matches and may be
1153 // left unable to search using their keyword provider from the omnibox.
1154 // Check for results on each verbatim calculation, as results from older
1155 // queries (on previous input) may be trimmed for failing to inline new input.
1156 if (keyword_verbatim_relevance_ >= 0 &&
1157 !input_.prevent_inline_autocomplete() &&
1158 (keyword_verbatim_relevance_ > 0 ||
1159 !keyword_suggest_results_.empty() ||
1160 !keyword_navigation_results_.empty())) {
1161 return keyword_verbatim_relevance_;
1162 }
1163 return CalculateRelevanceForKeywordVerbatim(
1164 keyword_input_.type(), keyword_input_.prefer_keyword());
1165}
1166
[email protected]5423e562013-02-07 03:58:451167// static
1168int SearchProvider::CalculateRelevanceForKeywordVerbatim(
1169 AutocompleteInput::Type type,
1170 bool prefer_keyword) {
1171 // This function is responsible for scoring verbatim query matches
1172 // for non-extension keywords. KeywordProvider::CalculateRelevance()
1173 // scores verbatim query matches for extension keywords, as well as
1174 // for keyword matches (i.e., suggestions of a keyword itself, not a
1175 // suggestion of a query on a keyword search engine). These two
1176 // functions are currently in sync, but there's no reason we
1177 // couldn't decide in the future to score verbatim matches
1178 // differently for extension and non-extension keywords. If you
1179 // make such a change, however, you should update this comment to
1180 // describe it, so it's clear why the functions diverge.
1181 if (prefer_keyword)
1182 return 1500;
1183 return (type == AutocompleteInput::QUERY) ? 1450 : 1100;
1184}
1185
[email protected]51124552011-07-16 01:37:101186int SearchProvider::CalculateRelevanceForHistory(
1187 const Time& time,
1188 bool is_keyword,
1189 bool prevent_inline_autocomplete) const {
[email protected]aa613d62010-11-09 20:40:181190 // The relevance of past searches falls off over time. There are two distinct
1191 // equations used. If the first equation is used (searches to the primary
[email protected]51124552011-07-16 01:37:101192 // provider that we want to inline autocomplete), the score starts at 1399 and
1193 // falls to 1300. If the second equation is used the relevance of a search 15
1194 // minutes ago is discounted 50 points, while the relevance of a search two
1195 // weeks ago is discounted 450 points.
[email protected]aa613d62010-11-09 20:40:181196 double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.);
[email protected]51124552011-07-16 01:37:101197 bool is_primary_provider = providers_.is_primary_provider(is_keyword);
1198 if (is_primary_provider && !prevent_inline_autocomplete) {
[email protected]aa613d62010-11-09 20:40:181199 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:101200 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:181201 if (elapsed_time < autocomplete_time) {
[email protected]e17511f2011-07-13 14:09:181202 return (is_keyword ? 1599 : 1399) - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:181203 std::pow(elapsed_time / autocomplete_time, 2.5));
1204 }
1205 elapsed_time -= autocomplete_time;
1206 }
1207
[email protected]c3a4bd992010-08-18 20:25:011208 const int score_discount =
1209 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:291210
[email protected]6c85aa02009-02-27 12:08:091211 // Don't let scores go below 0. Negative relevance scores are meaningful in
1212 // a different way.
initial.commit09911bf2008-07-26 23:55:291213 int base_score;
[email protected]51124552011-07-16 01:37:101214 if (is_primary_provider)
[email protected]52d08b12009-10-19 18:42:361215 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:101216 else
1217 base_score = 200;
initial.commit09911bf2008-07-26 23:55:291218 return std::max(0, base_score - score_discount);
1219}
1220
[email protected]55ce8f12012-05-09 04:44:081221int SearchProvider::CalculateRelevanceForSuggestion(bool for_keyword) const {
1222 return !providers_.is_primary_provider(for_keyword) ? 100 :
1223 ((input_.type() == AutocompleteInput::URL) ? 300 : 600);
initial.commit09911bf2008-07-26 23:55:291224}
1225
[email protected]55ce8f12012-05-09 04:44:081226int SearchProvider::CalculateRelevanceForNavigation(bool for_keyword) const {
1227 return providers_.is_primary_provider(for_keyword) ? 800 : 150;
initial.commit09911bf2008-07-26 23:55:291228}
1229
[email protected]a2fedb1e2011-01-25 15:23:361230void SearchProvider::AddMatchToMap(const string16& query_string,
1231 const string16& input_text,
initial.commit09911bf2008-07-26 23:55:291232 int relevance,
[email protected]4c1fb7ec2008-11-13 00:19:001233 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:291234 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:241235 bool is_keyword,
initial.commit09911bf2008-07-26 23:55:291236 MatchMap* map) {
[email protected]92513682011-09-01 06:16:521237 AutocompleteMatch match(this, relevance, false, type);
initial.commit09911bf2008-07-26 23:55:291238 std::vector<size_t> content_param_offsets;
[email protected]85b8d6f2012-05-08 20:53:471239 // Bail out now if we don't actually have a valid provider.
1240 match.keyword = is_keyword ?
[email protected]3954c3a2012-04-10 20:17:551241 providers_.keyword_provider() : providers_.default_provider();
[email protected]dbff446582012-10-30 00:20:261242 const TemplateURL* provider_url = match.GetTemplateURL(profile_, false);
[email protected]85b8d6f2012-05-08 20:53:471243 if (provider_url == NULL)
1244 return;
1245
[email protected]70833262011-01-05 23:40:441246 match.contents.assign(query_string);
[email protected]fb5153c52009-07-31 19:40:331247 // We do intra-string highlighting for suggestions - the suggested segment
1248 // will be highlighted, e.g. for input_text = "you" the suggestion may be
1249 // "youtube", so we'll bold the "tube" section: you*tube*.
1250 if (input_text != query_string) {
[email protected]fb5153c52009-07-31 19:40:331251 size_t input_position = match.contents.find(input_text);
[email protected]a2fedb1e2011-01-25 15:23:361252 if (input_position == string16::npos) {
[email protected]fb5153c52009-07-31 19:40:331253 // The input text is not a substring of the query string, e.g. input
1254 // text is "slasdot" and the query string is "slashdot", so we bold the
1255 // whole thing.
1256 match.contents_class.push_back(
1257 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]ec2379162009-06-09 23:58:171258 } else {
[email protected]fb5153c52009-07-31 19:40:331259 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
1260 // "bold" this. Consider modifying the terminology.
1261 // We don't iterate over the string here annotating all matches because
1262 // it looks odd to have every occurrence of a substring that may be as
1263 // short as a single character highlighted in a query suggestion result,
1264 // e.g. for input text "s" and query string "southwest airlines", it
1265 // looks odd if both the first and last s are highlighted.
1266 if (input_position != 0) {
1267 match.contents_class.push_back(
1268 ACMatchClassification(0, ACMatchClassification::NONE));
1269 }
1270 match.contents_class.push_back(
1271 ACMatchClassification(input_position, ACMatchClassification::DIM));
1272 size_t next_fragment_position = input_position + input_text.length();
1273 if (next_fragment_position < query_string.length()) {
1274 match.contents_class.push_back(
1275 ACMatchClassification(next_fragment_position,
1276 ACMatchClassification::NONE));
1277 }
[email protected]ec2379162009-06-09 23:58:171278 }
initial.commit09911bf2008-07-26 23:55:291279 } else {
[email protected]fb5153c52009-07-31 19:40:331280 // Otherwise, we're dealing with the "default search" result which has no
[email protected]70833262011-01-05 23:40:441281 // completion.
[email protected]fb5153c52009-07-31 19:40:331282 match.contents_class.push_back(
1283 ACMatchClassification(0, ACMatchClassification::NONE));
initial.commit09911bf2008-07-26 23:55:291284 }
1285
1286 // When the user forced a query, we need to make sure all the fill_into_edit
1287 // values preserve that property. Otherwise, if the user starts editing a
1288 // suggestion, non-Search results will suddenly appear.
[email protected]8f2249b2012-08-29 02:27:241289 if (input_.type() == AutocompleteInput::FORCED_QUERY)
[email protected]a2fedb1e2011-01-25 15:23:361290 match.fill_into_edit.assign(ASCIIToUTF16("?"));
[email protected]8f2249b2012-08-29 02:27:241291 if (is_keyword)
[email protected]033f3422012-03-13 21:24:181292 match.fill_into_edit.append(match.keyword + char16(' '));
[email protected]8f2249b2012-08-29 02:27:241293 if (!input_.prevent_inline_autocomplete() &&
1294 StartsWith(query_string, input_text, false)) {
1295 match.inline_autocomplete_offset =
1296 match.fill_into_edit.length() + input_text.length();
[email protected]c0048b42009-05-04 21:47:171297 }
initial.commit09911bf2008-07-26 23:55:291298 match.fill_into_edit.append(query_string);
initial.commit09911bf2008-07-26 23:55:291299
[email protected]85b8d6f2012-05-08 20:53:471300 const TemplateURLRef& search_url = provider_url->url_ref();
[email protected]360ba052012-04-04 17:26:131301 DCHECK(search_url.SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:041302 match.search_terms_args.reset(
1303 new TemplateURLRef::SearchTermsArgs(query_string));
1304 match.search_terms_args->original_query = input_text;
1305 match.search_terms_args->accepted_suggestion = accepted_suggestion;
1306 // This is the destination URL sans assisted query stats. This must be set
1307 // so the AutocompleteController can properly de-dupe; the controller will
1308 // eventually overwrite it before it reaches the user.
1309 match.destination_url =
1310 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get()));
initial.commit09911bf2008-07-26 23:55:291311
1312 // Search results don't look like URLs.
[email protected]2905f742011-10-13 03:51:581313 match.transition = is_keyword ?
1314 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED;
initial.commit09911bf2008-07-26 23:55:291315
1316 // Try to add |match| to |map|. If a match for |query_string| is already in
1317 // |map|, replace it if |match| is more relevant.
1318 // NOTE: Keep this ToLower() call in sync with url_database.cc.
1319 const std::pair<MatchMap::iterator, bool> i = map->insert(
[email protected]a2fedb1e2011-01-25 15:23:361320 std::pair<string16, AutocompleteMatch>(
[email protected]503d03872011-05-06 08:36:261321 base::i18n::ToLower(query_string), match));
initial.commit09911bf2008-07-26 23:55:291322 // NOTE: We purposefully do a direct relevance comparison here instead of
1323 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
1324 // first" rather than "items alphabetically first" when the scores are equal.
1325 // The only case this matters is when a user has results with the same score
1326 // that differ only by capitalization; because the history system returns
1327 // results sorted by recency, this means we'll pick the most recent such
1328 // result even if the precision of our relevance score is too low to
1329 // distinguish the two.
1330 if (!i.second && (match.relevance > i.first->second.relevance))
1331 i.first->second = match;
1332}
1333
1334AutocompleteMatch SearchProvider::NavigationToMatch(
1335 const NavigationResult& navigation,
[email protected]257ab712009-04-14 17:16:241336 bool is_keyword) {
[email protected]14710852013-02-05 23:45:411337 const string16& input = is_keyword ? keyword_input_.text() : input_.text();
[email protected]55ce8f12012-05-09 04:44:081338 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]4c1fb7ec2008-11-13 00:19:001339 AutocompleteMatch::NAVSUGGEST);
[email protected]55ce8f12012-05-09 04:44:081340 match.destination_url = navigation.url();
[email protected]371dab12012-06-01 03:23:551341
1342 // First look for the user's input inside the fill_into_edit as it would be
1343 // without trimming the scheme, so we can find matches at the beginning of the
1344 // scheme.
1345 const string16 untrimmed_fill_into_edit(
1346 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1347 StringForURLDisplay(navigation.url(), true, false)));
1348 const URLPrefix* prefix =
1349 URLPrefix::BestURLPrefix(untrimmed_fill_into_edit, input);
1350 size_t match_start = (prefix == NULL) ?
1351 untrimmed_fill_into_edit.find(input) : prefix->prefix.length();
1352 size_t inline_autocomplete_offset = (prefix == NULL) ?
1353 string16::npos : (match_start + input.length());
1354 bool trim_http = !HasHTTPScheme(input) && (!prefix || (match_start != 0));
1355
1356 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1357 // Otherwise, user edits to a suggestion would show non-Search results.
1358 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
1359 match.fill_into_edit = ASCIIToUTF16("?");
1360 if (inline_autocomplete_offset != string16::npos)
1361 ++inline_autocomplete_offset;
1362 }
1363
1364 const std::string languages(
1365 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
1366 const net::FormatUrlTypes format_types =
1367 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
1368 match.fill_into_edit +=
1369 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1370 net::FormatUrl(navigation.url(), languages, format_types,
1371 net::UnescapeRule::SPACES, NULL, NULL,
1372 &inline_autocomplete_offset));
1373 if (!input_.prevent_inline_autocomplete())
1374 match.inline_autocomplete_offset = inline_autocomplete_offset;
1375 DCHECK((match.inline_autocomplete_offset == string16::npos) ||
1376 (match.inline_autocomplete_offset <= match.fill_into_edit.length()));
1377
1378 match.contents = net::FormatUrl(navigation.url(), languages,
1379 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start);
1380 // If the first match in the untrimmed string was inside a scheme that we
1381 // trimmed, look for a subsequent match.
1382 if (match_start == string16::npos)
1383 match_start = match.contents.find(input);
1384 // Safe if |match_start| is npos; also safe if the input is longer than the
1385 // remaining contents after |match_start|.
1386 AutocompleteMatch::ClassifyLocationInString(match_start, input.length(),
1387 match.contents.length(), ACMatchClassification::URL,
1388 &match.contents_class);
initial.commit09911bf2008-07-26 23:55:291389
[email protected]55ce8f12012-05-09 04:44:081390 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551391 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1392 ACMatchClassification::NONE, &match.description_class);
initial.commit09911bf2008-07-26 23:55:291393 return match;
1394}
[email protected]4ab4c7c2010-11-24 04:49:341395
1396void SearchProvider::UpdateDone() {
[email protected]2cdf1172012-08-26 12:21:331397 // We're done when the timer isn't running, there are no suggest queries
1398 // pending, and we're not waiting on instant.
1399 done_ = (!timer_.IsRunning() && (suggest_results_pending_ == 0) &&
[email protected]b67d0a42012-09-04 20:57:351400 (instant_finalized_ ||
[email protected]249a0352012-11-26 21:06:191401 (!chrome::BrowserInstantController::IsInstantEnabled(profile_) &&
1402 !chrome::search::IsInstantExtendedAPIEnabled(profile_))));
[email protected]4ab4c7c2010-11-24 04:49:341403}