blob: 688677023c0f752fbed0d8be82ddfe147906b7f4 [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"
18#include "chrome/browser/autocomplete/autocomplete_classifier_factory.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]6ce7f612012-09-05 23:53:0724#include "chrome/browser/profiles/profile.h"
[email protected]a00008d42012-09-15 05:07:5825#include "chrome/common/pref_names.h"
[email protected]3dc75b12014-06-08 00:02:2226#include "components/metrics/proto/omnibox_input_type.pb.h"
[email protected]b1c5ab682014-08-07 11:53:1727#include "components/omnibox/autocomplete_input.h"
28#include "components/omnibox/autocomplete_match.h"
29#include "components/omnibox/autocomplete_provider_listener.h"
[email protected]4c583b62014-08-08 10:37:2330#include "components/omnibox/omnibox_field_trial.h"
[email protected]f0c8c4992014-05-15 17:37:2631#include "components/pref_registry/pref_registry_syncable.h"
[email protected]bf5c532d2014-07-05 00:29:5332#include "components/search_engines/template_url_service.h"
[email protected]71011c1682014-07-09 17:19:1633#include "components/variations/variations_http_header_provider.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/base/net_util.h"
37#include "net/http/http_request_headers.h"
[email protected]6ce7f612012-09-05 23:53:0738#include "net/url_request/url_fetcher.h"
39#include "net/url_request/url_request_status.h"
[email protected]761fa4702013-07-02 15:25:1540#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0741
42namespace {
[email protected]bb1fb2b2013-05-31 00:21:0143
44// TODO(hfung): The histogram code was copied and modified from
45// search_provider.cc. Refactor and consolidate the code.
46// We keep track in a histogram how many suggest requests we send, how
47// many suggest requests we invalidate (e.g., due to a user typing
48// another character), and how many replies we receive.
49// *** ADD NEW ENUMS AFTER ALL PREVIOUSLY DEFINED ONES! ***
50// (excluding the end-of-list enum value)
51// We do not want values of existing enums to change or else it screws
52// up the statistics.
53enum ZeroSuggestRequestsHistogramValue {
54 ZERO_SUGGEST_REQUEST_SENT = 1,
55 ZERO_SUGGEST_REQUEST_INVALIDATED,
56 ZERO_SUGGEST_REPLY_RECEIVED,
57 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
58};
59
60void LogOmniboxZeroSuggestRequest(
61 ZeroSuggestRequestsHistogramValue request_value) {
62 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
63 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
64}
65
66// The maximum relevance of the top match from this provider.
67const int kDefaultVerbatimZeroSuggestRelevance = 1300;
68
69// Relevance value to use if it was not set explicitly by the server.
70const int kDefaultZeroSuggestRelevance = 100;
71
[email protected]6ce7f612012-09-05 23:53:0772} // namespace
73
[email protected]a00008d42012-09-15 05:07:5874// static
75ZeroSuggestProvider* ZeroSuggestProvider::Create(
76 AutocompleteProviderListener* listener,
[email protected]e6477f12014-08-05 07:59:5477 TemplateURLService* template_url_service,
[email protected]a00008d42012-09-15 05:07:5878 Profile* profile) {
[email protected]e6477f12014-08-05 07:59:5479 return new ZeroSuggestProvider(listener, template_url_service, profile);
[email protected]6ce7f612012-09-05 23:53:0780}
81
[email protected]855ebff2014-05-09 07:14:3882// static
83void ZeroSuggestProvider::RegisterProfilePrefs(
84 user_prefs::PrefRegistrySyncable* registry) {
85 registry->RegisterStringPref(
86 prefs::kZeroSuggestCachedResults,
87 std::string(),
88 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
89}
90
[email protected]6ce7f612012-09-05 23:53:0791void ZeroSuggestProvider::Start(const AutocompleteInput& input,
[email protected]f030c4d2014-03-25 01:05:5492 bool minimal_changes) {
93 matches_.clear();
[email protected]3dc75b12014-06-08 00:02:2294 if (input.type() == metrics::OmniboxInputType::INVALID)
[email protected]f030c4d2014-03-25 01:05:5495 return;
[email protected]bb1fb2b2013-05-31 00:21:0196
[email protected]bb1fb2b2013-05-31 00:21:0197 Stop(true);
98 field_trial_triggered_ = false;
99 field_trial_triggered_in_session_ = false;
[email protected]855ebff2014-05-09 07:14:38100 results_from_cache_ = false;
[email protected]f030c4d2014-03-25 01:05:54101 permanent_text_ = input.text();
102 current_query_ = input.current_url().spec();
103 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52104 current_url_match_ = MatchForCurrentURL();
105
106 const TemplateURL* default_provider =
107 template_url_service_->GetDefaultSearchProvider();
108 if (default_provider == NULL)
109 return;
[email protected]162c8d9fa2014-03-18 20:25:41110
[email protected]96920152013-12-04 21:00:16111 base::string16 prefix;
[email protected]9b9fa672013-11-07 06:04:52112 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
[email protected]162c8d9fa2014-03-18 20:25:41113 GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms(
[email protected]ce7ee5f2014-06-16 23:41:19114 search_term_args, template_url_service_->search_terms_data()));
[email protected]162c8d9fa2014-03-18 20:25:41115 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07116 return;
[email protected]162c8d9fa2014-03-18 20:25:41117
118 // No need to send the current page URL in personalized suggest field trial.
[email protected]f030c4d2014-03-25 01:05:54119 if (CanSendURL(input.current_url(), suggest_url, default_provider,
[email protected]e6477f12014-08-05 07:59:54120 current_page_classification_,
121 template_url_service_->search_terms_data(), profile_) &&
[email protected]162c8d9fa2014-03-18 20:25:41122 !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]ec3f679b2014-08-18 07:45:13142void ZeroSuggestProvider::Stop(bool clear_cached_results) {
143 if (fetcher_)
144 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
145 fetcher_.reset();
146 done_ = true;
147
148 if (clear_cached_results) {
149 // We do not call Clear() on |results_| to retain |verbatim_relevance|
150 // value in the |results_| object. |verbatim_relevance| is used at the
151 // beginning of the next StartZeroSuggest() call to determine the current
152 // url match relevance.
153 results_.suggest_results.clear();
154 results_.navigation_results.clear();
155 current_query_.clear();
156 }
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.
163 profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults,
164 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);
171 if (!results_.suggest_results.empty() || !results_.navigation_results.empty())
172 provider_info->back().set_times_returned_results_in_session(1);
173}
174
[email protected]f030c4d2014-03-25 01:05:54175void ZeroSuggestProvider::ResetSession() {
176 // The user has started editing in the omnibox, so leave
177 // |field_trial_triggered_in_session_| unchanged and set
178 // |field_trial_triggered_| to false since zero suggest is inactive now.
179 field_trial_triggered_ = false;
180}
181
[email protected]bb1fb2b2013-05-31 00:21:01182ZeroSuggestProvider::ZeroSuggestProvider(
183 AutocompleteProviderListener* listener,
[email protected]e6477f12014-08-05 07:59:54184 TemplateURLService* template_url_service,
[email protected]bb1fb2b2013-05-31 00:21:01185 Profile* profile)
[email protected]776ee5902014-08-11 09:15:19186 : BaseSearchProvider(template_url_service, profile,
[email protected]02346202014-02-05 05:18:30187 AutocompleteProvider::TYPE_ZERO_SUGGEST),
[email protected]776ee5902014-08-11 09:15:19188 listener_(listener),
[email protected]855ebff2014-05-09 07:14:38189 results_from_cache_(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);
199 return template_url_service_->GetDefaultSearchProvider();
200}
201
202const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
203 return AutocompleteInput(
204 base::string16(), base::string16::npos, base::string16(),
205 GURL(current_query_), current_page_classification_, true, false, false,
206 true, ChromeAutocompleteSchemeClassifier(profile_));
207}
208
209bool ZeroSuggestProvider::ShouldAppendExtraParams(
210 const SearchSuggestionParser::SuggestResult& result) const {
211 // We always use the default provider for search, so append the params.
212 return true;
213}
214
[email protected]776ee5902014-08-11 09:15:19215void ZeroSuggestProvider::RecordDeletionResult(bool success) {
216 if (success) {
217 base::RecordAction(
218 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
219 } else {
220 base::RecordAction(
221 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
222 }
223}
224
225void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) {
226 DCHECK(!done_);
227 DCHECK_EQ(fetcher_.get(), source);
228
229 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
230
231 bool results_updated = false;
232 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) {
233 std::string json_data = SearchSuggestionParser::ExtractJsonData(source);
234 scoped_ptr<base::Value> data(
235 SearchSuggestionParser::DeserializeJsonData(json_data));
236 if (data) {
237 if (StoreSuggestionResponse(json_data, *data))
238 return;
239 results_updated = ParseSuggestResults(
240 *data, kDefaultZeroSuggestRelevance, false, &results_);
241 }
242 }
243 fetcher_.reset();
244 done_ = true;
245 ConvertResultsToAutocompleteMatches();
246 listener_->OnProviderUpdate(results_updated);
247}
248
[email protected]855ebff2014-05-09 07:14:38249bool ZeroSuggestProvider::StoreSuggestionResponse(
250 const std::string& json_data,
251 const base::Value& parsed_data) {
252 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() ||
253 json_data.empty())
254 return false;
255 profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults, json_data);
256
257 // If we received an empty result list, we should update the display, as it
258 // may be showing cached results that should not be shown.
259 const base::ListValue* root_list = NULL;
260 const base::ListValue* results_list = NULL;
261 if (parsed_data.GetAsList(&root_list) &&
262 root_list->GetList(1, &results_list) &&
263 results_list->empty())
264 return false;
265
266 // We are finished with the request and want to bail early.
267 if (results_from_cache_)
268 done_ = true;
269
270 return results_from_cache_;
271}
272
[email protected]bb1fb2b2013-05-31 00:21:01273void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37274 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30275 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22276 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11277 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01278}
279
[email protected]bb1fb2b2013-05-31 00:21:01280AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37281 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01282 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47283 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01284 match.destination_url = navigation.url();
285
[email protected]23db6492014-01-16 02:35:30286 // Zero suggest results should always omit protocols and never appear bold.
[email protected]bb1fb2b2013-05-31 00:21:01287 const std::string languages(
288 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
289 match.contents = net::FormatUrl(navigation.url(), languages,
290 net::kFormatUrlOmitAll, net::UnescapeRule::SPACES, NULL, NULL, NULL);
291 match.fill_into_edit +=
292 AutocompleteInput::FormattedStringWithEquivalentMeaning(navigation.url(),
[email protected]a817ed392014-06-27 05:03:00293 match.contents, ChromeAutocompleteSchemeClassifier(profile_));
[email protected]bb1fb2b2013-05-31 00:21:01294
[email protected]b959d7d42013-12-13 17:26:37295 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01296 match.contents.length(), ACMatchClassification::URL,
297 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16298
299 match.description =
300 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37301 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16302 match.description.length(), ACMatchClassification::NONE,
303 &match.description_class);
[email protected]bb1fb2b2013-05-31 00:21:01304 return match;
305}
306
[email protected]9b9fa672013-11-07 06:04:52307void ZeroSuggestProvider::Run(const GURL& suggest_url) {
[email protected]bb1fb2b2013-05-31 00:21:01308 const int kFetcherID = 1;
[email protected]bb1fb2b2013-05-31 00:21:01309 fetcher_.reset(
310 net::URLFetcher::Create(kFetcherID,
311 suggest_url,
312 net::URLFetcher::GET, this));
313 fetcher_->SetRequestContext(profile_->GetRequestContext());
314 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
315 // Add Chrome experiment state to the request headers.
316 net::HttpRequestHeaders headers;
[email protected]71011c1682014-07-09 17:19:16317 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
[email protected]bb1fb2b2013-05-31 00:21:01318 fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers);
319 fetcher_->SetExtraRequestHeaders(headers.ToString());
[email protected]bb1fb2b2013-05-31 00:21:01320 fetcher_->Start();
[email protected]8f064e52013-09-18 01:17:14321
322 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
323 most_visited_urls_.clear();
324 history::TopSites* ts = profile_->GetTopSites();
325 if (ts) {
326 ts->GetMostVisitedURLs(
327 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
[email protected]ce767ab22013-11-12 03:50:09328 weak_ptr_factory_.GetWeakPtr()), false);
[email protected]8f064e52013-09-18 01:17:14329 }
330 }
[email protected]bb1fb2b2013-05-31 00:21:01331 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
332}
333
[email protected]8f064e52013-09-18 01:17:14334void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
335 const history::MostVisitedURLList& urls) {
336 most_visited_urls_ = urls;
337}
338
[email protected]9c97f89c2013-06-25 03:12:16339void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01340 matches_.clear();
341
342 const TemplateURL* default_provider =
[email protected]6ce7f612012-09-05 23:53:07343 template_url_service_->GetDefaultSearchProvider();
344 // Fail if we can't set the clickthrough URL for query suggestions.
[email protected]ce7ee5f2014-06-16 23:41:19345 if (default_provider == NULL || !default_provider->SupportsReplacement(
346 template_url_service_->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07347 return;
[email protected]6ce7f612012-09-05 23:53:07348
[email protected]00404742014-02-20 13:09:05349 MatchMap map;
350 AddSuggestResultsToMap(results_.suggest_results, &map);
351
352 const int num_query_results = map.size();
353 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01354 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16355 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
[email protected]78981d8c2014-05-09 15:05:47356 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
[email protected]9c97f89c2013-06-25 03:12:16357 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01358
[email protected]8f064e52013-09-18 01:17:14359 // Show Most Visited results after ZeroSuggest response is received.
360 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13361 if (!current_url_match_.destination_url.is_valid())
362 return;
[email protected]8f064e52013-09-18 01:17:14363 matches_.push_back(current_url_match_);
364 int relevance = 600;
365 if (num_results > 0) {
366 UMA_HISTOGRAM_COUNTS(
367 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
368 most_visited_urls_.size());
369 }
[email protected]23db6492014-01-16 02:35:30370 const base::string16 current_query_string16(
371 base::ASCIIToUTF16(current_query_));
372 const std::string languages(
373 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]8f064e52013-09-18 01:17:14374 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
375 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37376 SearchSuggestionParser::NavigationResult nav(
[email protected]7720fc32014-07-09 06:10:05377 ChromeAutocompleteSchemeClassifier(profile_), url.url,
378 AutocompleteMatchType::NAVSUGGEST, url.title, std::string(), false,
379 relevance, true, current_query_string16, languages);
[email protected]8f064e52013-09-18 01:17:14380 matches_.push_back(NavigationToMatch(nav));
381 --relevance;
382 }
383 return;
384 }
385
[email protected]9c97f89c2013-06-25 03:12:16386 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01387 return;
388
389 // TODO(jered): Rip this out once the first match is decoupled from the
390 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01391 matches_.push_back(current_url_match_);
392
[email protected]00404742014-02-20 13:09:05393 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01394 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01395
[email protected]0b9575f2014-07-30 11:58:37396 const SearchSuggestionParser::NavigationResults& nav_results(
397 results_.navigation_results);
398 for (SearchSuggestionParser::NavigationResults::const_iterator it(
399 nav_results.begin()); it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01400 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07401}
402
[email protected]bb1fb2b2013-05-31 00:21:01403AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]2d915782013-08-29 09:50:21404 AutocompleteMatch match;
405 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify(
[email protected]51abb7b2014-02-09 23:00:08406 permanent_text_, false, true, current_page_classification_, &match, NULL);
[email protected]bb1fb2b2013-05-31 00:21:01407 match.is_history_what_you_typed_match = false;
[email protected]45f89a92013-08-12 13:41:36408 match.allowed_to_be_default_match = true;
[email protected]6ce7f612012-09-05 23:53:07409
[email protected]bb1fb2b2013-05-31 00:21:01410 // The placeholder suggestion for the current URL has high relevance so
411 // that it is in the first suggestion slot and inline autocompleted. It
412 // gets dropped as soon as the user types something.
[email protected]00404742014-02-20 13:09:05413 match.relevance = GetVerbatimRelevance();
[email protected]6ce7f612012-09-05 23:53:07414
[email protected]bb1fb2b2013-05-31 00:21:01415 return match;
[email protected]6ce7f612012-09-05 23:53:07416}
[email protected]00404742014-02-20 13:09:05417
418int ZeroSuggestProvider::GetVerbatimRelevance() const {
419 return results_.verbatim_relevance >= 0 ?
420 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance;
421}
[email protected]162c8d9fa2014-03-18 20:25:41422
423bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL(
424 const GURL& suggest_url,
425 const GURL& current_page_url) const {
426 if (!ZeroSuggestEnabled(suggest_url,
427 template_url_service_->GetDefaultSearchProvider(),
[email protected]e6477f12014-08-05 07:59:54428 current_page_classification_,
429 template_url_service_->search_terms_data(), profile_))
[email protected]162c8d9fa2014-03-18 20:25:41430 return false;
431
432 // If we cannot send URLs, then only the MostVisited and Personalized
433 // variations can be shown.
434 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() &&
435 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
436 return false;
437
438 // Only show zero suggest for HTTP[S] pages.
439 // TODO(mariakhomenko): We may be able to expand this set to include pages
440 // with other schemes (e.g. chrome://). That may require improvements to
441 // the formatting of the verbatim result returned by MatchForCurrentURL().
442 if (!current_page_url.is_valid() ||
[email protected]e8ca69c2014-05-07 15:31:19443 ((current_page_url.scheme() != url::kHttpScheme) &&
444 (current_page_url.scheme() != url::kHttpsScheme)))
[email protected]162c8d9fa2014-03-18 20:25:41445 return false;
446
447 return true;
448}
[email protected]855ebff2014-05-09 07:14:38449
450void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
451 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
452 return;
453
454 std::string json_data = profile_->GetPrefs()->GetString(
455 prefs::kZeroSuggestCachedResults);
456 if (!json_data.empty()) {
[email protected]2c802d12014-07-31 12:57:14457 scoped_ptr<base::Value> data(
458 SearchSuggestionParser::DeserializeJsonData(json_data));
[email protected]776ee5902014-08-11 09:15:19459 if (data && ParseSuggestResults(
460 *data, kDefaultZeroSuggestRelevance, false, &results_)) {
[email protected]855ebff2014-05-09 07:14:38461 ConvertResultsToAutocompleteMatches();
462 results_from_cache_ = !matches_.empty();
463 }
464 }
465}