blob: 723622b989d9d4db0e6a01e49ff958d1e7824cfd [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]f0c8c4992014-05-15 17:37:2635#include "components/pref_registry/pref_registry_syncable.h"
[email protected]0cfddf1b2014-03-12 01:46:0036#include "content/public/browser/user_metrics.h"
[email protected]bb1fb2b2013-05-31 00:21:0137#include "net/base/escape.h"
[email protected]6ce7f612012-09-05 23:53:0738#include "net/base/load_flags.h"
[email protected]bb1fb2b2013-05-31 00:21:0139#include "net/base/net_util.h"
40#include "net/http/http_request_headers.h"
[email protected]6ce7f612012-09-05 23:53:0741#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]855ebff2014-05-09 07:14:3884// static
85void 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]6ce7f612012-09-05 23:53:0793void ZeroSuggestProvider::Start(const AutocompleteInput& input,
[email protected]f030c4d2014-03-25 01:05:5494 bool minimal_changes) {
95 matches_.clear();
96 if (input.type() == AutocompleteInput::INVALID)
97 return;
[email protected]bb1fb2b2013-05-31 00:21:0198
[email protected]bb1fb2b2013-05-31 00:21:0199 Stop(true);
100 field_trial_triggered_ = false;
101 field_trial_triggered_in_session_ = false;
[email protected]855ebff2014-05-09 07:14:38102 results_from_cache_ = false;
[email protected]f030c4d2014-03-25 01:05:54103 permanent_text_ = input.text();
104 current_query_ = input.current_url().spec();
105 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52106 current_url_match_ = MatchForCurrentURL();
107
108 const TemplateURL* default_provider =
109 template_url_service_->GetDefaultSearchProvider();
110 if (default_provider == NULL)
111 return;
[email protected]162c8d9fa2014-03-18 20:25:41112
[email protected]96920152013-12-04 21:00:16113 base::string16 prefix;
[email protected]9b9fa672013-11-07 06:04:52114 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
[email protected]162c8d9fa2014-03-18 20:25:41115 GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms(
116 search_term_args));
117 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07118 return;
[email protected]162c8d9fa2014-03-18 20:25:41119
120 // No need to send the current page URL in personalized suggest field trial.
[email protected]f030c4d2014-03-25 01:05:54121 if (CanSendURL(input.current_url(), suggest_url, default_provider,
[email protected]162c8d9fa2014-03-18 20:25:41122 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]f030c4d2014-03-25 01:05:54129 input.current_url())) {
[email protected]162c8d9fa2014-03-18 20:25:41130 return;
131 }
132
[email protected]6ce7f612012-09-05 23:53:07133 done_ = false;
[email protected]6ce7f612012-09-05 23:53:07134 // 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]855ebff2014-05-09 07:14:38137 MaybeUseCachedSuggestions();
[email protected]9b9fa672013-11-07 06:04:52138 Run(suggest_url);
[email protected]6ce7f612012-09-05 23:53:07139}
140
[email protected]855ebff2014-05-09 07:14:38141void 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]f030c4d2014-03-25 01:05:54151void 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]ff88364292014-05-09 09:25:16158void ZeroSuggestProvider::ModifyProviderInfo(
159 metrics::OmniboxEventProto_ProviderInfo* provider_info) const {
160 if (!results_.suggest_results.empty() || !results_.navigation_results.empty())
161 provider_info->set_times_returned_results_in_session(1);
162}
163
[email protected]bb1fb2b2013-05-31 00:21:01164ZeroSuggestProvider::ZeroSuggestProvider(
165 AutocompleteProviderListener* listener,
166 Profile* profile)
[email protected]02346202014-02-05 05:18:30167 : BaseSearchProvider(listener, profile,
168 AutocompleteProvider::TYPE_ZERO_SUGGEST),
[email protected]bb1fb2b2013-05-31 00:21:01169 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]855ebff2014-05-09 07:14:38170 results_from_cache_(false),
[email protected]8f064e52013-09-18 01:17:14171 weak_ptr_factory_(this) {
[email protected]6ce7f612012-09-05 23:53:07172}
173
174ZeroSuggestProvider::~ZeroSuggestProvider() {
175}
176
[email protected]855ebff2014-05-09 07:14:38177bool ZeroSuggestProvider::StoreSuggestionResponse(
178 const std::string& json_data,
179 const base::Value& parsed_data) {
180 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() ||
181 json_data.empty())
182 return false;
183 profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults, json_data);
184
185 // If we received an empty result list, we should update the display, as it
186 // may be showing cached results that should not be shown.
187 const base::ListValue* root_list = NULL;
188 const base::ListValue* results_list = NULL;
189 if (parsed_data.GetAsList(&root_list) &&
190 root_list->GetList(1, &results_list) &&
191 results_list->empty())
192 return false;
193
194 // We are finished with the request and want to bail early.
195 if (results_from_cache_)
196 done_ = true;
197
198 return results_from_cache_;
199}
200
[email protected]cfa164bf2014-03-19 11:51:15201const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
[email protected]9487b392014-02-14 02:48:18202 // Zero suggest provider should not receive keyword results.
[email protected]cfa164bf2014-03-19 11:51:15203 DCHECK(!is_keyword);
[email protected]9487b392014-02-14 02:48:18204 return template_url_service_->GetDefaultSearchProvider();
205}
206
[email protected]d4a94b92014-03-04 01:35:22207const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
208 return AutocompleteInput(
209 base::string16(), base::string16::npos, base::string16(),
210 GURL(current_query_), current_page_classification_, true, false, false,
[email protected]a2770a7d2014-04-22 19:33:35211 true);
[email protected]9487b392014-02-14 02:48:18212}
213
[email protected]cfa164bf2014-03-19 11:51:15214BaseSearchProvider::Results* ZeroSuggestProvider::GetResultsToFill(
215 bool is_keyword) {
216 DCHECK(!is_keyword);
217 return &results_;
218}
219
[email protected]9487b392014-02-14 02:48:18220bool ZeroSuggestProvider::ShouldAppendExtraParams(
221 const SuggestResult& result) const {
222 // We always use the default provider for search, so append the params.
223 return true;
224}
225
[email protected]ef6866f2014-02-18 08:26:34226void ZeroSuggestProvider::StopSuggest() {
[email protected]cfa164bf2014-03-19 11:51:15227 if (suggest_results_pending_ > 0)
[email protected]ef6866f2014-02-18 08:26:34228 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
[email protected]cfa164bf2014-03-19 11:51:15229 suggest_results_pending_ = 0;
[email protected]ef6866f2014-02-18 08:26:34230 fetcher_.reset();
231}
232
233void ZeroSuggestProvider::ClearAllResults() {
[email protected]00404742014-02-20 13:09:05234 // We do not call Clear() on |results_| to retain |verbatim_relevance|
235 // value in the |results_| object. |verbatim_relevance| is used at the
236 // beginning of the next StartZeroSuggest() call to determine the current url
237 // match relevance.
238 results_.suggest_results.clear();
239 results_.navigation_results.clear();
[email protected]ef6866f2014-02-18 08:26:34240 current_query_.clear();
[email protected]ef6866f2014-02-18 08:26:34241}
242
[email protected]d4a94b92014-03-04 01:35:22243int ZeroSuggestProvider::GetDefaultResultRelevance() const {
244 return kDefaultZeroSuggestRelevance;
245}
246
[email protected]0cfddf1b2014-03-12 01:46:00247void ZeroSuggestProvider::RecordDeletionResult(bool success) {
248 if (success) {
249 content::RecordAction(
250 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
251 } else {
252 content::RecordAction(
253 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
254 }
255}
256
[email protected]cfa164bf2014-03-19 11:51:15257void ZeroSuggestProvider::LogFetchComplete(bool success, bool is_keyword) {
258 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
259}
260
261bool ZeroSuggestProvider::IsKeywordFetcher(
262 const net::URLFetcher* fetcher) const {
263 // ZeroSuggestProvider does not have a keyword provider.
264 DCHECK_EQ(fetcher, fetcher_.get());
265 return false;
266}
267
268void ZeroSuggestProvider::UpdateMatches() {
269 done_ = true;
270 ConvertResultsToAutocompleteMatches();
271}
272
[email protected]bb1fb2b2013-05-31 00:21:01273void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]02346202014-02-05 05:18:30274 const SuggestResults& results,
[email protected]02346202014-02-05 05:18:30275 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22276 for (size_t i = 0; i < results.size(); ++i)
[email protected]57482a72014-03-14 22:27:37277 AddMatchToMap(results[i], std::string(), i, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01278}
279
[email protected]bb1fb2b2013-05-31 00:21:01280AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]02346202014-02-05 05:18:30281 const NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01282 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47283 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01284 match.destination_url = navigation.url();
285
[email protected]23db6492014-01-16 02:35:30286 // Zero suggest results should always omit protocols and never appear bold.
[email protected]bb1fb2b2013-05-31 00:21:01287 const std::string languages(
288 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
289 match.contents = net::FormatUrl(navigation.url(), languages,
290 net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, NULL, NULL, NULL);
291 match.fill_into_edit +=
292 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
293 match.contents);
[email protected]bb1fb2b2013-05-31 00:21:01294
[email protected]b959d7d42013-12-13 17:26:37295 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01296 match.contents.length(), ACMatchClassification::URL,
297 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16298
299 match.description =
300 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37301 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16302 match.description.length(), ACMatchClassification::NONE,
303 &match.description_class);
[email protected]bb1fb2b2013-05-31 00:21:01304 return match;
305}
306
[email protected]9b9fa672013-11-07 06:04:52307void ZeroSuggestProvider::Run(const GURL& suggest_url) {
[email protected]cfa164bf2014-03-19 11:51:15308 suggest_results_pending_ = 0;
[email protected]bb1fb2b2013-05-31 00:21:01309 const int kFetcherID = 1;
[email protected]bb1fb2b2013-05-31 00:21:01310 fetcher_.reset(
311 net::URLFetcher::Create(kFetcherID,
312 suggest_url,
313 net::URLFetcher::GET, this));
314 fetcher_->SetRequestContext(profile_->GetRequestContext());
315 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
316 // Add Chrome experiment state to the request headers.
317 net::HttpRequestHeaders headers;
318 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
319 fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
320 fetcher_->SetExtraRequestHeaders(headers.ToString());
[email protected]bb1fb2b2013-05-31 00:21:01321 fetcher_->Start();
[email protected]8f064e52013-09-18 01:17:14322
323 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
324 most_visited_urls_.clear();
325 history::TopSites* ts = profile_->GetTopSites();
326 if (ts) {
327 ts->GetMostVisitedURLs(
328 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
[email protected]ce767ab22013-11-12 03:50:09329 weak_ptr_factory_.GetWeakPtr()), false);
[email protected]8f064e52013-09-18 01:17:14330 }
331 }
[email protected]cfa164bf2014-03-19 11:51:15332 suggest_results_pending_ = 1;
[email protected]bb1fb2b2013-05-31 00:21:01333 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
334}
335
[email protected]8f064e52013-09-18 01:17:14336void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
337 const history::MostVisitedURLList& urls) {
338 most_visited_urls_ = urls;
339}
340
[email protected]9c97f89c2013-06-25 03:12:16341void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01342 matches_.clear();
343
344 const TemplateURL* default_provider =
[email protected]6ce7f612012-09-05 23:53:07345 template_url_service_->GetDefaultSearchProvider();
346 // Fail if we can't set the clickthrough URL for query suggestions.
[email protected]bb1fb2b2013-05-31 00:21:01347 if (default_provider == NULL || !default_provider->SupportsReplacement())
[email protected]6ce7f612012-09-05 23:53:07348 return;
[email protected]6ce7f612012-09-05 23:53:07349
[email protected]00404742014-02-20 13:09:05350 MatchMap map;
351 AddSuggestResultsToMap(results_.suggest_results, &map);
352
353 const int num_query_results = map.size();
354 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01355 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16356 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
[email protected]78981d8c2014-05-09 15:05:47357 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
[email protected]9c97f89c2013-06-25 03:12:16358 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01359
[email protected]8f064e52013-09-18 01:17:14360 // Show Most Visited results after ZeroSuggest response is received.
361 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13362 if (!current_url_match_.destination_url.is_valid())
363 return;
[email protected]8f064e52013-09-18 01:17:14364 matches_.push_back(current_url_match_);
365 int relevance = 600;
366 if (num_results > 0) {
367 UMA_HISTOGRAM_COUNTS(
368 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
369 most_visited_urls_.size());
370 }
[email protected]23db6492014-01-16 02:35:30371 const base::string16 current_query_string16(
372 base::ASCIIToUTF16(current_query_));
373 const std::string languages(
374 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]8f064e52013-09-18 01:17:14375 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
376 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]78981d8c2014-05-09 15:05:47377 NavigationResult nav(*this, url.url, AutocompleteMatchType::NAVSUGGEST,
378 url.title, std::string(), false, relevance, true,
[email protected]23db6492014-01-16 02:35:30379 current_query_string16, languages);
[email protected]8f064e52013-09-18 01:17:14380 matches_.push_back(NavigationToMatch(nav));
381 --relevance;
382 }
383 return;
384 }
385
[email protected]9c97f89c2013-06-25 03:12:16386 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01387 return;
388
389 // TODO(jered): Rip this out once the first match is decoupled from the
390 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01391 matches_.push_back(current_url_match_);
392
[email protected]00404742014-02-20 13:09:05393 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01394 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01395
[email protected]00404742014-02-20 13:09:05396 const NavigationResults& nav_results(results_.navigation_results);
397 for (NavigationResults::const_iterator it(nav_results.begin());
398 it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01399 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07400}
401
[email protected]bb1fb2b2013-05-31 00:21:01402AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]2d915782013-08-29 09:50:21403 AutocompleteMatch match;
404 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
[email protected]51abb7b2014-02-09 23:00:08405 permanent_text_, false, true, current_page_classification_, &match, NULL);
[email protected]bb1fb2b2013-05-31 00:21:01406 match.is_history_what_you_typed_match = false;
[email protected]45f89a92013-08-12 13:41:36407 match.allowed_to_be_default_match = true;
[email protected]6ce7f612012-09-05 23:53:07408
[email protected]bb1fb2b2013-05-31 00:21:01409 // The placeholder suggestion for the current URL has high relevance so
410 // that it is in the first suggestion slot and inline autocompleted. It
411 // gets dropped as soon as the user types something.
[email protected]00404742014-02-20 13:09:05412 match.relevance = GetVerbatimRelevance();
[email protected]6ce7f612012-09-05 23:53:07413
[email protected]bb1fb2b2013-05-31 00:21:01414 return match;
[email protected]6ce7f612012-09-05 23:53:07415}
[email protected]00404742014-02-20 13:09:05416
417int ZeroSuggestProvider::GetVerbatimRelevance() const {
418 return results_.verbatim_relevance >= 0 ?
419 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
420}
[email protected]162c8d9fa2014-03-18 20:25:41421
422bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL(
423 const GURL& suggest_url,
424 const GURL& current_page_url) const {
425 if (!ZeroSuggestEnabled(suggest_url,
426 template_url_service_->GetDefaultSearchProvider(),
427 current_page_classification_, profile_))
428 return false;
429
430 // If we cannot send URLs, then only the MostVisited and Personalized
431 // variations can be shown.
432 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() &&
433 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
434 return false;
435
436 // Only show zero suggest for HTTP[S] pages.
437 // TODO(mariakhomenko): We may be able to expand this set to include pages
438 // with other schemes (e.g. chrome://). That may require improvements to
439 // the formatting of the verbatim result returned by MatchForCurrentURL().
440 if (!current_page_url.is_valid() ||
[email protected]e8ca69c2014-05-07 15:31:19441 ((current_page_url.scheme() != url::kHttpScheme) &&
442 (current_page_url.scheme() != url::kHttpsScheme)))
[email protected]162c8d9fa2014-03-18 20:25:41443 return false;
444
445 return true;
446}
[email protected]855ebff2014-05-09 07:14:38447
448void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
449 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
450 return;
451
452 std::string json_data = profile_->GetPrefs()->GetString(
453 prefs::kZeroSuggestCachedResults);
454 if (!json_data.empty()) {
455 scoped_ptr<base::Value> data(DeserializeJsonData(json_data));
456 if (data && ParseSuggestResults(*data.get(), false, &results_)) {
457 ConvertResultsToAutocompleteMatches();
458 results_from_cache_ = !matches_.empty();
459 }
460 }
461}