blob: 2c67f7dc2be13b56636d369fc1fe551932205fb6 [file] [log] [blame]
[email protected]2c33dd22010-02-11 21:46:351// Copyright (c) 2010 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]a92b8642009-05-05 23:38:5610#include "app/l10n_util.h"
[email protected]2041cf342010-02-19 03:15:5911#include "base/callback.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]257ab712009-04-14 17:16:2416#include "chrome/browser/autocomplete/keyword_provider.h"
initial.commit09911bf2008-07-26 23:55:2917#include "chrome/browser/browser_process.h"
[email protected]f7578f52010-08-30 22:22:4918#include "chrome/browser/google/google_util.h"
[email protected]ce560f82009-06-03 09:39:4419#include "chrome/browser/history/history.h"
[email protected]f870a322009-01-16 21:47:2720#include "chrome/browser/net/url_fixer_upper.h"
[email protected]37858e52010-08-26 00:22:0221#include "chrome/browser/prefs/pref_service.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/profile.h"
[email protected]d54e03a52009-01-16 00:31:0423#include "chrome/browser/search_engines/template_url_model.h"
initial.commit09911bf2008-07-26 23:55:2924#include "chrome/common/json_value_serializer.h"
initial.commit09911bf2008-07-26 23:55:2925#include "chrome/common/pref_names.h"
[email protected]dcf7d352009-02-26 01:56:0226#include "chrome/common/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2927#include "googleurl/src/url_util.h"
[email protected]34ac8f32009-02-22 23:03:2728#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2929#include "net/base/escape.h"
[email protected]319d9e6f2009-02-18 19:47:2130#include "net/http/http_response_headers.h"
31#include "net/url_request/url_request_status.h"
initial.commit09911bf2008-07-26 23:55:2932
[email protected]e1acf6f2008-10-27 20:43:3333using base::Time;
34using base::TimeDelta;
35
[email protected]b547666d2009-04-23 16:37:5836// static
37const int SearchProvider::kDefaultProviderURLFetcherID = 1;
38// static
39const int SearchProvider::kKeywordProviderURLFetcherID = 2;
40
41// static
42bool SearchProvider::query_suggest_immediately_ = false;
43
[email protected]257ab712009-04-14 17:16:2444void SearchProvider::Providers::Set(const TemplateURL* default_provider,
45 const TemplateURL* keyword_provider) {
46 // TODO(pkasting): http://b/1162970 We shouldn't need to structure-copy
47 // this. Nor should we need |default_provider_| and |keyword_provider_|
48 // just to know whether the provider changed.
49 default_provider_ = default_provider;
50 if (default_provider)
51 cached_default_provider_ = *default_provider;
52 keyword_provider_ = keyword_provider;
53 if (keyword_provider)
54 cached_keyword_provider_ = *keyword_provider;
55}
56
initial.commit09911bf2008-07-26 23:55:2957void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:2758 bool minimal_changes) {
initial.commit09911bf2008-07-26 23:55:2959 matches_.clear();
60
[email protected]6c85aa02009-02-27 12:08:0961 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:2962 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
63 Stop();
64 return;
65 }
66
[email protected]257ab712009-04-14 17:16:2467 keyword_input_text_.clear();
68 const TemplateURL* keyword_provider =
69 KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input,
70 &keyword_input_text_);
71 if (!TemplateURL::SupportsReplacement(keyword_provider) ||
72 keyword_input_text_.empty()) {
73 keyword_provider = NULL;
74 }
75
76 const TemplateURL* default_provider =
initial.commit09911bf2008-07-26 23:55:2977 profile_->GetTemplateURLModel()->GetDefaultSearchProvider();
[email protected]257ab712009-04-14 17:16:2478 if (!TemplateURL::SupportsReplacement(default_provider))
79 default_provider = NULL;
80
81 if (keyword_provider == default_provider)
82 keyword_provider = NULL; // No use in querying the same provider twice.
83
84 if (!default_provider && !keyword_provider) {
85 // No valid providers.
initial.commit09911bf2008-07-26 23:55:2986 Stop();
87 return;
88 }
89
90 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:2491 // or the providers, abort the query.
initial.commit09911bf2008-07-26 23:55:2992 if (!done_ && (!minimal_changes ||
[email protected]257ab712009-04-14 17:16:2493 !providers_.equals(default_provider, keyword_provider))) {
initial.commit09911bf2008-07-26 23:55:2994 Stop();
[email protected]257ab712009-04-14 17:16:2495 }
initial.commit09911bf2008-07-26 23:55:2996
[email protected]257ab712009-04-14 17:16:2497 providers_.Set(default_provider, keyword_provider);
initial.commit09911bf2008-07-26 23:55:2998
99 if (input.text().empty()) {
100 // User typed "?" alone. Give them a placeholder result indicating what
101 // this syntax does.
[email protected]257ab712009-04-14 17:16:24102 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00103 AutocompleteMatch match;
104 match.provider = this;
[email protected]2c33dd22010-02-11 21:46:35105 match.contents.assign(l10n_util::GetString(IDS_EMPTY_KEYWORD_VALUE));
[email protected]257ab712009-04-14 17:16:24106 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35107 ACMatchClassification(0, ACMatchClassification::NONE));
108 match.description.assign(l10n_util::GetStringF(
109 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION,
110 default_provider->AdjustedShortNameForLocaleDirection()));
111 match.description_class.push_back(
[email protected]257ab712009-04-14 17:16:24112 ACMatchClassification(0, ACMatchClassification::DIM));
113 matches_.push_back(match);
114 }
initial.commit09911bf2008-07-26 23:55:29115 Stop();
116 return;
117 }
118
119 input_ = input;
120
[email protected]8deeb952008-10-09 18:21:27121 StartOrStopHistoryQuery(minimal_changes);
122 StartOrStopSuggestQuery(minimal_changes);
initial.commit09911bf2008-07-26 23:55:29123 ConvertResultsToAutocompleteMatches();
124}
125
126void SearchProvider::Run() {
127 // Start a new request with the current input.
128 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24129 suggest_results_pending_ = 0;
130 if (providers_.valid_suggest_for_keyword_provider()) {
131 suggest_results_pending_++;
132 keyword_fetcher_.reset(
[email protected]b547666d2009-04-23 16:37:58133 CreateSuggestFetcher(kKeywordProviderURLFetcherID,
134 providers_.keyword_provider(),
[email protected]257ab712009-04-14 17:16:24135 keyword_input_text_));
136 }
137 if (providers_.valid_suggest_for_default_provider()) {
138 suggest_results_pending_++;
139 default_fetcher_.reset(
[email protected]b547666d2009-04-23 16:37:58140 CreateSuggestFetcher(kDefaultProviderURLFetcherID,
141 providers_.default_provider(), input_.text()));
[email protected]257ab712009-04-14 17:16:24142 }
143 // We should only get here if we have a suggest url for the keyword or default
144 // providers.
[email protected]1cb2dac2010-03-08 21:49:15145 DCHECK_GT(suggest_results_pending_, 0);
initial.commit09911bf2008-07-26 23:55:29146}
147
148void SearchProvider::Stop() {
149 StopHistory();
150 StopSuggest();
151 done_ = true;
152}
153
154void SearchProvider::OnURLFetchComplete(const URLFetcher* source,
155 const GURL& url,
156 const URLRequestStatus& status,
157 int response_code,
158 const ResponseCookies& cookie,
159 const std::string& data) {
160 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24161 suggest_results_pending_--;
[email protected]1cb2dac2010-03-08 21:49:15162 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06163 const net::HttpResponseHeaders* const response_headers =
164 source->response_headers();
165 std::string json_data(data);
[email protected]6c85aa02009-02-27 12:08:09166 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
167 // files in non-UTF-8 encodings. The actual encoding is usually specified in
168 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06169 if (response_headers) {
170 std::string charset;
171 if (response_headers->GetCharset(&charset)) {
172 std::wstring wide_data;
173 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]d6e58c6e2009-10-10 20:40:50174 if (base::CodepageToWide(data, charset.c_str(),
175 base::OnStringConversionError::FAIL,
176 &wide_data))
[email protected]f0a51fb52009-03-05 12:46:38177 json_data = WideToUTF8(wide_data);
[email protected]ec9207d32008-09-26 00:51:06178 }
179 }
180
[email protected]257ab712009-04-14 17:16:24181 bool is_keyword_results = (source == keyword_fetcher_.get());
182 SuggestResults* suggest_results = is_keyword_results ?
183 &keyword_suggest_results_ : &default_suggest_results_;
184
[email protected]b4cebf82008-12-29 19:59:08185 if (status.is_success() && response_code == 200) {
186 JSONStringValueSerializer deserializer(json_data);
187 deserializer.set_allow_trailing_comma(true);
[email protected]ba399672010-04-06 15:42:39188 scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL));
[email protected]257ab712009-04-14 17:16:24189 const std::wstring& input_text =
190 is_keyword_results ? keyword_input_text_ : input_.text();
[email protected]b4cebf82008-12-29 19:59:08191 have_suggest_results_ =
[email protected]257ab712009-04-14 17:16:24192 root_val.get() &&
193 ParseSuggestResults(root_val.get(), is_keyword_results, input_text,
194 suggest_results);
[email protected]b4cebf82008-12-29 19:59:08195 }
196
initial.commit09911bf2008-07-26 23:55:29197 ConvertResultsToAutocompleteMatches();
[email protected]257ab712009-04-14 17:16:24198 listener_->OnProviderUpdate(!suggest_results->empty());
initial.commit09911bf2008-07-26 23:55:29199}
200
[email protected]8deeb952008-10-09 18:21:27201void SearchProvider::StartOrStopHistoryQuery(bool minimal_changes) {
initial.commit09911bf2008-07-26 23:55:29202 // For the minimal_changes case, if we finished the previous query and still
203 // have its results, or are allowed to keep running it, just do that, rather
204 // than starting a new query.
205 if (minimal_changes &&
[email protected]8deeb952008-10-09 18:21:27206 (have_history_results_ || (!done_ && !input_.synchronous_only())))
initial.commit09911bf2008-07-26 23:55:29207 return;
208
209 // We can't keep running any previous query, so halt it.
210 StopHistory();
211
212 // We can't start a new query if we're only allowed synchronous results.
[email protected]8deeb952008-10-09 18:21:27213 if (input_.synchronous_only())
initial.commit09911bf2008-07-26 23:55:29214 return;
215
[email protected]257ab712009-04-14 17:16:24216 // Request history for both the keyword and default provider.
217 if (providers_.valid_keyword_provider()) {
218 ScheduleHistoryQuery(providers_.keyword_provider().id(),
219 keyword_input_text_);
220 }
221 if (providers_.valid_default_provider()) {
222 ScheduleHistoryQuery(providers_.default_provider().id(),
223 input_.text());
224 }
initial.commit09911bf2008-07-26 23:55:29225}
226
[email protected]8deeb952008-10-09 18:21:27227void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]6c85aa02009-02-27 12:08:09228 // Don't send any queries to the server until some time has elapsed after
229 // the last keypress, to avoid flooding the server with requests we are
230 // likely to end up throwing away anyway.
231 static const int kQueryDelayMs = 200;
232
[email protected]83c726482008-09-10 06:36:34233 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29234 StopSuggest();
235 return;
236 }
237
238 // For the minimal_changes case, if we finished the previous query and still
239 // have its results, or are allowed to keep running it, just do that, rather
240 // than starting a new query.
241 if (minimal_changes &&
[email protected]8deeb952008-10-09 18:21:27242 (have_suggest_results_ || (!done_ && !input_.synchronous_only())))
initial.commit09911bf2008-07-26 23:55:29243 return;
244
245 // We can't keep running any previous query, so halt it.
246 StopSuggest();
247
248 // We can't start a new query if we're only allowed synchronous results.
[email protected]8deeb952008-10-09 18:21:27249 if (input_.synchronous_only())
initial.commit09911bf2008-07-26 23:55:29250 return;
251
[email protected]257ab712009-04-14 17:16:24252 // We'll have at least one pending fetch. Set it to 1 now, but the value is
253 // correctly set in Run. As Run isn't invoked immediately we need to set this
254 // now, else we won't think we're waiting on results from the server when we
255 // really are.
256 suggest_results_pending_ = 1;
257
initial.commit09911bf2008-07-26 23:55:29258 // Kick off a timer that will start the URL fetch if it completes before
259 // the user types another character.
[email protected]b547666d2009-04-23 16:37:58260 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs;
261 timer_.Start(TimeDelta::FromMilliseconds(delay), this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29262}
263
[email protected]83c726482008-09-10 06:36:34264bool SearchProvider::IsQuerySuitableForSuggest() const {
265 // Don't run Suggest when off the record, the engine doesn't support it, or
266 // the user has disabled it.
267 if (profile_->IsOffTheRecord() ||
[email protected]257ab712009-04-14 17:16:24268 (!providers_.valid_suggest_for_keyword_provider() &&
269 !providers_.valid_suggest_for_default_provider()) ||
[email protected]83c726482008-09-10 06:36:34270 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
271 return false;
272
[email protected]cac59d32010-08-09 23:23:14273 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34274 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34275
[email protected]cac59d32010-08-09 23:23:14276 // FORCED_QUERY means the user is explicitly asking us to search for this, so
277 // we assume it isn't a URL and/or there isn't private data.
278 if (input_.type() == AutocompleteInput::FORCED_QUERY)
279 return true;
[email protected]83c726482008-09-10 06:36:34280
[email protected]cac59d32010-08-09 23:23:14281 // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a
282 // scheme that isn't http/https/ftp, we shouldn't send it. Sending things
283 // like file: and data: is both a waste of time and a disclosure of
284 // potentially private, local data. Other "schemes" may actually be
285 // usernames, and we don't want to send passwords. If the scheme is OK, we
286 // still need to check other cases below. If this is QUERY, then the presence
287 // of these schemes means the user explicitly typed one, and thus this is
288 // probably a URL that's being entered and happens to currently be invalid --
289 // in which case we again want to run our checks below. Other QUERY cases are
290 // less likely to be URLs and thus we assume we're OK.
291 if ((input_.scheme() != L"http") && (input_.scheme() != L"https") &&
292 (input_.scheme() != L"ftp"))
293 return (input_.type() == AutocompleteInput::QUERY);
294
295 // Don't send URLs with usernames, queries or refs. Some of these are
296 // private, and the Suggest server is unlikely to have any useful results
297 // for any of them. Also don't send URLs with ports, as we may initially
298 // think that a username + password is a host + port (and we don't want to
299 // send usernames/passwords), and even if the port really is a port, the
300 // server is once again unlikely to have and useful results.
301 const url_parse::Parsed& parts = input_.parts();
302 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
303 parts.query.is_nonempty() || parts.ref.is_nonempty())
304 return false;
305
306 // Don't send anything for https except the hostname. Hostnames are OK
307 // because they are visible when the TCP connection is established, but the
308 // specific path may reveal private information.
309 if ((input_.scheme() == L"https") && parts.path.is_nonempty())
310 return false;
[email protected]83c726482008-09-10 06:36:34311
312 return true;
313}
314
initial.commit09911bf2008-07-26 23:55:29315void SearchProvider::StopHistory() {
316 history_request_consumer_.CancelAllRequests();
317 history_request_pending_ = false;
[email protected]257ab712009-04-14 17:16:24318 keyword_history_results_.clear();
319 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29320 have_history_results_ = false;
321}
322
323void SearchProvider::StopSuggest() {
[email protected]257ab712009-04-14 17:16:24324 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14325 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24326 // Stop any in-progress URL fetches.
327 keyword_fetcher_.reset();
328 default_fetcher_.reset();
329 keyword_suggest_results_.clear();
330 default_suggest_results_.clear();
331 keyword_navigation_results_.clear();
332 default_navigation_results_.clear();
initial.commit09911bf2008-07-26 23:55:29333 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29334}
335
[email protected]8b62334b2010-08-31 22:37:11336void SearchProvider::ScheduleHistoryQuery(TemplateURLID search_id,
[email protected]257ab712009-04-14 17:16:24337 const std::wstring& text) {
338 DCHECK(!text.empty());
339 HistoryService* const history_service =
340 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
341 HistoryService::Handle request_handle =
342 history_service->GetMostRecentKeywordSearchTerms(
[email protected]e53668962010-06-23 15:35:25343 search_id, WideToUTF16(text), static_cast<int>(kMaxMatches),
[email protected]257ab712009-04-14 17:16:24344 &history_request_consumer_,
345 NewCallback(this,
346 &SearchProvider::OnGotMostRecentKeywordSearchTerms));
347 history_request_consumer_.SetClientData(history_service, request_handle,
348 search_id);
349 history_request_pending_ = true;
350}
351
initial.commit09911bf2008-07-26 23:55:29352void SearchProvider::OnGotMostRecentKeywordSearchTerms(
353 CancelableRequestProvider::Handle handle,
354 HistoryResults* results) {
[email protected]257ab712009-04-14 17:16:24355 HistoryService* history_service =
356 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
357 DCHECK(history_service);
358 if (providers_.valid_keyword_provider() &&
[email protected]bed9bd6c2009-04-21 17:27:47359 (providers_.keyword_provider().id() ==
[email protected]257ab712009-04-14 17:16:24360 history_request_consumer_.GetClientData(history_service, handle))) {
361 keyword_history_results_ = *results;
362 } else {
363 default_history_results_ = *results;
364 }
[email protected]257ab712009-04-14 17:16:24365
366 if (history_request_consumer_.PendingRequestCount() == 1) {
367 // Requests are removed AFTER the callback is invoked. If the count == 1,
368 // it means no more history requests are pending.
369 history_request_pending_ = false;
370 have_history_results_ = true;
371 }
[email protected]b547666d2009-04-23 16:37:58372
373 ConvertResultsToAutocompleteMatches();
374 listener_->OnProviderUpdate(!results->empty());
initial.commit09911bf2008-07-26 23:55:29375}
376
[email protected]b547666d2009-04-23 16:37:58377URLFetcher* SearchProvider::CreateSuggestFetcher(int id,
378 const TemplateURL& provider,
[email protected]257ab712009-04-14 17:16:24379 const std::wstring& text) {
380 const TemplateURLRef* const suggestions_url = provider.suggestions_url();
381 DCHECK(suggestions_url->SupportsReplacement());
[email protected]b547666d2009-04-23 16:37:58382 URLFetcher* fetcher = URLFetcher::Create(id,
[email protected]ddd231e2010-06-29 20:35:19383 GURL(suggestions_url->ReplaceSearchTerms(
384 provider, text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
385 std::wstring())),
[email protected]257ab712009-04-14 17:16:24386 URLFetcher::GET, this);
387 fetcher->set_request_context(profile_->GetRequestContext());
388 fetcher->Start();
389 return fetcher;
390}
391
392bool SearchProvider::ParseSuggestResults(Value* root_val,
393 bool is_keyword,
394 const std::wstring& input_text,
395 SuggestResults* suggest_results) {
initial.commit09911bf2008-07-26 23:55:29396 if (!root_val->IsType(Value::TYPE_LIST))
397 return false;
398 ListValue* root_list = static_cast<ListValue*>(root_val);
399
400 Value* query_val;
[email protected]dc9a6762010-08-16 07:13:53401 string16 query_str;
initial.commit09911bf2008-07-26 23:55:29402 Value* result_val;
403 if ((root_list->GetSize() < 2) || !root_list->Get(0, &query_val) ||
[email protected]dc9a6762010-08-16 07:13:53404 !query_val->GetAsString(&query_str) ||
405 (query_str != WideToUTF16Hack(input_text)) ||
initial.commit09911bf2008-07-26 23:55:29406 !root_list->Get(1, &result_val) || !result_val->IsType(Value::TYPE_LIST))
407 return false;
408
409 ListValue* description_list = NULL;
410 if (root_list->GetSize() > 2) {
411 // 3rd element: Description list.
412 Value* description_val;
413 if (root_list->Get(2, &description_val) &&
414 description_val->IsType(Value::TYPE_LIST))
415 description_list = static_cast<ListValue*>(description_val);
416 }
417
418 // We don't care about the query URL list (the fourth element in the
419 // response) for now.
420
421 // Parse optional data in the results from the Suggest server if any.
422 ListValue* type_list = NULL;
423 // 5th argument: Optional key-value pairs.
424 // TODO: We may iterate the 5th+ arguments of the root_list if any other
425 // optional data are defined.
426 if (root_list->GetSize() > 4) {
427 Value* optional_val;
428 if (root_list->Get(4, &optional_val) &&
429 optional_val->IsType(Value::TYPE_DICTIONARY)) {
430 DictionaryValue* dict_val = static_cast<DictionaryValue*>(optional_val);
431
432 // Parse Google Suggest specific type extension.
[email protected]a65175d2010-08-17 04:00:57433 static const std::string kGoogleSuggestType("google:suggesttype");
initial.commit09911bf2008-07-26 23:55:29434 if (dict_val->HasKey(kGoogleSuggestType))
435 dict_val->GetList(kGoogleSuggestType, &type_list);
436 }
437 }
438
439 ListValue* result_list = static_cast<ListValue*>(result_val);
440 for (size_t i = 0; i < result_list->GetSize(); ++i) {
441 Value* suggestion_val;
[email protected]dc9a6762010-08-16 07:13:53442 string16 suggestion_str;
initial.commit09911bf2008-07-26 23:55:29443 if (!result_list->Get(i, &suggestion_val) ||
444 !suggestion_val->GetAsString(&suggestion_str))
445 return false;
446
447 Value* type_val;
[email protected]dc9a6762010-08-16 07:13:53448 std::string type_str;
initial.commit09911bf2008-07-26 23:55:29449 if (type_list && type_list->Get(i, &type_val) &&
[email protected]dc9a6762010-08-16 07:13:53450 type_val->GetAsString(&type_str) && (type_str == "NAVIGATION")) {
initial.commit09911bf2008-07-26 23:55:29451 Value* site_val;
[email protected]dc9a6762010-08-16 07:13:53452 string16 site_name;
[email protected]257ab712009-04-14 17:16:24453 NavigationResults& navigation_results =
454 is_keyword ? keyword_navigation_results_ :
455 default_navigation_results_;
[email protected]0be9b612010-05-18 01:13:41456 if ((navigation_results.size() < kMaxMatches) &&
initial.commit09911bf2008-07-26 23:55:29457 description_list && description_list->Get(i, &site_val) &&
458 site_val->IsType(Value::TYPE_STRING) &&
459 site_val->GetAsString(&site_name)) {
[email protected]16afe222009-01-08 18:57:45460 // We can't blindly trust the URL coming from the server to be valid.
[email protected]dc9a6762010-08-16 07:13:53461 GURL result_url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion_str),
[email protected]76e7da22010-06-18 22:44:49462 std::string()));
[email protected]dc9a6762010-08-16 07:13:53463 if (result_url.is_valid()) {
464 navigation_results.push_back(NavigationResult(result_url,
465 UTF16ToWideHack(site_name)));
466 }
initial.commit09911bf2008-07-26 23:55:29467 }
468 } else {
469 // TODO(kochi): Currently we treat a calculator result as a query, but it
470 // is better to have better presentation for caluculator results.
[email protected]0be9b612010-05-18 01:13:41471 if (suggest_results->size() < kMaxMatches)
[email protected]dc9a6762010-08-16 07:13:53472 suggest_results->push_back(UTF16ToWideHack(suggestion_str));
initial.commit09911bf2008-07-26 23:55:29473 }
474 }
475
initial.commit09911bf2008-07-26 23:55:29476 return true;
477}
478
479void SearchProvider::ConvertResultsToAutocompleteMatches() {
480 // Convert all the results to matches and add them to a map, so we can keep
481 // the most relevant match for each result.
482 MatchMap map;
[email protected]257ab712009-04-14 17:16:24483 const Time no_time;
484 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ?
initial.commit09911bf2008-07-26 23:55:29485 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
486 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]257ab712009-04-14 17:16:24487 // Keyword what you typed results are handled by the KeywordProvider.
initial.commit09911bf2008-07-26 23:55:29488
[email protected]257ab712009-04-14 17:16:24489 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
490 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
491 TemplateURLRef::NO_SUGGESTION_CHOSEN;
492 if (providers_.valid_default_provider()) {
493 AddMatchToMap(input_.text(), CalculateRelevanceForWhatYouTyped(),
494 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
495 did_not_accept_default_suggestion, false, &map);
initial.commit09911bf2008-07-26 23:55:29496 }
497
[email protected]257ab712009-04-14 17:16:24498 AddHistoryResultsToMap(keyword_history_results_, true,
499 did_not_accept_keyword_suggestion, &map);
500 AddHistoryResultsToMap(default_history_results_, false,
501 did_not_accept_default_suggestion, &map);
502
503 AddSuggestResultsToMap(keyword_suggest_results_, true,
504 did_not_accept_keyword_suggestion, &map);
505 AddSuggestResultsToMap(default_suggest_results_, false,
506 did_not_accept_default_suggestion, &map);
initial.commit09911bf2008-07-26 23:55:29507
508 // Now add the most relevant matches from the map to |matches_|.
509 matches_.clear();
510 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
511 matches_.push_back(i->second);
512
[email protected]257ab712009-04-14 17:16:24513 AddNavigationResultsToMatches(keyword_navigation_results_, true);
514 AddNavigationResultsToMatches(default_navigation_results_, false);
initial.commit09911bf2008-07-26 23:55:29515
[email protected]0be9b612010-05-18 01:13:41516 const size_t max_total_matches = kMaxMatches + 1; // 1 for "what you typed"
initial.commit09911bf2008-07-26 23:55:29517 std::partial_sort(matches_.begin(),
518 matches_.begin() + std::min(max_total_matches, matches_.size()),
519 matches_.end(), &AutocompleteMatch::MoreRelevant);
520 if (matches_.size() > max_total_matches)
[email protected]a28e95662008-11-12 19:19:02521 matches_.erase(matches_.begin() + max_total_matches, matches_.end());
initial.commit09911bf2008-07-26 23:55:29522
[email protected]cc63dea2008-08-21 20:56:31523 UpdateStarredStateOfMatches();
524
[email protected]6c85aa02009-02-27 12:08:09525 // We're done when both asynchronous subcomponents have finished. We can't
526 // use CancelableRequestConsumer.HasPendingRequests() for history requests
527 // here. A pending request is not cleared until after the completion
528 // callback has returned, but we've reached here from inside that callback.
529 // HasPendingRequests() would therefore return true, and if this is the last
530 // thing left to calculate for this query, we'll never mark the query "done".
[email protected]257ab712009-04-14 17:16:24531 done_ = !history_request_pending_ && !suggest_results_pending_;
532}
533
534void SearchProvider::AddNavigationResultsToMatches(
535 const NavigationResults& navigation_results,
536 bool is_keyword) {
537 if (!navigation_results.empty()) {
538 // TODO(kochi): http://b/1170574 We add only one results for navigational
539 // suggestions. If we can get more useful information about the score,
540 // consider adding more results.
[email protected]52d08b12009-10-19 18:42:36541 const size_t num_results = is_keyword ?
542 keyword_navigation_results_.size() : default_navigation_results_.size();
543 matches_.push_back(NavigationToMatch(navigation_results.front(),
544 CalculateRelevanceForNavigation(num_results, 0, is_keyword),
545 is_keyword));
[email protected]257ab712009-04-14 17:16:24546 }
547}
548
549void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
550 bool is_keyword,
551 int did_not_accept_suggestion,
552 MatchMap* map) {
553 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
554 ++i) {
[email protected]e53668962010-06-23 15:35:25555 AddMatchToMap(UTF16ToWide(i->term),
556 CalculateRelevanceForHistory(i->time, is_keyword),
[email protected]257ab712009-04-14 17:16:24557 AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion,
558 is_keyword, map);
559 }
560}
561
562void SearchProvider::AddSuggestResultsToMap(
563 const SuggestResults& suggest_results,
564 bool is_keyword,
565 int did_not_accept_suggestion,
566 MatchMap* map) {
567 for (size_t i = 0; i < suggest_results.size(); ++i) {
568 AddMatchToMap(suggest_results[i],
[email protected]52d08b12009-10-19 18:42:36569 CalculateRelevanceForSuggestion(suggest_results.size(), i,
[email protected]257ab712009-04-14 17:16:24570 is_keyword),
571 AutocompleteMatch::SEARCH_SUGGEST,
572 static_cast<int>(i), is_keyword, map);
573 }
initial.commit09911bf2008-07-26 23:55:29574}
575
576int SearchProvider::CalculateRelevanceForWhatYouTyped() const {
[email protected]52d08b12009-10-19 18:42:36577 if (providers_.valid_keyword_provider())
578 return 250;
579
initial.commit09911bf2008-07-26 23:55:29580 switch (input_.type()) {
581 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:36582 case AutocompleteInput::QUERY:
583 case AutocompleteInput::FORCED_QUERY:
584 return 1300;
initial.commit09911bf2008-07-26 23:55:29585
586 case AutocompleteInput::REQUESTED_URL:
[email protected]52d08b12009-10-19 18:42:36587 return 1150;
initial.commit09911bf2008-07-26 23:55:29588
589 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:36590 return 850;
initial.commit09911bf2008-07-26 23:55:29591
592 default:
593 NOTREACHED();
594 return 0;
595 }
596}
597
[email protected]257ab712009-04-14 17:16:24598int SearchProvider::CalculateRelevanceForHistory(const Time& time,
599 bool is_keyword) const {
initial.commit09911bf2008-07-26 23:55:29600 // The relevance of past searches falls off over time. This curve is chosen
601 // so that the relevance of a search 15 minutes ago is discounted about 50
602 // points, while the relevance of a search two weeks ago is discounted about
603 // 450 points.
604 const double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.);
[email protected]c3a4bd992010-08-18 20:25:01605 const int score_discount =
606 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:29607
[email protected]6c85aa02009-02-27 12:08:09608 // Don't let scores go below 0. Negative relevance scores are meaningful in
609 // a different way.
initial.commit09911bf2008-07-26 23:55:29610 int base_score;
[email protected]52d08b12009-10-19 18:42:36611 if (!providers_.is_primary_provider(is_keyword))
612 base_score = 200;
613 else
614 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
initial.commit09911bf2008-07-26 23:55:29615 return std::max(0, base_score - score_discount);
616}
617
[email protected]52d08b12009-10-19 18:42:36618int SearchProvider::CalculateRelevanceForSuggestion(size_t num_results,
619 size_t result_number,
620 bool is_keyword) const {
621 DCHECK(result_number < num_results);
622 int base_score;
623 if (!providers_.is_primary_provider(is_keyword))
624 base_score = 100;
625 else
626 base_score = (input_.type() == AutocompleteInput::URL) ? 300 : 600;
627 return base_score +
628 static_cast<int>(num_results - 1 - result_number);
initial.commit09911bf2008-07-26 23:55:29629}
630
[email protected]52d08b12009-10-19 18:42:36631int SearchProvider::CalculateRelevanceForNavigation(size_t num_results,
632 size_t result_number,
633 bool is_keyword) const {
634 DCHECK(result_number < num_results);
initial.commit09911bf2008-07-26 23:55:29635 // TODO(kochi): http://b/784900 Use relevance score from the NavSuggest
636 // server if possible.
[email protected]52d08b12009-10-19 18:42:36637 return (providers_.is_primary_provider(is_keyword) ? 800 : 150) +
638 static_cast<int>(num_results - 1 - result_number);
initial.commit09911bf2008-07-26 23:55:29639}
640
641void SearchProvider::AddMatchToMap(const std::wstring& query_string,
642 int relevance,
[email protected]4c1fb7ec2008-11-13 00:19:00643 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:29644 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:24645 bool is_keyword,
initial.commit09911bf2008-07-26 23:55:29646 MatchMap* map) {
[email protected]257ab712009-04-14 17:16:24647 const std::wstring& input_text =
648 is_keyword ? keyword_input_text_ : input_.text();
[email protected]4c1fb7ec2008-11-13 00:19:00649 AutocompleteMatch match(this, relevance, false, type);
initial.commit09911bf2008-07-26 23:55:29650 std::vector<size_t> content_param_offsets;
[email protected]257ab712009-04-14 17:16:24651 const TemplateURL& provider = is_keyword ? providers_.keyword_provider() :
652 providers_.default_provider();
[email protected]fb5153c52009-07-31 19:40:33653 // We do intra-string highlighting for suggestions - the suggested segment
654 // will be highlighted, e.g. for input_text = "you" the suggestion may be
655 // "youtube", so we'll bold the "tube" section: you*tube*.
656 if (input_text != query_string) {
657 match.contents.assign(query_string);
658 size_t input_position = match.contents.find(input_text);
659 if (input_position == std::wstring::npos) {
660 // The input text is not a substring of the query string, e.g. input
661 // text is "slasdot" and the query string is "slashdot", so we bold the
662 // whole thing.
663 match.contents_class.push_back(
664 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]ec2379162009-06-09 23:58:17665 } else {
[email protected]fb5153c52009-07-31 19:40:33666 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
667 // "bold" this. Consider modifying the terminology.
668 // We don't iterate over the string here annotating all matches because
669 // it looks odd to have every occurrence of a substring that may be as
670 // short as a single character highlighted in a query suggestion result,
671 // e.g. for input text "s" and query string "southwest airlines", it
672 // looks odd if both the first and last s are highlighted.
673 if (input_position != 0) {
674 match.contents_class.push_back(
675 ACMatchClassification(0, ACMatchClassification::NONE));
676 }
677 match.contents_class.push_back(
678 ACMatchClassification(input_position, ACMatchClassification::DIM));
679 size_t next_fragment_position = input_position + input_text.length();
680 if (next_fragment_position < query_string.length()) {
681 match.contents_class.push_back(
682 ACMatchClassification(next_fragment_position,
683 ACMatchClassification::NONE));
684 }
[email protected]ec2379162009-06-09 23:58:17685 }
initial.commit09911bf2008-07-26 23:55:29686 } else {
[email protected]fb5153c52009-07-31 19:40:33687 // Otherwise, we're dealing with the "default search" result which has no
688 // completion, but has the search provider name as the description.
689 match.contents.assign(query_string);
690 match.contents_class.push_back(
691 ACMatchClassification(0, ACMatchClassification::NONE));
692 match.description.assign(l10n_util::GetStringF(
693 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION,
[email protected]2c33dd22010-02-11 21:46:35694 provider.AdjustedShortNameForLocaleDirection()));
[email protected]fb5153c52009-07-31 19:40:33695 match.description_class.push_back(
696 ACMatchClassification(0, ACMatchClassification::DIM));
initial.commit09911bf2008-07-26 23:55:29697 }
698
699 // When the user forced a query, we need to make sure all the fill_into_edit
700 // values preserve that property. Otherwise, if the user starts editing a
701 // suggestion, non-Search results will suddenly appear.
702 size_t search_start = 0;
703 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
704 match.fill_into_edit.assign(L"?");
705 ++search_start;
706 }
[email protected]c0048b42009-05-04 21:47:17707 if (is_keyword) {
708 match.fill_into_edit.append(providers_.keyword_provider().keyword() + L" ");
709 match.template_url = &providers_.keyword_provider();
710 }
initial.commit09911bf2008-07-26 23:55:29711 match.fill_into_edit.append(query_string);
[email protected]2c33dd22010-02-11 21:46:35712 // Not all suggestions start with the original input.
initial.commit09911bf2008-07-26 23:55:29713 if (!input_.prevent_inline_autocomplete() &&
[email protected]257ab712009-04-14 17:16:24714 !match.fill_into_edit.compare(search_start, input_text.length(),
715 input_text))
716 match.inline_autocomplete_offset = search_start + input_text.length();
initial.commit09911bf2008-07-26 23:55:29717
[email protected]257ab712009-04-14 17:16:24718 const TemplateURLRef* const search_url = provider.url();
initial.commit09911bf2008-07-26 23:55:29719 DCHECK(search_url->SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:22720 match.destination_url =
[email protected]ddd231e2010-06-29 20:35:19721 GURL(search_url->ReplaceSearchTerms(provider,
722 query_string,
723 accepted_suggestion,
724 input_text));
initial.commit09911bf2008-07-26 23:55:29725
726 // Search results don't look like URLs.
[email protected]0bfc29a2009-04-27 16:15:44727 match.transition =
728 is_keyword ? PageTransition::KEYWORD : PageTransition::GENERATED;
initial.commit09911bf2008-07-26 23:55:29729
730 // Try to add |match| to |map|. If a match for |query_string| is already in
731 // |map|, replace it if |match| is more relevant.
732 // NOTE: Keep this ToLower() call in sync with url_database.cc.
733 const std::pair<MatchMap::iterator, bool> i = map->insert(
734 std::pair<std::wstring, AutocompleteMatch>(
[email protected]e5a8c472010-08-04 19:47:20735 UTF16ToWide(l10n_util::ToLower(WideToUTF16(query_string))), match));
initial.commit09911bf2008-07-26 23:55:29736 // NOTE: We purposefully do a direct relevance comparison here instead of
737 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
738 // first" rather than "items alphabetically first" when the scores are equal.
739 // The only case this matters is when a user has results with the same score
740 // that differ only by capitalization; because the history system returns
741 // results sorted by recency, this means we'll pick the most recent such
742 // result even if the precision of our relevance score is too low to
743 // distinguish the two.
744 if (!i.second && (match.relevance > i.first->second.relevance))
745 i.first->second = match;
746}
747
748AutocompleteMatch SearchProvider::NavigationToMatch(
749 const NavigationResult& navigation,
[email protected]257ab712009-04-14 17:16:24750 int relevance,
751 bool is_keyword) {
752 const std::wstring& input_text =
753 is_keyword ? keyword_input_text_ : input_.text();
[email protected]4c1fb7ec2008-11-13 00:19:00754 AutocompleteMatch match(this, relevance, false,
755 AutocompleteMatch::NAVSUGGEST);
initial.commit09911bf2008-07-26 23:55:29756 match.destination_url = navigation.url;
[email protected]76e7da22010-06-18 22:44:49757 match.contents =
758 StringForURLDisplay(navigation.url, true, !HasHTTPScheme(input_text));
[email protected]257ab712009-04-14 17:16:24759 AutocompleteMatch::ClassifyMatchInString(input_text, match.contents,
initial.commit09911bf2008-07-26 23:55:29760 ACMatchClassification::URL,
761 &match.contents_class);
762
763 match.description = navigation.site_name;
[email protected]257ab712009-04-14 17:16:24764 AutocompleteMatch::ClassifyMatchInString(input_text, navigation.site_name,
initial.commit09911bf2008-07-26 23:55:29765 ACMatchClassification::NONE,
766 &match.description_class);
767
initial.commit09911bf2008-07-26 23:55:29768 // When the user forced a query, we need to make sure all the fill_into_edit
769 // values preserve that property. Otherwise, if the user starts editing a
770 // suggestion, non-Search results will suddenly appear.
771 if (input_.type() == AutocompleteInput::FORCED_QUERY)
772 match.fill_into_edit.assign(L"?");
[email protected]79845ef2010-06-02 02:37:40773 match.fill_into_edit.append(
774 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url,
775 match.contents));
initial.commit09911bf2008-07-26 23:55:29776 // TODO(pkasting): http://b/1112879 These should perhaps be
777 // inline-autocompletable?
778
779 return match;
780}