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