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