[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 5 | #include "components/omnibox/browser/zero_suggest_provider.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 6 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 9 | #include "base/callback.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 10 | #include "base/i18n/case_conversion.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 11 | #include "base/json/json_string_value_serializer.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 12 | #include "base/metrics/histogram.h" |
[email protected] | f7f41c0e | 2014-08-11 04:22:23 | [diff] [blame] | 13 | #include "base/metrics/user_metrics.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 14 | #include "base/strings/string16.h" |
| 15 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 4dcb797 | 2013-06-28 15:15:41 | [diff] [blame] | 17 | #include "base/time/time.h" |
a-v-y | dd768d5 | 2016-03-25 21:07:46 | [diff] [blame] | 18 | #include "base/trace_event/trace_event.h" |
amohammadkhan | f76ae11 | 2015-09-14 17:34:43 | [diff] [blame] | 19 | #include "components/data_use_measurement/core/data_use_user_data.h" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 20 | #include "components/history/core/browser/history_types.h" |
sdefresne | 0da3bc0 | 2015-01-29 18:26:35 | [diff] [blame] | 21 | #include "components/history/core/browser/top_sites.h" |
[email protected] | 3dc75b1 | 2014-06-08 00:02:22 | [diff] [blame] | 22 | #include "components/metrics/proto/omnibox_input_type.pb.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 23 | #include "components/omnibox/browser/autocomplete_classifier.h" |
| 24 | #include "components/omnibox/browser/autocomplete_input.h" |
| 25 | #include "components/omnibox/browser/autocomplete_match.h" |
| 26 | #include "components/omnibox/browser/autocomplete_provider_listener.h" |
| 27 | #include "components/omnibox/browser/history_url_provider.h" |
| 28 | #include "components/omnibox/browser/omnibox_field_trial.h" |
| 29 | #include "components/omnibox/browser/omnibox_pref_names.h" |
| 30 | #include "components/omnibox/browser/search_provider.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 31 | #include "components/omnibox/browser/verbatim_match.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame] | 32 | #include "components/pref_registry/pref_registry_syncable.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 33 | #include "components/prefs/pref_service.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 34 | #include "components/search_engines/template_url_service.h" |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 35 | #include "components/url_formatter/url_formatter.h" |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 36 | #include "components/variations/net/variations_http_headers.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 37 | #include "net/base/escape.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 38 | #include "net/base/load_flags.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 39 | #include "net/http/http_request_headers.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 40 | #include "net/url_request/url_fetcher.h" |
| 41 | #include "net/url_request/url_request_status.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 42 | #include "url/gurl.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 43 | |
| 44 | namespace { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 45 | |
| 46 | // TODO(hfung): The histogram code was copied and modified from |
| 47 | // search_provider.cc. Refactor and consolidate the code. |
| 48 | // We keep track in a histogram how many suggest requests we send, how |
| 49 | // many suggest requests we invalidate (e.g., due to a user typing |
| 50 | // another character), and how many replies we receive. |
| 51 | // *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! *** |
| 52 | // (excluding the end-of-list enum value) |
| 53 | // We do not want values of existing enums to change or else it screws |
| 54 | // up the statistics. |
| 55 | enum ZeroSuggestRequestsHistogramValue { |
| 56 | ZERO_SUGGEST_REQUEST_SENT = 1, |
| 57 | ZERO_SUGGEST_REQUEST_INVALIDATED, |
| 58 | ZERO_SUGGEST_REPLY_RECEIVED, |
| 59 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE |
| 60 | }; |
| 61 | |
| 62 | void LogOmniboxZeroSuggestRequest( |
| 63 | ZeroSuggestRequestsHistogramValue request_value) { |
| 64 | UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value, |
| 65 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE); |
| 66 | } |
| 67 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 68 | // Relevance value to use if it was not set explicitly by the server. |
| 69 | const int kDefaultZeroSuggestRelevance = 100; |
| 70 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 71 | } // namespace |
| 72 | |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 73 | // static |
| 74 | ZeroSuggestProvider* ZeroSuggestProvider::Create( |
blundell | 55e35e8 | 2015-06-16 08:46:18 | [diff] [blame] | 75 | AutocompleteProviderClient* client, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 76 | AutocompleteProviderListener* listener) { |
| 77 | return new ZeroSuggestProvider(client, listener); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 80 | // static |
| 81 | void ZeroSuggestProvider::RegisterProfilePrefs( |
| 82 | user_prefs::PrefRegistrySyncable* registry) { |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 83 | registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults, |
| 84 | std::string()); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 87 | void ZeroSuggestProvider::Start(const AutocompleteInput& input, |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 88 | bool minimal_changes) { |
a-v-y | dd768d5 | 2016-03-25 21:07:46 | [diff] [blame] | 89 | TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start"); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 90 | matches_.clear(); |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 91 | if (!input.from_omnibox_focus() || |
mariakhomenko | 3ef531d7 | 2015-01-10 00:03:43 | [diff] [blame] | 92 | input.type() == metrics::OmniboxInputType::INVALID) |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 93 | return; |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 94 | |
mpearson | 8a37c38 | 2015-03-07 05:58:57 | [diff] [blame] | 95 | Stop(true, false); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 96 | set_field_trial_triggered(false); |
| 97 | set_field_trial_triggered_in_session(false); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 98 | results_from_cache_ = false; |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 99 | permanent_text_ = input.text(); |
| 100 | current_query_ = input.current_url().spec(); |
| 101 | current_page_classification_ = input.current_page_classification(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 102 | current_url_match_ = MatchForCurrentURL(); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 103 | TemplateURLService* template_url_service = client()->GetTemplateURLService(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 104 | |
| 105 | const TemplateURL* default_provider = |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 106 | template_url_service->GetDefaultSearchProvider(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 107 | if (default_provider == NULL) |
| 108 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 109 | |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 110 | base::string16 prefix; |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 111 | TemplateURLRef::SearchTermsArgs search_term_args(prefix); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 112 | GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms( |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 113 | search_term_args, template_url_service->search_terms_data())); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 114 | if (!suggest_url.is_valid()) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 115 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 116 | |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 117 | // No need to send the current page URL in personalized suggest or |
| 118 | // most visited field trials. |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 119 | if (CanSendURL(input.current_url(), suggest_url, default_provider, |
[email protected] | e6477f1 | 2014-08-05 07:59:54 | [diff] [blame] | 120 | current_page_classification_, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 121 | template_url_service->search_terms_data(), client()) && |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 122 | !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() && |
| 123 | !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 124 | // Update suggest_url to include the current_page_url. |
| 125 | search_term_args.current_page_url = current_query_; |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 126 | suggest_url = |
| 127 | GURL(default_provider->suggestions_url_ref().ReplaceSearchTerms( |
| 128 | search_term_args, template_url_service->search_terms_data())); |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 129 | } else if (!ShouldShowNonContextualZeroSuggest(suggest_url, |
| 130 | input.current_url())) { |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 134 | done_ = false; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 135 | // TODO(jered): Consider adding locally-sourced zero-suggestions here too. |
| 136 | // These may be useful on the NTP or more relevant to the user than server |
| 137 | // suggestions, if based on local browsing history. |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 138 | MaybeUseCachedSuggestions(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 139 | Run(suggest_url); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 140 | } |
| 141 | |
mpearson | 8a37c38 | 2015-03-07 05:58:57 | [diff] [blame] | 142 | void ZeroSuggestProvider::Stop(bool clear_cached_results, |
| 143 | bool due_to_user_inactivity) { |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 144 | if (fetcher_) |
| 145 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); |
| 146 | fetcher_.reset(); |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 147 | waiting_for_most_visited_urls_request_ = false; |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 148 | done_ = true; |
| 149 | |
| 150 | if (clear_cached_results) { |
| 151 | // We do not call Clear() on |results_| to retain |verbatim_relevance| |
| 152 | // value in the |results_| object. |verbatim_relevance| is used at the |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 153 | // beginning of the next call to Start() to determine the current url |
| 154 | // match relevance. |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 155 | results_.suggest_results.clear(); |
| 156 | results_.navigation_results.clear(); |
| 157 | current_query_.clear(); |
mariakhomenko | 1535e6a | 2015-03-20 07:48:45 | [diff] [blame] | 158 | most_visited_urls_.clear(); |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 162 | void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) { |
| 163 | if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { |
| 164 | // Remove the deleted match from the cache, so it is not shown to the user |
| 165 | // again. Since we cannot remove just one result, blow away the cache. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 166 | client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults, |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 167 | std::string()); |
| 168 | } |
| 169 | BaseSearchProvider::DeleteMatch(match); |
| 170 | } |
| 171 | |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 172 | void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 173 | BaseSearchProvider::AddProviderInfo(provider_info); |
mariakhomenko | 1535e6a | 2015-03-20 07:48:45 | [diff] [blame] | 174 | if (!results_.suggest_results.empty() || |
| 175 | !results_.navigation_results.empty() || |
| 176 | !most_visited_urls_.empty()) |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 177 | provider_info->back().set_times_returned_results_in_session(1); |
| 178 | } |
| 179 | |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 180 | void ZeroSuggestProvider::ResetSession() { |
| 181 | // The user has started editing in the omnibox, so leave |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 182 | // |field_trial_triggered_in_session| unchanged and set |
| 183 | // |field_trial_triggered| to false since zero suggest is inactive now. |
| 184 | set_field_trial_triggered(false); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 185 | } |
| 186 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 187 | ZeroSuggestProvider::ZeroSuggestProvider(AutocompleteProviderClient* client, |
| 188 | AutocompleteProviderListener* listener) |
| 189 | : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client), |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 190 | listener_(listener), |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 191 | results_from_cache_(false), |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 192 | waiting_for_most_visited_urls_request_(false), |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 193 | weak_ptr_factory_(this) { |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | ZeroSuggestProvider::~ZeroSuggestProvider() { |
| 197 | } |
| 198 | |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 199 | const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const { |
| 200 | // Zero suggest provider should not receive keyword results. |
| 201 | DCHECK(!is_keyword); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 202 | return client()->GetTemplateURLService()->GetDefaultSearchProvider(); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const { |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 206 | // The callers of this method won't look at the AutocompleteInput's |
| 207 | // |from_omnibox_focus| member, so we can set its value to false. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 208 | return AutocompleteInput(base::string16(), base::string16::npos, |
| 209 | std::string(), GURL(current_query_), |
| 210 | current_page_classification_, true, false, false, |
| 211 | true, false, client()->GetSchemeClassifier()); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | bool ZeroSuggestProvider::ShouldAppendExtraParams( |
| 215 | const SearchSuggestionParser::SuggestResult& result) const { |
| 216 | // We always use the default provider for search, so append the params. |
| 217 | return true; |
| 218 | } |
| 219 | |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 220 | void ZeroSuggestProvider::RecordDeletionResult(bool success) { |
| 221 | if (success) { |
| 222 | base::RecordAction( |
| 223 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success")); |
| 224 | } else { |
| 225 | base::RecordAction( |
| 226 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure")); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) { |
| 231 | DCHECK(!done_); |
| 232 | DCHECK_EQ(fetcher_.get(), source); |
| 233 | |
| 234 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED); |
| 235 | |
| 236 | bool results_updated = false; |
| 237 | if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { |
| 238 | std::string json_data = SearchSuggestionParser::ExtractJsonData(source); |
| 239 | scoped_ptr<base::Value> data( |
| 240 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
| 241 | if (data) { |
| 242 | if (StoreSuggestionResponse(json_data, *data)) |
| 243 | return; |
| 244 | results_updated = ParseSuggestResults( |
| 245 | *data, kDefaultZeroSuggestRelevance, false, &results_); |
| 246 | } |
| 247 | } |
| 248 | fetcher_.reset(); |
| 249 | done_ = true; |
| 250 | ConvertResultsToAutocompleteMatches(); |
| 251 | listener_->OnProviderUpdate(results_updated); |
| 252 | } |
| 253 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 254 | bool ZeroSuggestProvider::StoreSuggestionResponse( |
| 255 | const std::string& json_data, |
| 256 | const base::Value& parsed_data) { |
| 257 | if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() || |
| 258 | json_data.empty()) |
| 259 | return false; |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 260 | client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults, |
| 261 | json_data); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 262 | |
| 263 | // If we received an empty result list, we should update the display, as it |
| 264 | // may be showing cached results that should not be shown. |
| 265 | const base::ListValue* root_list = NULL; |
| 266 | const base::ListValue* results_list = NULL; |
| 267 | if (parsed_data.GetAsList(&root_list) && |
| 268 | root_list->GetList(1, &results_list) && |
| 269 | results_list->empty()) |
| 270 | return false; |
| 271 | |
| 272 | // We are finished with the request and want to bail early. |
| 273 | if (results_from_cache_) |
| 274 | done_ = true; |
| 275 | |
| 276 | return results_from_cache_; |
| 277 | } |
| 278 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 279 | void ZeroSuggestProvider::AddSuggestResultsToMap( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 280 | const SearchSuggestionParser::SuggestResults& results, |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 281 | MatchMap* map) { |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 282 | for (size_t i = 0; i < results.size(); ++i) |
[email protected] | 7bc5e16 | 2014-08-15 19:41:11 | [diff] [blame] | 283 | AddMatchToMap(results[i], std::string(), i, false, false, map); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 284 | } |
| 285 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 286 | AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 287 | const SearchSuggestionParser::NavigationResult& navigation) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 288 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame] | 289 | navigation.type()); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 290 | match.destination_url = navigation.url(); |
| 291 | |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 292 | // Zero suggest results should always omit protocols and never appear bold. |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 293 | match.contents = url_formatter::FormatUrl( |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame^] | 294 | navigation.url(), url_formatter::kFormatUrlOmitAll, |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 295 | net::UnescapeRule::SPACES, nullptr, nullptr, nullptr); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 296 | match.fill_into_edit += |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 297 | AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 298 | navigation.url(), match.contents, client()->GetSchemeClassifier()); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 299 | |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 300 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 301 | match.contents.length(), ACMatchClassification::URL, |
| 302 | &match.contents_class); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 303 | |
| 304 | match.description = |
| 305 | AutocompleteMatch::SanitizeString(navigation.description()); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 306 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 307 | match.description.length(), ACMatchClassification::NONE, |
| 308 | &match.description_class); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 309 | return match; |
| 310 | } |
| 311 | |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 312 | void ZeroSuggestProvider::Run(const GURL& suggest_url) { |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 313 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
| 314 | most_visited_urls_.clear(); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 315 | scoped_refptr<history::TopSites> ts = client()->GetTopSites(); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 316 | if (ts) { |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 317 | waiting_for_most_visited_urls_request_ = true; |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 318 | ts->GetMostVisitedURLs( |
| 319 | base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, |
[email protected] | ce767ab2 | 2013-11-12 03:50:09 | [diff] [blame] | 320 | weak_ptr_factory_.GetWeakPtr()), false); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 321 | } |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 322 | } else { |
| 323 | const int kFetcherID = 1; |
dtapuska | dafcf89 | 2015-05-01 13:58:25 | [diff] [blame] | 324 | fetcher_ = net::URLFetcher::Create(kFetcherID, suggest_url, |
| 325 | net::URLFetcher::GET, this); |
amohammadkhan | f76ae11 | 2015-09-14 17:34:43 | [diff] [blame] | 326 | data_use_measurement::DataUseUserData::AttachToFetcher( |
| 327 | fetcher_.get(), data_use_measurement::DataUseUserData::OMNIBOX); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 328 | fetcher_->SetRequestContext(client()->GetRequestContext()); |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 329 | fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 330 | // Add Chrome experiment state to the request headers. |
| 331 | net::HttpRequestHeaders headers; |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 332 | variations::AppendVariationHeaders(fetcher_->GetOriginalURL(), |
| 333 | client()->IsOffTheRecord(), false, |
| 334 | &headers); |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 335 | fetcher_->SetExtraRequestHeaders(headers.ToString()); |
| 336 | fetcher_->Start(); |
| 337 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 338 | } |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 339 | } |
| 340 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 341 | void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
| 342 | const history::MostVisitedURLList& urls) { |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 343 | if (!waiting_for_most_visited_urls_request_) return; |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 344 | most_visited_urls_ = urls; |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 345 | waiting_for_most_visited_urls_request_ = false; |
| 346 | done_ = true; |
| 347 | ConvertResultsToAutocompleteMatches(); |
| 348 | listener_->OnProviderUpdate(true); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 349 | } |
| 350 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 351 | void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 352 | matches_.clear(); |
| 353 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 354 | TemplateURLService* template_url_service = client()->GetTemplateURLService(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 355 | const TemplateURL* default_provider = |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 356 | template_url_service->GetDefaultSearchProvider(); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 357 | // Fail if we can't set the clickthrough URL for query suggestions. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 358 | if (default_provider == NULL || |
| 359 | !default_provider->SupportsReplacement( |
| 360 | template_url_service->search_terms_data())) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 361 | return; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 362 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 363 | MatchMap map; |
| 364 | AddSuggestResultsToMap(results_.suggest_results, &map); |
| 365 | |
| 366 | const int num_query_results = map.size(); |
| 367 | const int num_nav_results = results_.navigation_results.size(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 368 | const int num_results = num_query_results + num_nav_results; |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 369 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results); |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame] | 370 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 371 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 372 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 373 | // Show Most Visited results after ZeroSuggest response is received. |
| 374 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
[email protected] | 3feb8b00 | 2013-10-14 23:50:13 | [diff] [blame] | 375 | if (!current_url_match_.destination_url.is_valid()) |
| 376 | return; |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 377 | matches_.push_back(current_url_match_); |
| 378 | int relevance = 600; |
| 379 | if (num_results > 0) { |
| 380 | UMA_HISTOGRAM_COUNTS( |
| 381 | "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual", |
| 382 | most_visited_urls_.size()); |
| 383 | } |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 384 | const base::string16 current_query_string16( |
| 385 | base::ASCIIToUTF16(current_query_)); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 386 | for (size_t i = 0; i < most_visited_urls_.size(); i++) { |
| 387 | const history::MostVisitedURL& url = most_visited_urls_[i]; |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 388 | SearchSuggestionParser::NavigationResult nav( |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 389 | client()->GetSchemeClassifier(), url.url, |
[email protected] | 7720fc3 | 2014-07-09 06:10:05 | [diff] [blame] | 390 | AutocompleteMatchType::NAVSUGGEST, url.title, std::string(), false, |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame^] | 391 | relevance, true, current_query_string16); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 392 | matches_.push_back(NavigationToMatch(nav)); |
| 393 | --relevance; |
| 394 | } |
| 395 | return; |
| 396 | } |
| 397 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 398 | if (num_results == 0) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 399 | return; |
| 400 | |
| 401 | // TODO(jered): Rip this out once the first match is decoupled from the |
| 402 | // current typing in the omnibox. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 403 | matches_.push_back(current_url_match_); |
| 404 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 405 | for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 406 | matches_.push_back(it->second); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 407 | |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 408 | const SearchSuggestionParser::NavigationResults& nav_results( |
| 409 | results_.navigation_results); |
| 410 | for (SearchSuggestionParser::NavigationResults::const_iterator it( |
| 411 | nav_results.begin()); it != nav_results.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 412 | matches_.push_back(NavigationToMatch(*it)); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 413 | } |
| 414 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 415 | AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 416 | // The placeholder suggestion for the current URL has high relevance so |
| 417 | // that it is in the first suggestion slot and inline autocompleted. It |
| 418 | // gets dropped as soon as the user types something. |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 419 | return VerbatimMatchForURL(client(), permanent_text_, |
| 420 | current_page_classification_, |
| 421 | results_.verbatim_relevance); |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 422 | } |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 423 | |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 424 | bool ZeroSuggestProvider::ShouldShowNonContextualZeroSuggest( |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 425 | const GURL& suggest_url, |
| 426 | const GURL& current_page_url) const { |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 427 | const TemplateURLService* template_url_service = |
| 428 | client()->GetTemplateURLService(); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 429 | if (!ZeroSuggestEnabled(suggest_url, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 430 | template_url_service->GetDefaultSearchProvider(), |
[email protected] | e6477f1 | 2014-08-05 07:59:54 | [diff] [blame] | 431 | current_page_classification_, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 432 | template_url_service->search_terms_data(), client())) |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 433 | return false; |
| 434 | |
| 435 | // If we cannot send URLs, then only the MostVisited and Personalized |
| 436 | // variations can be shown. |
| 437 | if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() && |
| 438 | !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 439 | return false; |
| 440 | |
| 441 | // Only show zero suggest for HTTP[S] pages. |
| 442 | // TODO(mariakhomenko): We may be able to expand this set to include pages |
| 443 | // with other schemes (e.g. chrome://). That may require improvements to |
| 444 | // the formatting of the verbatim result returned by MatchForCurrentURL(). |
| 445 | if (!current_page_url.is_valid() || |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 446 | ((current_page_url.scheme() != url::kHttpScheme) && |
| 447 | (current_page_url.scheme() != url::kHttpsScheme))) |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 448 | return false; |
| 449 | |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 450 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() && |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 451 | client() |
| 452 | ->GetTemplateURLService() |
| 453 | ->IsSearchResultsPageFromDefaultSearchProvider(current_page_url)) |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 454 | return false; |
| 455 | |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 456 | return true; |
| 457 | } |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 458 | |
| 459 | void ZeroSuggestProvider::MaybeUseCachedSuggestions() { |
| 460 | if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 461 | return; |
| 462 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 463 | std::string json_data = |
| 464 | client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 465 | if (!json_data.empty()) { |
[email protected] | 2c802d1 | 2014-07-31 12:57:14 | [diff] [blame] | 466 | scoped_ptr<base::Value> data( |
| 467 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 468 | if (data && ParseSuggestResults( |
| 469 | *data, kDefaultZeroSuggestRelevance, false, &results_)) { |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 470 | ConvertResultsToAutocompleteMatches(); |
| 471 | results_from_cache_ = !matches_.empty(); |
| 472 | } |
| 473 | } |
| 474 | } |