blob: ef9d6ace89cd2c1b9eb07a99359c803e4fb49962 [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]1cb2dac2010-03-08 21:49:1518#include "base/utf_string_conversions.h"
[email protected]ea3b9a502011-04-04 14:19:3719#include "chrome/browser/autocomplete/autocomplete_classifier.h"
[email protected]f5b95ba92012-03-27 14:05:1920#include "chrome/browser/autocomplete/autocomplete_field_trial.h"
[email protected]9ac40092010-10-27 23:05:2621#include "chrome/browser/autocomplete/autocomplete_match.h"
[email protected]2c812ba02011-07-14 00:23:1522#include "chrome/browser/autocomplete/keyword_provider.h"
[email protected]ce560f82009-06-03 09:39:4423#include "chrome/browser/history/history.h"
[email protected]10c2d692012-05-11 05:32:2324#include "chrome/browser/history/in_memory_database.h"
[email protected]4ab4c7c2010-11-24 04:49:3425#include "chrome/browser/instant/instant_controller.h"
[email protected]f870a322009-01-16 21:47:2726#include "chrome/browser/net/url_fixer_upper.h"
[email protected]37858e52010-08-26 00:22:0227#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3328#include "chrome/browser/profiles/profile.h"
[email protected]a0ad93ea2012-05-07 22:11:5329#include "chrome/browser/search_engines/search_engine_type.h"
[email protected]8e5c89a2011-06-07 18:13:3330#include "chrome/browser/search_engines/template_url_service.h"
31#include "chrome/browser/search_engines/template_url_service_factory.h"
initial.commit09911bf2008-07-26 23:55:2932#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0233#include "chrome/common/url_constants.h"
[email protected]36aea2702011-10-26 01:12:2234#include "content/public/common/url_fetcher.h"
initial.commit09911bf2008-07-26 23:55:2935#include "googleurl/src/url_util.h"
[email protected]34ac8f32009-02-22 23:03:2736#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2937#include "net/base/escape.h"
[email protected]d3cf8682f02012-02-29 23:29:3438#include "net/base/load_flags.h"
[email protected]319d9e6f2009-02-18 19:47:2139#include "net/http/http_response_headers.h"
40#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1741#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2942
[email protected]e1acf6f2008-10-27 20:43:3343using base::Time;
44using base::TimeDelta;
45
[email protected]51124552011-07-16 01:37:1046namespace {
47
48bool HasMultipleWords(const string16& text) {
49 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
50 bool found_word = false;
51 if (i.Init()) {
52 while (i.Advance()) {
53 if (i.IsWord()) {
54 if (found_word)
55 return true;
56 found_word = true;
57 }
58 }
59 }
60 return false;
61}
62
63};
64
[email protected]033f3422012-03-13 21:24:1865
[email protected]3954c3a2012-04-10 20:17:5566// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:5867
[email protected]85b8d6f2012-05-08 20:53:4768SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
69 : template_url_service_(template_url_service) {
70}
71
72const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
73 return default_provider_.empty() ? NULL :
74 template_url_service_->GetTemplateURLForKeyword(default_provider_);
75}
76
77const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
78 return keyword_provider_.empty() ? NULL :
79 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:2480}
81
[email protected]3954c3a2012-04-10 20:17:5582
83// SearchProvider -------------------------------------------------------------
84
85// static
86const int SearchProvider::kDefaultProviderURLFetcherID = 1;
87// static
88const int SearchProvider::kKeywordProviderURLFetcherID = 2;
89// static
90bool SearchProvider::query_suggest_immediately_ = false;
91
[email protected]601858c02010-09-01 17:08:2092SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile)
93 : AutocompleteProvider(listener, profile, "Search"),
[email protected]85b8d6f2012-05-08 20:53:4794 providers_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]601858c02010-09-01 17:08:2095 suggest_results_pending_(0),
[email protected]8e5cc282010-12-05 18:11:3996 have_suggest_results_(false),
[email protected]4ab4c7c2010-11-24 04:49:3497 instant_finalized_(false) {
[email protected]f5b95ba92012-03-27 14:05:1998 // We use GetSuggestNumberOfGroups() as the group ID to mean "not in field
99 // trial." Field trial groups run from 0 to GetSuggestNumberOfGroups() - 1
100 // (inclusive).
101 int suggest_field_trial_group_number =
102 AutocompleteFieldTrial::GetSuggestNumberOfGroups();
103 if (AutocompleteFieldTrial::InSuggestFieldTrial()) {
104 suggest_field_trial_group_number =
105 AutocompleteFieldTrial::GetSuggestGroupNameAsNumber();
106 }
107 // Add a beacon to the logs that'll allow us to identify later what
108 // suggest field trial group a user is in. Do this by incrementing a
109 // bucket in a histogram, where the bucket represents the user's
110 // suggest group id.
111 UMA_HISTOGRAM_ENUMERATION(
112 "Omnibox.SuggestFieldTrialBeacon",
113 suggest_field_trial_group_number,
114 AutocompleteFieldTrial::GetSuggestNumberOfGroups() + 1);
[email protected]4ab4c7c2010-11-24 04:49:34115}
116
[email protected]a2fedb1e2011-01-25 15:23:36117void SearchProvider::FinalizeInstantQuery(const string16& input_text,
118 const string16& suggest_text) {
[email protected]4ab4c7c2010-11-24 04:49:34119 if (done_ || instant_finalized_)
120 return;
121
122 instant_finalized_ = true;
123 UpdateDone();
124
[email protected]e918c112010-12-08 23:03:49125 if (input_text.empty()) {
[email protected]4ab4c7c2010-11-24 04:49:34126 // We only need to update the listener if we're actually done.
127 if (done_)
128 listener_->OnProviderUpdate(false);
129 return;
130 }
131
[email protected]9e789742011-01-10 23:27:32132 default_provider_suggest_text_ = suggest_text;
133
[email protected]a2fedb1e2011-01-25 15:23:36134 string16 adjusted_input_text(input_text);
[email protected]e918c112010-12-08 23:03:49135 AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(),
136 &adjusted_input_text);
137
[email protected]a2fedb1e2011-01-25 15:23:36138 const string16 text = adjusted_input_text + suggest_text;
[email protected]4ab4c7c2010-11-24 04:49:34139 // Remove any matches that are identical to |text|. We don't use the
140 // destination_url for comparison as it varies depending upon the index passed
141 // to TemplateURL::ReplaceSearchTerms.
142 for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) {
143 if (((i->type == AutocompleteMatch::SEARCH_HISTORY) ||
144 (i->type == AutocompleteMatch::SEARCH_SUGGEST)) &&
145 (i->fill_into_edit == text)) {
[email protected]e030de62010-11-24 05:41:19146 i = matches_.erase(i);
[email protected]4ab4c7c2010-11-24 04:49:34147 } else {
148 ++i;
149 }
150 }
151
[email protected]55ce8f12012-05-09 04:44:08152 // Add the new instant suggest result. We give it a rank higher than
[email protected]4ab4c7c2010-11-24 04:49:34153 // SEARCH_WHAT_YOU_TYPED so that it gets autocompleted.
154 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
155 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
156 TemplateURLRef::NO_SUGGESTION_CHOSEN;
157 MatchMap match_map;
[email protected]e918c112010-12-08 23:03:49158 AddMatchToMap(text, adjusted_input_text,
159 CalculateRelevanceForWhatYouTyped() + 1,
[email protected]4ab4c7c2010-11-24 04:49:34160 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08161 did_not_accept_default_suggestion, false, &match_map);
[email protected]4ab4c7c2010-11-24 04:49:34162 DCHECK_EQ(1u, match_map.size());
163 matches_.push_back(match_map.begin()->second);
164
165 listener_->OnProviderUpdate(true);
[email protected]601858c02010-09-01 17:08:20166}
167
initial.commit09911bf2008-07-26 23:55:29168void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27169 bool minimal_changes) {
initial.commit09911bf2008-07-26 23:55:29170 matches_.clear();
171
[email protected]ea3b9a502011-04-04 14:19:37172 instant_finalized_ =
173 (input.matches_requested() != AutocompleteInput::ALL_MATCHES);
[email protected]4ab4c7c2010-11-24 04:49:34174
[email protected]6c85aa02009-02-27 12:08:09175 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29176 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
177 Stop();
178 return;
179 }
180
[email protected]257ab712009-04-14 17:16:24181 keyword_input_text_.clear();
182 const TemplateURL* keyword_provider =
183 KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input,
184 &keyword_input_text_);
[email protected]8d457132010-11-04 18:13:40185 if (keyword_input_text_.empty())
[email protected]257ab712009-04-14 17:16:24186 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24187
[email protected]85b8d6f2012-05-08 20:53:47188 TemplateURLService* model = providers_.template_url_service();
189 DCHECK(model);
190 model->Load();
191 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]9b74ab52012-03-30 16:08:07192 if (default_provider && !default_provider->SupportsReplacement())
[email protected]257ab712009-04-14 17:16:24193 default_provider = NULL;
194
195 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18196 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24197
198 if (!default_provider && !keyword_provider) {
199 // No valid providers.
initial.commit09911bf2008-07-26 23:55:29200 Stop();
201 return;
202 }
203
204 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24205 // or the providers, abort the query.
[email protected]85b8d6f2012-05-08 20:53:47206 string16 default_provider_keyword(default_provider ?
207 default_provider->keyword() : string16());
208 string16 keyword_provider_keyword(keyword_provider ?
209 keyword_provider->keyword() : string16());
[email protected]9e789742011-01-10 23:27:32210 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47211 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]9e789742011-01-10 23:27:32212 if (done_)
213 default_provider_suggest_text_.clear();
214 else
215 Stop();
[email protected]257ab712009-04-14 17:16:24216 }
initial.commit09911bf2008-07-26 23:55:29217
[email protected]85b8d6f2012-05-08 20:53:47218 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29219
220 if (input.text().empty()) {
221 // User typed "?" alone. Give them a placeholder result indicating what
222 // this syntax does.
[email protected]257ab712009-04-14 17:16:24223 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00224 AutocompleteMatch match;
225 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36226 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24227 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35228 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47229 match.keyword = providers_.default_provider();
[email protected]257ab712009-04-14 17:16:24230 matches_.push_back(match);
231 }
initial.commit09911bf2008-07-26 23:55:29232 Stop();
233 return;
234 }
235
236 input_ = input;
237
[email protected]8d457132010-11-04 18:13:40238 DoHistoryQuery(minimal_changes);
[email protected]8deeb952008-10-09 18:21:27239 StartOrStopSuggestQuery(minimal_changes);
initial.commit09911bf2008-07-26 23:55:29240 ConvertResultsToAutocompleteMatches();
241}
242
[email protected]55ce8f12012-05-09 04:44:08243SearchProvider::Result::Result(int relevance) : relevance_(relevance) {}
244SearchProvider::Result::~Result() {}
245
246SearchProvider::SuggestResult::SuggestResult(const string16& suggestion,
247 int relevance)
248 : Result(relevance),
249 suggestion_(suggestion) {
250}
251
252SearchProvider::SuggestResult::~SuggestResult() {}
253
254SearchProvider::NavigationResult::NavigationResult(const GURL& url,
255 const string16& description,
256 int relevance)
257 : Result(relevance),
258 url_(url),
259 description_(description) {
260 DCHECK(url_.is_valid());
261}
262
263SearchProvider::NavigationResult::~NavigationResult() {}
264
265class SearchProvider::CompareScoredResults {
[email protected]51124552011-07-16 01:37:10266 public:
[email protected]55ce8f12012-05-09 04:44:08267 bool operator()(const Result& a, const Result& b) {
[email protected]51124552011-07-16 01:37:10268 // Sort in descending relevance order.
[email protected]55ce8f12012-05-09 04:44:08269 return a.relevance() > b.relevance();
[email protected]51124552011-07-16 01:37:10270 }
271};
272
initial.commit09911bf2008-07-26 23:55:29273void SearchProvider::Run() {
274 // Start a new request with the current input.
275 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24276 suggest_results_pending_ = 0;
[email protected]a0ad93ea2012-05-07 22:11:53277 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]85b8d6f2012-05-08 20:53:47278 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
279 if (default_url && !default_url->suggestions_url().empty()) {
[email protected]257ab712009-04-14 17:16:24280 suggest_results_pending_++;
[email protected]033f3422012-03-13 21:24:18281 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
[email protected]85b8d6f2012-05-08 20:53:47282 default_url->suggestions_url_ref(), input_.text()));
[email protected]3954c3a2012-04-10 20:17:55283 }
[email protected]85b8d6f2012-05-08 20:53:47284 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
285 if (keyword_url && !keyword_url->suggestions_url().empty()) {
[email protected]3954c3a2012-04-10 20:17:55286 suggest_results_pending_++;
287 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
[email protected]85b8d6f2012-05-08 20:53:47288 keyword_url->suggestions_url_ref(), keyword_input_text_));
[email protected]257ab712009-04-14 17:16:24289 }
[email protected]85b8d6f2012-05-08 20:53:47290
291 // Both the above can fail if the providers have been modified or deleted
292 // since the query began.
293 if (suggest_results_pending_ == 0) {
294 UpdateDone();
295 // We only need to update the listener if we're actually done.
296 if (done_)
297 listener_->OnProviderUpdate(false);
298 }
initial.commit09911bf2008-07-26 23:55:29299}
300
301void SearchProvider::Stop() {
initial.commit09911bf2008-07-26 23:55:29302 StopSuggest();
[email protected]55ce8f12012-05-09 04:44:08303 ClearResults();
initial.commit09911bf2008-07-26 23:55:29304 done_ = true;
[email protected]9e789742011-01-10 23:27:32305 default_provider_suggest_text_.clear();
initial.commit09911bf2008-07-26 23:55:29306}
307
[email protected]0e9e8782012-05-15 23:01:51308void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
309 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
310 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
311 new_entry.set_provider(AsOmniboxEventProviderType());
312 new_entry.set_provider_done(done_);
313}
314
[email protected]10c2d692012-05-11 05:32:23315void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) {
initial.commit09911bf2008-07-26 23:55:29316 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24317 suggest_results_pending_--;
[email protected]1cb2dac2010-03-08 21:49:15318 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06319 const net::HttpResponseHeaders* const response_headers =
[email protected]7cc6e5632011-10-25 17:56:12320 source->GetResponseHeaders();
[email protected]c530c852011-10-24 18:18:34321 std::string json_data;
322 source->GetResponseAsString(&json_data);
[email protected]6c85aa02009-02-27 12:08:09323 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
324 // files in non-UTF-8 encodings. The actual encoding is usually specified in
325 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06326 if (response_headers) {
327 std::string charset;
328 if (response_headers->GetCharset(&charset)) {
[email protected]a2fedb1e2011-01-25 15:23:36329 string16 data_16;
[email protected]ec9207d32008-09-26 00:51:06330 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]c530c852011-10-24 18:18:34331 if (base::CodepageToUTF16(json_data, charset.c_str(),
[email protected]a2fedb1e2011-01-25 15:23:36332 base::OnStringConversionError::FAIL,
333 &data_16))
334 json_data = UTF16ToUTF8(data_16);
[email protected]ec9207d32008-09-26 00:51:06335 }
336 }
337
[email protected]55ce8f12012-05-09 04:44:08338 bool is_keyword = (source == keyword_fetcher_.get());
339 SuggestResults* suggest_results =
340 is_keyword ? &keyword_suggest_results_ : &default_suggest_results_;
[email protected]257ab712009-04-14 17:16:24341
[email protected]013e9a02012-05-18 20:27:10342 const bool request_succeeded =
343 source->GetStatus().is_success() && source->GetResponseCode() == 200;
344 if (request_succeeded) {
[email protected]b4cebf82008-12-29 19:59:08345 JSONStringValueSerializer deserializer(json_data);
346 deserializer.set_allow_trailing_comma(true);
[email protected]ba399672010-04-06 15:42:39347 scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL));
[email protected]55ce8f12012-05-09 04:44:08348 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
349 have_suggest_results_ = root_val.get() &&
350 ParseSuggestResults(root_val.get(), is_keyword, input, suggest_results);
[email protected]a0ad93ea2012-05-07 22:11:53351 }
352
353 // Record response time for suggest requests sent to Google. We care
354 // only about the common case: the Google default provider used in
355 // non-keyword mode.
[email protected]85b8d6f2012-05-08 20:53:47356 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
[email protected]55ce8f12012-05-09 04:44:08357 if (!is_keyword && default_url &&
[email protected]85b8d6f2012-05-08 20:53:47358 (default_url->prepopulate_id() == SEARCH_ENGINE_GOOGLE)) {
[email protected]013e9a02012-05-18 20:27:10359 const base::TimeDelta elapsed_time =
360 base::TimeTicks::Now() - time_suggest_request_sent_;
361 if (request_succeeded) {
362 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime",
363 elapsed_time);
364 } else {
365 UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime",
366 elapsed_time);
367 }
[email protected]b4cebf82008-12-29 19:59:08368 }
369
initial.commit09911bf2008-07-26 23:55:29370 ConvertResultsToAutocompleteMatches();
[email protected]257ab712009-04-14 17:16:24371 listener_->OnProviderUpdate(!suggest_results->empty());
initial.commit09911bf2008-07-26 23:55:29372}
373
[email protected]601858c02010-09-01 17:08:20374SearchProvider::~SearchProvider() {
375}
376
[email protected]8d457132010-11-04 18:13:40377void SearchProvider::DoHistoryQuery(bool minimal_changes) {
378 // The history query results are synchronous, so if minimal_changes is true,
379 // we still have the last results and don't need to do anything.
380 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29381 return;
382
[email protected]8d457132010-11-04 18:13:40383 keyword_history_results_.clear();
384 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29385
[email protected]8d457132010-11-04 18:13:40386 HistoryService* const history_service =
387 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
388 history::URLDatabase* url_db = history_service ?
389 history_service->InMemoryDatabase() : NULL;
390 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29391 return;
392
[email protected]51124552011-07-16 01:37:10393 // Request history for both the keyword and default provider. We grab many
394 // more matches than we'll ultimately clamp to so that if there are several
395 // recent multi-word matches who scores are lowered (see
396 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
397 // matches. Note that this doesn't fix the problem entirely, but merely
398 // limits it to cases with a very large number of such multi-word matches; for
399 // now, this seems OK compared with the complexity of a real fix, which would
400 // require multiple searches and tracking of "single- vs. multi-word" in the
401 // database.
402 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47403 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
404 if (default_url) {
405 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
406 num_matches, &default_history_results_);
[email protected]257ab712009-04-14 17:16:24407 }
[email protected]85b8d6f2012-05-08 20:53:47408 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
409 if (keyword_url) {
410 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]3954c3a2012-04-10 20:17:55411 keyword_input_text_, num_matches, &keyword_history_results_);
412 }
initial.commit09911bf2008-07-26 23:55:29413}
414
[email protected]8deeb952008-10-09 18:21:27415void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]6c85aa02009-02-27 12:08:09416 // Don't send any queries to the server until some time has elapsed after
417 // the last keypress, to avoid flooding the server with requests we are
418 // likely to end up throwing away anyway.
[email protected]02c3f6832011-11-16 18:37:40419 const int kQueryDelayMs = 200;
[email protected]6c85aa02009-02-27 12:08:09420
[email protected]83c726482008-09-10 06:36:34421 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29422 StopSuggest();
[email protected]55ce8f12012-05-09 04:44:08423 ClearResults();
initial.commit09911bf2008-07-26 23:55:29424 return;
425 }
426
427 // For the minimal_changes case, if we finished the previous query and still
428 // have its results, or are allowed to keep running it, just do that, rather
429 // than starting a new query.
430 if (minimal_changes &&
[email protected]ea3b9a502011-04-04 14:19:37431 (have_suggest_results_ ||
432 (!done_ &&
433 input_.matches_requested() == AutocompleteInput::ALL_MATCHES)))
initial.commit09911bf2008-07-26 23:55:29434 return;
435
436 // We can't keep running any previous query, so halt it.
437 StopSuggest();
[email protected]55ce8f12012-05-09 04:44:08438 ClearResults();
initial.commit09911bf2008-07-26 23:55:29439
440 // We can't start a new query if we're only allowed synchronous results.
[email protected]ea3b9a502011-04-04 14:19:37441 if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES)
initial.commit09911bf2008-07-26 23:55:29442 return;
443
[email protected]257ab712009-04-14 17:16:24444 // We'll have at least one pending fetch. Set it to 1 now, but the value is
445 // correctly set in Run. As Run isn't invoked immediately we need to set this
446 // now, else we won't think we're waiting on results from the server when we
447 // really are.
448 suggest_results_pending_ = 1;
449
initial.commit09911bf2008-07-26 23:55:29450 // Kick off a timer that will start the URL fetch if it completes before
451 // the user types another character.
[email protected]b547666d2009-04-23 16:37:58452 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs;
[email protected]d323a172011-09-02 18:23:02453 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay), this,
454 &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29455}
456
[email protected]83c726482008-09-10 06:36:34457bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55458 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
459 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47460 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
461 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34462 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47463 ((!default_url || default_url->suggestions_url().empty()) &&
464 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34465 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
466 return false;
467
[email protected]cac59d32010-08-09 23:23:14468 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34469 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34470
[email protected]cac59d32010-08-09 23:23:14471 // FORCED_QUERY means the user is explicitly asking us to search for this, so
472 // we assume it isn't a URL and/or there isn't private data.
473 if (input_.type() == AutocompleteInput::FORCED_QUERY)
474 return true;
[email protected]83c726482008-09-10 06:36:34475
[email protected]cac59d32010-08-09 23:23:14476 // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a
477 // scheme that isn't http/https/ftp, we shouldn't send it. Sending things
478 // like file: and data: is both a waste of time and a disclosure of
479 // potentially private, local data. Other "schemes" may actually be
480 // usernames, and we don't want to send passwords. If the scheme is OK, we
481 // still need to check other cases below. If this is QUERY, then the presence
482 // of these schemes means the user explicitly typed one, and thus this is
483 // probably a URL that's being entered and happens to currently be invalid --
484 // in which case we again want to run our checks below. Other QUERY cases are
485 // less likely to be URLs and thus we assume we're OK.
[email protected]a2fedb1e2011-01-25 15:23:36486 if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) &&
487 !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
488 !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme))
[email protected]cac59d32010-08-09 23:23:14489 return (input_.type() == AutocompleteInput::QUERY);
490
491 // Don't send URLs with usernames, queries or refs. Some of these are
492 // private, and the Suggest server is unlikely to have any useful results
493 // for any of them. Also don't send URLs with ports, as we may initially
494 // think that a username + password is a host + port (and we don't want to
495 // send usernames/passwords), and even if the port really is a port, the
496 // server is once again unlikely to have and useful results.
497 const url_parse::Parsed& parts = input_.parts();
498 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
499 parts.query.is_nonempty() || parts.ref.is_nonempty())
500 return false;
501
502 // Don't send anything for https except the hostname. Hostnames are OK
503 // because they are visible when the TCP connection is established, but the
504 // specific path may reveal private information.
[email protected]a2fedb1e2011-01-25 15:23:36505 if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
506 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14507 return false;
[email protected]83c726482008-09-10 06:36:34508
509 return true;
510}
511
initial.commit09911bf2008-07-26 23:55:29512void SearchProvider::StopSuggest() {
[email protected]257ab712009-04-14 17:16:24513 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14514 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24515 // Stop any in-progress URL fetches.
516 keyword_fetcher_.reset();
517 default_fetcher_.reset();
[email protected]55ce8f12012-05-09 04:44:08518}
519
520void SearchProvider::ClearResults() {
[email protected]257ab712009-04-14 17:16:24521 keyword_suggest_results_.clear();
522 default_suggest_results_.clear();
523 keyword_navigation_results_.clear();
524 default_navigation_results_.clear();
initial.commit09911bf2008-07-26 23:55:29525 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29526}
527
[email protected]7cc6e5632011-10-25 17:56:12528content::URLFetcher* SearchProvider::CreateSuggestFetcher(
529 int id,
[email protected]3954c3a2012-04-10 20:17:55530 const TemplateURLRef& suggestions_url,
[email protected]7cc6e5632011-10-25 17:56:12531 const string16& text) {
[email protected]360ba052012-04-04 17:26:13532 DCHECK(suggestions_url.SupportsReplacement());
[email protected]033f3422012-03-13 21:24:18533 content::URLFetcher* fetcher = content::URLFetcher::Create(id,
[email protected]16fca9b82012-04-23 18:40:26534 GURL(suggestions_url.ReplaceSearchTerms(text,
535 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())),
[email protected]033f3422012-03-13 21:24:18536 content::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12537 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34538 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]257ab712009-04-14 17:16:24539 fetcher->Start();
540 return fetcher;
541}
542
543bool SearchProvider::ParseSuggestResults(Value* root_val,
544 bool is_keyword,
[email protected]a2fedb1e2011-01-25 15:23:36545 const string16& input_text,
[email protected]257ab712009-04-14 17:16:24546 SuggestResults* suggest_results) {
[email protected]0de764e2011-08-26 01:54:00547 if (!root_val->IsType(Value::TYPE_LIST))
initial.commit09911bf2008-07-26 23:55:29548 return false;
[email protected]0de764e2011-08-26 01:54:00549 ListValue* root_list = static_cast<ListValue*>(root_val);
initial.commit09911bf2008-07-26 23:55:29550
[email protected]dc9a6762010-08-16 07:13:53551 string16 query_str;
[email protected]55ce8f12012-05-09 04:44:08552 ListValue* result_list = NULL;
553 if ((root_list->GetSize() < 2) || !root_list->GetString(0, &query_str) ||
554 (query_str != input_text) || !root_list->GetList(1, &result_list))
initial.commit09911bf2008-07-26 23:55:29555 return false;
556
[email protected]55ce8f12012-05-09 04:44:08557 // 3rd element: Description list.
initial.commit09911bf2008-07-26 23:55:29558 ListValue* description_list = NULL;
[email protected]55ce8f12012-05-09 04:44:08559 if (root_list->GetSize() > 2)
560 root_list->GetList(2, &description_list);
initial.commit09911bf2008-07-26 23:55:29561
[email protected]55ce8f12012-05-09 04:44:08562 // 4th element: Disregard the query URL list for now.
initial.commit09911bf2008-07-26 23:55:29563
[email protected]55ce8f12012-05-09 04:44:08564 // 5th element: Optional key-value pairs from the Suggest server.
565 DictionaryValue* dict_val = NULL;
initial.commit09911bf2008-07-26 23:55:29566 ListValue* type_list = NULL;
[email protected]55ce8f12012-05-09 04:44:08567 if (root_list->GetSize() > 4 && root_list->GetDictionary(4, &dict_val)) {
568 // Parse Google Suggest specific type extension.
569 const std::string kGoogleSuggestType("google:suggesttype");
570 dict_val->GetList(kGoogleSuggestType, &type_list);
initial.commit09911bf2008-07-26 23:55:29571 }
572
[email protected]55ce8f12012-05-09 04:44:08573 // Add the suggestions in reverse order to assist relevance calculation.
574 for (size_t i = result_list->GetSize(); i > 0; --i) {
575 size_t current_index = i - 1;
576 string16 suggestion;
577 if (!result_list->GetString(current_index, &suggestion))
initial.commit09911bf2008-07-26 23:55:29578 return false;
579
[email protected]8e81f5092010-09-29 23:19:40580 // Google search may return empty suggestions for weird input characters,
[email protected]55ce8f12012-05-09 04:44:08581 // they make no sense at all and can cause problems in our code.
[email protected]8e81f5092010-09-29 23:19:40582 // See http://crbug.com/56214
[email protected]55ce8f12012-05-09 04:44:08583 if (!suggestion.length())
[email protected]8e81f5092010-09-29 23:19:40584 continue;
585
[email protected]55ce8f12012-05-09 04:44:08586 std::string type;
587 if (type_list && type_list->GetString(current_index, &type) &&
588 (type == "NAVIGATION")) {
589 string16 description;
590 NavigationResults& navigation_results = is_keyword ?
591 keyword_navigation_results_ : default_navigation_results_;
592 if ((navigation_results.size() < kMaxMatches) && description_list &&
593 description_list->GetString(current_index, &description)) {
[email protected]16afe222009-01-08 18:57:45594 // We can't blindly trust the URL coming from the server to be valid.
[email protected]55ce8f12012-05-09 04:44:08595 GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion),
596 std::string()));
597 if (url.is_valid()) {
598 // Increment the relevance for successive results to preserve order.
599 int relevance = CalculateRelevanceForNavigation(is_keyword) +
600 navigation_results.size();
601 navigation_results.push_back(
602 NavigationResult(url, description, relevance));
[email protected]dc9a6762010-08-16 07:13:53603 }
initial.commit09911bf2008-07-26 23:55:29604 }
605 } else {
606 // TODO(kochi): Currently we treat a calculator result as a query, but it
607 // is better to have better presentation for caluculator results.
[email protected]55ce8f12012-05-09 04:44:08608 if (suggest_results->size() < kMaxMatches) {
609 // Increment the relevance for successive results to preserve order.
610 int relevance = CalculateRelevanceForSuggestion(is_keyword) +
611 suggest_results->size();
612 suggest_results->push_back(SuggestResult(suggestion, relevance));
613 }
initial.commit09911bf2008-07-26 23:55:29614 }
615 }
616
initial.commit09911bf2008-07-26 23:55:29617 return true;
618}
619
620void SearchProvider::ConvertResultsToAutocompleteMatches() {
621 // Convert all the results to matches and add them to a map, so we can keep
622 // the most relevant match for each result.
623 MatchMap map;
[email protected]257ab712009-04-14 17:16:24624 const Time no_time;
625 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ?
initial.commit09911bf2008-07-26 23:55:29626 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
627 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]257ab712009-04-14 17:16:24628 // Keyword what you typed results are handled by the KeywordProvider.
initial.commit09911bf2008-07-26 23:55:29629
[email protected]55ce8f12012-05-09 04:44:08630 int verbatim_relevance = CalculateRelevanceForWhatYouTyped();
[email protected]257ab712009-04-14 17:16:24631 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
[email protected]55ce8f12012-05-09 04:44:08632 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
633 TemplateURLRef::NO_SUGGESTION_CHOSEN;
634 AddMatchToMap(input_.text(), input_.text(), verbatim_relevance,
[email protected]85b8d6f2012-05-08 20:53:47635 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
[email protected]55ce8f12012-05-09 04:44:08636 did_not_accept_default_suggestion, false, &map);
[email protected]85b8d6f2012-05-08 20:53:47637 if (!default_provider_suggest_text_.empty()) {
638 AddMatchToMap(input_.text() + default_provider_suggest_text_,
[email protected]55ce8f12012-05-09 04:44:08639 input_.text(), verbatim_relevance + 1,
[email protected]85b8d6f2012-05-08 20:53:47640 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08641 did_not_accept_default_suggestion, false, &map);
initial.commit09911bf2008-07-26 23:55:29642 }
643
[email protected]257ab712009-04-14 17:16:24644 AddHistoryResultsToMap(keyword_history_results_, true,
645 did_not_accept_keyword_suggestion, &map);
646 AddHistoryResultsToMap(default_history_results_, false,
647 did_not_accept_default_suggestion, &map);
648
[email protected]55ce8f12012-05-09 04:44:08649 AddSuggestResultsToMap(keyword_suggest_results_, true, &map);
650 AddSuggestResultsToMap(default_suggest_results_, false, &map);
initial.commit09911bf2008-07-26 23:55:29651
652 // Now add the most relevant matches from the map to |matches_|.
653 matches_.clear();
654 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
655 matches_.push_back(i->second);
656
[email protected]257ab712009-04-14 17:16:24657 AddNavigationResultsToMatches(keyword_navigation_results_, true);
658 AddNavigationResultsToMatches(default_navigation_results_, false);
initial.commit09911bf2008-07-26 23:55:29659
[email protected]55ce8f12012-05-09 04:44:08660 // Allow an additional match for "what you typed".
661 const size_t max_total_matches = kMaxMatches + 1;
initial.commit09911bf2008-07-26 23:55:29662 std::partial_sort(matches_.begin(),
663 matches_.begin() + std::min(max_total_matches, matches_.size()),
664 matches_.end(), &AutocompleteMatch::MoreRelevant);
665 if (matches_.size() > max_total_matches)
[email protected]a28e95662008-11-12 19:19:02666 matches_.erase(matches_.begin() + max_total_matches, matches_.end());
initial.commit09911bf2008-07-26 23:55:29667
[email protected]cc63dea2008-08-21 20:56:31668 UpdateStarredStateOfMatches();
[email protected]4ab4c7c2010-11-24 04:49:34669 UpdateDone();
[email protected]257ab712009-04-14 17:16:24670}
671
672void SearchProvider::AddNavigationResultsToMatches(
673 const NavigationResults& navigation_results,
674 bool is_keyword) {
675 if (!navigation_results.empty()) {
[email protected]6c535842012-05-15 05:20:55676 // TODO(kochi|msw): Add more navigational results if they get more
677 // meaningful relevance values; see http://b/1170574.
678 NavigationResults::const_iterator result(
679 std::max_element(navigation_results.begin(),
680 navigation_results.end(),
681 CompareScoredResults()));
682 matches_.push_back(NavigationToMatch(*result, is_keyword));
[email protected]257ab712009-04-14 17:16:24683 }
684}
685
686void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
687 bool is_keyword,
688 int did_not_accept_suggestion,
689 MatchMap* map) {
[email protected]51124552011-07-16 01:37:10690 if (results.empty())
691 return;
692
[email protected]55ce8f12012-05-09 04:44:08693 bool prevent_inline_autocomplete =
[email protected]51124552011-07-16 01:37:10694 (input_.type() == AutocompleteInput::URL) ||
695 input_.prevent_inline_autocomplete();
[email protected]55ce8f12012-05-09 04:44:08696 const string16& input_text(is_keyword ? keyword_input_text_ : input_.text());
[email protected]51124552011-07-16 01:37:10697 bool input_multiple_words = HasMultipleWords(input_text);
698
[email protected]55ce8f12012-05-09 04:44:08699 SuggestResults scored_results;
700 if (!prevent_inline_autocomplete && input_multiple_words) {
701 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
702 // queries if the input also has multiple words. But if we were already
[email protected]51124552011-07-16 01:37:10703 // autocompleting a multi-word, multi-visit query, and the current input is
704 // still a prefix of it, then changing the autocompletion suddenly feels
705 // wrong. To detect this case, first score as if only one word has been
706 // typed, then check for a best result that is an autocompleted, multi-word
707 // query. If we find one, then just keep that score set.
[email protected]55ce8f12012-05-09 04:44:08708 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
709 false, input_text, is_keyword);
710 if ((scored_results[0].relevance() <
711 AutocompleteResult::kLowestDefaultScore) ||
712 !HasMultipleWords(scored_results[0].suggestion()))
713 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:10714 }
[email protected]55ce8f12012-05-09 04:44:08715 if (scored_results.empty())
716 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
717 input_multiple_words, input_text,
718 is_keyword);
719 for (SuggestResults::const_iterator i(scored_results.begin());
720 i != scored_results.end(); ++i) {
721 AddMatchToMap(i->suggestion(), input_text, i->relevance(),
[email protected]51124552011-07-16 01:37:10722 AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion,
[email protected]55ce8f12012-05-09 04:44:08723 is_keyword, map);
[email protected]51124552011-07-16 01:37:10724 }
725}
726
[email protected]55ce8f12012-05-09 04:44:08727SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:10728 const HistoryResults& results,
729 bool base_prevent_inline_autocomplete,
730 bool input_multiple_words,
731 const string16& input_text,
732 bool is_keyword) {
[email protected]ea3b9a502011-04-04 14:19:37733 AutocompleteClassifier* classifier = profile_->GetAutocompleteClassifier();
[email protected]55ce8f12012-05-09 04:44:08734 SuggestResults scored_results;
[email protected]257ab712009-04-14 17:16:24735 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
736 ++i) {
[email protected]51124552011-07-16 01:37:10737 // Don't autocomplete multi-word queries that have only been seen once
738 // unless the user has typed more than one word.
739 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
740 (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term));
741
[email protected]ea3b9a502011-04-04 14:19:37742 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:10743 // when typed. For example, if the user searched for "google.com" and types
744 // "goog", don't autocomplete to the search term "google.com". Otherwise,
745 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:48746 // NOTE: We don't check this in the following cases:
747 // * When inline autocomplete is disabled, we won't be inline
748 // autocompleting this term, so we don't need to worry about confusion as
749 // much. This also prevents calling Classify() again from inside the
750 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:10751 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:48752 // * When the user has typed the whole term, the "what you typed" history
753 // match will outrank us for URL-like inputs anyway, so we need not do
754 // anything special.
[email protected]51124552011-07-16 01:37:10755 if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) {
[email protected]ea3b9a502011-04-04 14:19:37756 AutocompleteMatch match;
[email protected]72874a8d2011-05-11 03:48:54757 classifier->Classify(i->term, string16(), false, false, &match, NULL);
[email protected]2905f742011-10-13 03:51:58758 prevent_inline_autocomplete =
759 match.transition == content::PAGE_TRANSITION_TYPED;
[email protected]ea3b9a502011-04-04 14:19:37760 }
[email protected]51124552011-07-16 01:37:10761
762 int relevance = CalculateRelevanceForHistory(i->time, is_keyword,
763 prevent_inline_autocomplete);
[email protected]55ce8f12012-05-09 04:44:08764 scored_results.push_back(SuggestResult(i->term, relevance));
[email protected]257ab712009-04-14 17:16:24765 }
[email protected]51124552011-07-16 01:37:10766
767 // History returns results sorted for us. However, we may have docked some
768 // results' scores, so things are no longer in order. Do a stable sort to get
769 // things back in order without otherwise disturbing results with equal
770 // scores, then force the scores to be unique, so that the order in which
771 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:08772 std::stable_sort(scored_results.begin(), scored_results.end(),
773 CompareScoredResults());
[email protected]51124552011-07-16 01:37:10774 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:08775 for (SuggestResults::iterator i(scored_results.begin());
776 i != scored_results.end(); ++i) {
777 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
778 i->set_relevance(last_relevance - 1);
779 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:10780 }
781
[email protected]55ce8f12012-05-09 04:44:08782 return scored_results;
[email protected]257ab712009-04-14 17:16:24783}
784
[email protected]55ce8f12012-05-09 04:44:08785void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
786 bool is_keyword,
787 MatchMap* map) {
788 const string16& text = is_keyword ? keyword_input_text_ : input_.text();
789 for (size_t i = 0; i < results.size(); ++i) {
790 AddMatchToMap(results[i].suggestion(), text, results[i].relevance(),
791 AutocompleteMatch::SEARCH_SUGGEST, i, is_keyword, map);
[email protected]257ab712009-04-14 17:16:24792 }
initial.commit09911bf2008-07-26 23:55:29793}
794
795int SearchProvider::CalculateRelevanceForWhatYouTyped() const {
[email protected]85b8d6f2012-05-08 20:53:47796 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:36797 return 250;
798
initial.commit09911bf2008-07-26 23:55:29799 switch (input_.type()) {
800 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:36801 case AutocompleteInput::QUERY:
802 case AutocompleteInput::FORCED_QUERY:
803 return 1300;
initial.commit09911bf2008-07-26 23:55:29804
805 case AutocompleteInput::REQUESTED_URL:
[email protected]52d08b12009-10-19 18:42:36806 return 1150;
initial.commit09911bf2008-07-26 23:55:29807
808 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:36809 return 850;
initial.commit09911bf2008-07-26 23:55:29810
811 default:
812 NOTREACHED();
813 return 0;
814 }
815}
816
[email protected]51124552011-07-16 01:37:10817int SearchProvider::CalculateRelevanceForHistory(
818 const Time& time,
819 bool is_keyword,
820 bool prevent_inline_autocomplete) const {
[email protected]aa613d62010-11-09 20:40:18821 // The relevance of past searches falls off over time. There are two distinct
822 // equations used. If the first equation is used (searches to the primary
[email protected]51124552011-07-16 01:37:10823 // provider that we want to inline autocomplete), the score starts at 1399 and
824 // falls to 1300. If the second equation is used the relevance of a search 15
825 // minutes ago is discounted 50 points, while the relevance of a search two
826 // weeks ago is discounted 450 points.
[email protected]aa613d62010-11-09 20:40:18827 double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.);
[email protected]51124552011-07-16 01:37:10828 bool is_primary_provider = providers_.is_primary_provider(is_keyword);
829 if (is_primary_provider && !prevent_inline_autocomplete) {
[email protected]aa613d62010-11-09 20:40:18830 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:10831 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:18832 if (elapsed_time < autocomplete_time) {
[email protected]e17511f2011-07-13 14:09:18833 return (is_keyword ? 1599 : 1399) - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:18834 std::pow(elapsed_time / autocomplete_time, 2.5));
835 }
836 elapsed_time -= autocomplete_time;
837 }
838
[email protected]c3a4bd992010-08-18 20:25:01839 const int score_discount =
840 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:29841
[email protected]6c85aa02009-02-27 12:08:09842 // Don't let scores go below 0. Negative relevance scores are meaningful in
843 // a different way.
initial.commit09911bf2008-07-26 23:55:29844 int base_score;
[email protected]51124552011-07-16 01:37:10845 if (is_primary_provider)
[email protected]52d08b12009-10-19 18:42:36846 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:10847 else
848 base_score = 200;
initial.commit09911bf2008-07-26 23:55:29849 return std::max(0, base_score - score_discount);
850}
851
[email protected]55ce8f12012-05-09 04:44:08852int SearchProvider::CalculateRelevanceForSuggestion(bool for_keyword) const {
853 return !providers_.is_primary_provider(for_keyword) ? 100 :
854 ((input_.type() == AutocompleteInput::URL) ? 300 : 600);
initial.commit09911bf2008-07-26 23:55:29855}
856
[email protected]55ce8f12012-05-09 04:44:08857int SearchProvider::CalculateRelevanceForNavigation(bool for_keyword) const {
858 return providers_.is_primary_provider(for_keyword) ? 800 : 150;
initial.commit09911bf2008-07-26 23:55:29859}
860
[email protected]a2fedb1e2011-01-25 15:23:36861void SearchProvider::AddMatchToMap(const string16& query_string,
862 const string16& input_text,
initial.commit09911bf2008-07-26 23:55:29863 int relevance,
[email protected]4c1fb7ec2008-11-13 00:19:00864 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:29865 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:24866 bool is_keyword,
initial.commit09911bf2008-07-26 23:55:29867 MatchMap* map) {
[email protected]92513682011-09-01 06:16:52868 AutocompleteMatch match(this, relevance, false, type);
initial.commit09911bf2008-07-26 23:55:29869 std::vector<size_t> content_param_offsets;
[email protected]85b8d6f2012-05-08 20:53:47870 // Bail out now if we don't actually have a valid provider.
871 match.keyword = is_keyword ?
[email protected]3954c3a2012-04-10 20:17:55872 providers_.keyword_provider() : providers_.default_provider();
[email protected]85b8d6f2012-05-08 20:53:47873 const TemplateURL* provider_url = match.GetTemplateURL(profile_);
874 if (provider_url == NULL)
875 return;
876
[email protected]70833262011-01-05 23:40:44877 match.contents.assign(query_string);
[email protected]fb5153c52009-07-31 19:40:33878 // We do intra-string highlighting for suggestions - the suggested segment
879 // will be highlighted, e.g. for input_text = "you" the suggestion may be
880 // "youtube", so we'll bold the "tube" section: you*tube*.
881 if (input_text != query_string) {
[email protected]fb5153c52009-07-31 19:40:33882 size_t input_position = match.contents.find(input_text);
[email protected]a2fedb1e2011-01-25 15:23:36883 if (input_position == string16::npos) {
[email protected]fb5153c52009-07-31 19:40:33884 // The input text is not a substring of the query string, e.g. input
885 // text is "slasdot" and the query string is "slashdot", so we bold the
886 // whole thing.
887 match.contents_class.push_back(
888 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]ec2379162009-06-09 23:58:17889 } else {
[email protected]fb5153c52009-07-31 19:40:33890 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
891 // "bold" this. Consider modifying the terminology.
892 // We don't iterate over the string here annotating all matches because
893 // it looks odd to have every occurrence of a substring that may be as
894 // short as a single character highlighted in a query suggestion result,
895 // e.g. for input text "s" and query string "southwest airlines", it
896 // looks odd if both the first and last s are highlighted.
897 if (input_position != 0) {
898 match.contents_class.push_back(
899 ACMatchClassification(0, ACMatchClassification::NONE));
900 }
901 match.contents_class.push_back(
902 ACMatchClassification(input_position, ACMatchClassification::DIM));
903 size_t next_fragment_position = input_position + input_text.length();
904 if (next_fragment_position < query_string.length()) {
905 match.contents_class.push_back(
906 ACMatchClassification(next_fragment_position,
907 ACMatchClassification::NONE));
908 }
[email protected]ec2379162009-06-09 23:58:17909 }
initial.commit09911bf2008-07-26 23:55:29910 } else {
[email protected]fb5153c52009-07-31 19:40:33911 // Otherwise, we're dealing with the "default search" result which has no
[email protected]70833262011-01-05 23:40:44912 // completion.
[email protected]fb5153c52009-07-31 19:40:33913 match.contents_class.push_back(
914 ACMatchClassification(0, ACMatchClassification::NONE));
initial.commit09911bf2008-07-26 23:55:29915 }
916
917 // When the user forced a query, we need to make sure all the fill_into_edit
918 // values preserve that property. Otherwise, if the user starts editing a
919 // suggestion, non-Search results will suddenly appear.
920 size_t search_start = 0;
921 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
[email protected]a2fedb1e2011-01-25 15:23:36922 match.fill_into_edit.assign(ASCIIToUTF16("?"));
initial.commit09911bf2008-07-26 23:55:29923 ++search_start;
924 }
[email protected]c0048b42009-05-04 21:47:17925 if (is_keyword) {
[email protected]033f3422012-03-13 21:24:18926 match.fill_into_edit.append(match.keyword + char16(' '));
927 search_start += match.keyword.length() + 1;
[email protected]c0048b42009-05-04 21:47:17928 }
initial.commit09911bf2008-07-26 23:55:29929 match.fill_into_edit.append(query_string);
[email protected]2c33dd22010-02-11 21:46:35930 // Not all suggestions start with the original input.
[email protected]55ce8f12012-05-09 04:44:08931 if (!input_.prevent_inline_autocomplete() &&
[email protected]257ab712009-04-14 17:16:24932 !match.fill_into_edit.compare(search_start, input_text.length(),
933 input_text))
934 match.inline_autocomplete_offset = search_start + input_text.length();
initial.commit09911bf2008-07-26 23:55:29935
[email protected]85b8d6f2012-05-08 20:53:47936 const TemplateURLRef& search_url = provider_url->url_ref();
[email protected]360ba052012-04-04 17:26:13937 DCHECK(search_url.SupportsReplacement());
[email protected]16fca9b82012-04-23 18:40:26938 match.destination_url = GURL(search_url.ReplaceSearchTerms(query_string,
939 accepted_suggestion, input_text));
initial.commit09911bf2008-07-26 23:55:29940
941 // Search results don't look like URLs.
[email protected]2905f742011-10-13 03:51:58942 match.transition = is_keyword ?
943 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED;
initial.commit09911bf2008-07-26 23:55:29944
945 // Try to add |match| to |map|. If a match for |query_string| is already in
946 // |map|, replace it if |match| is more relevant.
947 // NOTE: Keep this ToLower() call in sync with url_database.cc.
948 const std::pair<MatchMap::iterator, bool> i = map->insert(
[email protected]a2fedb1e2011-01-25 15:23:36949 std::pair<string16, AutocompleteMatch>(
[email protected]503d03872011-05-06 08:36:26950 base::i18n::ToLower(query_string), match));
initial.commit09911bf2008-07-26 23:55:29951 // NOTE: We purposefully do a direct relevance comparison here instead of
952 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
953 // first" rather than "items alphabetically first" when the scores are equal.
954 // The only case this matters is when a user has results with the same score
955 // that differ only by capitalization; because the history system returns
956 // results sorted by recency, this means we'll pick the most recent such
957 // result even if the precision of our relevance score is too low to
958 // distinguish the two.
959 if (!i.second && (match.relevance > i.first->second.relevance))
960 i.first->second = match;
961}
962
963AutocompleteMatch SearchProvider::NavigationToMatch(
964 const NavigationResult& navigation,
[email protected]257ab712009-04-14 17:16:24965 bool is_keyword) {
[email protected]55ce8f12012-05-09 04:44:08966 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
967 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]4c1fb7ec2008-11-13 00:19:00968 AutocompleteMatch::NAVSUGGEST);
[email protected]55ce8f12012-05-09 04:44:08969 match.destination_url = navigation.url();
[email protected]76e7da22010-06-18 22:44:49970 match.contents =
[email protected]55ce8f12012-05-09 04:44:08971 StringForURLDisplay(navigation.url(), true, !HasHTTPScheme(input_text));
[email protected]257ab712009-04-14 17:16:24972 AutocompleteMatch::ClassifyMatchInString(input_text, match.contents,
initial.commit09911bf2008-07-26 23:55:29973 ACMatchClassification::URL,
974 &match.contents_class);
975
[email protected]55ce8f12012-05-09 04:44:08976 match.description = navigation.description();
977 AutocompleteMatch::ClassifyMatchInString(input_text, match.description,
initial.commit09911bf2008-07-26 23:55:29978 ACMatchClassification::NONE,
979 &match.description_class);
980
initial.commit09911bf2008-07-26 23:55:29981 // When the user forced a query, we need to make sure all the fill_into_edit
982 // values preserve that property. Otherwise, if the user starts editing a
983 // suggestion, non-Search results will suddenly appear.
984 if (input_.type() == AutocompleteInput::FORCED_QUERY)
[email protected]a2fedb1e2011-01-25 15:23:36985 match.fill_into_edit.assign(ASCIIToUTF16("?"));
[email protected]79845ef2010-06-02 02:37:40986 match.fill_into_edit.append(
[email protected]55ce8f12012-05-09 04:44:08987 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
[email protected]79845ef2010-06-02 02:37:40988 match.contents));
[email protected]55ce8f12012-05-09 04:44:08989 // TODO(pkasting|msw): Inline-autocomplete nav results; see http://b/1112879.
initial.commit09911bf2008-07-26 23:55:29990
991 return match;
992}
[email protected]4ab4c7c2010-11-24 04:49:34993
994void SearchProvider::UpdateDone() {
995 // We're done when there are no more suggest queries pending (this is set to 1
996 // when the timer is started) and we're not waiting on instant.
997 done_ = ((suggest_results_pending_ == 0) &&
998 (instant_finalized_ || !InstantController::IsEnabled(profile_)));
999}