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