blob: 5b2ede3128e1a15b37dd3d954d6546bbb52e13c0 [file] [log] [blame]
[email protected]6ce7f612012-09-05 23:53:071// 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]bb1fb2b2013-05-31 00:21:018#include "base/i18n/case_conversion.h"
[email protected]6ce7f612012-09-05 23:53:079#include "base/json/json_string_value_serializer.h"
[email protected]bb1fb2b2013-05-31 00:21:0110#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5711#include "base/prefs/pref_service.h"
[email protected]98570e12013-06-10 19:54:2212#include "base/strings/string16.h"
13#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2014#include "base/strings/utf_string_conversions.h"
[email protected]4dcb7972013-06-28 15:15:4115#include "base/time/time.h"
[email protected]2d915782013-08-29 09:50:2116#include "chrome/browser/autocomplete/autocomplete_classifier.h"
17#include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
[email protected]6ce7f612012-09-05 23:53:0718#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]bb1fb2b2013-05-31 00:21:0121#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]8f064e52013-09-18 01:17:1424#include "chrome/browser/history/history_types.h"
25#include "chrome/browser/history/top_sites.h"
[email protected]bb1fb2b2013-05-31 00:21:0126#include "chrome/browser/metrics/variations/variations_http_header_provider.h"
[email protected]bb1fb2b2013-05-31 00:21:0127#include "chrome/browser/omnibox/omnibox_field_trial.h"
[email protected]6ce7f612012-09-05 23:53:0728#include "chrome/browser/profiles/profile.h"
[email protected]bb1fb2b2013-05-31 00:21:0129#include "chrome/browser/search/search.h"
[email protected]6ce7f612012-09-05 23:53:0730#include "chrome/browser/search_engines/template_url_service.h"
31#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]4f3b4462013-07-27 19:20:1832#include "chrome/common/net/url_fixer_upper.h"
[email protected]a00008d42012-09-15 05:07:5833#include "chrome/common/pref_names.h"
[email protected]6ce7f612012-09-05 23:53:0734#include "chrome/common/url_constants.h"
[email protected]0cfddf1b2014-03-12 01:46:0035#include "content/public/browser/user_metrics.h"
[email protected]bb1fb2b2013-05-31 00:21:0136#include "net/base/escape.h"
[email protected]6ce7f612012-09-05 23:53:0737#include "net/base/load_flags.h"
[email protected]bb1fb2b2013-05-31 00:21:0138#include "net/base/net_util.h"
39#include "net/http/http_request_headers.h"
[email protected]6ce7f612012-09-05 23:53:0740#include "net/http/http_response_headers.h"
41#include "net/url_request/url_fetcher.h"
42#include "net/url_request/url_request_status.h"
[email protected]761fa4702013-07-02 15:25:1543#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0744
45namespace {
[email protected]bb1fb2b2013-05-31 00:21:0146
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.
56enum 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
63void 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.
70const int kDefaultVerbatimZeroSuggestRelevance = 1300;
71
72// Relevance value to use if it was not set explicitly by the server.
73const int kDefaultZeroSuggestRelevance = 100;
74
[email protected]6ce7f612012-09-05 23:53:0775} // namespace
76
[email protected]a00008d42012-09-15 05:07:5877// static
78ZeroSuggestProvider* ZeroSuggestProvider::Create(
79 AutocompleteProviderListener* listener,
80 Profile* profile) {
[email protected]bb1fb2b2013-05-31 00:21:0181 return new ZeroSuggestProvider(listener, profile);
[email protected]6ce7f612012-09-05 23:53:0782}
83
[email protected]6ce7f612012-09-05 23:53:0784void ZeroSuggestProvider::Start(const AutocompleteInput& input,
85 bool /*minimal_changes*/) {
[email protected]bb1fb2b2013-05-31 00:21:0186}
87
[email protected]bb1fb2b2013-05-31 00:21:0188void ZeroSuggestProvider::ResetSession() {
89 // The user has started editing in the omnibox, so leave
90 // |field_trial_triggered_in_session_| unchanged and set
91 // |field_trial_triggered_| to false since zero suggest is inactive now.
92 field_trial_triggered_ = false;
[email protected]9c97f89c2013-06-25 03:12:1693 Stop(true);
[email protected]bb1fb2b2013-05-31 00:21:0194}
95
96void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) {
97 have_pending_request_ = false;
98 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
99
100 std::string json_data;
101 source->GetResponseAsString(&json_data);
102 const bool request_succeeded =
103 source->GetStatus().is_success() && source->GetResponseCode() == 200;
104
[email protected]bb1fb2b2013-05-31 00:21:01105 if (request_succeeded) {
[email protected]843e4afb2014-02-07 23:11:03106 scoped_ptr<base::Value> data(DeserializeJsonData(json_data));
[email protected]8f064e52013-09-18 01:17:14107 if (data.get())
[email protected]d4a94b92014-03-04 01:35:22108 ParseSuggestResults(*data.get(), false, &results_);
[email protected]bb1fb2b2013-05-31 00:21:01109 }
110 done_ = true;
111
[email protected]8f064e52013-09-18 01:17:14112 ConvertResultsToAutocompleteMatches();
113 if (!matches_.empty())
[email protected]bb1fb2b2013-05-31 00:21:01114 listener_->OnProviderUpdate(true);
[email protected]6ce7f612012-09-05 23:53:07115}
116
[email protected]25d7e742013-07-30 17:31:06117void ZeroSuggestProvider::StartZeroSuggest(
[email protected]9b9fa672013-11-07 06:04:52118 const GURL& current_page_url,
[email protected]25d7e742013-07-30 17:31:06119 AutocompleteInput::PageClassification page_classification,
[email protected]96920152013-12-04 21:00:16120 const base::string16& permanent_text) {
[email protected]bb1fb2b2013-05-31 00:21:01121 Stop(true);
122 field_trial_triggered_ = false;
123 field_trial_triggered_in_session_ = false;
[email protected]9b9fa672013-11-07 06:04:52124 permanent_text_ = permanent_text;
125 current_query_ = current_page_url.spec();
126 current_page_classification_ = page_classification;
127 current_url_match_ = MatchForCurrentURL();
128
129 const TemplateURL* default_provider =
130 template_url_service_->GetDefaultSearchProvider();
131 if (default_provider == NULL)
132 return;
[email protected]96920152013-12-04 21:00:16133 base::string16 prefix;
[email protected]9b9fa672013-11-07 06:04:52134 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
135 search_term_args.current_page_url = current_query_;
136 GURL suggest_url(default_provider->suggestions_url_ref().
137 ReplaceSearchTerms(search_term_args));
[email protected]843e4afb2014-02-07 23:11:03138 if (!CanSendURL(current_page_url, suggest_url,
[email protected]9b9fa672013-11-07 06:04:52139 template_url_service_->GetDefaultSearchProvider(),
140 page_classification, profile_) ||
141 !OmniboxFieldTrial::InZeroSuggestFieldTrial())
[email protected]6ce7f612012-09-05 23:53:07142 return;
[email protected]6ce7f612012-09-05 23:53:07143 done_ = false;
[email protected]6ce7f612012-09-05 23:53:07144 // TODO(jered): Consider adding locally-sourced zero-suggestions here too.
145 // These may be useful on the NTP or more relevant to the user than server
146 // suggestions, if based on local browsing history.
[email protected]9b9fa672013-11-07 06:04:52147 Run(suggest_url);
[email protected]6ce7f612012-09-05 23:53:07148}
149
[email protected]bb1fb2b2013-05-31 00:21:01150ZeroSuggestProvider::ZeroSuggestProvider(
151 AutocompleteProviderListener* listener,
152 Profile* profile)
[email protected]02346202014-02-05 05:18:30153 : BaseSearchProvider(listener, profile,
154 AutocompleteProvider::TYPE_ZERO_SUGGEST),
[email protected]bb1fb2b2013-05-31 00:21:01155 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]bb1fb2b2013-05-31 00:21:01156 have_pending_request_(false),
[email protected]8f064e52013-09-18 01:17:14157 weak_ptr_factory_(this) {
[email protected]6ce7f612012-09-05 23:53:07158}
159
160ZeroSuggestProvider::~ZeroSuggestProvider() {
161}
162
[email protected]9487b392014-02-14 02:48:18163const TemplateURL* ZeroSuggestProvider::GetTemplateURL(
164 const SuggestResult& result) const {
165 // Zero suggest provider should not receive keyword results.
166 DCHECK(!result.from_keyword_provider());
167 return template_url_service_->GetDefaultSearchProvider();
168}
169
[email protected]d4a94b92014-03-04 01:35:22170const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
171 return AutocompleteInput(
172 base::string16(), base::string16::npos, base::string16(),
173 GURL(current_query_), current_page_classification_, true, false, false,
174 AutocompleteInput::ALL_MATCHES);
[email protected]9487b392014-02-14 02:48:18175}
176
177bool ZeroSuggestProvider::ShouldAppendExtraParams(
178 const SuggestResult& result) const {
179 // We always use the default provider for search, so append the params.
180 return true;
181}
182
[email protected]ef6866f2014-02-18 08:26:34183void ZeroSuggestProvider::StopSuggest() {
184 if (have_pending_request_)
185 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
186 have_pending_request_ = false;
187 fetcher_.reset();
188}
189
190void ZeroSuggestProvider::ClearAllResults() {
[email protected]00404742014-02-20 13:09:05191 // We do not call Clear() on |results_| to retain |verbatim_relevance|
192 // value in the |results_| object. |verbatim_relevance| is used at the
193 // beginning of the next StartZeroSuggest() call to determine the current url
194 // match relevance.
195 results_.suggest_results.clear();
196 results_.navigation_results.clear();
[email protected]ef6866f2014-02-18 08:26:34197 current_query_.clear();
198 matches_.clear();
199}
200
[email protected]d4a94b92014-03-04 01:35:22201int ZeroSuggestProvider::GetDefaultResultRelevance() const {
202 return kDefaultZeroSuggestRelevance;
203}
204
[email protected]0cfddf1b2014-03-12 01:46:00205void ZeroSuggestProvider::RecordDeletionResult(bool success) {
206 if (success) {
207 content::RecordAction(
208 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
209 } else {
210 content::RecordAction(
211 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
212 }
213}
214
[email protected]bb1fb2b2013-05-31 00:21:01215void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]02346202014-02-05 05:18:30216 const SuggestResults& results,
[email protected]02346202014-02-05 05:18:30217 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22218 for (size_t i = 0; i < results.size(); ++i)
219 AddMatchToMap(results[i], std::string(), i, map);
[email protected]bb1fb2b2013-05-31 00:21:01220}
221
[email protected]bb1fb2b2013-05-31 00:21:01222AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]02346202014-02-05 05:18:30223 const NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01224 AutocompleteMatch match(this, navigation.relevance(), false,
225 AutocompleteMatchType::NAVSUGGEST);
226 match.destination_url = navigation.url();
227
[email protected]23db6492014-01-16 02:35:30228 // Zero suggest results should always omit protocols and never appear bold.
[email protected]bb1fb2b2013-05-31 00:21:01229 const std::string languages(
230 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
231 match.contents = net::FormatUrl(navigation.url(), languages,
232 net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, NULL, NULL, NULL);
233 match.fill_into_edit +=
234 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
235 match.contents);
[email protected]bb1fb2b2013-05-31 00:21:01236
[email protected]b959d7d42013-12-13 17:26:37237 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01238 match.contents.length(), ACMatchClassification::URL,
239 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16240
241 match.description =
242 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37243 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16244 match.description.length(), ACMatchClassification::NONE,
245 &match.description_class);
[email protected]bb1fb2b2013-05-31 00:21:01246 return match;
247}
248
[email protected]9b9fa672013-11-07 06:04:52249void ZeroSuggestProvider::Run(const GURL& suggest_url) {
[email protected]bb1fb2b2013-05-31 00:21:01250 have_pending_request_ = false;
251 const int kFetcherID = 1;
[email protected]bb1fb2b2013-05-31 00:21:01252 fetcher_.reset(
253 net::URLFetcher::Create(kFetcherID,
254 suggest_url,
255 net::URLFetcher::GET, this));
256 fetcher_->SetRequestContext(profile_->GetRequestContext());
257 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
258 // Add Chrome experiment state to the request headers.
259 net::HttpRequestHeaders headers;
260 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
261 fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
262 fetcher_->SetExtraRequestHeaders(headers.ToString());
263
264 fetcher_->Start();
[email protected]8f064e52013-09-18 01:17:14265
266 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
267 most_visited_urls_.clear();
268 history::TopSites* ts = profile_->GetTopSites();
269 if (ts) {
270 ts->GetMostVisitedURLs(
271 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
[email protected]ce767ab22013-11-12 03:50:09272 weak_ptr_factory_.GetWeakPtr()), false);
[email protected]8f064e52013-09-18 01:17:14273 }
274 }
[email protected]bb1fb2b2013-05-31 00:21:01275 have_pending_request_ = true;
276 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
277}
278
[email protected]8f064e52013-09-18 01:17:14279void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
280 const history::MostVisitedURLList& urls) {
281 most_visited_urls_ = urls;
282}
283
[email protected]9c97f89c2013-06-25 03:12:16284void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01285 matches_.clear();
286
287 const TemplateURL* default_provider =
[email protected]6ce7f612012-09-05 23:53:07288 template_url_service_->GetDefaultSearchProvider();
289 // Fail if we can't set the clickthrough URL for query suggestions.
[email protected]bb1fb2b2013-05-31 00:21:01290 if (default_provider == NULL || !default_provider->SupportsReplacement())
[email protected]6ce7f612012-09-05 23:53:07291 return;
[email protected]6ce7f612012-09-05 23:53:07292
[email protected]00404742014-02-20 13:09:05293 MatchMap map;
294 AddSuggestResultsToMap(results_.suggest_results, &map);
295
296 const int num_query_results = map.size();
297 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01298 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16299 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
300 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
301 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01302
[email protected]8f064e52013-09-18 01:17:14303 // Show Most Visited results after ZeroSuggest response is received.
304 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13305 if (!current_url_match_.destination_url.is_valid())
306 return;
[email protected]8f064e52013-09-18 01:17:14307 matches_.push_back(current_url_match_);
308 int relevance = 600;
309 if (num_results > 0) {
310 UMA_HISTOGRAM_COUNTS(
311 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
312 most_visited_urls_.size());
313 }
[email protected]23db6492014-01-16 02:35:30314 const base::string16 current_query_string16(
315 base::ASCIIToUTF16(current_query_));
316 const std::string languages(
317 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]8f064e52013-09-18 01:17:14318 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
319 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]02346202014-02-05 05:18:30320 NavigationResult nav(*this, url.url, url.title, false, relevance, true,
[email protected]23db6492014-01-16 02:35:30321 current_query_string16, languages);
[email protected]8f064e52013-09-18 01:17:14322 matches_.push_back(NavigationToMatch(nav));
323 --relevance;
324 }
325 return;
326 }
327
[email protected]9c97f89c2013-06-25 03:12:16328 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01329 return;
330
331 // TODO(jered): Rip this out once the first match is decoupled from the
332 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01333 matches_.push_back(current_url_match_);
334
[email protected]00404742014-02-20 13:09:05335 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01336 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01337
[email protected]00404742014-02-20 13:09:05338 const NavigationResults& nav_results(results_.navigation_results);
339 for (NavigationResults::const_iterator it(nav_results.begin());
340 it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01341 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07342}
343
[email protected]bb1fb2b2013-05-31 00:21:01344AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]96920152013-12-04 21:00:16345 AutocompleteInput input(permanent_text_, base::string16::npos, base::string16(),
[email protected]25d7e742013-07-30 17:31:06346 GURL(current_query_), current_page_classification_,
[email protected]bb1fb2b2013-05-31 00:21:01347 false, false, true, AutocompleteInput::ALL_MATCHES);
[email protected]6ce7f612012-09-05 23:53:07348
[email protected]2d915782013-08-29 09:50:21349 AutocompleteMatch match;
350 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
[email protected]51abb7b2014-02-09 23:00:08351 permanent_text_, false, true, current_page_classification_, &match, NULL);
[email protected]bb1fb2b2013-05-31 00:21:01352 match.is_history_what_you_typed_match = false;
[email protected]45f89a92013-08-12 13:41:36353 match.allowed_to_be_default_match = true;
[email protected]6ce7f612012-09-05 23:53:07354
[email protected]bb1fb2b2013-05-31 00:21:01355 // The placeholder suggestion for the current URL has high relevance so
356 // that it is in the first suggestion slot and inline autocompleted. It
357 // gets dropped as soon as the user types something.
[email protected]00404742014-02-20 13:09:05358 match.relevance = GetVerbatimRelevance();
[email protected]6ce7f612012-09-05 23:53:07359
[email protected]bb1fb2b2013-05-31 00:21:01360 return match;
[email protected]6ce7f612012-09-05 23:53:07361}
[email protected]00404742014-02-20 13:09:05362
363int ZeroSuggestProvider::GetVerbatimRelevance() const {
364 return results_.verbatim_relevance >= 0 ?
365 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
366}