blob: 3ab642a2f7b5cc66c5ef00b48626eba8b082ee38 [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
avif57136c12015-12-25 23:27:457#include <stddef.h>
8
[email protected]6ce7f612012-09-05 23:53:079#include "base/callback.h"
[email protected]bb1fb2b2013-05-31 00:21:0110#include "base/i18n/case_conversion.h"
[email protected]6ce7f612012-09-05 23:53:0711#include "base/json/json_string_value_serializer.h"
asvitkine30330812016-08-30 04:01:0812#include "base/metrics/histogram_macros.h"
[email protected]f7f41c0e2014-08-11 04:22:2313#include "base/metrics/user_metrics.h"
[email protected]98570e12013-06-10 19:54:2214#include "base/strings/string16.h"
15#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2016#include "base/strings/utf_string_conversions.h"
[email protected]4dcb7972013-06-28 15:15:4117#include "base/time/time.h"
a-v-ydd768d52016-03-25 21:07:4618#include "base/trace_event/trace_event.h"
amohammadkhanf76ae112015-09-14 17:34:4319#include "components/data_use_measurement/core/data_use_user_data.h"
sdefresnebc766ef2014-09-25 09:28:1320#include "components/history/core/browser/history_types.h"
sdefresne0da3bc02015-01-29 18:26:3521#include "components/history/core/browser/top_sites.h"
mpearson3d89cdc2017-03-03 21:15:4522#include "components/metrics/proto/omnibox_event.pb.h"
[email protected]3dc75b12014-06-08 00:02:2223#include "components/metrics/proto/omnibox_input_type.pb.h"
blundell2102f7c2015-07-09 10:00:5324#include "components/omnibox/browser/autocomplete_classifier.h"
25#include "components/omnibox/browser/autocomplete_input.h"
26#include "components/omnibox/browser/autocomplete_match.h"
27#include "components/omnibox/browser/autocomplete_provider_listener.h"
28#include "components/omnibox/browser/history_url_provider.h"
29#include "components/omnibox/browser/omnibox_field_trial.h"
30#include "components/omnibox/browser/omnibox_pref_names.h"
31#include "components/omnibox/browser/search_provider.h"
sdefresne70948d62015-08-11 10:46:3532#include "components/omnibox/browser/verbatim_match.h"
[email protected]f0c8c4992014-05-15 17:37:2633#include "components/pref_registry/pref_registry_syncable.h"
brettwf00b9b42016-02-01 22:11:3834#include "components/prefs/pref_service.h"
[email protected]bf5c532d2014-07-05 00:29:5335#include "components/search_engines/template_url_service.h"
rsleevi24f64dc22015-08-07 21:39:2136#include "components/url_formatter/url_formatter.h"
asvitkine9a279832015-12-18 02:35:5037#include "components/variations/net/variations_http_headers.h"
[email protected]bb1fb2b2013-05-31 00:21:0138#include "net/base/escape.h"
[email protected]6ce7f612012-09-05 23:53:0739#include "net/base/load_flags.h"
[email protected]bb1fb2b2013-05-31 00:21:0140#include "net/http/http_request_headers.h"
[email protected]6ce7f612012-09-05 23:53:0741#include "net/url_request/url_fetcher.h"
42#include "net/url_request/url_request_status.h"
[email protected]761fa4702013-07-02 15:25:1543#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0744
45namespace {
[email protected]bb1fb2b2013-05-31 00:21:0146
mpearson3d89cdc2017-03-03 21:15:4547// Represents whether ZeroSuggestProvider is allowed to display contextual
48// suggestions on focus, and if not, why not.
49// These values are written to logs. New enum values can be added, but existing
50// enums must never be renumbered or deleted and reused.
51enum class ZeroSuggestEligibility {
52 ELIGIBLE = 0,
53 // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL
54 // itself prevents ZeroSuggest from triggering.
55 URL_INELIGIBLE = 1,
56 GENERALLY_INELIGIBLE = 2,
57 ELIGIBLE_MAX_VALUE
58};
59
[email protected]bb1fb2b2013-05-31 00:21:0160// TODO(hfung): The histogram code was copied and modified from
61// search_provider.cc. Refactor and consolidate the code.
62// We keep track in a histogram how many suggest requests we send, how
63// many suggest requests we invalidate (e.g., due to a user typing
64// another character), and how many replies we receive.
mpearson3d89cdc2017-03-03 21:15:4565// These values are written to logs. New enum values can be added, but existing
66// enums must never be renumbered or deleted and reused.
[email protected]bb1fb2b2013-05-31 00:21:0167enum ZeroSuggestRequestsHistogramValue {
68 ZERO_SUGGEST_REQUEST_SENT = 1,
mpearson3d89cdc2017-03-03 21:15:4569 ZERO_SUGGEST_REQUEST_INVALIDATED = 2,
70 ZERO_SUGGEST_REPLY_RECEIVED = 3,
[email protected]bb1fb2b2013-05-31 00:21:0171 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
72};
73
74void LogOmniboxZeroSuggestRequest(
75 ZeroSuggestRequestsHistogramValue request_value) {
76 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
77 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
78}
79
[email protected]bb1fb2b2013-05-31 00:21:0180// Relevance value to use if it was not set explicitly by the server.
81const int kDefaultZeroSuggestRelevance = 100;
82
mpearson3d89cdc2017-03-03 21:15:4583// Used for testing whether zero suggest is ever available.
mpearsonc90c24b2017-03-04 00:11:2684constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/";
mpearson3d89cdc2017-03-03 21:15:4585
[email protected]6ce7f612012-09-05 23:53:0786} // namespace
87
[email protected]a00008d42012-09-15 05:07:5888// static
89ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:1890 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:1191 HistoryURLProvider* history_url_provider,
blundelld130d592015-06-21 19:29:1392 AutocompleteProviderListener* listener) {
mpearson931028c2016-07-01 18:55:1193 return new ZeroSuggestProvider(client, history_url_provider, listener);
[email protected]6ce7f612012-09-05 23:53:0794}
95
[email protected]855ebff2014-05-09 07:14:3896// static
97void ZeroSuggestProvider::RegisterProfilePrefs(
98 user_prefs::PrefRegistrySyncable* registry) {
blundelld130d592015-06-21 19:29:1399 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
100 std::string());
[email protected]855ebff2014-05-09 07:14:38101}
102
[email protected]6ce7f612012-09-05 23:53:07103void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18104 bool minimal_changes) {
a-v-ydd768d52016-03-25 21:07:46105 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54106 matches_.clear();
jifcf322cd2015-06-17 11:01:18107 if (!input.from_omnibox_focus() ||
mariakhomenko3ef531d72015-01-10 00:03:43108 input.type() == metrics::OmniboxInputType::INVALID)
[email protected]f030c4d2014-03-25 01:05:54109 return;
[email protected]bb1fb2b2013-05-31 00:21:01110
mpearson8a37c382015-03-07 05:58:57111 Stop(true, false);
blundelld130d592015-06-21 19:29:13112 set_field_trial_triggered(false);
113 set_field_trial_triggered_in_session(false);
[email protected]855ebff2014-05-09 07:14:38114 results_from_cache_ = false;
[email protected]f030c4d2014-03-25 01:05:54115 permanent_text_ = input.text();
116 current_query_ = input.current_url().spec();
117 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52118 current_url_match_ = MatchForCurrentURL();
[email protected]9b9fa672013-11-07 06:04:52119
mpearson3d89cdc2017-03-03 21:15:45120 std::string url_string = GetContextualSuggestionsUrl();
gcomanici9c1a2d82017-02-14 19:35:57121 GURL suggest_url(url_string);
[email protected]162c8d9fa2014-03-18 20:25:41122 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07123 return;
[email protected]162c8d9fa2014-03-18 20:25:41124
mariakhomenkobfc3a2a2014-10-24 00:48:22125 // No need to send the current page URL in personalized suggest or
126 // most visited field trials.
mpearson3d89cdc2017-03-03 21:15:45127 const TemplateURLService* template_url_service =
128 client()->GetTemplateURLService();
129 const TemplateURL* default_provider =
130 template_url_service->GetDefaultSearchProvider();
131 const bool can_send_current_url =
132 CanSendURL(input.current_url(), suggest_url, default_provider,
[email protected]e6477f12014-08-05 07:59:54133 current_page_classification_,
mpearson3d89cdc2017-03-03 21:15:45134 template_url_service->search_terms_data(), client());
135 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
136 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
137 if (!can_send_current_url) {
138 const bool can_send_ordinary_url =
139 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
140 current_page_classification_,
141 template_url_service->search_terms_data(), client());
142 eligibility = can_send_ordinary_url
143 ? ZeroSuggestEligibility::URL_INELIGIBLE
144 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
145 }
146 UMA_HISTOGRAM_ENUMERATION(
147 "Omnibox.ZeroSuggest.Eligible.OnFocus", static_cast<int>(eligibility),
148 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
149 if (can_send_current_url &&
mariakhomenkobfc3a2a2014-10-24 00:48:22150 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() &&
151 !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]162c8d9fa2014-03-18 20:25:41152 // Update suggest_url to include the current_page_url.
gcomanici9c1a2d82017-02-14 19:35:57153 if (OmniboxFieldTrial::InZeroSuggestRedirectToChromeFieldTrial()) {
154 url_string +=
155 "/url=" + net::EscapePath(current_query_) +
156 OmniboxFieldTrial::ZeroSuggestRedirectToChromeAdditionalFields();
157 suggest_url = GURL(url_string);
158 } else {
mpearson3d89cdc2017-03-03 21:15:45159 base::string16 prefix;
160 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
gcomanici9c1a2d82017-02-14 19:35:57161 search_term_args.current_page_url = current_query_;
162 suggest_url =
163 GURL(default_provider->suggestions_url_ref().ReplaceSearchTerms(
164 search_term_args, template_url_service->search_terms_data()));
165 }
mariakhomenkobfc3a2a2014-10-24 00:48:22166 } else if (!ShouldShowNonContextualZeroSuggest(suggest_url,
167 input.current_url())) {
[email protected]162c8d9fa2014-03-18 20:25:41168 return;
169 }
170
[email protected]6ce7f612012-09-05 23:53:07171 done_ = false;
[email protected]6ce7f612012-09-05 23:53:07172 // TODO(jered): Consider adding locally-sourced zero-suggestions here too.
173 // These may be useful on the NTP or more relevant to the user than server
174 // suggestions, if based on local browsing history.
[email protected]855ebff2014-05-09 07:14:38175 MaybeUseCachedSuggestions();
[email protected]9b9fa672013-11-07 06:04:52176 Run(suggest_url);
[email protected]6ce7f612012-09-05 23:53:07177}
178
mpearson8a37c382015-03-07 05:58:57179void ZeroSuggestProvider::Stop(bool clear_cached_results,
180 bool due_to_user_inactivity) {
[email protected]ec3f679b2014-08-18 07:45:13181 if (fetcher_)
182 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
183 fetcher_.reset();
mariakhomenkobfc3a2a2014-10-24 00:48:22184 waiting_for_most_visited_urls_request_ = false;
[email protected]ec3f679b2014-08-18 07:45:13185 done_ = true;
186
187 if (clear_cached_results) {
188 // We do not call Clear() on |results_| to retain |verbatim_relevance|
189 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18190 // beginning of the next call to Start() to determine the current url
191 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13192 results_.suggest_results.clear();
193 results_.navigation_results.clear();
194 current_query_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45195 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13196 }
197}
198
[email protected]855ebff2014-05-09 07:14:38199void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
200 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) {
201 // Remove the deleted match from the cache, so it is not shown to the user
202 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13203 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38204 std::string());
205 }
206 BaseSearchProvider::DeleteMatch(match);
207}
208
[email protected]ec3f679b2014-08-18 07:45:13209void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
210 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45211 if (!results_.suggest_results.empty() ||
212 !results_.navigation_results.empty() ||
213 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13214 provider_info->back().set_times_returned_results_in_session(1);
215}
216
[email protected]f030c4d2014-03-25 01:05:54217void ZeroSuggestProvider::ResetSession() {
218 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13219 // |field_trial_triggered_in_session| unchanged and set
220 // |field_trial_triggered| to false since zero suggest is inactive now.
221 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54222}
223
mpearson931028c2016-07-01 18:55:11224ZeroSuggestProvider::ZeroSuggestProvider(
225 AutocompleteProviderClient* client,
226 HistoryURLProvider* history_url_provider,
227 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13228 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
mpearson931028c2016-07-01 18:55:11229 history_url_provider_(history_url_provider),
[email protected]776ee5902014-08-11 09:15:19230 listener_(listener),
[email protected]855ebff2014-05-09 07:14:38231 results_from_cache_(false),
mariakhomenkobfc3a2a2014-10-24 00:48:22232 waiting_for_most_visited_urls_request_(false),
[email protected]8f064e52013-09-18 01:17:14233 weak_ptr_factory_(this) {
mpearson3d89cdc2017-03-03 21:15:45234 // Record whether contextual zero suggest is possible for this user / profile.
235 const TemplateURLService* template_url_service =
236 client->GetTemplateURLService();
237 // Template URL service can be null in tests.
238 if (template_url_service != nullptr) {
239 GURL suggest_url(GetContextualSuggestionsUrl());
240 // To check whether this is allowed, use an arbitrary insecure (http) URL
241 // as the URL we'd want suggestions for. The value of OTHER as the current
242 // page classification is to correspond with that URL.
243 UMA_HISTOGRAM_BOOLEAN(
244 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
245 suggest_url.is_valid() &&
246 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
247 template_url_service->GetDefaultSearchProvider(),
248 metrics::OmniboxEventProto::OTHER,
249 template_url_service->search_terms_data(), client));
250 }
[email protected]6ce7f612012-09-05 23:53:07251}
252
253ZeroSuggestProvider::~ZeroSuggestProvider() {
254}
255
[email protected]776ee5902014-08-11 09:15:19256const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
257 // Zero suggest provider should not receive keyword results.
258 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13259 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19260}
261
262const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18263 // The callers of this method won't look at the AutocompleteInput's
264 // |from_omnibox_focus| member, so we can set its value to false.
blundelld130d592015-06-21 19:29:13265 return AutocompleteInput(base::string16(), base::string16::npos,
266 std::string(), GURL(current_query_),
267 current_page_classification_, true, false, false,
268 true, false, client()->GetSchemeClassifier());
[email protected]776ee5902014-08-11 09:15:19269}
270
271bool ZeroSuggestProvider::ShouldAppendExtraParams(
272 const SearchSuggestionParser::SuggestResult& result) const {
273 // We always use the default provider for search, so append the params.
274 return true;
275}
276
[email protected]776ee5902014-08-11 09:15:19277void ZeroSuggestProvider::RecordDeletionResult(bool success) {
278 if (success) {
279 base::RecordAction(
280 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
281 } else {
282 base::RecordAction(
283 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
284 }
285}
286
287void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) {
288 DCHECK(!done_);
289 DCHECK_EQ(fetcher_.get(), source);
290
291 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
292
293 bool results_updated = false;
294 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) {
295 std::string json_data = SearchSuggestionParser::ExtractJsonData(source);
dcheng259570c2016-04-22 00:45:57296 std::unique_ptr<base::Value> data(
[email protected]776ee5902014-08-11 09:15:19297 SearchSuggestionParser::DeserializeJsonData(json_data));
298 if (data) {
299 if (StoreSuggestionResponse(json_data, *data))
300 return;
301 results_updated = ParseSuggestResults(
302 *data, kDefaultZeroSuggestRelevance, false, &results_);
303 }
304 }
305 fetcher_.reset();
306 done_ = true;
307 ConvertResultsToAutocompleteMatches();
308 listener_->OnProviderUpdate(results_updated);
309}
310
[email protected]855ebff2014-05-09 07:14:38311bool ZeroSuggestProvider::StoreSuggestionResponse(
312 const std::string& json_data,
313 const base::Value& parsed_data) {
314 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() ||
315 json_data.empty())
316 return false;
blundelld130d592015-06-21 19:29:13317 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
318 json_data);
[email protected]855ebff2014-05-09 07:14:38319
320 // If we received an empty result list, we should update the display, as it
321 // may be showing cached results that should not be shown.
322 const base::ListValue* root_list = NULL;
323 const base::ListValue* results_list = NULL;
324 if (parsed_data.GetAsList(&root_list) &&
325 root_list->GetList(1, &results_list) &&
326 results_list->empty())
327 return false;
328
329 // We are finished with the request and want to bail early.
330 if (results_from_cache_)
331 done_ = true;
332
333 return results_from_cache_;
334}
335
[email protected]bb1fb2b2013-05-31 00:21:01336void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37337 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30338 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22339 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11340 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01341}
342
[email protected]bb1fb2b2013-05-31 00:21:01343AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37344 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01345 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47346 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01347 match.destination_url = navigation.url();
348
[email protected]23db6492014-01-16 02:35:30349 // Zero suggest results should always omit protocols and never appear bold.
rsleevi24f64dc22015-08-07 21:39:21350 match.contents = url_formatter::FormatUrl(
jshin1fb76462016-04-05 22:13:03351 navigation.url(), url_formatter::kFormatUrlOmitAll,
rsleevi24f64dc22015-08-07 21:39:21352 net::UnescapeRule::SPACES, nullptr, nullptr, nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01353 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13354 AutocompleteInput::FormattedStringWithEquivalentMeaning(
355 navigation.url(), match.contents, client()->GetSchemeClassifier());
[email protected]bb1fb2b2013-05-31 00:21:01356
[email protected]b959d7d42013-12-13 17:26:37357 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01358 match.contents.length(), ACMatchClassification::URL,
359 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16360
361 match.description =
362 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37363 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16364 match.description.length(), ACMatchClassification::NONE,
365 &match.description_class);
[email protected]bb1fb2b2013-05-31 00:21:01366 return match;
367}
368
[email protected]9b9fa672013-11-07 06:04:52369void ZeroSuggestProvider::Run(const GURL& suggest_url) {
[email protected]8f064e52013-09-18 01:17:14370 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
371 most_visited_urls_.clear();
blundelld130d592015-06-21 19:29:13372 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
[email protected]8f064e52013-09-18 01:17:14373 if (ts) {
mariakhomenkobfc3a2a2014-10-24 00:48:22374 waiting_for_most_visited_urls_request_ = true;
[email protected]8f064e52013-09-18 01:17:14375 ts->GetMostVisitedURLs(
376 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
[email protected]ce767ab22013-11-12 03:50:09377 weak_ptr_factory_.GetWeakPtr()), false);
[email protected]8f064e52013-09-18 01:17:14378 }
mariakhomenkobfc3a2a2014-10-24 00:48:22379 } else {
380 const int kFetcherID = 1;
dtapuskadafcf892015-05-01 13:58:25381 fetcher_ = net::URLFetcher::Create(kFetcherID, suggest_url,
382 net::URLFetcher::GET, this);
amohammadkhanf76ae112015-09-14 17:34:43383 data_use_measurement::DataUseUserData::AttachToFetcher(
384 fetcher_.get(), data_use_measurement::DataUseUserData::OMNIBOX);
blundelld130d592015-06-21 19:29:13385 fetcher_->SetRequestContext(client()->GetRequestContext());
mariakhomenkobfc3a2a2014-10-24 00:48:22386 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
387 // Add Chrome experiment state to the request headers.
388 net::HttpRequestHeaders headers;
asvitkineb2ea4c92017-01-12 16:13:23389 // Note: It's OK to pass |is_signed_in| false if it's unknown, as it does
390 // not affect transmission of experiments coming from the variations server.
yutak3305f49d2016-12-13 10:32:31391 bool is_signed_in = false;
asvitkine9a279832015-12-18 02:35:50392 variations::AppendVariationHeaders(fetcher_->GetOriginalURL(),
393 client()->IsOffTheRecord(), false,
yutak3305f49d2016-12-13 10:32:31394 is_signed_in, &headers);
mariakhomenkobfc3a2a2014-10-24 00:48:22395 fetcher_->SetExtraRequestHeaders(headers.ToString());
396 fetcher_->Start();
397 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
[email protected]8f064e52013-09-18 01:17:14398 }
[email protected]bb1fb2b2013-05-31 00:21:01399}
400
[email protected]8f064e52013-09-18 01:17:14401void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
402 const history::MostVisitedURLList& urls) {
mariakhomenkobfc3a2a2014-10-24 00:48:22403 if (!waiting_for_most_visited_urls_request_) return;
[email protected]8f064e52013-09-18 01:17:14404 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22405 waiting_for_most_visited_urls_request_ = false;
406 done_ = true;
407 ConvertResultsToAutocompleteMatches();
408 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14409}
410
[email protected]9c97f89c2013-06-25 03:12:16411void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01412 matches_.clear();
413
blundelld130d592015-06-21 19:29:13414 TemplateURLService* template_url_service = client()->GetTemplateURLService();
[email protected]bb1fb2b2013-05-31 00:21:01415 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13416 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07417 // Fail if we can't set the clickthrough URL for query suggestions.
blundelld130d592015-06-21 19:29:13418 if (default_provider == NULL ||
419 !default_provider->SupportsReplacement(
420 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07421 return;
[email protected]6ce7f612012-09-05 23:53:07422
[email protected]00404742014-02-20 13:09:05423 MatchMap map;
424 AddSuggestResultsToMap(results_.suggest_results, &map);
425
426 const int num_query_results = map.size();
427 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01428 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16429 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
[email protected]78981d8c2014-05-09 15:05:47430 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
[email protected]9c97f89c2013-06-25 03:12:16431 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01432
[email protected]8f064e52013-09-18 01:17:14433 // Show Most Visited results after ZeroSuggest response is received.
434 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) {
[email protected]3feb8b002013-10-14 23:50:13435 if (!current_url_match_.destination_url.is_valid())
436 return;
[email protected]8f064e52013-09-18 01:17:14437 matches_.push_back(current_url_match_);
438 int relevance = 600;
439 if (num_results > 0) {
440 UMA_HISTOGRAM_COUNTS(
441 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
442 most_visited_urls_.size());
443 }
[email protected]23db6492014-01-16 02:35:30444 const base::string16 current_query_string16(
445 base::ASCIIToUTF16(current_query_));
[email protected]8f064e52013-09-18 01:17:14446 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
447 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37448 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13449 client()->GetSchemeClassifier(), url.url,
[email protected]7720fc32014-07-09 06:10:05450 AutocompleteMatchType::NAVSUGGEST, url.title, std::string(), false,
jshin1fb76462016-04-05 22:13:03451 relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14452 matches_.push_back(NavigationToMatch(nav));
453 --relevance;
454 }
455 return;
456 }
457
[email protected]9c97f89c2013-06-25 03:12:16458 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01459 return;
460
461 // TODO(jered): Rip this out once the first match is decoupled from the
462 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01463 matches_.push_back(current_url_match_);
464
[email protected]00404742014-02-20 13:09:05465 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01466 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01467
[email protected]0b9575f2014-07-30 11:58:37468 const SearchSuggestionParser::NavigationResults& nav_results(
469 results_.navigation_results);
470 for (SearchSuggestionParser::NavigationResults::const_iterator it(
471 nav_results.begin()); it != nav_results.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01472 matches_.push_back(NavigationToMatch(*it));
[email protected]6ce7f612012-09-05 23:53:07473}
474
[email protected]bb1fb2b2013-05-31 00:21:01475AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]bb1fb2b2013-05-31 00:21:01476 // The placeholder suggestion for the current URL has high relevance so
477 // that it is in the first suggestion slot and inline autocompleted. It
478 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11479 AutocompleteInput tmp(GetInput(false));
480 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
481 return VerbatimMatchForURL(client(), tmp, GURL(current_query_),
482 history_url_provider_,
sdefresne70948d62015-08-11 10:46:35483 results_.verbatim_relevance);
[email protected]00404742014-02-20 13:09:05484}
[email protected]162c8d9fa2014-03-18 20:25:41485
mariakhomenkobfc3a2a2014-10-24 00:48:22486bool ZeroSuggestProvider::ShouldShowNonContextualZeroSuggest(
[email protected]162c8d9fa2014-03-18 20:25:41487 const GURL& suggest_url,
488 const GURL& current_page_url) const {
blundelld130d592015-06-21 19:29:13489 const TemplateURLService* template_url_service =
490 client()->GetTemplateURLService();
[email protected]162c8d9fa2014-03-18 20:25:41491 if (!ZeroSuggestEnabled(suggest_url,
blundelld130d592015-06-21 19:29:13492 template_url_service->GetDefaultSearchProvider(),
[email protected]e6477f12014-08-05 07:59:54493 current_page_classification_,
blundelld130d592015-06-21 19:29:13494 template_url_service->search_terms_data(), client()))
[email protected]162c8d9fa2014-03-18 20:25:41495 return false;
496
497 // If we cannot send URLs, then only the MostVisited and Personalized
498 // variations can be shown.
499 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() &&
500 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
501 return false;
502
503 // Only show zero suggest for HTTP[S] pages.
504 // TODO(mariakhomenko): We may be able to expand this set to include pages
505 // with other schemes (e.g. chrome://). That may require improvements to
506 // the formatting of the verbatim result returned by MatchForCurrentURL().
507 if (!current_page_url.is_valid() ||
[email protected]e8ca69c2014-05-07 15:31:19508 ((current_page_url.scheme() != url::kHttpScheme) &&
509 (current_page_url.scheme() != url::kHttpsScheme)))
[email protected]162c8d9fa2014-03-18 20:25:41510 return false;
511
mariakhomenkobfc3a2a2014-10-24 00:48:22512 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() &&
blundelld130d592015-06-21 19:29:13513 client()
514 ->GetTemplateURLService()
515 ->IsSearchResultsPageFromDefaultSearchProvider(current_page_url))
mariakhomenkobfc3a2a2014-10-24 00:48:22516 return false;
517
[email protected]162c8d9fa2014-03-18 20:25:41518 return true;
519}
[email protected]855ebff2014-05-09 07:14:38520
mpearson3d89cdc2017-03-03 21:15:45521std::string ZeroSuggestProvider::GetContextualSuggestionsUrl() const {
522 // Without a default search provider, refuse to do anything (even if the user
523 // is in the redirect-to-chrome field trial).
524 const TemplateURLService* template_url_service =
525 client()->GetTemplateURLService();
526 const TemplateURL* default_provider =
527 template_url_service->GetDefaultSearchProvider();
528 if (default_provider == nullptr)
529 return std::string();
530
531 if (OmniboxFieldTrial::InZeroSuggestRedirectToChromeFieldTrial())
532 return OmniboxFieldTrial::ZeroSuggestRedirectToChromeServerAddress();
533 base::string16 prefix;
534 TemplateURLRef::SearchTermsArgs search_term_args(prefix);
535 return default_provider->suggestions_url_ref().ReplaceSearchTerms(
536 search_term_args, template_url_service->search_terms_data());
537}
538
[email protected]855ebff2014-05-09 07:14:38539void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
540 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
541 return;
542
blundelld130d592015-06-21 19:29:13543 std::string json_data =
544 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38545 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57546 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14547 SearchSuggestionParser::DeserializeJsonData(json_data));
[email protected]776ee5902014-08-11 09:15:19548 if (data && ParseSuggestResults(
549 *data, kDefaultZeroSuggestRelevance, false, &results_)) {
[email protected]855ebff2014-05-09 07:14:38550 ConvertResultsToAutocompleteMatches();
551 results_from_cache_ = !matches_.empty();
552 }
553 }
554}