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