blob: 97386b852eb0bb654dbaa05bd20d399b814011ba [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]bb1fb2b2013-05-31 00:21:0135#include "net/base/escape.h"
[email protected]6ce7f612012-09-05 23:53:0736#include "net/base/load_flags.h"
[email protected]bb1fb2b2013-05-31 00:21:0137#include "net/base/net_util.h"
38#include "net/http/http_request_headers.h"
[email protected]6ce7f612012-09-05 23:53:0739#include "net/http/http_response_headers.h"
40#include "net/url_request/url_fetcher.h"
41#include "net/url_request/url_request_status.h"
[email protected]761fa4702013-07-02 15:25:1542#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0743
44namespace {
[email protected]bb1fb2b2013-05-31 00:21:0145
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.
55enum 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
62void 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.
69const int kDefaultVerbatimZeroSuggestRelevance = 1300;
70
71// Relevance value to use if it was not set explicitly by the server.
72const int kDefaultZeroSuggestRelevance = 100;
73
[email protected]6ce7f612012-09-05 23:53:0774} // namespace
75
[email protected]a00008d42012-09-15 05:07:5876// static
77ZeroSuggestProvider* ZeroSuggestProvider::Create(
78 AutocompleteProviderListener* listener,
79 Profile* profile) {
[email protected]bb1fb2b2013-05-31 00:21:0180 return new ZeroSuggestProvider(listener, profile);
[email protected]6ce7f612012-09-05 23:53:0781}
82
[email protected]6ce7f612012-09-05 23:53:0783void ZeroSuggestProvider::Start(const AutocompleteInput& input,
84 bool /*minimal_changes*/) {
[email protected]bb1fb2b2013-05-31 00:21:0185}
86
[email protected]bb1fb2b2013-05-31 00:21:0187void ZeroSuggestProvider::ResetSession() {
88 // The user has started editing in the omnibox, so leave
89 // |field_trial_triggered_in_session_| unchanged and set
90 // |field_trial_triggered_| to false since zero suggest is inactive now.
91 field_trial_triggered_ = false;
[email protected]9c97f89c2013-06-25 03:12:1692 Stop(true);
[email protected]bb1fb2b2013-05-31 00:21:0193}
94
95void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) {
96 have_pending_request_ = false;
97 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
98
99 std::string json_data;
100 source->GetResponseAsString(&json_data);
101 const bool request_succeeded =
102 source->GetStatus().is_success() && source->GetResponseCode() == 200;
103
[email protected]bb1fb2b2013-05-31 00:21:01104 if (request_succeeded) {
[email protected]843e4afb2014-02-07 23:11:03105 scoped_ptr<base::Value> data(DeserializeJsonData(json_data));
[email protected]8f064e52013-09-18 01:17:14106 if (data.get())
[email protected]d4a94b92014-03-04 01:35:22107 ParseSuggestResults(*data.get(), false, &results_);
[email protected]bb1fb2b2013-05-31 00:21:01108 }
109 done_ = true;
110
[email protected]8f064e52013-09-18 01:17:14111 ConvertResultsToAutocompleteMatches();
112 if (!matches_.empty())
[email protected]bb1fb2b2013-05-31 00:21:01113 listener_->OnProviderUpdate(true);
[email protected]6ce7f612012-09-05 23:53:07114}
115
[email protected]25d7e742013-07-30 17:31:06116void ZeroSuggestProvider::StartZeroSuggest(
[email protected]9b9fa672013-11-07 06:04:52117 const GURL& current_page_url,
[email protected]25d7e742013-07-30 17:31:06118 AutocompleteInput::PageClassification page_classification,
[email protected]96920152013-12-04 21:00:16119 const base::string16& permanent_text) {
[email protected]bb1fb2b2013-05-31 00:21:01120 Stop(true);
121 field_trial_triggered_ = false;
122 field_trial_triggered_in_session_ = false;
[email protected]9b9fa672013-11-07 06:04:52123 permanent_text_ = permanent_text;
124 current_query_ = current_page_url.spec();
125 current_page_classification_ = page_classification;
126 current_url_match_ = MatchForCurrentURL();
127
128 const TemplateURL* default_provider =
129 template_url_service_->GetDefaultSearchProvider();
130 if (default_provider == NULL)
131 return;
[email protected]96920152013-12-04 21:00:16132 base::string16 prefix;
[email protected]9b9fa672013-11-07 06:04:52133 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
134 search_term_args.current_page_url = current_query_;
135 GURL suggest_url(default_provider->suggestions_url_ref().
136 ReplaceSearchTerms(search_term_args));
[email protected]843e4afb2014-02-07 23:11:03137 if (!CanSendURL(current_page_url, suggest_url,
[email protected]9b9fa672013-11-07 06:04:52138 template_url_service_->GetDefaultSearchProvider(),
139 page_classification, profile_) ||
140 !OmniboxFieldTrial::InZeroSuggestFieldTrial())
[email protected]6ce7f612012-09-05 23:53:07141 return;
[email protected]6ce7f612012-09-05 23:53:07142 done_ = false;
[email protected]6ce7f612012-09-05 23:53:07143 // TODO(jered): Consider adding locally-sourced zero-suggestions here too.
144 // These may be useful on the NTP or more relevant to the user than server
145 // suggestions, if based on local browsing history.
[email protected]9b9fa672013-11-07 06:04:52146 Run(suggest_url);
[email protected]6ce7f612012-09-05 23:53:07147}
148
[email protected]bb1fb2b2013-05-31 00:21:01149ZeroSuggestProvider::ZeroSuggestProvider(
150 AutocompleteProviderListener* listener,
151 Profile* profile)
[email protected]02346202014-02-05 05:18:30152 : BaseSearchProvider(listener, profile,
153 AutocompleteProvider::TYPE_ZERO_SUGGEST),
[email protected]bb1fb2b2013-05-31 00:21:01154 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]bb1fb2b2013-05-31 00:21:01155 have_pending_request_(false),
[email protected]8f064e52013-09-18 01:17:14156 weak_ptr_factory_(this) {
[email protected]6ce7f612012-09-05 23:53:07157}
158
159ZeroSuggestProvider::~ZeroSuggestProvider() {
160}
161
[email protected]9487b392014-02-14 02:48:18162const TemplateURL* ZeroSuggestProvider::GetTemplateURL(
163 const SuggestResult& result) const {
164 // Zero suggest provider should not receive keyword results.
165 DCHECK(!result.from_keyword_provider());
166 return template_url_service_->GetDefaultSearchProvider();
167}
168
[email protected]d4a94b92014-03-04 01:35:22169const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
170 return AutocompleteInput(
171 base::string16(), base::string16::npos, base::string16(),
172 GURL(current_query_), current_page_classification_, true, false, false,
173 AutocompleteInput::ALL_MATCHES);
[email protected]9487b392014-02-14 02:48:18174}
175
176bool ZeroSuggestProvider::ShouldAppendExtraParams(
177 const SuggestResult& result) const {
178 // We always use the default provider for search, so append the params.
179 return true;
180}
181
[email protected]ef6866f2014-02-18 08:26:34182void ZeroSuggestProvider::StopSuggest() {
183 if (have_pending_request_)
184 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
185 have_pending_request_ = false;
186 fetcher_.reset();
187}
188
189void ZeroSuggestProvider::ClearAllResults() {
[email protected]00404742014-02-20 13:09:05190 // We do not call Clear() on |results_| to retain |verbatim_relevance|
191 // value in the |results_| object. |verbatim_relevance| is used at the
192 // beginning of the next StartZeroSuggest() call to determine the current url
193 // match relevance.
194 results_.suggest_results.clear();
195 results_.navigation_results.clear();
[email protected]ef6866f2014-02-18 08:26:34196 current_query_.clear();
197 matches_.clear();
198}
199
[email protected]d4a94b92014-03-04 01:35:22200int ZeroSuggestProvider::GetDefaultResultRelevance() const {
201 return kDefaultZeroSuggestRelevance;
202}
203
[email protected]bb1fb2b2013-05-31 00:21:01204void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]02346202014-02-05 05:18:30205 const SuggestResults& results,
[email protected]02346202014-02-05 05:18:30206 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22207 for (size_t i = 0; i < results.size(); ++i)
208 AddMatchToMap(results[i], std::string(), i, map);
[email protected]bb1fb2b2013-05-31 00:21:01209}
210
[email protected]bb1fb2b2013-05-31 00:21:01211AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]02346202014-02-05 05:18:30212 const NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01213 AutocompleteMatch match(this, navigation.relevance(), false,
214 AutocompleteMatchType::NAVSUGGEST);
215 match.destination_url = navigation.url();
216
[email protected]23db6492014-01-16 02:35:30217 // Zero suggest results should always omit protocols and never appear bold.
[email protected]bb1fb2b2013-05-31 00:21:01218 const std::string languages(
219 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
220 match.contents = net::FormatUrl(navigation.url(), languages,
221 net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, NULL, NULL, NULL);
222 match.fill_into_edit +=
223 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
224 match.contents);
[email protected]bb1fb2b2013-05-31 00:21:01225
[email protected]b959d7d42013-12-13 17:26:37226 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01227 match.contents.length(), ACMatchClassification::URL,
228 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16229
230 match.description =
231 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37232 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16233 match.description.length(), ACMatchClassification::NONE,
234 &match.description_class);
[email protected]bb1fb2b2013-05-31 00:21:01235 return match;
236}
237
[email protected]9b9fa672013-11-07 06:04:52238void ZeroSuggestProvider::Run(const GURL& suggest_url) {
[email protected]bb1fb2b2013-05-31 00:21:01239 have_pending_request_ = false;
240 const int kFetcherID = 1;
[email protected]bb1fb2b2013-05-31 00:21:01241 fetcher_.reset(
242 net::URLFetcher::Create(kFetcherID,
243 suggest_url,
244 net::URLFetcher::GET, this));
245 fetcher_->SetRequestContext(profile_->GetRequestContext());
246 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
247 // Add Chrome experiment state to the request headers.
248 net::HttpRequestHeaders headers;
249 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
250 fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
251 fetcher_->SetExtraRequestHeaders(headers.ToString());
252
253 fetcher_->Start();
[email protected]8f064e52013-09-18 01:17:14254
255 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
256 most_visited_urls_.clear();
257 history::TopSites* ts = profile_->GetTopSites();
258 if (ts) {
259 ts->GetMostVisitedURLs(
260 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
[email protected]ce767ab22013-11-12 03:50:09261 weak_ptr_factory_.GetWeakPtr()), false);
[email protected]8f064e52013-09-18 01:17:14262 }
263 }
[email protected]bb1fb2b2013-05-31 00:21:01264 have_pending_request_ = true;
265 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
266}
267
[email protected]8f064e52013-09-18 01:17:14268void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
269 const history::MostVisitedURLList& urls) {
270 most_visited_urls_ = urls;
271}
272
[email protected]9c97f89c2013-06-25 03:12:16273void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01274 matches_.clear();
275
276 const TemplateURL* default_provider =
[email protected]6ce7f612012-09-05 23:53:07277 template_url_service_->GetDefaultSearchProvider();
278 // Fail if we can't set the clickthrough URL for query suggestions.
[email protected]bb1fb2b2013-05-31 00:21:01279 if (default_provider == NULL || !default_provider->SupportsReplacement())
[email protected]6ce7f612012-09-05 23:53:07280 return;
[email protected]6ce7f612012-09-05 23:53:07281
[email protected]00404742014-02-20 13:09:05282 MatchMap map;
283 AddSuggestResultsToMap(results_.suggest_results, &map);
284
285 const int num_query_results = map.size();
286 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01287 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16288 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
289 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
290 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01291
[email protected]8f064e52013-09-18 01:17:14292 // Show Most Visited results after ZeroSuggest response is received.
293 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13294 if (!current_url_match_.destination_url.is_valid())
295 return;
[email protected]8f064e52013-09-18 01:17:14296 matches_.push_back(current_url_match_);
297 int relevance = 600;
298 if (num_results > 0) {
299 UMA_HISTOGRAM_COUNTS(
300 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
301 most_visited_urls_.size());
302 }
[email protected]23db6492014-01-16 02:35:30303 const base::string16 current_query_string16(
304 base::ASCIIToUTF16(current_query_));
305 const std::string languages(
306 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]8f064e52013-09-18 01:17:14307 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
308 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]02346202014-02-05 05:18:30309 NavigationResult nav(*this, url.url, url.title, false, relevance, true,
[email protected]23db6492014-01-16 02:35:30310 current_query_string16, languages);
[email protected]8f064e52013-09-18 01:17:14311 matches_.push_back(NavigationToMatch(nav));
312 --relevance;
313 }
314 return;
315 }
316
[email protected]9c97f89c2013-06-25 03:12:16317 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01318 return;
319
320 // TODO(jered): Rip this out once the first match is decoupled from the
321 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01322 matches_.push_back(current_url_match_);
323
[email protected]00404742014-02-20 13:09:05324 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01325 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01326
[email protected]00404742014-02-20 13:09:05327 const NavigationResults& nav_results(results_.navigation_results);
328 for (NavigationResults::const_iterator it(nav_results.begin());
329 it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01330 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07331}
332
[email protected]bb1fb2b2013-05-31 00:21:01333AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]96920152013-12-04 21:00:16334 AutocompleteInput input(permanent_text_, base::string16::npos, base::string16(),
[email protected]25d7e742013-07-30 17:31:06335 GURL(current_query_), current_page_classification_,
[email protected]bb1fb2b2013-05-31 00:21:01336 false, false, true, AutocompleteInput::ALL_MATCHES);
[email protected]6ce7f612012-09-05 23:53:07337
[email protected]2d915782013-08-29 09:50:21338 AutocompleteMatch match;
339 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
[email protected]51abb7b2014-02-09 23:00:08340 permanent_text_, false, true, current_page_classification_, &match, NULL);
[email protected]bb1fb2b2013-05-31 00:21:01341 match.is_history_what_you_typed_match = false;
[email protected]45f89a92013-08-12 13:41:36342 match.allowed_to_be_default_match = true;
[email protected]6ce7f612012-09-05 23:53:07343
[email protected]bb1fb2b2013-05-31 00:21:01344 // The placeholder suggestion for the current URL has high relevance so
345 // that it is in the first suggestion slot and inline autocompleted. It
346 // gets dropped as soon as the user types something.
[email protected]00404742014-02-20 13:09:05347 match.relevance = GetVerbatimRelevance();
[email protected]6ce7f612012-09-05 23:53:07348
[email protected]bb1fb2b2013-05-31 00:21:01349 return match;
[email protected]6ce7f612012-09-05 23:53:07350}
[email protected]00404742014-02-20 13:09:05351
352int ZeroSuggestProvider::GetVerbatimRelevance() const {
353 return results_.verbatim_relevance >= 0 ?
354 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
355}