blob: 1a1ca34f6724b00e04c8fc4a7ac250e2323ffa64 [file] [log] [blame]
[email protected]ffbec692012-02-26 20:26:421// Copyright (c) 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]dc9a6762010-08-16 07:13:5317#include "base/string16.h"
[email protected]371dab12012-06-01 03:23:5518#include "base/string_util.h"
[email protected]1cb2dac2010-03-08 21:49:1519#include "base/utf_string_conversions.h"
[email protected]ea3b9a502011-04-04 14:19:3720#include "chrome/browser/autocomplete/autocomplete_classifier.h"
[email protected]f5b95ba92012-03-27 14:05:1921#include "chrome/browser/autocomplete/autocomplete_field_trial.h"
[email protected]9ac40092010-10-27 23:05:2622#include "chrome/browser/autocomplete/autocomplete_match.h"
[email protected]2c812ba02011-07-14 00:23:1523#include "chrome/browser/autocomplete/keyword_provider.h"
[email protected]371dab12012-06-01 03:23:5524#include "chrome/browser/autocomplete/url_prefix.h"
[email protected]ce560f82009-06-03 09:39:4425#include "chrome/browser/history/history.h"
[email protected]10c2d692012-05-11 05:32:2326#include "chrome/browser/history/in_memory_database.h"
[email protected]4ab4c7c2010-11-24 04:49:3427#include "chrome/browser/instant/instant_controller.h"
[email protected]f870a322009-01-16 21:47:2728#include "chrome/browser/net/url_fixer_upper.h"
[email protected]37858e52010-08-26 00:22:0229#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3330#include "chrome/browser/profiles/profile.h"
[email protected]a0ad93ea2012-05-07 22:11:5331#include "chrome/browser/search_engines/search_engine_type.h"
[email protected]8e5c89a2011-06-07 18:13:3332#include "chrome/browser/search_engines/template_url_service.h"
33#include "chrome/browser/search_engines/template_url_service_factory.h"
initial.commit09911bf2008-07-26 23:55:2934#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0235#include "chrome/common/url_constants.h"
[email protected]36aea2702011-10-26 01:12:2236#include "content/public/common/url_fetcher.h"
initial.commit09911bf2008-07-26 23:55:2937#include "googleurl/src/url_util.h"
[email protected]34ac8f32009-02-22 23:03:2738#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2939#include "net/base/escape.h"
[email protected]d3cf8682f02012-02-29 23:29:3440#include "net/base/load_flags.h"
[email protected]371dab12012-06-01 03:23:5541#include "net/base/net_util.h"
[email protected]319d9e6f2009-02-18 19:47:2142#include "net/http/http_response_headers.h"
43#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1744#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2945
[email protected]e1acf6f2008-10-27 20:43:3346using base::Time;
47using base::TimeDelta;
48
[email protected]51124552011-07-16 01:37:1049namespace {
50
51bool HasMultipleWords(const string16& text) {
52 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
53 bool found_word = false;
54 if (i.Init()) {
55 while (i.Advance()) {
56 if (i.IsWord()) {
57 if (found_word)
58 return true;
59 found_word = true;
60 }
61 }
62 }
63 return false;
64}
65
[email protected]d1f0a7f2012-06-05 10:26:4266} // namespace
[email protected]51124552011-07-16 01:37:1067
[email protected]033f3422012-03-13 21:24:1868
[email protected]3954c3a2012-04-10 20:17:5569// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:5870
[email protected]85b8d6f2012-05-08 20:53:4771SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
72 : template_url_service_(template_url_service) {
73}
74
75const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
76 return default_provider_.empty() ? NULL :
77 template_url_service_->GetTemplateURLForKeyword(default_provider_);
78}
79
80const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
81 return keyword_provider_.empty() ? NULL :
82 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:2483}
84
[email protected]3954c3a2012-04-10 20:17:5585
86// SearchProvider -------------------------------------------------------------
87
88// static
89const int SearchProvider::kDefaultProviderURLFetcherID = 1;
90// static
91const int SearchProvider::kKeywordProviderURLFetcherID = 2;
92// static
93bool SearchProvider::query_suggest_immediately_ = false;
94
[email protected]601858c02010-09-01 17:08:2095SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile)
96 : AutocompleteProvider(listener, profile, "Search"),
[email protected]85b8d6f2012-05-08 20:53:4797 providers_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]601858c02010-09-01 17:08:2098 suggest_results_pending_(0),
[email protected]d1f0a7f2012-06-05 10:26:4299 has_suggested_relevance_(false),
100 verbatim_relevance_(-1),
[email protected]8e5cc282010-12-05 18:11:39101 have_suggest_results_(false),
[email protected]4ab4c7c2010-11-24 04:49:34102 instant_finalized_(false) {
[email protected]f5b95ba92012-03-27 14:05:19103 // We use GetSuggestNumberOfGroups() as the group ID to mean "not in field
104 // trial." Field trial groups run from 0 to GetSuggestNumberOfGroups() - 1
105 // (inclusive).
106 int suggest_field_trial_group_number =
107 AutocompleteFieldTrial::GetSuggestNumberOfGroups();
108 if (AutocompleteFieldTrial::InSuggestFieldTrial()) {
109 suggest_field_trial_group_number =
110 AutocompleteFieldTrial::GetSuggestGroupNameAsNumber();
111 }
112 // Add a beacon to the logs that'll allow us to identify later what
113 // suggest field trial group a user is in. Do this by incrementing a
114 // bucket in a histogram, where the bucket represents the user's
115 // suggest group id.
116 UMA_HISTOGRAM_ENUMERATION(
117 "Omnibox.SuggestFieldTrialBeacon",
118 suggest_field_trial_group_number,
119 AutocompleteFieldTrial::GetSuggestNumberOfGroups() + 1);
[email protected]4ab4c7c2010-11-24 04:49:34120}
121
[email protected]a2fedb1e2011-01-25 15:23:36122void SearchProvider::FinalizeInstantQuery(const string16& input_text,
123 const string16& suggest_text) {
[email protected]4ab4c7c2010-11-24 04:49:34124 if (done_ || instant_finalized_)
125 return;
126
127 instant_finalized_ = true;
128 UpdateDone();
129
[email protected]e918c112010-12-08 23:03:49130 if (input_text.empty()) {
[email protected]4ab4c7c2010-11-24 04:49:34131 // We only need to update the listener if we're actually done.
132 if (done_)
133 listener_->OnProviderUpdate(false);
134 return;
135 }
136
[email protected]9e789742011-01-10 23:27:32137 default_provider_suggest_text_ = suggest_text;
138
[email protected]a2fedb1e2011-01-25 15:23:36139 string16 adjusted_input_text(input_text);
[email protected]e918c112010-12-08 23:03:49140 AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(),
141 &adjusted_input_text);
142
[email protected]a2fedb1e2011-01-25 15:23:36143 const string16 text = adjusted_input_text + suggest_text;
[email protected]9acdcdc02012-05-21 20:58:36144 bool results_updated = false;
[email protected]4ab4c7c2010-11-24 04:49:34145 // Remove any matches that are identical to |text|. We don't use the
146 // destination_url for comparison as it varies depending upon the index passed
147 // to TemplateURL::ReplaceSearchTerms.
148 for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) {
149 if (((i->type == AutocompleteMatch::SEARCH_HISTORY) ||
150 (i->type == AutocompleteMatch::SEARCH_SUGGEST)) &&
151 (i->fill_into_edit == text)) {
[email protected]e030de62010-11-24 05:41:19152 i = matches_.erase(i);
[email protected]9acdcdc02012-05-21 20:58:36153 results_updated = true;
[email protected]4ab4c7c2010-11-24 04:49:34154 } else {
155 ++i;
156 }
157 }
158
[email protected]55ce8f12012-05-09 04:44:08159 // Add the new instant suggest result. We give it a rank higher than
[email protected]4ab4c7c2010-11-24 04:49:34160 // SEARCH_WHAT_YOU_TYPED so that it gets autocompleted.
161 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
162 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
163 TemplateURLRef::NO_SUGGESTION_CHOSEN;
164 MatchMap match_map;
[email protected]382a0642012-06-06 06:13:52165 AddMatchToMap(text, adjusted_input_text, GetVerbatimRelevance() + 1,
[email protected]4ab4c7c2010-11-24 04:49:34166 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08167 did_not_accept_default_suggestion, false, &match_map);
[email protected]9acdcdc02012-05-21 20:58:36168 if (!match_map.empty()) {
169 matches_.push_back(match_map.begin()->second);
170 results_updated = true;
171 }
[email protected]4ab4c7c2010-11-24 04:49:34172
[email protected]9acdcdc02012-05-21 20:58:36173 if (results_updated || done_)
174 listener_->OnProviderUpdate(results_updated);
[email protected]601858c02010-09-01 17:08:20175}
176
initial.commit09911bf2008-07-26 23:55:29177void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27178 bool minimal_changes) {
initial.commit09911bf2008-07-26 23:55:29179 matches_.clear();
180
[email protected]ea3b9a502011-04-04 14:19:37181 instant_finalized_ =
182 (input.matches_requested() != AutocompleteInput::ALL_MATCHES);
[email protected]4ab4c7c2010-11-24 04:49:34183
[email protected]6c85aa02009-02-27 12:08:09184 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29185 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
186 Stop();
187 return;
188 }
189
[email protected]257ab712009-04-14 17:16:24190 keyword_input_text_.clear();
191 const TemplateURL* keyword_provider =
192 KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input,
193 &keyword_input_text_);
[email protected]8d457132010-11-04 18:13:40194 if (keyword_input_text_.empty())
[email protected]257ab712009-04-14 17:16:24195 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24196
[email protected]85b8d6f2012-05-08 20:53:47197 TemplateURLService* model = providers_.template_url_service();
198 DCHECK(model);
199 model->Load();
200 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]9b74ab52012-03-30 16:08:07201 if (default_provider && !default_provider->SupportsReplacement())
[email protected]257ab712009-04-14 17:16:24202 default_provider = NULL;
203
204 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18205 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24206
207 if (!default_provider && !keyword_provider) {
208 // No valid providers.
initial.commit09911bf2008-07-26 23:55:29209 Stop();
210 return;
211 }
212
213 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24214 // or the providers, abort the query.
[email protected]85b8d6f2012-05-08 20:53:47215 string16 default_provider_keyword(default_provider ?
216 default_provider->keyword() : string16());
217 string16 keyword_provider_keyword(keyword_provider ?
218 keyword_provider->keyword() : string16());
[email protected]9e789742011-01-10 23:27:32219 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47220 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]9e789742011-01-10 23:27:32221 if (done_)
222 default_provider_suggest_text_.clear();
223 else
224 Stop();
[email protected]257ab712009-04-14 17:16:24225 }
initial.commit09911bf2008-07-26 23:55:29226
[email protected]85b8d6f2012-05-08 20:53:47227 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29228
229 if (input.text().empty()) {
230 // User typed "?" alone. Give them a placeholder result indicating what
231 // this syntax does.
[email protected]257ab712009-04-14 17:16:24232 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00233 AutocompleteMatch match;
234 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36235 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24236 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35237 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47238 match.keyword = providers_.default_provider();
[email protected]257ab712009-04-14 17:16:24239 matches_.push_back(match);
240 }
initial.commit09911bf2008-07-26 23:55:29241 Stop();
242 return;
243 }
244
245 input_ = input;
246
[email protected]8d457132010-11-04 18:13:40247 DoHistoryQuery(minimal_changes);
[email protected]8deeb952008-10-09 18:21:27248 StartOrStopSuggestQuery(minimal_changes);
initial.commit09911bf2008-07-26 23:55:29249 ConvertResultsToAutocompleteMatches();
250}
251
[email protected]55ce8f12012-05-09 04:44:08252SearchProvider::Result::Result(int relevance) : relevance_(relevance) {}
253SearchProvider::Result::~Result() {}
254
255SearchProvider::SuggestResult::SuggestResult(const string16& suggestion,
256 int relevance)
257 : Result(relevance),
258 suggestion_(suggestion) {
259}
260
261SearchProvider::SuggestResult::~SuggestResult() {}
262
263SearchProvider::NavigationResult::NavigationResult(const GURL& url,
264 const string16& description,
265 int relevance)
266 : Result(relevance),
267 url_(url),
268 description_(description) {
269 DCHECK(url_.is_valid());
270}
271
272SearchProvider::NavigationResult::~NavigationResult() {}
273
274class SearchProvider::CompareScoredResults {
[email protected]51124552011-07-16 01:37:10275 public:
[email protected]55ce8f12012-05-09 04:44:08276 bool operator()(const Result& a, const Result& b) {
[email protected]51124552011-07-16 01:37:10277 // Sort in descending relevance order.
[email protected]55ce8f12012-05-09 04:44:08278 return a.relevance() > b.relevance();
[email protected]51124552011-07-16 01:37:10279 }
280};
281
initial.commit09911bf2008-07-26 23:55:29282void SearchProvider::Run() {
283 // Start a new request with the current input.
284 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24285 suggest_results_pending_ = 0;
[email protected]a0ad93ea2012-05-07 22:11:53286 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]85b8d6f2012-05-08 20:53:47287 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
288 if (default_url && !default_url->suggestions_url().empty()) {
[email protected]257ab712009-04-14 17:16:24289 suggest_results_pending_++;
[email protected]033f3422012-03-13 21:24:18290 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
[email protected]85b8d6f2012-05-08 20:53:47291 default_url->suggestions_url_ref(), input_.text()));
[email protected]3954c3a2012-04-10 20:17:55292 }
[email protected]85b8d6f2012-05-08 20:53:47293 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
294 if (keyword_url && !keyword_url->suggestions_url().empty()) {
[email protected]3954c3a2012-04-10 20:17:55295 suggest_results_pending_++;
296 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
[email protected]85b8d6f2012-05-08 20:53:47297 keyword_url->suggestions_url_ref(), keyword_input_text_));
[email protected]257ab712009-04-14 17:16:24298 }
[email protected]85b8d6f2012-05-08 20:53:47299
300 // Both the above can fail if the providers have been modified or deleted
301 // since the query began.
302 if (suggest_results_pending_ == 0) {
303 UpdateDone();
304 // We only need to update the listener if we're actually done.
305 if (done_)
306 listener_->OnProviderUpdate(false);
307 }
initial.commit09911bf2008-07-26 23:55:29308}
309
310void SearchProvider::Stop() {
initial.commit09911bf2008-07-26 23:55:29311 StopSuggest();
initial.commit09911bf2008-07-26 23:55:29312 done_ = true;
[email protected]9e789742011-01-10 23:27:32313 default_provider_suggest_text_.clear();
initial.commit09911bf2008-07-26 23:55:29314}
315
[email protected]0e9e8782012-05-15 23:01:51316void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
317 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
318 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
319 new_entry.set_provider(AsOmniboxEventProviderType());
320 new_entry.set_provider_done(done_);
321}
322
[email protected]10c2d692012-05-11 05:32:23323void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) {
initial.commit09911bf2008-07-26 23:55:29324 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24325 suggest_results_pending_--;
[email protected]1cb2dac2010-03-08 21:49:15326 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06327 const net::HttpResponseHeaders* const response_headers =
[email protected]7cc6e5632011-10-25 17:56:12328 source->GetResponseHeaders();
[email protected]c530c852011-10-24 18:18:34329 std::string json_data;
330 source->GetResponseAsString(&json_data);
[email protected]6c85aa02009-02-27 12:08:09331 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
332 // files in non-UTF-8 encodings. The actual encoding is usually specified in
333 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06334 if (response_headers) {
335 std::string charset;
336 if (response_headers->GetCharset(&charset)) {
[email protected]a2fedb1e2011-01-25 15:23:36337 string16 data_16;
[email protected]ec9207d32008-09-26 00:51:06338 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]c530c852011-10-24 18:18:34339 if (base::CodepageToUTF16(json_data, charset.c_str(),
[email protected]a2fedb1e2011-01-25 15:23:36340 base::OnStringConversionError::FAIL,
341 &data_16))
342 json_data = UTF16ToUTF8(data_16);
[email protected]ec9207d32008-09-26 00:51:06343 }
344 }
345
[email protected]d7ad4772012-06-01 03:12:54346 const bool is_keyword = (source == keyword_fetcher_.get());
[email protected]013e9a02012-05-18 20:27:10347 const bool request_succeeded =
348 source->GetStatus().is_success() && source->GetResponseCode() == 200;
[email protected]a0ad93ea2012-05-07 22:11:53349
350 // Record response time for suggest requests sent to Google. We care
351 // only about the common case: the Google default provider used in
352 // non-keyword mode.
[email protected]85b8d6f2012-05-08 20:53:47353 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
[email protected]55ce8f12012-05-09 04:44:08354 if (!is_keyword && default_url &&
[email protected]85b8d6f2012-05-08 20:53:47355 (default_url->prepopulate_id() == SEARCH_ENGINE_GOOGLE)) {
[email protected]013e9a02012-05-18 20:27:10356 const base::TimeDelta elapsed_time =
357 base::TimeTicks::Now() - time_suggest_request_sent_;
358 if (request_succeeded) {
359 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
360 elapsed_time);
361 } else {
362 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
363 elapsed_time);
364 }
[email protected]b4cebf82008-12-29 19:59:08365 }
366
[email protected]d7ad4772012-06-01 03:12:54367 bool results_updated = false;
368 if (request_succeeded) {
369 JSONStringValueSerializer deserializer(json_data);
370 deserializer.set_allow_trailing_comma(true);
371 scoped_ptr<Value> data(deserializer.Deserialize(NULL, NULL));
372 results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword);
373 }
374
initial.commit09911bf2008-07-26 23:55:29375 ConvertResultsToAutocompleteMatches();
[email protected]d7ad4772012-06-01 03:12:54376 if (done_ || results_updated)
377 listener_->OnProviderUpdate(results_updated);
initial.commit09911bf2008-07-26 23:55:29378}
379
[email protected]601858c02010-09-01 17:08:20380SearchProvider::~SearchProvider() {
381}
382
[email protected]8d457132010-11-04 18:13:40383void SearchProvider::DoHistoryQuery(bool minimal_changes) {
384 // The history query results are synchronous, so if minimal_changes is true,
385 // we still have the last results and don't need to do anything.
386 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29387 return;
388
[email protected]8d457132010-11-04 18:13:40389 keyword_history_results_.clear();
390 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29391
[email protected]8d457132010-11-04 18:13:40392 HistoryService* const history_service =
393 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
394 history::URLDatabase* url_db = history_service ?
395 history_service->InMemoryDatabase() : NULL;
396 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29397 return;
398
[email protected]51124552011-07-16 01:37:10399 // Request history for both the keyword and default provider. We grab many
400 // more matches than we'll ultimately clamp to so that if there are several
401 // recent multi-word matches who scores are lowered (see
402 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
403 // matches. Note that this doesn't fix the problem entirely, but merely
404 // limits it to cases with a very large number of such multi-word matches; for
405 // now, this seems OK compared with the complexity of a real fix, which would
406 // require multiple searches and tracking of "single- vs. multi-word" in the
407 // database.
408 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47409 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
410 if (default_url) {
411 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
412 num_matches, &default_history_results_);
[email protected]257ab712009-04-14 17:16:24413 }
[email protected]85b8d6f2012-05-08 20:53:47414 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
415 if (keyword_url) {
416 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]3954c3a2012-04-10 20:17:55417 keyword_input_text_, num_matches, &keyword_history_results_);
418 }
initial.commit09911bf2008-07-26 23:55:29419}
420
[email protected]8deeb952008-10-09 18:21:27421void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]6c85aa02009-02-27 12:08:09422 // Don't send any queries to the server until some time has elapsed after
423 // the last keypress, to avoid flooding the server with requests we are
424 // likely to end up throwing away anyway.
[email protected]02c3f6832011-11-16 18:37:40425 const int kQueryDelayMs = 200;
[email protected]6c85aa02009-02-27 12:08:09426
[email protected]83c726482008-09-10 06:36:34427 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29428 StopSuggest();
[email protected]55ce8f12012-05-09 04:44:08429 ClearResults();
initial.commit09911bf2008-07-26 23:55:29430 return;
431 }
432
433 // For the minimal_changes case, if we finished the previous query and still
434 // have its results, or are allowed to keep running it, just do that, rather
435 // than starting a new query.
436 if (minimal_changes &&
[email protected]ea3b9a502011-04-04 14:19:37437 (have_suggest_results_ ||
438 (!done_ &&
439 input_.matches_requested() == AutocompleteInput::ALL_MATCHES)))
initial.commit09911bf2008-07-26 23:55:29440 return;
441
442 // We can't keep running any previous query, so halt it.
443 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42444
445 // Remove existing results that cannot inline autocomplete the new input.
446 RemoveStaleResults();
initial.commit09911bf2008-07-26 23:55:29447
448 // We can't start a new query if we're only allowed synchronous results.
[email protected]ea3b9a502011-04-04 14:19:37449 if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES)
initial.commit09911bf2008-07-26 23:55:29450 return;
451
[email protected]257ab712009-04-14 17:16:24452 // We'll have at least one pending fetch. Set it to 1 now, but the value is
453 // correctly set in Run. As Run isn't invoked immediately we need to set this
454 // now, else we won't think we're waiting on results from the server when we
455 // really are.
456 suggest_results_pending_ = 1;
457
initial.commit09911bf2008-07-26 23:55:29458 // Kick off a timer that will start the URL fetch if it completes before
459 // the user types another character.
[email protected]b547666d2009-04-23 16:37:58460 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs;
[email protected]d323a172011-09-02 18:23:02461 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay), this,
462 &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29463}
464
[email protected]83c726482008-09-10 06:36:34465bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55466 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
467 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47468 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
469 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34470 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47471 ((!default_url || default_url->suggestions_url().empty()) &&
472 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34473 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
474 return false;
475
[email protected]cac59d32010-08-09 23:23:14476 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34477 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34478
[email protected]cac59d32010-08-09 23:23:14479 // FORCED_QUERY means the user is explicitly asking us to search for this, so
480 // we assume it isn't a URL and/or there isn't private data.
481 if (input_.type() == AutocompleteInput::FORCED_QUERY)
482 return true;
[email protected]83c726482008-09-10 06:36:34483
[email protected]cac59d32010-08-09 23:23:14484 // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a
485 // scheme that isn't http/https/ftp, we shouldn't send it. Sending things
486 // like file: and data: is both a waste of time and a disclosure of
487 // potentially private, local data. Other "schemes" may actually be
488 // usernames, and we don't want to send passwords. If the scheme is OK, we
489 // still need to check other cases below. If this is QUERY, then the presence
490 // of these schemes means the user explicitly typed one, and thus this is
491 // probably a URL that's being entered and happens to currently be invalid --
492 // in which case we again want to run our checks below. Other QUERY cases are
493 // less likely to be URLs and thus we assume we're OK.
[email protected]a2fedb1e2011-01-25 15:23:36494 if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) &&
495 !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
496 !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme))
[email protected]cac59d32010-08-09 23:23:14497 return (input_.type() == AutocompleteInput::QUERY);
498
499 // Don't send URLs with usernames, queries or refs. Some of these are
500 // private, and the Suggest server is unlikely to have any useful results
501 // for any of them. Also don't send URLs with ports, as we may initially
502 // think that a username + password is a host + port (and we don't want to
503 // send usernames/passwords), and even if the port really is a port, the
504 // server is once again unlikely to have and useful results.
505 const url_parse::Parsed& parts = input_.parts();
506 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
507 parts.query.is_nonempty() || parts.ref.is_nonempty())
508 return false;
509
510 // Don't send anything for https except the hostname. Hostnames are OK
511 // because they are visible when the TCP connection is established, but the
512 // specific path may reveal private information.
[email protected]a2fedb1e2011-01-25 15:23:36513 if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
514 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14515 return false;
[email protected]83c726482008-09-10 06:36:34516
517 return true;
518}
519
initial.commit09911bf2008-07-26 23:55:29520void SearchProvider::StopSuggest() {
[email protected]257ab712009-04-14 17:16:24521 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14522 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24523 // Stop any in-progress URL fetches.
524 keyword_fetcher_.reset();
525 default_fetcher_.reset();
[email protected]55ce8f12012-05-09 04:44:08526}
527
528void SearchProvider::ClearResults() {
[email protected]257ab712009-04-14 17:16:24529 keyword_suggest_results_.clear();
530 default_suggest_results_.clear();
531 keyword_navigation_results_.clear();
532 default_navigation_results_.clear();
[email protected]d1f0a7f2012-06-05 10:26:42533 has_suggested_relevance_ = false;
534 verbatim_relevance_ = -1;
initial.commit09911bf2008-07-26 23:55:29535 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29536}
537
[email protected]d1f0a7f2012-06-05 10:26:42538void SearchProvider::RemoveStaleResults() {
539 RemoveStaleSuggestResults(&keyword_suggest_results_, true);
540 RemoveStaleSuggestResults(&default_suggest_results_, false);
541 RemoveStaleNavigationResults(&keyword_navigation_results_, true);
542 RemoveStaleNavigationResults(&default_navigation_results_, false);
543}
544
545void SearchProvider::RemoveStaleSuggestResults(SuggestResults* list,
546 bool is_keyword) {
547 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
548 for (SuggestResults::iterator i = list->begin(); i < list->end();)
549 i = StartsWith(i->suggestion(), input, false) ? (i + 1) : list->erase(i);
550}
551
552void SearchProvider::RemoveStaleNavigationResults(NavigationResults* list,
553 bool is_keyword) {
554 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
555 for (NavigationResults::iterator i = list->begin(); i < list->end();) {
556 const string16 fill(AutocompleteInput::FormattedStringWithEquivalentMeaning(
557 i->url(), StringForURLDisplay(i->url(), true, false)));
558 i = URLPrefix::BestURLPrefix(fill, input) ? (i + 1) : list->erase(i);
559 }
560}
561
562void SearchProvider::ApplyCalculatedRelevance() {
563 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true);
564 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false);
565 ApplyCalculatedNavigationRelevance(&keyword_navigation_results_, true);
566 ApplyCalculatedNavigationRelevance(&default_navigation_results_, false);
567 has_suggested_relevance_ = false;
568 verbatim_relevance_ = -1;
569}
570
571void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list,
572 bool is_keyword) {
573 for (size_t i = 0; i < list->size(); ++i) {
574 (*list)[i].set_relevance(CalculateRelevanceForSuggestion(is_keyword) +
575 (list->size() - i - 1));
576 }
577}
578
579void SearchProvider::ApplyCalculatedNavigationRelevance(NavigationResults* list,
580 bool is_keyword) {
581 for (size_t i = 0; i < list->size(); ++i) {
582 (*list)[i].set_relevance(CalculateRelevanceForNavigation(is_keyword) +
583 (list->size() - i - 1));
584 }
585}
586
[email protected]15fb2aa2012-05-22 22:52:59587net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12588 int id,
[email protected]3954c3a2012-04-10 20:17:55589 const TemplateURLRef& suggestions_url,
[email protected]7cc6e5632011-10-25 17:56:12590 const string16& text) {
[email protected]360ba052012-04-04 17:26:13591 DCHECK(suggestions_url.SupportsReplacement());
[email protected]d3ec669b2012-05-23 07:12:14592 net::URLFetcher* fetcher = content::URLFetcher::Create(id,
[email protected]16fca9b82012-04-23 18:40:26593 GURL(suggestions_url.ReplaceSearchTerms(text,
594 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())),
[email protected]d3ec669b2012-05-23 07:12:14595 net::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12596 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34597 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]257ab712009-04-14 17:16:24598 fetcher->Start();
599 return fetcher;
600}
601
[email protected]d7ad4772012-06-01 03:12:54602bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) {
603 // TODO(pkasting): Fix |have_suggest_results_|; see http://crbug.com/130631
604 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29605
[email protected]d7ad4772012-06-01 03:12:54606 string16 query;
607 ListValue* root_list = NULL;
608 ListValue* results = NULL;
609 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
610 if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) ||
611 (query != input_text) || !root_list->GetList(1, &results))
initial.commit09911bf2008-07-26 23:55:29612 return false;
613
[email protected]55ce8f12012-05-09 04:44:08614 // 3rd element: Description list.
[email protected]d7ad4772012-06-01 03:12:54615 ListValue* descriptions = NULL;
616 root_list->GetList(2, &descriptions);
initial.commit09911bf2008-07-26 23:55:29617
[email protected]55ce8f12012-05-09 04:44:08618 // 4th element: Disregard the query URL list for now.
initial.commit09911bf2008-07-26 23:55:29619
[email protected]d1f0a7f2012-06-05 10:26:42620 // Reset suggested relevance information from the default provider.
621 if (!is_keyword) {
622 has_suggested_relevance_ = false;
623 verbatim_relevance_ = -1;
624 }
625
[email protected]55ce8f12012-05-09 04:44:08626 // 5th element: Optional key-value pairs from the Suggest server.
[email protected]d7ad4772012-06-01 03:12:54627 ListValue* types = NULL;
[email protected]d1f0a7f2012-06-05 10:26:42628 ListValue* relevances = NULL;
629 DictionaryValue* extras = NULL;
630 if (root_list->GetDictionary(4, &extras)) {
631 extras->GetList("google:suggesttype", &types);
632
633 // Only accept relevance suggestions if Instant is disabled.
634 if (!is_keyword && !InstantController::IsEnabled(profile_)) {
635 // Discard this list if its size does not match that of the suggestions.
636 if (extras->GetList("google:suggestrelevance", &relevances) &&
637 relevances->GetSize() != results->GetSize())
638 relevances = NULL;
639
640 extras->GetInteger("google:verbatimrelevance", &verbatim_relevance_);
641 }
initial.commit09911bf2008-07-26 23:55:29642 }
643
[email protected]d7ad4772012-06-01 03:12:54644 SuggestResults* suggest_results =
645 is_keyword ? &keyword_suggest_results_ : &default_suggest_results_;
646 NavigationResults* navigation_results =
647 is_keyword ? &keyword_navigation_results_ : &default_navigation_results_;
initial.commit09911bf2008-07-26 23:55:29648
[email protected]d1f0a7f2012-06-05 10:26:42649 // Clear the previous results now that new results are available.
650 suggest_results->clear();
651 navigation_results->clear();
652
653 string16 result, title;
654 std::string type;
655 int relevance = -1;
[email protected]d7ad4772012-06-01 03:12:54656 for (size_t index = 0; results->GetString(index, &result); ++index) {
[email protected]8e81f5092010-09-29 23:19:40657 // Google search may return empty suggestions for weird input characters,
[email protected]55ce8f12012-05-09 04:44:08658 // they make no sense at all and can cause problems in our code.
[email protected]d7ad4772012-06-01 03:12:54659 if (result.empty())
[email protected]8e81f5092010-09-29 23:19:40660 continue;
661
[email protected]d1f0a7f2012-06-05 10:26:42662 // Apply valid suggested relevance scores; discard invalid lists.
663 if (relevances != NULL && !relevances->GetInteger(index, &relevance))
664 relevances = NULL;
[email protected]d7ad4772012-06-01 03:12:54665 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) {
[email protected]d1f0a7f2012-06-05 10:26:42666 // Do not blindly trust the URL coming from the server to be valid.
667 GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(result), std::string()));
668 if (url.is_valid()) {
669 if (descriptions != NULL)
670 descriptions->GetString(index, &title);
671 navigation_results->push_back(NavigationResult(url, title, relevance));
initial.commit09911bf2008-07-26 23:55:29672 }
673 } else {
[email protected]d1f0a7f2012-06-05 10:26:42674 // TODO(kochi): Improve calculator result presentation.
675 suggest_results->push_back(SuggestResult(result, relevance));
initial.commit09911bf2008-07-26 23:55:29676 }
677 }
678
[email protected]d1f0a7f2012-06-05 10:26:42679 // Apply calculated relevance scores if a valid list was not provided.
680 if (relevances == NULL) {
681 ApplyCalculatedSuggestRelevance(suggest_results, is_keyword);
682 ApplyCalculatedNavigationRelevance(navigation_results, is_keyword);
683 } else if (!is_keyword) {
684 has_suggested_relevance_ = true;
685 }
686
[email protected]d7ad4772012-06-01 03:12:54687 have_suggest_results_ = true;
[email protected]d1f0a7f2012-06-05 10:26:42688 return true;
initial.commit09911bf2008-07-26 23:55:29689}
690
691void SearchProvider::ConvertResultsToAutocompleteMatches() {
692 // Convert all the results to matches and add them to a map, so we can keep
693 // the most relevant match for each result.
694 MatchMap map;
[email protected]257ab712009-04-14 17:16:24695 const Time no_time;
696 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ?
initial.commit09911bf2008-07-26 23:55:29697 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
698 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]257ab712009-04-14 17:16:24699 // Keyword what you typed results are handled by the KeywordProvider.
initial.commit09911bf2008-07-26 23:55:29700
[email protected]382a0642012-06-06 06:13:52701 int verbatim_relevance = GetVerbatimRelevance();
[email protected]257ab712009-04-14 17:16:24702 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
[email protected]55ce8f12012-05-09 04:44:08703 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
704 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]d1f0a7f2012-06-05 10:26:42705 if (verbatim_relevance > 0) {
706 AddMatchToMap(input_.text(), input_.text(), verbatim_relevance,
707 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
708 did_not_accept_default_suggestion, false, &map);
709 }
[email protected]b9ce8532012-05-24 20:51:15710 const size_t what_you_typed_size = map.size();
[email protected]85b8d6f2012-05-08 20:53:47711 if (!default_provider_suggest_text_.empty()) {
712 AddMatchToMap(input_.text() + default_provider_suggest_text_,
[email protected]55ce8f12012-05-09 04:44:08713 input_.text(), verbatim_relevance + 1,
[email protected]85b8d6f2012-05-08 20:53:47714 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08715 did_not_accept_default_suggestion, false, &map);
initial.commit09911bf2008-07-26 23:55:29716 }
717
[email protected]257ab712009-04-14 17:16:24718 AddHistoryResultsToMap(keyword_history_results_, true,
719 did_not_accept_keyword_suggestion, &map);
720 AddHistoryResultsToMap(default_history_results_, false,
721 did_not_accept_default_suggestion, &map);
722
[email protected]55ce8f12012-05-09 04:44:08723 AddSuggestResultsToMap(keyword_suggest_results_, true, &map);
724 AddSuggestResultsToMap(default_suggest_results_, false, &map);
initial.commit09911bf2008-07-26 23:55:29725
726 // Now add the most relevant matches from the map to |matches_|.
727 matches_.clear();
728 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
729 matches_.push_back(i->second);
730
[email protected]257ab712009-04-14 17:16:24731 AddNavigationResultsToMatches(keyword_navigation_results_, true);
732 AddNavigationResultsToMatches(default_navigation_results_, false);
initial.commit09911bf2008-07-26 23:55:29733
[email protected]b9ce8532012-05-24 20:51:15734 // Allow an additional match for "what you typed" if it's present.
735 const size_t max_total_matches = kMaxMatches + what_you_typed_size;
initial.commit09911bf2008-07-26 23:55:29736 std::partial_sort(matches_.begin(),
737 matches_.begin() + std::min(max_total_matches, matches_.size()),
738 matches_.end(), &AutocompleteMatch::MoreRelevant);
739 if (matches_.size() > max_total_matches)
[email protected]a28e95662008-11-12 19:19:02740 matches_.erase(matches_.begin() + max_total_matches, matches_.end());
initial.commit09911bf2008-07-26 23:55:29741
[email protected]382a0642012-06-06 06:13:52742 // Check constraints that may be violated by suggested relevances.
[email protected]d1f0a7f2012-06-05 10:26:42743 if (!matches_.empty() &&
744 (has_suggested_relevance_ || verbatim_relevance_ >= 0) &&
745 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST ||
[email protected]382a0642012-06-06 06:13:52746 matches_.front().type == AutocompleteMatch::NAVSUGGEST)) {
747 bool reconstruct_matches = false;
748 if (matches_.front().inline_autocomplete_offset == string16::npos &&
749 matches_.front().fill_into_edit != input_.text()) {
750 // Disregard all suggested relevances if the top result is not inlinable.
751 ApplyCalculatedRelevance();
752 reconstruct_matches = true;
753 } else if (matches_.front().relevance < CalculateRelevanceForVerbatim()) {
754 // Disregard the suggested verbatim relevance if the top score is
755 // potentially lower than other providers' non-inlinable suggestions.
756 verbatim_relevance_ = -1;
757 reconstruct_matches = true;
758 }
759 if (reconstruct_matches) {
760 ConvertResultsToAutocompleteMatches();
761 return;
762 }
[email protected]d1f0a7f2012-06-05 10:26:42763 }
764
[email protected]cc63dea2008-08-21 20:56:31765 UpdateStarredStateOfMatches();
[email protected]4ab4c7c2010-11-24 04:49:34766 UpdateDone();
[email protected]257ab712009-04-14 17:16:24767}
768
769void SearchProvider::AddNavigationResultsToMatches(
770 const NavigationResults& navigation_results,
771 bool is_keyword) {
772 if (!navigation_results.empty()) {
[email protected]6c535842012-05-15 05:20:55773 // TODO(kochi|msw): Add more navigational results if they get more
774 // meaningful relevance values; see http://b/1170574.
[email protected]d7ad4772012-06-01 03:12:54775 // CompareScoredResults sorts by descending relevance; so use min_element.
[email protected]6c535842012-05-15 05:20:55776 NavigationResults::const_iterator result(
[email protected]d7ad4772012-06-01 03:12:54777 std::min_element(navigation_results.begin(),
[email protected]6c535842012-05-15 05:20:55778 navigation_results.end(),
779 CompareScoredResults()));
780 matches_.push_back(NavigationToMatch(*result, is_keyword));
[email protected]257ab712009-04-14 17:16:24781 }
782}
783
784void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
785 bool is_keyword,
786 int did_not_accept_suggestion,
787 MatchMap* map) {
[email protected]51124552011-07-16 01:37:10788 if (results.empty())
789 return;
790
[email protected]d7ad4772012-06-01 03:12:54791 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
792 (input_.type() == AutocompleteInput::URL);
793 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
[email protected]51124552011-07-16 01:37:10794 bool input_multiple_words = HasMultipleWords(input_text);
795
[email protected]55ce8f12012-05-09 04:44:08796 SuggestResults scored_results;
797 if (!prevent_inline_autocomplete && input_multiple_words) {
798 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
799 // queries if the input also has multiple words. But if we were already
[email protected]51124552011-07-16 01:37:10800 // autocompleting a multi-word, multi-visit query, and the current input is
801 // still a prefix of it, then changing the autocompletion suddenly feels
802 // wrong. To detect this case, first score as if only one word has been
803 // typed, then check for a best result that is an autocompleted, multi-word
804 // query. If we find one, then just keep that score set.
[email protected]55ce8f12012-05-09 04:44:08805 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
806 false, input_text, is_keyword);
807 if ((scored_results[0].relevance() <
808 AutocompleteResult::kLowestDefaultScore) ||
809 !HasMultipleWords(scored_results[0].suggestion()))
810 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:10811 }
[email protected]55ce8f12012-05-09 04:44:08812 if (scored_results.empty())
813 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
814 input_multiple_words, input_text,
815 is_keyword);
816 for (SuggestResults::const_iterator i(scored_results.begin());
817 i != scored_results.end(); ++i) {
818 AddMatchToMap(i->suggestion(), input_text, i->relevance(),
[email protected]51124552011-07-16 01:37:10819 AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion,
[email protected]55ce8f12012-05-09 04:44:08820 is_keyword, map);
[email protected]51124552011-07-16 01:37:10821 }
822}
823
[email protected]55ce8f12012-05-09 04:44:08824SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:10825 const HistoryResults& results,
826 bool base_prevent_inline_autocomplete,
827 bool input_multiple_words,
828 const string16& input_text,
829 bool is_keyword) {
[email protected]ea3b9a502011-04-04 14:19:37830 AutocompleteClassifier* classifier = profile_->GetAutocompleteClassifier();
[email protected]55ce8f12012-05-09 04:44:08831 SuggestResults scored_results;
[email protected]257ab712009-04-14 17:16:24832 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
833 ++i) {
[email protected]51124552011-07-16 01:37:10834 // Don't autocomplete multi-word queries that have only been seen once
835 // unless the user has typed more than one word.
836 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
837 (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term));
838
[email protected]ea3b9a502011-04-04 14:19:37839 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:10840 // when typed. For example, if the user searched for "google.com" and types
841 // "goog", don't autocomplete to the search term "google.com". Otherwise,
842 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:48843 // NOTE: We don't check this in the following cases:
844 // * When inline autocomplete is disabled, we won't be inline
845 // autocompleting this term, so we don't need to worry about confusion as
846 // much. This also prevents calling Classify() again from inside the
847 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:10848 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:48849 // * When the user has typed the whole term, the "what you typed" history
850 // match will outrank us for URL-like inputs anyway, so we need not do
851 // anything special.
[email protected]51124552011-07-16 01:37:10852 if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) {
[email protected]ea3b9a502011-04-04 14:19:37853 AutocompleteMatch match;
[email protected]72874a8d2011-05-11 03:48:54854 classifier->Classify(i->term, string16(), false, false, &match, NULL);
[email protected]2905f742011-10-13 03:51:58855 prevent_inline_autocomplete =
856 match.transition == content::PAGE_TRANSITION_TYPED;
[email protected]ea3b9a502011-04-04 14:19:37857 }
[email protected]51124552011-07-16 01:37:10858
859 int relevance = CalculateRelevanceForHistory(i->time, is_keyword,
860 prevent_inline_autocomplete);
[email protected]55ce8f12012-05-09 04:44:08861 scored_results.push_back(SuggestResult(i->term, relevance));
[email protected]257ab712009-04-14 17:16:24862 }
[email protected]51124552011-07-16 01:37:10863
864 // History returns results sorted for us. However, we may have docked some
865 // results' scores, so things are no longer in order. Do a stable sort to get
866 // things back in order without otherwise disturbing results with equal
867 // scores, then force the scores to be unique, so that the order in which
868 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:08869 std::stable_sort(scored_results.begin(), scored_results.end(),
870 CompareScoredResults());
[email protected]51124552011-07-16 01:37:10871 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:08872 for (SuggestResults::iterator i(scored_results.begin());
873 i != scored_results.end(); ++i) {
874 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
875 i->set_relevance(last_relevance - 1);
876 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:10877 }
878
[email protected]55ce8f12012-05-09 04:44:08879 return scored_results;
[email protected]257ab712009-04-14 17:16:24880}
881
[email protected]55ce8f12012-05-09 04:44:08882void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
883 bool is_keyword,
884 MatchMap* map) {
[email protected]d7ad4772012-06-01 03:12:54885 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
[email protected]55ce8f12012-05-09 04:44:08886 for (size_t i = 0; i < results.size(); ++i) {
[email protected]d7ad4772012-06-01 03:12:54887 AddMatchToMap(results[i].suggestion(), input_text, results[i].relevance(),
[email protected]55ce8f12012-05-09 04:44:08888 AutocompleteMatch::SEARCH_SUGGEST, i, is_keyword, map);
[email protected]257ab712009-04-14 17:16:24889 }
initial.commit09911bf2008-07-26 23:55:29890}
891
[email protected]382a0642012-06-06 06:13:52892int SearchProvider::GetVerbatimRelevance() const {
[email protected]d1f0a7f2012-06-05 10:26:42893 if (verbatim_relevance_ >= 0 && !input_.prevent_inline_autocomplete())
894 return verbatim_relevance_;
[email protected]382a0642012-06-06 06:13:52895 return CalculateRelevanceForVerbatim();
896}
[email protected]d1f0a7f2012-06-05 10:26:42897
[email protected]382a0642012-06-06 06:13:52898int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:47899 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:36900 return 250;
901
initial.commit09911bf2008-07-26 23:55:29902 switch (input_.type()) {
903 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:36904 case AutocompleteInput::QUERY:
905 case AutocompleteInput::FORCED_QUERY:
906 return 1300;
initial.commit09911bf2008-07-26 23:55:29907
908 case AutocompleteInput::REQUESTED_URL:
[email protected]52d08b12009-10-19 18:42:36909 return 1150;
initial.commit09911bf2008-07-26 23:55:29910
911 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:36912 return 850;
initial.commit09911bf2008-07-26 23:55:29913
914 default:
915 NOTREACHED();
916 return 0;
917 }
918}
919
[email protected]51124552011-07-16 01:37:10920int SearchProvider::CalculateRelevanceForHistory(
921 const Time& time,
922 bool is_keyword,
923 bool prevent_inline_autocomplete) const {
[email protected]aa613d62010-11-09 20:40:18924 // The relevance of past searches falls off over time. There are two distinct
925 // equations used. If the first equation is used (searches to the primary
[email protected]51124552011-07-16 01:37:10926 // provider that we want to inline autocomplete), the score starts at 1399 and
927 // falls to 1300. If the second equation is used the relevance of a search 15
928 // minutes ago is discounted 50 points, while the relevance of a search two
929 // weeks ago is discounted 450 points.
[email protected]aa613d62010-11-09 20:40:18930 double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.);
[email protected]51124552011-07-16 01:37:10931 bool is_primary_provider = providers_.is_primary_provider(is_keyword);
932 if (is_primary_provider && !prevent_inline_autocomplete) {
[email protected]aa613d62010-11-09 20:40:18933 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:10934 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:18935 if (elapsed_time < autocomplete_time) {
[email protected]e17511f2011-07-13 14:09:18936 return (is_keyword ? 1599 : 1399) - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:18937 std::pow(elapsed_time / autocomplete_time, 2.5));
938 }
939 elapsed_time -= autocomplete_time;
940 }
941
[email protected]c3a4bd992010-08-18 20:25:01942 const int score_discount =
943 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:29944
[email protected]6c85aa02009-02-27 12:08:09945 // Don't let scores go below 0. Negative relevance scores are meaningful in
946 // a different way.
initial.commit09911bf2008-07-26 23:55:29947 int base_score;
[email protected]51124552011-07-16 01:37:10948 if (is_primary_provider)
[email protected]52d08b12009-10-19 18:42:36949 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:10950 else
951 base_score = 200;
initial.commit09911bf2008-07-26 23:55:29952 return std::max(0, base_score - score_discount);
953}
954
[email protected]55ce8f12012-05-09 04:44:08955int SearchProvider::CalculateRelevanceForSuggestion(bool for_keyword) const {
956 return !providers_.is_primary_provider(for_keyword) ? 100 :
957 ((input_.type() == AutocompleteInput::URL) ? 300 : 600);
initial.commit09911bf2008-07-26 23:55:29958}
959
[email protected]55ce8f12012-05-09 04:44:08960int SearchProvider::CalculateRelevanceForNavigation(bool for_keyword) const {
961 return providers_.is_primary_provider(for_keyword) ? 800 : 150;
initial.commit09911bf2008-07-26 23:55:29962}
963
[email protected]a2fedb1e2011-01-25 15:23:36964void SearchProvider::AddMatchToMap(const string16& query_string,
965 const string16& input_text,
initial.commit09911bf2008-07-26 23:55:29966 int relevance,
[email protected]4c1fb7ec2008-11-13 00:19:00967 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:29968 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:24969 bool is_keyword,
initial.commit09911bf2008-07-26 23:55:29970 MatchMap* map) {
[email protected]92513682011-09-01 06:16:52971 AutocompleteMatch match(this, relevance, false, type);
initial.commit09911bf2008-07-26 23:55:29972 std::vector<size_t> content_param_offsets;
[email protected]85b8d6f2012-05-08 20:53:47973 // Bail out now if we don't actually have a valid provider.
974 match.keyword = is_keyword ?
[email protected]3954c3a2012-04-10 20:17:55975 providers_.keyword_provider() : providers_.default_provider();
[email protected]85b8d6f2012-05-08 20:53:47976 const TemplateURL* provider_url = match.GetTemplateURL(profile_);
977 if (provider_url == NULL)
978 return;
979
[email protected]70833262011-01-05 23:40:44980 match.contents.assign(query_string);
[email protected]fb5153c52009-07-31 19:40:33981 // We do intra-string highlighting for suggestions - the suggested segment
982 // will be highlighted, e.g. for input_text = "you" the suggestion may be
983 // "youtube", so we'll bold the "tube" section: you*tube*.
984 if (input_text != query_string) {
[email protected]fb5153c52009-07-31 19:40:33985 size_t input_position = match.contents.find(input_text);
[email protected]a2fedb1e2011-01-25 15:23:36986 if (input_position == string16::npos) {
[email protected]fb5153c52009-07-31 19:40:33987 // The input text is not a substring of the query string, e.g. input
988 // text is "slasdot" and the query string is "slashdot", so we bold the
989 // whole thing.
990 match.contents_class.push_back(
991 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]ec2379162009-06-09 23:58:17992 } else {
[email protected]fb5153c52009-07-31 19:40:33993 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
994 // "bold" this. Consider modifying the terminology.
995 // We don't iterate over the string here annotating all matches because
996 // it looks odd to have every occurrence of a substring that may be as
997 // short as a single character highlighted in a query suggestion result,
998 // e.g. for input text "s" and query string "southwest airlines", it
999 // looks odd if both the first and last s are highlighted.
1000 if (input_position != 0) {
1001 match.contents_class.push_back(
1002 ACMatchClassification(0, ACMatchClassification::NONE));
1003 }
1004 match.contents_class.push_back(
1005 ACMatchClassification(input_position, ACMatchClassification::DIM));
1006 size_t next_fragment_position = input_position + input_text.length();
1007 if (next_fragment_position < query_string.length()) {
1008 match.contents_class.push_back(
1009 ACMatchClassification(next_fragment_position,
1010 ACMatchClassification::NONE));
1011 }
[email protected]ec2379162009-06-09 23:58:171012 }
initial.commit09911bf2008-07-26 23:55:291013 } else {
[email protected]fb5153c52009-07-31 19:40:331014 // Otherwise, we're dealing with the "default search" result which has no
[email protected]70833262011-01-05 23:40:441015 // completion.
[email protected]fb5153c52009-07-31 19:40:331016 match.contents_class.push_back(
1017 ACMatchClassification(0, ACMatchClassification::NONE));
initial.commit09911bf2008-07-26 23:55:291018 }
1019
1020 // When the user forced a query, we need to make sure all the fill_into_edit
1021 // values preserve that property. Otherwise, if the user starts editing a
1022 // suggestion, non-Search results will suddenly appear.
1023 size_t search_start = 0;
1024 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
[email protected]a2fedb1e2011-01-25 15:23:361025 match.fill_into_edit.assign(ASCIIToUTF16("?"));
initial.commit09911bf2008-07-26 23:55:291026 ++search_start;
1027 }
[email protected]c0048b42009-05-04 21:47:171028 if (is_keyword) {
[email protected]033f3422012-03-13 21:24:181029 match.fill_into_edit.append(match.keyword + char16(' '));
1030 search_start += match.keyword.length() + 1;
[email protected]c0048b42009-05-04 21:47:171031 }
initial.commit09911bf2008-07-26 23:55:291032 match.fill_into_edit.append(query_string);
[email protected]2c33dd22010-02-11 21:46:351033 // Not all suggestions start with the original input.
[email protected]55ce8f12012-05-09 04:44:081034 if (!input_.prevent_inline_autocomplete() &&
[email protected]257ab712009-04-14 17:16:241035 !match.fill_into_edit.compare(search_start, input_text.length(),
1036 input_text))
1037 match.inline_autocomplete_offset = search_start + input_text.length();
initial.commit09911bf2008-07-26 23:55:291038
[email protected]85b8d6f2012-05-08 20:53:471039 const TemplateURLRef& search_url = provider_url->url_ref();
[email protected]360ba052012-04-04 17:26:131040 DCHECK(search_url.SupportsReplacement());
[email protected]16fca9b82012-04-23 18:40:261041 match.destination_url = GURL(search_url.ReplaceSearchTerms(query_string,
1042 accepted_suggestion, input_text));
initial.commit09911bf2008-07-26 23:55:291043
1044 // Search results don't look like URLs.
[email protected]2905f742011-10-13 03:51:581045 match.transition = is_keyword ?
1046 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED;
initial.commit09911bf2008-07-26 23:55:291047
1048 // Try to add |match| to |map|. If a match for |query_string| is already in
1049 // |map|, replace it if |match| is more relevant.
1050 // NOTE: Keep this ToLower() call in sync with url_database.cc.
1051 const std::pair<MatchMap::iterator, bool> i = map->insert(
[email protected]a2fedb1e2011-01-25 15:23:361052 std::pair<string16, AutocompleteMatch>(
[email protected]503d03872011-05-06 08:36:261053 base::i18n::ToLower(query_string), match));
initial.commit09911bf2008-07-26 23:55:291054 // NOTE: We purposefully do a direct relevance comparison here instead of
1055 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
1056 // first" rather than "items alphabetically first" when the scores are equal.
1057 // The only case this matters is when a user has results with the same score
1058 // that differ only by capitalization; because the history system returns
1059 // results sorted by recency, this means we'll pick the most recent such
1060 // result even if the precision of our relevance score is too low to
1061 // distinguish the two.
1062 if (!i.second && (match.relevance > i.first->second.relevance))
1063 i.first->second = match;
1064}
1065
1066AutocompleteMatch SearchProvider::NavigationToMatch(
1067 const NavigationResult& navigation,
[email protected]257ab712009-04-14 17:16:241068 bool is_keyword) {
[email protected]371dab12012-06-01 03:23:551069 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
[email protected]55ce8f12012-05-09 04:44:081070 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]4c1fb7ec2008-11-13 00:19:001071 AutocompleteMatch::NAVSUGGEST);
[email protected]55ce8f12012-05-09 04:44:081072 match.destination_url = navigation.url();
[email protected]371dab12012-06-01 03:23:551073
1074 // First look for the user's input inside the fill_into_edit as it would be
1075 // without trimming the scheme, so we can find matches at the beginning of the
1076 // scheme.
1077 const string16 untrimmed_fill_into_edit(
1078 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1079 StringForURLDisplay(navigation.url(), true, false)));
1080 const URLPrefix* prefix =
1081 URLPrefix::BestURLPrefix(untrimmed_fill_into_edit, input);
1082 size_t match_start = (prefix == NULL) ?
1083 untrimmed_fill_into_edit.find(input) : prefix->prefix.length();
1084 size_t inline_autocomplete_offset = (prefix == NULL) ?
1085 string16::npos : (match_start + input.length());
1086 bool trim_http = !HasHTTPScheme(input) && (!prefix || (match_start != 0));
1087
1088 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1089 // Otherwise, user edits to a suggestion would show non-Search results.
1090 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
1091 match.fill_into_edit = ASCIIToUTF16("?");
1092 if (inline_autocomplete_offset != string16::npos)
1093 ++inline_autocomplete_offset;
1094 }
1095
1096 const std::string languages(
1097 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
1098 const net::FormatUrlTypes format_types =
1099 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
1100 match.fill_into_edit +=
1101 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1102 net::FormatUrl(navigation.url(), languages, format_types,
1103 net::UnescapeRule::SPACES, NULL, NULL,
1104 &inline_autocomplete_offset));
1105 if (!input_.prevent_inline_autocomplete())
1106 match.inline_autocomplete_offset = inline_autocomplete_offset;
1107 DCHECK((match.inline_autocomplete_offset == string16::npos) ||
1108 (match.inline_autocomplete_offset <= match.fill_into_edit.length()));
1109
1110 match.contents = net::FormatUrl(navigation.url(), languages,
1111 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start);
1112 // If the first match in the untrimmed string was inside a scheme that we
1113 // trimmed, look for a subsequent match.
1114 if (match_start == string16::npos)
1115 match_start = match.contents.find(input);
1116 // Safe if |match_start| is npos; also safe if the input is longer than the
1117 // remaining contents after |match_start|.
1118 AutocompleteMatch::ClassifyLocationInString(match_start, input.length(),
1119 match.contents.length(), ACMatchClassification::URL,
1120 &match.contents_class);
initial.commit09911bf2008-07-26 23:55:291121
[email protected]55ce8f12012-05-09 04:44:081122 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551123 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1124 ACMatchClassification::NONE, &match.description_class);
initial.commit09911bf2008-07-26 23:55:291125 return match;
1126}
[email protected]4ab4c7c2010-11-24 04:49:341127
1128void SearchProvider::UpdateDone() {
1129 // We're done when there are no more suggest queries pending (this is set to 1
1130 // when the timer is started) and we're not waiting on instant.
1131 done_ = ((suggest_results_pending_ == 0) &&
1132 (instant_finalized_ || !InstantController::IsEnabled(profile_)));
1133}