blob: 9d1281b3cc74ca5850ce6cc2c06643119c647690 [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]810ffba2012-06-12 01:07:4821#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
[email protected]f5b95ba92012-03-27 14:05:1922#include "chrome/browser/autocomplete/autocomplete_field_trial.h"
[email protected]9ac40092010-10-27 23:05:2623#include "chrome/browser/autocomplete/autocomplete_match.h"
[email protected]3723e6e2012-06-11 21:06:5624#include "chrome/browser/autocomplete/history_url_provider.h"
[email protected]2c812ba02011-07-14 00:23:1525#include "chrome/browser/autocomplete/keyword_provider.h"
[email protected]371dab12012-06-01 03:23:5526#include "chrome/browser/autocomplete/url_prefix.h"
[email protected]ce560f82009-06-03 09:39:4427#include "chrome/browser/history/history.h"
[email protected]10c2d692012-05-11 05:32:2328#include "chrome/browser/history/in_memory_database.h"
[email protected]4ab4c7c2010-11-24 04:49:3429#include "chrome/browser/instant/instant_controller.h"
[email protected]f870a322009-01-16 21:47:2730#include "chrome/browser/net/url_fixer_upper.h"
[email protected]37858e52010-08-26 00:22:0231#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3332#include "chrome/browser/profiles/profile.h"
[email protected]a0ad93ea2012-05-07 22:11:5333#include "chrome/browser/search_engines/search_engine_type.h"
[email protected]8e5c89a2011-06-07 18:13:3334#include "chrome/browser/search_engines/template_url_service.h"
35#include "chrome/browser/search_engines/template_url_service_factory.h"
initial.commit09911bf2008-07-26 23:55:2936#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0237#include "chrome/common/url_constants.h"
[email protected]36aea2702011-10-26 01:12:2238#include "content/public/common/url_fetcher.h"
initial.commit09911bf2008-07-26 23:55:2939#include "googleurl/src/url_util.h"
[email protected]34ac8f32009-02-22 23:03:2740#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2941#include "net/base/escape.h"
[email protected]d3cf8682f02012-02-29 23:29:3442#include "net/base/load_flags.h"
[email protected]371dab12012-06-01 03:23:5543#include "net/base/net_util.h"
[email protected]319d9e6f2009-02-18 19:47:2144#include "net/http/http_response_headers.h"
45#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1746#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2947
[email protected]e1acf6f2008-10-27 20:43:3348using base::Time;
49using base::TimeDelta;
50
[email protected]51124552011-07-16 01:37:1051namespace {
52
53bool HasMultipleWords(const string16& text) {
54 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
55 bool found_word = false;
56 if (i.Init()) {
57 while (i.Advance()) {
58 if (i.IsWord()) {
59 if (found_word)
60 return true;
61 found_word = true;
62 }
63 }
64 }
65 return false;
66}
67
[email protected]d1f0a7f2012-06-05 10:26:4268} // namespace
[email protected]51124552011-07-16 01:37:1069
[email protected]033f3422012-03-13 21:24:1870
[email protected]3954c3a2012-04-10 20:17:5571// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:5872
[email protected]85b8d6f2012-05-08 20:53:4773SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
74 : template_url_service_(template_url_service) {
75}
76
77const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
78 return default_provider_.empty() ? NULL :
79 template_url_service_->GetTemplateURLForKeyword(default_provider_);
80}
81
82const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
83 return keyword_provider_.empty() ? NULL :
84 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:2485}
86
[email protected]3954c3a2012-04-10 20:17:5587
88// SearchProvider -------------------------------------------------------------
89
90// static
91const int SearchProvider::kDefaultProviderURLFetcherID = 1;
92// static
93const int SearchProvider::kKeywordProviderURLFetcherID = 2;
94// static
95bool SearchProvider::query_suggest_immediately_ = false;
96
[email protected]601858c02010-09-01 17:08:2097SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile)
98 : AutocompleteProvider(listener, profile, "Search"),
[email protected]85b8d6f2012-05-08 20:53:4799 providers_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]601858c02010-09-01 17:08:20100 suggest_results_pending_(0),
[email protected]d1f0a7f2012-06-05 10:26:42101 has_suggested_relevance_(false),
102 verbatim_relevance_(-1),
[email protected]8e5cc282010-12-05 18:11:39103 have_suggest_results_(false),
[email protected]4ab4c7c2010-11-24 04:49:34104 instant_finalized_(false) {
[email protected]f5b95ba92012-03-27 14:05:19105 // We use GetSuggestNumberOfGroups() as the group ID to mean "not in field
106 // trial." Field trial groups run from 0 to GetSuggestNumberOfGroups() - 1
107 // (inclusive).
108 int suggest_field_trial_group_number =
109 AutocompleteFieldTrial::GetSuggestNumberOfGroups();
110 if (AutocompleteFieldTrial::InSuggestFieldTrial()) {
111 suggest_field_trial_group_number =
112 AutocompleteFieldTrial::GetSuggestGroupNameAsNumber();
113 }
114 // Add a beacon to the logs that'll allow us to identify later what
115 // suggest field trial group a user is in. Do this by incrementing a
116 // bucket in a histogram, where the bucket represents the user's
117 // suggest group id.
118 UMA_HISTOGRAM_ENUMERATION(
119 "Omnibox.SuggestFieldTrialBeacon",
120 suggest_field_trial_group_number,
121 AutocompleteFieldTrial::GetSuggestNumberOfGroups() + 1);
[email protected]4ab4c7c2010-11-24 04:49:34122}
123
[email protected]a2fedb1e2011-01-25 15:23:36124void SearchProvider::FinalizeInstantQuery(const string16& input_text,
125 const string16& suggest_text) {
[email protected]4ab4c7c2010-11-24 04:49:34126 if (done_ || instant_finalized_)
127 return;
128
129 instant_finalized_ = true;
130 UpdateDone();
131
[email protected]e918c112010-12-08 23:03:49132 if (input_text.empty()) {
[email protected]4ab4c7c2010-11-24 04:49:34133 // We only need to update the listener if we're actually done.
134 if (done_)
135 listener_->OnProviderUpdate(false);
136 return;
137 }
138
[email protected]9e789742011-01-10 23:27:32139 default_provider_suggest_text_ = suggest_text;
140
[email protected]a2fedb1e2011-01-25 15:23:36141 string16 adjusted_input_text(input_text);
[email protected]e918c112010-12-08 23:03:49142 AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(),
143 &adjusted_input_text);
144
[email protected]a2fedb1e2011-01-25 15:23:36145 const string16 text = adjusted_input_text + suggest_text;
[email protected]9acdcdc02012-05-21 20:58:36146 bool results_updated = false;
[email protected]4ab4c7c2010-11-24 04:49:34147 // Remove any matches that are identical to |text|. We don't use the
148 // destination_url for comparison as it varies depending upon the index passed
149 // to TemplateURL::ReplaceSearchTerms.
150 for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) {
151 if (((i->type == AutocompleteMatch::SEARCH_HISTORY) ||
152 (i->type == AutocompleteMatch::SEARCH_SUGGEST)) &&
153 (i->fill_into_edit == text)) {
[email protected]e030de62010-11-24 05:41:19154 i = matches_.erase(i);
[email protected]9acdcdc02012-05-21 20:58:36155 results_updated = true;
[email protected]4ab4c7c2010-11-24 04:49:34156 } else {
157 ++i;
158 }
159 }
160
[email protected]55ce8f12012-05-09 04:44:08161 // Add the new instant suggest result. We give it a rank higher than
[email protected]4ab4c7c2010-11-24 04:49:34162 // SEARCH_WHAT_YOU_TYPED so that it gets autocompleted.
163 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
164 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
165 TemplateURLRef::NO_SUGGESTION_CHOSEN;
166 MatchMap match_map;
[email protected]382a0642012-06-06 06:13:52167 AddMatchToMap(text, adjusted_input_text, GetVerbatimRelevance() + 1,
[email protected]4ab4c7c2010-11-24 04:49:34168 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08169 did_not_accept_default_suggestion, false, &match_map);
[email protected]9acdcdc02012-05-21 20:58:36170 if (!match_map.empty()) {
171 matches_.push_back(match_map.begin()->second);
172 results_updated = true;
173 }
[email protected]4ab4c7c2010-11-24 04:49:34174
[email protected]9acdcdc02012-05-21 20:58:36175 if (results_updated || done_)
176 listener_->OnProviderUpdate(results_updated);
[email protected]601858c02010-09-01 17:08:20177}
178
initial.commit09911bf2008-07-26 23:55:29179void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27180 bool minimal_changes) {
initial.commit09911bf2008-07-26 23:55:29181 matches_.clear();
182
[email protected]ea3b9a502011-04-04 14:19:37183 instant_finalized_ =
184 (input.matches_requested() != AutocompleteInput::ALL_MATCHES);
[email protected]4ab4c7c2010-11-24 04:49:34185
[email protected]6c85aa02009-02-27 12:08:09186 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29187 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
188 Stop();
189 return;
190 }
191
[email protected]257ab712009-04-14 17:16:24192 keyword_input_text_.clear();
193 const TemplateURL* keyword_provider =
194 KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input,
195 &keyword_input_text_);
[email protected]8d457132010-11-04 18:13:40196 if (keyword_input_text_.empty())
[email protected]257ab712009-04-14 17:16:24197 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24198
[email protected]85b8d6f2012-05-08 20:53:47199 TemplateURLService* model = providers_.template_url_service();
200 DCHECK(model);
201 model->Load();
202 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]9b74ab52012-03-30 16:08:07203 if (default_provider && !default_provider->SupportsReplacement())
[email protected]257ab712009-04-14 17:16:24204 default_provider = NULL;
205
206 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18207 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24208
209 if (!default_provider && !keyword_provider) {
210 // No valid providers.
initial.commit09911bf2008-07-26 23:55:29211 Stop();
212 return;
213 }
214
215 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24216 // or the providers, abort the query.
[email protected]85b8d6f2012-05-08 20:53:47217 string16 default_provider_keyword(default_provider ?
218 default_provider->keyword() : string16());
219 string16 keyword_provider_keyword(keyword_provider ?
220 keyword_provider->keyword() : string16());
[email protected]9e789742011-01-10 23:27:32221 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47222 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]9e789742011-01-10 23:27:32223 if (done_)
224 default_provider_suggest_text_.clear();
225 else
226 Stop();
[email protected]257ab712009-04-14 17:16:24227 }
initial.commit09911bf2008-07-26 23:55:29228
[email protected]85b8d6f2012-05-08 20:53:47229 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29230
231 if (input.text().empty()) {
232 // User typed "?" alone. Give them a placeholder result indicating what
233 // this syntax does.
[email protected]257ab712009-04-14 17:16:24234 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00235 AutocompleteMatch match;
236 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36237 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24238 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35239 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47240 match.keyword = providers_.default_provider();
[email protected]257ab712009-04-14 17:16:24241 matches_.push_back(match);
242 }
initial.commit09911bf2008-07-26 23:55:29243 Stop();
244 return;
245 }
246
247 input_ = input;
248
[email protected]8d457132010-11-04 18:13:40249 DoHistoryQuery(minimal_changes);
[email protected]8deeb952008-10-09 18:21:27250 StartOrStopSuggestQuery(minimal_changes);
initial.commit09911bf2008-07-26 23:55:29251 ConvertResultsToAutocompleteMatches();
252}
253
[email protected]55ce8f12012-05-09 04:44:08254SearchProvider::Result::Result(int relevance) : relevance_(relevance) {}
255SearchProvider::Result::~Result() {}
256
257SearchProvider::SuggestResult::SuggestResult(const string16& suggestion,
258 int relevance)
259 : Result(relevance),
260 suggestion_(suggestion) {
261}
262
263SearchProvider::SuggestResult::~SuggestResult() {}
264
265SearchProvider::NavigationResult::NavigationResult(const GURL& url,
266 const string16& description,
267 int relevance)
268 : Result(relevance),
269 url_(url),
270 description_(description) {
271 DCHECK(url_.is_valid());
272}
273
274SearchProvider::NavigationResult::~NavigationResult() {}
275
276class SearchProvider::CompareScoredResults {
[email protected]51124552011-07-16 01:37:10277 public:
[email protected]55ce8f12012-05-09 04:44:08278 bool operator()(const Result& a, const Result& b) {
[email protected]51124552011-07-16 01:37:10279 // Sort in descending relevance order.
[email protected]55ce8f12012-05-09 04:44:08280 return a.relevance() > b.relevance();
[email protected]51124552011-07-16 01:37:10281 }
282};
283
initial.commit09911bf2008-07-26 23:55:29284void SearchProvider::Run() {
285 // Start a new request with the current input.
286 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24287 suggest_results_pending_ = 0;
[email protected]a0ad93ea2012-05-07 22:11:53288 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]85b8d6f2012-05-08 20:53:47289 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
290 if (default_url && !default_url->suggestions_url().empty()) {
[email protected]257ab712009-04-14 17:16:24291 suggest_results_pending_++;
[email protected]033f3422012-03-13 21:24:18292 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
[email protected]85b8d6f2012-05-08 20:53:47293 default_url->suggestions_url_ref(), input_.text()));
[email protected]3954c3a2012-04-10 20:17:55294 }
[email protected]85b8d6f2012-05-08 20:53:47295 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
296 if (keyword_url && !keyword_url->suggestions_url().empty()) {
[email protected]3954c3a2012-04-10 20:17:55297 suggest_results_pending_++;
298 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
[email protected]85b8d6f2012-05-08 20:53:47299 keyword_url->suggestions_url_ref(), keyword_input_text_));
[email protected]257ab712009-04-14 17:16:24300 }
[email protected]85b8d6f2012-05-08 20:53:47301
302 // Both the above can fail if the providers have been modified or deleted
303 // since the query began.
304 if (suggest_results_pending_ == 0) {
305 UpdateDone();
306 // We only need to update the listener if we're actually done.
307 if (done_)
308 listener_->OnProviderUpdate(false);
309 }
initial.commit09911bf2008-07-26 23:55:29310}
311
312void SearchProvider::Stop() {
initial.commit09911bf2008-07-26 23:55:29313 StopSuggest();
314 done_ = true;
[email protected]9e789742011-01-10 23:27:32315 default_provider_suggest_text_.clear();
initial.commit09911bf2008-07-26 23:55:29316}
317
[email protected]0e9e8782012-05-15 23:01:51318void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
319 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
320 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
321 new_entry.set_provider(AsOmniboxEventProviderType());
322 new_entry.set_provider_done(done_);
323}
324
[email protected]10c2d692012-05-11 05:32:23325void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) {
initial.commit09911bf2008-07-26 23:55:29326 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24327 suggest_results_pending_--;
[email protected]1cb2dac2010-03-08 21:49:15328 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06329 const net::HttpResponseHeaders* const response_headers =
[email protected]7cc6e5632011-10-25 17:56:12330 source->GetResponseHeaders();
[email protected]c530c852011-10-24 18:18:34331 std::string json_data;
332 source->GetResponseAsString(&json_data);
[email protected]6c85aa02009-02-27 12:08:09333 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
334 // files in non-UTF-8 encodings. The actual encoding is usually specified in
335 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06336 if (response_headers) {
337 std::string charset;
338 if (response_headers->GetCharset(&charset)) {
[email protected]a2fedb1e2011-01-25 15:23:36339 string16 data_16;
[email protected]ec9207d32008-09-26 00:51:06340 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]c530c852011-10-24 18:18:34341 if (base::CodepageToUTF16(json_data, charset.c_str(),
[email protected]a2fedb1e2011-01-25 15:23:36342 base::OnStringConversionError::FAIL,
343 &data_16))
344 json_data = UTF16ToUTF8(data_16);
[email protected]ec9207d32008-09-26 00:51:06345 }
346 }
347
[email protected]d7ad4772012-06-01 03:12:54348 const bool is_keyword = (source == keyword_fetcher_.get());
[email protected]013e9a02012-05-18 20:27:10349 const bool request_succeeded =
350 source->GetStatus().is_success() && source->GetResponseCode() == 200;
[email protected]a0ad93ea2012-05-07 22:11:53351
352 // Record response time for suggest requests sent to Google. We care
353 // only about the common case: the Google default provider used in
354 // non-keyword mode.
[email protected]85b8d6f2012-05-08 20:53:47355 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
[email protected]55ce8f12012-05-09 04:44:08356 if (!is_keyword && default_url &&
[email protected]85b8d6f2012-05-08 20:53:47357 (default_url->prepopulate_id() == SEARCH_ENGINE_GOOGLE)) {
[email protected]013e9a02012-05-18 20:27:10358 const base::TimeDelta elapsed_time =
359 base::TimeTicks::Now() - time_suggest_request_sent_;
360 if (request_succeeded) {
361 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
362 elapsed_time);
363 } else {
364 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
365 elapsed_time);
366 }
[email protected]b4cebf82008-12-29 19:59:08367 }
368
[email protected]d7ad4772012-06-01 03:12:54369 bool results_updated = false;
370 if (request_succeeded) {
371 JSONStringValueSerializer deserializer(json_data);
372 deserializer.set_allow_trailing_comma(true);
373 scoped_ptr<Value> data(deserializer.Deserialize(NULL, NULL));
374 results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword);
375 }
376
initial.commit09911bf2008-07-26 23:55:29377 ConvertResultsToAutocompleteMatches();
[email protected]d7ad4772012-06-01 03:12:54378 if (done_ || results_updated)
379 listener_->OnProviderUpdate(results_updated);
initial.commit09911bf2008-07-26 23:55:29380}
381
[email protected]601858c02010-09-01 17:08:20382SearchProvider::~SearchProvider() {
383}
384
[email protected]8d457132010-11-04 18:13:40385void SearchProvider::DoHistoryQuery(bool minimal_changes) {
386 // The history query results are synchronous, so if minimal_changes is true,
387 // we still have the last results and don't need to do anything.
388 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29389 return;
390
[email protected]8d457132010-11-04 18:13:40391 keyword_history_results_.clear();
392 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29393
[email protected]8d457132010-11-04 18:13:40394 HistoryService* const history_service =
395 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
396 history::URLDatabase* url_db = history_service ?
397 history_service->InMemoryDatabase() : NULL;
398 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29399 return;
400
[email protected]51124552011-07-16 01:37:10401 // Request history for both the keyword and default provider. We grab many
402 // more matches than we'll ultimately clamp to so that if there are several
403 // recent multi-word matches who scores are lowered (see
404 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
405 // matches. Note that this doesn't fix the problem entirely, but merely
406 // limits it to cases with a very large number of such multi-word matches; for
407 // now, this seems OK compared with the complexity of a real fix, which would
408 // require multiple searches and tracking of "single- vs. multi-word" in the
409 // database.
410 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47411 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
412 if (default_url) {
413 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
414 num_matches, &default_history_results_);
[email protected]257ab712009-04-14 17:16:24415 }
[email protected]85b8d6f2012-05-08 20:53:47416 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
417 if (keyword_url) {
418 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]3954c3a2012-04-10 20:17:55419 keyword_input_text_, num_matches, &keyword_history_results_);
420 }
initial.commit09911bf2008-07-26 23:55:29421}
422
[email protected]8deeb952008-10-09 18:21:27423void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]6c85aa02009-02-27 12:08:09424 // Don't send any queries to the server until some time has elapsed after
425 // the last keypress, to avoid flooding the server with requests we are
426 // likely to end up throwing away anyway.
[email protected]02c3f6832011-11-16 18:37:40427 const int kQueryDelayMs = 200;
[email protected]6c85aa02009-02-27 12:08:09428
[email protected]83c726482008-09-10 06:36:34429 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29430 StopSuggest();
[email protected]55ce8f12012-05-09 04:44:08431 ClearResults();
initial.commit09911bf2008-07-26 23:55:29432 return;
433 }
434
435 // For the minimal_changes case, if we finished the previous query and still
436 // have its results, or are allowed to keep running it, just do that, rather
437 // than starting a new query.
438 if (minimal_changes &&
[email protected]ea3b9a502011-04-04 14:19:37439 (have_suggest_results_ ||
440 (!done_ &&
441 input_.matches_requested() == AutocompleteInput::ALL_MATCHES)))
initial.commit09911bf2008-07-26 23:55:29442 return;
443
444 // We can't keep running any previous query, so halt it.
445 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42446
447 // Remove existing results that cannot inline autocomplete the new input.
448 RemoveStaleResults();
initial.commit09911bf2008-07-26 23:55:29449
450 // We can't start a new query if we're only allowed synchronous results.
[email protected]ea3b9a502011-04-04 14:19:37451 if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES)
initial.commit09911bf2008-07-26 23:55:29452 return;
453
[email protected]257ab712009-04-14 17:16:24454 // We'll have at least one pending fetch. Set it to 1 now, but the value is
455 // correctly set in Run. As Run isn't invoked immediately we need to set this
456 // now, else we won't think we're waiting on results from the server when we
457 // really are.
458 suggest_results_pending_ = 1;
459
initial.commit09911bf2008-07-26 23:55:29460 // Kick off a timer that will start the URL fetch if it completes before
461 // the user types another character.
[email protected]b547666d2009-04-23 16:37:58462 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs;
[email protected]d323a172011-09-02 18:23:02463 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay), this,
464 &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29465}
466
[email protected]83c726482008-09-10 06:36:34467bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55468 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
469 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47470 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
471 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34472 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47473 ((!default_url || default_url->suggestions_url().empty()) &&
474 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34475 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
476 return false;
477
[email protected]cac59d32010-08-09 23:23:14478 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34479 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34480
[email protected]cac59d32010-08-09 23:23:14481 // FORCED_QUERY means the user is explicitly asking us to search for this, so
482 // we assume it isn't a URL and/or there isn't private data.
483 if (input_.type() == AutocompleteInput::FORCED_QUERY)
484 return true;
[email protected]83c726482008-09-10 06:36:34485
[email protected]cac59d32010-08-09 23:23:14486 // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a
487 // scheme that isn't http/https/ftp, we shouldn't send it. Sending things
488 // like file: and data: is both a waste of time and a disclosure of
489 // potentially private, local data. Other "schemes" may actually be
490 // usernames, and we don't want to send passwords. If the scheme is OK, we
491 // still need to check other cases below. If this is QUERY, then the presence
492 // of these schemes means the user explicitly typed one, and thus this is
493 // probably a URL that's being entered and happens to currently be invalid --
494 // in which case we again want to run our checks below. Other QUERY cases are
495 // less likely to be URLs and thus we assume we're OK.
[email protected]a2fedb1e2011-01-25 15:23:36496 if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) &&
497 !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
498 !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme))
[email protected]cac59d32010-08-09 23:23:14499 return (input_.type() == AutocompleteInput::QUERY);
500
501 // Don't send URLs with usernames, queries or refs. Some of these are
502 // private, and the Suggest server is unlikely to have any useful results
503 // for any of them. Also don't send URLs with ports, as we may initially
504 // think that a username + password is a host + port (and we don't want to
505 // send usernames/passwords), and even if the port really is a port, the
506 // server is once again unlikely to have and useful results.
507 const url_parse::Parsed& parts = input_.parts();
508 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
509 parts.query.is_nonempty() || parts.ref.is_nonempty())
510 return false;
511
512 // Don't send anything for https except the hostname. Hostnames are OK
513 // because they are visible when the TCP connection is established, but the
514 // specific path may reveal private information.
[email protected]a2fedb1e2011-01-25 15:23:36515 if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
516 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14517 return false;
[email protected]83c726482008-09-10 06:36:34518
519 return true;
520}
521
initial.commit09911bf2008-07-26 23:55:29522void SearchProvider::StopSuggest() {
[email protected]257ab712009-04-14 17:16:24523 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14524 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24525 // Stop any in-progress URL fetches.
526 keyword_fetcher_.reset();
527 default_fetcher_.reset();
[email protected]55ce8f12012-05-09 04:44:08528}
529
530void SearchProvider::ClearResults() {
[email protected]257ab712009-04-14 17:16:24531 keyword_suggest_results_.clear();
532 default_suggest_results_.clear();
533 keyword_navigation_results_.clear();
534 default_navigation_results_.clear();
[email protected]d1f0a7f2012-06-05 10:26:42535 has_suggested_relevance_ = false;
536 verbatim_relevance_ = -1;
initial.commit09911bf2008-07-26 23:55:29537 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29538}
539
[email protected]d1f0a7f2012-06-05 10:26:42540void SearchProvider::RemoveStaleResults() {
541 RemoveStaleSuggestResults(&keyword_suggest_results_, true);
542 RemoveStaleSuggestResults(&default_suggest_results_, false);
543 RemoveStaleNavigationResults(&keyword_navigation_results_, true);
544 RemoveStaleNavigationResults(&default_navigation_results_, false);
545}
546
547void SearchProvider::RemoveStaleSuggestResults(SuggestResults* list,
548 bool is_keyword) {
549 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
550 for (SuggestResults::iterator i = list->begin(); i < list->end();)
551 i = StartsWith(i->suggestion(), input, false) ? (i + 1) : list->erase(i);
552}
553
554void SearchProvider::RemoveStaleNavigationResults(NavigationResults* list,
555 bool is_keyword) {
556 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
557 for (NavigationResults::iterator i = list->begin(); i < list->end();) {
558 const string16 fill(AutocompleteInput::FormattedStringWithEquivalentMeaning(
559 i->url(), StringForURLDisplay(i->url(), true, false)));
560 i = URLPrefix::BestURLPrefix(fill, input) ? (i + 1) : list->erase(i);
561 }
562}
563
564void SearchProvider::ApplyCalculatedRelevance() {
565 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true);
566 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false);
567 ApplyCalculatedNavigationRelevance(&keyword_navigation_results_, true);
568 ApplyCalculatedNavigationRelevance(&default_navigation_results_, false);
569 has_suggested_relevance_ = false;
570 verbatim_relevance_ = -1;
571}
572
573void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list,
574 bool is_keyword) {
575 for (size_t i = 0; i < list->size(); ++i) {
576 (*list)[i].set_relevance(CalculateRelevanceForSuggestion(is_keyword) +
577 (list->size() - i - 1));
578 }
579}
580
581void SearchProvider::ApplyCalculatedNavigationRelevance(NavigationResults* list,
582 bool is_keyword) {
583 for (size_t i = 0; i < list->size(); ++i) {
584 (*list)[i].set_relevance(CalculateRelevanceForNavigation(is_keyword) +
585 (list->size() - i - 1));
586 }
587}
588
[email protected]15fb2aa2012-05-22 22:52:59589net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12590 int id,
[email protected]3954c3a2012-04-10 20:17:55591 const TemplateURLRef& suggestions_url,
[email protected]7cc6e5632011-10-25 17:56:12592 const string16& text) {
[email protected]360ba052012-04-04 17:26:13593 DCHECK(suggestions_url.SupportsReplacement());
[email protected]d3ec669b2012-05-23 07:12:14594 net::URLFetcher* fetcher = content::URLFetcher::Create(id,
[email protected]16fca9b82012-04-23 18:40:26595 GURL(suggestions_url.ReplaceSearchTerms(text,
596 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())),
[email protected]d3ec669b2012-05-23 07:12:14597 net::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12598 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34599 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]257ab712009-04-14 17:16:24600 fetcher->Start();
601 return fetcher;
602}
603
[email protected]d7ad4772012-06-01 03:12:54604bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) {
605 // TODO(pkasting): Fix |have_suggest_results_|; see http://crbug.com/130631
606 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29607
[email protected]d7ad4772012-06-01 03:12:54608 string16 query;
609 ListValue* root_list = NULL;
610 ListValue* results = NULL;
611 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
612 if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) ||
613 (query != input_text) || !root_list->GetList(1, &results))
initial.commit09911bf2008-07-26 23:55:29614 return false;
615
[email protected]55ce8f12012-05-09 04:44:08616 // 3rd element: Description list.
[email protected]d7ad4772012-06-01 03:12:54617 ListValue* descriptions = NULL;
618 root_list->GetList(2, &descriptions);
initial.commit09911bf2008-07-26 23:55:29619
[email protected]55ce8f12012-05-09 04:44:08620 // 4th element: Disregard the query URL list for now.
initial.commit09911bf2008-07-26 23:55:29621
[email protected]d1f0a7f2012-06-05 10:26:42622 // Reset suggested relevance information from the default provider.
623 if (!is_keyword) {
624 has_suggested_relevance_ = false;
625 verbatim_relevance_ = -1;
626 }
627
[email protected]55ce8f12012-05-09 04:44:08628 // 5th element: Optional key-value pairs from the Suggest server.
[email protected]d7ad4772012-06-01 03:12:54629 ListValue* types = NULL;
[email protected]d1f0a7f2012-06-05 10:26:42630 ListValue* relevances = NULL;
631 DictionaryValue* extras = NULL;
632 if (root_list->GetDictionary(4, &extras)) {
633 extras->GetList("google:suggesttype", &types);
634
635 // Only accept relevance suggestions if Instant is disabled.
636 if (!is_keyword && !InstantController::IsEnabled(profile_)) {
637 // Discard this list if its size does not match that of the suggestions.
638 if (extras->GetList("google:suggestrelevance", &relevances) &&
639 relevances->GetSize() != results->GetSize())
640 relevances = NULL;
641
642 extras->GetInteger("google:verbatimrelevance", &verbatim_relevance_);
643 }
initial.commit09911bf2008-07-26 23:55:29644 }
645
[email protected]d7ad4772012-06-01 03:12:54646 SuggestResults* suggest_results =
647 is_keyword ? &keyword_suggest_results_ : &default_suggest_results_;
648 NavigationResults* navigation_results =
649 is_keyword ? &keyword_navigation_results_ : &default_navigation_results_;
initial.commit09911bf2008-07-26 23:55:29650
[email protected]d1f0a7f2012-06-05 10:26:42651 // Clear the previous results now that new results are available.
652 suggest_results->clear();
653 navigation_results->clear();
654
655 string16 result, title;
656 std::string type;
657 int relevance = -1;
[email protected]d7ad4772012-06-01 03:12:54658 for (size_t index = 0; results->GetString(index, &result); ++index) {
[email protected]8e81f5092010-09-29 23:19:40659 // Google search may return empty suggestions for weird input characters,
[email protected]55ce8f12012-05-09 04:44:08660 // they make no sense at all and can cause problems in our code.
[email protected]d7ad4772012-06-01 03:12:54661 if (result.empty())
[email protected]8e81f5092010-09-29 23:19:40662 continue;
663
[email protected]d1f0a7f2012-06-05 10:26:42664 // Apply valid suggested relevance scores; discard invalid lists.
665 if (relevances != NULL && !relevances->GetInteger(index, &relevance))
666 relevances = NULL;
[email protected]d7ad4772012-06-01 03:12:54667 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) {
[email protected]d1f0a7f2012-06-05 10:26:42668 // Do not blindly trust the URL coming from the server to be valid.
669 GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(result), std::string()));
670 if (url.is_valid()) {
671 if (descriptions != NULL)
672 descriptions->GetString(index, &title);
673 navigation_results->push_back(NavigationResult(url, title, relevance));
initial.commit09911bf2008-07-26 23:55:29674 }
675 } else {
[email protected]d1f0a7f2012-06-05 10:26:42676 // TODO(kochi): Improve calculator result presentation.
677 suggest_results->push_back(SuggestResult(result, relevance));
initial.commit09911bf2008-07-26 23:55:29678 }
679 }
680
[email protected]d1f0a7f2012-06-05 10:26:42681 // Apply calculated relevance scores if a valid list was not provided.
682 if (relevances == NULL) {
683 ApplyCalculatedSuggestRelevance(suggest_results, is_keyword);
684 ApplyCalculatedNavigationRelevance(navigation_results, is_keyword);
685 } else if (!is_keyword) {
686 has_suggested_relevance_ = true;
687 }
688
[email protected]d7ad4772012-06-01 03:12:54689 have_suggest_results_ = true;
[email protected]d1f0a7f2012-06-05 10:26:42690 return true;
initial.commit09911bf2008-07-26 23:55:29691}
692
693void SearchProvider::ConvertResultsToAutocompleteMatches() {
694 // Convert all the results to matches and add them to a map, so we can keep
695 // the most relevant match for each result.
696 MatchMap map;
[email protected]257ab712009-04-14 17:16:24697 const Time no_time;
698 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ?
initial.commit09911bf2008-07-26 23:55:29699 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
700 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]257ab712009-04-14 17:16:24701 // Keyword what you typed results are handled by the KeywordProvider.
initial.commit09911bf2008-07-26 23:55:29702
[email protected]382a0642012-06-06 06:13:52703 int verbatim_relevance = GetVerbatimRelevance();
[email protected]257ab712009-04-14 17:16:24704 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
[email protected]55ce8f12012-05-09 04:44:08705 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
706 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]d1f0a7f2012-06-05 10:26:42707 if (verbatim_relevance > 0) {
708 AddMatchToMap(input_.text(), input_.text(), verbatim_relevance,
709 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
710 did_not_accept_default_suggestion, false, &map);
711 }
[email protected]b9ce8532012-05-24 20:51:15712 const size_t what_you_typed_size = map.size();
[email protected]85b8d6f2012-05-08 20:53:47713 if (!default_provider_suggest_text_.empty()) {
714 AddMatchToMap(input_.text() + default_provider_suggest_text_,
[email protected]55ce8f12012-05-09 04:44:08715 input_.text(), verbatim_relevance + 1,
[email protected]85b8d6f2012-05-08 20:53:47716 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08717 did_not_accept_default_suggestion, false, &map);
initial.commit09911bf2008-07-26 23:55:29718 }
719
[email protected]257ab712009-04-14 17:16:24720 AddHistoryResultsToMap(keyword_history_results_, true,
721 did_not_accept_keyword_suggestion, &map);
722 AddHistoryResultsToMap(default_history_results_, false,
723 did_not_accept_default_suggestion, &map);
724
[email protected]55ce8f12012-05-09 04:44:08725 AddSuggestResultsToMap(keyword_suggest_results_, true, &map);
726 AddSuggestResultsToMap(default_suggest_results_, false, &map);
initial.commit09911bf2008-07-26 23:55:29727
728 // Now add the most relevant matches from the map to |matches_|.
729 matches_.clear();
730 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
731 matches_.push_back(i->second);
732
[email protected]257ab712009-04-14 17:16:24733 AddNavigationResultsToMatches(keyword_navigation_results_, true);
734 AddNavigationResultsToMatches(default_navigation_results_, false);
initial.commit09911bf2008-07-26 23:55:29735
[email protected]b9ce8532012-05-24 20:51:15736 // Allow an additional match for "what you typed" if it's present.
737 const size_t max_total_matches = kMaxMatches + what_you_typed_size;
initial.commit09911bf2008-07-26 23:55:29738 std::partial_sort(matches_.begin(),
739 matches_.begin() + std::min(max_total_matches, matches_.size()),
740 matches_.end(), &AutocompleteMatch::MoreRelevant);
[email protected]3723e6e2012-06-11 21:06:56741
742 // If the top match is effectively 'verbatim' but exceeds the calculated
743 // verbatim relevance, and REQUESTED_URL |input_| has a |desired_tld|
744 // (for example ".com" when the CTRL key is pressed for REQUESTED_URL input),
745 // promote a URL_WHAT_YOU_TYPED match to the top. Otherwise, these matches can
746 // stomp the HistoryURLProvider's similar transient URL_WHAT_YOU_TYPED match,
747 // and CTRL+ENTER will invoke the search instead of the expected navigation.
748 if ((has_suggested_relevance_ || verbatim_relevance_ >= 0) &&
749 input_.type() == AutocompleteInput::REQUESTED_URL &&
750 !input_.desired_tld().empty() && !matches_.empty() &&
751 matches_.front().relevance > CalculateRelevanceForVerbatim() &&
752 matches_.front().fill_into_edit == input_.text()) {
753 AutocompleteMatch match = HistoryURLProvider::SuggestExactInput(
754 this, input_, !HasHTTPScheme(input_.text()));
755 match.relevance = matches_.front().relevance + 1;
756 matches_.insert(matches_.begin(), match);
757 }
758
initial.commit09911bf2008-07-26 23:55:29759 if (matches_.size() > max_total_matches)
[email protected]a28e95662008-11-12 19:19:02760 matches_.erase(matches_.begin() + max_total_matches, matches_.end());
initial.commit09911bf2008-07-26 23:55:29761
[email protected]382a0642012-06-06 06:13:52762 // Check constraints that may be violated by suggested relevances.
[email protected]d1f0a7f2012-06-05 10:26:42763 if (!matches_.empty() &&
[email protected]e6acd002012-06-16 22:27:47764 (has_suggested_relevance_ || verbatim_relevance_ >= 0)) {
[email protected]382a0642012-06-06 06:13:52765 bool reconstruct_matches = false;
[email protected]e6acd002012-06-16 22:27:47766 if (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST ||
767 matches_.front().type == AutocompleteMatch::NAVSUGGEST) {
768 if (matches_.front().inline_autocomplete_offset == string16::npos &&
769 matches_.front().fill_into_edit != input_.text()) {
770 // Disregard suggested relevances if the top result is not inlinable.
771 // For example, input "foo" should not invoke a search for "bar", which
772 // would happen if the "bar" search match outranked all other matches.
773 ApplyCalculatedRelevance();
774 reconstruct_matches = true;
775 } else if (matches_.front().relevance < CalculateRelevanceForVerbatim()) {
776 // Disregard the suggested verbatim relevance if the top score is below
777 // the usual verbatim value. For example, a BarProvider may rely on
778 // SearchProvider's verbatim or inlineable matches for input "foo" to
779 // always outrank its own lowly-ranked non-inlineable "bar" match.
780 verbatim_relevance_ = -1;
781 reconstruct_matches = true;
782 }
783 }
784 if (input_.type() == AutocompleteInput::URL &&
785 matches_.front().relevance > CalculateRelevanceForVerbatim() &&
786 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST ||
787 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED)) {
788 // Disregard the suggested search and verbatim relevances if the input
789 // type is URL and the top match is a highly-ranked search suggestion.
790 // For example, prevent a search for "foo.com" from outranking another
791 // provider's navigation for "foo.com" or "foo.com/url_from_history".
792 // Reconstruction will also ensure that the new top match is inlineable.
793 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true);
794 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false);
[email protected]382a0642012-06-06 06:13:52795 verbatim_relevance_ = -1;
796 reconstruct_matches = true;
797 }
798 if (reconstruct_matches) {
799 ConvertResultsToAutocompleteMatches();
800 return;
801 }
[email protected]d1f0a7f2012-06-05 10:26:42802 }
803
[email protected]cc63dea2008-08-21 20:56:31804 UpdateStarredStateOfMatches();
[email protected]4ab4c7c2010-11-24 04:49:34805 UpdateDone();
[email protected]257ab712009-04-14 17:16:24806}
807
808void SearchProvider::AddNavigationResultsToMatches(
809 const NavigationResults& navigation_results,
810 bool is_keyword) {
811 if (!navigation_results.empty()) {
[email protected]6c535842012-05-15 05:20:55812 // TODO(kochi|msw): Add more navigational results if they get more
813 // meaningful relevance values; see http://b/1170574.
[email protected]d7ad4772012-06-01 03:12:54814 // CompareScoredResults sorts by descending relevance; so use min_element.
[email protected]6c535842012-05-15 05:20:55815 NavigationResults::const_iterator result(
[email protected]d7ad4772012-06-01 03:12:54816 std::min_element(navigation_results.begin(),
[email protected]6c535842012-05-15 05:20:55817 navigation_results.end(),
818 CompareScoredResults()));
819 matches_.push_back(NavigationToMatch(*result, is_keyword));
[email protected]257ab712009-04-14 17:16:24820 }
821}
822
823void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
824 bool is_keyword,
825 int did_not_accept_suggestion,
826 MatchMap* map) {
[email protected]51124552011-07-16 01:37:10827 if (results.empty())
828 return;
829
[email protected]d7ad4772012-06-01 03:12:54830 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
831 (input_.type() == AutocompleteInput::URL);
832 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
[email protected]51124552011-07-16 01:37:10833 bool input_multiple_words = HasMultipleWords(input_text);
834
[email protected]55ce8f12012-05-09 04:44:08835 SuggestResults scored_results;
836 if (!prevent_inline_autocomplete && input_multiple_words) {
837 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
838 // queries if the input also has multiple words. But if we were already
[email protected]51124552011-07-16 01:37:10839 // autocompleting a multi-word, multi-visit query, and the current input is
840 // still a prefix of it, then changing the autocompletion suddenly feels
841 // wrong. To detect this case, first score as if only one word has been
842 // typed, then check for a best result that is an autocompleted, multi-word
843 // query. If we find one, then just keep that score set.
[email protected]55ce8f12012-05-09 04:44:08844 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
845 false, input_text, is_keyword);
846 if ((scored_results[0].relevance() <
847 AutocompleteResult::kLowestDefaultScore) ||
848 !HasMultipleWords(scored_results[0].suggestion()))
849 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:10850 }
[email protected]55ce8f12012-05-09 04:44:08851 if (scored_results.empty())
852 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
853 input_multiple_words, input_text,
854 is_keyword);
855 for (SuggestResults::const_iterator i(scored_results.begin());
856 i != scored_results.end(); ++i) {
857 AddMatchToMap(i->suggestion(), input_text, i->relevance(),
[email protected]51124552011-07-16 01:37:10858 AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion,
[email protected]55ce8f12012-05-09 04:44:08859 is_keyword, map);
[email protected]51124552011-07-16 01:37:10860 }
861}
862
[email protected]55ce8f12012-05-09 04:44:08863SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:10864 const HistoryResults& results,
865 bool base_prevent_inline_autocomplete,
866 bool input_multiple_words,
867 const string16& input_text,
868 bool is_keyword) {
[email protected]810ffba2012-06-12 01:07:48869 AutocompleteClassifier* classifier =
870 AutocompleteClassifierFactory::GetForProfile(profile_);
[email protected]55ce8f12012-05-09 04:44:08871 SuggestResults scored_results;
[email protected]257ab712009-04-14 17:16:24872 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
873 ++i) {
[email protected]51124552011-07-16 01:37:10874 // Don't autocomplete multi-word queries that have only been seen once
875 // unless the user has typed more than one word.
876 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
877 (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term));
878
[email protected]ea3b9a502011-04-04 14:19:37879 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:10880 // when typed. For example, if the user searched for "google.com" and types
881 // "goog", don't autocomplete to the search term "google.com". Otherwise,
882 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:48883 // NOTE: We don't check this in the following cases:
884 // * When inline autocomplete is disabled, we won't be inline
885 // autocompleting this term, so we don't need to worry about confusion as
886 // much. This also prevents calling Classify() again from inside the
887 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:10888 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:48889 // * When the user has typed the whole term, the "what you typed" history
890 // match will outrank us for URL-like inputs anyway, so we need not do
891 // anything special.
[email protected]51124552011-07-16 01:37:10892 if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) {
[email protected]ea3b9a502011-04-04 14:19:37893 AutocompleteMatch match;
[email protected]72874a8d2011-05-11 03:48:54894 classifier->Classify(i->term, string16(), false, false, &match, NULL);
[email protected]2905f742011-10-13 03:51:58895 prevent_inline_autocomplete =
896 match.transition == content::PAGE_TRANSITION_TYPED;
[email protected]ea3b9a502011-04-04 14:19:37897 }
[email protected]51124552011-07-16 01:37:10898
899 int relevance = CalculateRelevanceForHistory(i->time, is_keyword,
900 prevent_inline_autocomplete);
[email protected]55ce8f12012-05-09 04:44:08901 scored_results.push_back(SuggestResult(i->term, relevance));
[email protected]257ab712009-04-14 17:16:24902 }
[email protected]51124552011-07-16 01:37:10903
904 // History returns results sorted for us. However, we may have docked some
905 // results' scores, so things are no longer in order. Do a stable sort to get
906 // things back in order without otherwise disturbing results with equal
907 // scores, then force the scores to be unique, so that the order in which
908 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:08909 std::stable_sort(scored_results.begin(), scored_results.end(),
910 CompareScoredResults());
[email protected]51124552011-07-16 01:37:10911 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:08912 for (SuggestResults::iterator i(scored_results.begin());
913 i != scored_results.end(); ++i) {
914 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
915 i->set_relevance(last_relevance - 1);
916 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:10917 }
918
[email protected]55ce8f12012-05-09 04:44:08919 return scored_results;
[email protected]257ab712009-04-14 17:16:24920}
921
[email protected]55ce8f12012-05-09 04:44:08922void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
923 bool is_keyword,
924 MatchMap* map) {
[email protected]d7ad4772012-06-01 03:12:54925 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
[email protected]55ce8f12012-05-09 04:44:08926 for (size_t i = 0; i < results.size(); ++i) {
[email protected]d7ad4772012-06-01 03:12:54927 AddMatchToMap(results[i].suggestion(), input_text, results[i].relevance(),
[email protected]55ce8f12012-05-09 04:44:08928 AutocompleteMatch::SEARCH_SUGGEST, i, is_keyword, map);
[email protected]257ab712009-04-14 17:16:24929 }
initial.commit09911bf2008-07-26 23:55:29930}
931
[email protected]382a0642012-06-06 06:13:52932int SearchProvider::GetVerbatimRelevance() const {
[email protected]d1f0a7f2012-06-05 10:26:42933 if (verbatim_relevance_ >= 0 && !input_.prevent_inline_autocomplete())
934 return verbatim_relevance_;
[email protected]382a0642012-06-06 06:13:52935 return CalculateRelevanceForVerbatim();
936}
[email protected]d1f0a7f2012-06-05 10:26:42937
[email protected]382a0642012-06-06 06:13:52938int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:47939 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:36940 return 250;
941
initial.commit09911bf2008-07-26 23:55:29942 switch (input_.type()) {
943 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:36944 case AutocompleteInput::QUERY:
945 case AutocompleteInput::FORCED_QUERY:
946 return 1300;
initial.commit09911bf2008-07-26 23:55:29947
948 case AutocompleteInput::REQUESTED_URL:
[email protected]52d08b12009-10-19 18:42:36949 return 1150;
initial.commit09911bf2008-07-26 23:55:29950
951 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:36952 return 850;
initial.commit09911bf2008-07-26 23:55:29953
954 default:
955 NOTREACHED();
956 return 0;
957 }
958}
959
[email protected]51124552011-07-16 01:37:10960int SearchProvider::CalculateRelevanceForHistory(
961 const Time& time,
962 bool is_keyword,
963 bool prevent_inline_autocomplete) const {
[email protected]aa613d62010-11-09 20:40:18964 // The relevance of past searches falls off over time. There are two distinct
965 // equations used. If the first equation is used (searches to the primary
[email protected]51124552011-07-16 01:37:10966 // provider that we want to inline autocomplete), the score starts at 1399 and
967 // falls to 1300. If the second equation is used the relevance of a search 15
968 // minutes ago is discounted 50 points, while the relevance of a search two
969 // weeks ago is discounted 450 points.
[email protected]aa613d62010-11-09 20:40:18970 double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.);
[email protected]51124552011-07-16 01:37:10971 bool is_primary_provider = providers_.is_primary_provider(is_keyword);
972 if (is_primary_provider && !prevent_inline_autocomplete) {
[email protected]aa613d62010-11-09 20:40:18973 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:10974 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:18975 if (elapsed_time < autocomplete_time) {
[email protected]e17511f2011-07-13 14:09:18976 return (is_keyword ? 1599 : 1399) - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:18977 std::pow(elapsed_time / autocomplete_time, 2.5));
978 }
979 elapsed_time -= autocomplete_time;
980 }
981
[email protected]c3a4bd992010-08-18 20:25:01982 const int score_discount =
983 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:29984
[email protected]6c85aa02009-02-27 12:08:09985 // Don't let scores go below 0. Negative relevance scores are meaningful in
986 // a different way.
initial.commit09911bf2008-07-26 23:55:29987 int base_score;
[email protected]51124552011-07-16 01:37:10988 if (is_primary_provider)
[email protected]52d08b12009-10-19 18:42:36989 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:10990 else
991 base_score = 200;
initial.commit09911bf2008-07-26 23:55:29992 return std::max(0, base_score - score_discount);
993}
994
[email protected]55ce8f12012-05-09 04:44:08995int SearchProvider::CalculateRelevanceForSuggestion(bool for_keyword) const {
996 return !providers_.is_primary_provider(for_keyword) ? 100 :
997 ((input_.type() == AutocompleteInput::URL) ? 300 : 600);
initial.commit09911bf2008-07-26 23:55:29998}
999
[email protected]55ce8f12012-05-09 04:44:081000int SearchProvider::CalculateRelevanceForNavigation(bool for_keyword) const {
1001 return providers_.is_primary_provider(for_keyword) ? 800 : 150;
initial.commit09911bf2008-07-26 23:55:291002}
1003
[email protected]a2fedb1e2011-01-25 15:23:361004void SearchProvider::AddMatchToMap(const string16& query_string,
1005 const string16& input_text,
initial.commit09911bf2008-07-26 23:55:291006 int relevance,
[email protected]4c1fb7ec2008-11-13 00:19:001007 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:291008 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:241009 bool is_keyword,
initial.commit09911bf2008-07-26 23:55:291010 MatchMap* map) {
[email protected]92513682011-09-01 06:16:521011 AutocompleteMatch match(this, relevance, false, type);
initial.commit09911bf2008-07-26 23:55:291012 std::vector<size_t> content_param_offsets;
[email protected]85b8d6f2012-05-08 20:53:471013 // Bail out now if we don't actually have a valid provider.
1014 match.keyword = is_keyword ?
[email protected]3954c3a2012-04-10 20:17:551015 providers_.keyword_provider() : providers_.default_provider();
[email protected]85b8d6f2012-05-08 20:53:471016 const TemplateURL* provider_url = match.GetTemplateURL(profile_);
1017 if (provider_url == NULL)
1018 return;
1019
[email protected]70833262011-01-05 23:40:441020 match.contents.assign(query_string);
[email protected]fb5153c52009-07-31 19:40:331021 // We do intra-string highlighting for suggestions - the suggested segment
1022 // will be highlighted, e.g. for input_text = "you" the suggestion may be
1023 // "youtube", so we'll bold the "tube" section: you*tube*.
1024 if (input_text != query_string) {
[email protected]fb5153c52009-07-31 19:40:331025 size_t input_position = match.contents.find(input_text);
[email protected]a2fedb1e2011-01-25 15:23:361026 if (input_position == string16::npos) {
[email protected]fb5153c52009-07-31 19:40:331027 // The input text is not a substring of the query string, e.g. input
1028 // text is "slasdot" and the query string is "slashdot", so we bold the
1029 // whole thing.
1030 match.contents_class.push_back(
1031 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]ec2379162009-06-09 23:58:171032 } else {
[email protected]fb5153c52009-07-31 19:40:331033 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
1034 // "bold" this. Consider modifying the terminology.
1035 // We don't iterate over the string here annotating all matches because
1036 // it looks odd to have every occurrence of a substring that may be as
1037 // short as a single character highlighted in a query suggestion result,
1038 // e.g. for input text "s" and query string "southwest airlines", it
1039 // looks odd if both the first and last s are highlighted.
1040 if (input_position != 0) {
1041 match.contents_class.push_back(
1042 ACMatchClassification(0, ACMatchClassification::NONE));
1043 }
1044 match.contents_class.push_back(
1045 ACMatchClassification(input_position, ACMatchClassification::DIM));
1046 size_t next_fragment_position = input_position + input_text.length();
1047 if (next_fragment_position < query_string.length()) {
1048 match.contents_class.push_back(
1049 ACMatchClassification(next_fragment_position,
1050 ACMatchClassification::NONE));
1051 }
[email protected]ec2379162009-06-09 23:58:171052 }
initial.commit09911bf2008-07-26 23:55:291053 } else {
[email protected]fb5153c52009-07-31 19:40:331054 // Otherwise, we're dealing with the "default search" result which has no
[email protected]70833262011-01-05 23:40:441055 // completion.
[email protected]fb5153c52009-07-31 19:40:331056 match.contents_class.push_back(
1057 ACMatchClassification(0, ACMatchClassification::NONE));
initial.commit09911bf2008-07-26 23:55:291058 }
1059
1060 // When the user forced a query, we need to make sure all the fill_into_edit
1061 // values preserve that property. Otherwise, if the user starts editing a
1062 // suggestion, non-Search results will suddenly appear.
1063 size_t search_start = 0;
1064 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
[email protected]a2fedb1e2011-01-25 15:23:361065 match.fill_into_edit.assign(ASCIIToUTF16("?"));
initial.commit09911bf2008-07-26 23:55:291066 ++search_start;
1067 }
[email protected]c0048b42009-05-04 21:47:171068 if (is_keyword) {
[email protected]033f3422012-03-13 21:24:181069 match.fill_into_edit.append(match.keyword + char16(' '));
1070 search_start += match.keyword.length() + 1;
[email protected]c0048b42009-05-04 21:47:171071 }
initial.commit09911bf2008-07-26 23:55:291072 match.fill_into_edit.append(query_string);
[email protected]2c33dd22010-02-11 21:46:351073 // Not all suggestions start with the original input.
[email protected]55ce8f12012-05-09 04:44:081074 if (!input_.prevent_inline_autocomplete() &&
[email protected]257ab712009-04-14 17:16:241075 !match.fill_into_edit.compare(search_start, input_text.length(),
1076 input_text))
1077 match.inline_autocomplete_offset = search_start + input_text.length();
initial.commit09911bf2008-07-26 23:55:291078
[email protected]85b8d6f2012-05-08 20:53:471079 const TemplateURLRef& search_url = provider_url->url_ref();
[email protected]360ba052012-04-04 17:26:131080 DCHECK(search_url.SupportsReplacement());
[email protected]16fca9b82012-04-23 18:40:261081 match.destination_url = GURL(search_url.ReplaceSearchTerms(query_string,
1082 accepted_suggestion, input_text));
initial.commit09911bf2008-07-26 23:55:291083
1084 // Search results don't look like URLs.
[email protected]2905f742011-10-13 03:51:581085 match.transition = is_keyword ?
1086 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED;
initial.commit09911bf2008-07-26 23:55:291087
1088 // Try to add |match| to |map|. If a match for |query_string| is already in
1089 // |map|, replace it if |match| is more relevant.
1090 // NOTE: Keep this ToLower() call in sync with url_database.cc.
1091 const std::pair<MatchMap::iterator, bool> i = map->insert(
[email protected]a2fedb1e2011-01-25 15:23:361092 std::pair<string16, AutocompleteMatch>(
[email protected]503d03872011-05-06 08:36:261093 base::i18n::ToLower(query_string), match));
initial.commit09911bf2008-07-26 23:55:291094 // NOTE: We purposefully do a direct relevance comparison here instead of
1095 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
1096 // first" rather than "items alphabetically first" when the scores are equal.
1097 // The only case this matters is when a user has results with the same score
1098 // that differ only by capitalization; because the history system returns
1099 // results sorted by recency, this means we'll pick the most recent such
1100 // result even if the precision of our relevance score is too low to
1101 // distinguish the two.
1102 if (!i.second && (match.relevance > i.first->second.relevance))
1103 i.first->second = match;
1104}
1105
1106AutocompleteMatch SearchProvider::NavigationToMatch(
1107 const NavigationResult& navigation,
[email protected]257ab712009-04-14 17:16:241108 bool is_keyword) {
[email protected]371dab12012-06-01 03:23:551109 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
[email protected]55ce8f12012-05-09 04:44:081110 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]4c1fb7ec2008-11-13 00:19:001111 AutocompleteMatch::NAVSUGGEST);
[email protected]55ce8f12012-05-09 04:44:081112 match.destination_url = navigation.url();
[email protected]371dab12012-06-01 03:23:551113
1114 // First look for the user's input inside the fill_into_edit as it would be
1115 // without trimming the scheme, so we can find matches at the beginning of the
1116 // scheme.
1117 const string16 untrimmed_fill_into_edit(
1118 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1119 StringForURLDisplay(navigation.url(), true, false)));
1120 const URLPrefix* prefix =
1121 URLPrefix::BestURLPrefix(untrimmed_fill_into_edit, input);
1122 size_t match_start = (prefix == NULL) ?
1123 untrimmed_fill_into_edit.find(input) : prefix->prefix.length();
1124 size_t inline_autocomplete_offset = (prefix == NULL) ?
1125 string16::npos : (match_start + input.length());
1126 bool trim_http = !HasHTTPScheme(input) && (!prefix || (match_start != 0));
1127
1128 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1129 // Otherwise, user edits to a suggestion would show non-Search results.
1130 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
1131 match.fill_into_edit = ASCIIToUTF16("?");
1132 if (inline_autocomplete_offset != string16::npos)
1133 ++inline_autocomplete_offset;
1134 }
1135
1136 const std::string languages(
1137 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
1138 const net::FormatUrlTypes format_types =
1139 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
1140 match.fill_into_edit +=
1141 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1142 net::FormatUrl(navigation.url(), languages, format_types,
1143 net::UnescapeRule::SPACES, NULL, NULL,
1144 &inline_autocomplete_offset));
1145 if (!input_.prevent_inline_autocomplete())
1146 match.inline_autocomplete_offset = inline_autocomplete_offset;
1147 DCHECK((match.inline_autocomplete_offset == string16::npos) ||
1148 (match.inline_autocomplete_offset <= match.fill_into_edit.length()));
1149
1150 match.contents = net::FormatUrl(navigation.url(), languages,
1151 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start);
1152 // If the first match in the untrimmed string was inside a scheme that we
1153 // trimmed, look for a subsequent match.
1154 if (match_start == string16::npos)
1155 match_start = match.contents.find(input);
1156 // Safe if |match_start| is npos; also safe if the input is longer than the
1157 // remaining contents after |match_start|.
1158 AutocompleteMatch::ClassifyLocationInString(match_start, input.length(),
1159 match.contents.length(), ACMatchClassification::URL,
1160 &match.contents_class);
initial.commit09911bf2008-07-26 23:55:291161
[email protected]55ce8f12012-05-09 04:44:081162 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551163 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1164 ACMatchClassification::NONE, &match.description_class);
initial.commit09911bf2008-07-26 23:55:291165 return match;
1166}
[email protected]4ab4c7c2010-11-24 04:49:341167
1168void SearchProvider::UpdateDone() {
1169 // We're done when there are no more suggest queries pending (this is set to 1
1170 // when the timer is started) and we're not waiting on instant.
1171 done_ = ((suggest_results_pending_ == 0) &&
1172 (instant_finalized_ || !InstantController::IsEnabled(profile_)));
1173}