blob: 122739a13df3b321eb483f7a62fdaf5d9d21d7d4 [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"
gcomanici8cabc77f2017-04-27 20:04:5410#include "base/feature_list.h"
[email protected]bb1fb2b2013-05-31 00:21:0111#include "base/i18n/case_conversion.h"
[email protected]6ce7f612012-09-05 23:53:0712#include "base/json/json_string_value_serializer.h"
asvitkine30330812016-08-30 04:01:0813#include "base/metrics/histogram_macros.h"
[email protected]f7f41c0e2014-08-11 04:22:2314#include "base/metrics/user_metrics.h"
[email protected]98570e12013-06-10 19:54:2215#include "base/strings/string16.h"
16#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2017#include "base/strings/utf_string_conversions.h"
[email protected]4dcb7972013-06-28 15:15:4118#include "base/time/time.h"
a-v-ydd768d52016-03-25 21:07:4619#include "base/trace_event/trace_event.h"
amohammadkhanf76ae112015-09-14 17:34:4320#include "components/data_use_measurement/core/data_use_user_data.h"
sdefresnebc766ef2014-09-25 09:28:1321#include "components/history/core/browser/history_types.h"
sdefresne0da3bc02015-01-29 18:26:3522#include "components/history/core/browser/top_sites.h"
blundell2102f7c2015-07-09 10:00:5323#include "components/omnibox/browser/autocomplete_classifier.h"
24#include "components/omnibox/browser/autocomplete_input.h"
25#include "components/omnibox/browser/autocomplete_match.h"
26#include "components/omnibox/browser/autocomplete_provider_listener.h"
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:1127#include "components/omnibox/browser/contextual_suggestions_service.h"
blundell2102f7c2015-07-09 10:00:5328#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/url_request/url_fetcher.h"
40#include "net/url_request/url_request_status.h"
Steven Holtef9d5ed62017-10-21 02:02:3041#include "third_party/metrics_proto/omnibox_event.pb.h"
42#include "third_party/metrics_proto/omnibox_input_type.pb.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();
mpearson6eaf7fc2017-04-11 04:09:57107 if (!input.from_omnibox_focus() || client()->IsOffTheRecord() ||
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();
gcomanici8cabc77f2017-04-27 20:04:54117 current_title_ = input.current_title();
[email protected]f030c4d2014-03-25 01:05:54118 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52119 current_url_match_ = MatchForCurrentURL();
120
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11121 GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl(
122 /*current_url=*/"", client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41123 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07124 return;
[email protected]162c8d9fa2014-03-18 20:25:41125
mariakhomenkobfc3a2a2014-10-24 00:48:22126 // No need to send the current page URL in personalized suggest or
127 // most visited field trials.
mpearson3d89cdc2017-03-03 21:15:45128 const TemplateURLService* template_url_service =
129 client()->GetTemplateURLService();
130 const TemplateURL* default_provider =
131 template_url_service->GetDefaultSearchProvider();
132 const bool can_send_current_url =
133 CanSendURL(input.current_url(), suggest_url, default_provider,
[email protected]e6477f12014-08-05 07:59:54134 current_page_classification_,
mpearson3d89cdc2017-03-03 21:15:45135 template_url_service->search_terms_data(), client());
136 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
137 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
138 if (!can_send_current_url) {
139 const bool can_send_ordinary_url =
140 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
141 current_page_classification_,
142 template_url_service->search_terms_data(), client());
143 eligibility = can_send_ordinary_url
144 ? ZeroSuggestEligibility::URL_INELIGIBLE
145 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
146 }
147 UMA_HISTOGRAM_ENUMERATION(
148 "Omnibox.ZeroSuggest.Eligible.OnFocus", static_cast<int>(eligibility),
149 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11150
151 bool can_attach_current_url =
152 can_send_current_url &&
Theresa Wellingtondd71c5c2017-10-02 15:50:38153 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
154 client()->GetPrefs()) &&
155 !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial(
156 client()->GetPrefs());
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11157
158 if (!can_attach_current_url &&
159 !ShouldShowNonContextualZeroSuggest(input.current_url())) {
[email protected]162c8d9fa2014-03-18 20:25:41160 return;
161 }
162
[email protected]6ce7f612012-09-05 23:53:07163 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11164
[email protected]6ce7f612012-09-05 23:53:07165 // TODO(jered): Consider adding locally-sourced zero-suggestions here too.
166 // These may be useful on the NTP or more relevant to the user than server
167 // suggestions, if based on local browsing history.
[email protected]855ebff2014-05-09 07:14:38168 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11169
Theresa Wellingtondd71c5c2017-10-02 15:50:38170 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial(
171 client()->GetPrefs())) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11172 most_visited_urls_.clear();
173 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
174 if (ts) {
175 waiting_for_most_visited_urls_request_ = true;
176 ts->GetMostVisitedURLs(
177 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
178 weak_ptr_factory_.GetWeakPtr()),
179 false);
180 }
181 return;
182 }
183
184 // Create a request for suggestions with |this| as the fetcher delegate.
185 client()
Gheorghe Comanici86bbdf62017-08-28 17:20:33186 ->GetContextualSuggestionsService(/*create_if_necessary=*/true)
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11187 ->CreateContextualSuggestionsRequest(
188 can_attach_current_url ? current_query_ : std::string(),
189 client()->GetTemplateURLService(),
190 /*fetcher_delegate=*/this,
191 base::BindOnce(
192 &ZeroSuggestProvider::OnContextualSuggestionsFetcherAvailable,
193 weak_ptr_factory_.GetWeakPtr()));
[email protected]6ce7f612012-09-05 23:53:07194}
195
mpearson8a37c382015-03-07 05:58:57196void ZeroSuggestProvider::Stop(bool clear_cached_results,
197 bool due_to_user_inactivity) {
[email protected]ec3f679b2014-08-18 07:45:13198 if (fetcher_)
199 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
200 fetcher_.reset();
mariakhomenkobfc3a2a2014-10-24 00:48:22201 waiting_for_most_visited_urls_request_ = false;
Gheorghe Comanici86bbdf62017-08-28 17:20:33202 auto* contextual_suggestions_service =
203 client()->GetContextualSuggestionsService(/*create_if_necessary=*/false);
204 // contextual_suggestions_service can be null if in incognito mode.
205 if (contextual_suggestions_service != nullptr) {
206 contextual_suggestions_service->StopCreatingContextualSuggestionsRequest();
207 }
[email protected]ec3f679b2014-08-18 07:45:13208 done_ = true;
209
210 if (clear_cached_results) {
211 // We do not call Clear() on |results_| to retain |verbatim_relevance|
212 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18213 // beginning of the next call to Start() to determine the current url
214 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13215 results_.suggest_results.clear();
216 results_.navigation_results.clear();
217 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54218 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45219 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13220 }
221}
222
[email protected]855ebff2014-05-09 07:14:38223void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Theresa Wellingtondd71c5c2017-10-02 15:50:38224 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
225 client()->GetPrefs())) {
[email protected]855ebff2014-05-09 07:14:38226 // Remove the deleted match from the cache, so it is not shown to the user
227 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13228 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38229 std::string());
230 }
231 BaseSearchProvider::DeleteMatch(match);
232}
233
[email protected]ec3f679b2014-08-18 07:45:13234void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
235 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45236 if (!results_.suggest_results.empty() ||
237 !results_.navigation_results.empty() ||
238 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13239 provider_info->back().set_times_returned_results_in_session(1);
240}
241
[email protected]f030c4d2014-03-25 01:05:54242void ZeroSuggestProvider::ResetSession() {
243 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13244 // |field_trial_triggered_in_session| unchanged and set
245 // |field_trial_triggered| to false since zero suggest is inactive now.
246 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54247}
248
mpearson931028c2016-07-01 18:55:11249ZeroSuggestProvider::ZeroSuggestProvider(
250 AutocompleteProviderClient* client,
251 HistoryURLProvider* history_url_provider,
252 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13253 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
mpearson931028c2016-07-01 18:55:11254 history_url_provider_(history_url_provider),
[email protected]776ee5902014-08-11 09:15:19255 listener_(listener),
[email protected]855ebff2014-05-09 07:14:38256 results_from_cache_(false),
mariakhomenkobfc3a2a2014-10-24 00:48:22257 waiting_for_most_visited_urls_request_(false),
[email protected]8f064e52013-09-18 01:17:14258 weak_ptr_factory_(this) {
mpearson3d89cdc2017-03-03 21:15:45259 // Record whether contextual zero suggest is possible for this user / profile.
260 const TemplateURLService* template_url_service =
261 client->GetTemplateURLService();
262 // Template URL service can be null in tests.
263 if (template_url_service != nullptr) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11264 GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl(
265 /*current_url=*/"", template_url_service);
mpearson3d89cdc2017-03-03 21:15:45266 // To check whether this is allowed, use an arbitrary insecure (http) URL
267 // as the URL we'd want suggestions for. The value of OTHER as the current
268 // page classification is to correspond with that URL.
269 UMA_HISTOGRAM_BOOLEAN(
270 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
271 suggest_url.is_valid() &&
272 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
273 template_url_service->GetDefaultSearchProvider(),
274 metrics::OmniboxEventProto::OTHER,
275 template_url_service->search_terms_data(), client));
276 }
[email protected]6ce7f612012-09-05 23:53:07277}
278
279ZeroSuggestProvider::~ZeroSuggestProvider() {
280}
281
[email protected]776ee5902014-08-11 09:15:19282const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
283 // Zero suggest provider should not receive keyword results.
284 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13285 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19286}
287
288const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18289 // The callers of this method won't look at the AutocompleteInput's
290 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02291 AutocompleteInput input(base::string16(), current_page_classification_,
292 client()->GetSchemeClassifier());
293 input.set_current_url(GURL(current_query_));
294 input.set_current_title(current_title_);
295 input.set_prevent_inline_autocomplete(true);
296 input.set_allow_exact_keyword_match(false);
297 return input;
[email protected]776ee5902014-08-11 09:15:19298}
299
300bool ZeroSuggestProvider::ShouldAppendExtraParams(
301 const SearchSuggestionParser::SuggestResult& result) const {
302 // We always use the default provider for search, so append the params.
303 return true;
304}
305
[email protected]776ee5902014-08-11 09:15:19306void ZeroSuggestProvider::RecordDeletionResult(bool success) {
307 if (success) {
308 base::RecordAction(
309 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
310 } else {
311 base::RecordAction(
312 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
313 }
314}
315
316void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) {
317 DCHECK(!done_);
318 DCHECK_EQ(fetcher_.get(), source);
319
320 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
321
322 bool results_updated = false;
323 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) {
324 std::string json_data = SearchSuggestionParser::ExtractJsonData(source);
dcheng259570c2016-04-22 00:45:57325 std::unique_ptr<base::Value> data(
[email protected]776ee5902014-08-11 09:15:19326 SearchSuggestionParser::DeserializeJsonData(json_data));
327 if (data) {
328 if (StoreSuggestionResponse(json_data, *data))
329 return;
330 results_updated = ParseSuggestResults(
331 *data, kDefaultZeroSuggestRelevance, false, &results_);
332 }
333 }
334 fetcher_.reset();
335 done_ = true;
336 ConvertResultsToAutocompleteMatches();
337 listener_->OnProviderUpdate(results_updated);
338}
339
[email protected]855ebff2014-05-09 07:14:38340bool ZeroSuggestProvider::StoreSuggestionResponse(
341 const std::string& json_data,
342 const base::Value& parsed_data) {
Theresa Wellingtondd71c5c2017-10-02 15:50:38343 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
344 client()->GetPrefs()) ||
[email protected]855ebff2014-05-09 07:14:38345 json_data.empty())
346 return false;
blundelld130d592015-06-21 19:29:13347 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
348 json_data);
[email protected]855ebff2014-05-09 07:14:38349
350 // If we received an empty result list, we should update the display, as it
351 // may be showing cached results that should not be shown.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24352 const base::ListValue* root_list = nullptr;
353 const base::ListValue* results_list = nullptr;
[email protected]855ebff2014-05-09 07:14:38354 if (parsed_data.GetAsList(&root_list) &&
355 root_list->GetList(1, &results_list) &&
356 results_list->empty())
357 return false;
358
359 // We are finished with the request and want to bail early.
360 if (results_from_cache_)
361 done_ = true;
362
363 return results_from_cache_;
364}
365
[email protected]bb1fb2b2013-05-31 00:21:01366void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37367 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30368 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22369 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11370 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01371}
372
[email protected]bb1fb2b2013-05-31 00:21:01373AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37374 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01375 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47376 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01377 match.destination_url = navigation.url();
378
[email protected]23db6492014-01-16 02:35:30379 // Zero suggest results should always omit protocols and never appear bold.
Tommy C. Lia46e6382017-08-01 23:26:27380 auto format_types = AutocompleteMatch::GetFormatTypes(false, false, false);
tommycli72014f62017-06-29 21:42:16381 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
382 net::UnescapeRule::SPACES, nullptr,
383 nullptr, nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01384 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13385 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26386 navigation.url(), url_formatter::FormatUrl(navigation.url()),
387 client()->GetSchemeClassifier());
[email protected]bb1fb2b2013-05-31 00:21:01388
[email protected]b959d7d42013-12-13 17:26:37389 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01390 match.contents.length(), ACMatchClassification::URL,
391 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16392
393 match.description =
394 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37395 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16396 match.description.length(), ACMatchClassification::NONE,
397 &match.description_class);
gcomanici67d53ac2017-04-01 17:07:19398 match.subtype_identifier = navigation.subtype_identifier();
[email protected]bb1fb2b2013-05-31 00:21:01399 return match;
400}
401
[email protected]8f064e52013-09-18 01:17:14402void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
403 const history::MostVisitedURLList& urls) {
mariakhomenkobfc3a2a2014-10-24 00:48:22404 if (!waiting_for_most_visited_urls_request_) return;
[email protected]8f064e52013-09-18 01:17:14405 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22406 waiting_for_most_visited_urls_request_ = false;
407 done_ = true;
408 ConvertResultsToAutocompleteMatches();
409 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14410}
411
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11412void ZeroSuggestProvider::OnContextualSuggestionsFetcherAvailable(
413 std::unique_ptr<net::URLFetcher> fetcher) {
414 fetcher_ = std::move(fetcher);
415 fetcher_->Start();
416 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
417}
418
[email protected]9c97f89c2013-06-25 03:12:16419void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01420 matches_.clear();
421
blundelld130d592015-06-21 19:29:13422 TemplateURLService* template_url_service = client()->GetTemplateURLService();
[email protected]bb1fb2b2013-05-31 00:21:01423 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13424 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07425 // Fail if we can't set the clickthrough URL for query suggestions.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24426 if (default_provider == nullptr ||
blundelld130d592015-06-21 19:29:13427 !default_provider->SupportsReplacement(
428 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07429 return;
[email protected]6ce7f612012-09-05 23:53:07430
[email protected]00404742014-02-20 13:09:05431 MatchMap map;
432 AddSuggestResultsToMap(results_.suggest_results, &map);
433
434 const int num_query_results = map.size();
435 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01436 const int num_results = num_query_results + num_nav_results;
[email protected]9c97f89c2013-06-25 03:12:16437 UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results);
[email protected]78981d8c2014-05-09 15:05:47438 UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results);
[email protected]9c97f89c2013-06-25 03:12:16439 UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01440
[email protected]8f064e52013-09-18 01:17:14441 // Show Most Visited results after ZeroSuggest response is received.
Theresa Wellingtondd71c5c2017-10-02 15:50:38442 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial(
443 client()->GetPrefs())) {
[email protected]3feb8b002013-10-14 23:50:13444 if (!current_url_match_.destination_url.is_valid())
445 return;
[email protected]8f064e52013-09-18 01:17:14446 matches_.push_back(current_url_match_);
447 int relevance = 600;
448 if (num_results > 0) {
449 UMA_HISTOGRAM_COUNTS(
450 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
451 most_visited_urls_.size());
452 }
[email protected]23db6492014-01-16 02:35:30453 const base::string16 current_query_string16(
454 base::ASCIIToUTF16(current_query_));
[email protected]8f064e52013-09-18 01:17:14455 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
456 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37457 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13458 client()->GetSchemeClassifier(), url.url,
gcomanici67d53ac2017-04-01 17:07:19459 AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false,
jshin1fb76462016-04-05 22:13:03460 relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14461 matches_.push_back(NavigationToMatch(nav));
462 --relevance;
463 }
464 return;
465 }
466
[email protected]9c97f89c2013-06-25 03:12:16467 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01468 return;
469
470 // TODO(jered): Rip this out once the first match is decoupled from the
471 // current typing in the omnibox.
[email protected]bb1fb2b2013-05-31 00:21:01472 matches_.push_back(current_url_match_);
473
[email protected]00404742014-02-20 13:09:05474 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01475 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01476
[email protected]0b9575f2014-07-30 11:58:37477 const SearchSuggestionParser::NavigationResults& nav_results(
478 results_.navigation_results);
479 for (SearchSuggestionParser::NavigationResults::const_iterator it(
gcomanici67d53ac2017-04-01 17:07:19480 nav_results.begin());
481 it != nav_results.end(); ++it) {
[email protected]bb1fb2b2013-05-31 00:21:01482 matches_.push_back(NavigationToMatch(*it));
gcomanici67d53ac2017-04-01 17:07:19483 }
[email protected]6ce7f612012-09-05 23:53:07484}
485
[email protected]bb1fb2b2013-05-31 00:21:01486AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]bb1fb2b2013-05-31 00:21:01487 // The placeholder suggestion for the current URL has high relevance so
488 // that it is in the first suggestion slot and inline autocompleted. It
489 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11490 AutocompleteInput tmp(GetInput(false));
491 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54492 const base::string16 description =
493 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
494 ? current_title_
495 : base::string16();
496 return VerbatimMatchForURL(client(), tmp, GURL(current_query_), description,
mpearson931028c2016-07-01 18:55:11497 history_url_provider_,
sdefresne70948d62015-08-11 10:46:35498 results_.verbatim_relevance);
[email protected]00404742014-02-20 13:09:05499}
[email protected]162c8d9fa2014-03-18 20:25:41500
mariakhomenkobfc3a2a2014-10-24 00:48:22501bool ZeroSuggestProvider::ShouldShowNonContextualZeroSuggest(
[email protected]162c8d9fa2014-03-18 20:25:41502 const GURL& current_page_url) const {
gcomanicide29a4362017-06-02 15:44:08503 if (!ZeroSuggestEnabled(current_page_classification_, client()))
[email protected]162c8d9fa2014-03-18 20:25:41504 return false;
505
506 // If we cannot send URLs, then only the MostVisited and Personalized
507 // variations can be shown.
Theresa Wellingtondd71c5c2017-10-02 15:50:38508 // The const-cast allows the non-const AutocompleteProviderClient::GetPrefs()
509 // function to be called. OmniboxFieldTrial does not modify prefs, so the
510 // cast is safe in this application.
511 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial(
512 const_cast<AutocompleteProviderClient*>(client())->GetPrefs()) &&
513 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
514 const_cast<AutocompleteProviderClient*>(client())->GetPrefs()))
[email protected]162c8d9fa2014-03-18 20:25:41515 return false;
516
517 // Only show zero suggest for HTTP[S] pages.
518 // TODO(mariakhomenko): We may be able to expand this set to include pages
519 // with other schemes (e.g. chrome://). That may require improvements to
520 // the formatting of the verbatim result returned by MatchForCurrentURL().
521 if (!current_page_url.is_valid() ||
[email protected]e8ca69c2014-05-07 15:31:19522 ((current_page_url.scheme() != url::kHttpScheme) &&
523 (current_page_url.scheme() != url::kHttpsScheme)))
[email protected]162c8d9fa2014-03-18 20:25:41524 return false;
525
Theresa Wellingtondd71c5c2017-10-02 15:50:38526 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial(
527 const_cast<AutocompleteProviderClient*>(client())->GetPrefs()) &&
blundelld130d592015-06-21 19:29:13528 client()
529 ->GetTemplateURLService()
530 ->IsSearchResultsPageFromDefaultSearchProvider(current_page_url))
mariakhomenkobfc3a2a2014-10-24 00:48:22531 return false;
532
[email protected]162c8d9fa2014-03-18 20:25:41533 return true;
534}
[email protected]855ebff2014-05-09 07:14:38535
536void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Theresa Wellingtondd71c5c2017-10-02 15:50:38537 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
538 client()->GetPrefs()))
[email protected]855ebff2014-05-09 07:14:38539 return;
540
blundelld130d592015-06-21 19:29:13541 std::string json_data =
542 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38543 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57544 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14545 SearchSuggestionParser::DeserializeJsonData(json_data));
[email protected]776ee5902014-08-11 09:15:19546 if (data && ParseSuggestResults(
547 *data, kDefaultZeroSuggestRelevance, false, &results_)) {
[email protected]855ebff2014-05-09 07:14:38548 ConvertResultsToAutocompleteMatches();
549 results_from_cache_ = !matches_.empty();
550 }
551 }
552}