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