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