blob: 68c8019673621e5215f9ebdcc292df3e4f5f6544 [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]f7f41c0e2014-08-11 04:22:2311#include "base/metrics/user_metrics.h"
[email protected]3853a4c2013-02-11 17:15:5712#include "base/prefs/pref_service.h"
[email protected]98570e12013-06-10 19:54:2213#include "base/strings/string16.h"
14#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2015#include "base/strings/utf_string_conversions.h"
[email protected]4dcb7972013-06-28 15:15:4116#include "base/time/time.h"
[email protected]2d915782013-08-29 09:50:2117#include "chrome/browser/autocomplete/autocomplete_classifier.h"
sdefresnebc766ef2014-09-25 09:28:1318#include "components/history/core/browser/history_types.h"
sdefresne0da3bc02015-01-29 18:26:3519#include "components/history/core/browser/top_sites.h"
[email protected]3dc75b12014-06-08 00:02:2220#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]b1c5ab682014-08-07 11:53:1721#include "components/omnibox/autocomplete_input.h"
22#include "components/omnibox/autocomplete_match.h"
23#include "components/omnibox/autocomplete_provider_listener.h"
blundell5894d2b2015-06-18 11:39:3624#include "components/omnibox/history_url_provider.h"
[email protected]4c583b62014-08-08 10:37:2325#include "components/omnibox/omnibox_field_trial.h"
blundelld130d592015-06-21 19:29:1326#include "components/omnibox/omnibox_pref_names.h"
hashimoto5f7db4b2014-08-27 02:46:2027#include "components/omnibox/search_provider.h"
[email protected]f0c8c4992014-05-15 17:37:2628#include "components/pref_registry/pref_registry_syncable.h"
[email protected]bf5c532d2014-07-05 00:29:5329#include "components/search_engines/template_url_service.h"
isherman3be67db2014-10-24 05:57:4430#include "components/variations/net/variations_http_header_provider.h"
[email protected]bb1fb2b2013-05-31 00:21:0131#include "net/base/escape.h"
[email protected]6ce7f612012-09-05 23:53:0732#include "net/base/load_flags.h"
[email protected]bb1fb2b2013-05-31 00:21:0133#include "net/base/net_util.h"
34#include "net/http/http_request_headers.h"
[email protected]6ce7f612012-09-05 23:53:0735#include "net/url_request/url_fetcher.h"
36#include "net/url_request/url_request_status.h"
[email protected]761fa4702013-07-02 15:25:1537#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0738
39namespace {
[email protected]bb1fb2b2013-05-31 00:21:0140
41// TODO(hfung): The histogram code was copied and modified from
42// search_provider.cc. Refactor and consolidate the code.
43// We keep track in a histogram how many suggest requests we send, how
44// many suggest requests we invalidate (e.g., due to a user typing
45// another character), and how many replies we receive.
46// *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
47// (excluding the end-of-list enum value)
48// We do not want values of existing enums to change or else it screws
49// up the statistics.
50enum ZeroSuggestRequestsHistogramValue {
51 ZERO_SUGGEST_REQUEST_SENT = 1,
52 ZERO_SUGGEST_REQUEST_INVALIDATED,
53 ZERO_SUGGEST_REPLY_RECEIVED,
54 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
55};
56
57void LogOmniboxZeroSuggestRequest(
58 ZeroSuggestRequestsHistogramValue request_value) {
59 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
60 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
61}
62
63// The maximum relevance of the top match from this provider.
64const int kDefaultVerbatimZeroSuggestRelevance = 1300;
65
66// Relevance value to use if it was not set explicitly by the server.
67const int kDefaultZeroSuggestRelevance = 100;
68
[email protected]6ce7f612012-09-05 23:53:0769} // namespace
70
[email protected]a00008d42012-09-15 05:07:5871// static
72ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:1873 AutocompleteProviderClient* client,
blundelld130d592015-06-21 19:29:1374 AutocompleteProviderListener* listener) {
75 return new ZeroSuggestProvider(client, listener);
[email protected]6ce7f612012-09-05 23:53:0776}
77
[email protected]855ebff2014-05-09 07:14:3878// static
79void ZeroSuggestProvider::RegisterProfilePrefs(
80 user_prefs::PrefRegistrySyncable* registry) {
blundelld130d592015-06-21 19:29:1381 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
82 std::string());
[email protected]855ebff2014-05-09 07:14:3883}
84
[email protected]6ce7f612012-09-05 23:53:0785void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:1886 bool minimal_changes) {
[email protected]f030c4d2014-03-25 01:05:5487 matches_.clear();
jifcf322cd2015-06-17 11:01:1888 if (!input.from_omnibox_focus() ||
mariakhomenko3ef531d72015-01-10 00:03:4389 input.type() == metrics::OmniboxInputType::INVALID)
[email protected]f030c4d2014-03-25 01:05:5490 return;
[email protected]bb1fb2b2013-05-31 00:21:0191
mpearson8a37c382015-03-07 05:58:5792 Stop(true, false);
blundelld130d592015-06-21 19:29:1393 set_field_trial_triggered(false);
94 set_field_trial_triggered_in_session(false);
[email protected]855ebff2014-05-09 07:14:3895 results_from_cache_ = false;
[email protected]f030c4d2014-03-25 01:05:5496 permanent_text_ = input.text();
97 current_query_ = input.current_url().spec();
98 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:5299 current_url_match_ = MatchForCurrentURL();
blundelld130d592015-06-21 19:29:13100 TemplateURLService* template_url_service = client()->GetTemplateURLService();
[email protected]9b9fa672013-11-07 06:04:52101
102 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13103 template_url_service->GetDefaultSearchProvider();
[email protected]9b9fa672013-11-07 06:04:52104 if (default_provider == NULL)
105 return;
[email protected]162c8d9fa2014-03-18 20:25:41106
[email protected]96920152013-12-04 21:00:16107 base::string16 prefix;
[email protected]9b9fa672013-11-07 06:04:52108 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
[email protected]162c8d9fa2014-03-18 20:25:41109 GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms(
blundelld130d592015-06-21 19:29:13110 search_term_args, template_url_service->search_terms_data()));
[email protected]162c8d9fa2014-03-18 20:25:41111 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07112 return;
[email protected]162c8d9fa2014-03-18 20:25:41113
mariakhomenkobfc3a2a2014-10-24 00:48:22114 // No need to send the current page URL in personalized suggest or
115 // most visited field trials.
[email protected]f030c4d2014-03-25 01:05:54116 if (CanSendURL(input.current_url(), suggest_url, default_provider,
[email protected]e6477f12014-08-05 07:59:54117 current_page_classification_,
blundelld130d592015-06-21 19:29:13118 template_url_service->search_terms_data(), client()) &&
mariakhomenkobfc3a2a2014-10-24 00:48:22119 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() &&
120 !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]162c8d9fa2014-03-18 20:25:41121 // Update suggest_url to include the current_page_url.
122 search_term_args.current_page_url = current_query_;
blundelld130d592015-06-21 19:29:13123 suggest_url =
124 GURL(default_provider->suggestions_url_ref().ReplaceSearchTerms(
125 search_term_args, template_url_service->search_terms_data()));
mariakhomenkobfc3a2a2014-10-24 00:48:22126 } else if (!ShouldShowNonContextualZeroSuggest(suggest_url,
127 input.current_url())) {
[email protected]162c8d9fa2014-03-18 20:25:41128 return;
129 }
130
[email protected]6ce7f612012-09-05 23:53:07131 done_ = false;
[email protected]6ce7f612012-09-05 23:53:07132 // TODO(jered): Consider adding locally-sourced zero-suggestions here too.
133 // These may be useful on the NTP or more relevant to the user than server
134 // suggestions, if based on local browsing history.
[email protected]855ebff2014-05-09 07:14:38135 MaybeUseCachedSuggestions();
[email protected]9b9fa672013-11-07 06:04:52136 Run(suggest_url);
[email protected]6ce7f612012-09-05 23:53:07137}
138
mpearson8a37c382015-03-07 05:58:57139void ZeroSuggestProvider::Stop(bool clear_cached_results,
140 bool due_to_user_inactivity) {
[email protected]ec3f679b2014-08-18 07:45:13141 if (fetcher_)
142 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
143 fetcher_.reset();
mariakhomenkobfc3a2a2014-10-24 00:48:22144 waiting_for_most_visited_urls_request_ = false;
[email protected]ec3f679b2014-08-18 07:45:13145 done_ = true;
146
147 if (clear_cached_results) {
148 // We do not call Clear() on |results_| to retain |verbatim_relevance|
149 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18150 // beginning of the next call to Start() to determine the current url
151 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13152 results_.suggest_results.clear();
153 results_.navigation_results.clear();
154 current_query_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45155 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13156 }
157}
158
[email protected]855ebff2014-05-09 07:14:38159void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
160 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) {
161 // Remove the deleted match from the cache, so it is not shown to the user
162 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13163 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38164 std::string());
165 }
166 BaseSearchProvider::DeleteMatch(match);
167}
168
[email protected]ec3f679b2014-08-18 07:45:13169void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
170 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45171 if (!results_.suggest_results.empty() ||
172 !results_.navigation_results.empty() ||
173 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13174 provider_info->back().set_times_returned_results_in_session(1);
175}
176
[email protected]f030c4d2014-03-25 01:05:54177void ZeroSuggestProvider::ResetSession() {
178 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13179 // |field_trial_triggered_in_session| unchanged and set
180 // |field_trial_triggered| to false since zero suggest is inactive now.
181 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54182}
183
blundelld130d592015-06-21 19:29:13184ZeroSuggestProvider::ZeroSuggestProvider(AutocompleteProviderClient* client,
185 AutocompleteProviderListener* listener)
186 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
[email protected]776ee5902014-08-11 09:15:19187 listener_(listener),
[email protected]855ebff2014-05-09 07:14:38188 results_from_cache_(false),
mariakhomenkobfc3a2a2014-10-24 00:48:22189 waiting_for_most_visited_urls_request_(false),
[email protected]8f064e52013-09-18 01:17:14190 weak_ptr_factory_(this) {
[email protected]6ce7f612012-09-05 23:53:07191}
192
193ZeroSuggestProvider::~ZeroSuggestProvider() {
194}
195
[email protected]776ee5902014-08-11 09:15:19196const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
197 // Zero suggest provider should not receive keyword results.
198 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13199 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19200}
201
202const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18203 // The callers of this method won't look at the AutocompleteInput's
204 // |from_omnibox_focus| member, so we can set its value to false.
blundelld130d592015-06-21 19:29:13205 return AutocompleteInput(base::string16(), base::string16::npos,
206 std::string(), GURL(current_query_),
207 current_page_classification_, true, false, false,
208 true, false, client()->GetSchemeClassifier());
[email protected]776ee5902014-08-11 09:15:19209}
210
211bool ZeroSuggestProvider::ShouldAppendExtraParams(
212 const SearchSuggestionParser::SuggestResult& result) const {
213 // We always use the default provider for search, so append the params.
214 return true;
215}
216
[email protected]776ee5902014-08-11 09:15:19217void ZeroSuggestProvider::RecordDeletionResult(bool success) {
218 if (success) {
219 base::RecordAction(
220 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
221 } else {
222 base::RecordAction(
223 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
224 }
225}
226
227void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) {
228 DCHECK(!done_);
229 DCHECK_EQ(fetcher_.get(), source);
230
231 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
232
233 bool results_updated = false;
234 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) {
235 std::string json_data = SearchSuggestionParser::ExtractJsonData(source);
236 scoped_ptr<base::Value> data(
237 SearchSuggestionParser::DeserializeJsonData(json_data));
238 if (data) {
239 if (StoreSuggestionResponse(json_data, *data))
240 return;
241 results_updated = ParseSuggestResults(
242 *data, kDefaultZeroSuggestRelevance, false, &results_);
243 }
244 }
245 fetcher_.reset();
246 done_ = true;
247 ConvertResultsToAutocompleteMatches();
248 listener_->OnProviderUpdate(results_updated);
249}
250
[email protected]855ebff2014-05-09 07:14:38251bool ZeroSuggestProvider::StoreSuggestionResponse(
252 const std::string& json_data,
253 const base::Value& parsed_data) {
254 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() ||
255 json_data.empty())
256 return false;
blundelld130d592015-06-21 19:29:13257 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
258 json_data);
[email protected]855ebff2014-05-09 07:14:38259
260 // If we received an empty result list, we should update the display, as it
261 // may be showing cached results that should not be shown.
262 const base::ListValue* root_list = NULL;
263 const base::ListValue* results_list = NULL;
264 if (parsed_data.GetAsList(&root_list) &&
265 root_list->GetList(1, &results_list) &&
266 results_list->empty())
267 return false;
268
269 // We are finished with the request and want to bail early.
270 if (results_from_cache_)
271 done_ = true;
272
273 return results_from_cache_;
274}
275
[email protected]bb1fb2b2013-05-31 00:21:01276void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37277 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30278 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22279 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11280 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01281}
282
[email protected]bb1fb2b2013-05-31 00:21:01283AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37284 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01285 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47286 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01287 match.destination_url = navigation.url();
288
[email protected]23db6492014-01-16 02:35:30289 // Zero suggest results should always omit protocols and never appear bold.
blundelld130d592015-06-21 19:29:13290 const std::string languages(client()->GetAcceptLanguages());
[email protected]bb1fb2b2013-05-31 00:21:01291 match.contents = net::FormatUrl(navigation.url(), languages,
292 net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, NULL, NULL, NULL);
293 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13294 AutocompleteInput::FormattedStringWithEquivalentMeaning(
295 navigation.url(), match.contents, client()->GetSchemeClassifier());
[email protected]bb1fb2b2013-05-31 00:21:01296
[email protected]b959d7d42013-12-13 17:26:37297 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01298 match.contents.length(), ACMatchClassification::URL,
299 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16300
301 match.description =
302 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37303 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16304 match.description.length(), ACMatchClassification::NONE,
305 &match.description_class);
[email protected]bb1fb2b2013-05-31 00:21:01306 return match;
307}
308
[email protected]9b9fa672013-11-07 06:04:52309void ZeroSuggestProvider::Run(const GURL& suggest_url) {
[email protected]8f064e52013-09-18 01:17:14310 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
311 most_visited_urls_.clear();
blundelld130d592015-06-21 19:29:13312 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
[email protected]8f064e52013-09-18 01:17:14313 if (ts) {
mariakhomenkobfc3a2a2014-10-24 00:48:22314 waiting_for_most_visited_urls_request_ = true;
[email protected]8f064e52013-09-18 01:17:14315 ts->GetMostVisitedURLs(
316 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
[email protected]ce767ab22013-11-12 03:50:09317 weak_ptr_factory_.GetWeakPtr()), false);
[email protected]8f064e52013-09-18 01:17:14318 }
mariakhomenkobfc3a2a2014-10-24 00:48:22319 } else {
320 const int kFetcherID = 1;
dtapuskadafcf892015-05-01 13:58:25321 fetcher_ = net::URLFetcher::Create(kFetcherID, suggest_url,
322 net::URLFetcher::GET, this);
blundelld130d592015-06-21 19:29:13323 fetcher_->SetRequestContext(client()->GetRequestContext());
mariakhomenkobfc3a2a2014-10-24 00:48:22324 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
325 // Add Chrome experiment state to the request headers.
326 net::HttpRequestHeaders headers;
327 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
blundelld130d592015-06-21 19:29:13328 fetcher_->GetOriginalURL(), client()->IsOffTheRecord(), false,
mariakhomenkobfc3a2a2014-10-24 00:48:22329 &headers);
330 fetcher_->SetExtraRequestHeaders(headers.ToString());
331 fetcher_->Start();
332 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
[email protected]8f064e52013-09-18 01:17:14333 }
[email protected]bb1fb2b2013-05-31 00:21:01334}
335
[email protected]8f064e52013-09-18 01:17:14336void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
337 const history::MostVisitedURLList& urls) {
mariakhomenkobfc3a2a2014-10-24 00:48:22338 if (!waiting_for_most_visited_urls_request_) return;
[email protected]8f064e52013-09-18 01:17:14339 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22340 waiting_for_most_visited_urls_request_ = false;
341 done_ = true;
342 ConvertResultsToAutocompleteMatches();
343 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14344}
345
[email protected]9c97f89c2013-06-25 03:12:16346void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01347 matches_.clear();
348
blundelld130d592015-06-21 19:29:13349 TemplateURLService* template_url_service = client()->GetTemplateURLService();
[email protected]bb1fb2b2013-05-31 00:21:01350 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13351 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07352 // Fail if we can't set the clickthrough URL for query suggestions.
blundelld130d592015-06-21 19:29:13353 if (default_provider == NULL ||
354 !default_provider->SupportsReplacement(
355 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07356 return;
[email protected]6ce7f612012-09-05 23:53:07357
[email protected]00404742014-02-20 13:09:05358 MatchMap map;
359 AddSuggestResultsToMap(results_.suggest_results, &map);
360
361 const int num_query_results = map.size();
362 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01363 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16364 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
[email protected]78981d8c2014-05-09 15:05:47365 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
[email protected]9c97f89c2013-06-25 03:12:16366 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01367
[email protected]8f064e52013-09-18 01:17:14368 // Show Most Visited results after ZeroSuggest response is received.
369 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13370 if (!current_url_match_.destination_url.is_valid())
371 return;
[email protected]8f064e52013-09-18 01:17:14372 matches_.push_back(current_url_match_);
373 int relevance = 600;
374 if (num_results > 0) {
375 UMA_HISTOGRAM_COUNTS(
376 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
377 most_visited_urls_.size());
378 }
[email protected]23db6492014-01-16 02:35:30379 const base::string16 current_query_string16(
380 base::ASCIIToUTF16(current_query_));
blundelld130d592015-06-21 19:29:13381 const std::string languages(client()->GetAcceptLanguages());
[email protected]8f064e52013-09-18 01:17:14382 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
383 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37384 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13385 client()->GetSchemeClassifier(), url.url,
[email protected]7720fc32014-07-09 06:10:05386 AutocompleteMatchType::NAVSUGGEST, url.title, std::string(), false,
387 relevance, true, current_query_string16, languages);
[email protected]8f064e52013-09-18 01:17:14388 matches_.push_back(NavigationToMatch(nav));
389 --relevance;
390 }
391 return;
392 }
393
[email protected]9c97f89c2013-06-25 03:12:16394 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01395 return;
396
397 // TODO(jered): Rip this out once the first match is decoupled from the
398 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01399 matches_.push_back(current_url_match_);
400
[email protected]00404742014-02-20 13:09:05401 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01402 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01403
[email protected]0b9575f2014-07-30 11:58:37404 const SearchSuggestionParser::NavigationResults& nav_results(
405 results_.navigation_results);
406 for (SearchSuggestionParser::NavigationResults::const_iterator it(
407 nav_results.begin()); it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01408 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07409}
410
[email protected]bb1fb2b2013-05-31 00:21:01411AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]2d915782013-08-29 09:50:21412 AutocompleteMatch match;
blundelld130d592015-06-21 19:29:13413 client()->GetAutocompleteClassifier()->Classify(
[email protected]51abb7b2014-02-09 23:00:08414 permanent_text_, false, true, current_page_classification_, &match, NULL);
[email protected]45f89a92013-08-12 13:41:36415 match.allowed_to_be_default_match = true;
[email protected]6ce7f612012-09-05 23:53:07416
[email protected]bb1fb2b2013-05-31 00:21:01417 // The placeholder suggestion for the current URL has high relevance so
418 // that it is in the first suggestion slot and inline autocompleted. It
419 // gets dropped as soon as the user types something.
[email protected]00404742014-02-20 13:09:05420 match.relevance = GetVerbatimRelevance();
[email protected]6ce7f612012-09-05 23:53:07421
[email protected]bb1fb2b2013-05-31 00:21:01422 return match;
[email protected]6ce7f612012-09-05 23:53:07423}
[email protected]00404742014-02-20 13:09:05424
425int ZeroSuggestProvider::GetVerbatimRelevance() const {
426 return results_.verbatim_relevance >= 0 ?
427 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
428}
[email protected]162c8d9fa2014-03-18 20:25:41429
mariakhomenkobfc3a2a2014-10-24 00:48:22430bool ZeroSuggestProvider::ShouldShowNonContextualZeroSuggest(
[email protected]162c8d9fa2014-03-18 20:25:41431 const GURL& suggest_url,
432 const GURL& current_page_url) const {
blundelld130d592015-06-21 19:29:13433 const TemplateURLService* template_url_service =
434 client()->GetTemplateURLService();
[email protected]162c8d9fa2014-03-18 20:25:41435 if (!ZeroSuggestEnabled(suggest_url,
blundelld130d592015-06-21 19:29:13436 template_url_service->GetDefaultSearchProvider(),
[email protected]e6477f12014-08-05 07:59:54437 current_page_classification_,
blundelld130d592015-06-21 19:29:13438 template_url_service->search_terms_data(), client()))
[email protected]162c8d9fa2014-03-18 20:25:41439 return false;
440
441 // If we cannot send URLs, then only the MostVisited and Personalized
442 // variations can be shown.
443 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() &&
444 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
445 return false;
446
447 // Only show zero suggest for HTTP[S] pages.
448 // TODO(mariakhomenko): We may be able to expand this set to include pages
449 // with other schemes (e.g. chrome://). That may require improvements to
450 // the formatting of the verbatim result returned by MatchForCurrentURL().
451 if (!current_page_url.is_valid() ||
[email protected]e8ca69c2014-05-07 15:31:19452 ((current_page_url.scheme() != url::kHttpScheme) &&
453 (current_page_url.scheme() != url::kHttpsScheme)))
[email protected]162c8d9fa2014-03-18 20:25:41454 return false;
455
mariakhomenkobfc3a2a2014-10-24 00:48:22456 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() &&
blundelld130d592015-06-21 19:29:13457 client()
458 ->GetTemplateURLService()
459 ->IsSearchResultsPageFromDefaultSearchProvider(current_page_url))
mariakhomenkobfc3a2a2014-10-24 00:48:22460 return false;
461
[email protected]162c8d9fa2014-03-18 20:25:41462 return true;
463}
[email protected]855ebff2014-05-09 07:14:38464
465void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
466 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
467 return;
468
blundelld130d592015-06-21 19:29:13469 std::string json_data =
470 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38471 if (!json_data.empty()) {
[email protected]2c802d12014-07-31 12:57:14472 scoped_ptr<base::Value> data(
473 SearchSuggestionParser::DeserializeJsonData(json_data));
[email protected]776ee5902014-08-11 09:15:19474 if (data && ParseSuggestResults(
475 *data, kDefaultZeroSuggestRelevance, false, &results_)) {
[email protected]855ebff2014-05-09 07:14:38476 ConvertResultsToAutocompleteMatches();
477 results_from_cache_ = !matches_.empty();
478 }
479 }
480}