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