[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] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 13 | #include "base/json/json_string_value_serializer.h" |
[email protected] | fa1e0e1 | 2013-07-18 00:10:14 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
[email protected] | f5b95ba9 | 2012-03-27 14:05:19 | [diff] [blame] | 15 | #include "base/metrics/histogram.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 16 | #include "base/prefs/pref_service.h" |
[email protected] | 5889bfb | 2014-03-19 00:26:48 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 19 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 20 | #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
[email protected] | 5af9bc8 | 2012-06-29 00:53:48 | [diff] [blame] | 21 | #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
[email protected] | 73c2b163 | 2012-07-02 22:51:38 | [diff] [blame] | 22 | #include "chrome/browser/autocomplete/autocomplete_result.h" |
[email protected] | 2c812ba0 | 2011-07-14 00:23:15 | [diff] [blame] | 23 | #include "chrome/browser/autocomplete/keyword_provider.h" |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 24 | #include "chrome/browser/autocomplete/url_prefix.h" |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 25 | #include "chrome/browser/google/google_util.h" |
[email protected] | 6a2c09f | 2013-01-25 04:50:07 | [diff] [blame] | 26 | #include "chrome/browser/history/history_service.h" |
[email protected] | 9d2db76 | 2012-06-19 00:01:10 | [diff] [blame] | 27 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 28 | #include "chrome/browser/history/in_memory_database.h" |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 29 | #include "chrome/browser/metrics/variations/variations_http_header_provider.h" |
[email protected] | 684fcc1 | 2013-03-08 22:50:02 | [diff] [blame] | 30 | #include "chrome/browser/omnibox/omnibox_field_trial.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 31 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 32 | #include "chrome/browser/search/search.h" |
[email protected] | 9899a61 | 2012-08-21 23:50:04 | [diff] [blame] | 33 | #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
[email protected] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 34 | #include "chrome/browser/search_engines/template_url_service.h" |
| 35 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
[email protected] | c4a9d4c | 2013-08-10 07:06:27 | [diff] [blame] | 36 | #include "chrome/browser/ui/search/instant_controller.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | #include "chrome/common/pref_names.h" |
[email protected] | dcf7d35 | 2009-02-26 01:56:02 | [diff] [blame] | 38 | #include "chrome/common/url_constants.h" |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 39 | #include "content/public/browser/user_metrics.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 40 | #include "grit/generated_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | #include "net/base/escape.h" |
[email protected] | d3cf8682f0 | 2012-02-29 23:29:34 | [diff] [blame] | 42 | #include "net/base/load_flags.h" |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 43 | #include "net/base/net_util.h" |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 44 | #include "net/http/http_request_headers.h" |
[email protected] | 3dc1bc4 | 2012-06-19 08:20:53 | [diff] [blame] | 45 | #include "net/url_request/url_fetcher.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 46 | #include "net/url_request/url_request_status.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 47 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 48 | #include "url/url_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 50 | // Helpers -------------------------------------------------------------------- |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 51 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 52 | namespace { |
| 53 | |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 54 | // We keep track in a histogram how many suggest requests we send, how |
| 55 | // many suggest requests we invalidate (e.g., due to a user typing |
| 56 | // another character), and how many replies we receive. |
| 57 | // *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! *** |
| 58 | // (excluding the end-of-list enum value) |
| 59 | // We do not want values of existing enums to change or else it screws |
| 60 | // up the statistics. |
| 61 | enum SuggestRequestsHistogramValue { |
| 62 | REQUEST_SENT = 1, |
| 63 | REQUEST_INVALIDATED, |
| 64 | REPLY_RECEIVED, |
| 65 | MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE |
| 66 | }; |
| 67 | |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 68 | // The verbatim score for an input which is not an URL. |
| 69 | const int kNonURLVerbatimRelevance = 1300; |
| 70 | |
[email protected] | 7706a52 | 2012-08-16 17:42:25 | [diff] [blame] | 71 | // Increments the appropriate value in the histogram by one. |
| 72 | void LogOmniboxSuggestRequest( |
| 73 | SuggestRequestsHistogramValue request_value) { |
| 74 | UMA_HISTOGRAM_ENUMERATION("Omnibox.SuggestRequests", request_value, |
| 75 | MAX_SUGGEST_REQUEST_HISTOGRAM_VALUE); |
| 76 | } |
| 77 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 78 | bool HasMultipleWords(const base::string16& text) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 79 | base::i18n::BreakIterator i(text, base::i18n::BreakIterator::BREAK_WORD); |
| 80 | bool found_word = false; |
| 81 | if (i.Init()) { |
| 82 | while (i.Advance()) { |
| 83 | if (i.IsWord()) { |
| 84 | if (found_word) |
| 85 | return true; |
| 86 | found_word = true; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 93 | } // namespace |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 94 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 95 | // SearchProvider::Providers -------------------------------------------------- |
[email protected] | b547666d | 2009-04-23 16:37:58 | [diff] [blame] | 96 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 97 | SearchProvider::Providers::Providers(TemplateURLService* template_url_service) |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 98 | : template_url_service_(template_url_service) {} |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 99 | |
| 100 | const TemplateURL* SearchProvider::Providers::GetDefaultProviderURL() const { |
| 101 | return default_provider_.empty() ? NULL : |
| 102 | template_url_service_->GetTemplateURLForKeyword(default_provider_); |
| 103 | } |
| 104 | |
| 105 | const TemplateURL* SearchProvider::Providers::GetKeywordProviderURL() const { |
| 106 | return keyword_provider_.empty() ? NULL : |
| 107 | template_url_service_->GetTemplateURLForKeyword(keyword_provider_); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 110 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 111 | // SearchProvider::CompareScoredResults --------------------------------------- |
| 112 | |
| 113 | class SearchProvider::CompareScoredResults { |
| 114 | public: |
| 115 | bool operator()(const Result& a, const Result& b) { |
| 116 | // Sort in descending relevance order. |
| 117 | return a.relevance() > b.relevance(); |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 122 | // SearchProvider ------------------------------------------------------------- |
| 123 | |
| 124 | // static |
[email protected] | 24dbf230 | 2013-04-05 16:59:25 | [diff] [blame] | 125 | int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100; |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 126 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 127 | SearchProvider::SearchProvider(AutocompleteProviderListener* listener, |
| 128 | Profile* profile) |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 129 | : BaseSearchProvider(listener, profile, AutocompleteProvider::TYPE_SEARCH), |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 130 | providers_(TemplateURLServiceFactory::GetForProfile(profile)) { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | cb86ee6f | 2013-04-28 16:58:15 | [diff] [blame] | 133 | // static |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 134 | std::string SearchProvider::GetSuggestMetadata(const AutocompleteMatch& match) { |
| 135 | return match.GetAdditionalInfo(kSuggestMetadataKey); |
| 136 | } |
| 137 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 138 | void SearchProvider::ResetSession() { |
| 139 | field_trial_triggered_in_session_ = false; |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 142 | SearchProvider::~SearchProvider() { |
| 143 | } |
| 144 | |
[email protected] | ee6110b | 2014-01-09 22:26:31 | [diff] [blame] | 145 | void SearchProvider::UpdateMatchContentsClass(const base::string16& input_text, |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 146 | Results* results) { |
| 147 | for (SuggestResults::iterator sug_it = results->suggest_results.begin(); |
| 148 | sug_it != results->suggest_results.end(); ++sug_it) { |
[email protected] | ee6110b | 2014-01-09 22:26:31 | [diff] [blame] | 149 | sug_it->ClassifyMatchContents(false, input_text); |
| 150 | } |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 151 | const std::string languages( |
| 152 | profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 153 | for (NavigationResults::iterator nav_it = results->navigation_results.begin(); |
| 154 | nav_it != results->navigation_results.end(); ++nav_it) { |
| 155 | nav_it->CalculateAndClassifyMatchContents(false, input_text, languages); |
| 156 | } |
[email protected] | ee6110b | 2014-01-09 22:26:31 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // static |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 160 | int SearchProvider::CalculateRelevanceForKeywordVerbatim( |
| 161 | AutocompleteInput::Type type, |
| 162 | bool prefer_keyword) { |
| 163 | // This function is responsible for scoring verbatim query matches |
| 164 | // for non-extension keywords. KeywordProvider::CalculateRelevance() |
| 165 | // scores verbatim query matches for extension keywords, as well as |
| 166 | // for keyword matches (i.e., suggestions of a keyword itself, not a |
| 167 | // suggestion of a query on a keyword search engine). These two |
| 168 | // functions are currently in sync, but there's no reason we |
| 169 | // couldn't decide in the future to score verbatim matches |
| 170 | // differently for extension and non-extension keywords. If you |
| 171 | // make such a change, however, you should update this comment to |
| 172 | // describe it, so it's clear why the functions diverge. |
| 173 | if (prefer_keyword) |
| 174 | return 1500; |
| 175 | return (type == AutocompleteInput::QUERY) ? 1450 : 1100; |
| 176 | } |
| 177 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | void SearchProvider::Start(const AutocompleteInput& input, |
[email protected] | 8deeb95 | 2008-10-09 18:21:27 | [diff] [blame] | 179 | bool minimal_changes) { |
[email protected] | 04504c24 | 2013-01-22 21:08:55 | [diff] [blame] | 180 | // Do our best to load the model as early as possible. This will reduce |
| 181 | // odds of having the model not ready when really needed (a non-empty input). |
| 182 | TemplateURLService* model = providers_.template_url_service(); |
| 183 | DCHECK(model); |
| 184 | model->Load(); |
| 185 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | matches_.clear(); |
[email protected] | 618d6e6 | 2012-12-16 05:55:57 | [diff] [blame] | 187 | field_trial_triggered_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 189 | // Can't return search/suggest results for bogus input or without a profile. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | if (!profile_ || (input.type() == AutocompleteInput::INVALID)) { |
[email protected] | 9950e5d5 | 2014-02-25 23:34:19 | [diff] [blame] | 191 | Stop(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 195 | keyword_input_ = input; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 196 | const TemplateURL* keyword_provider = |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 197 | KeywordProvider::GetSubstitutingTemplateURLForInput(model, |
| 198 | &keyword_input_); |
| 199 | if (keyword_provider == NULL) |
| 200 | keyword_input_.Clear(); |
| 201 | else if (keyword_input_.text().empty()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 202 | keyword_provider = NULL; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 203 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 204 | const TemplateURL* default_provider = model->GetDefaultSearchProvider(); |
[email protected] | 9b74ab5 | 2012-03-30 16:08:07 | [diff] [blame] | 205 | if (default_provider && !default_provider->SupportsReplacement()) |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 206 | default_provider = NULL; |
| 207 | |
| 208 | if (keyword_provider == default_provider) |
[email protected] | e17511f | 2011-07-13 14:09:18 | [diff] [blame] | 209 | default_provider = NULL; // No use in querying the same provider twice. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 210 | |
| 211 | if (!default_provider && !keyword_provider) { |
| 212 | // No valid providers. |
[email protected] | 9950e5d5 | 2014-02-25 23:34:19 | [diff] [blame] | 213 | Stop(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | |
| 217 | // 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] | 218 | // or the providers, abort the query. |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 219 | base::string16 default_provider_keyword(default_provider ? |
| 220 | default_provider->keyword() : base::string16()); |
| 221 | base::string16 keyword_provider_keyword(keyword_provider ? |
| 222 | keyword_provider->keyword() : base::string16()); |
[email protected] | 9e78974 | 2011-01-10 23:27:32 | [diff] [blame] | 223 | if (!minimal_changes || |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 224 | !providers_.equal(default_provider_keyword, keyword_provider_keyword)) { |
[email protected] | bb900e0 | 2013-03-14 14:15:29 | [diff] [blame] | 225 | // Cancel any in-flight suggest requests. |
[email protected] | e1290ee6 | 2013-06-26 18:31:15 | [diff] [blame] | 226 | if (!done_) |
[email protected] | e29249dc5 | 2012-07-19 17:33:50 | [diff] [blame] | 227 | Stop(false); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 228 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 230 | providers_.set(default_provider_keyword, keyword_provider_keyword); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | |
| 232 | if (input.text().empty()) { |
| 233 | // User typed "?" alone. Give them a placeholder result indicating what |
| 234 | // this syntax does. |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 235 | if (default_provider) { |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 236 | AutocompleteMatch match; |
| 237 | match.provider = this; |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 238 | match.contents.assign(l10n_util::GetStringUTF16(IDS_EMPTY_KEYWORD_VALUE)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 239 | match.contents_class.push_back( |
[email protected] | 2c33dd2 | 2010-02-11 21:46:35 | [diff] [blame] | 240 | ACMatchClassification(0, ACMatchClassification::NONE)); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 241 | match.keyword = providers_.default_provider(); |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 242 | match.allowed_to_be_default_match = true; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 243 | matches_.push_back(match); |
| 244 | } |
[email protected] | 9950e5d5 | 2014-02-25 23:34:19 | [diff] [blame] | 245 | Stop(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | |
| 249 | input_ = input; |
| 250 | |
[email protected] | e1290ee6 | 2013-06-26 18:31:15 | [diff] [blame] | 251 | DoHistoryQuery(minimal_changes); |
| 252 | StartOrStopSuggestQuery(minimal_changes); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 253 | UpdateMatches(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 254 | } |
| 255 | |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 256 | void SearchProvider::SortResults(bool is_keyword, |
| 257 | const base::ListValue* relevances, |
| 258 | Results* results) { |
| 259 | // Ignore suggested scores for non-keyword matches in keyword mode; if the |
| 260 | // server is allowed to score these, it could interfere with the user's |
| 261 | // ability to get good keyword results. |
| 262 | const bool abandon_suggested_scores = |
| 263 | !is_keyword && !providers_.keyword_provider().empty(); |
| 264 | // Apply calculated relevance scores to suggestions if a valid list was |
| 265 | // not provided or we're abandoning suggested scores entirely. |
| 266 | if ((relevances == NULL) || abandon_suggested_scores) { |
| 267 | ApplyCalculatedSuggestRelevance(&results->suggest_results); |
| 268 | ApplyCalculatedNavigationRelevance(&results->navigation_results); |
| 269 | // If abandoning scores entirely, also abandon the verbatim score. |
| 270 | if (abandon_suggested_scores) |
| 271 | results->verbatim_relevance = -1; |
| 272 | } |
| 273 | |
| 274 | // Keep the result lists sorted. |
| 275 | const CompareScoredResults comparator = CompareScoredResults(); |
| 276 | std::stable_sort(results->suggest_results.begin(), |
| 277 | results->suggest_results.end(), |
| 278 | comparator); |
| 279 | std::stable_sort(results->navigation_results.begin(), |
| 280 | results->navigation_results.end(), |
| 281 | comparator); |
| 282 | } |
| 283 | |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 284 | const TemplateURL* SearchProvider::GetTemplateURL(bool is_keyword) const { |
| 285 | return is_keyword ? providers_.GetKeywordProviderURL() |
| 286 | : providers_.GetDefaultProviderURL(); |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 287 | } |
| 288 | |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 289 | const AutocompleteInput SearchProvider::GetInput(bool is_keyword) const { |
| 290 | return is_keyword ? keyword_input_ : input_; |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 291 | } |
| 292 | |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 293 | BaseSearchProvider::Results* SearchProvider::GetResultsToFill(bool is_keyword) { |
| 294 | return is_keyword ? &keyword_results_ : &default_results_; |
| 295 | } |
| 296 | |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 297 | bool SearchProvider::ShouldAppendExtraParams( |
| 298 | const SuggestResult& result) const { |
| 299 | return !result.from_keyword_provider() || |
| 300 | providers_.default_provider().empty(); |
| 301 | } |
| 302 | |
[email protected] | ef6866f | 2014-02-18 08:26:34 | [diff] [blame] | 303 | void SearchProvider::StopSuggest() { |
| 304 | // Increment the appropriate field in the histogram by the number of |
| 305 | // pending requests that were invalidated. |
| 306 | for (int i = 0; i < suggest_results_pending_; ++i) |
| 307 | LogOmniboxSuggestRequest(REQUEST_INVALIDATED); |
| 308 | suggest_results_pending_ = 0; |
| 309 | timer_.Stop(); |
| 310 | // Stop any in-progress URL fetches. |
| 311 | keyword_fetcher_.reset(); |
| 312 | default_fetcher_.reset(); |
| 313 | } |
| 314 | |
| 315 | void SearchProvider::ClearAllResults() { |
| 316 | keyword_results_.Clear(); |
| 317 | default_results_.Clear(); |
| 318 | } |
| 319 | |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 320 | int SearchProvider::GetDefaultResultRelevance() const { |
| 321 | return -1; |
| 322 | } |
| 323 | |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 324 | void SearchProvider::RecordDeletionResult(bool success) { |
| 325 | if (success) { |
| 326 | content::RecordAction( |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 327 | base::UserMetricsAction("Omnibox.ServerSuggestDelete.Success")); |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 328 | } else { |
| 329 | content::RecordAction( |
[email protected] | e6e30ac | 2014-01-13 21:24:39 | [diff] [blame] | 330 | base::UserMetricsAction("Omnibox.ServerSuggestDelete.Failure")); |
[email protected] | 7267ff8 | 2013-12-03 17:20:30 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 334 | void SearchProvider::LogFetchComplete(bool success, bool is_keyword) { |
| 335 | LogOmniboxSuggestRequest(REPLY_RECEIVED); |
| 336 | // Record response time for suggest requests sent to Google. We care |
| 337 | // only about the common case: the Google default provider used in |
| 338 | // non-keyword mode. |
| 339 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 340 | if (!is_keyword && default_url && |
| 341 | (TemplateURLPrepopulateData::GetEngineType(*default_url) == |
| 342 | SEARCH_ENGINE_GOOGLE)) { |
| 343 | const base::TimeDelta elapsed_time = |
| 344 | base::TimeTicks::Now() - time_suggest_request_sent_; |
| 345 | if (success) { |
| 346 | UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Success.GoogleResponseTime", |
| 347 | elapsed_time); |
| 348 | } else { |
| 349 | UMA_HISTOGRAM_TIMES("Omnibox.SuggestRequest.Failure.GoogleResponseTime", |
| 350 | elapsed_time); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | bool SearchProvider::IsKeywordFetcher(const net::URLFetcher* fetcher) const { |
| 356 | return fetcher == keyword_fetcher_.get(); |
| 357 | } |
| 358 | |
| 359 | void SearchProvider::UpdateMatches() { |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 360 | ConvertResultsToAutocompleteMatches(); |
| 361 | |
| 362 | // Check constraints that may be violated by suggested relevances. |
| 363 | if (!matches_.empty() && |
| 364 | (default_results_.HasServerProvidedScores() || |
| 365 | keyword_results_.HasServerProvidedScores())) { |
| 366 | // These blocks attempt to repair undesirable behavior by suggested |
| 367 | // relevances with minimal impact, preserving other suggested relevances. |
| 368 | |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 369 | if (!HasKeywordDefaultMatchInKeywordMode()) { |
| 370 | // In keyword mode, disregard the keyword verbatim suggested relevance |
| 371 | // if necessary so there at least one keyword match that's allowed to |
| 372 | // be the default match. |
| 373 | keyword_results_.verbatim_relevance = -1; |
| 374 | ConvertResultsToAutocompleteMatches(); |
| 375 | } |
[email protected] | 89bd27d1 | 2014-04-12 17:36:23 | [diff] [blame] | 376 | if (IsTopMatchSearchWithURLInput()) { |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 377 | // Disregard the suggested search and verbatim relevances if the input |
| 378 | // type is URL and the top match is a highly-ranked search suggestion. |
| 379 | // For example, prevent a search for "foo.com" from outranking another |
| 380 | // provider's navigation for "foo.com" or "foo.com/url_from_history". |
| 381 | ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results); |
| 382 | ApplyCalculatedSuggestRelevance(&default_results_.suggest_results); |
| 383 | default_results_.verbatim_relevance = -1; |
| 384 | keyword_results_.verbatim_relevance = -1; |
| 385 | ConvertResultsToAutocompleteMatches(); |
| 386 | } |
[email protected] | 89bd27d1 | 2014-04-12 17:36:23 | [diff] [blame] | 387 | if (FindTopMatch() == matches_.end()) { |
| 388 | // Guarantee that SearchProvider returns a legal default match. (The |
| 389 | // omnibox always needs at least one legal default match, and it relies |
| 390 | // on SearchProvider to always return one.) |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 391 | ApplyCalculatedRelevance(); |
| 392 | ConvertResultsToAutocompleteMatches(); |
| 393 | } |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 394 | DCHECK(HasKeywordDefaultMatchInKeywordMode()); |
[email protected] | 89bd27d1 | 2014-04-12 17:36:23 | [diff] [blame] | 395 | DCHECK(!IsTopMatchSearchWithURLInput()); |
| 396 | DCHECK(FindTopMatch() != matches_.end()); |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 397 | } |
| 398 | UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 399 | "Omnibox.SearchProviderMatches", matches_.size(), 1, 6, 7); |
| 400 | |
| 401 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 402 | if ((keyword_url != NULL) && HasKeywordDefaultMatchInKeywordMode()) { |
| 403 | // If there is a keyword match that is allowed to be the default match, |
| 404 | // then prohibit default provider matches from being the default match lest |
| 405 | // such matches cause the user to break out of keyword mode. |
| 406 | for (ACMatches::iterator it = matches_.begin(); it != matches_.end(); |
| 407 | ++it) { |
| 408 | if (it->keyword != keyword_url->keyword()) |
| 409 | it->allowed_to_be_default_match = false; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | base::TimeTicks update_starred_start_time(base::TimeTicks::Now()); |
| 414 | UpdateStarredStateOfMatches(); |
| 415 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateStarredTime", |
| 416 | base::TimeTicks::Now() - update_starred_start_time); |
| 417 | UpdateDone(); |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 418 | } |
| 419 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 420 | void SearchProvider::Run() { |
| 421 | // Start a new request with the current input. |
| 422 | suggest_results_pending_ = 0; |
| 423 | time_suggest_request_sent_ = base::TimeTicks::Now(); |
[email protected] | abe441e | 2013-05-06 12:35:05 | [diff] [blame] | 424 | |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 425 | default_fetcher_.reset(CreateSuggestFetcher(kDefaultProviderURLFetcherID, |
| 426 | providers_.GetDefaultProviderURL(), input_)); |
| 427 | keyword_fetcher_.reset(CreateSuggestFetcher(kKeywordProviderURLFetcherID, |
| 428 | providers_.GetKeywordProviderURL(), keyword_input_)); |
| 429 | |
| 430 | // Both the above can fail if the providers have been modified or deleted |
| 431 | // since the query began. |
| 432 | if (suggest_results_pending_ == 0) { |
| 433 | UpdateDone(); |
| 434 | // We only need to update the listener if we're actually done. |
| 435 | if (done_) |
| 436 | listener_->OnProviderUpdate(false); |
| 437 | } |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 438 | } |
| 439 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 440 | void SearchProvider::DoHistoryQuery(bool minimal_changes) { |
| 441 | // The history query results are synchronous, so if minimal_changes is true, |
| 442 | // we still have the last results and don't need to do anything. |
| 443 | if (minimal_changes) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | return; |
| 445 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 446 | keyword_history_results_.clear(); |
| 447 | default_history_results_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 448 | |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 449 | if (OmniboxFieldTrial::SearchHistoryDisable( |
| 450 | input_.current_page_classification())) |
[email protected] | d8cd76b | 2013-07-10 09:46:16 | [diff] [blame] | 451 | return; |
| 452 | |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 453 | HistoryService* const history_service = |
[email protected] | 9d2db76 | 2012-06-19 00:01:10 | [diff] [blame] | 454 | HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 455 | history::URLDatabase* url_db = history_service ? |
| 456 | history_service->InMemoryDatabase() : NULL; |
[email protected] | 8d45713 | 2010-11-04 18:13:40 | [diff] [blame] | 457 | if (!url_db) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | return; |
| 459 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 460 | // Request history for both the keyword and default provider. We grab many |
| 461 | // more matches than we'll ultimately clamp to so that if there are several |
| 462 | // recent multi-word matches who scores are lowered (see |
| 463 | // AddHistoryResultsToMap()), they won't crowd out older, higher-scoring |
| 464 | // matches. Note that this doesn't fix the problem entirely, but merely |
| 465 | // limits it to cases with a very large number of such multi-word matches; for |
| 466 | // now, this seems OK compared with the complexity of a real fix, which would |
| 467 | // require multiple searches and tracking of "single- vs. multi-word" in the |
| 468 | // database. |
| 469 | int num_matches = kMaxMatches * 5; |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 470 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 471 | if (default_url) { |
[email protected] | b4bec97 | 2014-04-05 18:07:15 | [diff] [blame] | 472 | const base::TimeTicks start_time = base::TimeTicks::Now(); |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 473 | url_db->GetMostRecentKeywordSearchTerms(default_url->id(), input_.text(), |
| 474 | num_matches, &default_history_results_); |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 475 | UMA_HISTOGRAM_TIMES( |
| 476 | "Omnibox.SearchProvider.GetMostRecentKeywordTermsDefaultProviderTime", |
| 477 | base::TimeTicks::Now() - start_time); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 478 | } |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 479 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 480 | if (keyword_url) { |
| 481 | url_db->GetMostRecentKeywordSearchTerms(keyword_url->id(), |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 482 | keyword_input_.text(), num_matches, &keyword_history_results_); |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 483 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | } |
| 485 | |
[email protected] | 6dc950f | 2012-07-16 19:49:08 | [diff] [blame] | 486 | void SearchProvider::StartOrStopSuggestQuery(bool minimal_changes) { |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 487 | if (!IsQuerySuitableForSuggest()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 488 | StopSuggest(); |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 489 | ClearAllResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
| 493 | // For the minimal_changes case, if we finished the previous query and still |
| 494 | // have its results, or are allowed to keep running it, just do that, rather |
| 495 | // than starting a new query. |
| 496 | if (minimal_changes && |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 497 | (!default_results_.suggest_results.empty() || |
| 498 | !default_results_.navigation_results.empty() || |
| 499 | !keyword_results_.suggest_results.empty() || |
| 500 | !keyword_results_.navigation_results.empty() || |
[email protected] | a2770a7d | 2014-04-22 19:33:35 | [diff] [blame] | 501 | (!done_ && input_.want_asynchronous_matches()))) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 502 | return; |
| 503 | |
| 504 | // We can't keep running any previous query, so halt it. |
| 505 | StopSuggest(); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 506 | |
| 507 | // Remove existing results that cannot inline autocomplete the new input. |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 508 | RemoveAllStaleResults(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 509 | |
[email protected] | ee6110b | 2014-01-09 22:26:31 | [diff] [blame] | 510 | // Update the content classifications of remaining results so they look good |
| 511 | // against the current input. |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 512 | UpdateMatchContentsClass(input_.text(), &default_results_); |
| 513 | if (!keyword_input_.text().empty()) |
| 514 | UpdateMatchContentsClass(keyword_input_.text(), &keyword_results_); |
[email protected] | ee6110b | 2014-01-09 22:26:31 | [diff] [blame] | 515 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | // We can't start a new query if we're only allowed synchronous results. |
[email protected] | a2770a7d | 2014-04-22 19:33:35 | [diff] [blame] | 517 | if (!input_.want_asynchronous_matches()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 518 | return; |
| 519 | |
[email protected] | 24dbf230 | 2013-04-05 16:59:25 | [diff] [blame] | 520 | // To avoid flooding the suggest server, don't send a query until at |
| 521 | // least 100 ms since the last query. |
[email protected] | 515ffa94 | 2012-11-27 20:18:24 | [diff] [blame] | 522 | base::TimeTicks next_suggest_time(time_suggest_request_sent_ + |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 523 | base::TimeDelta::FromMilliseconds(kMinimumTimeBetweenSuggestQueriesMs)); |
[email protected] | 515ffa94 | 2012-11-27 20:18:24 | [diff] [blame] | 524 | base::TimeTicks now(base::TimeTicks::Now()); |
| 525 | if (now >= next_suggest_time) { |
| 526 | Run(); |
| 527 | return; |
| 528 | } |
| 529 | timer_.Start(FROM_HERE, next_suggest_time - now, this, &SearchProvider::Run); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 530 | } |
| 531 | |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 532 | bool SearchProvider::IsQuerySuitableForSuggest() const { |
[email protected] | 3954c3a | 2012-04-10 20:17:55 | [diff] [blame] | 533 | // Don't run Suggest in incognito mode, if the engine doesn't support it, or |
| 534 | // if the user has disabled it. |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 535 | const TemplateURL* default_url = providers_.GetDefaultProviderURL(); |
| 536 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 537 | if (profile_->IsOffTheRecord() || |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 538 | ((!default_url || default_url->suggestions_url().empty()) && |
| 539 | (!keyword_url || keyword_url->suggestions_url().empty())) || |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 540 | !profile_->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled)) |
| 541 | return false; |
| 542 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 543 | // 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] | 544 | // isn't sent to the server. |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 545 | |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 546 | // FORCED_QUERY means the user is explicitly asking us to search for this, so |
| 547 | // we assume it isn't a URL and/or there isn't private data. |
| 548 | if (input_.type() == AutocompleteInput::FORCED_QUERY) |
| 549 | return true; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 550 | |
[email protected] | f608ea10 | 2013-03-18 15:08:09 | [diff] [blame] | 551 | // Next we check the scheme. If this is UNKNOWN/URL with a scheme that isn't |
| 552 | // http/https/ftp, we shouldn't send it. Sending things like file: and data: |
| 553 | // is both a waste of time and a disclosure of potentially private, local |
| 554 | // data. Other "schemes" may actually be usernames, and we don't want to send |
| 555 | // passwords. If the scheme is OK, we still need to check other cases below. |
| 556 | // If this is QUERY, then the presence of these schemes means the user |
| 557 | // explicitly typed one, and thus this is probably a URL that's being entered |
| 558 | // and happens to currently be invalid -- in which case we again want to run |
| 559 | // our checks below. Other QUERY cases are less likely to be URLs and thus we |
| 560 | // assume we're OK. |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 561 | if (!LowerCaseEqualsASCII(input_.scheme(), url::kHttpScheme) && |
| 562 | !LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) && |
[email protected] | 72aa4c2 | 2013-12-01 15:09:35 | [diff] [blame] | 563 | !LowerCaseEqualsASCII(input_.scheme(), content::kFtpScheme)) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 564 | return (input_.type() == AutocompleteInput::QUERY); |
| 565 | |
| 566 | // Don't send URLs with usernames, queries or refs. Some of these are |
| 567 | // private, and the Suggest server is unlikely to have any useful results |
| 568 | // for any of them. Also don't send URLs with ports, as we may initially |
| 569 | // think that a username + password is a host + port (and we don't want to |
| 570 | // send usernames/passwords), and even if the port really is a port, the |
| 571 | // server is once again unlikely to have and useful results. |
[email protected] | 825e16f | 2013-09-30 23:52:58 | [diff] [blame] | 572 | // Note that we only block based on refs if the input is URL-typed, as search |
| 573 | // queries can legitimately have #s in them which the URL parser |
| 574 | // overaggressively categorizes as a url with a ref. |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 575 | const url::Parsed& parts = input_.parts(); |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 576 | if (parts.username.is_nonempty() || parts.port.is_nonempty() || |
[email protected] | 825e16f | 2013-09-30 23:52:58 | [diff] [blame] | 577 | parts.query.is_nonempty() || |
| 578 | (parts.ref.is_nonempty() && (input_.type() == AutocompleteInput::URL))) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 579 | return false; |
| 580 | |
| 581 | // Don't send anything for https except the hostname. Hostnames are OK |
| 582 | // because they are visible when the TCP connection is established, but the |
| 583 | // specific path may reveal private information. |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 584 | if (LowerCaseEqualsASCII(input_.scheme(), url::kHttpsScheme) && |
[email protected] | a2fedb1e | 2011-01-25 15:23:36 | [diff] [blame] | 585 | parts.path.is_nonempty()) |
[email protected] | cac59d3 | 2010-08-09 23:23:14 | [diff] [blame] | 586 | return false; |
[email protected] | 83c72648 | 2008-09-10 06:36:34 | [diff] [blame] | 587 | |
| 588 | return true; |
| 589 | } |
| 590 | |
[email protected] | 71b4615 | 2013-05-03 16:39:20 | [diff] [blame] | 591 | void SearchProvider::RemoveAllStaleResults() { |
[email protected] | dc735c0 | 2013-11-12 23:23:41 | [diff] [blame] | 592 | if (keyword_input_.text().empty()) { |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 593 | // User is either in keyword mode with a blank input or out of |
| 594 | // keyword mode entirely. |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 595 | keyword_results_.Clear(); |
[email protected] | 1e1550e | 2013-05-02 17:37:51 | [diff] [blame] | 596 | } |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 597 | } |
| 598 | |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 599 | void SearchProvider::ApplyCalculatedRelevance() { |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 600 | ApplyCalculatedSuggestRelevance(&keyword_results_.suggest_results); |
| 601 | ApplyCalculatedSuggestRelevance(&default_results_.suggest_results); |
| 602 | ApplyCalculatedNavigationRelevance(&keyword_results_.navigation_results); |
| 603 | ApplyCalculatedNavigationRelevance(&default_results_.navigation_results); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 604 | default_results_.verbatim_relevance = -1; |
| 605 | keyword_results_.verbatim_relevance = -1; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 606 | } |
| 607 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 608 | void SearchProvider::ApplyCalculatedSuggestRelevance(SuggestResults* list) { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 609 | for (size_t i = 0; i < list->size(); ++i) { |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 610 | SuggestResult& result = (*list)[i]; |
| 611 | result.set_relevance( |
| 612 | result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
| 613 | (list->size() - i - 1)); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 614 | result.set_relevance_from_server(false); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 618 | void SearchProvider::ApplyCalculatedNavigationRelevance( |
| 619 | NavigationResults* list) { |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 620 | for (size_t i = 0; i < list->size(); ++i) { |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 621 | NavigationResult& result = (*list)[i]; |
| 622 | result.set_relevance( |
| 623 | result.CalculateRelevance(input_, providers_.has_keyword_provider()) + |
| 624 | (list->size() - i - 1)); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 625 | result.set_relevance_from_server(false); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 629 | net::URLFetcher* SearchProvider::CreateSuggestFetcher( |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 630 | int id, |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 631 | const TemplateURL* template_url, |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 632 | const AutocompleteInput& input) { |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 633 | if (!template_url || template_url->suggestions_url().empty()) |
| 634 | return NULL; |
| 635 | |
| 636 | // Bail if the suggestion URL is invalid with the given replacements. |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 637 | TemplateURLRef::SearchTermsArgs search_term_args(input.text()); |
| 638 | search_term_args.cursor_position = input.cursor_position(); |
[email protected] | d5015ca | 2013-08-08 22:04:18 | [diff] [blame] | 639 | search_term_args.page_classification = input.current_page_classification(); |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 640 | GURL suggest_url(template_url->suggestions_url_ref().ReplaceSearchTerms( |
[email protected] | 0079056 | 2012-12-14 09:57:16 | [diff] [blame] | 641 | search_term_args)); |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 642 | if (!suggest_url.is_valid()) |
| 643 | return NULL; |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 644 | // Send the current page URL if user setting and URL requirements are met and |
| 645 | // the user is in the field trial. |
| 646 | if (CanSendURL(current_page_url_, suggest_url, template_url, |
| 647 | input.current_page_classification(), profile_) && |
| 648 | OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) { |
| 649 | search_term_args.current_page_url = current_page_url_.spec(); |
| 650 | // Create the suggest URL again with the current page URL. |
| 651 | suggest_url = GURL(template_url->suggestions_url_ref().ReplaceSearchTerms( |
| 652 | search_term_args)); |
| 653 | } |
[email protected] | 9ff9172 | 2012-09-07 05:29:12 | [diff] [blame] | 654 | |
| 655 | suggest_results_pending_++; |
| 656 | LogOmniboxSuggestRequest(REQUEST_SENT); |
| 657 | |
| 658 | net::URLFetcher* fetcher = |
| 659 | net::URLFetcher::Create(id, suggest_url, net::URLFetcher::GET, this); |
[email protected] | 7cc6e563 | 2011-10-25 17:56:12 | [diff] [blame] | 660 | fetcher->SetRequestContext(profile_->GetRequestContext()); |
[email protected] | d3cf8682f0 | 2012-02-29 23:29:34 | [diff] [blame] | 661 | fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 662 | // Add Chrome experiment state to the request headers. |
| 663 | net::HttpRequestHeaders headers; |
[email protected] | ab778079 | 2013-01-10 01:26:09 | [diff] [blame] | 664 | chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( |
[email protected] | bd3b471 | 2012-12-18 17:01:30 | [diff] [blame] | 665 | fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); |
| 666 | fetcher->SetExtraRequestHeaders(headers.ToString()); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 667 | fetcher->Start(); |
| 668 | return fetcher; |
| 669 | } |
| 670 | |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 671 | void SearchProvider::ConvertResultsToAutocompleteMatches() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 672 | // Convert all the results to matches and add them to a map, so we can keep |
| 673 | // the most relevant match for each result. |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 674 | base::TimeTicks start_time(base::TimeTicks::Now()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 675 | MatchMap map; |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 676 | const base::Time no_time; |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 677 | int did_not_accept_keyword_suggestion = |
| 678 | keyword_results_.suggest_results.empty() ? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 679 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 680 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 681 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 682 | bool relevance_from_server; |
| 683 | int verbatim_relevance = GetVerbatimRelevance(&relevance_from_server); |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 684 | int did_not_accept_default_suggestion = |
| 685 | default_results_.suggest_results.empty() ? |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 686 | TemplateURLRef::NO_SUGGESTIONS_AVAILABLE : |
| 687 | TemplateURLRef::NO_SUGGESTION_CHOSEN; |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 688 | if (verbatim_relevance > 0) { |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 689 | const base::string16& trimmed_verbatim = |
| 690 | base::CollapseWhitespace(input_.text(), false); |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 691 | SuggestResult verbatim( |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 692 | trimmed_verbatim, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
| 693 | trimmed_verbatim, base::string16(), base::string16(), std::string(), |
[email protected] | 61f9908 | 2014-02-24 02:17:49 | [diff] [blame] | 694 | std::string(), false, verbatim_relevance, relevance_from_server, false, |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 695 | trimmed_verbatim); |
[email protected] | 57482a7 | 2014-03-14 22:27:37 | [diff] [blame] | 696 | AddMatchToMap(verbatim, std::string(), did_not_accept_default_suggestion, |
| 697 | false, &map); |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 698 | } |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 699 | if (!keyword_input_.text().empty()) { |
| 700 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 701 | // We only create the verbatim search query match for a keyword |
| 702 | // if it's not an extension keyword. Extension keywords are handled |
| 703 | // in KeywordProvider::Start(). (Extensions are complicated...) |
| 704 | // Note: in this provider, SEARCH_OTHER_ENGINE must correspond |
| 705 | // to the keyword verbatim search query. Do not create other matches |
| 706 | // of type SEARCH_OTHER_ENGINE. |
[email protected] | bdcbcd8 | 2013-10-28 13:40:25 | [diff] [blame] | 707 | if (keyword_url && |
| 708 | (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 709 | bool keyword_relevance_from_server; |
| 710 | const int keyword_verbatim_relevance = |
| 711 | GetKeywordVerbatimRelevance(&keyword_relevance_from_server); |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 712 | if (keyword_verbatim_relevance > 0) { |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 713 | const base::string16& trimmed_verbatim = |
| 714 | base::CollapseWhitespace(keyword_input_.text(), false); |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 715 | SuggestResult verbatim( |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 716 | trimmed_verbatim, AutocompleteMatchType::SEARCH_OTHER_ENGINE, |
| 717 | trimmed_verbatim, base::string16(), base::string16(), |
[email protected] | 61f9908 | 2014-02-24 02:17:49 | [diff] [blame] | 718 | std::string(), std::string(), true, keyword_verbatim_relevance, |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 719 | keyword_relevance_from_server, false, trimmed_verbatim); |
[email protected] | 57482a7 | 2014-03-14 22:27:37 | [diff] [blame] | 720 | AddMatchToMap(verbatim, std::string(), |
| 721 | did_not_accept_keyword_suggestion, false, &map); |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 722 | } |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 723 | } |
| 724 | } |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 725 | AddHistoryResultsToMap(keyword_history_results_, true, |
| 726 | did_not_accept_keyword_suggestion, &map); |
| 727 | AddHistoryResultsToMap(default_history_results_, false, |
| 728 | did_not_accept_default_suggestion, &map); |
| 729 | |
[email protected] | d1cb6a82 | 2013-09-18 19:43:00 | [diff] [blame] | 730 | AddSuggestResultsToMap(keyword_results_.suggest_results, |
| 731 | keyword_results_.metadata, &map); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 732 | AddSuggestResultsToMap(default_results_.suggest_results, |
| 733 | default_results_.metadata, &map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 734 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 735 | ACMatches matches; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 736 | for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 737 | matches.push_back(i->second); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 738 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 739 | AddNavigationResultsToMatches(keyword_results_.navigation_results, &matches); |
| 740 | AddNavigationResultsToMatches(default_results_.navigation_results, &matches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 741 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 742 | // Now add the most relevant matches to |matches_|. We take up to kMaxMatches |
| 743 | // suggest/navsuggest matches, regardless of origin. If Instant Extended is |
| 744 | // enabled and we have server-provided (and thus hopefully more accurate) |
| 745 | // scores for some suggestions, we allow more of those, until we reach |
| 746 | // AutocompleteResult::kMaxMatches total matches (that is, enough to fill the |
| 747 | // whole popup). |
| 748 | // |
| 749 | // We will always return any verbatim matches, no matter how we obtained their |
| 750 | // scores, unless we have already accepted AutocompleteResult::kMaxMatches |
| 751 | // higher-scoring matches under the conditions above. |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 752 | std::sort(matches.begin(), matches.end(), &AutocompleteMatch::MoreRelevant); |
| 753 | matches_.clear(); |
[email protected] | 3723e6e | 2012-06-11 21:06:56 | [diff] [blame] | 754 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 755 | size_t num_suggestions = 0; |
| 756 | for (ACMatches::const_iterator i(matches.begin()); |
| 757 | (i != matches.end()) && |
| 758 | (matches_.size() < AutocompleteResult::kMaxMatches); |
| 759 | ++i) { |
| 760 | // SEARCH_OTHER_ENGINE is only used in the SearchProvider for the keyword |
| 761 | // verbatim result, so this condition basically means "if this match is a |
| 762 | // suggestion of some sort". |
| 763 | if ((i->type != AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED) && |
| 764 | (i->type != AutocompleteMatchType::SEARCH_OTHER_ENGINE)) { |
| 765 | // If we've already hit the limit on non-server-scored suggestions, and |
| 766 | // this isn't a server-scored suggestion we can add, skip it. |
| 767 | if ((num_suggestions >= kMaxMatches) && |
| 768 | (!chrome::IsInstantExtendedAPIEnabled() || |
| 769 | (i->GetAdditionalInfo(kRelevanceFromServerKey) != kTrue))) { |
| 770 | continue; |
| 771 | } |
| 772 | |
| 773 | ++num_suggestions; |
| 774 | } |
| 775 | |
| 776 | matches_.push_back(*i); |
| 777 | } |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 778 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.ConvertResultsTime", |
| 779 | base::TimeTicks::Now() - start_time); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 780 | } |
| 781 | |
[email protected] | 89bd27d1 | 2014-04-12 17:36:23 | [diff] [blame] | 782 | ACMatches::const_iterator SearchProvider::FindTopMatch() const { |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 783 | ACMatches::const_iterator it = matches_.begin(); |
| 784 | while ((it != matches_.end()) && !it->allowed_to_be_default_match) |
| 785 | ++it; |
| 786 | return it; |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 787 | } |
| 788 | |
[email protected] | 1c1e784 | 2013-11-22 16:28:16 | [diff] [blame] | 789 | bool SearchProvider::HasKeywordDefaultMatchInKeywordMode() const { |
| 790 | const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); |
| 791 | // If the user is not in keyword mode, return true to say that this |
| 792 | // constraint is not violated. |
| 793 | if (keyword_url == NULL) |
| 794 | return true; |
| 795 | for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end(); |
| 796 | ++it) { |
| 797 | if ((it->keyword == keyword_url->keyword()) && |
| 798 | it->allowed_to_be_default_match) |
| 799 | return true; |
| 800 | } |
| 801 | return false; |
| 802 | } |
| 803 | |
[email protected] | 89bd27d1 | 2014-04-12 17:36:23 | [diff] [blame] | 804 | bool SearchProvider::IsTopMatchSearchWithURLInput() const { |
| 805 | ACMatches::const_iterator first_match = FindTopMatch(); |
[email protected] | 0a8718b1 | 2013-11-13 18:41:31 | [diff] [blame] | 806 | return (input_.type() == AutocompleteInput::URL) && |
| 807 | (first_match != matches_.end()) && |
| 808 | (first_match->relevance > CalculateRelevanceForVerbatim()) && |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame^] | 809 | (first_match->type != AutocompleteMatchType::NAVSUGGEST) && |
| 810 | (first_match->type != AutocompleteMatchType::NAVSUGGEST_PERSONALIZED); |
[email protected] | 344946a1 | 2012-12-20 12:03:42 | [diff] [blame] | 811 | } |
| 812 | |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 813 | void SearchProvider::AddNavigationResultsToMatches( |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 814 | const NavigationResults& navigation_results, |
| 815 | ACMatches* matches) { |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 816 | for (NavigationResults::const_iterator it = navigation_results.begin(); |
| 817 | it != navigation_results.end(); ++it) { |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 818 | matches->push_back(NavigationToMatch(*it)); |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 819 | // In the absence of suggested relevance scores, use only the single |
| 820 | // highest-scoring result. (The results are already sorted by relevance.) |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 821 | if (!it->relevance_from_server()) |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 822 | return; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
| 826 | void SearchProvider::AddHistoryResultsToMap(const HistoryResults& results, |
| 827 | bool is_keyword, |
| 828 | int did_not_accept_suggestion, |
| 829 | MatchMap* map) { |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 830 | if (results.empty()) |
| 831 | return; |
| 832 | |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 833 | base::TimeTicks start_time(base::TimeTicks::Now()); |
[email protected] | d7ad477 | 2012-06-01 03:12:54 | [diff] [blame] | 834 | bool prevent_inline_autocomplete = input_.prevent_inline_autocomplete() || |
| 835 | (input_.type() == AutocompleteInput::URL); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 836 | const base::string16& input_text = |
[email protected] | 1471085 | 2013-02-05 23:45:41 | [diff] [blame] | 837 | is_keyword ? keyword_input_.text() : input_.text(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 838 | bool input_multiple_words = HasMultipleWords(input_text); |
| 839 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 840 | SuggestResults scored_results; |
| 841 | if (!prevent_inline_autocomplete && input_multiple_words) { |
| 842 | // ScoreHistoryResults() allows autocompletion of multi-word, 1-visit |
| 843 | // queries if the input also has multiple words. But if we were already |
[email protected] | f13a213 | 2014-04-22 08:13:52 | [diff] [blame] | 844 | // scoring a multi-word, multi-visit query aggressively, and the current |
| 845 | // input is still a prefix of it, then changing the suggestion suddenly |
| 846 | // feels wrong. To detect this case, first score as if only one word has |
| 847 | // been typed, then check if the best result came from aggressive search |
| 848 | // history scoring. If it did, then just keep that score set. This |
| 849 | // 1200 the lowest possible score in CalculateRelevanceForHistory()'s |
| 850 | // aggressive-scoring curve. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 851 | scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
| 852 | false, input_text, is_keyword); |
[email protected] | f13a213 | 2014-04-22 08:13:52 | [diff] [blame] | 853 | if ((scored_results.front().relevance() < 1200) || |
[email protected] | 9dfb4d36 | 2013-04-05 02:15:12 | [diff] [blame] | 854 | !HasMultipleWords(scored_results.front().suggestion())) |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 855 | scored_results.clear(); // Didn't detect the case above, score normally. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 856 | } |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 857 | if (scored_results.empty()) |
| 858 | scored_results = ScoreHistoryResults(results, prevent_inline_autocomplete, |
| 859 | input_multiple_words, input_text, |
| 860 | is_keyword); |
| 861 | for (SuggestResults::const_iterator i(scored_results.begin()); |
| 862 | i != scored_results.end(); ++i) { |
[email protected] | 57482a7 | 2014-03-14 22:27:37 | [diff] [blame] | 863 | AddMatchToMap(*i, std::string(), did_not_accept_suggestion, true, map); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 864 | } |
[email protected] | 31afdf7 | 2013-09-26 04:29:36 | [diff] [blame] | 865 | UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime", |
| 866 | base::TimeTicks::Now() - start_time); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 867 | } |
| 868 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 869 | SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 870 | const HistoryResults& results, |
| 871 | bool base_prevent_inline_autocomplete, |
| 872 | bool input_multiple_words, |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 873 | const base::string16& input_text, |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 874 | bool is_keyword) { |
[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 875 | AutocompleteClassifier* classifier = |
| 876 | AutocompleteClassifierFactory::GetForProfile(profile_); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 877 | SuggestResults scored_results; |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 878 | const bool prevent_search_history_inlining = |
| 879 | OmniboxFieldTrial::SearchHistoryPreventInlining( |
| 880 | input_.current_page_classification()); |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 881 | const base::string16& trimmed_input = |
| 882 | base::CollapseWhitespace(input_text, false); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 883 | for (HistoryResults::const_iterator i(results.begin()); i != results.end(); |
| 884 | ++i) { |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 885 | const base::string16& trimmed_suggestion = |
| 886 | base::CollapseWhitespace(i->term, false); |
| 887 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 888 | // Don't autocomplete multi-word queries that have only been seen once |
| 889 | // unless the user has typed more than one word. |
| 890 | bool prevent_inline_autocomplete = base_prevent_inline_autocomplete || |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 891 | (!input_multiple_words && (i->visits < 2) && |
| 892 | HasMultipleWords(trimmed_suggestion)); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 893 | |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 894 | // Don't autocomplete search terms that would normally be treated as URLs |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 895 | // when typed. For example, if the user searched for "google.com" and types |
| 896 | // "goog", don't autocomplete to the search term "google.com". Otherwise, |
| 897 | // 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] | 898 | // NOTE: We don't check this in the following cases: |
| 899 | // * When inline autocomplete is disabled, we won't be inline |
| 900 | // autocompleting this term, so we don't need to worry about confusion as |
| 901 | // much. This also prevents calling Classify() again from inside the |
| 902 | // classifier (which will corrupt state and likely crash), since the |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 903 | // classifier always disables inline autocomplete. |
[email protected] | cc44736 | 2011-04-06 03:57:48 | [diff] [blame] | 904 | // * When the user has typed the whole term, the "what you typed" history |
| 905 | // match will outrank us for URL-like inputs anyway, so we need not do |
| 906 | // anything special. |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 907 | if (!prevent_inline_autocomplete && classifier && |
| 908 | (trimmed_suggestion != trimmed_input)) { |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 909 | AutocompleteMatch match; |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 910 | classifier->Classify(trimmed_suggestion, false, false, |
[email protected] | 51abb7b | 2014-02-09 23:00:08 | [diff] [blame] | 911 | input_.current_page_classification(), &match, NULL); |
[email protected] | 2905f74 | 2011-10-13 03:51:58 | [diff] [blame] | 912 | prevent_inline_autocomplete = |
[email protected] | 749e7ae0 | 2012-09-05 18:47:46 | [diff] [blame] | 913 | !AutocompleteMatch::IsSearchType(match.type); |
[email protected] | ea3b9a50 | 2011-04-04 14:19:37 | [diff] [blame] | 914 | } |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 915 | |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 916 | int relevance = CalculateRelevanceForHistory( |
| 917 | i->time, is_keyword, !prevent_inline_autocomplete, |
| 918 | prevent_search_history_inlining); |
[email protected] | 2c03c06b | 2013-12-11 20:45:02 | [diff] [blame] | 919 | scored_results.push_back(SuggestResult( |
[email protected] | c2ca3fd | 2014-03-22 03:07:44 | [diff] [blame] | 920 | trimmed_suggestion, AutocompleteMatchType::SEARCH_HISTORY, |
| 921 | trimmed_suggestion, base::string16(), base::string16(), std::string(), |
| 922 | std::string(), is_keyword, relevance, false, false, trimmed_input)); |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 923 | } |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 924 | |
| 925 | // History returns results sorted for us. However, we may have docked some |
| 926 | // results' scores, so things are no longer in order. Do a stable sort to get |
| 927 | // things back in order without otherwise disturbing results with equal |
| 928 | // scores, then force the scores to be unique, so that the order in which |
| 929 | // they're shown is deterministic. |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 930 | std::stable_sort(scored_results.begin(), scored_results.end(), |
| 931 | CompareScoredResults()); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 932 | int last_relevance = 0; |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 933 | for (SuggestResults::iterator i(scored_results.begin()); |
| 934 | i != scored_results.end(); ++i) { |
| 935 | if ((i != scored_results.begin()) && (i->relevance() >= last_relevance)) |
| 936 | i->set_relevance(last_relevance - 1); |
| 937 | last_relevance = i->relevance(); |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 938 | } |
| 939 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 940 | return scored_results; |
[email protected] | 257ab71 | 2009-04-14 17:16:24 | [diff] [blame] | 941 | } |
| 942 | |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 943 | void SearchProvider::AddSuggestResultsToMap(const SuggestResults& results, |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 944 | const std::string& metadata, |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 945 | MatchMap* map) { |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 946 | for (size_t i = 0; i < results.size(); ++i) |
[email protected] | 57482a7 | 2014-03-14 22:27:37 | [diff] [blame] | 947 | AddMatchToMap(results[i], metadata, i, false, map); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 948 | } |
| 949 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 950 | int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const { |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 951 | // Use the suggested verbatim relevance score if it is non-negative (valid), |
| 952 | // if inline autocomplete isn't prevented (always show verbatim on backspace), |
[email protected] | 1beee34 | 2012-06-19 22:22:28 | [diff] [blame] | 953 | // and if it won't suppress verbatim, leaving no default provider matches. |
| 954 | // Otherwise, if the default provider returned no matches and was still able |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 955 | // 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] | 956 | // left unable to search using their default provider from the omnibox. |
[email protected] | dc6943b | 2012-06-19 06:39:56 | [diff] [blame] | 957 | // Check for results on each verbatim calculation, as results from older |
| 958 | // queries (on previous input) may be trimmed for failing to inline new input. |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 959 | bool use_server_relevance = |
| 960 | (default_results_.verbatim_relevance >= 0) && |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 961 | !input_.prevent_inline_autocomplete() && |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 962 | ((default_results_.verbatim_relevance > 0) || |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 963 | !default_results_.suggest_results.empty() || |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 964 | !default_results_.navigation_results.empty()); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 965 | if (relevance_from_server) |
| 966 | *relevance_from_server = use_server_relevance; |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 967 | return use_server_relevance ? |
| 968 | default_results_.verbatim_relevance : CalculateRelevanceForVerbatim(); |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 969 | } |
[email protected] | d1f0a7f | 2012-06-05 10:26:42 | [diff] [blame] | 970 | |
[email protected] | 382a064 | 2012-06-06 06:13:52 | [diff] [blame] | 971 | int SearchProvider::CalculateRelevanceForVerbatim() const { |
[email protected] | 85b8d6f | 2012-05-08 20:53:47 | [diff] [blame] | 972 | if (!providers_.keyword_provider().empty()) |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 973 | return 250; |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 974 | return CalculateRelevanceForVerbatimIgnoringKeywordModeState(); |
| 975 | } |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 976 | |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 977 | int SearchProvider:: |
| 978 | CalculateRelevanceForVerbatimIgnoringKeywordModeState() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 979 | switch (input_.type()) { |
| 980 | case AutocompleteInput::UNKNOWN: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 981 | case AutocompleteInput::QUERY: |
| 982 | case AutocompleteInput::FORCED_QUERY: |
[email protected] | 90fe2bb | 2013-01-15 03:42:13 | [diff] [blame] | 983 | return kNonURLVerbatimRelevance; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 984 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 985 | case AutocompleteInput::URL: |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 986 | return 850; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 987 | |
| 988 | default: |
| 989 | NOTREACHED(); |
| 990 | return 0; |
| 991 | } |
| 992 | } |
| 993 | |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 994 | int SearchProvider::GetKeywordVerbatimRelevance( |
| 995 | bool* relevance_from_server) const { |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 996 | // Use the suggested verbatim relevance score if it is non-negative (valid), |
| 997 | // if inline autocomplete isn't prevented (always show verbatim on backspace), |
| 998 | // and if it won't suppress verbatim, leaving no keyword provider matches. |
| 999 | // Otherwise, if the keyword provider returned no matches and was still able |
| 1000 | // to suppress verbatim, the user would have no search/nav matches and may be |
| 1001 | // left unable to search using their keyword provider from the omnibox. |
| 1002 | // Check for results on each verbatim calculation, as results from older |
| 1003 | // queries (on previous input) may be trimmed for failing to inline new input. |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1004 | bool use_server_relevance = |
| 1005 | (keyword_results_.verbatim_relevance >= 0) && |
[email protected] | dab8d52d | 2013-03-05 07:35:28 | [diff] [blame] | 1006 | !input_.prevent_inline_autocomplete() && |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1007 | ((keyword_results_.verbatim_relevance > 0) || |
[email protected] | cc1526e | 2013-05-17 04:04:24 | [diff] [blame] | 1008 | !keyword_results_.suggest_results.empty() || |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1009 | !keyword_results_.navigation_results.empty()); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1010 | if (relevance_from_server) |
| 1011 | *relevance_from_server = use_server_relevance; |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1012 | return use_server_relevance ? |
| 1013 | keyword_results_.verbatim_relevance : |
| 1014 | CalculateRelevanceForKeywordVerbatim(keyword_input_.type(), |
| 1015 | keyword_input_.prefer_keyword()); |
[email protected] | 5423e56 | 2013-02-07 03:58:45 | [diff] [blame] | 1016 | } |
| 1017 | |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1018 | int SearchProvider::CalculateRelevanceForHistory( |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1019 | const base::Time& time, |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1020 | bool is_keyword, |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1021 | bool use_aggressive_method, |
| 1022 | bool prevent_search_history_inlining) const { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1023 | // The relevance of past searches falls off over time. There are two distinct |
| 1024 | // equations used. If the first equation is used (searches to the primary |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1025 | // provider that we want to score aggressively), the score is in the range |
| 1026 | // 1300-1599 (unless |prevent_search_history_inlining|, in which case |
[email protected] | d8cd76b | 2013-07-10 09:46:16 | [diff] [blame] | 1027 | // it's in the range 1200-1299). If the second equation is used the |
| 1028 | // relevance of a search 15 minutes ago is discounted 50 points, while the |
| 1029 | // relevance of a search two weeks ago is discounted 450 points. |
[email protected] | bc8bb0cd | 2013-06-24 21:50:23 | [diff] [blame] | 1030 | double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0); |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1031 | bool is_primary_provider = is_keyword || !providers_.has_keyword_provider(); |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1032 | if (is_primary_provider && use_aggressive_method) { |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1033 | // Searches with the past two days get a different curve. |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1034 | const double autocomplete_time = 2 * 24 * 60 * 60; |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1035 | if (elapsed_time < autocomplete_time) { |
[email protected] | d8cd76b | 2013-07-10 09:46:16 | [diff] [blame] | 1036 | int max_score = is_keyword ? 1599 : 1399; |
[email protected] | 78e5e43 | 2013-08-03 02:10:10 | [diff] [blame] | 1037 | if (prevent_search_history_inlining) |
[email protected] | d8cd76b | 2013-07-10 09:46:16 | [diff] [blame] | 1038 | max_score = 1299; |
| 1039 | return max_score - static_cast<int>(99 * |
[email protected] | aa613d6 | 2010-11-09 20:40:18 | [diff] [blame] | 1040 | std::pow(elapsed_time / autocomplete_time, 2.5)); |
| 1041 | } |
| 1042 | elapsed_time -= autocomplete_time; |
| 1043 | } |
| 1044 | |
[email protected] | c3a4bd99 | 2010-08-18 20:25:01 | [diff] [blame] | 1045 | const int score_discount = |
| 1046 | static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1047 | |
[email protected] | 6c85aa0 | 2009-02-27 12:08:09 | [diff] [blame] | 1048 | // Don't let scores go below 0. Negative relevance scores are meaningful in |
| 1049 | // a different way. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1050 | int base_score; |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1051 | if (is_primary_provider) |
[email protected] | 52d08b1 | 2009-10-19 18:42:36 | [diff] [blame] | 1052 | base_score = (input_.type() == AutocompleteInput::URL) ? 750 : 1050; |
[email protected] | 5112455 | 2011-07-16 01:37:10 | [diff] [blame] | 1053 | else |
| 1054 | base_score = 200; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1055 | return std::max(0, base_score - score_discount); |
| 1056 | } |
| 1057 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1058 | AutocompleteMatch SearchProvider::NavigationToMatch( |
[email protected] | 188b50c | 2013-03-28 07:19:42 | [diff] [blame] | 1059 | const NavigationResult& navigation) { |
[email protected] | 5889bfb | 2014-03-19 00:26:48 | [diff] [blame] | 1060 | base::string16 input; |
| 1061 | const bool trimmed_whitespace = base::TrimWhitespace( |
| 1062 | navigation.from_keyword_provider() ? |
| 1063 | keyword_input_.text() : input_.text(), |
| 1064 | base::TRIM_TRAILING, &input) != base::TRIM_NONE; |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1065 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame^] | 1066 | navigation.type()); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1067 | match.destination_url = navigation.url(); |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame^] | 1068 | BaseSearchProvider::SetDeletionURL(navigation.deletion_url(), &match); |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 1069 | // First look for the user's input inside the formatted url as it would be |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1070 | // without trimming the scheme, so we can find matches at the beginning of the |
| 1071 | // scheme. |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1072 | const URLPrefix* prefix = |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 1073 | URLPrefix::BestURLPrefix(navigation.formatted_url(), input); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1074 | size_t match_start = (prefix == NULL) ? |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 1075 | navigation.formatted_url().find(input) : prefix->prefix.length(); |
[email protected] | d2445c8 | 2013-11-04 22:28:35 | [diff] [blame] | 1076 | bool trim_http = !AutocompleteInput::HasHTTPScheme(input) && |
| 1077 | (!prefix || (match_start != 0)); |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 1078 | const net::FormatUrlTypes format_types = |
| 1079 | net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1080 | |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1081 | const std::string languages( |
| 1082 | profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 1083 | size_t inline_autocomplete_offset = (prefix == NULL) ? |
| 1084 | base::string16::npos : (match_start + input.length()); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1085 | match.fill_into_edit += |
| 1086 | AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(), |
| 1087 | net::FormatUrl(navigation.url(), languages, format_types, |
| 1088 | net::UnescapeRule::SPACES, NULL, NULL, |
| 1089 | &inline_autocomplete_offset)); |
[email protected] | 1411903 | 2013-11-07 08:14:26 | [diff] [blame] | 1090 | // Preserve the forced query '?' prefix in |match.fill_into_edit|. |
| 1091 | // Otherwise, user edits to a suggestion would show non-Search results. |
| 1092 | if (input_.type() == AutocompleteInput::FORCED_QUERY) { |
[email protected] | 670d323 | 2013-12-24 17:58:58 | [diff] [blame] | 1093 | match.fill_into_edit.insert(0, base::ASCIIToUTF16("?")); |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 1094 | if (inline_autocomplete_offset != base::string16::npos) |
[email protected] | 1411903 | 2013-11-07 08:14:26 | [diff] [blame] | 1095 | ++inline_autocomplete_offset; |
| 1096 | } |
[email protected] | 6c94a102 | 2014-02-21 03:48:04 | [diff] [blame] | 1097 | if (inline_autocomplete_offset != base::string16::npos) { |
[email protected] | 518024c | 2013-07-19 23:40:25 | [diff] [blame] | 1098 | DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length()); |
| 1099 | match.inline_autocompletion = |
| 1100 | match.fill_into_edit.substr(inline_autocomplete_offset); |
| 1101 | } |
[email protected] | 6c94a102 | 2014-02-21 03:48:04 | [diff] [blame] | 1102 | // An inlineable navsuggestion can only be the default match when there |
| 1103 | // is no keyword provider active, lest it appear first and break the user |
[email protected] | 5889bfb | 2014-03-19 00:26:48 | [diff] [blame] | 1104 | // out of keyword mode. It can also only be default if either the inline |
| 1105 | // autocompletion is empty or we're not preventing inline autocompletion. |
| 1106 | // Finally, if we have an inlineable navsuggestion with an inline completion |
| 1107 | // that we're not preventing, make sure we didn't trim any whitespace. |
| 1108 | // We don't want to claim http://foo.com/bar is inlineable against the |
| 1109 | // input "foo.com/b ". |
[email protected] | 6c94a102 | 2014-02-21 03:48:04 | [diff] [blame] | 1110 | match.allowed_to_be_default_match = navigation.IsInlineable(input) && |
| 1111 | (providers_.GetKeywordProviderURL() == NULL) && |
[email protected] | 5889bfb | 2014-03-19 00:26:48 | [diff] [blame] | 1112 | (match.inline_autocompletion.empty() || |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame^] | 1113 | (!input_.prevent_inline_autocomplete() && !trimmed_whitespace)); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1114 | |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 1115 | match.contents = navigation.match_contents(); |
| 1116 | match.contents_class = navigation.match_contents_class(); |
[email protected] | 55ce8f1 | 2012-05-09 04:44:08 | [diff] [blame] | 1117 | match.description = navigation.description(); |
[email protected] | 371dab1 | 2012-06-01 03:23:55 | [diff] [blame] | 1118 | AutocompleteMatch::ClassifyMatchInString(input, match.description, |
| 1119 | ACMatchClassification::NONE, &match.description_class); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1120 | |
| 1121 | match.RecordAdditionalInfo( |
| 1122 | kRelevanceFromServerKey, |
| 1123 | navigation.relevance_from_server() ? kTrue : kFalse); |
[email protected] | 987fad78 | 2013-08-28 06:23:18 | [diff] [blame] | 1124 | match.RecordAdditionalInfo(kShouldPrefetchKey, kFalse); |
[email protected] | d30268a | 2013-06-25 22:31:07 | [diff] [blame] | 1125 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1126 | return match; |
| 1127 | } |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1128 | |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1129 | void SearchProvider::UpdateDone() { |
[email protected] | 2cdf117 | 2012-08-26 12:21:33 | [diff] [blame] | 1130 | // We're done when the timer isn't running, there are no suggest queries |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 1131 | // pending, and we're not waiting on Instant. |
[email protected] | e1290ee6 | 2013-06-26 18:31:15 | [diff] [blame] | 1132 | done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); |
[email protected] | 4ab4c7c | 2010-11-24 04:49:34 | [diff] [blame] | 1133 | } |