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