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