[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 11 | #include "base/i18n/break_iterator.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 12 | #include "base/i18n/case_conversion.h" |
[email protected] | d6e58c6e | 2009-10-10 20:40:50 | [diff] [blame] | 13 | #include "base/i18n/icu_string_conversions.h" |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 14 | #include "base/json/json_string_value_serializer.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/message_loop.h" |
[email protected] | f5b95ba9 | 2012-03-27 14:05:19 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 17 | #include "base/string16.h" |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 18 | #include "base/utf_string_conversions.h" |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 19 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
[email protected] | f5b95ba9 | 2012-03-27 14:05:19 | [diff] [blame] | 20 | #include "chrome/browser/autocomplete/autocomplete_field_trial.h" |
[email protected] | 9ac4009 | 2010-10-27 23:05:26 | [diff] [blame] | 21 | #include "chrome/browser/autocomplete/autocomplete_match.h" |
[email protected] | 2c812ba0 | 2011-07-14 00:23:15 | [diff] [blame] | 22 | #include "chrome/browser/autocomplete/keyword_provider.h" |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 23 | #include "chrome/browser/history/history.h" |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 24 | #include "chrome/browser/history/in_memory_database.h" |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 25 | #include "chrome/browser/instant/instant_controller.h" |
[email protected] | f870a32 | 2009-01-16 21:47:27 | [diff] [blame] | 26 | #include "chrome/browser/net/url_fixer_upper.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 27 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 28 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a0ad93ea | 2012-05-07 22:11:53 | [diff] [blame] | 29 | #include "chrome/browser/search_engines/search_engine_type.h" |
[email protected] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 30 | #include "chrome/browser/search_engines/template_url_service.h" |
| 31 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | #include "chrome/common/pref_names.h" |
[email protected] | dcf7d35 | 2009-02-26 01:56:02 | [diff] [blame] | 33 | #include "chrome/common/url_constants.h" |
[email protected] | 36aea270 | 2011-10-26 01:12:22 | [diff] [blame] | 34 | #include "content/public/common/url_fetcher.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | #include "googleurl/src/url_util.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 36 | #include "grit/generated_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | #include "net/base/escape.h" |
[email protected] | d3cf8682f0 | 2012-02-29 23:29:34 | [diff] [blame] | 38 | #include "net/base/load_flags.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 39 | #include "net/http/http_response_headers.h" |
| 40 | #include "net/url_request/url_request_status.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 41 | #include "ui/base/l10n/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 43 | using base::Time; |
| 44 | using base::TimeDelta; |
| 45 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | bool HasMultipleWords(const string16& text) { |
| 49 | base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD); |
| 50 | bool found_word = false; |
| 51 | if (i.Init()) { |
| 52 | while (i.Advance()) { |
| 53 | if (i.IsWord()) { |
| 54 | if (found_word) |
| 55 | return true; |
| 56 | found_word = true; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | }; |
| 64 | |
[email protected] | 033f342 | 2012-03-13 21:24:18 | [diff] [blame] | 65 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 66 | // SearchProvider::Providers -------------------------------------------------- |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 67 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 68 | SearchProvider::Providers::Providers(TemplateURLService* template_url_service) |
| 69 | : template_url_service_(template_url_service) { |
| 70 | } |
| 71 | |
| 72 | const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const { |
| 73 | return default_provider_.empty() ? NULL : |
| 74 | template_url_service_->GetTemplateURLForKeyword(default_provider_); |
| 75 | } |
| 76 | |
| 77 | const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const { |
| 78 | return keyword_provider_.empty() ? NULL : |
| 79 | template_url_service_->GetTemplateURLForKeyword(keyword_provider_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 82 | |
| 83 | // SearchProvider ------------------------------------------------------------- |
| 84 | |
| 85 | // static |
| 86 | const int SearchProvider::kDefaultProviderURLFetcherID = 1; |
| 87 | // static |
| 88 | const int SearchProvider::kKeywordProviderURLFetcherID = 2; |
| 89 | // static |
| 90 | bool SearchProvider::query_suggest_immediately_ = false; |
| 91 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 92 | SearchProvider::SearchProvider(ACProviderListener* listener, Profile* profile) |
| 93 | : AutocompleteProvider(listener, profile, "Search"), |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 94 | providers_(TemplateURLServiceFactory::GetForProfile(profile)), |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 95 | suggest_results_pending_(0), |
[email protected] | 8e5cc28 | 2010-12-05 18:11:39 | [diff] [blame] | 96 | have_suggest_results_(false), |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 97 | instant_finalized_(false) { |
[email protected] | f5b95ba9 | 2012-03-27 14:05:19 | [diff] [blame] | 98 | // We use GetSuggestNumberOfGroups() as the group ID to mean "not in field |
| 99 | // trial." Field trial groups run from 0 to GetSuggestNumberOfGroups() - 1 |
| 100 | // (inclusive). |
| 101 | int suggest_field_trial_group_number = |
| 102 | AutocompleteFieldTrial::GetSuggestNumberOfGroups(); |
| 103 | if (AutocompleteFieldTrial::InSuggestFieldTrial()) { |
| 104 | suggest_field_trial_group_number = |
| 105 | AutocompleteFieldTrial::GetSuggestGroupNameAsNumber(); |
| 106 | } |
| 107 | // Add a beacon to the logs that'll allow us to identify later what |
| 108 | // suggest field trial group a user is in. Do this by incrementing a |
| 109 | // bucket in a histogram, where the bucket represents the user's |
| 110 | // suggest group id. |
| 111 | UMA_HISTOGRAM_ENUMERATION( |
| 112 | "Omnibox.SuggestFieldTrialBeacon", |
| 113 | suggest_field_trial_group_number, |
| 114 | AutocompleteFieldTrial::GetSuggestNumberOfGroups() + 1); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 117 | void SearchProvider::FinalizeInstantQuery(const string16& input_text, |
| 118 | const string16& suggest_text) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 119 | if (done_ || instant_finalized_) |
| 120 | return; |
| 121 | |
| 122 | instant_finalized_ = true; |
| 123 | UpdateDone(); |
| 124 | |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 125 | if (input_text.empty()) { |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 126 | // We only need to update the listener if we're actually done. |
| 127 | if (done_) |
| 128 | listener_->OnProviderUpdate(false); |
| 129 | return; |
| 130 | } |
| 131 | |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 132 | default_provider_suggest_text_ = suggest_text; |
| 133 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 134 | string16 adjusted_input_text(input_text); |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 135 | AutocompleteInput::RemoveForcedQueryStringIfNecessary(input_.type(), |
| 136 | &adjusted_input_text); |
| 137 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 138 | const string16 text = adjusted_input_text + suggest_text; |
[email protected] | 9acdcdc0 | 2012-05-21 20:58:36 | [diff] [blame] | 139 | bool results_updated = false; |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 140 | // Remove any matches that are identical to |text|. We don't use the |
| 141 | // destination_url for comparison as it varies depending upon the index passed |
| 142 | // to TemplateURL::ReplaceSearchTerms. |
| 143 | for (ACMatches::iterator i = matches_.begin(); i != matches_.end();) { |
| 144 | if (((i->type == AutocompleteMatch::SEARCH_HISTORY) || |
| 145 | (i->type == AutocompleteMatch::SEARCH_SUGGEST)) && |
| 146 | (i->fill_into_edit == text)) { |
[email protected] | e030de6 | 2010-11-24 05:41:19 | [diff] [blame] | 147 | i = matches_.erase(i); |
[email protected] | 9acdcdc0 | 2012-05-21 20:58:36 | [diff] [blame] | 148 | results_updated = true; |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 149 | } else { |
| 150 | ++i; |
| 151 | } |
| 152 | } |
| 153 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 154 | // Add the new instant suggest result. We give it a rank higher than |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 155 | // SEARCH_WHAT_YOU_TYPED so that it gets autocompleted. |
| 156 | int did_not_accept_default_suggestion = default_suggest_results_.empty() ? |
| 157 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 158 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
| 159 | MatchMap match_map; |
[email protected] | e918c11 | 2010-12-08 23:03:49 | [diff] [blame] | 160 | AddMatchToMap(text, adjusted_input_text, |
| 161 | CalculateRelevanceForWhatYouTyped() + 1, |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 162 | AutocompleteMatch::SEARCH_SUGGEST, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 163 | did_not_accept_default_suggestion, false, &match_map); |
[email protected] | 9acdcdc0 | 2012-05-21 20:58:36 | [diff] [blame] | 164 | if (!match_map.empty()) { |
| 165 | matches_.push_back(match_map.begin()->second); |
| 166 | results_updated = true; |
| 167 | } |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 168 | |
[email protected] | 9acdcdc0 | 2012-05-21 20:58:36 | [diff] [blame] | 169 | if (results_updated || done_) |
| 170 | listener_->OnProviderUpdate(results_updated); |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 171 | } |
| 172 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 173 | void SearchProvider::Start(const AutocompleteInput& input, |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 174 | bool minimal_changes) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 175 | matches_.clear(); |
| 176 | |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 177 | instant_finalized_ = |
| 178 | (input.matches_requested() != AutocompleteInput::ALL_MATCHES); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 179 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 180 | // Can't return search/suggest results for bogus input or without a profile. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | if (!profile_ || (input.type() == AutocompleteInput::INVALID)) { |
| 182 | Stop(); |
| 183 | return; |
| 184 | } |
| 185 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 186 | keyword_input_text_.clear(); |
| 187 | const TemplateURL* keyword_provider = |
| 188 | KeywordProvider::GetSubstitutingTemplateURLForInput(profile_, input, |
| 189 | &keyword_input_text_); |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 190 | if (keyword_input_text_.empty()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 191 | keyword_provider = NULL; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 192 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 193 | TemplateURLService* model = providers_.template_url_service(); |
| 194 | DCHECK(model); |
| 195 | model->Load(); |
| 196 | const TemplateURL* default_provider = model->GetDefaultSearchProvider(); |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 197 | if (default_provider && !default_provider->SupportsReplacement()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 198 | default_provider = NULL; |
| 199 | |
| 200 | if (keyword_provider == default_provider) |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame] | 201 | default_provider = NULL; // No use in querying the same provider twice. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 202 | |
| 203 | if (!default_provider && !keyword_provider) { |
| 204 | // No valid providers. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | Stop(); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // 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] | 210 | // or the providers, abort the query. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 211 | string16 default_provider_keyword(default_provider ? |
| 212 | default_provider->keyword() : string16()); |
| 213 | string16 keyword_provider_keyword(keyword_provider ? |
| 214 | keyword_provider->keyword() : string16()); |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 215 | if (!minimal_changes || |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 216 | !providers_.equal(default_provider_keyword, keyword_provider_keyword)) { |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 217 | if (done_) |
| 218 | default_provider_suggest_text_.clear(); |
| 219 | else |
| 220 | Stop(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 221 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 223 | providers_.set(default_provider_keyword, keyword_provider_keyword); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | |
| 225 | if (input.text().empty()) { |
| 226 | // User typed "?" alone. Give them a placeholder result indicating what |
| 227 | // this syntax does. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 228 | if (default_provider) { |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 229 | AutocompleteMatch match; |
| 230 | match.provider = this; |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 231 | match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 232 | match.contents_class.push_back( |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 233 | ACMatchClassification(0, ACMatchClassification::NONE)); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 234 | match.keyword = providers_.default_provider(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 235 | matches_.push_back(match); |
| 236 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | Stop(); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | input_ = input; |
| 242 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 243 | DoHistoryQuery(minimal_changes); |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 244 | StartOrStopSuggestQuery(minimal_changes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | ConvertResultsToAutocompleteMatches(); |
| 246 | } |
| 247 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 248 | SearchProvider::Result::Result(int relevance) : relevance_(relevance) {} |
| 249 | SearchProvider::Result::~Result() {} |
| 250 | |
| 251 | SearchProvider::SuggestResult::SuggestResult(const string16& suggestion, |
| 252 | int relevance) |
| 253 | : Result(relevance), |
| 254 | suggestion_(suggestion) { |
| 255 | } |
| 256 | |
| 257 | SearchProvider::SuggestResult::~SuggestResult() {} |
| 258 | |
| 259 | SearchProvider::NavigationResult::NavigationResult(const GURL& url, |
| 260 | const string16& description, |
| 261 | int relevance) |
| 262 | : Result(relevance), |
| 263 | url_(url), |
| 264 | description_(description) { |
| 265 | DCHECK(url_.is_valid()); |
| 266 | } |
| 267 | |
| 268 | SearchProvider::NavigationResult::~NavigationResult() {} |
| 269 | |
| 270 | class SearchProvider::CompareScoredResults { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 271 | public: |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 272 | bool operator()(const Result& a, const Result& b) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 273 | // Sort in descending relevance order. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 274 | return a.relevance() > b.relevance(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 275 | } |
| 276 | }; |
| 277 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | void SearchProvider::Run() { |
| 279 | // Start a new request with the current input. |
| 280 | DCHECK(!done_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 281 | suggest_results_pending_ = 0; |
[email protected] | a0ad93ea | 2012-05-07 22:11:53 | [diff] [blame] | 282 | time_suggest_request_sent_ = base::TimeTicks::Now(); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 283 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 284 | if (default_url && !default_url->suggestions_url().empty()) { |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 285 | suggest_results_pending_++; |
[email protected] | 033f342 | 2012-03-13 21:24:18 | [diff] [blame] | 286 | default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 287 | default_url->suggestions_url_ref(), input_.text())); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 288 | } |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 289 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 290 | if (keyword_url && !keyword_url->suggestions_url().empty()) { |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 291 | suggest_results_pending_++; |
| 292 | keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 293 | keyword_url->suggestions_url_ref(), keyword_input_text_)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 294 | } |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 295 | |
| 296 | // Both the above can fail if the providers have been modified or deleted |
| 297 | // since the query began. |
| 298 | if (suggest_results_pending_ == 0) { |
| 299 | UpdateDone(); |
| 300 | // We only need to update the listener if we're actually done. |
| 301 | if (done_) |
| 302 | listener_->OnProviderUpdate(false); |
| 303 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void SearchProvider::Stop() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 307 | StopSuggest(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 308 | ClearResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 309 | done_ = true; |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 310 | default_provider_suggest_text_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | 0e9e878 | 2012-05-15 23:01:51 | [diff] [blame] | 313 | void SearchProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 314 | provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); |
| 315 | metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); |
| 316 | new_entry.set_provider(AsOmniboxEventProviderType()); |
| 317 | new_entry.set_provider_done(done_); |
| 318 | } |
| 319 | |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 320 | void SearchProvider::OnURLFetchComplete(const net::URLFetcher* source) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | DCHECK(!done_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 322 | suggest_results_pending_--; |
[email protected] | 1cb2dac | 2010-03-08 21:49:15 | [diff] [blame] | 323 | DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 324 | const net::HttpResponseHeaders* const response_headers = |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 325 | source->GetResponseHeaders(); |
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 326 | std::string json_data; |
| 327 | source->GetResponseAsString(&json_data); |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 328 | // JSON is supposed to be UTF-8, but some suggest service providers send JSON |
| 329 | // files in non-UTF-8 encodings. The actual encoding is usually specified in |
| 330 | // the Content-Type header field. |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 331 | if (response_headers) { |
| 332 | std::string charset; |
| 333 | if (response_headers->GetCharset(&charset)) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 334 | string16 data_16; |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 335 | // TODO(jungshik): Switch to CodePageToUTF8 after it's added. |
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 336 | if (base::CodepageToUTF16(json_data, charset.c_str(), |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 337 | base::OnStringConversionError::FAIL, |
| 338 | &data_16)) |
| 339 | json_data = UTF16ToUTF8(data_16); |
[email protected] | ec9207d3 | 2008-09-26 00:51:06 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 343 | bool is_keyword = (source == keyword_fetcher_.get()); |
| 344 | SuggestResults* suggest_results = |
| 345 | is_keyword ? &keyword_suggest_results_ : &default_suggest_results_; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 346 | |
[email protected] | 013e9a0 | 2012-05-18 20:27:10 | [diff] [blame] | 347 | const bool request_succeeded = |
| 348 | source->GetStatus().is_success() && source->GetResponseCode() == 200; |
| 349 | if (request_succeeded) { |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 350 | JSONStringValueSerializer deserializer(json_data); |
| 351 | deserializer.set_allow_trailing_comma(true); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 352 | scoped_ptr<Value> root_val(deserializer.Deserialize(NULL, NULL)); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 353 | const string16& input = is_keyword ? keyword_input_text_ : input_.text(); |
| 354 | have_suggest_results_ = root_val.get() && |
| 355 | ParseSuggestResults(root_val.get(), is_keyword, input, suggest_results); |
[email protected] | a0ad93ea | 2012-05-07 22:11:53 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // Record response time for suggest requests sent to Google. We care |
| 359 | // only about the common case: the Google default provider used in |
| 360 | // non-keyword mode. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 361 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 362 | if (!is_keyword && default_url && |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 363 | (default_url->prepopulate_id() == SEARCH_ENGINE_GOOGLE)) { |
[email protected] | 013e9a0 | 2012-05-18 20:27:10 | [diff] [blame] | 364 | const base::TimeDelta elapsed_time = |
| 365 | base::TimeTicks::Now() - time_suggest_request_sent_; |
| 366 | if (request_succeeded) { |
| 367 | UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", |
| 368 | elapsed_time); |
| 369 | } else { |
| 370 | UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", |
| 371 | elapsed_time); |
| 372 | } |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 373 | } |
| 374 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 376 | listener_->OnProviderUpdate(!suggest_results->empty()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 379 | SearchProvider::~SearchProvider() { |
| 380 | } |
| 381 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 382 | void SearchProvider::DoHistoryQuery(bool minimal_changes) { |
| 383 | // The history query results are synchronous, so if minimal_changes is true, |
| 384 | // we still have the last results and don't need to do anything. |
| 385 | if (minimal_changes) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | return; |
| 387 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 388 | keyword_history_results_.clear(); |
| 389 | default_history_results_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 391 | HistoryService* const history_service = |
| 392 | profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 393 | history::URLDatabase* url_db = history_service ? |
| 394 | history_service->InMemoryDatabase() : NULL; |
| 395 | if (!url_db) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | return; |
| 397 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 398 | // Request history for both the keyword and default provider. We grab many |
| 399 | // more matches than we'll ultimately clamp to so that if there are several |
| 400 | // recent multi-word matches who scores are lowered (see |
| 401 | // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring |
| 402 | // matches. Note that this doesn't fix the problem entirely, but merely |
| 403 | // limits it to cases with a very large number of such multi-word matches; for |
| 404 | // now, this seems OK compared with the complexity of a real fix, which would |
| 405 | // require multiple searches and tracking of "single- vs. multi-word" in the |
| 406 | // database. |
| 407 | int num_matches = kMaxMatches * 5; |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 408 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 409 | if (default_url) { |
| 410 | url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(), |
| 411 | num_matches, &default_history_results_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 412 | } |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 413 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 414 | if (keyword_url) { |
| 415 | url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(), |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 416 | keyword_input_text_, num_matches, &keyword_history_results_); |
| 417 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 418 | } |
| 419 | |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 420 | void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 421 | // Don't send any queries to the server until some time has elapsed after |
| 422 | // the last keypress, to avoid flooding the server with requests we are |
| 423 | // likely to end up throwing away anyway. |
[email protected] | 02c3f683 | 2011-11-16 18:37:40 | [diff] [blame] | 424 | const int kQueryDelayMs = 200; |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 425 | |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 426 | if (!IsQuerySuitableForSuggest()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 427 | StopSuggest(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 428 | ClearResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | |
| 432 | // For the minimal_changes case, if we finished the previous query and still |
| 433 | // have its results, or are allowed to keep running it, just do that, rather |
| 434 | // than starting a new query. |
| 435 | if (minimal_changes && |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 436 | (have_suggest_results_ || |
| 437 | (!done_ && |
| 438 | input_.matches_requested() == AutocompleteInput::ALL_MATCHES))) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 439 | return; |
| 440 | |
| 441 | // We can't keep running any previous query, so halt it. |
| 442 | StopSuggest(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 443 | ClearResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | |
| 445 | // 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] | 446 | if (input_.matches_requested() != AutocompleteInput::ALL_MATCHES) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | return; |
| 448 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 449 | // We'll have at least one pending fetch. Set it to 1 now, but the value is |
| 450 | // correctly set in Run. As Run isn't invoked immediately we need to set this |
| 451 | // now, else we won't think we're waiting on results from the server when we |
| 452 | // really are. |
| 453 | suggest_results_pending_ = 1; |
| 454 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 455 | // Kick off a timer that will start the URL fetch if it completes before |
| 456 | // the user types another character. |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 457 | int delay = query_suggest_immediately_ ? 0 : kQueryDelayMs; |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 458 | timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay), this, |
| 459 | &SearchProvider::Run); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | } |
| 461 | |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 462 | bool SearchProvider::IsQuerySuitableForSuggest() const { |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 463 | // Don't run Suggest in incognito mode, if the engine doesn't support it, or |
| 464 | // if the user has disabled it. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 465 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 466 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 467 | if (profile_->IsOffTheRecord() || |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 468 | ((!default_url || default_url->suggestions_url().empty()) && |
| 469 | (!keyword_url || keyword_url->suggestions_url().empty())) || |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 470 | !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)) |
| 471 | return false; |
| 472 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 473 | // 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] | 474 | // isn't sent to the server. |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 475 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 476 | // FORCED_QUERY means the user is explicitly asking us to search for this, so |
| 477 | // we assume it isn't a URL and/or there isn't private data. |
| 478 | if (input_.type() == AutocompleteInput::FORCED_QUERY) |
| 479 | return true; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 480 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 481 | // Next we check the scheme. If this is UNKNOWN/REQUESTED_URL/URL with a |
| 482 | // scheme that isn't http/https/ftp, we shouldn't send it. Sending things |
| 483 | // like file: and data: is both a waste of time and a disclosure of |
| 484 | // potentially private, local data. Other "schemes" may actually be |
| 485 | // usernames, and we don't want to send passwords. If the scheme is OK, we |
| 486 | // still need to check other cases below. If this is QUERY, then the presence |
| 487 | // of these schemes means the user explicitly typed one, and thus this is |
| 488 | // probably a URL that's being entered and happens to currently be invalid -- |
| 489 | // in which case we again want to run our checks below. Other QUERY cases are |
| 490 | // less likely to be URLs and thus we assume we're OK. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 491 | if (!LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpScheme) && |
| 492 | !LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) && |
| 493 | !LowerCaseEqualsASCII(input_.scheme(), chrome::kFtpScheme)) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 494 | return (input_.type() == AutocompleteInput::QUERY); |
| 495 | |
| 496 | // Don't send URLs with usernames, queries or refs. Some of these are |
| 497 | // private, and the Suggest server is unlikely to have any useful results |
| 498 | // for any of them. Also don't send URLs with ports, as we may initially |
| 499 | // think that a username + password is a host + port (and we don't want to |
| 500 | // send usernames/passwords), and even if the port really is a port, the |
| 501 | // server is once again unlikely to have and useful results. |
| 502 | const url_parse::Parsed& parts = input_.parts(); |
| 503 | if (parts.username.is_nonempty() || parts.port.is_nonempty() || |
| 504 | parts.query.is_nonempty() || parts.ref.is_nonempty()) |
| 505 | return false; |
| 506 | |
| 507 | // Don't send anything for https except the hostname. Hostnames are OK |
| 508 | // because they are visible when the TCP connection is established, but the |
| 509 | // specific path may reveal private information. |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 510 | if (LowerCaseEqualsASCII(input_.scheme(), chrome::kHttpsScheme) && |
| 511 | parts.path.is_nonempty()) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 512 | return false; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 513 | |
| 514 | return true; |
| 515 | } |
| 516 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 517 | void SearchProvider::StopSuggest() { |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 518 | suggest_results_pending_ = 0; |
[email protected] | 2d31666 | 2008-09-03 18:18:14 | [diff] [blame] | 519 | timer_.Stop(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 520 | // Stop any in-progress URL fetches. |
| 521 | keyword_fetcher_.reset(); |
| 522 | default_fetcher_.reset(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void SearchProvider::ClearResults() { |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 526 | keyword_suggest_results_.clear(); |
| 527 | default_suggest_results_.clear(); |
| 528 | keyword_navigation_results_.clear(); |
| 529 | default_navigation_results_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 530 | have_suggest_results_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | } |
| 532 | |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 533 | net::URLFetcher* SearchProvider::CreateSuggestFetcher( |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 534 | int id, |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 535 | const TemplateURLRef& suggestions_url, |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 536 | const string16& text) { |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 537 | DCHECK(suggestions_url.SupportsReplacement()); |
[email protected] | d3ec669b | 2012-05-23 07:12:14 | [diff] [blame^] | 538 | net::URLFetcher* fetcher = content::URLFetcher::Create(id, |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 539 | GURL(suggestions_url.ReplaceSearchTerms(text, |
| 540 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), |
[email protected] | d3ec669b | 2012-05-23 07:12:14 | [diff] [blame^] | 541 | net::URLFetcher::GET, this); |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 542 | fetcher->SetRequestContext(profile_->GetRequestContext()); |
[email protected] | d3cf8682f0 | 2012-02-29 23:29:34 | [diff] [blame] | 543 | fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 544 | fetcher->Start(); |
| 545 | return fetcher; |
| 546 | } |
| 547 | |
| 548 | bool SearchProvider::ParseSuggestResults(Value* root_val, |
| 549 | bool is_keyword, |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 550 | const string16& input_text, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 551 | SuggestResults* suggest_results) { |
[email protected] | 0de764e | 2011-08-26 01:54:00 | [diff] [blame] | 552 | if (!root_val->IsType(Value::TYPE_LIST)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 553 | return false; |
[email protected] | 0de764e | 2011-08-26 01:54:00 | [diff] [blame] | 554 | ListValue* root_list = static_cast<ListValue*>(root_val); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 555 | |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 556 | string16 query_str; |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 557 | ListValue* result_list = NULL; |
| 558 | if ((root_list->GetSize() < 2) || !root_list->GetString(0, &query_str) || |
| 559 | (query_str != input_text) || !root_list->GetList(1, &result_list)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 560 | return false; |
| 561 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 562 | // 3rd element: Description list. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 563 | ListValue* description_list = NULL; |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 564 | if (root_list->GetSize() > 2) |
| 565 | root_list->GetList(2, &description_list); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 566 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 567 | // 4th element: Disregard the query URL list for now. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 568 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 569 | // 5th element: Optional key-value pairs from the Suggest server. |
| 570 | DictionaryValue* dict_val = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | ListValue* type_list = NULL; |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 572 | if (root_list->GetSize() > 4 && root_list->GetDictionary(4, &dict_val)) { |
| 573 | // Parse Google Suggest specific type extension. |
| 574 | const std::string kGoogleSuggestType("google:suggesttype"); |
| 575 | dict_val->GetList(kGoogleSuggestType, &type_list); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 576 | } |
| 577 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 578 | // Add the suggestions in reverse order to assist relevance calculation. |
| 579 | for (size_t i = result_list->GetSize(); i > 0; --i) { |
| 580 | size_t current_index = i - 1; |
| 581 | string16 suggestion; |
| 582 | if (!result_list->GetString(current_index, &suggestion)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 583 | return false; |
| 584 | |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 585 | // Google search may return empty suggestions for weird input characters, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 586 | // they make no sense at all and can cause problems in our code. |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 587 | // See http://crbug.com/56214 |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 588 | if (!suggestion.length()) |
[email protected] | 8e81f509 | 2010-09-29 23:19:40 | [diff] [blame] | 589 | continue; |
| 590 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 591 | std::string type; |
| 592 | if (type_list && type_list->GetString(current_index, &type) && |
| 593 | (type == "NAVIGATION")) { |
| 594 | string16 description; |
| 595 | NavigationResults& navigation_results = is_keyword ? |
| 596 | keyword_navigation_results_ : default_navigation_results_; |
| 597 | if ((navigation_results.size() < kMaxMatches) && description_list && |
| 598 | description_list->GetString(current_index, &description)) { |
[email protected] | 16afe22 | 2009-01-08 18:57:45 | [diff] [blame] | 599 | // We can't blindly trust the URL coming from the server to be valid. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 600 | GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion), |
| 601 | std::string())); |
| 602 | if (url.is_valid()) { |
| 603 | // Increment the relevance for successive results to preserve order. |
| 604 | int relevance = CalculateRelevanceForNavigation(is_keyword) + |
| 605 | navigation_results.size(); |
| 606 | navigation_results.push_back( |
| 607 | NavigationResult(url, description, relevance)); |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 608 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 609 | } |
| 610 | } else { |
| 611 | // TODO(kochi): Currently we treat a calculator result as a query, but it |
| 612 | // is better to have better presentation for caluculator results. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 613 | if (suggest_results->size() < kMaxMatches) { |
| 614 | // Increment the relevance for successive results to preserve order. |
| 615 | int relevance = CalculateRelevanceForSuggestion(is_keyword) + |
| 616 | suggest_results->size(); |
| 617 | suggest_results->push_back(SuggestResult(suggestion, relevance)); |
| 618 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 622 | return true; |
| 623 | } |
| 624 | |
| 625 | void SearchProvider::ConvertResultsToAutocompleteMatches() { |
| 626 | // Convert all the results to matches and add them to a map, so we can keep |
| 627 | // the most relevant match for each result. |
| 628 | MatchMap map; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 629 | const Time no_time; |
| 630 | int did_not_accept_keyword_suggestion = keyword_suggest_results_.empty() ? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 631 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 632 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 633 | // Keyword what you typed results are handled by the KeywordProvider. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 634 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 635 | int verbatim_relevance = CalculateRelevanceForWhatYouTyped(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 636 | int did_not_accept_default_suggestion = default_suggest_results_.empty() ? |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 637 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 638 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
| 639 | AddMatchToMap(input_.text(), input_.text(), verbatim_relevance, |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 640 | AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 641 | did_not_accept_default_suggestion, false, &map); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 642 | if (!default_provider_suggest_text_.empty()) { |
| 643 | AddMatchToMap(input_.text() + default_provider_suggest_text_, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 644 | input_.text(), verbatim_relevance + 1, |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 645 | AutocompleteMatch::SEARCH_SUGGEST, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 646 | did_not_accept_default_suggestion, false, &map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 647 | } |
| 648 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 649 | AddHistoryResultsToMap(keyword_history_results_, true, |
| 650 | did_not_accept_keyword_suggestion, &map); |
| 651 | AddHistoryResultsToMap(default_history_results_, false, |
| 652 | did_not_accept_default_suggestion, &map); |
| 653 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 654 | AddSuggestResultsToMap(keyword_suggest_results_, true, &map); |
| 655 | AddSuggestResultsToMap(default_suggest_results_, false, &map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 656 | |
| 657 | // Now add the most relevant matches from the map to |matches_|. |
| 658 | matches_.clear(); |
| 659 | for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |
| 660 | matches_.push_back(i->second); |
| 661 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 662 | AddNavigationResultsToMatches(keyword_navigation_results_, true); |
| 663 | AddNavigationResultsToMatches(default_navigation_results_, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 664 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 665 | // Allow an additional match for "what you typed". |
| 666 | const size_t max_total_matches = kMaxMatches + 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 667 | std::partial_sort(matches_.begin(), |
| 668 | matches_.begin() + std::min(max_total_matches, matches_.size()), |
| 669 | matches_.end(), &AutocompleteMatch::MoreRelevant); |
| 670 | if (matches_.size() > max_total_matches) |
[email protected] | a28e9566 | 2008-11-12 19:19:02 | [diff] [blame] | 671 | matches_.erase(matches_.begin() + max_total_matches, matches_.end()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 672 | |
[email protected] | cc63dea | 2008-08-21 20:56:31 | [diff] [blame] | 673 | UpdateStarredStateOfMatches(); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 674 | UpdateDone(); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | void SearchProvider::AddNavigationResultsToMatches( |
| 678 | const NavigationResults& navigation_results, |
| 679 | bool is_keyword) { |
| 680 | if (!navigation_results.empty()) { |
[email protected] | 6c53584 | 2012-05-15 05:20:55 | [diff] [blame] | 681 | // TODO(kochi|msw): Add more navigational results if they get more |
| 682 | // meaningful relevance values; see http://b/1170574. |
| 683 | NavigationResults::const_iterator result( |
| 684 | std::max_element(navigation_results.begin(), |
| 685 | navigation_results.end(), |
| 686 | CompareScoredResults())); |
| 687 | matches_.push_back(NavigationToMatch(*result, is_keyword)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results, |
| 692 | bool is_keyword, |
| 693 | int did_not_accept_suggestion, |
| 694 | MatchMap* map) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 695 | if (results.empty()) |
| 696 | return; |
| 697 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 698 | bool prevent_inline_autocomplete = |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 699 | (input_.type() == AutocompleteInput::URL) || |
| 700 | input_.prevent_inline_autocomplete(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 701 | const string16& input_text(is_keyword ? keyword_input_text_ : input_.text()); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 702 | bool input_multiple_words = HasMultipleWords(input_text); |
| 703 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 704 | SuggestResults scored_results; |
| 705 | if (!prevent_inline_autocomplete && input_multiple_words) { |
| 706 | // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit |
| 707 | // queries if the input also has multiple words. But if we were already |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 708 | // autocompleting a multi-word, multi-visit query, and the current input is |
| 709 | // still a prefix of it, then changing the autocompletion suddenly feels |
| 710 | // wrong. To detect this case, first score as if only one word has been |
| 711 | // typed, then check for a best result that is an autocompleted, multi-word |
| 712 | // query. If we find one, then just keep that score set. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 713 | scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
| 714 | false, input_text, is_keyword); |
| 715 | if ((scored_results[0].relevance() < |
| 716 | AutocompleteResult::kLowestDefaultScore) || |
| 717 | !HasMultipleWords(scored_results[0].suggestion())) |
| 718 | scored_results.clear(); // Didn't detect the case above, score normally. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 719 | } |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 720 | if (scored_results.empty()) |
| 721 | scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
| 722 | input_multiple_words, input_text, |
| 723 | is_keyword); |
| 724 | for (SuggestResults::const_iterator i(scored_results.begin()); |
| 725 | i != scored_results.end(); ++i) { |
| 726 | AddMatchToMap(i->suggestion(), input_text, i->relevance(), |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 727 | AutocompleteMatch::SEARCH_HISTORY, did_not_accept_suggestion, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 728 | is_keyword, map); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 732 | SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 733 | const HistoryResults& results, |
| 734 | bool base_prevent_inline_autocomplete, |
| 735 | bool input_multiple_words, |
| 736 | const string16& input_text, |
| 737 | bool is_keyword) { |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 738 | AutocompleteClassifier* classifier = profile_->GetAutocompleteClassifier(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 739 | SuggestResults scored_results; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 740 | for (HistoryResults::const_iterator i(results.begin()); i != results.end(); |
| 741 | ++i) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 742 | // Don't autocomplete multi-word queries that have only been seen once |
| 743 | // unless the user has typed more than one word. |
| 744 | bool prevent_inline_autocomplete = base_prevent_inline_autocomplete || |
| 745 | (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term)); |
| 746 | |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 747 | // Don't autocomplete search terms that would normally be treated as URLs |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 748 | // when typed. For example, if the user searched for "google.com" and types |
| 749 | // "goog", don't autocomplete to the search term "google.com". Otherwise, |
| 750 | // the 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] | 751 | // NOTE: We don't check this in the following cases: |
| 752 | // * When inline autocomplete is disabled, we won't be inline |
| 753 | // autocompleting this term, so we don't need to worry about confusion as |
| 754 | // much. This also prevents calling Classify() again from inside the |
| 755 | // classifier (which will corrupt state and likely crash), since the |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 756 | // classifier always disables inline autocomplete. |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 757 | // * When the user has typed the whole term, the "what you typed" history |
| 758 | // match will outrank us for URL-like inputs anyway, so we need not do |
| 759 | // anything special. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 760 | if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) { |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 761 | AutocompleteMatch match; |
[email protected] | 72874a8d | 2011-05-11 03:48:54 | [diff] [blame] | 762 | classifier->Classify(i->term, string16(), false, false, &match, NULL); |
[email protected] | 2905f74 | 2011-10-13 03:51:58 | [diff] [blame] | 763 | prevent_inline_autocomplete = |
| 764 | match.transition == content::PAGE_TRANSITION_TYPED; |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 765 | } |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 766 | |
| 767 | int relevance = CalculateRelevanceForHistory(i->time, is_keyword, |
| 768 | prevent_inline_autocomplete); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 769 | scored_results.push_back(SuggestResult(i->term, relevance)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 770 | } |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 771 | |
| 772 | // History returns results sorted for us. However, we may have docked some |
| 773 | // results' scores, so things are no longer in order. Do a stable sort to get |
| 774 | // things back in order without otherwise disturbing results with equal |
| 775 | // scores, then force the scores to be unique, so that the order in which |
| 776 | // they're shown is deterministic. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 777 | std::stable_sort(scored_results.begin(), scored_results.end(), |
| 778 | CompareScoredResults()); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 779 | int last_relevance = 0; |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 780 | for (SuggestResults::iterator i(scored_results.begin()); |
| 781 | i != scored_results.end(); ++i) { |
| 782 | if ((i != scored_results.begin()) && (i->relevance() >= last_relevance)) |
| 783 | i->set_relevance(last_relevance - 1); |
| 784 | last_relevance = i->relevance(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 785 | } |
| 786 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 787 | return scored_results; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 788 | } |
| 789 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 790 | void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results, |
| 791 | bool is_keyword, |
| 792 | MatchMap* map) { |
| 793 | const string16& text = is_keyword ? keyword_input_text_ : input_.text(); |
| 794 | for (size_t i = 0; i < results.size(); ++i) { |
| 795 | AddMatchToMap(results[i].suggestion(), text, results[i].relevance(), |
| 796 | AutocompleteMatch::SEARCH_SUGGEST, i, is_keyword, map); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 797 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | int SearchProvider::CalculateRelevanceForWhatYouTyped() const { |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 801 | if (!providers_.keyword_provider().empty()) |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 802 | return 250; |
| 803 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 804 | switch (input_.type()) { |
| 805 | case AutocompleteInput::UNKNOWN: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 806 | case AutocompleteInput::QUERY: |
| 807 | case AutocompleteInput::FORCED_QUERY: |
| 808 | return 1300; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 809 | |
| 810 | case AutocompleteInput::REQUESTED_URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 811 | return 1150; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 812 | |
| 813 | case AutocompleteInput::URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 814 | return 850; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 815 | |
| 816 | default: |
| 817 | NOTREACHED(); |
| 818 | return 0; |
| 819 | } |
| 820 | } |
| 821 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 822 | int SearchProvider::CalculateRelevanceForHistory( |
| 823 | const Time& time, |
| 824 | bool is_keyword, |
| 825 | bool prevent_inline_autocomplete) const { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 826 | // The relevance of past searches falls off over time. There are two distinct |
| 827 | // equations used. If the first equation is used (searches to the primary |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 828 | // provider that we want to inline autocomplete), the score starts at 1399 and |
| 829 | // falls to 1300. If the second equation is used the relevance of a search 15 |
| 830 | // minutes ago is discounted 50 points, while the relevance of a search two |
| 831 | // weeks ago is discounted 450 points. |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 832 | double elapsed_time = std::max((Time::Now() - time).InSecondsF(), 0.); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 833 | bool is_primary_provider = providers_.is_primary_provider(is_keyword); |
| 834 | if (is_primary_provider && !prevent_inline_autocomplete) { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 835 | // Searches with the past two days get a different curve. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 836 | const double autocomplete_time = 2 * 24 * 60 * 60; |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 837 | if (elapsed_time < autocomplete_time) { |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame] | 838 | return (is_keyword ? 1599 : 1399) - static_cast<int>(99 * |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 839 | std::pow(elapsed_time / autocomplete_time, 2.5)); |
| 840 | } |
| 841 | elapsed_time -= autocomplete_time; |
| 842 | } |
| 843 | |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 844 | const int score_discount = |
| 845 | static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 846 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 847 | // Don't let scores go below 0. Negative relevance scores are meaningful in |
| 848 | // a different way. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 849 | int base_score; |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 850 | if (is_primary_provider) |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 851 | base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050; |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 852 | else |
| 853 | base_score = 200; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 854 | return std::max(0, base_score - score_discount); |
| 855 | } |
| 856 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 857 | int SearchProvider::CalculateRelevanceForSuggestion(bool for_keyword) const { |
| 858 | return !providers_.is_primary_provider(for_keyword) ? 100 : |
| 859 | ((input_.type() == AutocompleteInput::URL) ? 300 : 600); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 860 | } |
| 861 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 862 | int SearchProvider::CalculateRelevanceForNavigation(bool for_keyword) const { |
| 863 | return providers_.is_primary_provider(for_keyword) ? 800 : 150; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 864 | } |
| 865 | |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 866 | void SearchProvider::AddMatchToMap(const string16& query_string, |
| 867 | const string16& input_text, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 868 | int relevance, |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 869 | AutocompleteMatch::Type type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 870 | int accepted_suggestion, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 871 | bool is_keyword, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 872 | MatchMap* map) { |
[email protected] | 9251368 | 2011-09-01 06:16:52 | [diff] [blame] | 873 | AutocompleteMatch match(this, relevance, false, type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 874 | std::vector<size_t> content_param_offsets; |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 875 | // Bail out now if we don't actually have a valid provider. |
| 876 | match.keyword = is_keyword ? |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 877 | providers_.keyword_provider() : providers_.default_provider(); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 878 | const TemplateURL* provider_url = match.GetTemplateURL(profile_); |
| 879 | if (provider_url == NULL) |
| 880 | return; |
| 881 | |
[email protected] | 7083326 | 2011-01-05 23:40:44 | [diff] [blame] | 882 | match.contents.assign(query_string); |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 883 | // We do intra-string highlighting for suggestions - the suggested segment |
| 884 | // will be highlighted, e.g. for input_text = "you" the suggestion may be |
| 885 | // "youtube", so we'll bold the "tube" section: you*tube*. |
| 886 | if (input_text != query_string) { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 887 | size_t input_position = match.contents.find(input_text); |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 888 | if (input_position == string16::npos) { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 889 | // The input text is not a substring of the query string, e.g. input |
| 890 | // text is "slasdot" and the query string is "slashdot", so we bold the |
| 891 | // whole thing. |
| 892 | match.contents_class.push_back( |
| 893 | ACMatchClassification(0, ACMatchClassification::MATCH)); |
[email protected] | ec237916 | 2009-06-09 23:58:17 | [diff] [blame] | 894 | } else { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 895 | // TODO(beng): ACMatchClassification::MATCH now seems to just mean |
| 896 | // "bold" this. Consider modifying the terminology. |
| 897 | // We don't iterate over the string here annotating all matches because |
| 898 | // it looks odd to have every occurrence of a substring that may be as |
| 899 | // short as a single character highlighted in a query suggestion result, |
| 900 | // e.g. for input text "s" and query string "southwest airlines", it |
| 901 | // looks odd if both the first and last s are highlighted. |
| 902 | if (input_position != 0) { |
| 903 | match.contents_class.push_back( |
| 904 | ACMatchClassification(0, ACMatchClassification::NONE)); |
| 905 | } |
| 906 | match.contents_class.push_back( |
| 907 | ACMatchClassification(input_position, ACMatchClassification::DIM)); |
| 908 | size_t next_fragment_position = input_position + input_text.length(); |
| 909 | if (next_fragment_position < query_string.length()) { |
| 910 | match.contents_class.push_back( |
| 911 | ACMatchClassification(next_fragment_position, |
| 912 | ACMatchClassification::NONE)); |
| 913 | } |
[email protected] | ec237916 | 2009-06-09 23:58:17 | [diff] [blame] | 914 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 915 | } else { |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 916 | // Otherwise, we're dealing with the "default search" result which has no |
[email protected] | 7083326 | 2011-01-05 23:40:44 | [diff] [blame] | 917 | // completion. |
[email protected] | fb5153c5 | 2009-07-31 19:40:33 | [diff] [blame] | 918 | match.contents_class.push_back( |
| 919 | ACMatchClassification(0, ACMatchClassification::NONE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // When the user forced a query, we need to make sure all the fill_into_edit |
| 923 | // values preserve that property. Otherwise, if the user starts editing a |
| 924 | // suggestion, non-Search results will suddenly appear. |
| 925 | size_t search_start = 0; |
| 926 | if (input_.type() == AutocompleteInput::FORCED_QUERY) { |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 927 | match.fill_into_edit.assign(ASCIIToUTF16("?")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 928 | ++search_start; |
| 929 | } |
[email protected] | c0048b4 | 2009-05-04 21:47:17 | [diff] [blame] | 930 | if (is_keyword) { |
[email protected] | 033f342 | 2012-03-13 21:24:18 | [diff] [blame] | 931 | match.fill_into_edit.append(match.keyword + char16(' ')); |
| 932 | search_start += match.keyword.length() + 1; |
[email protected] | c0048b4 | 2009-05-04 21:47:17 | [diff] [blame] | 933 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 934 | match.fill_into_edit.append(query_string); |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 935 | // Not all suggestions start with the original input. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 936 | if (!input_.prevent_inline_autocomplete() && |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 937 | !match.fill_into_edit.compare(search_start, input_text.length(), |
| 938 | input_text)) |
| 939 | match.inline_autocomplete_offset = search_start + input_text.length(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 940 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 941 | const TemplateURLRef& search_url = provider_url->url_ref(); |
[email protected] | 360ba05 | 2012-04-04 17:26:13 | [diff] [blame] | 942 | DCHECK(search_url.SupportsReplacement()); |
[email protected] | 16fca9b8 | 2012-04-23 18:40:26 | [diff] [blame] | 943 | match.destination_url = GURL(search_url.ReplaceSearchTerms(query_string, |
| 944 | accepted_suggestion, input_text)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 945 | |
| 946 | // Search results don't look like URLs. |
[email protected] | 2905f74 | 2011-10-13 03:51:58 | [diff] [blame] | 947 | match.transition = is_keyword ? |
| 948 | content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 949 | |
| 950 | // Try to add |match| to |map|. If a match for |query_string| is already in |
| 951 | // |map|, replace it if |match| is more relevant. |
| 952 | // NOTE: Keep this ToLower() call in sync with url_database.cc. |
| 953 | const std::pair<MatchMap::iterator, bool> i = map->insert( |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 954 | std::pair<string16, AutocompleteMatch>( |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 955 | base::i18n::ToLower(query_string), match)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 956 | // NOTE: We purposefully do a direct relevance comparison here instead of |
| 957 | // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added |
| 958 | // first" rather than "items alphabetically first" when the scores are equal. |
| 959 | // The only case this matters is when a user has results with the same score |
| 960 | // that differ only by capitalization; because the history system returns |
| 961 | // results sorted by recency, this means we'll pick the most recent such |
| 962 | // result even if the precision of our relevance score is too low to |
| 963 | // distinguish the two. |
| 964 | if (!i.second && (match.relevance > i.first->second.relevance)) |
| 965 | i.first->second = match; |
| 966 | } |
| 967 | |
| 968 | AutocompleteMatch SearchProvider::NavigationToMatch( |
| 969 | const NavigationResult& navigation, |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 970 | bool is_keyword) { |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 971 | const string16& input_text = is_keyword ? keyword_input_text_ : input_.text(); |
| 972 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | 4c1fb7ec | 2008-11-13 00:19:00 | [diff] [blame] | 973 | AutocompleteMatch::NAVSUGGEST); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 974 | match.destination_url = navigation.url(); |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 975 | match.contents = |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 976 | StringForURLDisplay(navigation.url(), true, !HasHTTPScheme(input_text)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 977 | AutocompleteMatch::ClassifyMatchInString(input_text, match.contents, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 978 | ACMatchClassification::URL, |
| 979 | &match.contents_class); |
| 980 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 981 | match.description = navigation.description(); |
| 982 | AutocompleteMatch::ClassifyMatchInString(input_text, match.description, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 983 | ACMatchClassification::NONE, |
| 984 | &match.description_class); |
| 985 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 986 | // When the user forced a query, we need to make sure all the fill_into_edit |
| 987 | // values preserve that property. Otherwise, if the user starts editing a |
| 988 | // suggestion, non-Search results will suddenly appear. |
| 989 | if (input_.type() == AutocompleteInput::FORCED_QUERY) |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 990 | match.fill_into_edit.assign(ASCIIToUTF16("?")); |
[email protected] | 79845ef | 2010-06-02 02:37:40 | [diff] [blame] | 991 | match.fill_into_edit.append( |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 992 | AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(), |
[email protected] | 79845ef | 2010-06-02 02:37:40 | [diff] [blame] | 993 | match.contents)); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 994 | // TODO(pkasting|msw): Inline-autocomplete nav results; see http://b/1112879. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 995 | |
| 996 | return match; |
| 997 | } |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 998 | |
| 999 | void SearchProvider::UpdateDone() { |
| 1000 | // We're done when there are no more suggest queries pending (this is set to 1 |
| 1001 | // when the timer is started) and we're not waiting on instant. |
| 1002 | done_ = ((suggest_results_pending_ == 0) && |
| 1003 | (instant_finalized_ || !InstantController::IsEnabled(profile_))); |
| 1004 | } |