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