blob: 915ce9f16469a6dc4823f4a864115b3d66df33a5 [file] [log] [blame]
[email protected]f90bf0d92011-01-13 02:12:441// Copyright (c) 2011 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]503d03872011-05-06 08:36:2611#include "base/i18n/case_conversion.h"
[email protected]d6e58c6e2009-10-10 20:40:5012#include "base/i18n/icu_string_conversions.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/message_loop.h"
[email protected]dc9a6762010-08-16 07:13:5314#include "base/string16.h"
[email protected]1cb2dac2010-03-08 21:49:1515#include "base/utf_string_conversions.h"
[email protected]ea3b9a502011-04-04 14:19:3716#include "chrome/browser/autocomplete/autocomplete_classifier.h"
[email protected]9ac40092010-10-27 23:05:2617#include "chrome/browser/autocomplete/autocomplete_match.h"
[email protected]2c812ba02011-07-14 00:23:1518#include "chrome/browser/autocomplete/keyword_provider.h"
[email protected]ce560f82009-06-03 09:39:4419#include "chrome/browser/history/history.h"
[email protected]4ab4c7c2010-11-24 04:49:3420#include "chrome/browser/instant/instant_controller.h"
[email protected]f870a322009-01-16 21:47:2721#include "chrome/browser/net/url_fixer_upper.h"
[email protected]37858e52010-08-26 00:22:0222#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3323#include "chrome/browser/profiles/profile.h"
[email protected]8d457132010-11-04 18:13:4024#include "chrome/browser/history/in_memory_database.h"
[email protected]8e5c89a2011-06-07 18:13:3325#include "chrome/browser/search_engines/template_url_service.h"
26#include "chrome/browser/search_engines/template_url_service_factory.h"
initial.commit09911bf2008-07-26 23:55:2927#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0228#include "chrome/common/url_constants.h"
[email protected]c3113022011-04-16 03:26:3029#include "content/common/json_value_serializer.h"
initial.commit09911bf2008-07-26 23:55:2930#include "googleurl/src/url_util.h"
[email protected]34ac8f32009-02-22 23:03:2731#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2932#include "net/base/escape.h"
[email protected]319d9e6f2009-02-18 19:47:2133#include "net/http/http_response_headers.h"
34#include "net/url_request/url_request_status.h"
[email protected]c051a1b2011-01-21 23:30:1735#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2936
[email protected]e1acf6f2008-10-27 20:43:3337using base::Time;
38using base::TimeDelta;
39
[email protected]b547666d2009-04-23 16:37:5840// static
41const int SearchProvider::kDefaultProviderURLFetcherID = 1;
42// static
43const int SearchProvider::kKeywordProviderURLFetcherID = 2;
44
45// static
46bool SearchProvider::query_suggest_immediately_ = false;
47
[email protected]257ab712009-04-14 17:16:2448void SearchProvider::Providers::Set(const TemplateURL* default_provider,
49 const TemplateURL* keyword_provider) {
50 // TODO(pkasting): http://b/1162970 We shouldn't need to structure-copy
51 // this. Nor should we need |default_provider_| and |keyword_provider_|
52 // just to know whether the provider changed.
53 default_provider_ = default_provider;
54 if (default_provider)
55 cached_default_provider_ = *default_provider;
56 keyword_provider_ = keyword_provider;
57 if (keyword_provider)
58 cached_keyword_provider_ = *keyword_provider;
59}
60
[email protected]601858c02010-09-01 17:08:2061SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile)
62 : AutocompleteProvider(listener, profile, "Search"),
[email protected]601858c02010-09-01 17:08:2063 suggest_results_pending_(0),
[email protected]8e5cc282010-12-05 18:11:3964 have_suggest_results_(false),
[email protected]4ab4c7c2010-11-24 04:49:3465 instant_finalized_(false) {
66}
67
[email protected]a2fedb1e2011-01-25 15:23:3668void SearchProvider::FinalizeInstantQuery(const string16& input_text,
69 const string16& suggest_text) {
[email protected]4ab4c7c2010-11-24 04:49:3470 if (done_ || instant_finalized_)
71 return;
72
73 instant_finalized_ = true;
74 UpdateDone();
75
[email protected]e918c112010-12-08 23:03:4976 if (input_text.empty()) {
[email protected]4ab4c7c2010-11-24 04:49:3477 // We only need to update the listener if we're actually done.
78 if (done_)
79 listener_->OnProviderUpdate(false);
80 return;
81 }
82
[email protected]9e789742011-01-10 23:27:3283 default_provider_suggest_text_ = suggest_text;
84
[email protected]a2fedb1e2011-01-25 15:23:3685 string16 adjusted_input_text(input_text);
[email protected]e918c112010-12-08 23:03:4986 AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(),
87 &adjusted_input_text);
88
[email protected]a2fedb1e2011-01-25 15:23:3689 const string16 text = adjusted_input_text + suggest_text;
[email protected]4ab4c7c2010-11-24 04:49:3490 // Remove any matches that are identical to |text|. We don't use the
91 // destination_url for comparison as it varies depending upon the index passed
92 // to TemplateURL::ReplaceSearchTerms.
93 for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) {
[email protected]4ab4c7c2010-11-24 04:49:3494 if (((i->type == AutocompleteMatch::SEARCH_HISTORY) ||
95 (i->type == AutocompleteMatch::SEARCH_SUGGEST)) &&
96 (i->fill_into_edit == text)) {
[email protected]e030de62010-11-24 05:41:1997 i = matches_.erase(i);
[email protected]4ab4c7c2010-11-24 04:49:3498 } else {
99 ++i;
100 }
101 }
102
103 // Add the new suggest result. We give it a rank higher than
104 // SEARCH_WHAT_YOU_TYPED so that it gets autocompleted.
105 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
106 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
107 TemplateURLRef::NO_SUGGESTION_CHOSEN;
108 MatchMap match_map;
[email protected]e918c112010-12-08 23:03:49109 AddMatchToMap(text, adjusted_input_text,
110 CalculateRelevanceForWhatYouTyped() + 1,
[email protected]4ab4c7c2010-11-24 04:49:34111 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]e918c112010-12-08 23:03:49112 did_not_accept_default_suggestion, false,
[email protected]e0b231d2011-05-09 21:26:42113 input_.prevent_inline_autocomplete(), &match_map);
[email protected]4ab4c7c2010-11-24 04:49:34114 DCHECK_EQ(1u, match_map.size());
115 matches_.push_back(match_map.begin()->second);
[email protected]4ab4c7c2010-11-24 04:49:34116
117 listener_->OnProviderUpdate(true);
[email protected]601858c02010-09-01 17:08:20118}
119
initial.commit09911bf2008-07-26 23:55:29120void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27121 bool minimal_changes) {
initial.commit09911bf2008-07-26 23:55:29122 matches_.clear();
123
[email protected]ea3b9a502011-04-04 14:19:37124 instant_finalized_ =
125 (input.matches_requested() != AutocompleteInput::ALL_MATCHES);
[email protected]4ab4c7c2010-11-24 04:49:34126
[email protected]6c85aa02009-02-27 12:08:09127 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29128 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
129 Stop();
130 return;
131 }
132
[email protected]257ab712009-04-14 17:16:24133 keyword_input_text_.clear();
134 const TemplateURL* keyword_provider =
135 KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input,
136 &keyword_input_text_);
[email protected]8d457132010-11-04 18:13:40137 if (keyword_input_text_.empty())
[email protected]257ab712009-04-14 17:16:24138 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24139
140 const TemplateURL* default_provider =
[email protected]8e5c89a2011-06-07 18:13:33141 TemplateURLServiceFactory::GetForProfile(profile_)->
142 GetDefaultSearchProvider();
[email protected]257ab712009-04-14 17:16:24143 if (!TemplateURL::SupportsReplacement(default_provider))
144 default_provider = NULL;
145
146 if (keyword_provider == default_provider)
[email protected]e17511f2011-07-13 14:09:18147 default_provider = NULL; // No use in querying the same provider twice.
[email protected]257ab712009-04-14 17:16:24148
149 if (!default_provider && !keyword_provider) {
150 // No valid providers.
initial.commit09911bf2008-07-26 23:55:29151 Stop();
152 return;
153 }
154
155 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24156 // or the providers, abort the query.
[email protected]9e789742011-01-10 23:27:32157 if (!minimal_changes ||
158 !providers_.equals(default_provider, keyword_provider)) {
159 if (done_)
160 default_provider_suggest_text_.clear();
161 else
162 Stop();
[email protected]257ab712009-04-14 17:16:24163 }
initial.commit09911bf2008-07-26 23:55:29164
[email protected]257ab712009-04-14 17:16:24165 providers_.Set(default_provider, keyword_provider);
initial.commit09911bf2008-07-26 23:55:29166
167 if (input.text().empty()) {
168 // User typed "?" alone. Give them a placeholder result indicating what
169 // this syntax does.
[email protected]257ab712009-04-14 17:16:24170 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00171 AutocompleteMatch match;
172 match.provider = this;
[email protected]a2fedb1e2011-01-25 15:23:36173 match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24174 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35175 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]0385fc682011-07-07 19:36:17176 match.template_url = &providers_.default_provider();
[email protected]257ab712009-04-14 17:16:24177 matches_.push_back(match);
[email protected]257ab712009-04-14 17:16:24178 }
initial.commit09911bf2008-07-26 23:55:29179 Stop();
180 return;
181 }
182
183 input_ = input;
184
[email protected]8d457132010-11-04 18:13:40185 DoHistoryQuery(minimal_changes);
[email protected]8deeb952008-10-09 18:21:27186 StartOrStopSuggestQuery(minimal_changes);
initial.commit09911bf2008-07-26 23:55:29187 ConvertResultsToAutocompleteMatches();
188}
189
190void SearchProvider::Run() {
191 // Start a new request with the current input.
192 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24193 suggest_results_pending_ = 0;
194 if (providers_.valid_suggest_for_keyword_provider()) {
195 suggest_results_pending_++;
196 keyword_fetcher_.reset(
[email protected]b547666d2009-04-23 16:37:58197 CreateSuggestFetcher(kKeywordProviderURLFetcherID,
198 providers_.keyword_provider(),
[email protected]257ab712009-04-14 17:16:24199 keyword_input_text_));
200 }
201 if (providers_.valid_suggest_for_default_provider()) {
202 suggest_results_pending_++;
203 default_fetcher_.reset(
[email protected]b547666d2009-04-23 16:37:58204 CreateSuggestFetcher(kDefaultProviderURLFetcherID,
205 providers_.default_provider(), input_.text()));
[email protected]257ab712009-04-14 17:16:24206 }
207 // We should only get here if we have a suggest url for the keyword or default
208 // providers.
[email protected]1cb2dac2010-03-08 21:49:15209 DCHECK_GT(suggest_results_pending_, 0);
initial.commit09911bf2008-07-26 23:55:29210}
211
212void SearchProvider::Stop() {
initial.commit09911bf2008-07-26 23:55:29213 StopSuggest();
214 done_ = true;
[email protected]9e789742011-01-10 23:27:32215 default_provider_suggest_text_.clear();
initial.commit09911bf2008-07-26 23:55:29216}
217
initial.commit09911bf2008-07-26 23:55:29218void SearchProvider::OnURLFetchComplete(const URLFetcher* source,
219 const GURL& url,
[email protected]f90bf0d92011-01-13 02:12:44220 const net::URLRequestStatus& status,
initial.commit09911bf2008-07-26 23:55:29221 int response_code,
[email protected]cb04f5e2011-05-06 01:10:00222 const net::ResponseCookies& cookie,
initial.commit09911bf2008-07-26 23:55:29223 const std::string& data) {
224 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24225 suggest_results_pending_--;
[email protected]1cb2dac2010-03-08 21:49:15226 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06227 const net::HttpResponseHeaders* const response_headers =
228 source->response_headers();
229 std::string json_data(data);
[email protected]6c85aa02009-02-27 12:08:09230 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
231 // files in non-UTF-8 encodings. The actual encoding is usually specified in
232 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06233 if (response_headers) {
234 std::string charset;
235 if (response_headers->GetCharset(&charset)) {
[email protected]a2fedb1e2011-01-25 15:23:36236 string16 data_16;
[email protected]ec9207d32008-09-26 00:51:06237 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]a2fedb1e2011-01-25 15:23:36238 if (base::CodepageToUTF16(data, charset.c_str(),
239 base::OnStringConversionError::FAIL,
240 &data_16))
241 json_data = UTF16ToUTF8(data_16);
[email protected]ec9207d32008-09-26 00:51:06242 }
243 }
244
[email protected]257ab712009-04-14 17:16:24245 bool is_keyword_results = (source == keyword_fetcher_.get());
246 SuggestResults* suggest_results = is_keyword_results ?
247 &keyword_suggest_results_ : &default_suggest_results_;
248
[email protected]b4cebf82008-12-29 19:59:08249 if (status.is_success() && response_code == 200) {
250 JSONStringValueSerializer deserializer(json_data);
251 deserializer.set_allow_trailing_comma(true);
[email protected]ba399672010-04-06 15:42:39252 scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL));
[email protected]a2fedb1e2011-01-25 15:23:36253 const string16& input_text =
[email protected]257ab712009-04-14 17:16:24254 is_keyword_results ? keyword_input_text_ : input_.text();
[email protected]b4cebf82008-12-29 19:59:08255 have_suggest_results_ =
[email protected]257ab712009-04-14 17:16:24256 root_val.get() &&
257 ParseSuggestResults(root_val.get(), is_keyword_results, input_text,
258 suggest_results);
[email protected]b4cebf82008-12-29 19:59:08259 }
260
initial.commit09911bf2008-07-26 23:55:29261 ConvertResultsToAutocompleteMatches();
[email protected]257ab712009-04-14 17:16:24262 listener_->OnProviderUpdate(!suggest_results->empty());
initial.commit09911bf2008-07-26 23:55:29263}
264
[email protected]601858c02010-09-01 17:08:20265SearchProvider::~SearchProvider() {
266}
267
[email protected]8d457132010-11-04 18:13:40268void SearchProvider::DoHistoryQuery(bool minimal_changes) {
269 // The history query results are synchronous, so if minimal_changes is true,
270 // we still have the last results and don't need to do anything.
271 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29272 return;
273
[email protected]8d457132010-11-04 18:13:40274 keyword_history_results_.clear();
275 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29276
[email protected]8d457132010-11-04 18:13:40277 HistoryService* const history_service =
278 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
279 history::URLDatabase* url_db = history_service ?
280 history_service->InMemoryDatabase() : NULL;
281 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29282 return;
283
[email protected]257ab712009-04-14 17:16:24284 // Request history for both the keyword and default provider.
285 if (providers_.valid_keyword_provider()) {
[email protected]8d457132010-11-04 18:13:40286 url_db->GetMostRecentKeywordSearchTerms(
287 providers_.keyword_provider().id(),
[email protected]a2fedb1e2011-01-25 15:23:36288 keyword_input_text_,
[email protected]8d457132010-11-04 18:13:40289 static_cast<int>(kMaxMatches),
290 &keyword_history_results_);
[email protected]257ab712009-04-14 17:16:24291 }
292 if (providers_.valid_default_provider()) {
[email protected]8d457132010-11-04 18:13:40293 url_db->GetMostRecentKeywordSearchTerms(
294 providers_.default_provider().id(),
[email protected]a2fedb1e2011-01-25 15:23:36295 input_.text(),
[email protected]8d457132010-11-04 18:13:40296 static_cast<int>(kMaxMatches),
297 &default_history_results_);
[email protected]257ab712009-04-14 17:16:24298 }
initial.commit09911bf2008-07-26 23:55:29299}
300
[email protected]8deeb952008-10-09 18:21:27301void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]6c85aa02009-02-27 12:08:09302 // Don't send any queries to the server until some time has elapsed after
303 // the last keypress, to avoid flooding the server with requests we are
304 // likely to end up throwing away anyway.
305 static const int kQueryDelayMs = 200;
306
[email protected]83c726482008-09-10 06:36:34307 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29308 StopSuggest();
309 return;
310 }
311
312 // For the minimal_changes case, if we finished the previous query and still
313 // have its results, or are allowed to keep running it, just do that, rather
314 // than starting a new query.
315 if (minimal_changes &&
[email protected]ea3b9a502011-04-04 14:19:37316 (have_suggest_results_ ||
317 (!done_ &&
318 input_.matches_requested() == AutocompleteInput::ALL_MATCHES)))
initial.commit09911bf2008-07-26 23:55:29319 return;
320
321 // We can't keep running any previous query, so halt it.
322 StopSuggest();
323
324 // We can't start a new query if we're only allowed synchronous results.
[email protected]ea3b9a502011-04-04 14:19:37325 if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES)
initial.commit09911bf2008-07-26 23:55:29326 return;
327
[email protected]257ab712009-04-14 17:16:24328 // We'll have at least one pending fetch. Set it to 1 now, but the value is
329 // correctly set in Run. As Run isn't invoked immediately we need to set this
330 // now, else we won't think we're waiting on results from the server when we
331 // really are.
332 suggest_results_pending_ = 1;
333
initial.commit09911bf2008-07-26 23:55:29334 // Kick off a timer that will start the URL fetch if it completes before
335 // the user types another character.
[email protected]b547666d2009-04-23 16:37:58336 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs;
337 timer_.Start(TimeDelta::FromMilliseconds(delay), this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29338}
339
[email protected]83c726482008-09-10 06:36:34340bool SearchProvider::IsQuerySuitableForSuggest() const {
[email protected]2c910b72011-03-08 21:16:32341 // Don't run Suggest in incognito mode, the engine doesn't support it, or
[email protected]83c726482008-09-10 06:36:34342 // the user has disabled it.
343 if (profile_->IsOffTheRecord() ||
[email protected]257ab712009-04-14 17:16:24344 (!providers_.valid_suggest_for_keyword_provider() &&
345 !providers_.valid_suggest_for_default_provider()) ||
[email protected]83c726482008-09-10 06:36:34346 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
347 return false;
348
[email protected]cac59d32010-08-09 23:23:14349 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34350 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34351
[email protected]cac59d32010-08-09 23:23:14352 // FORCED_QUERY means the user is explicitly asking us to search for this, so
353 // we assume it isn't a URL and/or there isn't private data.
354 if (input_.type() == AutocompleteInput::FORCED_QUERY)
355 return true;
[email protected]83c726482008-09-10 06:36:34356
[email protected]cac59d32010-08-09 23:23:14357 // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a
358 // scheme that isn't http/https/ftp, we shouldn't send it. Sending things
359 // like file: and data: is both a waste of time and a disclosure of
360 // potentially private, local data. Other "schemes" may actually be
361 // usernames, and we don't want to send passwords. If the scheme is OK, we
362 // still need to check other cases below. If this is QUERY, then the presence
363 // of these schemes means the user explicitly typed one, and thus this is
364 // probably a URL that's being entered and happens to currently be invalid --
365 // in which case we again want to run our checks below. Other QUERY cases are
366 // less likely to be URLs and thus we assume we're OK.
[email protected]a2fedb1e2011-01-25 15:23:36367 if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) &&
368 !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
369 !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme))
[email protected]cac59d32010-08-09 23:23:14370 return (input_.type() == AutocompleteInput::QUERY);
371
372 // Don't send URLs with usernames, queries or refs. Some of these are
373 // private, and the Suggest server is unlikely to have any useful results
374 // for any of them. Also don't send URLs with ports, as we may initially
375 // think that a username + password is a host + port (and we don't want to
376 // send usernames/passwords), and even if the port really is a port, the
377 // server is once again unlikely to have and useful results.
378 const url_parse::Parsed& parts = input_.parts();
379 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
380 parts.query.is_nonempty() || parts.ref.is_nonempty())
381 return false;
382
383 // Don't send anything for https except the hostname. Hostnames are OK
384 // because they are visible when the TCP connection is established, but the
385 // specific path may reveal private information.
[email protected]a2fedb1e2011-01-25 15:23:36386 if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) &&
387 parts.path.is_nonempty())
[email protected]cac59d32010-08-09 23:23:14388 return false;
[email protected]83c726482008-09-10 06:36:34389
390 return true;
391}
392
initial.commit09911bf2008-07-26 23:55:29393void SearchProvider::StopSuggest() {
[email protected]257ab712009-04-14 17:16:24394 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14395 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24396 // Stop any in-progress URL fetches.
397 keyword_fetcher_.reset();
398 default_fetcher_.reset();
399 keyword_suggest_results_.clear();
400 default_suggest_results_.clear();
401 keyword_navigation_results_.clear();
402 default_navigation_results_.clear();
initial.commit09911bf2008-07-26 23:55:29403 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29404}
405
[email protected]b547666d2009-04-23 16:37:58406URLFetcher* SearchProvider::CreateSuggestFetcher(int id,
407 const TemplateURL& provider,
[email protected]a2fedb1e2011-01-25 15:23:36408 const string16& text) {
[email protected]257ab712009-04-14 17:16:24409 const TemplateURLRef* const suggestions_url = provider.suggestions_url();
410 DCHECK(suggestions_url->SupportsReplacement());
[email protected]b547666d2009-04-23 16:37:58411 URLFetcher* fetcher = URLFetcher::Create(id,
[email protected]ddd231e2010-06-29 20:35:19412 GURL(suggestions_url->ReplaceSearchTerms(
[email protected]a2fedb1e2011-01-25 15:23:36413 provider, text,
[email protected]400b133f2011-01-19 18:32:30414 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())),
[email protected]257ab712009-04-14 17:16:24415 URLFetcher::GET, this);
416 fetcher->set_request_context(profile_->GetRequestContext());
417 fetcher->Start();
418 return fetcher;
419}
420
421bool SearchProvider::ParseSuggestResults(Value* root_val,
422 bool is_keyword,
[email protected]a2fedb1e2011-01-25 15:23:36423 const string16& input_text,
[email protected]257ab712009-04-14 17:16:24424 SuggestResults* suggest_results) {
initial.commit09911bf2008-07-26 23:55:29425 if (!root_val->IsType(Value::TYPE_LIST))
426 return false;
427 ListValue* root_list = static_cast<ListValue*>(root_val);
428
429 Value* query_val;
[email protected]dc9a6762010-08-16 07:13:53430 string16 query_str;
initial.commit09911bf2008-07-26 23:55:29431 Value* result_val;
432 if ((root_list->GetSize() < 2) || !root_list->Get(0, &query_val) ||
[email protected]dc9a6762010-08-16 07:13:53433 !query_val->GetAsString(&query_str) ||
[email protected]a2fedb1e2011-01-25 15:23:36434 (query_str != input_text) ||
initial.commit09911bf2008-07-26 23:55:29435 !root_list->Get(1, &result_val) || !result_val->IsType(Value::TYPE_LIST))
436 return false;
437
438 ListValue* description_list = NULL;
439 if (root_list->GetSize() > 2) {
440 // 3rd element: Description list.
441 Value* description_val;
442 if (root_list->Get(2, &description_val) &&
443 description_val->IsType(Value::TYPE_LIST))
444 description_list = static_cast<ListValue*>(description_val);
445 }
446
447 // We don't care about the query URL list (the fourth element in the
448 // response) for now.
449
450 // Parse optional data in the results from the Suggest server if any.
451 ListValue* type_list = NULL;
452 // 5th argument: Optional key-value pairs.
453 // TODO: We may iterate the 5th+ arguments of the root_list if any other
454 // optional data are defined.
455 if (root_list->GetSize() > 4) {
456 Value* optional_val;
457 if (root_list->Get(4, &optional_val) &&
458 optional_val->IsType(Value::TYPE_DICTIONARY)) {
459 DictionaryValue* dict_val = static_cast<DictionaryValue*>(optional_val);
460
461 // Parse Google Suggest specific type extension.
[email protected]a65175d2010-08-17 04:00:57462 static const std::string kGoogleSuggestType("google:suggesttype");
initial.commit09911bf2008-07-26 23:55:29463 if (dict_val->HasKey(kGoogleSuggestType))
464 dict_val->GetList(kGoogleSuggestType, &type_list);
465 }
466 }
467
468 ListValue* result_list = static_cast<ListValue*>(result_val);
469 for (size_t i = 0; i < result_list->GetSize(); ++i) {
470 Value* suggestion_val;
[email protected]dc9a6762010-08-16 07:13:53471 string16 suggestion_str;
initial.commit09911bf2008-07-26 23:55:29472 if (!result_list->Get(i, &suggestion_val) ||
473 !suggestion_val->GetAsString(&suggestion_str))
474 return false;
475
[email protected]8e81f5092010-09-29 23:19:40476 // Google search may return empty suggestions for weird input characters,
477 // they make no sense at all and can cause problem in our code.
478 // See http://crbug.com/56214
479 if (!suggestion_str.length())
480 continue;
481
initial.commit09911bf2008-07-26 23:55:29482 Value* type_val;
[email protected]dc9a6762010-08-16 07:13:53483 std::string type_str;
initial.commit09911bf2008-07-26 23:55:29484 if (type_list && type_list->Get(i, &type_val) &&
[email protected]dc9a6762010-08-16 07:13:53485 type_val->GetAsString(&type_str) && (type_str == "NAVIGATION")) {
initial.commit09911bf2008-07-26 23:55:29486 Value* site_val;
[email protected]dc9a6762010-08-16 07:13:53487 string16 site_name;
[email protected]257ab712009-04-14 17:16:24488 NavigationResults& navigation_results =
489 is_keyword ? keyword_navigation_results_ :
490 default_navigation_results_;
[email protected]0be9b612010-05-18 01:13:41491 if ((navigation_results.size() < kMaxMatches) &&
initial.commit09911bf2008-07-26 23:55:29492 description_list && description_list->Get(i, &site_val) &&
493 site_val->IsType(Value::TYPE_STRING) &&
494 site_val->GetAsString(&site_name)) {
[email protected]16afe222009-01-08 18:57:45495 // We can't blindly trust the URL coming from the server to be valid.
[email protected]dc9a6762010-08-16 07:13:53496 GURL result_url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion_str),
[email protected]76e7da22010-06-18 22:44:49497 std::string()));
[email protected]dc9a6762010-08-16 07:13:53498 if (result_url.is_valid()) {
[email protected]a2fedb1e2011-01-25 15:23:36499 navigation_results.push_back(NavigationResult(result_url, site_name));
[email protected]dc9a6762010-08-16 07:13:53500 }
initial.commit09911bf2008-07-26 23:55:29501 }
502 } else {
503 // TODO(kochi): Currently we treat a calculator result as a query, but it
504 // is better to have better presentation for caluculator results.
[email protected]0be9b612010-05-18 01:13:41505 if (suggest_results->size() < kMaxMatches)
[email protected]a2fedb1e2011-01-25 15:23:36506 suggest_results->push_back(suggestion_str);
initial.commit09911bf2008-07-26 23:55:29507 }
508 }
509
initial.commit09911bf2008-07-26 23:55:29510 return true;
511}
512
513void SearchProvider::ConvertResultsToAutocompleteMatches() {
514 // Convert all the results to matches and add them to a map, so we can keep
515 // the most relevant match for each result.
516 MatchMap map;
[email protected]257ab712009-04-14 17:16:24517 const Time no_time;
518 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ?
initial.commit09911bf2008-07-26 23:55:29519 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
520 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]257ab712009-04-14 17:16:24521 // Keyword what you typed results are handled by the KeywordProvider.
initial.commit09911bf2008-07-26 23:55:29522
[email protected]257ab712009-04-14 17:16:24523 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
524 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
525 TemplateURLRef::NO_SUGGESTION_CHOSEN;
526 if (providers_.valid_default_provider()) {
[email protected]e918c112010-12-08 23:03:49527 AddMatchToMap(input_.text(), input_.text(),
528 CalculateRelevanceForWhatYouTyped(),
[email protected]257ab712009-04-14 17:16:24529 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
[email protected]e918c112010-12-08 23:03:49530 did_not_accept_default_suggestion, false,
[email protected]e0b231d2011-05-09 21:26:42531 input_.prevent_inline_autocomplete(), &map);
[email protected]9e789742011-01-10 23:27:32532 if (!default_provider_suggest_text_.empty()) {
533 AddMatchToMap(input_.text() + default_provider_suggest_text_,
534 input_.text(), CalculateRelevanceForWhatYouTyped() + 1,
535 AutocompleteMatch::SEARCH_SUGGEST,
536 did_not_accept_default_suggestion, false,
[email protected]e0b231d2011-05-09 21:26:42537 input_.prevent_inline_autocomplete(), &map);
[email protected]9e789742011-01-10 23:27:32538 }
initial.commit09911bf2008-07-26 23:55:29539 }
540
[email protected]257ab712009-04-14 17:16:24541 AddHistoryResultsToMap(keyword_history_results_, true,
542 did_not_accept_keyword_suggestion, &map);
543 AddHistoryResultsToMap(default_history_results_, false,
544 did_not_accept_default_suggestion, &map);
545
546 AddSuggestResultsToMap(keyword_suggest_results_, true,
547 did_not_accept_keyword_suggestion, &map);
548 AddSuggestResultsToMap(default_suggest_results_, false,
549 did_not_accept_default_suggestion, &map);
initial.commit09911bf2008-07-26 23:55:29550
551 // Now add the most relevant matches from the map to |matches_|.
552 matches_.clear();
553 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
554 matches_.push_back(i->second);
555
[email protected]257ab712009-04-14 17:16:24556 AddNavigationResultsToMatches(keyword_navigation_results_, true);
557 AddNavigationResultsToMatches(default_navigation_results_, false);
initial.commit09911bf2008-07-26 23:55:29558
[email protected]0be9b612010-05-18 01:13:41559 const size_t max_total_matches = kMaxMatches + 1; // 1 for "what you typed"
initial.commit09911bf2008-07-26 23:55:29560 std::partial_sort(matches_.begin(),
561 matches_.begin() + std::min(max_total_matches, matches_.size()),
562 matches_.end(), &AutocompleteMatch::MoreRelevant);
563 if (matches_.size() > max_total_matches)
[email protected]a28e95662008-11-12 19:19:02564 matches_.erase(matches_.begin() + max_total_matches, matches_.end());
initial.commit09911bf2008-07-26 23:55:29565
[email protected]cc63dea2008-08-21 20:56:31566 UpdateStarredStateOfMatches();
567
[email protected]4ab4c7c2010-11-24 04:49:34568 UpdateDone();
[email protected]257ab712009-04-14 17:16:24569}
570
571void SearchProvider::AddNavigationResultsToMatches(
572 const NavigationResults& navigation_results,
573 bool is_keyword) {
574 if (!navigation_results.empty()) {
575 // TODO(kochi): http://b/1170574 We add only one results for navigational
576 // suggestions. If we can get more useful information about the score,
577 // consider adding more results.
[email protected]52d08b12009-10-19 18:42:36578 const size_t num_results = is_keyword ?
579 keyword_navigation_results_.size() : default_navigation_results_.size();
580 matches_.push_back(NavigationToMatch(navigation_results.front(),
581 CalculateRelevanceForNavigation(num_results, 0, is_keyword),
582 is_keyword));
[email protected]257ab712009-04-14 17:16:24583 }
584}
585
586void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
587 bool is_keyword,
588 int did_not_accept_suggestion,
589 MatchMap* map) {
[email protected]ab49320e2011-01-06 22:45:15590 int last_relevance = 0;
[email protected]ea3b9a502011-04-04 14:19:37591 AutocompleteClassifier* classifier = profile_->GetAutocompleteClassifier();
[email protected]257ab712009-04-14 17:16:24592 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
593 ++i) {
[email protected]ab49320e2011-01-06 22:45:15594 // History returns results sorted for us. We force the relevance to decrease
595 // so that the sort from history is honored. We should never end up with a
596 // match having a relevance greater than the previous, but they might be
597 // equal. If we didn't force the relevance to decrease and we ended up in a
598 // situation where the relevance was equal, then which was shown first would
599 // be random.
600 // This uses >= to handle the case where 3 or more results have the same
601 // relevance.
[email protected]ea3b9a502011-04-04 14:19:37602 bool term_looks_like_url = false;
603 // Don't autocomplete search terms that would normally be treated as URLs
604 // when typed. For example, if the user searched for google.com and types
605 // goog, don't autocomplete to the search term google.com. Otherwise, the
606 // input will look like a URL but act like a search, which is confusing.
[email protected]cc447362011-04-06 03:57:48607 // NOTE: We don't check this in the following cases:
608 // * When inline autocomplete is disabled, we won't be inline
609 // autocompleting this term, so we don't need to worry about confusion as
610 // much. This also prevents calling Classify() again from inside the
611 // classifier (which will corrupt state and likely crash), since the
612 // classifier always disabled inline autocomplete.
613 // * When the user has typed the whole term, the "what you typed" history
614 // match will outrank us for URL-like inputs anyway, so we need not do
615 // anything special.
616 if (!input_.prevent_inline_autocomplete() && classifier &&
617 i->term != input_.text()) {
[email protected]ea3b9a502011-04-04 14:19:37618 AutocompleteMatch match;
[email protected]72874a8d2011-05-11 03:48:54619 classifier->Classify(i->term, string16(), false, false, &match, NULL);
[email protected]ea3b9a502011-04-04 14:19:37620 term_looks_like_url = match.transition == PageTransition::TYPED;
621 }
622 int relevance = CalculateRelevanceForHistory(i->time, term_looks_like_url,
623 is_keyword);
[email protected]ab49320e2011-01-06 22:45:15624 if (i != results.begin() && relevance >= last_relevance)
625 relevance = last_relevance - 1;
626 last_relevance = relevance;
[email protected]a2fedb1e2011-01-25 15:23:36627 AddMatchToMap(i->term,
[email protected]e918c112010-12-08 23:03:49628 is_keyword ? keyword_input_text_ : input_.text(),
[email protected]ab49320e2011-01-06 22:45:15629 relevance,
[email protected]257ab712009-04-14 17:16:24630 AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion,
[email protected]e0b231d2011-05-09 21:26:42631 is_keyword, input_.prevent_inline_autocomplete(),
[email protected]e918c112010-12-08 23:03:49632 map);
[email protected]257ab712009-04-14 17:16:24633 }
634}
635
636void SearchProvider::AddSuggestResultsToMap(
637 const SuggestResults& suggest_results,
638 bool is_keyword,
639 int did_not_accept_suggestion,
640 MatchMap* map) {
641 for (size_t i = 0; i < suggest_results.size(); ++i) {
642 AddMatchToMap(suggest_results[i],
[email protected]e918c112010-12-08 23:03:49643 is_keyword ? keyword_input_text_ : input_.text(),
[email protected]52d08b12009-10-19 18:42:36644 CalculateRelevanceForSuggestion(suggest_results.size(), i,
[email protected]257ab712009-04-14 17:16:24645 is_keyword),
646 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]e918c112010-12-08 23:03:49647 static_cast<int>(i), is_keyword,
[email protected]e0b231d2011-05-09 21:26:42648 input_.prevent_inline_autocomplete(), map);
[email protected]257ab712009-04-14 17:16:24649 }
initial.commit09911bf2008-07-26 23:55:29650}
651
652int SearchProvider::CalculateRelevanceForWhatYouTyped() const {
[email protected]52d08b12009-10-19 18:42:36653 if (providers_.valid_keyword_provider())
654 return 250;
655
initial.commit09911bf2008-07-26 23:55:29656 switch (input_.type()) {
657 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:36658 case AutocompleteInput::QUERY:
659 case AutocompleteInput::FORCED_QUERY:
660 return 1300;
initial.commit09911bf2008-07-26 23:55:29661
662 case AutocompleteInput::REQUESTED_URL:
[email protected]52d08b12009-10-19 18:42:36663 return 1150;
initial.commit09911bf2008-07-26 23:55:29664
665 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:36666 return 850;
initial.commit09911bf2008-07-26 23:55:29667
668 default:
669 NOTREACHED();
670 return 0;
671 }
672}
673
[email protected]257ab712009-04-14 17:16:24674int SearchProvider::CalculateRelevanceForHistory(const Time& time,
[email protected]ea3b9a502011-04-04 14:19:37675 bool looks_like_url,
[email protected]257ab712009-04-14 17:16:24676 bool is_keyword) const {
[email protected]aa613d62010-11-09 20:40:18677 // The relevance of past searches falls off over time. There are two distinct
678 // equations used. If the first equation is used (searches to the primary
[email protected]ea3b9a502011-04-04 14:19:37679 // provider with a type other than URL that don't autocomplete to a url) the
680 // score starts at 1399 and falls to 1300. If the second equation is used the
681 // relevance of a search 15 minutes ago is discounted about 50 points, while
682 // the relevance of a search two weeks ago is discounted about 450 points.
[email protected]aa613d62010-11-09 20:40:18683 double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.);
684
685 if (providers_.is_primary_provider(is_keyword) &&
[email protected]bdca9beb2010-11-15 22:56:19686 input_.type() != AutocompleteInput::URL &&
[email protected]ea3b9a502011-04-04 14:19:37687 !input_.prevent_inline_autocomplete() && !looks_like_url) {
[email protected]aa613d62010-11-09 20:40:18688 // Searches with the past two days get a different curve.
689 const double autocomplete_time= 2 * 24 * 60 * 60;
690 if (elapsed_time < autocomplete_time) {
[email protected]e17511f2011-07-13 14:09:18691 return (is_keyword ? 1599 : 1399) - static_cast<int>(99 *
[email protected]aa613d62010-11-09 20:40:18692 std::pow(elapsed_time / autocomplete_time, 2.5));
693 }
694 elapsed_time -= autocomplete_time;
695 }
696
[email protected]c3a4bd992010-08-18 20:25:01697 const int score_discount =
698 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:29699
[email protected]6c85aa02009-02-27 12:08:09700 // Don't let scores go below 0. Negative relevance scores are meaningful in
701 // a different way.
initial.commit09911bf2008-07-26 23:55:29702 int base_score;
[email protected]52d08b12009-10-19 18:42:36703 if (!providers_.is_primary_provider(is_keyword))
704 base_score = 200;
705 else
706 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
initial.commit09911bf2008-07-26 23:55:29707 return std::max(0, base_score - score_discount);
708}
709
[email protected]52d08b12009-10-19 18:42:36710int SearchProvider::CalculateRelevanceForSuggestion(size_t num_results,
711 size_t result_number,
712 bool is_keyword) const {
713 DCHECK(result_number < num_results);
714 int base_score;
715 if (!providers_.is_primary_provider(is_keyword))
716 base_score = 100;
717 else
718 base_score = (input_.type() == AutocompleteInput::URL) ? 300 : 600;
719 return base_score +
720 static_cast<int>(num_results - 1 - result_number);
initial.commit09911bf2008-07-26 23:55:29721}
722
[email protected]52d08b12009-10-19 18:42:36723int SearchProvider::CalculateRelevanceForNavigation(size_t num_results,
724 size_t result_number,
725 bool is_keyword) const {
726 DCHECK(result_number < num_results);
initial.commit09911bf2008-07-26 23:55:29727 // TODO(kochi): http://b/784900 Use relevance score from the NavSuggest
728 // server if possible.
[email protected]52d08b12009-10-19 18:42:36729 return (providers_.is_primary_provider(is_keyword) ? 800 : 150) +
730 static_cast<int>(num_results - 1 - result_number);
initial.commit09911bf2008-07-26 23:55:29731}
732
[email protected]a2fedb1e2011-01-25 15:23:36733void SearchProvider::AddMatchToMap(const string16& query_string,
734 const string16& input_text,
initial.commit09911bf2008-07-26 23:55:29735 int relevance,
[email protected]4c1fb7ec2008-11-13 00:19:00736 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:29737 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:24738 bool is_keyword,
[email protected]e918c112010-12-08 23:03:49739 bool prevent_inline_autocomplete,
initial.commit09911bf2008-07-26 23:55:29740 MatchMap* map) {
[email protected]4c1fb7ec2008-11-13 00:19:00741 AutocompleteMatch match(this, relevance, false, type);
initial.commit09911bf2008-07-26 23:55:29742 std::vector<size_t> content_param_offsets;
[email protected]257ab712009-04-14 17:16:24743 const TemplateURL& provider = is_keyword ? providers_.keyword_provider() :
744 providers_.default_provider();
[email protected]0385fc682011-07-07 19:36:17745 match.template_url = &provider;
[email protected]70833262011-01-05 23:40:44746 match.contents.assign(query_string);
[email protected]fb5153c52009-07-31 19:40:33747 // We do intra-string highlighting for suggestions - the suggested segment
748 // will be highlighted, e.g. for input_text = "you" the suggestion may be
749 // "youtube", so we'll bold the "tube" section: you*tube*.
750 if (input_text != query_string) {
[email protected]fb5153c52009-07-31 19:40:33751 size_t input_position = match.contents.find(input_text);
[email protected]a2fedb1e2011-01-25 15:23:36752 if (input_position == string16::npos) {
[email protected]fb5153c52009-07-31 19:40:33753 // The input text is not a substring of the query string, e.g. input
754 // text is "slasdot" and the query string is "slashdot", so we bold the
755 // whole thing.
756 match.contents_class.push_back(
757 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]ec2379162009-06-09 23:58:17758 } else {
[email protected]fb5153c52009-07-31 19:40:33759 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
760 // "bold" this. Consider modifying the terminology.
761 // We don't iterate over the string here annotating all matches because
762 // it looks odd to have every occurrence of a substring that may be as
763 // short as a single character highlighted in a query suggestion result,
764 // e.g. for input text "s" and query string "southwest airlines", it
765 // looks odd if both the first and last s are highlighted.
766 if (input_position != 0) {
767 match.contents_class.push_back(
768 ACMatchClassification(0, ACMatchClassification::NONE));
769 }
770 match.contents_class.push_back(
771 ACMatchClassification(input_position, ACMatchClassification::DIM));
772 size_t next_fragment_position = input_position + input_text.length();
773 if (next_fragment_position < query_string.length()) {
774 match.contents_class.push_back(
775 ACMatchClassification(next_fragment_position,
776 ACMatchClassification::NONE));
777 }
[email protected]ec2379162009-06-09 23:58:17778 }
initial.commit09911bf2008-07-26 23:55:29779 } else {
[email protected]fb5153c52009-07-31 19:40:33780 // Otherwise, we're dealing with the "default search" result which has no
[email protected]70833262011-01-05 23:40:44781 // completion.
[email protected]fb5153c52009-07-31 19:40:33782 match.contents_class.push_back(
783 ACMatchClassification(0, ACMatchClassification::NONE));
initial.commit09911bf2008-07-26 23:55:29784 }
785
786 // When the user forced a query, we need to make sure all the fill_into_edit
787 // values preserve that property. Otherwise, if the user starts editing a
788 // suggestion, non-Search results will suddenly appear.
789 size_t search_start = 0;
790 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
[email protected]a2fedb1e2011-01-25 15:23:36791 match.fill_into_edit.assign(ASCIIToUTF16("?"));
initial.commit09911bf2008-07-26 23:55:29792 ++search_start;
793 }
[email protected]c0048b42009-05-04 21:47:17794 if (is_keyword) {
[email protected]a2fedb1e2011-01-25 15:23:36795 match.fill_into_edit.append(
796 providers_.keyword_provider().keyword() + char16(' '));
[email protected]e17511f2011-07-13 14:09:18797 search_start += providers_.keyword_provider().keyword().size() + 1;
[email protected]c0048b42009-05-04 21:47:17798 }
initial.commit09911bf2008-07-26 23:55:29799 match.fill_into_edit.append(query_string);
[email protected]2c33dd22010-02-11 21:46:35800 // Not all suggestions start with the original input.
[email protected]e918c112010-12-08 23:03:49801 if (!prevent_inline_autocomplete &&
[email protected]257ab712009-04-14 17:16:24802 !match.fill_into_edit.compare(search_start, input_text.length(),
803 input_text))
804 match.inline_autocomplete_offset = search_start + input_text.length();
initial.commit09911bf2008-07-26 23:55:29805
[email protected]257ab712009-04-14 17:16:24806 const TemplateURLRef* const search_url = provider.url();
initial.commit09911bf2008-07-26 23:55:29807 DCHECK(search_url->SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:22808 match.destination_url =
[email protected]ddd231e2010-06-29 20:35:19809 GURL(search_url->ReplaceSearchTerms(provider,
[email protected]a2fedb1e2011-01-25 15:23:36810 query_string,
[email protected]ddd231e2010-06-29 20:35:19811 accepted_suggestion,
[email protected]a2fedb1e2011-01-25 15:23:36812 input_text));
initial.commit09911bf2008-07-26 23:55:29813
814 // Search results don't look like URLs.
[email protected]0bfc29a2009-04-27 16:15:44815 match.transition =
816 is_keyword ? PageTransition::KEYWORD : PageTransition::GENERATED;
initial.commit09911bf2008-07-26 23:55:29817
818 // Try to add |match| to |map|. If a match for |query_string| is already in
819 // |map|, replace it if |match| is more relevant.
820 // NOTE: Keep this ToLower() call in sync with url_database.cc.
821 const std::pair<MatchMap::iterator, bool> i = map->insert(
[email protected]a2fedb1e2011-01-25 15:23:36822 std::pair<string16, AutocompleteMatch>(
[email protected]503d03872011-05-06 08:36:26823 base::i18n::ToLower(query_string), match));
initial.commit09911bf2008-07-26 23:55:29824 // NOTE: We purposefully do a direct relevance comparison here instead of
825 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
826 // first" rather than "items alphabetically first" when the scores are equal.
827 // The only case this matters is when a user has results with the same score
828 // that differ only by capitalization; because the history system returns
829 // results sorted by recency, this means we'll pick the most recent such
830 // result even if the precision of our relevance score is too low to
831 // distinguish the two.
832 if (!i.second && (match.relevance > i.first->second.relevance))
833 i.first->second = match;
834}
835
836AutocompleteMatch SearchProvider::NavigationToMatch(
837 const NavigationResult& navigation,
[email protected]257ab712009-04-14 17:16:24838 int relevance,
839 bool is_keyword) {
[email protected]a2fedb1e2011-01-25 15:23:36840 const string16& input_text =
[email protected]257ab712009-04-14 17:16:24841 is_keyword ? keyword_input_text_ : input_.text();
[email protected]4c1fb7ec2008-11-13 00:19:00842 AutocompleteMatch match(this, relevance, false,
843 AutocompleteMatch::NAVSUGGEST);
initial.commit09911bf2008-07-26 23:55:29844 match.destination_url = navigation.url;
[email protected]76e7da22010-06-18 22:44:49845 match.contents =
846 StringForURLDisplay(navigation.url, true, !HasHTTPScheme(input_text));
[email protected]0385fc682011-07-07 19:36:17847 match.template_url = is_keyword ? &providers_.keyword_provider() :
848 &providers_.default_provider();
[email protected]257ab712009-04-14 17:16:24849 AutocompleteMatch::ClassifyMatchInString(input_text, match.contents,
initial.commit09911bf2008-07-26 23:55:29850 ACMatchClassification::URL,
851 &match.contents_class);
852
853 match.description = navigation.site_name;
[email protected]257ab712009-04-14 17:16:24854 AutocompleteMatch::ClassifyMatchInString(input_text, navigation.site_name,
initial.commit09911bf2008-07-26 23:55:29855 ACMatchClassification::NONE,
856 &match.description_class);
857
initial.commit09911bf2008-07-26 23:55:29858 // When the user forced a query, we need to make sure all the fill_into_edit
859 // values preserve that property. Otherwise, if the user starts editing a
860 // suggestion, non-Search results will suddenly appear.
861 if (input_.type() == AutocompleteInput::FORCED_QUERY)
[email protected]a2fedb1e2011-01-25 15:23:36862 match.fill_into_edit.assign(ASCIIToUTF16("?"));
[email protected]79845ef2010-06-02 02:37:40863 match.fill_into_edit.append(
864 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url,
865 match.contents));
initial.commit09911bf2008-07-26 23:55:29866 // TODO(pkasting): http://b/1112879 These should perhaps be
867 // inline-autocompletable?
868
869 return match;
870}
[email protected]4ab4c7c2010-11-24 04:49:34871
872void SearchProvider::UpdateDone() {
873 // We're done when there are no more suggest queries pending (this is set to 1
874 // when the timer is started) and we're not waiting on instant.
875 done_ = ((suggest_results_pending_ == 0) &&
876 (instant_finalized_ || !InstantController::IsEnabled(profile_)));
877}