[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] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 11 | #include "base/prefs/pref_service.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
| 13 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 4dcb797 | 2013-06-28 15:15:41 | [diff] [blame] | 15 | #include "base/time/time.h" |
[email protected] | 2d91578 | 2013-08-29 09:50:21 | [diff] [blame] | 16 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| 17 | #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 18 | #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 19 | #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 20 | #include "chrome/browser/autocomplete/history_url_provider.h" |
| 21 | #include "chrome/browser/autocomplete/search_provider.h" |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 22 | #include "chrome/browser/history/history_types.h" |
| 23 | #include "chrome/browser/history/top_sites.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 24 | #include "chrome/browser/omnibox/omnibox_field_trial.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 25 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 26 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 27 | #include "chrome/common/pref_names.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 28 | #include "chrome/common/url_constants.h" |
[email protected] | 4209e424 | 2014-06-28 05:38:28 | [diff] [blame] | 29 | #include "components/autocomplete/autocomplete_input.h" |
[email protected] | e7e0771 | 2014-07-30 08:53:23 | [diff] [blame] | 30 | #include "components/autocomplete/autocomplete_match.h" |
[email protected] | 3dc75b1 | 2014-06-08 00:02:22 | [diff] [blame] | 31 | #include "components/metrics/proto/omnibox_input_type.pb.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame] | 32 | #include "components/pref_registry/pref_registry_syncable.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 33 | #include "components/search_engines/template_url_service.h" |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 34 | #include "components/variations/variations_http_header_provider.h" |
[email protected] | 0cfddf1b | 2014-03-12 01:46:00 | [diff] [blame] | 35 | #include "content/public/browser/user_metrics.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 36 | #include "net/base/escape.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 37 | #include "net/base/load_flags.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 38 | #include "net/base/net_util.h" |
| 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 | |
| 68 | // The maximum relevance of the top match from this provider. |
| 69 | const int kDefaultVerbatimZeroSuggestRelevance = 1300; |
| 70 | |
| 71 | // Relevance value to use if it was not set explicitly by the server. |
| 72 | const int kDefaultZeroSuggestRelevance = 100; |
| 73 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 74 | } // namespace |
| 75 | |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 76 | // static |
| 77 | ZeroSuggestProvider* ZeroSuggestProvider::Create( |
| 78 | AutocompleteProviderListener* listener, |
| 79 | Profile* profile) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 80 | return new ZeroSuggestProvider(listener, profile); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 83 | // static |
| 84 | void ZeroSuggestProvider::RegisterProfilePrefs( |
| 85 | user_prefs::PrefRegistrySyncable* registry) { |
| 86 | registry->RegisterStringPref( |
| 87 | prefs::kZeroSuggestCachedResults, |
| 88 | std::string(), |
| 89 | user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 90 | } |
| 91 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 92 | void ZeroSuggestProvider::Start(const AutocompleteInput& input, |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 93 | bool minimal_changes) { |
| 94 | matches_.clear(); |
[email protected] | 3dc75b1 | 2014-06-08 00:02:22 | [diff] [blame] | 95 | if (input.type() == metrics::OmniboxInputType::INVALID) |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 96 | return; |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 97 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 98 | Stop(true); |
| 99 | field_trial_triggered_ = false; |
| 100 | field_trial_triggered_in_session_ = false; |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 101 | results_from_cache_ = false; |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 102 | permanent_text_ = input.text(); |
| 103 | current_query_ = input.current_url().spec(); |
| 104 | current_page_classification_ = input.current_page_classification(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 105 | current_url_match_ = MatchForCurrentURL(); |
| 106 | |
| 107 | const TemplateURL* default_provider = |
| 108 | template_url_service_->GetDefaultSearchProvider(); |
| 109 | if (default_provider == NULL) |
| 110 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 111 | |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 112 | base::string16 prefix; |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 113 | TemplateURLRef::SearchTermsArgs search_term_args(prefix); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 114 | GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms( |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 115 | search_term_args, template_url_service_->search_terms_data())); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 116 | if (!suggest_url.is_valid()) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 117 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 118 | |
| 119 | // No need to send the current page URL in personalized suggest field trial. |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 120 | if (CanSendURL(input.current_url(), suggest_url, default_provider, |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 121 | current_page_classification_, profile_) && |
| 122 | !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { |
| 123 | // Update suggest_url to include the current_page_url. |
| 124 | search_term_args.current_page_url = current_query_; |
| 125 | suggest_url = GURL(default_provider->suggestions_url_ref(). |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 126 | ReplaceSearchTerms( |
| 127 | search_term_args, |
| 128 | template_url_service_->search_terms_data())); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 129 | } else if (!CanShowZeroSuggestWithoutSendingURL(suggest_url, |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 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 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 142 | void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) { |
| 143 | if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { |
| 144 | // Remove the deleted match from the cache, so it is not shown to the user |
| 145 | // again. Since we cannot remove just one result, blow away the cache. |
| 146 | profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults, |
| 147 | std::string()); |
| 148 | } |
| 149 | BaseSearchProvider::DeleteMatch(match); |
| 150 | } |
| 151 | |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 152 | void ZeroSuggestProvider::ResetSession() { |
| 153 | // The user has started editing in the omnibox, so leave |
| 154 | // |field_trial_triggered_in_session_| unchanged and set |
| 155 | // |field_trial_triggered_| to false since zero suggest is inactive now. |
| 156 | field_trial_triggered_ = false; |
| 157 | } |
| 158 | |
[email protected] | ff8836429 | 2014-05-09 09:25:16 | [diff] [blame] | 159 | void ZeroSuggestProvider::ModifyProviderInfo( |
| 160 | metrics::OmniboxEventProto_ProviderInfo* provider_info) const { |
| 161 | if (!results_.suggest_results.empty() || !results_.navigation_results.empty()) |
| 162 | provider_info->set_times_returned_results_in_session(1); |
| 163 | } |
| 164 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 165 | ZeroSuggestProvider::ZeroSuggestProvider( |
| 166 | AutocompleteProviderListener* listener, |
| 167 | Profile* profile) |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 168 | : BaseSearchProvider(listener, profile, |
| 169 | AutocompleteProvider::TYPE_ZERO_SUGGEST), |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 170 | template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 171 | results_from_cache_(false), |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 172 | weak_ptr_factory_(this) { |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | ZeroSuggestProvider::~ZeroSuggestProvider() { |
| 176 | } |
| 177 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 178 | bool ZeroSuggestProvider::StoreSuggestionResponse( |
| 179 | const std::string& json_data, |
| 180 | const base::Value& parsed_data) { |
| 181 | if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() || |
| 182 | json_data.empty()) |
| 183 | return false; |
| 184 | profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults, json_data); |
| 185 | |
| 186 | // If we received an empty result list, we should update the display, as it |
| 187 | // may be showing cached results that should not be shown. |
| 188 | const base::ListValue* root_list = NULL; |
| 189 | const base::ListValue* results_list = NULL; |
| 190 | if (parsed_data.GetAsList(&root_list) && |
| 191 | root_list->GetList(1, &results_list) && |
| 192 | results_list->empty()) |
| 193 | return false; |
| 194 | |
| 195 | // We are finished with the request and want to bail early. |
| 196 | if (results_from_cache_) |
| 197 | done_ = true; |
| 198 | |
| 199 | return results_from_cache_; |
| 200 | } |
| 201 | |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 202 | const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const { |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 203 | // Zero suggest provider should not receive keyword results. |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 204 | DCHECK(!is_keyword); |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 205 | return template_url_service_->GetDefaultSearchProvider(); |
| 206 | } |
| 207 | |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 208 | const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const { |
| 209 | return AutocompleteInput( |
| 210 | base::string16(), base::string16::npos, base::string16(), |
| 211 | GURL(current_query_), current_page_classification_, true, false, false, |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 212 | true, ChromeAutocompleteSchemeClassifier(profile_)); |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 215 | SearchSuggestionParser::Results* ZeroSuggestProvider::GetResultsToFill( |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 216 | bool is_keyword) { |
| 217 | DCHECK(!is_keyword); |
| 218 | return &results_; |
| 219 | } |
| 220 | |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 221 | bool ZeroSuggestProvider::ShouldAppendExtraParams( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 222 | const SearchSuggestionParser::SuggestResult& result) const { |
[email protected] | 9487b39 | 2014-02-14 02:48:18 | [diff] [blame] | 223 | // We always use the default provider for search, so append the params. |
| 224 | return true; |
| 225 | } |
| 226 | |
[email protected] | ef6866f | 2014-02-18 08:26:34 | [diff] [blame] | 227 | void ZeroSuggestProvider::StopSuggest() { |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 228 | if (suggest_results_pending_ > 0) |
[email protected] | ef6866f | 2014-02-18 08:26:34 | [diff] [blame] | 229 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 230 | suggest_results_pending_ = 0; |
[email protected] | ef6866f | 2014-02-18 08:26:34 | [diff] [blame] | 231 | fetcher_.reset(); |
| 232 | } |
| 233 | |
| 234 | void ZeroSuggestProvider::ClearAllResults() { |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 235 | // We do not call Clear() on |results_| to retain |verbatim_relevance| |
| 236 | // value in the |results_| object. |verbatim_relevance| is used at the |
| 237 | // beginning of the next StartZeroSuggest() call to determine the current url |
| 238 | // match relevance. |
| 239 | results_.suggest_results.clear(); |
| 240 | results_.navigation_results.clear(); |
[email protected] | ef6866f | 2014-02-18 08:26:34 | [diff] [blame] | 241 | current_query_.clear(); |
[email protected] | ef6866f | 2014-02-18 08:26:34 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 244 | int ZeroSuggestProvider::GetDefaultResultRelevance() const { |
| 245 | return kDefaultZeroSuggestRelevance; |
| 246 | } |
| 247 | |
[email protected] | 0cfddf1b | 2014-03-12 01:46:00 | [diff] [blame] | 248 | void ZeroSuggestProvider::RecordDeletionResult(bool success) { |
| 249 | if (success) { |
| 250 | content::RecordAction( |
| 251 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success")); |
| 252 | } else { |
| 253 | content::RecordAction( |
| 254 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure")); |
| 255 | } |
| 256 | } |
| 257 | |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 258 | void ZeroSuggestProvider::LogFetchComplete(bool success, bool is_keyword) { |
| 259 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED); |
| 260 | } |
| 261 | |
| 262 | bool ZeroSuggestProvider::IsKeywordFetcher( |
| 263 | const net::URLFetcher* fetcher) const { |
| 264 | // ZeroSuggestProvider does not have a keyword provider. |
| 265 | DCHECK_EQ(fetcher, fetcher_.get()); |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | void ZeroSuggestProvider::UpdateMatches() { |
| 270 | done_ = true; |
| 271 | ConvertResultsToAutocompleteMatches(); |
| 272 | } |
| 273 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 274 | void ZeroSuggestProvider::AddSuggestResultsToMap( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 275 | const SearchSuggestionParser::SuggestResults& results, |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 276 | MatchMap* map) { |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 277 | for (size_t i = 0; i < results.size(); ++i) |
[email protected] | 57482a7 | 2014-03-14 22:27:37 | [diff] [blame] | 278 | AddMatchToMap(results[i], std::string(), i, false, map); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 281 | AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 282 | const SearchSuggestionParser::NavigationResult& navigation) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 283 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame] | 284 | navigation.type()); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 285 | match.destination_url = navigation.url(); |
| 286 | |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 287 | // Zero suggest results should always omit protocols and never appear bold. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 288 | const std::string languages( |
| 289 | profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 290 | match.contents = net::FormatUrl(navigation.url(), languages, |
| 291 | net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, NULL, NULL, NULL); |
| 292 | match.fill_into_edit += |
| 293 | AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(), |
[email protected] | a817ed39 | 2014-06-27 05:03:00 | [diff] [blame] | 294 | match.contents, ChromeAutocompleteSchemeClassifier(profile_)); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 295 | |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 296 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 297 | match.contents.length(), ACMatchClassification::URL, |
| 298 | &match.contents_class); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 299 | |
| 300 | match.description = |
| 301 | AutocompleteMatch::SanitizeString(navigation.description()); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 302 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 303 | match.description.length(), ACMatchClassification::NONE, |
| 304 | &match.description_class); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 305 | return match; |
| 306 | } |
| 307 | |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 308 | void ZeroSuggestProvider::Run(const GURL& suggest_url) { |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 309 | suggest_results_pending_ = 0; |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 310 | const int kFetcherID = 1; |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 311 | fetcher_.reset( |
| 312 | net::URLFetcher::Create(kFetcherID, |
| 313 | suggest_url, |
| 314 | net::URLFetcher::GET, this)); |
| 315 | fetcher_->SetRequestContext(profile_->GetRequestContext()); |
| 316 | fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 317 | // Add Chrome experiment state to the request headers. |
| 318 | net::HttpRequestHeaders headers; |
[email protected] | 71011c168 | 2014-07-09 17:19:16 | [diff] [blame] | 319 | variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 320 | fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); |
| 321 | fetcher_->SetExtraRequestHeaders(headers.ToString()); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 322 | fetcher_->Start(); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 323 | |
| 324 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
| 325 | most_visited_urls_.clear(); |
| 326 | history::TopSites* ts = profile_->GetTopSites(); |
| 327 | if (ts) { |
| 328 | ts->GetMostVisitedURLs( |
| 329 | base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, |
[email protected] | ce767ab2 | 2013-11-12 03:50:09 | [diff] [blame] | 330 | weak_ptr_factory_.GetWeakPtr()), false); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 331 | } |
| 332 | } |
[email protected] | cfa164bf | 2014-03-19 11:51:15 | [diff] [blame] | 333 | suggest_results_pending_ = 1; |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 334 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
| 335 | } |
| 336 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 337 | void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
| 338 | const history::MostVisitedURLList& urls) { |
| 339 | most_visited_urls_ = urls; |
| 340 | } |
| 341 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 342 | void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 343 | matches_.clear(); |
| 344 | |
| 345 | const TemplateURL* default_provider = |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 346 | template_url_service_->GetDefaultSearchProvider(); |
| 347 | // Fail if we can't set the clickthrough URL for query suggestions. |
[email protected] | ce7ee5f | 2014-06-16 23:41:19 | [diff] [blame] | 348 | if (default_provider == NULL || !default_provider->SupportsReplacement( |
| 349 | template_url_service_->search_terms_data())) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 350 | return; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 351 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 352 | MatchMap map; |
| 353 | AddSuggestResultsToMap(results_.suggest_results, &map); |
| 354 | |
| 355 | const int num_query_results = map.size(); |
| 356 | const int num_nav_results = results_.navigation_results.size(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 357 | const int num_results = num_query_results + num_nav_results; |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 358 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results); |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame] | 359 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 360 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 361 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 362 | // Show Most Visited results after ZeroSuggest response is received. |
| 363 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
[email protected] | 3feb8b00 | 2013-10-14 23:50:13 | [diff] [blame] | 364 | if (!current_url_match_.destination_url.is_valid()) |
| 365 | return; |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 366 | matches_.push_back(current_url_match_); |
| 367 | int relevance = 600; |
| 368 | if (num_results > 0) { |
| 369 | UMA_HISTOGRAM_COUNTS( |
| 370 | "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual", |
| 371 | most_visited_urls_.size()); |
| 372 | } |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 373 | const base::string16 current_query_string16( |
| 374 | base::ASCIIToUTF16(current_query_)); |
| 375 | const std::string languages( |
| 376 | profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 377 | for (size_t i = 0; i < most_visited_urls_.size(); i++) { |
| 378 | const history::MostVisitedURL& url = most_visited_urls_[i]; |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 379 | SearchSuggestionParser::NavigationResult nav( |
[email protected] | 7720fc3 | 2014-07-09 06:10:05 | [diff] [blame] | 380 | ChromeAutocompleteSchemeClassifier(profile_), url.url, |
| 381 | AutocompleteMatchType::NAVSUGGEST, url.title, std::string(), false, |
| 382 | relevance, true, current_query_string16, languages); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 383 | matches_.push_back(NavigationToMatch(nav)); |
| 384 | --relevance; |
| 385 | } |
| 386 | return; |
| 387 | } |
| 388 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 389 | if (num_results == 0) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 390 | return; |
| 391 | |
| 392 | // TODO(jered): Rip this out once the first match is decoupled from the |
| 393 | // current typing in the omnibox. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 394 | matches_.push_back(current_url_match_); |
| 395 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 396 | for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 397 | matches_.push_back(it->second); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 398 | |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 399 | const SearchSuggestionParser::NavigationResults& nav_results( |
| 400 | results_.navigation_results); |
| 401 | for (SearchSuggestionParser::NavigationResults::const_iterator it( |
| 402 | nav_results.begin()); it != nav_results.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 403 | matches_.push_back(NavigationToMatch(*it)); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 404 | } |
| 405 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 406 | AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { |
[email protected] | 2d91578 | 2013-08-29 09:50:21 | [diff] [blame] | 407 | AutocompleteMatch match; |
| 408 | AutocompleteClassifierFactory::GetForProfile(profile_)->Classify( |
[email protected] | 51abb7b | 2014-02-09 23:00:08 | [diff] [blame] | 409 | permanent_text_, false, true, current_page_classification_, &match, NULL); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 410 | match.is_history_what_you_typed_match = false; |
[email protected] | 45f89a9 | 2013-08-12 13:41:36 | [diff] [blame] | 411 | match.allowed_to_be_default_match = true; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 412 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 413 | // The placeholder suggestion for the current URL has high relevance so |
| 414 | // that it is in the first suggestion slot and inline autocompleted. It |
| 415 | // gets dropped as soon as the user types something. |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 416 | match.relevance = GetVerbatimRelevance(); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 417 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 418 | return match; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 419 | } |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 420 | |
| 421 | int ZeroSuggestProvider::GetVerbatimRelevance() const { |
| 422 | return results_.verbatim_relevance >= 0 ? |
| 423 | results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance; |
| 424 | } |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 425 | |
| 426 | bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL( |
| 427 | const GURL& suggest_url, |
| 428 | const GURL& current_page_url) const { |
| 429 | if (!ZeroSuggestEnabled(suggest_url, |
| 430 | template_url_service_->GetDefaultSearchProvider(), |
| 431 | current_page_classification_, profile_)) |
| 432 | return false; |
| 433 | |
| 434 | // If we cannot send URLs, then only the MostVisited and Personalized |
| 435 | // variations can be shown. |
| 436 | if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() && |
| 437 | !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 438 | return false; |
| 439 | |
| 440 | // Only show zero suggest for HTTP[S] pages. |
| 441 | // TODO(mariakhomenko): We may be able to expand this set to include pages |
| 442 | // with other schemes (e.g. chrome://). That may require improvements to |
| 443 | // the formatting of the verbatim result returned by MatchForCurrentURL(). |
| 444 | if (!current_page_url.is_valid() || |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 445 | ((current_page_url.scheme() != url::kHttpScheme) && |
| 446 | (current_page_url.scheme() != url::kHttpsScheme))) |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 447 | return false; |
| 448 | |
| 449 | return true; |
| 450 | } |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 451 | |
| 452 | void ZeroSuggestProvider::MaybeUseCachedSuggestions() { |
| 453 | if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 454 | return; |
| 455 | |
| 456 | std::string json_data = profile_->GetPrefs()->GetString( |
| 457 | prefs::kZeroSuggestCachedResults); |
| 458 | if (!json_data.empty()) { |
[email protected] | 2c802d1 | 2014-07-31 12:57:14 | [diff] [blame^] | 459 | scoped_ptr<base::Value> data( |
| 460 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 461 | if (data && ParseSuggestResults(*data.get(), false, &results_)) { |
| 462 | ConvertResultsToAutocompleteMatches(); |
| 463 | results_from_cache_ = !matches_.empty(); |
| 464 | } |
| 465 | } |
| 466 | } |