[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 8 | #include <cmath> |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 9 | |
[email protected] | a92b864 | 2009-05-05 23:38:56 | [diff] [blame] | 10 | #include "app/l10n_util.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | d6e58c6e | 2009-10-10 20:40:50 | [diff] [blame] | 12 | #include "base/i18n/icu_string_conversions.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/message_loop.h" |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 14 | #include "base/string16.h" |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 16 | #include "chrome/browser/autocomplete/keyword_provider.h" |
[email protected] | 9ac4009 | 2010-10-27 23:05:26 | [diff] [blame] | 17 | #include "chrome/browser/autocomplete/autocomplete_match.h" |
[email protected] | f7578f5 | 2010-08-30 22:22:49 | [diff] [blame] | 18 | #include "chrome/browser/google/google_util.h" |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 19 | #include "chrome/browser/history/history.h" |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 20 | #include "chrome/browser/instant/instant_controller.h" |
[email protected] | f870a32 | 2009-01-16 21:47:27 | [diff] [blame] | 21 | #include "chrome/browser/net/url_fixer_upper.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 22 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 24 | #include "chrome/browser/history/in_memory_database.h" |
[email protected] | d54e03a5 | 2009-01-16 00:31:04 | [diff] [blame] | 25 | #include "chrome/browser/search_engines/template_url_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include "chrome/common/json_value_serializer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | #include "chrome/common/pref_names.h" |
[email protected] | dcf7d35 | 2009-02-26 01:56:02 | [diff] [blame] | 28 | #include "chrome/common/url_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | #include "googleurl/src/url_util.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 30 | #include "grit/generated_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | #include "net/base/escape.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 32 | #include "net/http/http_response_headers.h" |
| 33 | #include "net/url_request/url_request_status.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 35 | using base::Time; |
| 36 | using base::TimeDelta; |
| 37 | |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 38 | // static |
| 39 | const int SearchProvider::kDefaultProviderURLFetcherID = 1; |
| 40 | // static |
| 41 | const int SearchProvider::kKeywordProviderURLFetcherID = 2; |
| 42 | |
| 43 | // static |
| 44 | bool SearchProvider::query_suggest_immediately_ = false; |
| 45 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 46 | void 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] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 59 | SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile) |
| 60 | : AutocompleteProvider(listener, profile, "Search"), |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 61 | suggest_results_pending_(0), |
[email protected] | 8e5cc28 | 2010-12-05 18:11:39 | [diff] [blame] | 62 | have_suggest_results_(false), |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 63 | instant_finalized_(false) { |
| 64 | } |
| 65 | |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 66 | void SearchProvider::FinalizeInstantQuery(const std::wstring& input_text, |
| 67 | const std::wstring& suggest_text) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 68 | if (done_ || instant_finalized_) |
| 69 | return; |
| 70 | |
| 71 | instant_finalized_ = true; |
| 72 | UpdateDone(); |
| 73 | |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 74 | if (input_text.empty()) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 75 | // 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] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 81 | 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] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 86 | // 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] | e030de6 | 2010-11-24 05:41:19 | [diff] [blame] | 93 | i = matches_.erase(i); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 94 | } 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] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 105 | AddMatchToMap(text, adjusted_input_text, |
| 106 | CalculateRelevanceForWhatYouTyped() + 1, |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 107 | AutocompleteMatch::SEARCH_SUGGEST, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 108 | did_not_accept_default_suggestion, false, |
| 109 | input_.initial_prevent_inline_autocomplete(), &match_map); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 110 | DCHECK_EQ(1u, match_map.size()); |
| 111 | matches_.push_back(match_map.begin()->second); |
| 112 | |
| 113 | listener_->OnProviderUpdate(true); |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 114 | } |
| 115 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | void SearchProvider::Start(const AutocompleteInput& input, |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 117 | bool minimal_changes) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | matches_.clear(); |
| 119 | |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 120 | instant_finalized_ = input.synchronous_only(); |
| 121 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 122 | // Can't return search/suggest results for bogus input or without a profile. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | if (!profile_ || (input.type() == AutocompleteInput::INVALID)) { |
| 124 | Stop(); |
| 125 | return; |
| 126 | } |
| 127 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 128 | keyword_input_text_.clear(); |
| 129 | const TemplateURL* keyword_provider = |
| 130 | KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input, |
| 131 | &keyword_input_text_); |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 132 | if (keyword_input_text_.empty()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 133 | keyword_provider = NULL; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 134 | |
| 135 | const TemplateURL* default_provider = |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 137 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | Stop(); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | // If we're still running an old query but have since changed the query text |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 150 | // or the providers, abort the query. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | if (!done_ && (!minimal_changes || |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 152 | !providers_.equals(default_provider, keyword_provider))) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | Stop(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 154 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 156 | providers_.Set(default_provider, keyword_provider); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | |
| 158 | if (input.text().empty()) { |
| 159 | // User typed "?" alone. Give them a placeholder result indicating what |
| 160 | // this syntax does. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 161 | if (default_provider) { |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 162 | AutocompleteMatch match; |
| 163 | match.provider = this; |
[email protected] | d131857 | 2010-12-29 22:37:45 | [diff] [blame^] | 164 | match.contents.assign(UTF16ToWideHack( |
| 165 | l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE))); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 166 | match.contents_class.push_back( |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 167 | ACMatchClassification(0, ACMatchClassification::NONE)); |
[email protected] | d131857 | 2010-12-29 22:37:45 | [diff] [blame^] | 168 | match.description.assign(UTF16ToWideHack(l10n_util::GetStringFUTF16( |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 169 | IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, |
[email protected] | d131857 | 2010-12-29 22:37:45 | [diff] [blame^] | 170 | WideToUTF16Hack( |
| 171 | default_provider->AdjustedShortNameForLocaleDirection())))); |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 172 | match.description_class.push_back( |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 173 | ACMatchClassification(0, ACMatchClassification::DIM)); |
| 174 | matches_.push_back(match); |
| 175 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | Stop(); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | input_ = input; |
| 181 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 182 | DoHistoryQuery(minimal_changes); |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 183 | StartOrStopSuggestQuery(minimal_changes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | ConvertResultsToAutocompleteMatches(); |
| 185 | } |
| 186 | |
| 187 | void SearchProvider::Run() { |
| 188 | // Start a new request with the current input. |
| 189 | DCHECK(!done_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 190 | suggest_results_pending_ = 0; |
| 191 | if (providers_.valid_suggest_for_keyword_provider()) { |
| 192 | suggest_results_pending_++; |
| 193 | keyword_fetcher_.reset( |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 194 | CreateSuggestFetcher(kKeywordProviderURLFetcherID, |
| 195 | providers_.keyword_provider(), |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 196 | keyword_input_text_)); |
| 197 | } |
| 198 | if (providers_.valid_suggest_for_default_provider()) { |
| 199 | suggest_results_pending_++; |
| 200 | default_fetcher_.reset( |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 201 | CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
| 202 | providers_.default_provider(), input_.text())); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 203 | } |
| 204 | // We should only get here if we have a suggest url for the keyword or default |
| 205 | // providers. |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 206 | DCHECK_GT(suggest_results_pending_, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void SearchProvider::Stop() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | StopSuggest(); |
| 211 | done_ = true; |
| 212 | } |
| 213 | |
| 214 | void 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] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 221 | suggest_results_pending_--; |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 222 | DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 223 | const net::HttpResponseHeaders* const response_headers = |
| 224 | source->response_headers(); |
| 225 | std::string json_data(data); |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 226 | // 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] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 229 | 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] | d6e58c6e | 2009-10-10 20:40:50 | [diff] [blame] | 234 | if (base::CodepageToWide(data, charset.c_str(), |
| 235 | base::OnStringConversionError::FAIL, |
| 236 | &wide_data)) |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 237 | json_data = WideToUTF8(wide_data); |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 241 | 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] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 245 | if (status.is_success() && response_code == 200) { |
| 246 | JSONStringValueSerializer deserializer(json_data); |
| 247 | deserializer.set_allow_trailing_comma(true); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 248 | scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 249 | const std::wstring& input_text = |
| 250 | is_keyword_results ? keyword_input_text_ : input_.text(); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 251 | have_suggest_results_ = |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 252 | root_val.get() && |
| 253 | ParseSuggestResults(root_val.get(), is_keyword_results, input_text, |
| 254 | suggest_results); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 255 | } |
| 256 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 258 | listener_->OnProviderUpdate(!suggest_results->empty()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 261 | SearchProvider::~SearchProvider() { |
| 262 | } |
| 263 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 264 | void 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | return; |
| 269 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 270 | keyword_history_results_.clear(); |
| 271 | default_history_results_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 273 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | return; |
| 279 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 280 | // Request history for both the keyword and default provider. |
| 281 | if (providers_.valid_keyword_provider()) { |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 282 | url_db->GetMostRecentKeywordSearchTerms( |
| 283 | providers_.keyword_provider().id(), |
| 284 | WideToUTF16(keyword_input_text_), |
| 285 | static_cast<int>(kMaxMatches), |
| 286 | &keyword_history_results_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 287 | } |
| 288 | if (providers_.valid_default_provider()) { |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 289 | url_db->GetMostRecentKeywordSearchTerms( |
| 290 | providers_.default_provider().id(), |
| 291 | WideToUTF16(input_.text()), |
| 292 | static_cast<int>(kMaxMatches), |
| 293 | &default_history_results_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 294 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 297 | void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 298 | // 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] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 303 | if (!IsQuerySuitableForSuggest()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | 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] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 312 | (have_suggest_results_ || (!done_ && !input_.synchronous_only()))) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 313 | 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] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 319 | if (input_.synchronous_only()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 320 | return; |
| 321 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 322 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | // Kick off a timer that will start the URL fetch if it completes before |
| 329 | // the user types another character. |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 330 | int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs; |
| 331 | timer_.Start(TimeDelta::FromMilliseconds(delay), this, &SearchProvider::Run); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 334 | bool 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] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 338 | (!providers_.valid_suggest_for_keyword_provider() && |
| 339 | !providers_.valid_suggest_for_default_provider()) || |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 340 | !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)) |
| 341 | return false; |
| 342 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 343 | // If the input type might be a URL, we take extra care so that private data |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 344 | // isn't sent to the server. |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 345 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 346 | // 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] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 350 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 351 | // 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] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 381 | |
| 382 | return true; |
| 383 | } |
| 384 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | void SearchProvider::StopSuggest() { |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 386 | suggest_results_pending_ = 0; |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 387 | timer_.Stop(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 388 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | have_suggest_results_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | } |
| 397 | |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 398 | URLFetcher* SearchProvider::CreateSuggestFetcher(int id, |
| 399 | const TemplateURL& provider, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 400 | const std::wstring& text) { |
| 401 | const TemplateURLRef* const suggestions_url = provider.suggestions_url(); |
| 402 | DCHECK(suggestions_url->SupportsReplacement()); |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 403 | URLFetcher* fetcher = URLFetcher::Create(id, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 404 | GURL(suggestions_url->ReplaceSearchTerms( |
| 405 | provider, text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, |
| 406 | std::wstring())), |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 407 | URLFetcher::GET, this); |
| 408 | fetcher->set_request_context(profile_->GetRequestContext()); |
| 409 | fetcher->Start(); |
| 410 | return fetcher; |
| 411 | } |
| 412 | |
| 413 | bool SearchProvider::ParseSuggestResults(Value* root_val, |
| 414 | bool is_keyword, |
| 415 | const std::wstring& input_text, |
| 416 | SuggestResults* suggest_results) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 417 | 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] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 422 | string16 query_str; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 423 | Value* result_val; |
| 424 | if ((root_list->GetSize() < 2) || !root_list->Get(0, &query_val) || |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 425 | !query_val->GetAsString(&query_str) || |
| 426 | (query_str != WideToUTF16Hack(input_text)) || |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 427 | !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] | a65175d | 2010-08-17 04:00:57 | [diff] [blame] | 454 | static const std::string kGoogleSuggestType("google:suggesttype"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 455 | 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] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 463 | string16 suggestion_str; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 464 | if (!result_list->Get(i, &suggestion_val) || |
| 465 | !suggestion_val->GetAsString(&suggestion_str)) |
| 466 | return false; |
| 467 | |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 468 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 474 | Value* type_val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 475 | std::string type_str; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 476 | if (type_list && type_list->Get(i, &type_val) && |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 477 | type_val->GetAsString(&type_str) && (type_str == "NAVIGATION")) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 478 | Value* site_val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 479 | string16 site_name; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 480 | NavigationResults& navigation_results = |
| 481 | is_keyword ? keyword_navigation_results_ : |
| 482 | default_navigation_results_; |
[email protected] | 0be9b61 | 2010-05-18 01:13:41 | [diff] [blame] | 483 | if ((navigation_results.size() < kMaxMatches) && |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | description_list && description_list->Get(i, &site_val) && |
| 485 | site_val->IsType(Value::TYPE_STRING) && |
| 486 | site_val->GetAsString(&site_name)) { |
[email protected] | 16afe22 | 2009-01-08 18:57:45 | [diff] [blame] | 487 | // We can't blindly trust the URL coming from the server to be valid. |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 488 | GURL result_url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion_str), |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 489 | std::string())); |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 490 | if (result_url.is_valid()) { |
| 491 | navigation_results.push_back(NavigationResult(result_url, |
| 492 | UTF16ToWideHack(site_name))); |
| 493 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | } |
| 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] | 0be9b61 | 2010-05-18 01:13:41 | [diff] [blame] | 498 | if (suggest_results->size() < kMaxMatches) |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 499 | suggest_results->push_back(UTF16ToWideHack(suggestion_str)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 503 | return true; |
| 504 | } |
| 505 | |
| 506 | void 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] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 510 | const Time no_time; |
| 511 | int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 513 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 514 | // Keyword what you typed results are handled by the KeywordProvider. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 515 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 516 | 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] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 520 | AddMatchToMap(input_.text(), input_.text(), |
| 521 | CalculateRelevanceForWhatYouTyped(), |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 522 | AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 523 | did_not_accept_default_suggestion, false, |
| 524 | input_.initial_prevent_inline_autocomplete(), &map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 525 | } |
| 526 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 527 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 536 | |
| 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] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 542 | AddNavigationResultsToMatches(keyword_navigation_results_, true); |
| 543 | AddNavigationResultsToMatches(default_navigation_results_, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 544 | |
[email protected] | 0be9b61 | 2010-05-18 01:13:41 | [diff] [blame] | 545 | const size_t max_total_matches = kMaxMatches + 1; // 1 for "what you typed" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 546 | 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] | a28e9566 | 2008-11-12 19:19:02 | [diff] [blame] | 550 | matches_.erase(matches_.begin() + max_total_matches, matches_.end()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 551 | |
[email protected] | cc63dea | 2008-08-21 20:56:31 | [diff] [blame] | 552 | UpdateStarredStateOfMatches(); |
| 553 | |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 554 | UpdateDone(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void 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] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 564 | 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] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | void 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] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 578 | AddMatchToMap(UTF16ToWide(i->term), |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 579 | is_keyword ? keyword_input_text_ : input_.text(), |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 580 | CalculateRelevanceForHistory(i->time, is_keyword), |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 581 | AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 582 | is_keyword, input_.initial_prevent_inline_autocomplete(), |
| 583 | map); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | void 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] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 594 | is_keyword ? keyword_input_text_ : input_.text(), |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 595 | CalculateRelevanceForSuggestion(suggest_results.size(), i, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 596 | is_keyword), |
| 597 | AutocompleteMatch::SEARCH_SUGGEST, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 598 | static_cast<int>(i), is_keyword, |
| 599 | input_.initial_prevent_inline_autocomplete(), map); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 600 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | int SearchProvider::CalculateRelevanceForWhatYouTyped() const { |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 604 | if (providers_.valid_keyword_provider()) |
| 605 | return 250; |
| 606 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 607 | switch (input_.type()) { |
| 608 | case AutocompleteInput::UNKNOWN: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 609 | case AutocompleteInput::QUERY: |
| 610 | case AutocompleteInput::FORCED_QUERY: |
| 611 | return 1300; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 612 | |
| 613 | case AutocompleteInput::REQUESTED_URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 614 | return 1150; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 615 | |
| 616 | case AutocompleteInput::URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 617 | return 850; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 618 | |
| 619 | default: |
| 620 | NOTREACHED(); |
| 621 | return 0; |
| 622 | } |
| 623 | } |
| 624 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 625 | int SearchProvider::CalculateRelevanceForHistory(const Time& time, |
| 626 | bool is_keyword) const { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 627 | // 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] | bdca9beb | 2010-11-15 22:56:19 | [diff] [blame] | 636 | input_.type() != AutocompleteInput::URL && |
| 637 | !input_.prevent_inline_autocomplete()) { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 638 | // 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] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 647 | const int score_discount = |
| 648 | static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 649 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 650 | // Don't let scores go below 0. Negative relevance scores are meaningful in |
| 651 | // a different way. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 652 | int base_score; |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 653 | if (!providers_.is_primary_provider(is_keyword)) |
| 654 | base_score = 200; |
| 655 | else |
| 656 | base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 657 | return std::max(0, base_score - score_discount); |
| 658 | } |
| 659 | |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 660 | int 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 671 | } |
| 672 | |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 673 | int SearchProvider::CalculateRelevanceForNavigation(size_t num_results, |
| 674 | size_t result_number, |
| 675 | bool is_keyword) const { |
| 676 | DCHECK(result_number < num_results); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 677 | // TODO(kochi): http://b/784900 Use relevance score from the NavSuggest |
| 678 | // server if possible. |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 679 | return (providers_.is_primary_provider(is_keyword) ? 800 : 150) + |
| 680 | static_cast<int>(num_results - 1 - result_number); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void SearchProvider::AddMatchToMap(const std::wstring& query_string, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 684 | const std::wstring& input_text, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 685 | int relevance, |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 686 | AutocompleteMatch::Type type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 687 | int accepted_suggestion, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 688 | bool is_keyword, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 689 | bool prevent_inline_autocomplete, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 690 | MatchMap* map) { |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 691 | AutocompleteMatch match(this, relevance, false, type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 692 | std::vector<size_t> content_param_offsets; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 693 | const TemplateURL& provider = is_keyword ? providers_.keyword_provider() : |
| 694 | providers_.default_provider(); |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 695 | // 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] | ec237916 | 2009-06-09 23:58:17 | [diff] [blame] | 707 | } else { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 708 | // 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] | ec237916 | 2009-06-09 23:58:17 | [diff] [blame] | 727 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 728 | } else { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 729 | // 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] | d131857 | 2010-12-29 22:37:45 | [diff] [blame^] | 734 | match.description.assign(UTF16ToWideHack(l10n_util::GetStringFUTF16( |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 735 | IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, |
[email protected] | d131857 | 2010-12-29 22:37:45 | [diff] [blame^] | 736 | WideToUTF16Hack(provider.AdjustedShortNameForLocaleDirection())))); |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 737 | match.description_class.push_back( |
| 738 | ACMatchClassification(0, ACMatchClassification::DIM)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 739 | } |
| 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] | c0048b4 | 2009-05-04 21:47:17 | [diff] [blame] | 749 | if (is_keyword) { |
| 750 | match.fill_into_edit.append(providers_.keyword_provider().keyword() + L" "); |
| 751 | match.template_url = &providers_.keyword_provider(); |
| 752 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 753 | match.fill_into_edit.append(query_string); |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 754 | // Not all suggestions start with the original input. |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 755 | if (!prevent_inline_autocomplete && |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 756 | !match.fill_into_edit.compare(search_start, input_text.length(), |
| 757 | input_text)) |
| 758 | match.inline_autocomplete_offset = search_start + input_text.length(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 759 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 760 | const TemplateURLRef* const search_url = provider.url(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 761 | DCHECK(search_url->SupportsReplacement()); |
[email protected] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 762 | match.destination_url = |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 763 | GURL(search_url->ReplaceSearchTerms(provider, |
| 764 | query_string, |
| 765 | accepted_suggestion, |
| 766 | input_text)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 767 | |
| 768 | // Search results don't look like URLs. |
[email protected] | 0bfc29a | 2009-04-27 16:15:44 | [diff] [blame] | 769 | match.transition = |
| 770 | is_keyword ? PageTransition::KEYWORD : PageTransition::GENERATED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 771 | |
| 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] | e5a8c47 | 2010-08-04 19:47:20 | [diff] [blame] | 777 | UTF16ToWide(l10n_util::ToLower(WideToUTF16(query_string))), match)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 778 | // 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 | |
| 790 | AutocompleteMatch SearchProvider::NavigationToMatch( |
| 791 | const NavigationResult& navigation, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 792 | int relevance, |
| 793 | bool is_keyword) { |
| 794 | const std::wstring& input_text = |
| 795 | is_keyword ? keyword_input_text_ : input_.text(); |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 796 | AutocompleteMatch match(this, relevance, false, |
| 797 | AutocompleteMatch::NAVSUGGEST); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 798 | match.destination_url = navigation.url; |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 799 | match.contents = |
| 800 | StringForURLDisplay(navigation.url, true, !HasHTTPScheme(input_text)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 801 | AutocompleteMatch::ClassifyMatchInString(input_text, match.contents, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 802 | ACMatchClassification::URL, |
| 803 | &match.contents_class); |
| 804 | |
| 805 | match.description = navigation.site_name; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 806 | AutocompleteMatch::ClassifyMatchInString(input_text, navigation.site_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 807 | ACMatchClassification::NONE, |
| 808 | &match.description_class); |
| 809 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 810 | // 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] | 79845ef | 2010-06-02 02:37:40 | [diff] [blame] | 815 | match.fill_into_edit.append( |
| 816 | AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url, |
| 817 | match.contents)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 818 | // TODO(pkasting): http://b/1112879 These should perhaps be |
| 819 | // inline-autocompletable? |
| 820 | |
| 821 | return match; |
| 822 | } |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 823 | |
| 824 | void 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 | } |