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