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