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