blob: a12a1b4432edccb648e1864ad89f1986a0079ebc [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
blundell2102f7c2015-07-09 10:00:535#include "components/omnibox/browser/zero_suggest_provider.h"
[email protected]6ce7f612012-09-05 23:53:076
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"
amohammadkhanf76ae112015-09-14 17:34:4317#include "components/data_use_measurement/core/data_use_user_data.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"
blundell2102f7c2015-07-09 10:00:5321#include "components/omnibox/browser/autocomplete_classifier.h"
22#include "components/omnibox/browser/autocomplete_input.h"
23#include "components/omnibox/browser/autocomplete_match.h"
24#include "components/omnibox/browser/autocomplete_provider_listener.h"
25#include "components/omnibox/browser/history_url_provider.h"
26#include "components/omnibox/browser/omnibox_field_trial.h"
27#include "components/omnibox/browser/omnibox_pref_names.h"
28#include "components/omnibox/browser/search_provider.h"
sdefresne70948d62015-08-11 10:46:3529#include "components/omnibox/browser/verbatim_match.h"
[email protected]f0c8c4992014-05-15 17:37:2630#include "components/pref_registry/pref_registry_syncable.h"
[email protected]bf5c532d2014-07-05 00:29:5331#include "components/search_engines/template_url_service.h"
rsleevi24f64dc22015-08-07 21:39:2132#include "components/url_formatter/url_formatter.h"
asvitkine9a279832015-12-18 02:35:5033#include "components/variations/net/variations_http_headers.h"
[email protected]bb1fb2b2013-05-31 00:21:0134#include "net/base/escape.h"
[email protected]6ce7f612012-09-05 23:53:0735#include "net/base/load_flags.h"
[email protected]bb1fb2b2013-05-31 00:21:0136#include "net/http/http_request_headers.h"
[email protected]6ce7f612012-09-05 23:53:0737#include "net/url_request/url_fetcher.h"
38#include "net/url_request/url_request_status.h"
[email protected]761fa4702013-07-02 15:25:1539#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0740
41namespace {
[email protected]bb1fb2b2013-05-31 00:21:0142
43// TODO(hfung): The histogram code was copied and modified from
44// search_provider.cc. Refactor and consolidate the code.
45// We keep track in a histogram how many suggest requests we send, how
46// many suggest requests we invalidate (e.g., due to a user typing
47// another character), and how many replies we receive.
48// *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
49// (excluding the end-of-list enum value)
50// We do not want values of existing enums to change or else it screws
51// up the statistics.
52enum ZeroSuggestRequestsHistogramValue {
53 ZERO_SUGGEST_REQUEST_SENT = 1,
54 ZERO_SUGGEST_REQUEST_INVALIDATED,
55 ZERO_SUGGEST_REPLY_RECEIVED,
56 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
57};
58
59void LogOmniboxZeroSuggestRequest(
60 ZeroSuggestRequestsHistogramValue request_value) {
61 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
62 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
63}
64
[email protected]bb1fb2b2013-05-31 00:21:0165// Relevance value to use if it was not set explicitly by the server.
66const int kDefaultZeroSuggestRelevance = 100;
67
[email protected]6ce7f612012-09-05 23:53:0768} // namespace
69
[email protected]a00008d42012-09-15 05:07:5870// static
71ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:1872 AutocompleteProviderClient* client,
blundelld130d592015-06-21 19:29:1373 AutocompleteProviderListener* listener) {
74 return new ZeroSuggestProvider(client, listener);
[email protected]6ce7f612012-09-05 23:53:0775}
76
[email protected]855ebff2014-05-09 07:14:3877// static
78void ZeroSuggestProvider::RegisterProfilePrefs(
79 user_prefs::PrefRegistrySyncable* registry) {
blundelld130d592015-06-21 19:29:1380 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
81 std::string());
[email protected]855ebff2014-05-09 07:14:3882}
83
[email protected]6ce7f612012-09-05 23:53:0784void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:1885 bool minimal_changes) {
[email protected]f030c4d2014-03-25 01:05:5486 matches_.clear();
jifcf322cd2015-06-17 11:01:1887 if (!input.from_omnibox_focus() ||
mariakhomenko3ef531d72015-01-10 00:03:4388 input.type() == metrics::OmniboxInputType::INVALID)
[email protected]f030c4d2014-03-25 01:05:5489 return;
[email protected]bb1fb2b2013-05-31 00:21:0190
mpearson8a37c382015-03-07 05:58:5791 Stop(true, false);
blundelld130d592015-06-21 19:29:1392 set_field_trial_triggered(false);
93 set_field_trial_triggered_in_session(false);
[email protected]855ebff2014-05-09 07:14:3894 results_from_cache_ = false;
[email protected]f030c4d2014-03-25 01:05:5495 permanent_text_ = input.text();
96 current_query_ = input.current_url().spec();
97 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:5298 current_url_match_ = MatchForCurrentURL();
blundelld130d592015-06-21 19:29:1399 TemplateURLService* template_url_service = client()->GetTemplateURLService();
[email protected]9b9fa672013-11-07 06:04:52100
101 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13102 template_url_service->GetDefaultSearchProvider();
[email protected]9b9fa672013-11-07 06:04:52103 if (default_provider == NULL)
104 return;
[email protected]162c8d9fa2014-03-18 20:25:41105
[email protected]96920152013-12-04 21:00:16106 base::string16 prefix;
[email protected]9b9fa672013-11-07 06:04:52107 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
[email protected]162c8d9fa2014-03-18 20:25:41108 GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms(
blundelld130d592015-06-21 19:29:13109 search_term_args, template_url_service->search_terms_data()));
[email protected]162c8d9fa2014-03-18 20:25:41110 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07111 return;
[email protected]162c8d9fa2014-03-18 20:25:41112
mariakhomenkobfc3a2a2014-10-24 00:48:22113 // No need to send the current page URL in personalized suggest or
114 // most visited field trials.
[email protected]f030c4d2014-03-25 01:05:54115 if (CanSendURL(input.current_url(), suggest_url, default_provider,
[email protected]e6477f12014-08-05 07:59:54116 current_page_classification_,
blundelld130d592015-06-21 19:29:13117 template_url_service->search_terms_data(), client()) &&
mariakhomenkobfc3a2a2014-10-24 00:48:22118 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() &&
119 !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]162c8d9fa2014-03-18 20:25:41120 // Update suggest_url to include the current_page_url.
121 search_term_args.current_page_url = current_query_;
blundelld130d592015-06-21 19:29:13122 suggest_url =
123 GURL(default_provider->suggestions_url_ref().ReplaceSearchTerms(
124 search_term_args, template_url_service->search_terms_data()));
mariakhomenkobfc3a2a2014-10-24 00:48:22125 } else if (!ShouldShowNonContextualZeroSuggest(suggest_url,
126 input.current_url())) {
[email protected]162c8d9fa2014-03-18 20:25:41127 return;
128 }
129
[email protected]6ce7f612012-09-05 23:53:07130 done_ = false;
[email protected]6ce7f612012-09-05 23:53:07131 // TODO(jered): Consider adding locally-sourced zero-suggestions here too.
132 // These may be useful on the NTP or more relevant to the user than server
133 // suggestions, if based on local browsing history.
[email protected]855ebff2014-05-09 07:14:38134 MaybeUseCachedSuggestions();
[email protected]9b9fa672013-11-07 06:04:52135 Run(suggest_url);
[email protected]6ce7f612012-09-05 23:53:07136}
137
mpearson8a37c382015-03-07 05:58:57138void ZeroSuggestProvider::Stop(bool clear_cached_results,
139 bool due_to_user_inactivity) {
[email protected]ec3f679b2014-08-18 07:45:13140 if (fetcher_)
141 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
142 fetcher_.reset();
mariakhomenkobfc3a2a2014-10-24 00:48:22143 waiting_for_most_visited_urls_request_ = false;
[email protected]ec3f679b2014-08-18 07:45:13144 done_ = true;
145
146 if (clear_cached_results) {
147 // We do not call Clear() on |results_| to retain |verbatim_relevance|
148 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18149 // beginning of the next call to Start() to determine the current url
150 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13151 results_.suggest_results.clear();
152 results_.navigation_results.clear();
153 current_query_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45154 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13155 }
156}
157
[email protected]855ebff2014-05-09 07:14:38158void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
159 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) {
160 // Remove the deleted match from the cache, so it is not shown to the user
161 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13162 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38163 std::string());
164 }
165 BaseSearchProvider::DeleteMatch(match);
166}
167
[email protected]ec3f679b2014-08-18 07:45:13168void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
169 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45170 if (!results_.suggest_results.empty() ||
171 !results_.navigation_results.empty() ||
172 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13173 provider_info->back().set_times_returned_results_in_session(1);
174}
175
[email protected]f030c4d2014-03-25 01:05:54176void ZeroSuggestProvider::ResetSession() {
177 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13178 // |field_trial_triggered_in_session| unchanged and set
179 // |field_trial_triggered| to false since zero suggest is inactive now.
180 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54181}
182
blundelld130d592015-06-21 19:29:13183ZeroSuggestProvider::ZeroSuggestProvider(AutocompleteProviderClient* client,
184 AutocompleteProviderListener* listener)
185 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
[email protected]776ee5902014-08-11 09:15:19186 listener_(listener),
[email protected]855ebff2014-05-09 07:14:38187 results_from_cache_(false),
mariakhomenkobfc3a2a2014-10-24 00:48:22188 waiting_for_most_visited_urls_request_(false),
[email protected]8f064e52013-09-18 01:17:14189 weak_ptr_factory_(this) {
[email protected]6ce7f612012-09-05 23:53:07190}
191
192ZeroSuggestProvider::~ZeroSuggestProvider() {
193}
194
[email protected]776ee5902014-08-11 09:15:19195const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
196 // Zero suggest provider should not receive keyword results.
197 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13198 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19199}
200
201const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18202 // The callers of this method won't look at the AutocompleteInput's
203 // |from_omnibox_focus| member, so we can set its value to false.
blundelld130d592015-06-21 19:29:13204 return AutocompleteInput(base::string16(), base::string16::npos,
205 std::string(), GURL(current_query_),
206 current_page_classification_, true, false, false,
207 true, false, client()->GetSchemeClassifier());
[email protected]776ee5902014-08-11 09:15:19208}
209
210bool ZeroSuggestProvider::ShouldAppendExtraParams(
211 const SearchSuggestionParser::SuggestResult& result) const {
212 // We always use the default provider for search, so append the params.
213 return true;
214}
215
[email protected]776ee5902014-08-11 09:15:19216void ZeroSuggestProvider::RecordDeletionResult(bool success) {
217 if (success) {
218 base::RecordAction(
219 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
220 } else {
221 base::RecordAction(
222 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
223 }
224}
225
226void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) {
227 DCHECK(!done_);
228 DCHECK_EQ(fetcher_.get(), source);
229
230 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
231
232 bool results_updated = false;
233 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) {
234 std::string json_data = SearchSuggestionParser::ExtractJsonData(source);
235 scoped_ptr<base::Value> data(
236 SearchSuggestionParser::DeserializeJsonData(json_data));
237 if (data) {
238 if (StoreSuggestionResponse(json_data, *data))
239 return;
240 results_updated = ParseSuggestResults(
241 *data, kDefaultZeroSuggestRelevance, false, &results_);
242 }
243 }
244 fetcher_.reset();
245 done_ = true;
246 ConvertResultsToAutocompleteMatches();
247 listener_->OnProviderUpdate(results_updated);
248}
249
[email protected]855ebff2014-05-09 07:14:38250bool ZeroSuggestProvider::StoreSuggestionResponse(
251 const std::string& json_data,
252 const base::Value& parsed_data) {
253 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() ||
254 json_data.empty())
255 return false;
blundelld130d592015-06-21 19:29:13256 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
257 json_data);
[email protected]855ebff2014-05-09 07:14:38258
259 // If we received an empty result list, we should update the display, as it
260 // may be showing cached results that should not be shown.
261 const base::ListValue* root_list = NULL;
262 const base::ListValue* results_list = NULL;
263 if (parsed_data.GetAsList(&root_list) &&
264 root_list->GetList(1, &results_list) &&
265 results_list->empty())
266 return false;
267
268 // We are finished with the request and want to bail early.
269 if (results_from_cache_)
270 done_ = true;
271
272 return results_from_cache_;
273}
274
[email protected]bb1fb2b2013-05-31 00:21:01275void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37276 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30277 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22278 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11279 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01280}
281
[email protected]bb1fb2b2013-05-31 00:21:01282AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37283 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01284 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47285 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01286 match.destination_url = navigation.url();
287
[email protected]23db6492014-01-16 02:35:30288 // Zero suggest results should always omit protocols and never appear bold.
blundelld130d592015-06-21 19:29:13289 const std::string languages(client()->GetAcceptLanguages());
rsleevi24f64dc22015-08-07 21:39:21290 match.contents = url_formatter::FormatUrl(
291 navigation.url(), languages, url_formatter::kFormatUrlOmitAll,
292 net::UnescapeRule::SPACES, nullptr, nullptr, nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01293 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);
amohammadkhanf76ae112015-09-14 17:34:43323 data_use_measurement::DataUseUserData::AttachToFetcher(
324 fetcher_.get(), data_use_measurement::DataUseUserData::OMNIBOX);
blundelld130d592015-06-21 19:29:13325 fetcher_->SetRequestContext(client()->GetRequestContext());
mariakhomenkobfc3a2a2014-10-24 00:48:22326 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
327 // Add Chrome experiment state to the request headers.
328 net::HttpRequestHeaders headers;
asvitkine9a279832015-12-18 02:35:50329 variations::AppendVariationHeaders(fetcher_->GetOriginalURL(),
330 client()->IsOffTheRecord(), false,
331 &headers);
mariakhomenkobfc3a2a2014-10-24 00:48:22332 fetcher_->SetExtraRequestHeaders(headers.ToString());
333 fetcher_->Start();
334 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
[email protected]8f064e52013-09-18 01:17:14335 }
[email protected]bb1fb2b2013-05-31 00:21:01336}
337
[email protected]8f064e52013-09-18 01:17:14338void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
339 const history::MostVisitedURLList& urls) {
mariakhomenkobfc3a2a2014-10-24 00:48:22340 if (!waiting_for_most_visited_urls_request_) return;
[email protected]8f064e52013-09-18 01:17:14341 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22342 waiting_for_most_visited_urls_request_ = false;
343 done_ = true;
344 ConvertResultsToAutocompleteMatches();
345 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14346}
347
[email protected]9c97f89c2013-06-25 03:12:16348void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01349 matches_.clear();
350
blundelld130d592015-06-21 19:29:13351 TemplateURLService* template_url_service = client()->GetTemplateURLService();
[email protected]bb1fb2b2013-05-31 00:21:01352 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13353 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07354 // Fail if we can't set the clickthrough URL for query suggestions.
blundelld130d592015-06-21 19:29:13355 if (default_provider == NULL ||
356 !default_provider->SupportsReplacement(
357 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07358 return;
[email protected]6ce7f612012-09-05 23:53:07359
[email protected]00404742014-02-20 13:09:05360 MatchMap map;
361 AddSuggestResultsToMap(results_.suggest_results, &map);
362
363 const int num_query_results = map.size();
364 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01365 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16366 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
[email protected]78981d8c2014-05-09 15:05:47367 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
[email protected]9c97f89c2013-06-25 03:12:16368 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01369
[email protected]8f064e52013-09-18 01:17:14370 // Show Most Visited results after ZeroSuggest response is received.
371 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13372 if (!current_url_match_.destination_url.is_valid())
373 return;
[email protected]8f064e52013-09-18 01:17:14374 matches_.push_back(current_url_match_);
375 int relevance = 600;
376 if (num_results > 0) {
377 UMA_HISTOGRAM_COUNTS(
378 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
379 most_visited_urls_.size());
380 }
[email protected]23db6492014-01-16 02:35:30381 const base::string16 current_query_string16(
382 base::ASCIIToUTF16(current_query_));
blundelld130d592015-06-21 19:29:13383 const std::string languages(client()->GetAcceptLanguages());
[email protected]8f064e52013-09-18 01:17:14384 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
385 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37386 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13387 client()->GetSchemeClassifier(), url.url,
[email protected]7720fc32014-07-09 06:10:05388 AutocompleteMatchType::NAVSUGGEST, url.title, std::string(), false,
389 relevance, true, current_query_string16, languages);
[email protected]8f064e52013-09-18 01:17:14390 matches_.push_back(NavigationToMatch(nav));
391 --relevance;
392 }
393 return;
394 }
395
[email protected]9c97f89c2013-06-25 03:12:16396 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01397 return;
398
399 // TODO(jered): Rip this out once the first match is decoupled from the
400 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01401 matches_.push_back(current_url_match_);
402
[email protected]00404742014-02-20 13:09:05403 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01404 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01405
[email protected]0b9575f2014-07-30 11:58:37406 const SearchSuggestionParser::NavigationResults& nav_results(
407 results_.navigation_results);
408 for (SearchSuggestionParser::NavigationResults::const_iterator it(
409 nav_results.begin()); it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01410 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07411}
412
[email protected]bb1fb2b2013-05-31 00:21:01413AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]bb1fb2b2013-05-31 00:21:01414 // The placeholder suggestion for the current URL has high relevance so
415 // that it is in the first suggestion slot and inline autocompleted. It
416 // gets dropped as soon as the user types something.
sdefresne70948d62015-08-11 10:46:35417 return VerbatimMatchForURL(client(), permanent_text_,
418 current_page_classification_,
419 results_.verbatim_relevance);
[email protected]00404742014-02-20 13:09:05420}
[email protected]162c8d9fa2014-03-18 20:25:41421
mariakhomenkobfc3a2a2014-10-24 00:48:22422bool ZeroSuggestProvider::ShouldShowNonContextualZeroSuggest(
[email protected]162c8d9fa2014-03-18 20:25:41423 const GURL& suggest_url,
424 const GURL& current_page_url) const {
blundelld130d592015-06-21 19:29:13425 const TemplateURLService* template_url_service =
426 client()->GetTemplateURLService();
[email protected]162c8d9fa2014-03-18 20:25:41427 if (!ZeroSuggestEnabled(suggest_url,
blundelld130d592015-06-21 19:29:13428 template_url_service->GetDefaultSearchProvider(),
[email protected]e6477f12014-08-05 07:59:54429 current_page_classification_,
blundelld130d592015-06-21 19:29:13430 template_url_service->search_terms_data(), client()))
[email protected]162c8d9fa2014-03-18 20:25:41431 return false;
432
433 // If we cannot send URLs, then only the MostVisited and Personalized
434 // variations can be shown.
435 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() &&
436 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
437 return false;
438
439 // Only show zero suggest for HTTP[S] pages.
440 // TODO(mariakhomenko): We may be able to expand this set to include pages
441 // with other schemes (e.g. chrome://). That may require improvements to
442 // the formatting of the verbatim result returned by MatchForCurrentURL().
443 if (!current_page_url.is_valid() ||
[email protected]e8ca69c2014-05-07 15:31:19444 ((current_page_url.scheme() != url::kHttpScheme) &&
445 (current_page_url.scheme() != url::kHttpsScheme)))
[email protected]162c8d9fa2014-03-18 20:25:41446 return false;
447
mariakhomenkobfc3a2a2014-10-24 00:48:22448 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() &&
blundelld130d592015-06-21 19:29:13449 client()
450 ->GetTemplateURLService()
451 ->IsSearchResultsPageFromDefaultSearchProvider(current_page_url))
mariakhomenkobfc3a2a2014-10-24 00:48:22452 return false;
453
[email protected]162c8d9fa2014-03-18 20:25:41454 return true;
455}
[email protected]855ebff2014-05-09 07:14:38456
457void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
458 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
459 return;
460
blundelld130d592015-06-21 19:29:13461 std::string json_data =
462 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38463 if (!json_data.empty()) {
[email protected]2c802d12014-07-31 12:57:14464 scoped_ptr<base::Value> data(
465 SearchSuggestionParser::DeserializeJsonData(json_data));
[email protected]776ee5902014-08-11 09:15:19466 if (data && ParseSuggestResults(
467 *data, kDefaultZeroSuggestRelevance, false, &results_)) {
[email protected]855ebff2014-05-09 07:14:38468 ConvertResultsToAutocompleteMatches();
469 results_from_cache_ = !matches_.empty();
470 }
471 }
472}