[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 11 | #include "base/i18n/case_conversion.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] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 16 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 17 | #include "chrome/browser/autocomplete/keyword_provider.h" |
[email protected] | 9ac4009 | 2010-10-27 23:05:26 | [diff] [blame] | 18 | #include "chrome/browser/autocomplete/autocomplete_match.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] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 25 | #include "chrome/browser/search_engines/template_url_service.h" |
| 26 | #include "chrome/browser/search_engines/template_url_service_factory.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" |
[email protected] | c311302 | 2011-04-16 03:26:30 | [diff] [blame] | 29 | #include "content/common/json_value_serializer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | #include "googleurl/src/url_util.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 31 | #include "grit/generated_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | #include "net/base/escape.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 33 | #include "net/http/http_response_headers.h" |
| 34 | #include "net/url_request/url_request_status.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 35 | #include "ui/base/l10n/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 37 | using base::Time; |
| 38 | using base::TimeDelta; |
| 39 | |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 40 | // static |
| 41 | const int SearchProvider::kDefaultProviderURLFetcherID = 1; |
| 42 | // static |
| 43 | const int SearchProvider::kKeywordProviderURLFetcherID = 2; |
| 44 | |
| 45 | // static |
| 46 | bool SearchProvider::query_suggest_immediately_ = false; |
| 47 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 48 | void SearchProvider::Providers::Set(const TemplateURL* default_provider, |
| 49 | const TemplateURL* keyword_provider) { |
| 50 | // TODO(pkasting): http://b/1162970 We shouldn't need to structure-copy |
| 51 | // this. Nor should we need |default_provider_| and |keyword_provider_| |
| 52 | // just to know whether the provider changed. |
| 53 | default_provider_ = default_provider; |
| 54 | if (default_provider) |
| 55 | cached_default_provider_ = *default_provider; |
| 56 | keyword_provider_ = keyword_provider; |
| 57 | if (keyword_provider) |
| 58 | cached_keyword_provider_ = *keyword_provider; |
| 59 | } |
| 60 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 61 | SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile) |
| 62 | : AutocompleteProvider(listener, profile, "Search"), |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 63 | suggest_results_pending_(0), |
[email protected] | 8e5cc28 | 2010-12-05 18:11:39 | [diff] [blame] | 64 | have_suggest_results_(false), |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 65 | instant_finalized_(false) { |
| 66 | } |
| 67 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 68 | void SearchProvider::FinalizeInstantQuery(const string16& input_text, |
| 69 | const string16& suggest_text) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 70 | if (done_ || instant_finalized_) |
| 71 | return; |
| 72 | |
| 73 | instant_finalized_ = true; |
| 74 | UpdateDone(); |
| 75 | |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 76 | if (input_text.empty()) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 77 | // We only need to update the listener if we're actually done. |
| 78 | if (done_) |
| 79 | listener_->OnProviderUpdate(false); |
| 80 | return; |
| 81 | } |
| 82 | |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 83 | default_provider_suggest_text_ = suggest_text; |
| 84 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 85 | string16 adjusted_input_text(input_text); |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 86 | AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(), |
| 87 | &adjusted_input_text); |
| 88 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 89 | const string16 text = adjusted_input_text + suggest_text; |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 90 | // Remove any matches that are identical to |text|. We don't use the |
| 91 | // destination_url for comparison as it varies depending upon the index passed |
| 92 | // to TemplateURL::ReplaceSearchTerms. |
| 93 | for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 94 | if (((i->type == AutocompleteMatch::SEARCH_HISTORY) || |
| 95 | (i->type == AutocompleteMatch::SEARCH_SUGGEST)) && |
| 96 | (i->fill_into_edit == text)) { |
[email protected] | e030de6 | 2010-11-24 05:41:19 | [diff] [blame] | 97 | i = matches_.erase(i); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 98 | } else { |
| 99 | ++i; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Add the new suggest result. We give it a rank higher than |
| 104 | // SEARCH_WHAT_YOU_TYPED so that it gets autocompleted. |
| 105 | int did_not_accept_default_suggestion = default_suggest_results_.empty() ? |
| 106 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 107 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
| 108 | MatchMap match_map; |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 109 | AddMatchToMap(text, adjusted_input_text, |
| 110 | CalculateRelevanceForWhatYouTyped() + 1, |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 111 | AutocompleteMatch::SEARCH_SUGGEST, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 112 | did_not_accept_default_suggestion, false, |
[email protected] | e0b231d | 2011-05-09 21:26:42 | [diff] [blame] | 113 | input_.prevent_inline_autocomplete(), &match_map); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 114 | DCHECK_EQ(1u, match_map.size()); |
| 115 | matches_.push_back(match_map.begin()->second); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 116 | |
| 117 | listener_->OnProviderUpdate(true); |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 118 | } |
| 119 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | void SearchProvider::Start(const AutocompleteInput& input, |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 121 | bool minimal_changes) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | matches_.clear(); |
| 123 | |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 124 | instant_finalized_ = |
| 125 | (input.matches_requested() != AutocompleteInput::ALL_MATCHES); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 126 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 127 | // Can't return search/suggest results for bogus input or without a profile. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | if (!profile_ || (input.type() == AutocompleteInput::INVALID)) { |
| 129 | Stop(); |
| 130 | return; |
| 131 | } |
| 132 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 133 | keyword_input_text_.clear(); |
| 134 | const TemplateURL* keyword_provider = |
| 135 | KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input, |
| 136 | &keyword_input_text_); |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 137 | if (keyword_input_text_.empty()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 138 | keyword_provider = NULL; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 139 | |
| 140 | const TemplateURL* default_provider = |
[email protected] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 141 | TemplateURLServiceFactory::GetForProfile(profile_)-> |
| 142 | GetDefaultSearchProvider(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 143 | if (!TemplateURL::SupportsReplacement(default_provider)) |
| 144 | default_provider = NULL; |
| 145 | |
| 146 | if (keyword_provider == default_provider) |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame^] | 147 | default_provider = NULL; // No use in querying the same provider twice. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 148 | |
| 149 | if (!default_provider && !keyword_provider) { |
| 150 | // No valid providers. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | Stop(); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | // 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] | 156 | // or the providers, abort the query. |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 157 | if (!minimal_changes || |
| 158 | !providers_.equals(default_provider, keyword_provider)) { |
| 159 | if (done_) |
| 160 | default_provider_suggest_text_.clear(); |
| 161 | else |
| 162 | Stop(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 163 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 165 | providers_.Set(default_provider, keyword_provider); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | |
| 167 | if (input.text().empty()) { |
| 168 | // User typed "?" alone. Give them a placeholder result indicating what |
| 169 | // this syntax does. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 170 | if (default_provider) { |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 171 | AutocompleteMatch match; |
| 172 | match.provider = this; |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 173 | match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 174 | match.contents_class.push_back( |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 175 | ACMatchClassification(0, ACMatchClassification::NONE)); |
[email protected] | 0385fc68 | 2011-07-07 19:36:17 | [diff] [blame] | 176 | match.template_url = &providers_.default_provider(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 177 | matches_.push_back(match); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 178 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 179 | Stop(); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | input_ = input; |
| 184 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 185 | DoHistoryQuery(minimal_changes); |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 186 | StartOrStopSuggestQuery(minimal_changes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | ConvertResultsToAutocompleteMatches(); |
| 188 | } |
| 189 | |
| 190 | void SearchProvider::Run() { |
| 191 | // Start a new request with the current input. |
| 192 | DCHECK(!done_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 193 | suggest_results_pending_ = 0; |
| 194 | if (providers_.valid_suggest_for_keyword_provider()) { |
| 195 | suggest_results_pending_++; |
| 196 | keyword_fetcher_.reset( |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 197 | CreateSuggestFetcher(kKeywordProviderURLFetcherID, |
| 198 | providers_.keyword_provider(), |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 199 | keyword_input_text_)); |
| 200 | } |
| 201 | if (providers_.valid_suggest_for_default_provider()) { |
| 202 | suggest_results_pending_++; |
| 203 | default_fetcher_.reset( |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 204 | CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
| 205 | providers_.default_provider(), input_.text())); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 206 | } |
| 207 | // We should only get here if we have a suggest url for the keyword or default |
| 208 | // providers. |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 209 | DCHECK_GT(suggest_results_pending_, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void SearchProvider::Stop() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | StopSuggest(); |
| 214 | done_ = true; |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 215 | default_provider_suggest_text_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | 0385fc68 | 2011-07-07 19:36:17 | [diff] [blame] | 218 | void SearchProvider::PostProcessResult(AutocompleteResult* result) { |
| 219 | // For each group of contiguous matches from the same TemplateURL, show the |
| 220 | // provider name as a description on the first match in the group. |
| 221 | const TemplateURL* last_template_url = NULL; |
| 222 | for (AutocompleteResult::iterator i = result->begin(); i != result->end(); |
| 223 | ++i) { |
| 224 | if (i->provider == this && |
| 225 | (i->type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || |
| 226 | i->type == AutocompleteMatch::SEARCH_HISTORY || |
| 227 | i->type == AutocompleteMatch::SEARCH_SUGGEST)) { |
| 228 | i->description.clear(); |
| 229 | i->description_class.clear(); |
| 230 | DCHECK(i->template_url); // We should always set a template_url |
| 231 | if (last_template_url != i->template_url) { |
| 232 | i->description = l10n_util::GetStringFUTF16( |
| 233 | IDS_AUTOCOMPLETE_SEARCH_DESCRIPTION, |
| 234 | i->template_url->AdjustedShortNameForLocaleDirection()); |
| 235 | i->description_class.push_back( |
| 236 | ACMatchClassification(0, ACMatchClassification::DIM)); |
| 237 | } |
| 238 | last_template_url = i->template_url; |
| 239 | } else { |
| 240 | last_template_url = NULL; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | void SearchProvider::OnURLFetchComplete(const URLFetcher* source, |
| 246 | const GURL& url, |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 247 | const net::URLRequestStatus& status, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | int response_code, |
[email protected] | cb04f5e | 2011-05-06 01:10:00 | [diff] [blame] | 249 | const net::ResponseCookies& cookie, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | const std::string& data) { |
| 251 | DCHECK(!done_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 252 | suggest_results_pending_--; |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 253 | DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 254 | const net::HttpResponseHeaders* const response_headers = |
| 255 | source->response_headers(); |
| 256 | std::string json_data(data); |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 257 | // JSON is supposed to be UTF-8, but some suggest service providers send JSON |
| 258 | // files in non-UTF-8 encodings. The actual encoding is usually specified in |
| 259 | // the Content-Type header field. |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 260 | if (response_headers) { |
| 261 | std::string charset; |
| 262 | if (response_headers->GetCharset(&charset)) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 263 | string16 data_16; |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 264 | // TODO(jungshik): Switch to CodePageToUTF8 after it's added. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 265 | if (base::CodepageToUTF16(data, charset.c_str(), |
| 266 | base::OnStringConversionError::FAIL, |
| 267 | &data_16)) |
| 268 | json_data = UTF16ToUTF8(data_16); |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 272 | bool is_keyword_results = (source == keyword_fetcher_.get()); |
| 273 | SuggestResults* suggest_results = is_keyword_results ? |
| 274 | &keyword_suggest_results_ : &default_suggest_results_; |
| 275 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 276 | if (status.is_success() && response_code == 200) { |
| 277 | JSONStringValueSerializer deserializer(json_data); |
| 278 | deserializer.set_allow_trailing_comma(true); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 279 | scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL)); |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 280 | const string16& input_text = |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 281 | is_keyword_results ? keyword_input_text_ : input_.text(); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 282 | have_suggest_results_ = |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 283 | root_val.get() && |
| 284 | ParseSuggestResults(root_val.get(), is_keyword_results, input_text, |
| 285 | suggest_results); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 286 | } |
| 287 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 289 | listener_->OnProviderUpdate(!suggest_results->empty()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 292 | SearchProvider::~SearchProvider() { |
| 293 | } |
| 294 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 295 | void SearchProvider::DoHistoryQuery(bool minimal_changes) { |
| 296 | // The history query results are synchronous, so if minimal_changes is true, |
| 297 | // we still have the last results and don't need to do anything. |
| 298 | if (minimal_changes) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | return; |
| 300 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 301 | keyword_history_results_.clear(); |
| 302 | default_history_results_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 304 | HistoryService* const history_service = |
| 305 | profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 306 | history::URLDatabase* url_db = history_service ? |
| 307 | history_service->InMemoryDatabase() : NULL; |
| 308 | if (!url_db) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 309 | return; |
| 310 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 311 | // Request history for both the keyword and default provider. |
| 312 | if (providers_.valid_keyword_provider()) { |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 313 | url_db->GetMostRecentKeywordSearchTerms( |
| 314 | providers_.keyword_provider().id(), |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 315 | keyword_input_text_, |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 316 | static_cast<int>(kMaxMatches), |
| 317 | &keyword_history_results_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 318 | } |
| 319 | if (providers_.valid_default_provider()) { |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 320 | url_db->GetMostRecentKeywordSearchTerms( |
| 321 | providers_.default_provider().id(), |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 322 | input_.text(), |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 323 | static_cast<int>(kMaxMatches), |
| 324 | &default_history_results_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 325 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 328 | void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 329 | // Don't send any queries to the server until some time has elapsed after |
| 330 | // the last keypress, to avoid flooding the server with requests we are |
| 331 | // likely to end up throwing away anyway. |
| 332 | static const int kQueryDelayMs = 200; |
| 333 | |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 334 | if (!IsQuerySuitableForSuggest()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 335 | StopSuggest(); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | // For the minimal_changes case, if we finished the previous query and still |
| 340 | // have its results, or are allowed to keep running it, just do that, rather |
| 341 | // than starting a new query. |
| 342 | if (minimal_changes && |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 343 | (have_suggest_results_ || |
| 344 | (!done_ && |
| 345 | input_.matches_requested() == AutocompleteInput::ALL_MATCHES))) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | return; |
| 347 | |
| 348 | // We can't keep running any previous query, so halt it. |
| 349 | StopSuggest(); |
| 350 | |
| 351 | // We can't start a new query if we're only allowed synchronous results. |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 352 | if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | return; |
| 354 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 355 | // We'll have at least one pending fetch. Set it to 1 now, but the value is |
| 356 | // correctly set in Run. As Run isn't invoked immediately we need to set this |
| 357 | // now, else we won't think we're waiting on results from the server when we |
| 358 | // really are. |
| 359 | suggest_results_pending_ = 1; |
| 360 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | // Kick off a timer that will start the URL fetch if it completes before |
| 362 | // the user types another character. |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 363 | int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs; |
| 364 | timer_.Start(TimeDelta::FromMilliseconds(delay), this, &SearchProvider::Run); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 365 | } |
| 366 | |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 367 | bool SearchProvider::IsQuerySuitableForSuggest() const { |
[email protected] | 2c910b7 | 2011-03-08 21:16:32 | [diff] [blame] | 368 | // Don't run Suggest in incognito mode, the engine doesn't support it, or |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 369 | // the user has disabled it. |
| 370 | if (profile_->IsOffTheRecord() || |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 371 | (!providers_.valid_suggest_for_keyword_provider() && |
| 372 | !providers_.valid_suggest_for_default_provider()) || |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 373 | !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)) |
| 374 | return false; |
| 375 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 376 | // 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] | 377 | // isn't sent to the server. |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 378 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 379 | // FORCED_QUERY means the user is explicitly asking us to search for this, so |
| 380 | // we assume it isn't a URL and/or there isn't private data. |
| 381 | if (input_.type() == AutocompleteInput::FORCED_QUERY) |
| 382 | return true; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 383 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 384 | // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a |
| 385 | // scheme that isn't http/https/ftp, we shouldn't send it. Sending things |
| 386 | // like file: and data: is both a waste of time and a disclosure of |
| 387 | // potentially private, local data. Other "schemes" may actually be |
| 388 | // usernames, and we don't want to send passwords. If the scheme is OK, we |
| 389 | // still need to check other cases below. If this is QUERY, then the presence |
| 390 | // of these schemes means the user explicitly typed one, and thus this is |
| 391 | // probably a URL that's being entered and happens to currently be invalid -- |
| 392 | // in which case we again want to run our checks below. Other QUERY cases are |
| 393 | // less likely to be URLs and thus we assume we're OK. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 394 | if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) && |
| 395 | !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) && |
| 396 | !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme)) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 397 | return (input_.type() == AutocompleteInput::QUERY); |
| 398 | |
| 399 | // Don't send URLs with usernames, queries or refs. Some of these are |
| 400 | // private, and the Suggest server is unlikely to have any useful results |
| 401 | // for any of them. Also don't send URLs with ports, as we may initially |
| 402 | // think that a username + password is a host + port (and we don't want to |
| 403 | // send usernames/passwords), and even if the port really is a port, the |
| 404 | // server is once again unlikely to have and useful results. |
| 405 | const url_parse::Parsed& parts = input_.parts(); |
| 406 | if (parts.username.is_nonempty() || parts.port.is_nonempty() || |
| 407 | parts.query.is_nonempty() || parts.ref.is_nonempty()) |
| 408 | return false; |
| 409 | |
| 410 | // Don't send anything for https except the hostname. Hostnames are OK |
| 411 | // because they are visible when the TCP connection is established, but the |
| 412 | // specific path may reveal private information. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 413 | if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) && |
| 414 | parts.path.is_nonempty()) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 415 | return false; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 416 | |
| 417 | return true; |
| 418 | } |
| 419 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | void SearchProvider::StopSuggest() { |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 421 | suggest_results_pending_ = 0; |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 422 | timer_.Stop(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 423 | // Stop any in-progress URL fetches. |
| 424 | keyword_fetcher_.reset(); |
| 425 | default_fetcher_.reset(); |
| 426 | keyword_suggest_results_.clear(); |
| 427 | default_suggest_results_.clear(); |
| 428 | keyword_navigation_results_.clear(); |
| 429 | default_navigation_results_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 430 | have_suggest_results_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 431 | } |
| 432 | |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 433 | URLFetcher* SearchProvider::CreateSuggestFetcher(int id, |
| 434 | const TemplateURL& provider, |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 435 | const string16& text) { |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 436 | const TemplateURLRef* const suggestions_url = provider.suggestions_url(); |
| 437 | DCHECK(suggestions_url->SupportsReplacement()); |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 438 | URLFetcher* fetcher = URLFetcher::Create(id, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 439 | GURL(suggestions_url->ReplaceSearchTerms( |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 440 | provider, text, |
[email protected] | 400b133f | 2011-01-19 18:32:30 | [diff] [blame] | 441 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 442 | URLFetcher::GET, this); |
| 443 | fetcher->set_request_context(profile_->GetRequestContext()); |
| 444 | fetcher->Start(); |
| 445 | return fetcher; |
| 446 | } |
| 447 | |
| 448 | bool SearchProvider::ParseSuggestResults(Value* root_val, |
| 449 | bool is_keyword, |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 450 | const string16& input_text, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 451 | SuggestResults* suggest_results) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | if (!root_val->IsType(Value::TYPE_LIST)) |
| 453 | return false; |
| 454 | ListValue* root_list = static_cast<ListValue*>(root_val); |
| 455 | |
| 456 | Value* query_val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 457 | string16 query_str; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | Value* result_val; |
| 459 | if ((root_list->GetSize() < 2) || !root_list->Get(0, &query_val) || |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 460 | !query_val->GetAsString(&query_str) || |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 461 | (query_str != input_text) || |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 462 | !root_list->Get(1, &result_val) || !result_val->IsType(Value::TYPE_LIST)) |
| 463 | return false; |
| 464 | |
| 465 | ListValue* description_list = NULL; |
| 466 | if (root_list->GetSize() > 2) { |
| 467 | // 3rd element: Description list. |
| 468 | Value* description_val; |
| 469 | if (root_list->Get(2, &description_val) && |
| 470 | description_val->IsType(Value::TYPE_LIST)) |
| 471 | description_list = static_cast<ListValue*>(description_val); |
| 472 | } |
| 473 | |
| 474 | // We don't care about the query URL list (the fourth element in the |
| 475 | // response) for now. |
| 476 | |
| 477 | // Parse optional data in the results from the Suggest server if any. |
| 478 | ListValue* type_list = NULL; |
| 479 | // 5th argument: Optional key-value pairs. |
| 480 | // TODO: We may iterate the 5th+ arguments of the root_list if any other |
| 481 | // optional data are defined. |
| 482 | if (root_list->GetSize() > 4) { |
| 483 | Value* optional_val; |
| 484 | if (root_list->Get(4, &optional_val) && |
| 485 | optional_val->IsType(Value::TYPE_DICTIONARY)) { |
| 486 | DictionaryValue* dict_val = static_cast<DictionaryValue*>(optional_val); |
| 487 | |
| 488 | // Parse Google Suggest specific type extension. |
[email protected] | a65175d | 2010-08-17 04:00:57 | [diff] [blame] | 489 | static const std::string kGoogleSuggestType("google:suggesttype"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 490 | if (dict_val->HasKey(kGoogleSuggestType)) |
| 491 | dict_val->GetList(kGoogleSuggestType, &type_list); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | ListValue* result_list = static_cast<ListValue*>(result_val); |
| 496 | for (size_t i = 0; i < result_list->GetSize(); ++i) { |
| 497 | Value* suggestion_val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 498 | string16 suggestion_str; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 499 | if (!result_list->Get(i, &suggestion_val) || |
| 500 | !suggestion_val->GetAsString(&suggestion_str)) |
| 501 | return false; |
| 502 | |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 503 | // Google search may return empty suggestions for weird input characters, |
| 504 | // they make no sense at all and can cause problem in our code. |
| 505 | // See http://crbug.com/56214 |
| 506 | if (!suggestion_str.length()) |
| 507 | continue; |
| 508 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 509 | Value* type_val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 510 | std::string type_str; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 511 | if (type_list && type_list->Get(i, &type_val) && |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 512 | type_val->GetAsString(&type_str) && (type_str == "NAVIGATION")) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | Value* site_val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 514 | string16 site_name; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 515 | NavigationResults& navigation_results = |
| 516 | is_keyword ? keyword_navigation_results_ : |
| 517 | default_navigation_results_; |
[email protected] | 0be9b61 | 2010-05-18 01:13:41 | [diff] [blame] | 518 | if ((navigation_results.size() < kMaxMatches) && |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 519 | description_list && description_list->Get(i, &site_val) && |
| 520 | site_val->IsType(Value::TYPE_STRING) && |
| 521 | site_val->GetAsString(&site_name)) { |
[email protected] | 16afe22 | 2009-01-08 18:57:45 | [diff] [blame] | 522 | // 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] | 523 | GURL result_url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion_str), |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 524 | std::string())); |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 525 | if (result_url.is_valid()) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 526 | navigation_results.push_back(NavigationResult(result_url, site_name)); |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 527 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 528 | } |
| 529 | } else { |
| 530 | // TODO(kochi): Currently we treat a calculator result as a query, but it |
| 531 | // is better to have better presentation for caluculator results. |
[email protected] | 0be9b61 | 2010-05-18 01:13:41 | [diff] [blame] | 532 | if (suggest_results->size() < kMaxMatches) |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 533 | suggest_results->push_back(suggestion_str); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 537 | return true; |
| 538 | } |
| 539 | |
| 540 | void SearchProvider::ConvertResultsToAutocompleteMatches() { |
| 541 | // Convert all the results to matches and add them to a map, so we can keep |
| 542 | // the most relevant match for each result. |
| 543 | MatchMap map; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 544 | const Time no_time; |
| 545 | int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 546 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 547 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 548 | // Keyword what you typed results are handled by the KeywordProvider. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 549 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 550 | int did_not_accept_default_suggestion = default_suggest_results_.empty() ? |
| 551 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 552 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
| 553 | if (providers_.valid_default_provider()) { |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 554 | AddMatchToMap(input_.text(), input_.text(), |
| 555 | CalculateRelevanceForWhatYouTyped(), |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 556 | AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 557 | did_not_accept_default_suggestion, false, |
[email protected] | e0b231d | 2011-05-09 21:26:42 | [diff] [blame] | 558 | input_.prevent_inline_autocomplete(), &map); |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 559 | if (!default_provider_suggest_text_.empty()) { |
| 560 | AddMatchToMap(input_.text() + default_provider_suggest_text_, |
| 561 | input_.text(), CalculateRelevanceForWhatYouTyped() + 1, |
| 562 | AutocompleteMatch::SEARCH_SUGGEST, |
| 563 | did_not_accept_default_suggestion, false, |
[email protected] | e0b231d | 2011-05-09 21:26:42 | [diff] [blame] | 564 | input_.prevent_inline_autocomplete(), &map); |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 565 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 566 | } |
| 567 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 568 | AddHistoryResultsToMap(keyword_history_results_, true, |
| 569 | did_not_accept_keyword_suggestion, &map); |
| 570 | AddHistoryResultsToMap(default_history_results_, false, |
| 571 | did_not_accept_default_suggestion, &map); |
| 572 | |
| 573 | AddSuggestResultsToMap(keyword_suggest_results_, true, |
| 574 | did_not_accept_keyword_suggestion, &map); |
| 575 | AddSuggestResultsToMap(default_suggest_results_, false, |
| 576 | did_not_accept_default_suggestion, &map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 577 | |
| 578 | // Now add the most relevant matches from the map to |matches_|. |
| 579 | matches_.clear(); |
| 580 | for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |
| 581 | matches_.push_back(i->second); |
| 582 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 583 | AddNavigationResultsToMatches(keyword_navigation_results_, true); |
| 584 | AddNavigationResultsToMatches(default_navigation_results_, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 585 | |
[email protected] | 0be9b61 | 2010-05-18 01:13:41 | [diff] [blame] | 586 | const size_t max_total_matches = kMaxMatches + 1; // 1 for "what you typed" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 587 | std::partial_sort(matches_.begin(), |
| 588 | matches_.begin() + std::min(max_total_matches, matches_.size()), |
| 589 | matches_.end(), &AutocompleteMatch::MoreRelevant); |
| 590 | if (matches_.size() > max_total_matches) |
[email protected] | a28e9566 | 2008-11-12 19:19:02 | [diff] [blame] | 591 | matches_.erase(matches_.begin() + max_total_matches, matches_.end()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 592 | |
[email protected] | cc63dea | 2008-08-21 20:56:31 | [diff] [blame] | 593 | UpdateStarredStateOfMatches(); |
| 594 | |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 595 | UpdateDone(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void SearchProvider::AddNavigationResultsToMatches( |
| 599 | const NavigationResults& navigation_results, |
| 600 | bool is_keyword) { |
| 601 | if (!navigation_results.empty()) { |
| 602 | // TODO(kochi): http://b/1170574 We add only one results for navigational |
| 603 | // suggestions. If we can get more useful information about the score, |
| 604 | // consider adding more results. |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 605 | const size_t num_results = is_keyword ? |
| 606 | keyword_navigation_results_.size() : default_navigation_results_.size(); |
| 607 | matches_.push_back(NavigationToMatch(navigation_results.front(), |
| 608 | CalculateRelevanceForNavigation(num_results, 0, is_keyword), |
| 609 | is_keyword)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results, |
| 614 | bool is_keyword, |
| 615 | int did_not_accept_suggestion, |
| 616 | MatchMap* map) { |
[email protected] | ab49320e | 2011-01-06 22:45:15 | [diff] [blame] | 617 | int last_relevance = 0; |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 618 | AutocompleteClassifier* classifier = profile_->GetAutocompleteClassifier(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 619 | for (HistoryResults::const_iterator i(results.begin()); i != results.end(); |
| 620 | ++i) { |
[email protected] | ab49320e | 2011-01-06 22:45:15 | [diff] [blame] | 621 | // History returns results sorted for us. We force the relevance to decrease |
| 622 | // so that the sort from history is honored. We should never end up with a |
| 623 | // match having a relevance greater than the previous, but they might be |
| 624 | // equal. If we didn't force the relevance to decrease and we ended up in a |
| 625 | // situation where the relevance was equal, then which was shown first would |
| 626 | // be random. |
| 627 | // This uses >= to handle the case where 3 or more results have the same |
| 628 | // relevance. |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 629 | bool term_looks_like_url = false; |
| 630 | // Don't autocomplete search terms that would normally be treated as URLs |
| 631 | // when typed. For example, if the user searched for google.com and types |
| 632 | // goog, don't autocomplete to the search term google.com. Otherwise, the |
| 633 | // input will look like a URL but act like a search, which is confusing. |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 634 | // NOTE: We don't check this in the following cases: |
| 635 | // * When inline autocomplete is disabled, we won't be inline |
| 636 | // autocompleting this term, so we don't need to worry about confusion as |
| 637 | // much. This also prevents calling Classify() again from inside the |
| 638 | // classifier (which will corrupt state and likely crash), since the |
| 639 | // classifier always disabled inline autocomplete. |
| 640 | // * When the user has typed the whole term, the "what you typed" history |
| 641 | // match will outrank us for URL-like inputs anyway, so we need not do |
| 642 | // anything special. |
| 643 | if (!input_.prevent_inline_autocomplete() && classifier && |
| 644 | i->term != input_.text()) { |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 645 | AutocompleteMatch match; |
[email protected] | 72874a8d | 2011-05-11 03:48:54 | [diff] [blame] | 646 | classifier->Classify(i->term, string16(), false, false, &match, NULL); |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 647 | term_looks_like_url = match.transition == PageTransition::TYPED; |
| 648 | } |
| 649 | int relevance = CalculateRelevanceForHistory(i->time, term_looks_like_url, |
| 650 | is_keyword); |
[email protected] | ab49320e | 2011-01-06 22:45:15 | [diff] [blame] | 651 | if (i != results.begin() && relevance >= last_relevance) |
| 652 | relevance = last_relevance - 1; |
| 653 | last_relevance = relevance; |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 654 | AddMatchToMap(i->term, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 655 | is_keyword ? keyword_input_text_ : input_.text(), |
[email protected] | ab49320e | 2011-01-06 22:45:15 | [diff] [blame] | 656 | relevance, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 657 | AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion, |
[email protected] | e0b231d | 2011-05-09 21:26:42 | [diff] [blame] | 658 | is_keyword, input_.prevent_inline_autocomplete(), |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 659 | map); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
| 663 | void SearchProvider::AddSuggestResultsToMap( |
| 664 | const SuggestResults& suggest_results, |
| 665 | bool is_keyword, |
| 666 | int did_not_accept_suggestion, |
| 667 | MatchMap* map) { |
| 668 | for (size_t i = 0; i < suggest_results.size(); ++i) { |
| 669 | AddMatchToMap(suggest_results[i], |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 670 | is_keyword ? keyword_input_text_ : input_.text(), |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 671 | CalculateRelevanceForSuggestion(suggest_results.size(), i, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 672 | is_keyword), |
| 673 | AutocompleteMatch::SEARCH_SUGGEST, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 674 | static_cast<int>(i), is_keyword, |
[email protected] | e0b231d | 2011-05-09 21:26:42 | [diff] [blame] | 675 | input_.prevent_inline_autocomplete(), map); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 676 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | int SearchProvider::CalculateRelevanceForWhatYouTyped() const { |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 680 | if (providers_.valid_keyword_provider()) |
| 681 | return 250; |
| 682 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 683 | switch (input_.type()) { |
| 684 | case AutocompleteInput::UNKNOWN: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 685 | case AutocompleteInput::QUERY: |
| 686 | case AutocompleteInput::FORCED_QUERY: |
| 687 | return 1300; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 688 | |
| 689 | case AutocompleteInput::REQUESTED_URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 690 | return 1150; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 691 | |
| 692 | case AutocompleteInput::URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 693 | return 850; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 694 | |
| 695 | default: |
| 696 | NOTREACHED(); |
| 697 | return 0; |
| 698 | } |
| 699 | } |
| 700 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 701 | int SearchProvider::CalculateRelevanceForHistory(const Time& time, |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 702 | bool looks_like_url, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 703 | bool is_keyword) const { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 704 | // The relevance of past searches falls off over time. There are two distinct |
| 705 | // equations used. If the first equation is used (searches to the primary |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 706 | // provider with a type other than URL that don't autocomplete to a url) the |
| 707 | // score starts at 1399 and falls to 1300. If the second equation is used the |
| 708 | // relevance of a search 15 minutes ago is discounted about 50 points, while |
| 709 | // the relevance of a search two weeks ago is discounted about 450 points. |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 710 | double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.); |
| 711 | |
| 712 | if (providers_.is_primary_provider(is_keyword) && |
[email protected] | bdca9beb | 2010-11-15 22:56:19 | [diff] [blame] | 713 | input_.type() != AutocompleteInput::URL && |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 714 | !input_.prevent_inline_autocomplete() && !looks_like_url) { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 715 | // Searches with the past two days get a different curve. |
| 716 | const double autocomplete_time= 2 * 24 * 60 * 60; |
| 717 | if (elapsed_time < autocomplete_time) { |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame^] | 718 | return (is_keyword ? 1599 : 1399) - static_cast<int>(99 * |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 719 | std::pow(elapsed_time / autocomplete_time, 2.5)); |
| 720 | } |
| 721 | elapsed_time -= autocomplete_time; |
| 722 | } |
| 723 | |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 724 | const int score_discount = |
| 725 | static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 726 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 727 | // Don't let scores go below 0. Negative relevance scores are meaningful in |
| 728 | // a different way. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 729 | int base_score; |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 730 | if (!providers_.is_primary_provider(is_keyword)) |
| 731 | base_score = 200; |
| 732 | else |
| 733 | base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 734 | return std::max(0, base_score - score_discount); |
| 735 | } |
| 736 | |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 737 | int SearchProvider::CalculateRelevanceForSuggestion(size_t num_results, |
| 738 | size_t result_number, |
| 739 | bool is_keyword) const { |
| 740 | DCHECK(result_number < num_results); |
| 741 | int base_score; |
| 742 | if (!providers_.is_primary_provider(is_keyword)) |
| 743 | base_score = 100; |
| 744 | else |
| 745 | base_score = (input_.type() == AutocompleteInput::URL) ? 300 : 600; |
| 746 | return base_score + |
| 747 | static_cast<int>(num_results - 1 - result_number); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 748 | } |
| 749 | |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 750 | int SearchProvider::CalculateRelevanceForNavigation(size_t num_results, |
| 751 | size_t result_number, |
| 752 | bool is_keyword) const { |
| 753 | DCHECK(result_number < num_results); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 754 | // TODO(kochi): http://b/784900 Use relevance score from the NavSuggest |
| 755 | // server if possible. |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 756 | return (providers_.is_primary_provider(is_keyword) ? 800 : 150) + |
| 757 | static_cast<int>(num_results - 1 - result_number); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 758 | } |
| 759 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 760 | void SearchProvider::AddMatchToMap(const string16& query_string, |
| 761 | const string16& input_text, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 762 | int relevance, |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 763 | AutocompleteMatch::Type type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 764 | int accepted_suggestion, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 765 | bool is_keyword, |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 766 | bool prevent_inline_autocomplete, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 767 | MatchMap* map) { |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 768 | AutocompleteMatch match(this, relevance, false, type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 769 | std::vector<size_t> content_param_offsets; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 770 | const TemplateURL& provider = is_keyword ? providers_.keyword_provider() : |
| 771 | providers_.default_provider(); |
[email protected] | 0385fc68 | 2011-07-07 19:36:17 | [diff] [blame] | 772 | match.template_url = &provider; |
[email protected] | 7083326 | 2011-01-05 23:40:44 | [diff] [blame] | 773 | match.contents.assign(query_string); |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 774 | // We do intra-string highlighting for suggestions - the suggested segment |
| 775 | // will be highlighted, e.g. for input_text = "you" the suggestion may be |
| 776 | // "youtube", so we'll bold the "tube" section: you*tube*. |
| 777 | if (input_text != query_string) { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 778 | size_t input_position = match.contents.find(input_text); |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 779 | if (input_position == string16::npos) { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 780 | // The input text is not a substring of the query string, e.g. input |
| 781 | // text is "slasdot" and the query string is "slashdot", so we bold the |
| 782 | // whole thing. |
| 783 | match.contents_class.push_back( |
| 784 | ACMatchClassification(0, ACMatchClassification::MATCH)); |
[email protected] | ec237916 | 2009-06-09 23:58:17 | [diff] [blame] | 785 | } else { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 786 | // TODO(beng): ACMatchClassification::MATCH now seems to just mean |
| 787 | // "bold" this. Consider modifying the terminology. |
| 788 | // We don't iterate over the string here annotating all matches because |
| 789 | // it looks odd to have every occurrence of a substring that may be as |
| 790 | // short as a single character highlighted in a query suggestion result, |
| 791 | // e.g. for input text "s" and query string "southwest airlines", it |
| 792 | // looks odd if both the first and last s are highlighted. |
| 793 | if (input_position != 0) { |
| 794 | match.contents_class.push_back( |
| 795 | ACMatchClassification(0, ACMatchClassification::NONE)); |
| 796 | } |
| 797 | match.contents_class.push_back( |
| 798 | ACMatchClassification(input_position, ACMatchClassification::DIM)); |
| 799 | size_t next_fragment_position = input_position + input_text.length(); |
| 800 | if (next_fragment_position < query_string.length()) { |
| 801 | match.contents_class.push_back( |
| 802 | ACMatchClassification(next_fragment_position, |
| 803 | ACMatchClassification::NONE)); |
| 804 | } |
[email protected] | ec237916 | 2009-06-09 23:58:17 | [diff] [blame] | 805 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 806 | } else { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 807 | // Otherwise, we're dealing with the "default search" result which has no |
[email protected] | 7083326 | 2011-01-05 23:40:44 | [diff] [blame] | 808 | // completion. |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 809 | match.contents_class.push_back( |
| 810 | ACMatchClassification(0, ACMatchClassification::NONE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | // When the user forced a query, we need to make sure all the fill_into_edit |
| 814 | // values preserve that property. Otherwise, if the user starts editing a |
| 815 | // suggestion, non-Search results will suddenly appear. |
| 816 | size_t search_start = 0; |
| 817 | if (input_.type() == AutocompleteInput::FORCED_QUERY) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 818 | match.fill_into_edit.assign(ASCIIToUTF16("?")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 819 | ++search_start; |
| 820 | } |
[email protected] | c0048b4 | 2009-05-04 21:47:17 | [diff] [blame] | 821 | if (is_keyword) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 822 | match.fill_into_edit.append( |
| 823 | providers_.keyword_provider().keyword() + char16(' ')); |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame^] | 824 | search_start += providers_.keyword_provider().keyword().size() + 1; |
[email protected] | c0048b4 | 2009-05-04 21:47:17 | [diff] [blame] | 825 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 826 | match.fill_into_edit.append(query_string); |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 827 | // Not all suggestions start with the original input. |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 828 | if (!prevent_inline_autocomplete && |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 829 | !match.fill_into_edit.compare(search_start, input_text.length(), |
| 830 | input_text)) |
| 831 | match.inline_autocomplete_offset = search_start + input_text.length(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 832 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 833 | const TemplateURLRef* const search_url = provider.url(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 834 | DCHECK(search_url->SupportsReplacement()); |
[email protected] | 7b9f367 | 2009-06-15 18:31:22 | [diff] [blame] | 835 | match.destination_url = |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 836 | GURL(search_url->ReplaceSearchTerms(provider, |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 837 | query_string, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 838 | accepted_suggestion, |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 839 | input_text)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 840 | |
| 841 | // Search results don't look like URLs. |
[email protected] | 0bfc29a | 2009-04-27 16:15:44 | [diff] [blame] | 842 | match.transition = |
| 843 | is_keyword ? PageTransition::KEYWORD : PageTransition::GENERATED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 844 | |
| 845 | // Try to add |match| to |map|. If a match for |query_string| is already in |
| 846 | // |map|, replace it if |match| is more relevant. |
| 847 | // NOTE: Keep this ToLower() call in sync with url_database.cc. |
| 848 | const std::pair<MatchMap::iterator, bool> i = map->insert( |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 849 | std::pair<string16, AutocompleteMatch>( |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 850 | base::i18n::ToLower(query_string), match)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 851 | // NOTE: We purposefully do a direct relevance comparison here instead of |
| 852 | // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added |
| 853 | // first" rather than "items alphabetically first" when the scores are equal. |
| 854 | // The only case this matters is when a user has results with the same score |
| 855 | // that differ only by capitalization; because the history system returns |
| 856 | // results sorted by recency, this means we'll pick the most recent such |
| 857 | // result even if the precision of our relevance score is too low to |
| 858 | // distinguish the two. |
| 859 | if (!i.second && (match.relevance > i.first->second.relevance)) |
| 860 | i.first->second = match; |
| 861 | } |
| 862 | |
| 863 | AutocompleteMatch SearchProvider::NavigationToMatch( |
| 864 | const NavigationResult& navigation, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 865 | int relevance, |
| 866 | bool is_keyword) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 867 | const string16& input_text = |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 868 | is_keyword ? keyword_input_text_ : input_.text(); |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 869 | AutocompleteMatch match(this, relevance, false, |
| 870 | AutocompleteMatch::NAVSUGGEST); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 871 | match.destination_url = navigation.url; |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 872 | match.contents = |
| 873 | StringForURLDisplay(navigation.url, true, !HasHTTPScheme(input_text)); |
[email protected] | 0385fc68 | 2011-07-07 19:36:17 | [diff] [blame] | 874 | match.template_url = is_keyword ? &providers_.keyword_provider() : |
| 875 | &providers_.default_provider(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 876 | AutocompleteMatch::ClassifyMatchInString(input_text, match.contents, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 877 | ACMatchClassification::URL, |
| 878 | &match.contents_class); |
| 879 | |
| 880 | match.description = navigation.site_name; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 881 | AutocompleteMatch::ClassifyMatchInString(input_text, navigation.site_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 882 | ACMatchClassification::NONE, |
| 883 | &match.description_class); |
| 884 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 885 | // When the user forced a query, we need to make sure all the fill_into_edit |
| 886 | // values preserve that property. Otherwise, if the user starts editing a |
| 887 | // suggestion, non-Search results will suddenly appear. |
| 888 | if (input_.type() == AutocompleteInput::FORCED_QUERY) |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 889 | match.fill_into_edit.assign(ASCIIToUTF16("?")); |
[email protected] | 79845ef | 2010-06-02 02:37:40 | [diff] [blame] | 890 | match.fill_into_edit.append( |
| 891 | AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url, |
| 892 | match.contents)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 893 | // TODO(pkasting): http://b/1112879 These should perhaps be |
| 894 | // inline-autocompletable? |
| 895 | |
| 896 | return match; |
| 897 | } |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 898 | |
| 899 | void SearchProvider::UpdateDone() { |
| 900 | // We're done when there are no more suggest queries pending (this is set to 1 |
| 901 | // when the timer is started) and we're not waiting on instant. |
| 902 | done_ = ((suggest_results_pending_ == 0) && |
| 903 | (instant_finalized_ || !InstantController::IsEnabled(profile_))); |
| 904 | } |