blob: beaa2e9640060f673511cf4a4259b85a190b48a3 [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]855ebff2014-05-09 07:14:3835#include "components/user_prefs/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]bb1fb2b2013-05-31 00:21:01158ZeroSuggestProvider::ZeroSuggestProvider(
159 AutocompleteProviderListener* listener,
160 Profile* profile)
[email protected]02346202014-02-05 05:18:30161 : BaseSearchProvider(listener, profile,
162 AutocompleteProvider::TYPE_ZERO_SUGGEST),
[email protected]bb1fb2b2013-05-31 00:21:01163 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]855ebff2014-05-09 07:14:38164 results_from_cache_(false),
[email protected]8f064e52013-09-18 01:17:14165 weak_ptr_factory_(this) {
[email protected]6ce7f612012-09-05 23:53:07166}
167
168ZeroSuggestProvider::~ZeroSuggestProvider() {
169}
170
[email protected]855ebff2014-05-09 07:14:38171bool 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]cfa164bf2014-03-19 11:51:15195const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
[email protected]9487b392014-02-14 02:48:18196 // Zero suggest provider should not receive keyword results.
[email protected]cfa164bf2014-03-19 11:51:15197 DCHECK(!is_keyword);
[email protected]9487b392014-02-14 02:48:18198 return template_url_service_->GetDefaultSearchProvider();
199}
200
[email protected]d4a94b92014-03-04 01:35:22201const 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]a2770a7d2014-04-22 19:33:35205 true);
[email protected]9487b392014-02-14 02:48:18206}
207
[email protected]cfa164bf2014-03-19 11:51:15208BaseSearchProvider::Results* ZeroSuggestProvider::GetResultsToFill(
209 bool is_keyword) {
210 DCHECK(!is_keyword);
211 return &results_;
212}
213
[email protected]9487b392014-02-14 02:48:18214bool 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]ef6866f2014-02-18 08:26:34220void ZeroSuggestProvider::StopSuggest() {
[email protected]cfa164bf2014-03-19 11:51:15221 if (suggest_results_pending_ > 0)
[email protected]ef6866f2014-02-18 08:26:34222 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
[email protected]cfa164bf2014-03-19 11:51:15223 suggest_results_pending_ = 0;
[email protected]ef6866f2014-02-18 08:26:34224 fetcher_.reset();
225}
226
227void ZeroSuggestProvider::ClearAllResults() {
[email protected]00404742014-02-20 13:09:05228 // 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]ef6866f2014-02-18 08:26:34234 current_query_.clear();
[email protected]ef6866f2014-02-18 08:26:34235}
236
[email protected]d4a94b92014-03-04 01:35:22237int ZeroSuggestProvider::GetDefaultResultRelevance() const {
238 return kDefaultZeroSuggestRelevance;
239}
240
[email protected]0cfddf1b2014-03-12 01:46:00241void 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]cfa164bf2014-03-19 11:51:15251void ZeroSuggestProvider::LogFetchComplete(bool success, bool is_keyword) {
252 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
253}
254
255bool 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
262void ZeroSuggestProvider::UpdateMatches() {
263 done_ = true;
264 ConvertResultsToAutocompleteMatches();
265}
266
[email protected]bb1fb2b2013-05-31 00:21:01267void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]02346202014-02-05 05:18:30268 const SuggestResults& results,
[email protected]02346202014-02-05 05:18:30269 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22270 for (size_t i = 0; i < results.size(); ++i)
[email protected]57482a72014-03-14 22:27:37271 AddMatchToMap(results[i], std::string(), i, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01272}
273
[email protected]bb1fb2b2013-05-31 00:21:01274AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]02346202014-02-05 05:18:30275 const NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01276 AutocompleteMatch match(this, navigation.relevance(), false,
277 AutocompleteMatchType::NAVSUGGEST);
278 match.destination_url = navigation.url();
279
[email protected]23db6492014-01-16 02:35:30280 // Zero suggest results should always omit protocols and never appear bold.
[email protected]bb1fb2b2013-05-31 00:21:01281 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]bb1fb2b2013-05-31 00:21:01288
[email protected]b959d7d42013-12-13 17:26:37289 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01290 match.contents.length(), ACMatchClassification::URL,
291 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16292
293 match.description =
294 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37295 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16296 match.description.length(), ACMatchClassification::NONE,
297 &match.description_class);
[email protected]bb1fb2b2013-05-31 00:21:01298 return match;
299}
300
[email protected]9b9fa672013-11-07 06:04:52301void ZeroSuggestProvider::Run(const GURL& suggest_url) {
[email protected]cfa164bf2014-03-19 11:51:15302 suggest_results_pending_ = 0;
[email protected]bb1fb2b2013-05-31 00:21:01303 const int kFetcherID = 1;
[email protected]bb1fb2b2013-05-31 00:21:01304 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]bb1fb2b2013-05-31 00:21:01315 fetcher_->Start();
[email protected]8f064e52013-09-18 01:17:14316
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]ce767ab22013-11-12 03:50:09323 weak_ptr_factory_.GetWeakPtr()), false);
[email protected]8f064e52013-09-18 01:17:14324 }
325 }
[email protected]cfa164bf2014-03-19 11:51:15326 suggest_results_pending_ = 1;
[email protected]bb1fb2b2013-05-31 00:21:01327 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
328}
329
[email protected]8f064e52013-09-18 01:17:14330void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
331 const history::MostVisitedURLList& urls) {
332 most_visited_urls_ = urls;
333}
334
[email protected]9c97f89c2013-06-25 03:12:16335void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01336 matches_.clear();
337
338 const TemplateURL* default_provider =
[email protected]6ce7f612012-09-05 23:53:07339 template_url_service_->GetDefaultSearchProvider();
340 // Fail if we can't set the clickthrough URL for query suggestions.
[email protected]bb1fb2b2013-05-31 00:21:01341 if (default_provider == NULL || !default_provider->SupportsReplacement())
[email protected]6ce7f612012-09-05 23:53:07342 return;
[email protected]6ce7f612012-09-05 23:53:07343
[email protected]00404742014-02-20 13:09:05344 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]bb1fb2b2013-05-31 00:21:01349 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16350 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]bb1fb2b2013-05-31 00:21:01353
[email protected]8f064e52013-09-18 01:17:14354 // Show Most Visited results after ZeroSuggest response is received.
355 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13356 if (!current_url_match_.destination_url.is_valid())
357 return;
[email protected]8f064e52013-09-18 01:17:14358 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]23db6492014-01-16 02:35:30365 const base::string16 current_query_string16(
366 base::ASCIIToUTF16(current_query_));
367 const std::string languages(
368 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]8f064e52013-09-18 01:17:14369 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
370 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]02346202014-02-05 05:18:30371 NavigationResult nav(*this, url.url, url.title, false, relevance, true,
[email protected]23db6492014-01-16 02:35:30372 current_query_string16, languages);
[email protected]8f064e52013-09-18 01:17:14373 matches_.push_back(NavigationToMatch(nav));
374 --relevance;
375 }
376 return;
377 }
378
[email protected]9c97f89c2013-06-25 03:12:16379 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01380 return;
381
382 // TODO(jered): Rip this out once the first match is decoupled from the
383 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01384 matches_.push_back(current_url_match_);
385
[email protected]00404742014-02-20 13:09:05386 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01387 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01388
[email protected]00404742014-02-20 13:09:05389 const NavigationResults& nav_results(results_.navigation_results);
390 for (NavigationResults::const_iterator it(nav_results.begin());
391 it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01392 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07393}
394
[email protected]bb1fb2b2013-05-31 00:21:01395AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]2d915782013-08-29 09:50:21396 AutocompleteMatch match;
397 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
[email protected]51abb7b2014-02-09 23:00:08398 permanent_text_, false, true, current_page_classification_, &match, NULL);
[email protected]bb1fb2b2013-05-31 00:21:01399 match.is_history_what_you_typed_match = false;
[email protected]45f89a92013-08-12 13:41:36400 match.allowed_to_be_default_match = true;
[email protected]6ce7f612012-09-05 23:53:07401
[email protected]bb1fb2b2013-05-31 00:21:01402 // 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]00404742014-02-20 13:09:05405 match.relevance = GetVerbatimRelevance();
[email protected]6ce7f612012-09-05 23:53:07406
[email protected]bb1fb2b2013-05-31 00:21:01407 return match;
[email protected]6ce7f612012-09-05 23:53:07408}
[email protected]00404742014-02-20 13:09:05409
410int ZeroSuggestProvider::GetVerbatimRelevance() const {
411 return results_.verbatim_relevance >= 0 ?
412 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
413}
[email protected]162c8d9fa2014-03-18 20:25:41414
415bool 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]e8ca69c2014-05-07 15:31:19434 ((current_page_url.scheme() != url::kHttpScheme) &&
435 (current_page_url.scheme() != url::kHttpsScheme)))
[email protected]162c8d9fa2014-03-18 20:25:41436 return false;
437
438 return true;
439}
[email protected]855ebff2014-05-09 07:14:38440
441void 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}