blob: 5c314dfc2ed0575c9ec9f5bab8bd4983d9b68631 [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]9d2db762012-06-19 00:01:1028#include "chrome/browser/history/history_service_factory.h"
[email protected]10c2d692012-05-11 05:32:2329#include "chrome/browser/history/in_memory_database.h"
[email protected]4ab4c7c2010-11-24 04:49:3430#include "chrome/browser/instant/instant_controller.h"
[email protected]f870a322009-01-16 21:47:2731#include "chrome/browser/net/url_fixer_upper.h"
[email protected]37858e52010-08-26 00:22:0232#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3333#include "chrome/browser/profiles/profile.h"
[email protected]a0ad93ea2012-05-07 22:11:5334#include "chrome/browser/search_engines/search_engine_type.h"
[email protected]8e5c89a2011-06-07 18:13:3335#include "chrome/browser/search_engines/template_url_service.h"
36#include "chrome/browser/search_engines/template_url_service_factory.h"
initial.commit09911bf2008-07-26 23:55:2937#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0238#include "chrome/common/url_constants.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"
[email protected]3dc1bc42012-06-19 08:20:5345#include "net/url_request/url_fetcher.h"
[email protected]319d9e6f2009-02-18 19:47:2146#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1747#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2948
[email protected]e1acf6f2008-10-27 20:43:3349using base::Time;
50using base::TimeDelta;
51
[email protected]51124552011-07-16 01:37:1052namespace {
53
54bool HasMultipleWords(const string16& text) {
55 base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD);
56 bool found_word = false;
57 if (i.Init()) {
58 while (i.Advance()) {
59 if (i.IsWord()) {
60 if (found_word)
61 return true;
62 found_word = true;
63 }
64 }
65 }
66 return false;
67}
68
[email protected]d1f0a7f2012-06-05 10:26:4269} // namespace
[email protected]51124552011-07-16 01:37:1070
[email protected]033f3422012-03-13 21:24:1871
[email protected]3954c3a2012-04-10 20:17:5572// SearchProvider::Providers --------------------------------------------------
[email protected]b547666d2009-04-23 16:37:5873
[email protected]85b8d6f2012-05-08 20:53:4774SearchProvider::Providers::Providers(TemplateURLService* template_url_service)
75 : template_url_service_(template_url_service) {
76}
77
78const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const {
79 return default_provider_.empty() ? NULL :
80 template_url_service_->GetTemplateURLForKeyword(default_provider_);
81}
82
83const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const {
84 return keyword_provider_.empty() ? NULL :
85 template_url_service_->GetTemplateURLForKeyword(keyword_provider_);
[email protected]257ab712009-04-14 17:16:2486}
87
[email protected]3954c3a2012-04-10 20:17:5588
89// SearchProvider -------------------------------------------------------------
90
91// static
92const int SearchProvider::kDefaultProviderURLFetcherID = 1;
93// static
94const int SearchProvider::kKeywordProviderURLFetcherID = 2;
95// static
96bool SearchProvider::query_suggest_immediately_ = false;
97
[email protected]601858c02010-09-01 17:08:2098SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile)
99 : AutocompleteProvider(listener, profile, "Search"),
[email protected]85b8d6f2012-05-08 20:53:47100 providers_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]601858c02010-09-01 17:08:20101 suggest_results_pending_(0),
[email protected]d1f0a7f2012-06-05 10:26:42102 has_suggested_relevance_(false),
103 verbatim_relevance_(-1),
[email protected]8e5cc282010-12-05 18:11:39104 have_suggest_results_(false),
[email protected]4ab4c7c2010-11-24 04:49:34105 instant_finalized_(false) {
[email protected]f5b95ba92012-03-27 14:05:19106 // We use GetSuggestNumberOfGroups() as the group ID to mean "not in field
107 // trial." Field trial groups run from 0 to GetSuggestNumberOfGroups() - 1
108 // (inclusive).
109 int suggest_field_trial_group_number =
110 AutocompleteFieldTrial::GetSuggestNumberOfGroups();
111 if (AutocompleteFieldTrial::InSuggestFieldTrial()) {
112 suggest_field_trial_group_number =
113 AutocompleteFieldTrial::GetSuggestGroupNameAsNumber();
114 }
115 // Add a beacon to the logs that'll allow us to identify later what
116 // suggest field trial group a user is in. Do this by incrementing a
117 // bucket in a histogram, where the bucket represents the user's
118 // suggest group id.
119 UMA_HISTOGRAM_ENUMERATION(
120 "Omnibox.SuggestFieldTrialBeacon",
121 suggest_field_trial_group_number,
122 AutocompleteFieldTrial::GetSuggestNumberOfGroups() + 1);
[email protected]4ab4c7c2010-11-24 04:49:34123}
124
[email protected]a2fedb1e2011-01-25 15:23:36125void SearchProvider::FinalizeInstantQuery(const string16& input_text,
126 const string16& suggest_text) {
[email protected]4ab4c7c2010-11-24 04:49:34127 if (done_ || instant_finalized_)
128 return;
129
130 instant_finalized_ = true;
131 UpdateDone();
132
[email protected]e918c112010-12-08 23:03:49133 if (input_text.empty()) {
[email protected]4ab4c7c2010-11-24 04:49:34134 // We only need to update the listener if we're actually done.
135 if (done_)
136 listener_->OnProviderUpdate(false);
137 return;
138 }
139
[email protected]9e789742011-01-10 23:27:32140 default_provider_suggest_text_ = suggest_text;
141
[email protected]a2fedb1e2011-01-25 15:23:36142 string16 adjusted_input_text(input_text);
[email protected]e918c112010-12-08 23:03:49143 AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(),
144 &adjusted_input_text);
145
[email protected]a2fedb1e2011-01-25 15:23:36146 const string16 text = adjusted_input_text + suggest_text;
[email protected]9acdcdc02012-05-21 20:58:36147 bool results_updated = false;
[email protected]4ab4c7c2010-11-24 04:49:34148 // Remove any matches that are identical to |text|. We don't use the
149 // destination_url for comparison as it varies depending upon the index passed
150 // to TemplateURL::ReplaceSearchTerms.
151 for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) {
152 if (((i->type == AutocompleteMatch::SEARCH_HISTORY) ||
153 (i->type == AutocompleteMatch::SEARCH_SUGGEST)) &&
154 (i->fill_into_edit == text)) {
[email protected]e030de62010-11-24 05:41:19155 i = matches_.erase(i);
[email protected]9acdcdc02012-05-21 20:58:36156 results_updated = true;
[email protected]4ab4c7c2010-11-24 04:49:34157 } else {
158 ++i;
159 }
160 }
161
[email protected]55ce8f12012-05-09 04:44:08162 // Add the new instant suggest result. We give it a rank higher than
[email protected]4ab4c7c2010-11-24 04:49:34163 // SEARCH_WHAT_YOU_TYPED so that it gets autocompleted.
164 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
165 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
166 TemplateURLRef::NO_SUGGESTION_CHOSEN;
167 MatchMap match_map;
[email protected]382a0642012-06-06 06:13:52168 AddMatchToMap(text, adjusted_input_text, GetVerbatimRelevance() + 1,
[email protected]4ab4c7c2010-11-24 04:49:34169 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08170 did_not_accept_default_suggestion, false, &match_map);
[email protected]9acdcdc02012-05-21 20:58:36171 if (!match_map.empty()) {
172 matches_.push_back(match_map.begin()->second);
173 results_updated = true;
174 }
[email protected]4ab4c7c2010-11-24 04:49:34175
[email protected]9acdcdc02012-05-21 20:58:36176 if (results_updated || done_)
177 listener_->OnProviderUpdate(results_updated);
[email protected]601858c02010-09-01 17:08:20178}
179
initial.commit09911bf2008-07-26 23:55:29180void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27181 bool minimal_changes) {
initial.commit09911bf2008-07-26 23:55:29182 matches_.clear();
183
[email protected]ea3b9a502011-04-04 14:19:37184 instant_finalized_ =
185 (input.matches_requested() != AutocompleteInput::ALL_MATCHES);
[email protected]4ab4c7c2010-11-24 04:49:34186
[email protected]6c85aa02009-02-27 12:08:09187 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29188 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
189 Stop();
190 return;
191 }
192
[email protected]257ab712009-04-14 17:16:24193 keyword_input_text_.clear();
194 const TemplateURL* keyword_provider =
195 KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input,
196 &keyword_input_text_);
[email protected]8d457132010-11-04 18:13:40197 if (keyword_input_text_.empty())
[email protected]257ab712009-04-14 17:16:24198 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24199
[email protected]85b8d6f2012-05-08 20:53:47200 TemplateURLService* model = providers_.template_url_service();
201 DCHECK(model);
202 model->Load();
203 const TemplateURL* default_provider = model->GetDefaultSearchProvider();
[email protected]9b74ab52012-03-30 16:08:07204 if (default_provider && !default_provider->SupportsReplacement())
[email protected]257ab712009-04-14 17:16:24205 default_provider = NULL;
206
207 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18208 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24209
210 if (!default_provider && !keyword_provider) {
211 // No valid providers.
initial.commit09911bf2008-07-26 23:55:29212 Stop();
213 return;
214 }
215
216 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24217 // or the providers, abort the query.
[email protected]85b8d6f2012-05-08 20:53:47218 string16 default_provider_keyword(default_provider ?
219 default_provider->keyword() : string16());
220 string16 keyword_provider_keyword(keyword_provider ?
221 keyword_provider->keyword() : string16());
[email protected]9e789742011-01-10 23:27:32222 if (!minimal_changes ||
[email protected]85b8d6f2012-05-08 20:53:47223 !providers_.equal(default_provider_keyword, keyword_provider_keyword)) {
[email protected]9e789742011-01-10 23:27:32224 if (done_)
225 default_provider_suggest_text_.clear();
226 else
227 Stop();
[email protected]257ab712009-04-14 17:16:24228 }
initial.commit09911bf2008-07-26 23:55:29229
[email protected]85b8d6f2012-05-08 20:53:47230 providers_.set(default_provider_keyword, keyword_provider_keyword);
initial.commit09911bf2008-07-26 23:55:29231
232 if (input.text().empty()) {
233 // User typed "?" alone. Give them a placeholder result indicating what
234 // this syntax does.
[email protected]257ab712009-04-14 17:16:24235 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00236 AutocompleteMatch match;
237 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36238 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24239 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35240 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]85b8d6f2012-05-08 20:53:47241 match.keyword = providers_.default_provider();
[email protected]257ab712009-04-14 17:16:24242 matches_.push_back(match);
243 }
initial.commit09911bf2008-07-26 23:55:29244 Stop();
245 return;
246 }
247
248 input_ = input;
249
[email protected]8d457132010-11-04 18:13:40250 DoHistoryQuery(minimal_changes);
[email protected]8deeb952008-10-09 18:21:27251 StartOrStopSuggestQuery(minimal_changes);
initial.commit09911bf2008-07-26 23:55:29252 ConvertResultsToAutocompleteMatches();
253}
254
[email protected]55ce8f12012-05-09 04:44:08255SearchProvider::Result::Result(int relevance) : relevance_(relevance) {}
256SearchProvider::Result::~Result() {}
257
258SearchProvider::SuggestResult::SuggestResult(const string16& suggestion,
259 int relevance)
260 : Result(relevance),
261 suggestion_(suggestion) {
262}
263
264SearchProvider::SuggestResult::~SuggestResult() {}
265
266SearchProvider::NavigationResult::NavigationResult(const GURL& url,
267 const string16& description,
268 int relevance)
269 : Result(relevance),
270 url_(url),
271 description_(description) {
272 DCHECK(url_.is_valid());
273}
274
275SearchProvider::NavigationResult::~NavigationResult() {}
276
277class SearchProvider::CompareScoredResults {
[email protected]51124552011-07-16 01:37:10278 public:
[email protected]55ce8f12012-05-09 04:44:08279 bool operator()(const Result& a, const Result& b) {
[email protected]51124552011-07-16 01:37:10280 // Sort in descending relevance order.
[email protected]55ce8f12012-05-09 04:44:08281 return a.relevance() > b.relevance();
[email protected]51124552011-07-16 01:37:10282 }
283};
284
initial.commit09911bf2008-07-26 23:55:29285void SearchProvider::Run() {
286 // Start a new request with the current input.
287 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24288 suggest_results_pending_ = 0;
[email protected]a0ad93ea2012-05-07 22:11:53289 time_suggest_request_sent_ = base::TimeTicks::Now();
[email protected]85b8d6f2012-05-08 20:53:47290 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
291 if (default_url && !default_url->suggestions_url().empty()) {
[email protected]257ab712009-04-14 17:16:24292 suggest_results_pending_++;
[email protected]033f3422012-03-13 21:24:18293 default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID,
[email protected]85b8d6f2012-05-08 20:53:47294 default_url->suggestions_url_ref(), input_.text()));
[email protected]3954c3a2012-04-10 20:17:55295 }
[email protected]85b8d6f2012-05-08 20:53:47296 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
297 if (keyword_url && !keyword_url->suggestions_url().empty()) {
[email protected]3954c3a2012-04-10 20:17:55298 suggest_results_pending_++;
299 keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID,
[email protected]85b8d6f2012-05-08 20:53:47300 keyword_url->suggestions_url_ref(), keyword_input_text_));
[email protected]257ab712009-04-14 17:16:24301 }
[email protected]85b8d6f2012-05-08 20:53:47302
303 // Both the above can fail if the providers have been modified or deleted
304 // since the query began.
305 if (suggest_results_pending_ == 0) {
306 UpdateDone();
307 // We only need to update the listener if we're actually done.
308 if (done_)
309 listener_->OnProviderUpdate(false);
310 }
initial.commit09911bf2008-07-26 23:55:29311}
312
313void SearchProvider::Stop() {
initial.commit09911bf2008-07-26 23:55:29314 StopSuggest();
315 done_ = true;
[email protected]9e789742011-01-10 23:27:32316 default_provider_suggest_text_.clear();
initial.commit09911bf2008-07-26 23:55:29317}
318
[email protected]0e9e8782012-05-15 23:01:51319void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
320 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo());
321 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back();
322 new_entry.set_provider(AsOmniboxEventProviderType());
323 new_entry.set_provider_done(done_);
324}
325
[email protected]10c2d692012-05-11 05:32:23326void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) {
initial.commit09911bf2008-07-26 23:55:29327 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24328 suggest_results_pending_--;
[email protected]1cb2dac2010-03-08 21:49:15329 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06330 const net::HttpResponseHeaders* const response_headers =
[email protected]7cc6e5632011-10-25 17:56:12331 source->GetResponseHeaders();
[email protected]c530c852011-10-24 18:18:34332 std::string json_data;
333 source->GetResponseAsString(&json_data);
[email protected]6c85aa02009-02-27 12:08:09334 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
335 // files in non-UTF-8 encodings. The actual encoding is usually specified in
336 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06337 if (response_headers) {
338 std::string charset;
339 if (response_headers->GetCharset(&charset)) {
[email protected]a2fedb1e2011-01-25 15:23:36340 string16 data_16;
[email protected]ec9207d32008-09-26 00:51:06341 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]c530c852011-10-24 18:18:34342 if (base::CodepageToUTF16(json_data, charset.c_str(),
[email protected]a2fedb1e2011-01-25 15:23:36343 base::OnStringConversionError::FAIL,
344 &data_16))
345 json_data = UTF16ToUTF8(data_16);
[email protected]ec9207d32008-09-26 00:51:06346 }
347 }
348
[email protected]d7ad4772012-06-01 03:12:54349 const bool is_keyword = (source == keyword_fetcher_.get());
[email protected]013e9a02012-05-18 20:27:10350 const bool request_succeeded =
351 source->GetStatus().is_success() && source->GetResponseCode() == 200;
[email protected]a0ad93ea2012-05-07 22:11:53352
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
[email protected]d7ad4772012-06-01 03:12:54370 bool results_updated = false;
371 if (request_succeeded) {
372 JSONStringValueSerializer deserializer(json_data);
373 deserializer.set_allow_trailing_comma(true);
374 scoped_ptr<Value> data(deserializer.Deserialize(NULL, NULL));
375 results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword);
376 }
377
initial.commit09911bf2008-07-26 23:55:29378 ConvertResultsToAutocompleteMatches();
[email protected]d7ad4772012-06-01 03:12:54379 if (done_ || results_updated)
380 listener_->OnProviderUpdate(results_updated);
initial.commit09911bf2008-07-26 23:55:29381}
382
[email protected]601858c02010-09-01 17:08:20383SearchProvider::~SearchProvider() {
384}
385
[email protected]8d457132010-11-04 18:13:40386void SearchProvider::DoHistoryQuery(bool minimal_changes) {
387 // The history query results are synchronous, so if minimal_changes is true,
388 // we still have the last results and don't need to do anything.
389 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29390 return;
391
[email protected]8d457132010-11-04 18:13:40392 keyword_history_results_.clear();
393 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29394
[email protected]8d457132010-11-04 18:13:40395 HistoryService* const history_service =
[email protected]9d2db762012-06-19 00:01:10396 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
[email protected]8d457132010-11-04 18:13:40397 history::URLDatabase* url_db = history_service ?
398 history_service->InMemoryDatabase() : NULL;
399 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29400 return;
401
[email protected]51124552011-07-16 01:37:10402 // Request history for both the keyword and default provider. We grab many
403 // more matches than we'll ultimately clamp to so that if there are several
404 // recent multi-word matches who scores are lowered (see
405 // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring
406 // matches. Note that this doesn't fix the problem entirely, but merely
407 // limits it to cases with a very large number of such multi-word matches; for
408 // now, this seems OK compared with the complexity of a real fix, which would
409 // require multiple searches and tracking of "single- vs. multi-word" in the
410 // database.
411 int num_matches = kMaxMatches * 5;
[email protected]85b8d6f2012-05-08 20:53:47412 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
413 if (default_url) {
414 url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(),
415 num_matches, &default_history_results_);
[email protected]257ab712009-04-14 17:16:24416 }
[email protected]85b8d6f2012-05-08 20:53:47417 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
418 if (keyword_url) {
419 url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(),
[email protected]3954c3a2012-04-10 20:17:55420 keyword_input_text_, num_matches, &keyword_history_results_);
421 }
initial.commit09911bf2008-07-26 23:55:29422}
423
[email protected]8deeb952008-10-09 18:21:27424void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]6c85aa02009-02-27 12:08:09425 // Don't send any queries to the server until some time has elapsed after
426 // the last keypress, to avoid flooding the server with requests we are
427 // likely to end up throwing away anyway.
[email protected]02c3f6832011-11-16 18:37:40428 const int kQueryDelayMs = 200;
[email protected]6c85aa02009-02-27 12:08:09429
[email protected]83c726482008-09-10 06:36:34430 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29431 StopSuggest();
[email protected]55ce8f12012-05-09 04:44:08432 ClearResults();
initial.commit09911bf2008-07-26 23:55:29433 return;
434 }
435
436 // For the minimal_changes case, if we finished the previous query and still
437 // have its results, or are allowed to keep running it, just do that, rather
438 // than starting a new query.
439 if (minimal_changes &&
[email protected]ea3b9a502011-04-04 14:19:37440 (have_suggest_results_ ||
441 (!done_ &&
442 input_.matches_requested() == AutocompleteInput::ALL_MATCHES)))
initial.commit09911bf2008-07-26 23:55:29443 return;
444
445 // We can't keep running any previous query, so halt it.
446 StopSuggest();
[email protected]d1f0a7f2012-06-05 10:26:42447
448 // Remove existing results that cannot inline autocomplete the new input.
449 RemoveStaleResults();
initial.commit09911bf2008-07-26 23:55:29450
451 // We can't start a new query if we're only allowed synchronous results.
[email protected]ea3b9a502011-04-04 14:19:37452 if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES)
initial.commit09911bf2008-07-26 23:55:29453 return;
454
[email protected]257ab712009-04-14 17:16:24455 // We'll have at least one pending fetch. Set it to 1 now, but the value is
456 // correctly set in Run. As Run isn't invoked immediately we need to set this
457 // now, else we won't think we're waiting on results from the server when we
458 // really are.
459 suggest_results_pending_ = 1;
460
initial.commit09911bf2008-07-26 23:55:29461 // Kick off a timer that will start the URL fetch if it completes before
462 // the user types another character.
[email protected]b547666d2009-04-23 16:37:58463 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs;
[email protected]d323a172011-09-02 18:23:02464 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay), this,
465 &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29466}
467
[email protected]83c726482008-09-10 06:36:34468bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]3954c3a2012-04-10 20:17:55469 // Don't run Suggest in incognito mode, if the engine doesn't support it, or
470 // if the user has disabled it.
[email protected]85b8d6f2012-05-08 20:53:47471 const TemplateURL* default_url = providers_.GetDefaultProviderURL();
472 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
[email protected]83c726482008-09-10 06:36:34473 if (profile_->IsOffTheRecord() ||
[email protected]85b8d6f2012-05-08 20:53:47474 ((!default_url || default_url->suggestions_url().empty()) &&
475 (!keyword_url || keyword_url->suggestions_url().empty())) ||
[email protected]83c726482008-09-10 06:36:34476 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
477 return false;
478
[email protected]cac59d32010-08-09 23:23:14479 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34480 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34481
[email protected]cac59d32010-08-09 23:23:14482 // FORCED_QUERY means the user is explicitly asking us to search for this, so
483 // we assume it isn't a URL and/or there isn't private data.
484 if (input_.type() == AutocompleteInput::FORCED_QUERY)
485 return true;
[email protected]83c726482008-09-10 06:36:34486
[email protected]cac59d32010-08-09 23:23:14487 // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a
488 // scheme that isn't http/https/ftp, we shouldn't send it. Sending things
489 // like file: and data: is both a waste of time and a disclosure of
490 // potentially private, local data. Other "schemes" may actually be
491 // usernames, and we don't want to send passwords. If the scheme is OK, we
492 // still need to check other cases below. If this is QUERY, then the presence
493 // of these schemes means the user explicitly typed one, and thus this is
494 // probably a URL that's being entered and happens to currently be invalid --
495 // in which case we again want to run our checks below. Other QUERY cases are
496 // less likely to be URLs and thus we assume we're OK.
[email protected]a2fedb1e2011-01-25 15:23:36497 if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) &&
498 !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
499 !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme))
[email protected]cac59d32010-08-09 23:23:14500 return (input_.type() == AutocompleteInput::QUERY);
501
502 // Don't send URLs with usernames, queries or refs. Some of these are
503 // private, and the Suggest server is unlikely to have any useful results
504 // for any of them. Also don't send URLs with ports, as we may initially
505 // think that a username + password is a host + port (and we don't want to
506 // send usernames/passwords), and even if the port really is a port, the
507 // server is once again unlikely to have and useful results.
508 const url_parse::Parsed& parts = input_.parts();
509 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
510 parts.query.is_nonempty() || parts.ref.is_nonempty())
511 return false;
512
513 // Don't send anything for https except the hostname. Hostnames are OK
514 // because they are visible when the TCP connection is established, but the
515 // specific path may reveal private information.
[email protected]a2fedb1e2011-01-25 15:23:36516 if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
517 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14518 return false;
[email protected]83c726482008-09-10 06:36:34519
520 return true;
521}
522
initial.commit09911bf2008-07-26 23:55:29523void SearchProvider::StopSuggest() {
[email protected]257ab712009-04-14 17:16:24524 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14525 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24526 // Stop any in-progress URL fetches.
527 keyword_fetcher_.reset();
528 default_fetcher_.reset();
[email protected]55ce8f12012-05-09 04:44:08529}
530
531void SearchProvider::ClearResults() {
[email protected]257ab712009-04-14 17:16:24532 keyword_suggest_results_.clear();
533 default_suggest_results_.clear();
534 keyword_navigation_results_.clear();
535 default_navigation_results_.clear();
[email protected]d1f0a7f2012-06-05 10:26:42536 has_suggested_relevance_ = false;
537 verbatim_relevance_ = -1;
initial.commit09911bf2008-07-26 23:55:29538 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29539}
540
[email protected]d1f0a7f2012-06-05 10:26:42541void SearchProvider::RemoveStaleResults() {
542 RemoveStaleSuggestResults(&keyword_suggest_results_, true);
543 RemoveStaleSuggestResults(&default_suggest_results_, false);
544 RemoveStaleNavigationResults(&keyword_navigation_results_, true);
545 RemoveStaleNavigationResults(&default_navigation_results_, false);
546}
547
548void SearchProvider::RemoveStaleSuggestResults(SuggestResults* list,
549 bool is_keyword) {
550 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
551 for (SuggestResults::iterator i = list->begin(); i < list->end();)
552 i = StartsWith(i->suggestion(), input, false) ? (i + 1) : list->erase(i);
553}
554
555void SearchProvider::RemoveStaleNavigationResults(NavigationResults* list,
556 bool is_keyword) {
557 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
558 for (NavigationResults::iterator i = list->begin(); i < list->end();) {
559 const string16 fill(AutocompleteInput::FormattedStringWithEquivalentMeaning(
560 i->url(), StringForURLDisplay(i->url(), true, false)));
561 i = URLPrefix::BestURLPrefix(fill, input) ? (i + 1) : list->erase(i);
562 }
563}
564
565void SearchProvider::ApplyCalculatedRelevance() {
566 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true);
567 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false);
568 ApplyCalculatedNavigationRelevance(&keyword_navigation_results_, true);
569 ApplyCalculatedNavigationRelevance(&default_navigation_results_, false);
570 has_suggested_relevance_ = false;
571 verbatim_relevance_ = -1;
572}
573
574void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list,
575 bool is_keyword) {
576 for (size_t i = 0; i < list->size(); ++i) {
577 (*list)[i].set_relevance(CalculateRelevanceForSuggestion(is_keyword) +
578 (list->size() - i - 1));
579 }
580}
581
582void SearchProvider::ApplyCalculatedNavigationRelevance(NavigationResults* list,
583 bool is_keyword) {
584 for (size_t i = 0; i < list->size(); ++i) {
585 (*list)[i].set_relevance(CalculateRelevanceForNavigation(is_keyword) +
586 (list->size() - i - 1));
587 }
588}
589
[email protected]15fb2aa2012-05-22 22:52:59590net::URLFetcher* SearchProvider::CreateSuggestFetcher(
[email protected]7cc6e5632011-10-25 17:56:12591 int id,
[email protected]3954c3a2012-04-10 20:17:55592 const TemplateURLRef& suggestions_url,
[email protected]7cc6e5632011-10-25 17:56:12593 const string16& text) {
[email protected]360ba052012-04-04 17:26:13594 DCHECK(suggestions_url.SupportsReplacement());
[email protected]3dc1bc42012-06-19 08:20:53595 net::URLFetcher* fetcher = net::URLFetcher::Create(id,
[email protected]bca359b2012-06-24 07:53:04596 GURL(suggestions_url.ReplaceSearchTerms(
597 TemplateURLRef::SearchTermsArgs(text))),
[email protected]d3ec669b2012-05-23 07:12:14598 net::URLFetcher::GET, this);
[email protected]7cc6e5632011-10-25 17:56:12599 fetcher->SetRequestContext(profile_->GetRequestContext());
[email protected]d3cf8682f02012-02-29 23:29:34600 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
[email protected]257ab712009-04-14 17:16:24601 fetcher->Start();
602 return fetcher;
603}
604
[email protected]d7ad4772012-06-01 03:12:54605bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) {
606 // TODO(pkasting): Fix |have_suggest_results_|; see http://crbug.com/130631
607 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29608
[email protected]d7ad4772012-06-01 03:12:54609 string16 query;
610 ListValue* root_list = NULL;
611 ListValue* results = NULL;
612 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
613 if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) ||
614 (query != input_text) || !root_list->GetList(1, &results))
initial.commit09911bf2008-07-26 23:55:29615 return false;
616
[email protected]55ce8f12012-05-09 04:44:08617 // 3rd element: Description list.
[email protected]d7ad4772012-06-01 03:12:54618 ListValue* descriptions = NULL;
619 root_list->GetList(2, &descriptions);
initial.commit09911bf2008-07-26 23:55:29620
[email protected]55ce8f12012-05-09 04:44:08621 // 4th element: Disregard the query URL list for now.
initial.commit09911bf2008-07-26 23:55:29622
[email protected]d1f0a7f2012-06-05 10:26:42623 // Reset suggested relevance information from the default provider.
624 if (!is_keyword) {
625 has_suggested_relevance_ = false;
626 verbatim_relevance_ = -1;
627 }
628
[email protected]55ce8f12012-05-09 04:44:08629 // 5th element: Optional key-value pairs from the Suggest server.
[email protected]d7ad4772012-06-01 03:12:54630 ListValue* types = NULL;
[email protected]d1f0a7f2012-06-05 10:26:42631 ListValue* relevances = NULL;
632 DictionaryValue* extras = NULL;
633 if (root_list->GetDictionary(4, &extras)) {
634 extras->GetList("google:suggesttype", &types);
635
636 // Only accept relevance suggestions if Instant is disabled.
637 if (!is_keyword && !InstantController::IsEnabled(profile_)) {
638 // Discard this list if its size does not match that of the suggestions.
639 if (extras->GetList("google:suggestrelevance", &relevances) &&
640 relevances->GetSize() != results->GetSize())
641 relevances = NULL;
642
643 extras->GetInteger("google:verbatimrelevance", &verbatim_relevance_);
644 }
initial.commit09911bf2008-07-26 23:55:29645 }
646
[email protected]d7ad4772012-06-01 03:12:54647 SuggestResults* suggest_results =
648 is_keyword ? &keyword_suggest_results_ : &default_suggest_results_;
649 NavigationResults* navigation_results =
650 is_keyword ? &keyword_navigation_results_ : &default_navigation_results_;
initial.commit09911bf2008-07-26 23:55:29651
[email protected]d1f0a7f2012-06-05 10:26:42652 // Clear the previous results now that new results are available.
653 suggest_results->clear();
654 navigation_results->clear();
655
656 string16 result, title;
657 std::string type;
658 int relevance = -1;
[email protected]d7ad4772012-06-01 03:12:54659 for (size_t index = 0; results->GetString(index, &result); ++index) {
[email protected]8e81f5092010-09-29 23:19:40660 // Google search may return empty suggestions for weird input characters,
[email protected]55ce8f12012-05-09 04:44:08661 // they make no sense at all and can cause problems in our code.
[email protected]d7ad4772012-06-01 03:12:54662 if (result.empty())
[email protected]8e81f5092010-09-29 23:19:40663 continue;
664
[email protected]d1f0a7f2012-06-05 10:26:42665 // Apply valid suggested relevance scores; discard invalid lists.
666 if (relevances != NULL && !relevances->GetInteger(index, &relevance))
667 relevances = NULL;
[email protected]d7ad4772012-06-01 03:12:54668 if (types && types->GetString(index, &type) && (type == "NAVIGATION")) {
[email protected]d1f0a7f2012-06-05 10:26:42669 // Do not blindly trust the URL coming from the server to be valid.
670 GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(result), std::string()));
671 if (url.is_valid()) {
672 if (descriptions != NULL)
673 descriptions->GetString(index, &title);
674 navigation_results->push_back(NavigationResult(url, title, relevance));
initial.commit09911bf2008-07-26 23:55:29675 }
676 } else {
[email protected]d1f0a7f2012-06-05 10:26:42677 // TODO(kochi): Improve calculator result presentation.
678 suggest_results->push_back(SuggestResult(result, relevance));
initial.commit09911bf2008-07-26 23:55:29679 }
680 }
681
[email protected]d1f0a7f2012-06-05 10:26:42682 // Apply calculated relevance scores if a valid list was not provided.
683 if (relevances == NULL) {
684 ApplyCalculatedSuggestRelevance(suggest_results, is_keyword);
685 ApplyCalculatedNavigationRelevance(navigation_results, is_keyword);
686 } else if (!is_keyword) {
687 has_suggested_relevance_ = true;
688 }
689
[email protected]d7ad4772012-06-01 03:12:54690 have_suggest_results_ = true;
[email protected]d1f0a7f2012-06-05 10:26:42691 return true;
initial.commit09911bf2008-07-26 23:55:29692}
693
694void SearchProvider::ConvertResultsToAutocompleteMatches() {
695 // Convert all the results to matches and add them to a map, so we can keep
696 // the most relevant match for each result.
697 MatchMap map;
[email protected]257ab712009-04-14 17:16:24698 const Time no_time;
699 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ?
initial.commit09911bf2008-07-26 23:55:29700 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
701 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]257ab712009-04-14 17:16:24702 // Keyword what you typed results are handled by the KeywordProvider.
initial.commit09911bf2008-07-26 23:55:29703
[email protected]382a0642012-06-06 06:13:52704 int verbatim_relevance = GetVerbatimRelevance();
[email protected]257ab712009-04-14 17:16:24705 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
[email protected]55ce8f12012-05-09 04:44:08706 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
707 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]d1f0a7f2012-06-05 10:26:42708 if (verbatim_relevance > 0) {
709 AddMatchToMap(input_.text(), input_.text(), verbatim_relevance,
710 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
711 did_not_accept_default_suggestion, false, &map);
712 }
[email protected]b9ce8532012-05-24 20:51:15713 const size_t what_you_typed_size = map.size();
[email protected]85b8d6f2012-05-08 20:53:47714 if (!default_provider_suggest_text_.empty()) {
715 AddMatchToMap(input_.text() + default_provider_suggest_text_,
[email protected]55ce8f12012-05-09 04:44:08716 input_.text(), verbatim_relevance + 1,
[email protected]85b8d6f2012-05-08 20:53:47717 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]55ce8f12012-05-09 04:44:08718 did_not_accept_default_suggestion, false, &map);
initial.commit09911bf2008-07-26 23:55:29719 }
720
[email protected]257ab712009-04-14 17:16:24721 AddHistoryResultsToMap(keyword_history_results_, true,
722 did_not_accept_keyword_suggestion, &map);
723 AddHistoryResultsToMap(default_history_results_, false,
724 did_not_accept_default_suggestion, &map);
725
[email protected]55ce8f12012-05-09 04:44:08726 AddSuggestResultsToMap(keyword_suggest_results_, true, &map);
727 AddSuggestResultsToMap(default_suggest_results_, false, &map);
initial.commit09911bf2008-07-26 23:55:29728
729 // Now add the most relevant matches from the map to |matches_|.
730 matches_.clear();
731 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
732 matches_.push_back(i->second);
733
[email protected]257ab712009-04-14 17:16:24734 AddNavigationResultsToMatches(keyword_navigation_results_, true);
735 AddNavigationResultsToMatches(default_navigation_results_, false);
initial.commit09911bf2008-07-26 23:55:29736
[email protected]b9ce8532012-05-24 20:51:15737 // Allow an additional match for "what you typed" if it's present.
738 const size_t max_total_matches = kMaxMatches + what_you_typed_size;
initial.commit09911bf2008-07-26 23:55:29739 std::partial_sort(matches_.begin(),
740 matches_.begin() + std::min(max_total_matches, matches_.size()),
741 matches_.end(), &AutocompleteMatch::MoreRelevant);
[email protected]3723e6e2012-06-11 21:06:56742
743 // If the top match is effectively 'verbatim' but exceeds the calculated
744 // verbatim relevance, and REQUESTED_URL |input_| has a |desired_tld|
745 // (for example ".com" when the CTRL key is pressed for REQUESTED_URL input),
746 // promote a URL_WHAT_YOU_TYPED match to the top. Otherwise, these matches can
747 // stomp the HistoryURLProvider's similar transient URL_WHAT_YOU_TYPED match,
748 // and CTRL+ENTER will invoke the search instead of the expected navigation.
749 if ((has_suggested_relevance_ || verbatim_relevance_ >= 0) &&
750 input_.type() == AutocompleteInput::REQUESTED_URL &&
751 !input_.desired_tld().empty() && !matches_.empty() &&
752 matches_.front().relevance > CalculateRelevanceForVerbatim() &&
753 matches_.front().fill_into_edit == input_.text()) {
754 AutocompleteMatch match = HistoryURLProvider::SuggestExactInput(
755 this, input_, !HasHTTPScheme(input_.text()));
756 match.relevance = matches_.front().relevance + 1;
757 matches_.insert(matches_.begin(), match);
758 }
759
initial.commit09911bf2008-07-26 23:55:29760 if (matches_.size() > max_total_matches)
[email protected]a28e95662008-11-12 19:19:02761 matches_.erase(matches_.begin() + max_total_matches, matches_.end());
initial.commit09911bf2008-07-26 23:55:29762
[email protected]382a0642012-06-06 06:13:52763 // Check constraints that may be violated by suggested relevances.
[email protected]d1f0a7f2012-06-05 10:26:42764 if (!matches_.empty() &&
[email protected]e6acd002012-06-16 22:27:47765 (has_suggested_relevance_ || verbatim_relevance_ >= 0)) {
[email protected]382a0642012-06-06 06:13:52766 bool reconstruct_matches = false;
[email protected]1beee342012-06-19 22:22:28767 if (matches_.front().type != AutocompleteMatch::SEARCH_WHAT_YOU_TYPED &&
768 matches_.front().type != AutocompleteMatch::URL_WHAT_YOU_TYPED &&
769 matches_.front().inline_autocomplete_offset == string16::npos &&
770 matches_.front().fill_into_edit != input_.text()) {
771 // Disregard suggested relevances if the top match is not SWYT, inlinable,
772 // or URL_WHAT_YOU_TYPED (which may be top match regardless of inlining).
773 // For example, input "foo" should not invoke a search for "bar", which
774 // would happen if the "bar" search match outranked all other matches.
775 ApplyCalculatedRelevance();
776 reconstruct_matches = true;
777 } else if (matches_.front().relevance < CalculateRelevanceForVerbatim()) {
778 // Disregard the suggested verbatim relevance if the top score is below
779 // the usual verbatim value. For example, a BarProvider may rely on
780 // SearchProvider's verbatim or inlineable matches for input "foo" to
781 // always outrank its own lowly-ranked non-inlineable "bar" match.
782 verbatim_relevance_ = -1;
783 reconstruct_matches = true;
[email protected]e6acd002012-06-16 22:27:47784 }
785 if (input_.type() == AutocompleteInput::URL &&
786 matches_.front().relevance > CalculateRelevanceForVerbatim() &&
787 (matches_.front().type == AutocompleteMatch::SEARCH_SUGGEST ||
788 matches_.front().type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED)) {
789 // Disregard the suggested search and verbatim relevances if the input
790 // type is URL and the top match is a highly-ranked search suggestion.
791 // For example, prevent a search for "foo.com" from outranking another
792 // provider's navigation for "foo.com" or "foo.com/url_from_history".
793 // Reconstruction will also ensure that the new top match is inlineable.
794 ApplyCalculatedSuggestRelevance(&keyword_suggest_results_, true);
795 ApplyCalculatedSuggestRelevance(&default_suggest_results_, false);
[email protected]382a0642012-06-06 06:13:52796 verbatim_relevance_ = -1;
797 reconstruct_matches = true;
798 }
799 if (reconstruct_matches) {
800 ConvertResultsToAutocompleteMatches();
801 return;
802 }
[email protected]d1f0a7f2012-06-05 10:26:42803 }
804
[email protected]cc63dea2008-08-21 20:56:31805 UpdateStarredStateOfMatches();
[email protected]4ab4c7c2010-11-24 04:49:34806 UpdateDone();
[email protected]257ab712009-04-14 17:16:24807}
808
809void SearchProvider::AddNavigationResultsToMatches(
810 const NavigationResults& navigation_results,
811 bool is_keyword) {
812 if (!navigation_results.empty()) {
[email protected]6c535842012-05-15 05:20:55813 // TODO(kochi|msw): Add more navigational results if they get more
814 // meaningful relevance values; see http://b/1170574.
[email protected]d7ad4772012-06-01 03:12:54815 // CompareScoredResults sorts by descending relevance; so use min_element.
[email protected]6c535842012-05-15 05:20:55816 NavigationResults::const_iterator result(
[email protected]d7ad4772012-06-01 03:12:54817 std::min_element(navigation_results.begin(),
[email protected]6c535842012-05-15 05:20:55818 navigation_results.end(),
819 CompareScoredResults()));
820 matches_.push_back(NavigationToMatch(*result, is_keyword));
[email protected]257ab712009-04-14 17:16:24821 }
822}
823
824void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
825 bool is_keyword,
826 int did_not_accept_suggestion,
827 MatchMap* map) {
[email protected]51124552011-07-16 01:37:10828 if (results.empty())
829 return;
830
[email protected]d7ad4772012-06-01 03:12:54831 bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() ||
832 (input_.type() == AutocompleteInput::URL);
833 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
[email protected]51124552011-07-16 01:37:10834 bool input_multiple_words = HasMultipleWords(input_text);
835
[email protected]55ce8f12012-05-09 04:44:08836 SuggestResults scored_results;
837 if (!prevent_inline_autocomplete && input_multiple_words) {
838 // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit
839 // queries if the input also has multiple words. But if we were already
[email protected]51124552011-07-16 01:37:10840 // autocompleting a multi-word, multi-visit query, and the current input is
841 // still a prefix of it, then changing the autocompletion suddenly feels
842 // wrong. To detect this case, first score as if only one word has been
843 // typed, then check for a best result that is an autocompleted, multi-word
844 // query. If we find one, then just keep that score set.
[email protected]55ce8f12012-05-09 04:44:08845 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
846 false, input_text, is_keyword);
847 if ((scored_results[0].relevance() <
848 AutocompleteResult::kLowestDefaultScore) ||
849 !HasMultipleWords(scored_results[0].suggestion()))
850 scored_results.clear(); // Didn't detect the case above, score normally.
[email protected]51124552011-07-16 01:37:10851 }
[email protected]55ce8f12012-05-09 04:44:08852 if (scored_results.empty())
853 scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete,
854 input_multiple_words, input_text,
855 is_keyword);
856 for (SuggestResults::const_iterator i(scored_results.begin());
857 i != scored_results.end(); ++i) {
858 AddMatchToMap(i->suggestion(), input_text, i->relevance(),
[email protected]51124552011-07-16 01:37:10859 AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion,
[email protected]55ce8f12012-05-09 04:44:08860 is_keyword, map);
[email protected]51124552011-07-16 01:37:10861 }
862}
863
[email protected]55ce8f12012-05-09 04:44:08864SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
[email protected]51124552011-07-16 01:37:10865 const HistoryResults& results,
866 bool base_prevent_inline_autocomplete,
867 bool input_multiple_words,
868 const string16& input_text,
869 bool is_keyword) {
[email protected]810ffba2012-06-12 01:07:48870 AutocompleteClassifier* classifier =
871 AutocompleteClassifierFactory::GetForProfile(profile_);
[email protected]55ce8f12012-05-09 04:44:08872 SuggestResults scored_results;
[email protected]257ab712009-04-14 17:16:24873 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
874 ++i) {
[email protected]51124552011-07-16 01:37:10875 // Don't autocomplete multi-word queries that have only been seen once
876 // unless the user has typed more than one word.
877 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete ||
878 (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term));
879
[email protected]ea3b9a502011-04-04 14:19:37880 // Don't autocomplete search terms that would normally be treated as URLs
[email protected]51124552011-07-16 01:37:10881 // when typed. For example, if the user searched for "google.com" and types
882 // "goog", don't autocomplete to the search term "google.com". Otherwise,
883 // the input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:48884 // NOTE: We don't check this in the following cases:
885 // * When inline autocomplete is disabled, we won't be inline
886 // autocompleting this term, so we don't need to worry about confusion as
887 // much. This also prevents calling Classify() again from inside the
888 // classifier (which will corrupt state and likely crash), since the
[email protected]51124552011-07-16 01:37:10889 // classifier always disables inline autocomplete.
[email protected]cc447362011-04-06 03:57:48890 // * When the user has typed the whole term, the "what you typed" history
891 // match will outrank us for URL-like inputs anyway, so we need not do
892 // anything special.
[email protected]51124552011-07-16 01:37:10893 if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) {
[email protected]ea3b9a502011-04-04 14:19:37894 AutocompleteMatch match;
[email protected]72874a8d2011-05-11 03:48:54895 classifier->Classify(i->term, string16(), false, false, &match, NULL);
[email protected]2905f742011-10-13 03:51:58896 prevent_inline_autocomplete =
897 match.transition == content::PAGE_TRANSITION_TYPED;
[email protected]ea3b9a502011-04-04 14:19:37898 }
[email protected]51124552011-07-16 01:37:10899
900 int relevance = CalculateRelevanceForHistory(i->time, is_keyword,
901 prevent_inline_autocomplete);
[email protected]55ce8f12012-05-09 04:44:08902 scored_results.push_back(SuggestResult(i->term, relevance));
[email protected]257ab712009-04-14 17:16:24903 }
[email protected]51124552011-07-16 01:37:10904
905 // History returns results sorted for us. However, we may have docked some
906 // results' scores, so things are no longer in order. Do a stable sort to get
907 // things back in order without otherwise disturbing results with equal
908 // scores, then force the scores to be unique, so that the order in which
909 // they're shown is deterministic.
[email protected]55ce8f12012-05-09 04:44:08910 std::stable_sort(scored_results.begin(), scored_results.end(),
911 CompareScoredResults());
[email protected]51124552011-07-16 01:37:10912 int last_relevance = 0;
[email protected]55ce8f12012-05-09 04:44:08913 for (SuggestResults::iterator i(scored_results.begin());
914 i != scored_results.end(); ++i) {
915 if ((i != scored_results.begin()) && (i->relevance() >= last_relevance))
916 i->set_relevance(last_relevance - 1);
917 last_relevance = i->relevance();
[email protected]51124552011-07-16 01:37:10918 }
919
[email protected]55ce8f12012-05-09 04:44:08920 return scored_results;
[email protected]257ab712009-04-14 17:16:24921}
922
[email protected]55ce8f12012-05-09 04:44:08923void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results,
924 bool is_keyword,
925 MatchMap* map) {
[email protected]d7ad4772012-06-01 03:12:54926 const string16& input_text = is_keyword ? keyword_input_text_ : input_.text();
[email protected]55ce8f12012-05-09 04:44:08927 for (size_t i = 0; i < results.size(); ++i) {
[email protected]d7ad4772012-06-01 03:12:54928 AddMatchToMap(results[i].suggestion(), input_text, results[i].relevance(),
[email protected]55ce8f12012-05-09 04:44:08929 AutocompleteMatch::SEARCH_SUGGEST, i, is_keyword, map);
[email protected]257ab712009-04-14 17:16:24930 }
initial.commit09911bf2008-07-26 23:55:29931}
932
[email protected]382a0642012-06-06 06:13:52933int SearchProvider::GetVerbatimRelevance() const {
[email protected]dc6943b2012-06-19 06:39:56934 // Use the suggested verbatim relevance score if it is non-negative (valid),
935 // if inline autocomplete isn't prevented (always show verbatim on backspace),
[email protected]1beee342012-06-19 22:22:28936 // and if it won't suppress verbatim, leaving no default provider matches.
937 // Otherwise, if the default provider returned no matches and was still able
[email protected]dc6943b2012-06-19 06:39:56938 // to suppress verbatim, the user would have no search/nav matches and may be
[email protected]1beee342012-06-19 22:22:28939 // left unable to search using their default provider from the omnibox.
[email protected]dc6943b2012-06-19 06:39:56940 // Check for results on each verbatim calculation, as results from older
941 // queries (on previous input) may be trimmed for failing to inline new input.
942 if (verbatim_relevance_ >= 0 && !input_.prevent_inline_autocomplete() &&
[email protected]1beee342012-06-19 22:22:28943 (verbatim_relevance_ > 0 ||
944 !default_suggest_results_.empty() ||
[email protected]dc6943b2012-06-19 06:39:56945 !default_navigation_results_.empty())) {
[email protected]d1f0a7f2012-06-05 10:26:42946 return verbatim_relevance_;
[email protected]dc6943b2012-06-19 06:39:56947 }
[email protected]382a0642012-06-06 06:13:52948 return CalculateRelevanceForVerbatim();
949}
[email protected]d1f0a7f2012-06-05 10:26:42950
[email protected]382a0642012-06-06 06:13:52951int SearchProvider::CalculateRelevanceForVerbatim() const {
[email protected]85b8d6f2012-05-08 20:53:47952 if (!providers_.keyword_provider().empty())
[email protected]52d08b12009-10-19 18:42:36953 return 250;
954
initial.commit09911bf2008-07-26 23:55:29955 switch (input_.type()) {
956 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:36957 case AutocompleteInput::QUERY:
958 case AutocompleteInput::FORCED_QUERY:
959 return 1300;
initial.commit09911bf2008-07-26 23:55:29960
961 case AutocompleteInput::REQUESTED_URL:
[email protected]52d08b12009-10-19 18:42:36962 return 1150;
initial.commit09911bf2008-07-26 23:55:29963
964 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:36965 return 850;
initial.commit09911bf2008-07-26 23:55:29966
967 default:
968 NOTREACHED();
969 return 0;
970 }
971}
972
[email protected]51124552011-07-16 01:37:10973int SearchProvider::CalculateRelevanceForHistory(
974 const Time& time,
975 bool is_keyword,
976 bool prevent_inline_autocomplete) const {
[email protected]aa613d62010-11-09 20:40:18977 // The relevance of past searches falls off over time. There are two distinct
978 // equations used. If the first equation is used (searches to the primary
[email protected]51124552011-07-16 01:37:10979 // provider that we want to inline autocomplete), the score starts at 1399 and
980 // falls to 1300. If the second equation is used the relevance of a search 15
981 // minutes ago is discounted 50 points, while the relevance of a search two
982 // weeks ago is discounted 450 points.
[email protected]aa613d62010-11-09 20:40:18983 double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.);
[email protected]51124552011-07-16 01:37:10984 bool is_primary_provider = providers_.is_primary_provider(is_keyword);
985 if (is_primary_provider && !prevent_inline_autocomplete) {
[email protected]aa613d62010-11-09 20:40:18986 // Searches with the past two days get a different curve.
[email protected]51124552011-07-16 01:37:10987 const double autocomplete_time = 2 * 24 * 60 * 60;
[email protected]aa613d62010-11-09 20:40:18988 if (elapsed_time < autocomplete_time) {
[email protected]e17511f2011-07-13 14:09:18989 return (is_keyword ? 1599 : 1399) - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:18990 std::pow(elapsed_time / autocomplete_time, 2.5));
991 }
992 elapsed_time -= autocomplete_time;
993 }
994
[email protected]c3a4bd992010-08-18 20:25:01995 const int score_discount =
996 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:29997
[email protected]6c85aa02009-02-27 12:08:09998 // Don't let scores go below 0. Negative relevance scores are meaningful in
999 // a different way.
initial.commit09911bf2008-07-26 23:55:291000 int base_score;
[email protected]51124552011-07-16 01:37:101001 if (is_primary_provider)
[email protected]52d08b12009-10-19 18:42:361002 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
[email protected]51124552011-07-16 01:37:101003 else
1004 base_score = 200;
initial.commit09911bf2008-07-26 23:55:291005 return std::max(0, base_score - score_discount);
1006}
1007
[email protected]55ce8f12012-05-09 04:44:081008int SearchProvider::CalculateRelevanceForSuggestion(bool for_keyword) const {
1009 return !providers_.is_primary_provider(for_keyword) ? 100 :
1010 ((input_.type() == AutocompleteInput::URL) ? 300 : 600);
initial.commit09911bf2008-07-26 23:55:291011}
1012
[email protected]55ce8f12012-05-09 04:44:081013int SearchProvider::CalculateRelevanceForNavigation(bool for_keyword) const {
1014 return providers_.is_primary_provider(for_keyword) ? 800 : 150;
initial.commit09911bf2008-07-26 23:55:291015}
1016
[email protected]a2fedb1e2011-01-25 15:23:361017void SearchProvider::AddMatchToMap(const string16& query_string,
1018 const string16& input_text,
initial.commit09911bf2008-07-26 23:55:291019 int relevance,
[email protected]4c1fb7ec2008-11-13 00:19:001020 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:291021 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:241022 bool is_keyword,
initial.commit09911bf2008-07-26 23:55:291023 MatchMap* map) {
[email protected]92513682011-09-01 06:16:521024 AutocompleteMatch match(this, relevance, false, type);
initial.commit09911bf2008-07-26 23:55:291025 std::vector<size_t> content_param_offsets;
[email protected]85b8d6f2012-05-08 20:53:471026 // Bail out now if we don't actually have a valid provider.
1027 match.keyword = is_keyword ?
[email protected]3954c3a2012-04-10 20:17:551028 providers_.keyword_provider() : providers_.default_provider();
[email protected]85b8d6f2012-05-08 20:53:471029 const TemplateURL* provider_url = match.GetTemplateURL(profile_);
1030 if (provider_url == NULL)
1031 return;
1032
[email protected]70833262011-01-05 23:40:441033 match.contents.assign(query_string);
[email protected]fb5153c52009-07-31 19:40:331034 // We do intra-string highlighting for suggestions - the suggested segment
1035 // will be highlighted, e.g. for input_text = "you" the suggestion may be
1036 // "youtube", so we'll bold the "tube" section: you*tube*.
1037 if (input_text != query_string) {
[email protected]fb5153c52009-07-31 19:40:331038 size_t input_position = match.contents.find(input_text);
[email protected]a2fedb1e2011-01-25 15:23:361039 if (input_position == string16::npos) {
[email protected]fb5153c52009-07-31 19:40:331040 // The input text is not a substring of the query string, e.g. input
1041 // text is "slasdot" and the query string is "slashdot", so we bold the
1042 // whole thing.
1043 match.contents_class.push_back(
1044 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]ec2379162009-06-09 23:58:171045 } else {
[email protected]fb5153c52009-07-31 19:40:331046 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
1047 // "bold" this. Consider modifying the terminology.
1048 // We don't iterate over the string here annotating all matches because
1049 // it looks odd to have every occurrence of a substring that may be as
1050 // short as a single character highlighted in a query suggestion result,
1051 // e.g. for input text "s" and query string "southwest airlines", it
1052 // looks odd if both the first and last s are highlighted.
1053 if (input_position != 0) {
1054 match.contents_class.push_back(
1055 ACMatchClassification(0, ACMatchClassification::NONE));
1056 }
1057 match.contents_class.push_back(
1058 ACMatchClassification(input_position, ACMatchClassification::DIM));
1059 size_t next_fragment_position = input_position + input_text.length();
1060 if (next_fragment_position < query_string.length()) {
1061 match.contents_class.push_back(
1062 ACMatchClassification(next_fragment_position,
1063 ACMatchClassification::NONE));
1064 }
[email protected]ec2379162009-06-09 23:58:171065 }
initial.commit09911bf2008-07-26 23:55:291066 } else {
[email protected]fb5153c52009-07-31 19:40:331067 // Otherwise, we're dealing with the "default search" result which has no
[email protected]70833262011-01-05 23:40:441068 // completion.
[email protected]fb5153c52009-07-31 19:40:331069 match.contents_class.push_back(
1070 ACMatchClassification(0, ACMatchClassification::NONE));
initial.commit09911bf2008-07-26 23:55:291071 }
1072
1073 // When the user forced a query, we need to make sure all the fill_into_edit
1074 // values preserve that property. Otherwise, if the user starts editing a
1075 // suggestion, non-Search results will suddenly appear.
1076 size_t search_start = 0;
1077 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
[email protected]a2fedb1e2011-01-25 15:23:361078 match.fill_into_edit.assign(ASCIIToUTF16("?"));
initial.commit09911bf2008-07-26 23:55:291079 ++search_start;
1080 }
[email protected]c0048b42009-05-04 21:47:171081 if (is_keyword) {
[email protected]033f3422012-03-13 21:24:181082 match.fill_into_edit.append(match.keyword + char16(' '));
1083 search_start += match.keyword.length() + 1;
[email protected]c0048b42009-05-04 21:47:171084 }
initial.commit09911bf2008-07-26 23:55:291085 match.fill_into_edit.append(query_string);
[email protected]2c33dd22010-02-11 21:46:351086 // Not all suggestions start with the original input.
[email protected]55ce8f12012-05-09 04:44:081087 if (!input_.prevent_inline_autocomplete() &&
[email protected]257ab712009-04-14 17:16:241088 !match.fill_into_edit.compare(search_start, input_text.length(),
1089 input_text))
1090 match.inline_autocomplete_offset = search_start + input_text.length();
initial.commit09911bf2008-07-26 23:55:291091
[email protected]85b8d6f2012-05-08 20:53:471092 const TemplateURLRef& search_url = provider_url->url_ref();
[email protected]360ba052012-04-04 17:26:131093 DCHECK(search_url.SupportsReplacement());
[email protected]bca359b2012-06-24 07:53:041094 match.search_terms_args.reset(
1095 new TemplateURLRef::SearchTermsArgs(query_string));
1096 match.search_terms_args->original_query = input_text;
1097 match.search_terms_args->accepted_suggestion = accepted_suggestion;
1098 // This is the destination URL sans assisted query stats. This must be set
1099 // so the AutocompleteController can properly de-dupe; the controller will
1100 // eventually overwrite it before it reaches the user.
1101 match.destination_url =
1102 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get()));
initial.commit09911bf2008-07-26 23:55:291103
1104 // Search results don't look like URLs.
[email protected]2905f742011-10-13 03:51:581105 match.transition = is_keyword ?
1106 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED;
initial.commit09911bf2008-07-26 23:55:291107
1108 // Try to add |match| to |map|. If a match for |query_string| is already in
1109 // |map|, replace it if |match| is more relevant.
1110 // NOTE: Keep this ToLower() call in sync with url_database.cc.
1111 const std::pair<MatchMap::iterator, bool> i = map->insert(
[email protected]a2fedb1e2011-01-25 15:23:361112 std::pair<string16, AutocompleteMatch>(
[email protected]503d03872011-05-06 08:36:261113 base::i18n::ToLower(query_string), match));
initial.commit09911bf2008-07-26 23:55:291114 // NOTE: We purposefully do a direct relevance comparison here instead of
1115 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
1116 // first" rather than "items alphabetically first" when the scores are equal.
1117 // The only case this matters is when a user has results with the same score
1118 // that differ only by capitalization; because the history system returns
1119 // results sorted by recency, this means we'll pick the most recent such
1120 // result even if the precision of our relevance score is too low to
1121 // distinguish the two.
1122 if (!i.second && (match.relevance > i.first->second.relevance))
1123 i.first->second = match;
1124}
1125
1126AutocompleteMatch SearchProvider::NavigationToMatch(
1127 const NavigationResult& navigation,
[email protected]257ab712009-04-14 17:16:241128 bool is_keyword) {
[email protected]371dab12012-06-01 03:23:551129 const string16& input = is_keyword ? keyword_input_text_ : input_.text();
[email protected]55ce8f12012-05-09 04:44:081130 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]4c1fb7ec2008-11-13 00:19:001131 AutocompleteMatch::NAVSUGGEST);
[email protected]55ce8f12012-05-09 04:44:081132 match.destination_url = navigation.url();
[email protected]371dab12012-06-01 03:23:551133
1134 // First look for the user's input inside the fill_into_edit as it would be
1135 // without trimming the scheme, so we can find matches at the beginning of the
1136 // scheme.
1137 const string16 untrimmed_fill_into_edit(
1138 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1139 StringForURLDisplay(navigation.url(), true, false)));
1140 const URLPrefix* prefix =
1141 URLPrefix::BestURLPrefix(untrimmed_fill_into_edit, input);
1142 size_t match_start = (prefix == NULL) ?
1143 untrimmed_fill_into_edit.find(input) : prefix->prefix.length();
1144 size_t inline_autocomplete_offset = (prefix == NULL) ?
1145 string16::npos : (match_start + input.length());
1146 bool trim_http = !HasHTTPScheme(input) && (!prefix || (match_start != 0));
1147
1148 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1149 // Otherwise, user edits to a suggestion would show non-Search results.
1150 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
1151 match.fill_into_edit = ASCIIToUTF16("?");
1152 if (inline_autocomplete_offset != string16::npos)
1153 ++inline_autocomplete_offset;
1154 }
1155
1156 const std::string languages(
1157 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
1158 const net::FormatUrlTypes format_types =
1159 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP);
1160 match.fill_into_edit +=
1161 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
1162 net::FormatUrl(navigation.url(), languages, format_types,
1163 net::UnescapeRule::SPACES, NULL, NULL,
1164 &inline_autocomplete_offset));
1165 if (!input_.prevent_inline_autocomplete())
1166 match.inline_autocomplete_offset = inline_autocomplete_offset;
1167 DCHECK((match.inline_autocomplete_offset == string16::npos) ||
1168 (match.inline_autocomplete_offset <= match.fill_into_edit.length()));
1169
1170 match.contents = net::FormatUrl(navigation.url(), languages,
1171 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start);
1172 // If the first match in the untrimmed string was inside a scheme that we
1173 // trimmed, look for a subsequent match.
1174 if (match_start == string16::npos)
1175 match_start = match.contents.find(input);
1176 // Safe if |match_start| is npos; also safe if the input is longer than the
1177 // remaining contents after |match_start|.
1178 AutocompleteMatch::ClassifyLocationInString(match_start, input.length(),
1179 match.contents.length(), ACMatchClassification::URL,
1180 &match.contents_class);
initial.commit09911bf2008-07-26 23:55:291181
[email protected]55ce8f12012-05-09 04:44:081182 match.description = navigation.description();
[email protected]371dab12012-06-01 03:23:551183 AutocompleteMatch::ClassifyMatchInString(input, match.description,
1184 ACMatchClassification::NONE, &match.description_class);
initial.commit09911bf2008-07-26 23:55:291185 return match;
1186}
[email protected]4ab4c7c2010-11-24 04:49:341187
1188void SearchProvider::UpdateDone() {
1189 // We're done when there are no more suggest queries pending (this is set to 1
1190 // when the timer is started) and we're not waiting on instant.
1191 done_ = ((suggest_results_pending_ == 0) &&
1192 (instant_finalized_ || !InstantController::IsEnabled(profile_)));
1193}