blob: e836499d12f0965c3ab713091a6a0ea7c31a5fe1 [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_provider_listener.h"
[email protected]a817ed392014-06-27 05:03:0019#include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
[email protected]bb1fb2b2013-05-31 00:21:0120#include "chrome/browser/autocomplete/history_url_provider.h"
21#include "chrome/browser/autocomplete/search_provider.h"
[email protected]8f064e52013-09-18 01:17:1422#include "chrome/browser/history/history_types.h"
23#include "chrome/browser/history/top_sites.h"
[email protected]bb1fb2b2013-05-31 00:21:0124#include "chrome/browser/omnibox/omnibox_field_trial.h"
[email protected]6ce7f612012-09-05 23:53:0725#include "chrome/browser/profiles/profile.h"
[email protected]6ce7f612012-09-05 23:53:0726#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]a00008d42012-09-15 05:07:5827#include "chrome/common/pref_names.h"
[email protected]6ce7f612012-09-05 23:53:0728#include "chrome/common/url_constants.h"
[email protected]4209e4242014-06-28 05:38:2829#include "components/autocomplete/autocomplete_input.h"
[email protected]e7e07712014-07-30 08:53:2330#include "components/autocomplete/autocomplete_match.h"
[email protected]3dc75b12014-06-08 00:02:2231#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]f0c8c4992014-05-15 17:37:2632#include "components/pref_registry/pref_registry_syncable.h"
[email protected]bf5c532d2014-07-05 00:29:5333#include "components/search_engines/template_url_service.h"
[email protected]71011c1682014-07-09 17:19:1634#include "components/variations/variations_http_header_provider.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/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]855ebff2014-05-09 07:14:3883// static
84void ZeroSuggestProvider::RegisterProfilePrefs(
85 user_prefs::PrefRegistrySyncable* registry) {
86 registry->RegisterStringPref(
87 prefs::kZeroSuggestCachedResults,
88 std::string(),
89 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
90}
91
[email protected]6ce7f612012-09-05 23:53:0792void ZeroSuggestProvider::Start(const AutocompleteInput& input,
[email protected]f030c4d2014-03-25 01:05:5493 bool minimal_changes) {
94 matches_.clear();
[email protected]3dc75b12014-06-08 00:02:2295 if (input.type() == metrics::OmniboxInputType::INVALID)
[email protected]f030c4d2014-03-25 01:05:5496 return;
[email protected]bb1fb2b2013-05-31 00:21:0197
[email protected]bb1fb2b2013-05-31 00:21:0198 Stop(true);
99 field_trial_triggered_ = false;
100 field_trial_triggered_in_session_ = false;
[email protected]855ebff2014-05-09 07:14:38101 results_from_cache_ = false;
[email protected]f030c4d2014-03-25 01:05:54102 permanent_text_ = input.text();
103 current_query_ = input.current_url().spec();
104 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52105 current_url_match_ = MatchForCurrentURL();
106
107 const TemplateURL* default_provider =
108 template_url_service_->GetDefaultSearchProvider();
109 if (default_provider == NULL)
110 return;
[email protected]162c8d9fa2014-03-18 20:25:41111
[email protected]96920152013-12-04 21:00:16112 base::string16 prefix;
[email protected]9b9fa672013-11-07 06:04:52113 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
[email protected]162c8d9fa2014-03-18 20:25:41114 GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19115 search_term_args, template_url_service_->search_terms_data()));
[email protected]162c8d9fa2014-03-18 20:25:41116 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07117 return;
[email protected]162c8d9fa2014-03-18 20:25:41118
119 // No need to send the current page URL in personalized suggest field trial.
[email protected]f030c4d2014-03-25 01:05:54120 if (CanSendURL(input.current_url(), suggest_url, default_provider,
[email protected]162c8d9fa2014-03-18 20:25:41121 current_page_classification_, profile_) &&
122 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) {
123 // Update suggest_url to include the current_page_url.
124 search_term_args.current_page_url = current_query_;
125 suggest_url = GURL(default_provider->suggestions_url_ref().
[email protected]ce7ee5f2014-06-16 23:41:19126 ReplaceSearchTerms(
127 search_term_args,
128 template_url_service_->search_terms_data()));
[email protected]162c8d9fa2014-03-18 20:25:41129 } else if (!CanShowZeroSuggestWithoutSendingURL(suggest_url,
[email protected]f030c4d2014-03-25 01:05:54130 input.current_url())) {
[email protected]162c8d9fa2014-03-18 20:25:41131 return;
132 }
133
[email protected]6ce7f612012-09-05 23:53:07134 done_ = false;
[email protected]6ce7f612012-09-05 23:53:07135 // TODO(jered): Consider adding locally-sourced zero-suggestions here too.
136 // These may be useful on the NTP or more relevant to the user than server
137 // suggestions, if based on local browsing history.
[email protected]855ebff2014-05-09 07:14:38138 MaybeUseCachedSuggestions();
[email protected]9b9fa672013-11-07 06:04:52139 Run(suggest_url);
[email protected]6ce7f612012-09-05 23:53:07140}
141
[email protected]855ebff2014-05-09 07:14:38142void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
143 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) {
144 // Remove the deleted match from the cache, so it is not shown to the user
145 // again. Since we cannot remove just one result, blow away the cache.
146 profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults,
147 std::string());
148 }
149 BaseSearchProvider::DeleteMatch(match);
150}
151
[email protected]f030c4d2014-03-25 01:05:54152void ZeroSuggestProvider::ResetSession() {
153 // The user has started editing in the omnibox, so leave
154 // |field_trial_triggered_in_session_| unchanged and set
155 // |field_trial_triggered_| to false since zero suggest is inactive now.
156 field_trial_triggered_ = false;
157}
158
[email protected]ff88364292014-05-09 09:25:16159void ZeroSuggestProvider::ModifyProviderInfo(
160 metrics::OmniboxEventProto_ProviderInfo* provider_info) const {
161 if (!results_.suggest_results.empty() || !results_.navigation_results.empty())
162 provider_info->set_times_returned_results_in_session(1);
163}
164
[email protected]bb1fb2b2013-05-31 00:21:01165ZeroSuggestProvider::ZeroSuggestProvider(
166 AutocompleteProviderListener* listener,
167 Profile* profile)
[email protected]02346202014-02-05 05:18:30168 : BaseSearchProvider(listener, profile,
169 AutocompleteProvider::TYPE_ZERO_SUGGEST),
[email protected]bb1fb2b2013-05-31 00:21:01170 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)),
[email protected]855ebff2014-05-09 07:14:38171 results_from_cache_(false),
[email protected]8f064e52013-09-18 01:17:14172 weak_ptr_factory_(this) {
[email protected]6ce7f612012-09-05 23:53:07173}
174
175ZeroSuggestProvider::~ZeroSuggestProvider() {
176}
177
[email protected]855ebff2014-05-09 07:14:38178bool ZeroSuggestProvider::StoreSuggestionResponse(
179 const std::string& json_data,
180 const base::Value& parsed_data) {
181 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() ||
182 json_data.empty())
183 return false;
184 profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults, json_data);
185
186 // If we received an empty result list, we should update the display, as it
187 // may be showing cached results that should not be shown.
188 const base::ListValue* root_list = NULL;
189 const base::ListValue* results_list = NULL;
190 if (parsed_data.GetAsList(&root_list) &&
191 root_list->GetList(1, &results_list) &&
192 results_list->empty())
193 return false;
194
195 // We are finished with the request and want to bail early.
196 if (results_from_cache_)
197 done_ = true;
198
199 return results_from_cache_;
200}
201
[email protected]cfa164bf2014-03-19 11:51:15202const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
[email protected]9487b392014-02-14 02:48:18203 // Zero suggest provider should not receive keyword results.
[email protected]cfa164bf2014-03-19 11:51:15204 DCHECK(!is_keyword);
[email protected]9487b392014-02-14 02:48:18205 return template_url_service_->GetDefaultSearchProvider();
206}
207
[email protected]d4a94b92014-03-04 01:35:22208const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
209 return AutocompleteInput(
210 base::string16(), base::string16::npos, base::string16(),
211 GURL(current_query_), current_page_classification_, true, false, false,
[email protected]a817ed392014-06-27 05:03:00212 true, ChromeAutocompleteSchemeClassifier(profile_));
[email protected]9487b392014-02-14 02:48:18213}
214
[email protected]0b9575f2014-07-30 11:58:37215SearchSuggestionParser::Results* ZeroSuggestProvider::GetResultsToFill(
[email protected]cfa164bf2014-03-19 11:51:15216 bool is_keyword) {
217 DCHECK(!is_keyword);
218 return &results_;
219}
220
[email protected]9487b392014-02-14 02:48:18221bool ZeroSuggestProvider::ShouldAppendExtraParams(
[email protected]0b9575f2014-07-30 11:58:37222 const SearchSuggestionParser::SuggestResult& result) const {
[email protected]9487b392014-02-14 02:48:18223 // We always use the default provider for search, so append the params.
224 return true;
225}
226
[email protected]ef6866f2014-02-18 08:26:34227void ZeroSuggestProvider::StopSuggest() {
[email protected]cfa164bf2014-03-19 11:51:15228 if (suggest_results_pending_ > 0)
[email protected]ef6866f2014-02-18 08:26:34229 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
[email protected]cfa164bf2014-03-19 11:51:15230 suggest_results_pending_ = 0;
[email protected]ef6866f2014-02-18 08:26:34231 fetcher_.reset();
232}
233
234void ZeroSuggestProvider::ClearAllResults() {
[email protected]00404742014-02-20 13:09:05235 // We do not call Clear() on |results_| to retain |verbatim_relevance|
236 // value in the |results_| object. |verbatim_relevance| is used at the
237 // beginning of the next StartZeroSuggest() call to determine the current url
238 // match relevance.
239 results_.suggest_results.clear();
240 results_.navigation_results.clear();
[email protected]ef6866f2014-02-18 08:26:34241 current_query_.clear();
[email protected]ef6866f2014-02-18 08:26:34242}
243
[email protected]d4a94b92014-03-04 01:35:22244int ZeroSuggestProvider::GetDefaultResultRelevance() const {
245 return kDefaultZeroSuggestRelevance;
246}
247
[email protected]0cfddf1b2014-03-12 01:46:00248void ZeroSuggestProvider::RecordDeletionResult(bool success) {
249 if (success) {
250 content::RecordAction(
251 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
252 } else {
253 content::RecordAction(
254 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
255 }
256}
257
[email protected]cfa164bf2014-03-19 11:51:15258void ZeroSuggestProvider::LogFetchComplete(bool success, bool is_keyword) {
259 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
260}
261
262bool ZeroSuggestProvider::IsKeywordFetcher(
263 const net::URLFetcher* fetcher) const {
264 // ZeroSuggestProvider does not have a keyword provider.
265 DCHECK_EQ(fetcher, fetcher_.get());
266 return false;
267}
268
269void ZeroSuggestProvider::UpdateMatches() {
270 done_ = true;
271 ConvertResultsToAutocompleteMatches();
272}
273
[email protected]bb1fb2b2013-05-31 00:21:01274void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37275 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30276 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22277 for (size_t i = 0; i < results.size(); ++i)
[email protected]57482a72014-03-14 22:27:37278 AddMatchToMap(results[i], std::string(), i, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01279}
280
[email protected]bb1fb2b2013-05-31 00:21:01281AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37282 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01283 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47284 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01285 match.destination_url = navigation.url();
286
[email protected]23db6492014-01-16 02:35:30287 // Zero suggest results should always omit protocols and never appear bold.
[email protected]bb1fb2b2013-05-31 00:21:01288 const std::string languages(
289 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
290 match.contents = net::FormatUrl(navigation.url(), languages,
291 net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, NULL, NULL, NULL);
292 match.fill_into_edit +=
293 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
[email protected]a817ed392014-06-27 05:03:00294 match.contents, ChromeAutocompleteSchemeClassifier(profile_));
[email protected]bb1fb2b2013-05-31 00:21:01295
[email protected]b959d7d42013-12-13 17:26:37296 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01297 match.contents.length(), ACMatchClassification::URL,
298 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16299
300 match.description =
301 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37302 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16303 match.description.length(), ACMatchClassification::NONE,
304 &match.description_class);
[email protected]bb1fb2b2013-05-31 00:21:01305 return match;
306}
307
[email protected]9b9fa672013-11-07 06:04:52308void ZeroSuggestProvider::Run(const GURL& suggest_url) {
[email protected]cfa164bf2014-03-19 11:51:15309 suggest_results_pending_ = 0;
[email protected]bb1fb2b2013-05-31 00:21:01310 const int kFetcherID = 1;
[email protected]bb1fb2b2013-05-31 00:21:01311 fetcher_.reset(
312 net::URLFetcher::Create(kFetcherID,
313 suggest_url,
314 net::URLFetcher::GET, this));
315 fetcher_->SetRequestContext(profile_->GetRequestContext());
316 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
317 // Add Chrome experiment state to the request headers.
318 net::HttpRequestHeaders headers;
[email protected]71011c1682014-07-09 17:19:16319 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
[email protected]bb1fb2b2013-05-31 00:21:01320 fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
321 fetcher_->SetExtraRequestHeaders(headers.ToString());
[email protected]bb1fb2b2013-05-31 00:21:01322 fetcher_->Start();
[email protected]8f064e52013-09-18 01:17:14323
324 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
325 most_visited_urls_.clear();
326 history::TopSites* ts = profile_->GetTopSites();
327 if (ts) {
328 ts->GetMostVisitedURLs(
329 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
[email protected]ce767ab22013-11-12 03:50:09330 weak_ptr_factory_.GetWeakPtr()), false);
[email protected]8f064e52013-09-18 01:17:14331 }
332 }
[email protected]cfa164bf2014-03-19 11:51:15333 suggest_results_pending_ = 1;
[email protected]bb1fb2b2013-05-31 00:21:01334 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
335}
336
[email protected]8f064e52013-09-18 01:17:14337void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
338 const history::MostVisitedURLList& urls) {
339 most_visited_urls_ = urls;
340}
341
[email protected]9c97f89c2013-06-25 03:12:16342void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01343 matches_.clear();
344
345 const TemplateURL* default_provider =
[email protected]6ce7f612012-09-05 23:53:07346 template_url_service_->GetDefaultSearchProvider();
347 // Fail if we can't set the clickthrough URL for query suggestions.
[email protected]ce7ee5f2014-06-16 23:41:19348 if (default_provider == NULL || !default_provider->SupportsReplacement(
349 template_url_service_->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07350 return;
[email protected]6ce7f612012-09-05 23:53:07351
[email protected]00404742014-02-20 13:09:05352 MatchMap map;
353 AddSuggestResultsToMap(results_.suggest_results, &map);
354
355 const int num_query_results = map.size();
356 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01357 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16358 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
[email protected]78981d8c2014-05-09 15:05:47359 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
[email protected]9c97f89c2013-06-25 03:12:16360 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01361
[email protected]8f064e52013-09-18 01:17:14362 // Show Most Visited results after ZeroSuggest response is received.
363 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13364 if (!current_url_match_.destination_url.is_valid())
365 return;
[email protected]8f064e52013-09-18 01:17:14366 matches_.push_back(current_url_match_);
367 int relevance = 600;
368 if (num_results > 0) {
369 UMA_HISTOGRAM_COUNTS(
370 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
371 most_visited_urls_.size());
372 }
[email protected]23db6492014-01-16 02:35:30373 const base::string16 current_query_string16(
374 base::ASCIIToUTF16(current_query_));
375 const std::string languages(
376 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]8f064e52013-09-18 01:17:14377 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
378 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37379 SearchSuggestionParser::NavigationResult nav(
[email protected]7720fc32014-07-09 06:10:05380 ChromeAutocompleteSchemeClassifier(profile_), url.url,
381 AutocompleteMatchType::NAVSUGGEST, url.title, std::string(), false,
382 relevance, true, current_query_string16, languages);
[email protected]8f064e52013-09-18 01:17:14383 matches_.push_back(NavigationToMatch(nav));
384 --relevance;
385 }
386 return;
387 }
388
[email protected]9c97f89c2013-06-25 03:12:16389 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01390 return;
391
392 // TODO(jered): Rip this out once the first match is decoupled from the
393 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01394 matches_.push_back(current_url_match_);
395
[email protected]00404742014-02-20 13:09:05396 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01397 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01398
[email protected]0b9575f2014-07-30 11:58:37399 const SearchSuggestionParser::NavigationResults& nav_results(
400 results_.navigation_results);
401 for (SearchSuggestionParser::NavigationResults::const_iterator it(
402 nav_results.begin()); it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01403 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07404}
405
[email protected]bb1fb2b2013-05-31 00:21:01406AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]2d915782013-08-29 09:50:21407 AutocompleteMatch match;
408 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
[email protected]51abb7b2014-02-09 23:00:08409 permanent_text_, false, true, current_page_classification_, &match, NULL);
[email protected]bb1fb2b2013-05-31 00:21:01410 match.is_history_what_you_typed_match = false;
[email protected]45f89a92013-08-12 13:41:36411 match.allowed_to_be_default_match = true;
[email protected]6ce7f612012-09-05 23:53:07412
[email protected]bb1fb2b2013-05-31 00:21:01413 // The placeholder suggestion for the current URL has high relevance so
414 // that it is in the first suggestion slot and inline autocompleted. It
415 // gets dropped as soon as the user types something.
[email protected]00404742014-02-20 13:09:05416 match.relevance = GetVerbatimRelevance();
[email protected]6ce7f612012-09-05 23:53:07417
[email protected]bb1fb2b2013-05-31 00:21:01418 return match;
[email protected]6ce7f612012-09-05 23:53:07419}
[email protected]00404742014-02-20 13:09:05420
421int ZeroSuggestProvider::GetVerbatimRelevance() const {
422 return results_.verbatim_relevance >= 0 ?
423 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
424}
[email protected]162c8d9fa2014-03-18 20:25:41425
426bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL(
427 const GURL& suggest_url,
428 const GURL& current_page_url) const {
429 if (!ZeroSuggestEnabled(suggest_url,
430 template_url_service_->GetDefaultSearchProvider(),
431 current_page_classification_, profile_))
432 return false;
433
434 // If we cannot send URLs, then only the MostVisited and Personalized
435 // variations can be shown.
436 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() &&
437 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
438 return false;
439
440 // Only show zero suggest for HTTP[S] pages.
441 // TODO(mariakhomenko): We may be able to expand this set to include pages
442 // with other schemes (e.g. chrome://). That may require improvements to
443 // the formatting of the verbatim result returned by MatchForCurrentURL().
444 if (!current_page_url.is_valid() ||
[email protected]e8ca69c2014-05-07 15:31:19445 ((current_page_url.scheme() != url::kHttpScheme) &&
446 (current_page_url.scheme() != url::kHttpsScheme)))
[email protected]162c8d9fa2014-03-18 20:25:41447 return false;
448
449 return true;
450}
[email protected]855ebff2014-05-09 07:14:38451
452void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
453 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
454 return;
455
456 std::string json_data = profile_->GetPrefs()->GetString(
457 prefs::kZeroSuggestCachedResults);
458 if (!json_data.empty()) {
[email protected]2c802d12014-07-31 12:57:14459 scoped_ptr<base::Value> data(
460 SearchSuggestionParser::DeserializeJsonData(json_data));
[email protected]855ebff2014-05-09 07:14:38461 if (data && ParseSuggestResults(*data.get(), false, &results_)) {
462 ConvertResultsToAutocompleteMatches();
463 results_from_cache_ = !matches_.empty();
464 }
465 }
466}