blob: ff49c8ad9aa963c75bcbf6a817ac47f1fd2111f3 [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"
[email protected]9ac40092010-10-27 23:05:2617#include "chrome/browser/autocomplete/autocomplete_match.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]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]d54e03a52009-01-16 00:31:0425#include "chrome/browser/search_engines/template_url_model.h"
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/common/json_value_serializer.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"
initial.commit09911bf2008-07-26 23:55:2929#include "googleurl/src/url_util.h"
[email protected]34ac8f32009-02-22 23:03:2730#include "grit/generated_resources.h"
initial.commit09911bf2008-07-26 23:55:2931#include "net/base/escape.h"
[email protected]319d9e6f2009-02-18 19:47:2132#include "net/http/http_response_headers.h"
33#include "net/url_request/url_request_status.h"
initial.commit09911bf2008-07-26 23:55:2934
[email protected]e1acf6f2008-10-27 20:43:3335using base::Time;
36using base::TimeDelta;
37
[email protected]b547666d2009-04-23 16:37:5838// static
39const int SearchProvider::kDefaultProviderURLFetcherID = 1;
40// static
41const int SearchProvider::kKeywordProviderURLFetcherID = 2;
42
43// static
44bool SearchProvider::query_suggest_immediately_ = false;
45
[email protected]257ab712009-04-14 17:16:2446void SearchProvider::Providers::Set(const TemplateURL* default_provider,
47 const TemplateURL* keyword_provider) {
48 // TODO(pkasting): http://b/1162970 We shouldn't need to structure-copy
49 // this. Nor should we need |default_provider_| and |keyword_provider_|
50 // just to know whether the provider changed.
51 default_provider_ = default_provider;
52 if (default_provider)
53 cached_default_provider_ = *default_provider;
54 keyword_provider_ = keyword_provider;
55 if (keyword_provider)
56 cached_keyword_provider_ = *keyword_provider;
57}
58
[email protected]601858c02010-09-01 17:08:2059SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile)
60 : AutocompleteProvider(listener, profile, "Search"),
[email protected]601858c02010-09-01 17:08:2061 suggest_results_pending_(0),
[email protected]8e5cc282010-12-05 18:11:3962 have_suggest_results_(false),
[email protected]4ab4c7c2010-11-24 04:49:3463 instant_finalized_(false) {
64}
65
[email protected]e918c112010-12-08 23:03:4966void SearchProvider::FinalizeInstantQuery(const std::wstring& input_text,
67 const std::wstring& suggest_text) {
[email protected]4ab4c7c2010-11-24 04:49:3468 if (done_ || instant_finalized_)
69 return;
70
71 instant_finalized_ = true;
72 UpdateDone();
73
[email protected]e918c112010-12-08 23:03:4974 if (input_text.empty()) {
[email protected]4ab4c7c2010-11-24 04:49:3475 // We only need to update the listener if we're actually done.
76 if (done_)
77 listener_->OnProviderUpdate(false);
78 return;
79 }
80
[email protected]e918c112010-12-08 23:03:4981 std::wstring adjusted_input_text(input_text);
82 AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(),
83 &adjusted_input_text);
84
85 const std::wstring text = adjusted_input_text + suggest_text;
[email protected]4ab4c7c2010-11-24 04:49:3486 // Remove any matches that are identical to |text|. We don't use the
87 // destination_url for comparison as it varies depending upon the index passed
88 // to TemplateURL::ReplaceSearchTerms.
89 for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) {
90 if (((i->type == AutocompleteMatch::SEARCH_HISTORY) ||
91 (i->type == AutocompleteMatch::SEARCH_SUGGEST)) &&
92 (i->fill_into_edit == text)) {
[email protected]e030de62010-11-24 05:41:1993 i = matches_.erase(i);
[email protected]4ab4c7c2010-11-24 04:49:3494 } else {
95 ++i;
96 }
97 }
98
99 // Add the new suggest result. We give it a rank higher than
100 // SEARCH_WHAT_YOU_TYPED so that it gets autocompleted.
101 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
102 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
103 TemplateURLRef::NO_SUGGESTION_CHOSEN;
104 MatchMap match_map;
[email protected]e918c112010-12-08 23:03:49105 AddMatchToMap(text, adjusted_input_text,
106 CalculateRelevanceForWhatYouTyped() + 1,
[email protected]4ab4c7c2010-11-24 04:49:34107 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]e918c112010-12-08 23:03:49108 did_not_accept_default_suggestion, false,
109 input_.initial_prevent_inline_autocomplete(), &match_map);
[email protected]4ab4c7c2010-11-24 04:49:34110 DCHECK_EQ(1u, match_map.size());
111 matches_.push_back(match_map.begin()->second);
112
113 listener_->OnProviderUpdate(true);
[email protected]601858c02010-09-01 17:08:20114}
115
initial.commit09911bf2008-07-26 23:55:29116void SearchProvider::Start(const AutocompleteInput& input,
[email protected]8deeb952008-10-09 18:21:27117 bool minimal_changes) {
initial.commit09911bf2008-07-26 23:55:29118 matches_.clear();
119
[email protected]4ab4c7c2010-11-24 04:49:34120 instant_finalized_ = input.synchronous_only();
121
[email protected]6c85aa02009-02-27 12:08:09122 // Can't return search/suggest results for bogus input or without a profile.
initial.commit09911bf2008-07-26 23:55:29123 if (!profile_ || (input.type() == AutocompleteInput::INVALID)) {
124 Stop();
125 return;
126 }
127
[email protected]257ab712009-04-14 17:16:24128 keyword_input_text_.clear();
129 const TemplateURL* keyword_provider =
130 KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input,
131 &keyword_input_text_);
[email protected]8d457132010-11-04 18:13:40132 if (keyword_input_text_.empty())
[email protected]257ab712009-04-14 17:16:24133 keyword_provider = NULL;
[email protected]257ab712009-04-14 17:16:24134
135 const TemplateURL* default_provider =
initial.commit09911bf2008-07-26 23:55:29136 profile_->GetTemplateURLModel()->GetDefaultSearchProvider();
[email protected]257ab712009-04-14 17:16:24137 if (!TemplateURL::SupportsReplacement(default_provider))
138 default_provider = NULL;
139
140 if (keyword_provider == default_provider)
141 keyword_provider = NULL; // No use in querying the same provider twice.
142
143 if (!default_provider && !keyword_provider) {
144 // No valid providers.
initial.commit09911bf2008-07-26 23:55:29145 Stop();
146 return;
147 }
148
149 // If we're still running an old query but have since changed the query text
[email protected]257ab712009-04-14 17:16:24150 // or the providers, abort the query.
initial.commit09911bf2008-07-26 23:55:29151 if (!done_ && (!minimal_changes ||
[email protected]257ab712009-04-14 17:16:24152 !providers_.equals(default_provider, keyword_provider))) {
initial.commit09911bf2008-07-26 23:55:29153 Stop();
[email protected]257ab712009-04-14 17:16:24154 }
initial.commit09911bf2008-07-26 23:55:29155
[email protected]257ab712009-04-14 17:16:24156 providers_.Set(default_provider, keyword_provider);
initial.commit09911bf2008-07-26 23:55:29157
158 if (input.text().empty()) {
159 // User typed "?" alone. Give them a placeholder result indicating what
160 // this syntax does.
[email protected]257ab712009-04-14 17:16:24161 if (default_provider) {
[email protected]69c579e2010-04-23 20:01:00162 AutocompleteMatch match;
163 match.provider = this;
[email protected]d1318572010-12-29 22:37:45164 match.contents.assign(UTF16ToWideHack(
165 l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)));
[email protected]257ab712009-04-14 17:16:24166 match.contents_class.push_back(
[email protected]2c33dd22010-02-11 21:46:35167 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]d1318572010-12-29 22:37:45168 match.description.assign(UTF16ToWideHack(l10n_util::GetStringFUTF16(
[email protected]2c33dd22010-02-11 21:46:35169 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION,
[email protected]d1318572010-12-29 22:37:45170 WideToUTF16Hack(
171 default_provider->AdjustedShortNameForLocaleDirection()))));
[email protected]2c33dd22010-02-11 21:46:35172 match.description_class.push_back(
[email protected]257ab712009-04-14 17:16:24173 ACMatchClassification(0, ACMatchClassification::DIM));
174 matches_.push_back(match);
175 }
initial.commit09911bf2008-07-26 23:55:29176 Stop();
177 return;
178 }
179
180 input_ = input;
181
[email protected]8d457132010-11-04 18:13:40182 DoHistoryQuery(minimal_changes);
[email protected]8deeb952008-10-09 18:21:27183 StartOrStopSuggestQuery(minimal_changes);
initial.commit09911bf2008-07-26 23:55:29184 ConvertResultsToAutocompleteMatches();
185}
186
187void SearchProvider::Run() {
188 // Start a new request with the current input.
189 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24190 suggest_results_pending_ = 0;
191 if (providers_.valid_suggest_for_keyword_provider()) {
192 suggest_results_pending_++;
193 keyword_fetcher_.reset(
[email protected]b547666d2009-04-23 16:37:58194 CreateSuggestFetcher(kKeywordProviderURLFetcherID,
195 providers_.keyword_provider(),
[email protected]257ab712009-04-14 17:16:24196 keyword_input_text_));
197 }
198 if (providers_.valid_suggest_for_default_provider()) {
199 suggest_results_pending_++;
200 default_fetcher_.reset(
[email protected]b547666d2009-04-23 16:37:58201 CreateSuggestFetcher(kDefaultProviderURLFetcherID,
202 providers_.default_provider(), input_.text()));
[email protected]257ab712009-04-14 17:16:24203 }
204 // We should only get here if we have a suggest url for the keyword or default
205 // providers.
[email protected]1cb2dac2010-03-08 21:49:15206 DCHECK_GT(suggest_results_pending_, 0);
initial.commit09911bf2008-07-26 23:55:29207}
208
209void SearchProvider::Stop() {
initial.commit09911bf2008-07-26 23:55:29210 StopSuggest();
211 done_ = true;
212}
213
214void SearchProvider::OnURLFetchComplete(const URLFetcher* source,
215 const GURL& url,
216 const URLRequestStatus& status,
217 int response_code,
218 const ResponseCookies& cookie,
219 const std::string& data) {
220 DCHECK(!done_);
[email protected]257ab712009-04-14 17:16:24221 suggest_results_pending_--;
[email protected]1cb2dac2010-03-08 21:49:15222 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative.
[email protected]ec9207d32008-09-26 00:51:06223 const net::HttpResponseHeaders* const response_headers =
224 source->response_headers();
225 std::string json_data(data);
[email protected]6c85aa02009-02-27 12:08:09226 // JSON is supposed to be UTF-8, but some suggest service providers send JSON
227 // files in non-UTF-8 encodings. The actual encoding is usually specified in
228 // the Content-Type header field.
[email protected]ec9207d32008-09-26 00:51:06229 if (response_headers) {
230 std::string charset;
231 if (response_headers->GetCharset(&charset)) {
232 std::wstring wide_data;
233 // TODO(jungshik): Switch to CodePageToUTF8 after it's added.
[email protected]d6e58c6e2009-10-10 20:40:50234 if (base::CodepageToWide(data, charset.c_str(),
235 base::OnStringConversionError::FAIL,
236 &wide_data))
[email protected]f0a51fb52009-03-05 12:46:38237 json_data = WideToUTF8(wide_data);
[email protected]ec9207d32008-09-26 00:51:06238 }
239 }
240
[email protected]257ab712009-04-14 17:16:24241 bool is_keyword_results = (source == keyword_fetcher_.get());
242 SuggestResults* suggest_results = is_keyword_results ?
243 &keyword_suggest_results_ : &default_suggest_results_;
244
[email protected]b4cebf82008-12-29 19:59:08245 if (status.is_success() && response_code == 200) {
246 JSONStringValueSerializer deserializer(json_data);
247 deserializer.set_allow_trailing_comma(true);
[email protected]ba399672010-04-06 15:42:39248 scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL));
[email protected]257ab712009-04-14 17:16:24249 const std::wstring& input_text =
250 is_keyword_results ? keyword_input_text_ : input_.text();
[email protected]b4cebf82008-12-29 19:59:08251 have_suggest_results_ =
[email protected]257ab712009-04-14 17:16:24252 root_val.get() &&
253 ParseSuggestResults(root_val.get(), is_keyword_results, input_text,
254 suggest_results);
[email protected]b4cebf82008-12-29 19:59:08255 }
256
initial.commit09911bf2008-07-26 23:55:29257 ConvertResultsToAutocompleteMatches();
[email protected]257ab712009-04-14 17:16:24258 listener_->OnProviderUpdate(!suggest_results->empty());
initial.commit09911bf2008-07-26 23:55:29259}
260
[email protected]601858c02010-09-01 17:08:20261SearchProvider::~SearchProvider() {
262}
263
[email protected]8d457132010-11-04 18:13:40264void SearchProvider::DoHistoryQuery(bool minimal_changes) {
265 // The history query results are synchronous, so if minimal_changes is true,
266 // we still have the last results and don't need to do anything.
267 if (minimal_changes)
initial.commit09911bf2008-07-26 23:55:29268 return;
269
[email protected]8d457132010-11-04 18:13:40270 keyword_history_results_.clear();
271 default_history_results_.clear();
initial.commit09911bf2008-07-26 23:55:29272
[email protected]8d457132010-11-04 18:13:40273 HistoryService* const history_service =
274 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
275 history::URLDatabase* url_db = history_service ?
276 history_service->InMemoryDatabase() : NULL;
277 if (!url_db)
initial.commit09911bf2008-07-26 23:55:29278 return;
279
[email protected]257ab712009-04-14 17:16:24280 // Request history for both the keyword and default provider.
281 if (providers_.valid_keyword_provider()) {
[email protected]8d457132010-11-04 18:13:40282 url_db->GetMostRecentKeywordSearchTerms(
283 providers_.keyword_provider().id(),
284 WideToUTF16(keyword_input_text_),
285 static_cast<int>(kMaxMatches),
286 &keyword_history_results_);
[email protected]257ab712009-04-14 17:16:24287 }
288 if (providers_.valid_default_provider()) {
[email protected]8d457132010-11-04 18:13:40289 url_db->GetMostRecentKeywordSearchTerms(
290 providers_.default_provider().id(),
291 WideToUTF16(input_.text()),
292 static_cast<int>(kMaxMatches),
293 &default_history_results_);
[email protected]257ab712009-04-14 17:16:24294 }
initial.commit09911bf2008-07-26 23:55:29295}
296
[email protected]8deeb952008-10-09 18:21:27297void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) {
[email protected]6c85aa02009-02-27 12:08:09298 // Don't send any queries to the server until some time has elapsed after
299 // the last keypress, to avoid flooding the server with requests we are
300 // likely to end up throwing away anyway.
301 static const int kQueryDelayMs = 200;
302
[email protected]83c726482008-09-10 06:36:34303 if (!IsQuerySuitableForSuggest()) {
initial.commit09911bf2008-07-26 23:55:29304 StopSuggest();
305 return;
306 }
307
308 // For the minimal_changes case, if we finished the previous query and still
309 // have its results, or are allowed to keep running it, just do that, rather
310 // than starting a new query.
311 if (minimal_changes &&
[email protected]8deeb952008-10-09 18:21:27312 (have_suggest_results_ || (!done_ && !input_.synchronous_only())))
initial.commit09911bf2008-07-26 23:55:29313 return;
314
315 // We can't keep running any previous query, so halt it.
316 StopSuggest();
317
318 // We can't start a new query if we're only allowed synchronous results.
[email protected]8deeb952008-10-09 18:21:27319 if (input_.synchronous_only())
initial.commit09911bf2008-07-26 23:55:29320 return;
321
[email protected]257ab712009-04-14 17:16:24322 // We'll have at least one pending fetch. Set it to 1 now, but the value is
323 // correctly set in Run. As Run isn't invoked immediately we need to set this
324 // now, else we won't think we're waiting on results from the server when we
325 // really are.
326 suggest_results_pending_ = 1;
327
initial.commit09911bf2008-07-26 23:55:29328 // Kick off a timer that will start the URL fetch if it completes before
329 // the user types another character.
[email protected]b547666d2009-04-23 16:37:58330 int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs;
331 timer_.Start(TimeDelta::FromMilliseconds(delay), this, &SearchProvider::Run);
initial.commit09911bf2008-07-26 23:55:29332}
333
[email protected]83c726482008-09-10 06:36:34334bool SearchProvider::IsQuerySuitableForSuggest() const {
335 // Don't run Suggest when off the record, the engine doesn't support it, or
336 // the user has disabled it.
337 if (profile_->IsOffTheRecord() ||
[email protected]257ab712009-04-14 17:16:24338 (!providers_.valid_suggest_for_keyword_provider() &&
339 !providers_.valid_suggest_for_default_provider()) ||
[email protected]83c726482008-09-10 06:36:34340 !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled))
341 return false;
342
[email protected]cac59d32010-08-09 23:23:14343 // If the input type might be a URL, we take extra care so that private data
[email protected]83c726482008-09-10 06:36:34344 // isn't sent to the server.
[email protected]83c726482008-09-10 06:36:34345
[email protected]cac59d32010-08-09 23:23:14346 // FORCED_QUERY means the user is explicitly asking us to search for this, so
347 // we assume it isn't a URL and/or there isn't private data.
348 if (input_.type() == AutocompleteInput::FORCED_QUERY)
349 return true;
[email protected]83c726482008-09-10 06:36:34350
[email protected]cac59d32010-08-09 23:23:14351 // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a
352 // scheme that isn't http/https/ftp, we shouldn't send it. Sending things
353 // like file: and data: is both a waste of time and a disclosure of
354 // potentially private, local data. Other "schemes" may actually be
355 // usernames, and we don't want to send passwords. If the scheme is OK, we
356 // still need to check other cases below. If this is QUERY, then the presence
357 // of these schemes means the user explicitly typed one, and thus this is
358 // probably a URL that's being entered and happens to currently be invalid --
359 // in which case we again want to run our checks below. Other QUERY cases are
360 // less likely to be URLs and thus we assume we're OK.
361 if ((input_.scheme() != L"http") && (input_.scheme() != L"https") &&
362 (input_.scheme() != L"ftp"))
363 return (input_.type() == AutocompleteInput::QUERY);
364
365 // Don't send URLs with usernames, queries or refs. Some of these are
366 // private, and the Suggest server is unlikely to have any useful results
367 // for any of them. Also don't send URLs with ports, as we may initially
368 // think that a username + password is a host + port (and we don't want to
369 // send usernames/passwords), and even if the port really is a port, the
370 // server is once again unlikely to have and useful results.
371 const url_parse::Parsed& parts = input_.parts();
372 if (parts.username.is_nonempty() || parts.port.is_nonempty() ||
373 parts.query.is_nonempty() || parts.ref.is_nonempty())
374 return false;
375
376 // Don't send anything for https except the hostname. Hostnames are OK
377 // because they are visible when the TCP connection is established, but the
378 // specific path may reveal private information.
379 if ((input_.scheme() == L"https") && parts.path.is_nonempty())
380 return false;
[email protected]83c726482008-09-10 06:36:34381
382 return true;
383}
384
initial.commit09911bf2008-07-26 23:55:29385void SearchProvider::StopSuggest() {
[email protected]257ab712009-04-14 17:16:24386 suggest_results_pending_ = 0;
[email protected]2d316662008-09-03 18:18:14387 timer_.Stop();
[email protected]257ab712009-04-14 17:16:24388 // Stop any in-progress URL fetches.
389 keyword_fetcher_.reset();
390 default_fetcher_.reset();
391 keyword_suggest_results_.clear();
392 default_suggest_results_.clear();
393 keyword_navigation_results_.clear();
394 default_navigation_results_.clear();
initial.commit09911bf2008-07-26 23:55:29395 have_suggest_results_ = false;
initial.commit09911bf2008-07-26 23:55:29396}
397
[email protected]b547666d2009-04-23 16:37:58398URLFetcher* SearchProvider::CreateSuggestFetcher(int id,
399 const TemplateURL& provider,
[email protected]257ab712009-04-14 17:16:24400 const std::wstring& text) {
401 const TemplateURLRef* const suggestions_url = provider.suggestions_url();
402 DCHECK(suggestions_url->SupportsReplacement());
[email protected]b547666d2009-04-23 16:37:58403 URLFetcher* fetcher = URLFetcher::Create(id,
[email protected]ddd231e2010-06-29 20:35:19404 GURL(suggestions_url->ReplaceSearchTerms(
405 provider, text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
406 std::wstring())),
[email protected]257ab712009-04-14 17:16:24407 URLFetcher::GET, this);
408 fetcher->set_request_context(profile_->GetRequestContext());
409 fetcher->Start();
410 return fetcher;
411}
412
413bool SearchProvider::ParseSuggestResults(Value* root_val,
414 bool is_keyword,
415 const std::wstring& input_text,
416 SuggestResults* suggest_results) {
initial.commit09911bf2008-07-26 23:55:29417 if (!root_val->IsType(Value::TYPE_LIST))
418 return false;
419 ListValue* root_list = static_cast<ListValue*>(root_val);
420
421 Value* query_val;
[email protected]dc9a6762010-08-16 07:13:53422 string16 query_str;
initial.commit09911bf2008-07-26 23:55:29423 Value* result_val;
424 if ((root_list->GetSize() < 2) || !root_list->Get(0, &query_val) ||
[email protected]dc9a6762010-08-16 07:13:53425 !query_val->GetAsString(&query_str) ||
426 (query_str != WideToUTF16Hack(input_text)) ||
initial.commit09911bf2008-07-26 23:55:29427 !root_list->Get(1, &result_val) || !result_val->IsType(Value::TYPE_LIST))
428 return false;
429
430 ListValue* description_list = NULL;
431 if (root_list->GetSize() > 2) {
432 // 3rd element: Description list.
433 Value* description_val;
434 if (root_list->Get(2, &description_val) &&
435 description_val->IsType(Value::TYPE_LIST))
436 description_list = static_cast<ListValue*>(description_val);
437 }
438
439 // We don't care about the query URL list (the fourth element in the
440 // response) for now.
441
442 // Parse optional data in the results from the Suggest server if any.
443 ListValue* type_list = NULL;
444 // 5th argument: Optional key-value pairs.
445 // TODO: We may iterate the 5th+ arguments of the root_list if any other
446 // optional data are defined.
447 if (root_list->GetSize() > 4) {
448 Value* optional_val;
449 if (root_list->Get(4, &optional_val) &&
450 optional_val->IsType(Value::TYPE_DICTIONARY)) {
451 DictionaryValue* dict_val = static_cast<DictionaryValue*>(optional_val);
452
453 // Parse Google Suggest specific type extension.
[email protected]a65175d2010-08-17 04:00:57454 static const std::string kGoogleSuggestType("google:suggesttype");
initial.commit09911bf2008-07-26 23:55:29455 if (dict_val->HasKey(kGoogleSuggestType))
456 dict_val->GetList(kGoogleSuggestType, &type_list);
457 }
458 }
459
460 ListValue* result_list = static_cast<ListValue*>(result_val);
461 for (size_t i = 0; i < result_list->GetSize(); ++i) {
462 Value* suggestion_val;
[email protected]dc9a6762010-08-16 07:13:53463 string16 suggestion_str;
initial.commit09911bf2008-07-26 23:55:29464 if (!result_list->Get(i, &suggestion_val) ||
465 !suggestion_val->GetAsString(&suggestion_str))
466 return false;
467
[email protected]8e81f5092010-09-29 23:19:40468 // Google search may return empty suggestions for weird input characters,
469 // they make no sense at all and can cause problem in our code.
470 // See http://crbug.com/56214
471 if (!suggestion_str.length())
472 continue;
473
initial.commit09911bf2008-07-26 23:55:29474 Value* type_val;
[email protected]dc9a6762010-08-16 07:13:53475 std::string type_str;
initial.commit09911bf2008-07-26 23:55:29476 if (type_list && type_list->Get(i, &type_val) &&
[email protected]dc9a6762010-08-16 07:13:53477 type_val->GetAsString(&type_str) && (type_str == "NAVIGATION")) {
initial.commit09911bf2008-07-26 23:55:29478 Value* site_val;
[email protected]dc9a6762010-08-16 07:13:53479 string16 site_name;
[email protected]257ab712009-04-14 17:16:24480 NavigationResults& navigation_results =
481 is_keyword ? keyword_navigation_results_ :
482 default_navigation_results_;
[email protected]0be9b612010-05-18 01:13:41483 if ((navigation_results.size() < kMaxMatches) &&
initial.commit09911bf2008-07-26 23:55:29484 description_list && description_list->Get(i, &site_val) &&
485 site_val->IsType(Value::TYPE_STRING) &&
486 site_val->GetAsString(&site_name)) {
[email protected]16afe222009-01-08 18:57:45487 // We can't blindly trust the URL coming from the server to be valid.
[email protected]dc9a6762010-08-16 07:13:53488 GURL result_url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion_str),
[email protected]76e7da22010-06-18 22:44:49489 std::string()));
[email protected]dc9a6762010-08-16 07:13:53490 if (result_url.is_valid()) {
491 navigation_results.push_back(NavigationResult(result_url,
492 UTF16ToWideHack(site_name)));
493 }
initial.commit09911bf2008-07-26 23:55:29494 }
495 } else {
496 // TODO(kochi): Currently we treat a calculator result as a query, but it
497 // is better to have better presentation for caluculator results.
[email protected]0be9b612010-05-18 01:13:41498 if (suggest_results->size() < kMaxMatches)
[email protected]dc9a6762010-08-16 07:13:53499 suggest_results->push_back(UTF16ToWideHack(suggestion_str));
initial.commit09911bf2008-07-26 23:55:29500 }
501 }
502
initial.commit09911bf2008-07-26 23:55:29503 return true;
504}
505
506void SearchProvider::ConvertResultsToAutocompleteMatches() {
507 // Convert all the results to matches and add them to a map, so we can keep
508 // the most relevant match for each result.
509 MatchMap map;
[email protected]257ab712009-04-14 17:16:24510 const Time no_time;
511 int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ?
initial.commit09911bf2008-07-26 23:55:29512 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
513 TemplateURLRef::NO_SUGGESTION_CHOSEN;
[email protected]257ab712009-04-14 17:16:24514 // Keyword what you typed results are handled by the KeywordProvider.
initial.commit09911bf2008-07-26 23:55:29515
[email protected]257ab712009-04-14 17:16:24516 int did_not_accept_default_suggestion = default_suggest_results_.empty() ?
517 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE :
518 TemplateURLRef::NO_SUGGESTION_CHOSEN;
519 if (providers_.valid_default_provider()) {
[email protected]e918c112010-12-08 23:03:49520 AddMatchToMap(input_.text(), input_.text(),
521 CalculateRelevanceForWhatYouTyped(),
[email protected]257ab712009-04-14 17:16:24522 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
[email protected]e918c112010-12-08 23:03:49523 did_not_accept_default_suggestion, false,
524 input_.initial_prevent_inline_autocomplete(), &map);
initial.commit09911bf2008-07-26 23:55:29525 }
526
[email protected]257ab712009-04-14 17:16:24527 AddHistoryResultsToMap(keyword_history_results_, true,
528 did_not_accept_keyword_suggestion, &map);
529 AddHistoryResultsToMap(default_history_results_, false,
530 did_not_accept_default_suggestion, &map);
531
532 AddSuggestResultsToMap(keyword_suggest_results_, true,
533 did_not_accept_keyword_suggestion, &map);
534 AddSuggestResultsToMap(default_suggest_results_, false,
535 did_not_accept_default_suggestion, &map);
initial.commit09911bf2008-07-26 23:55:29536
537 // Now add the most relevant matches from the map to |matches_|.
538 matches_.clear();
539 for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i)
540 matches_.push_back(i->second);
541
[email protected]257ab712009-04-14 17:16:24542 AddNavigationResultsToMatches(keyword_navigation_results_, true);
543 AddNavigationResultsToMatches(default_navigation_results_, false);
initial.commit09911bf2008-07-26 23:55:29544
[email protected]0be9b612010-05-18 01:13:41545 const size_t max_total_matches = kMaxMatches + 1; // 1 for "what you typed"
initial.commit09911bf2008-07-26 23:55:29546 std::partial_sort(matches_.begin(),
547 matches_.begin() + std::min(max_total_matches, matches_.size()),
548 matches_.end(), &AutocompleteMatch::MoreRelevant);
549 if (matches_.size() > max_total_matches)
[email protected]a28e95662008-11-12 19:19:02550 matches_.erase(matches_.begin() + max_total_matches, matches_.end());
initial.commit09911bf2008-07-26 23:55:29551
[email protected]cc63dea2008-08-21 20:56:31552 UpdateStarredStateOfMatches();
553
[email protected]4ab4c7c2010-11-24 04:49:34554 UpdateDone();
[email protected]257ab712009-04-14 17:16:24555}
556
557void SearchProvider::AddNavigationResultsToMatches(
558 const NavigationResults& navigation_results,
559 bool is_keyword) {
560 if (!navigation_results.empty()) {
561 // TODO(kochi): http://b/1170574 We add only one results for navigational
562 // suggestions. If we can get more useful information about the score,
563 // consider adding more results.
[email protected]52d08b12009-10-19 18:42:36564 const size_t num_results = is_keyword ?
565 keyword_navigation_results_.size() : default_navigation_results_.size();
566 matches_.push_back(NavigationToMatch(navigation_results.front(),
567 CalculateRelevanceForNavigation(num_results, 0, is_keyword),
568 is_keyword));
[email protected]257ab712009-04-14 17:16:24569 }
570}
571
572void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results,
573 bool is_keyword,
574 int did_not_accept_suggestion,
575 MatchMap* map) {
576 for (HistoryResults::const_iterator i(results.begin()); i != results.end();
577 ++i) {
[email protected]e53668962010-06-23 15:35:25578 AddMatchToMap(UTF16ToWide(i->term),
[email protected]e918c112010-12-08 23:03:49579 is_keyword ? keyword_input_text_ : input_.text(),
[email protected]e53668962010-06-23 15:35:25580 CalculateRelevanceForHistory(i->time, is_keyword),
[email protected]257ab712009-04-14 17:16:24581 AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion,
[email protected]e918c112010-12-08 23:03:49582 is_keyword, input_.initial_prevent_inline_autocomplete(),
583 map);
[email protected]257ab712009-04-14 17:16:24584 }
585}
586
587void SearchProvider::AddSuggestResultsToMap(
588 const SuggestResults& suggest_results,
589 bool is_keyword,
590 int did_not_accept_suggestion,
591 MatchMap* map) {
592 for (size_t i = 0; i < suggest_results.size(); ++i) {
593 AddMatchToMap(suggest_results[i],
[email protected]e918c112010-12-08 23:03:49594 is_keyword ? keyword_input_text_ : input_.text(),
[email protected]52d08b12009-10-19 18:42:36595 CalculateRelevanceForSuggestion(suggest_results.size(), i,
[email protected]257ab712009-04-14 17:16:24596 is_keyword),
597 AutocompleteMatch::SEARCH_SUGGEST,
[email protected]e918c112010-12-08 23:03:49598 static_cast<int>(i), is_keyword,
599 input_.initial_prevent_inline_autocomplete(), map);
[email protected]257ab712009-04-14 17:16:24600 }
initial.commit09911bf2008-07-26 23:55:29601}
602
603int SearchProvider::CalculateRelevanceForWhatYouTyped() const {
[email protected]52d08b12009-10-19 18:42:36604 if (providers_.valid_keyword_provider())
605 return 250;
606
initial.commit09911bf2008-07-26 23:55:29607 switch (input_.type()) {
608 case AutocompleteInput::UNKNOWN:
[email protected]52d08b12009-10-19 18:42:36609 case AutocompleteInput::QUERY:
610 case AutocompleteInput::FORCED_QUERY:
611 return 1300;
initial.commit09911bf2008-07-26 23:55:29612
613 case AutocompleteInput::REQUESTED_URL:
[email protected]52d08b12009-10-19 18:42:36614 return 1150;
initial.commit09911bf2008-07-26 23:55:29615
616 case AutocompleteInput::URL:
[email protected]52d08b12009-10-19 18:42:36617 return 850;
initial.commit09911bf2008-07-26 23:55:29618
619 default:
620 NOTREACHED();
621 return 0;
622 }
623}
624
[email protected]257ab712009-04-14 17:16:24625int SearchProvider::CalculateRelevanceForHistory(const Time& time,
626 bool is_keyword) const {
[email protected]aa613d62010-11-09 20:40:18627 // The relevance of past searches falls off over time. There are two distinct
628 // equations used. If the first equation is used (searches to the primary
629 // provider with a type other than URL) the score starts at 1399 and falls to
630 // 1300. If the second equation is used the relevance of a search 15 minutes
631 // ago is discounted about 50 points, while the relevance of a search two
632 // weeks ago is discounted about 450 points.
633 double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.);
634
635 if (providers_.is_primary_provider(is_keyword) &&
[email protected]bdca9beb2010-11-15 22:56:19636 input_.type() != AutocompleteInput::URL &&
637 !input_.prevent_inline_autocomplete()) {
[email protected]aa613d62010-11-09 20:40:18638 // Searches with the past two days get a different curve.
639 const double autocomplete_time= 2 * 24 * 60 * 60;
640 if (elapsed_time < autocomplete_time) {
641 return 1399 - static_cast<int>(99 *
642 std::pow(elapsed_time / autocomplete_time, 2.5));
643 }
644 elapsed_time -= autocomplete_time;
645 }
646
[email protected]c3a4bd992010-08-18 20:25:01647 const int score_discount =
648 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3));
initial.commit09911bf2008-07-26 23:55:29649
[email protected]6c85aa02009-02-27 12:08:09650 // Don't let scores go below 0. Negative relevance scores are meaningful in
651 // a different way.
initial.commit09911bf2008-07-26 23:55:29652 int base_score;
[email protected]52d08b12009-10-19 18:42:36653 if (!providers_.is_primary_provider(is_keyword))
654 base_score = 200;
655 else
656 base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050;
initial.commit09911bf2008-07-26 23:55:29657 return std::max(0, base_score - score_discount);
658}
659
[email protected]52d08b12009-10-19 18:42:36660int SearchProvider::CalculateRelevanceForSuggestion(size_t num_results,
661 size_t result_number,
662 bool is_keyword) const {
663 DCHECK(result_number < num_results);
664 int base_score;
665 if (!providers_.is_primary_provider(is_keyword))
666 base_score = 100;
667 else
668 base_score = (input_.type() == AutocompleteInput::URL) ? 300 : 600;
669 return base_score +
670 static_cast<int>(num_results - 1 - result_number);
initial.commit09911bf2008-07-26 23:55:29671}
672
[email protected]52d08b12009-10-19 18:42:36673int SearchProvider::CalculateRelevanceForNavigation(size_t num_results,
674 size_t result_number,
675 bool is_keyword) const {
676 DCHECK(result_number < num_results);
initial.commit09911bf2008-07-26 23:55:29677 // TODO(kochi): http://b/784900 Use relevance score from the NavSuggest
678 // server if possible.
[email protected]52d08b12009-10-19 18:42:36679 return (providers_.is_primary_provider(is_keyword) ? 800 : 150) +
680 static_cast<int>(num_results - 1 - result_number);
initial.commit09911bf2008-07-26 23:55:29681}
682
683void SearchProvider::AddMatchToMap(const std::wstring& query_string,
[email protected]e918c112010-12-08 23:03:49684 const std::wstring& input_text,
initial.commit09911bf2008-07-26 23:55:29685 int relevance,
[email protected]4c1fb7ec2008-11-13 00:19:00686 AutocompleteMatch::Type type,
initial.commit09911bf2008-07-26 23:55:29687 int accepted_suggestion,
[email protected]257ab712009-04-14 17:16:24688 bool is_keyword,
[email protected]e918c112010-12-08 23:03:49689 bool prevent_inline_autocomplete,
initial.commit09911bf2008-07-26 23:55:29690 MatchMap* map) {
[email protected]4c1fb7ec2008-11-13 00:19:00691 AutocompleteMatch match(this, relevance, false, type);
initial.commit09911bf2008-07-26 23:55:29692 std::vector<size_t> content_param_offsets;
[email protected]257ab712009-04-14 17:16:24693 const TemplateURL& provider = is_keyword ? providers_.keyword_provider() :
694 providers_.default_provider();
[email protected]fb5153c52009-07-31 19:40:33695 // We do intra-string highlighting for suggestions - the suggested segment
696 // will be highlighted, e.g. for input_text = "you" the suggestion may be
697 // "youtube", so we'll bold the "tube" section: you*tube*.
698 if (input_text != query_string) {
699 match.contents.assign(query_string);
700 size_t input_position = match.contents.find(input_text);
701 if (input_position == std::wstring::npos) {
702 // The input text is not a substring of the query string, e.g. input
703 // text is "slasdot" and the query string is "slashdot", so we bold the
704 // whole thing.
705 match.contents_class.push_back(
706 ACMatchClassification(0, ACMatchClassification::MATCH));
[email protected]ec2379162009-06-09 23:58:17707 } else {
[email protected]fb5153c52009-07-31 19:40:33708 // TODO(beng): ACMatchClassification::MATCH now seems to just mean
709 // "bold" this. Consider modifying the terminology.
710 // We don't iterate over the string here annotating all matches because
711 // it looks odd to have every occurrence of a substring that may be as
712 // short as a single character highlighted in a query suggestion result,
713 // e.g. for input text "s" and query string "southwest airlines", it
714 // looks odd if both the first and last s are highlighted.
715 if (input_position != 0) {
716 match.contents_class.push_back(
717 ACMatchClassification(0, ACMatchClassification::NONE));
718 }
719 match.contents_class.push_back(
720 ACMatchClassification(input_position, ACMatchClassification::DIM));
721 size_t next_fragment_position = input_position + input_text.length();
722 if (next_fragment_position < query_string.length()) {
723 match.contents_class.push_back(
724 ACMatchClassification(next_fragment_position,
725 ACMatchClassification::NONE));
726 }
[email protected]ec2379162009-06-09 23:58:17727 }
initial.commit09911bf2008-07-26 23:55:29728 } else {
[email protected]fb5153c52009-07-31 19:40:33729 // Otherwise, we're dealing with the "default search" result which has no
730 // completion, but has the search provider name as the description.
731 match.contents.assign(query_string);
732 match.contents_class.push_back(
733 ACMatchClassification(0, ACMatchClassification::NONE));
[email protected]d1318572010-12-29 22:37:45734 match.description.assign(UTF16ToWideHack(l10n_util::GetStringFUTF16(
[email protected]fb5153c52009-07-31 19:40:33735 IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION,
[email protected]d1318572010-12-29 22:37:45736 WideToUTF16Hack(provider.AdjustedShortNameForLocaleDirection()))));
[email protected]fb5153c52009-07-31 19:40:33737 match.description_class.push_back(
738 ACMatchClassification(0, ACMatchClassification::DIM));
initial.commit09911bf2008-07-26 23:55:29739 }
740
741 // When the user forced a query, we need to make sure all the fill_into_edit
742 // values preserve that property. Otherwise, if the user starts editing a
743 // suggestion, non-Search results will suddenly appear.
744 size_t search_start = 0;
745 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
746 match.fill_into_edit.assign(L"?");
747 ++search_start;
748 }
[email protected]c0048b42009-05-04 21:47:17749 if (is_keyword) {
750 match.fill_into_edit.append(providers_.keyword_provider().keyword() + L" ");
751 match.template_url = &providers_.keyword_provider();
752 }
initial.commit09911bf2008-07-26 23:55:29753 match.fill_into_edit.append(query_string);
[email protected]2c33dd22010-02-11 21:46:35754 // Not all suggestions start with the original input.
[email protected]e918c112010-12-08 23:03:49755 if (!prevent_inline_autocomplete &&
[email protected]257ab712009-04-14 17:16:24756 !match.fill_into_edit.compare(search_start, input_text.length(),
757 input_text))
758 match.inline_autocomplete_offset = search_start + input_text.length();
initial.commit09911bf2008-07-26 23:55:29759
[email protected]257ab712009-04-14 17:16:24760 const TemplateURLRef* const search_url = provider.url();
initial.commit09911bf2008-07-26 23:55:29761 DCHECK(search_url->SupportsReplacement());
[email protected]7b9f3672009-06-15 18:31:22762 match.destination_url =
[email protected]ddd231e2010-06-29 20:35:19763 GURL(search_url->ReplaceSearchTerms(provider,
764 query_string,
765 accepted_suggestion,
766 input_text));
initial.commit09911bf2008-07-26 23:55:29767
768 // Search results don't look like URLs.
[email protected]0bfc29a2009-04-27 16:15:44769 match.transition =
770 is_keyword ? PageTransition::KEYWORD : PageTransition::GENERATED;
initial.commit09911bf2008-07-26 23:55:29771
772 // Try to add |match| to |map|. If a match for |query_string| is already in
773 // |map|, replace it if |match| is more relevant.
774 // NOTE: Keep this ToLower() call in sync with url_database.cc.
775 const std::pair<MatchMap::iterator, bool> i = map->insert(
776 std::pair<std::wstring, AutocompleteMatch>(
[email protected]e5a8c472010-08-04 19:47:20777 UTF16ToWide(l10n_util::ToLower(WideToUTF16(query_string))), match));
initial.commit09911bf2008-07-26 23:55:29778 // NOTE: We purposefully do a direct relevance comparison here instead of
779 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added
780 // first" rather than "items alphabetically first" when the scores are equal.
781 // The only case this matters is when a user has results with the same score
782 // that differ only by capitalization; because the history system returns
783 // results sorted by recency, this means we'll pick the most recent such
784 // result even if the precision of our relevance score is too low to
785 // distinguish the two.
786 if (!i.second && (match.relevance > i.first->second.relevance))
787 i.first->second = match;
788}
789
790AutocompleteMatch SearchProvider::NavigationToMatch(
791 const NavigationResult& navigation,
[email protected]257ab712009-04-14 17:16:24792 int relevance,
793 bool is_keyword) {
794 const std::wstring& input_text =
795 is_keyword ? keyword_input_text_ : input_.text();
[email protected]4c1fb7ec2008-11-13 00:19:00796 AutocompleteMatch match(this, relevance, false,
797 AutocompleteMatch::NAVSUGGEST);
initial.commit09911bf2008-07-26 23:55:29798 match.destination_url = navigation.url;
[email protected]76e7da22010-06-18 22:44:49799 match.contents =
800 StringForURLDisplay(navigation.url, true, !HasHTTPScheme(input_text));
[email protected]257ab712009-04-14 17:16:24801 AutocompleteMatch::ClassifyMatchInString(input_text, match.contents,
initial.commit09911bf2008-07-26 23:55:29802 ACMatchClassification::URL,
803 &match.contents_class);
804
805 match.description = navigation.site_name;
[email protected]257ab712009-04-14 17:16:24806 AutocompleteMatch::ClassifyMatchInString(input_text, navigation.site_name,
initial.commit09911bf2008-07-26 23:55:29807 ACMatchClassification::NONE,
808 &match.description_class);
809
initial.commit09911bf2008-07-26 23:55:29810 // When the user forced a query, we need to make sure all the fill_into_edit
811 // values preserve that property. Otherwise, if the user starts editing a
812 // suggestion, non-Search results will suddenly appear.
813 if (input_.type() == AutocompleteInput::FORCED_QUERY)
814 match.fill_into_edit.assign(L"?");
[email protected]79845ef2010-06-02 02:37:40815 match.fill_into_edit.append(
816 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url,
817 match.contents));
initial.commit09911bf2008-07-26 23:55:29818 // TODO(pkasting): http://b/1112879 These should perhaps be
819 // inline-autocompletable?
820
821 return match;
822}
[email protected]4ab4c7c2010-11-24 04:49:34823
824void SearchProvider::UpdateDone() {
825 // We're done when there are no more suggest queries pending (this is set to 1
826 // when the timer is started) and we're not waiting on instant.
827 done_ = ((suggest_results_pending_ == 0) &&
828 (instant_finalized_ || !InstantController::IsEnabled(profile_)));
829}