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