blob: 22930fa820075f218f10b39ada31e77d250af45c [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
Kevin Bailey1e2a90e2017-10-27 21:02:059#include <string>
10#include <utility>
11
Sebastien Marchand53801a32019-01-25 16:26:1112#include "base/bind.h"
[email protected]6ce7f612012-09-05 23:53:0713#include "base/callback.h"
gcomanici8cabc77f2017-04-27 20:04:5414#include "base/feature_list.h"
[email protected]bb1fb2b2013-05-31 00:21:0115#include "base/i18n/case_conversion.h"
[email protected]6ce7f612012-09-05 23:53:0716#include "base/json/json_string_value_serializer.h"
asvitkine30330812016-08-30 04:01:0817#include "base/metrics/histogram_macros.h"
[email protected]f7f41c0e2014-08-11 04:22:2318#include "base/metrics/user_metrics.h"
Moe Ahmadi0993aec2019-09-19 21:42:3019#include "base/stl_util.h"
[email protected]98570e12013-06-10 19:54:2220#include "base/strings/string16.h"
21#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2022#include "base/strings/utf_string_conversions.h"
[email protected]4dcb7972013-06-28 15:15:4123#include "base/time/time.h"
a-v-ydd768d52016-03-25 21:07:4624#include "base/trace_event/trace_event.h"
Tommy C. Li0af09562019-06-11 23:58:5125#include "build/build_config.h"
sdefresnebc766ef2014-09-25 09:28:1326#include "components/history/core/browser/history_types.h"
sdefresne0da3bc02015-01-29 18:26:3527#include "components/history/core/browser/top_sites.h"
blundell2102f7c2015-07-09 10:00:5328#include "components/omnibox/browser/autocomplete_classifier.h"
29#include "components/omnibox/browser/autocomplete_input.h"
30#include "components/omnibox/browser/autocomplete_match.h"
manuk3972b2f2019-04-19 14:22:5631#include "components/omnibox/browser/autocomplete_match_classification.h"
Tommy Liec99def2019-10-14 22:37:2332#include "components/omnibox/browser/autocomplete_provider_client.h"
blundell2102f7c2015-07-09 10:00:5333#include "components/omnibox/browser/autocomplete_provider_listener.h"
Tommy Liec99def2019-10-14 22:37:2334#include "components/omnibox/browser/omnibox_field_trial.h"
Moe Ahmadi1266de82020-04-10 17:19:5335#include "components/omnibox/browser/omnibox_prefs.h"
Tommy C. Li06ec26a2019-06-10 18:01:4236#include "components/omnibox/browser/remote_suggestions_service.h"
blundell2102f7c2015-07-09 10:00:5337#include "components/omnibox/browser/search_provider.h"
Tommy Li823509e2019-12-06 22:42:4738#include "components/omnibox/browser/search_suggestion_parser.h"
sdefresne70948d62015-08-11 10:46:3539#include "components/omnibox/browser/verbatim_match.h"
Tomasz Wiszkowskid938a1112019-03-06 18:01:5740#include "components/omnibox/common/omnibox_features.h"
[email protected]f0c8c4992014-05-15 17:37:2641#include "components/pref_registry/pref_registry_syncable.h"
brettwf00b9b42016-02-01 22:11:3842#include "components/prefs/pref_service.h"
Tommy C. Li17b5ba112020-08-11 02:29:2143#include "components/search_engines/omnibox_focus_type.h"
Gheorghe Comanici864725b2017-11-28 21:48:0144#include "components/search_engines/search_engine_type.h"
[email protected]bf5c532d2014-07-05 00:29:5345#include "components/search_engines/template_url_service.h"
rsleevi24f64dc22015-08-07 21:39:2146#include "components/url_formatter/url_formatter.h"
asvitkine9a279832015-12-18 02:35:5047#include "components/variations/net/variations_http_headers.h"
[email protected]bb1fb2b2013-05-31 00:21:0148#include "net/base/escape.h"
Maks Orlovich1b208512018-06-13 21:08:1749#include "services/network/public/cpp/shared_url_loader_factory.h"
50#include "services/network/public/cpp/simple_url_loader.h"
Steven Holtef9d5ed62017-10-21 02:02:3051#include "third_party/metrics_proto/omnibox_event.pb.h"
52#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]761fa4702013-07-02 15:25:1553#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0754
Tommy C. Li0af09562019-06-11 23:58:5155using metrics::OmniboxEventProto;
56
[email protected]6ce7f612012-09-05 23:53:0757namespace {
[email protected]bb1fb2b2013-05-31 00:21:0158
mpearson3d89cdc2017-03-03 21:15:4559// Represents whether ZeroSuggestProvider is allowed to display contextual
60// suggestions on focus, and if not, why not.
61// These values are written to logs. New enum values can be added, but existing
62// enums must never be renumbered or deleted and reused.
63enum class ZeroSuggestEligibility {
64 ELIGIBLE = 0,
65 // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL
66 // itself prevents ZeroSuggest from triggering.
67 URL_INELIGIBLE = 1,
68 GENERALLY_INELIGIBLE = 2,
69 ELIGIBLE_MAX_VALUE
70};
71
Tomasz Wiszkowskifd790c02019-08-29 17:31:2972// Histogram values describing client eligibility to receive RemoteNoURL
73// suggestions on NTP.
74// These values are persisted to logs. New values can be added, but existing
75// enums must never be renumbered or deleted and reused.
76enum class ZeroSuggestEligibilityForRemoteNoURL {
77 kEligible = 0,
78 kIneligibleUserOffTheRecord = 1,
79 kIneligibleSuggestionsDisabled = 2,
80 kIneligibleUserNotAuthenticated = 3,
81 // Used to report users ineligible for RemoteNoURL suggestions when the
82 // search and suggest server of their choice cannot be used to offer the
83 // RemoteNoURL suggestions.
84 kIneligibleWithUserSelectedServer = 4,
85 kIneligibleUserNotParticipating = 5,
86
87 kMaxValue = kIneligibleUserNotParticipating
88};
89
[email protected]bb1fb2b2013-05-31 00:21:0190// TODO(hfung): The histogram code was copied and modified from
91// search_provider.cc. Refactor and consolidate the code.
92// We keep track in a histogram how many suggest requests we send, how
93// many suggest requests we invalidate (e.g., due to a user typing
94// another character), and how many replies we receive.
mpearson3d89cdc2017-03-03 21:15:4595// These values are written to logs. New enum values can be added, but existing
96// enums must never be renumbered or deleted and reused.
[email protected]bb1fb2b2013-05-31 00:21:0197enum ZeroSuggestRequestsHistogramValue {
98 ZERO_SUGGEST_REQUEST_SENT = 1,
mpearson3d89cdc2017-03-03 21:15:4599 ZERO_SUGGEST_REQUEST_INVALIDATED = 2,
100 ZERO_SUGGEST_REPLY_RECEIVED = 3,
[email protected]bb1fb2b2013-05-31 00:21:01101 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
102};
103
104void LogOmniboxZeroSuggestRequest(
105 ZeroSuggestRequestsHistogramValue request_value) {
106 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
107 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
108}
109
Tomasz Wiszkowskifd790c02019-08-29 17:31:29110// Record user eligibility for RemoteNoUrl suggestions for supplied page class.
111// The |histogram_variant| is used to specify particular variant of the
112// Omnibox.ZeroSuggest.Eligible.RemoteNoUrl histogram that should be updated.
113void LogOmniboxRemoteNoUrlEligibilityOnNTP(
114 OmniboxEventProto::PageClassification page_class,
115 bool log_for_profile_open,
116 AutocompleteProviderClient* client) {
117 ZeroSuggestEligibilityForRemoteNoURL value =
118 ZeroSuggestEligibilityForRemoteNoURL::kEligible;
119
120 auto* service = client->GetTemplateURLService();
121 auto* provider = service ? service->GetDefaultSearchProvider() : nullptr;
122 auto engine = provider ? provider->GetEngineType(service->search_terms_data())
123 : SEARCH_ENGINE_UNKNOWN;
Moe Ahmadi0993aec2019-09-19 21:42:30124 const auto variants = OmniboxFieldTrial::GetZeroSuggestVariants(page_class);
Tomasz Wiszkowskifd790c02019-08-29 17:31:29125
Moe Ahmadi0993aec2019-09-19 21:42:30126 if (!base::Contains(variants, ZeroSuggestProvider::kRemoteNoUrlVariant)) {
Tomasz Wiszkowskifd790c02019-08-29 17:31:29127 value =
128 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserNotParticipating;
129 } else if (client->IsOffTheRecord()) {
130 value = ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserOffTheRecord;
131 } else if (!client->SearchSuggestEnabled()) {
132 value =
133 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleSuggestionsDisabled;
134 } else if (!client->IsAuthenticated()) {
135 value =
136 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserNotAuthenticated;
137 } else if (service == nullptr || provider == nullptr ||
138 engine != SEARCH_ENGINE_GOOGLE) {
139 value =
140 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleWithUserSelectedServer;
141 }
142
143 if (log_for_profile_open) {
144 UMA_HISTOGRAM_ENUMERATION(
145 "Omnibox.ZeroSuggest.Eligible.RemoteNoUrl.OnNTP.OnProfileOpen", value);
146 } else {
147 UMA_HISTOGRAM_ENUMERATION(
148 "Omnibox.ZeroSuggest.Eligible.RemoteNoUrl.OnNTP.OnFocus", value);
149 }
150}
151
[email protected]bb1fb2b2013-05-31 00:21:01152// Relevance value to use if it was not set explicitly by the server.
153const int kDefaultZeroSuggestRelevance = 100;
154
mpearson3d89cdc2017-03-03 21:15:45155// Used for testing whether zero suggest is ever available.
mpearsonc90c24b2017-03-04 00:11:26156constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/";
mpearson3d89cdc2017-03-03 21:15:45157
Tomasz Wiszkowski57492ab2019-05-29 21:28:27158// Metric name tracking the omnibox suggestion eligibility.
159constexpr char kOmniboxZeroSuggestEligibleHistogramName[] =
160 "Omnibox.ZeroSuggest.Eligible.OnFocusV2";
161
Moe Ahmadi85bf70862019-10-11 00:03:32162// Remote suggestions are allowed only if the user is signed-in and has Google
163// set up as their default search engine. This only applies to
164// kRemoteNoUrlVariant since most of these checks are done in
165// BaseSearchProvider::CanSendURL (with the exception of the authentication
166// state) which applies to kRemoteSendUrlVariant.
167bool RemoteNoUrlSuggestionsAreAllowed(
Tomasz Wiszkowski5abea202019-06-06 22:51:14168 AutocompleteProviderClient* client,
Gheorghe Comanici864725b2017-11-28 21:48:01169 const TemplateURLService* template_url_service) {
Tomasz Wiszkowski5abea202019-06-06 22:51:14170 if (!client->SearchSuggestEnabled())
Moe Ahmadi85bf70862019-10-11 00:03:32171 return false;
Tomasz Wiszkowski5abea202019-06-06 22:51:14172
173 if (!client->IsAuthenticated())
Moe Ahmadi85bf70862019-10-11 00:03:32174 return false;
Gheorghe Comanici864725b2017-11-28 21:48:01175
Gheorghe Comanici9a364f572018-01-24 17:22:00176 if (template_url_service == nullptr)
177 return false;
Gheorghe Comanici682504102017-11-30 17:47:25178
Gheorghe Comanici9a364f572018-01-24 17:22:00179 const TemplateURL* default_provider =
180 template_url_service->GetDefaultSearchProvider();
Moe Ahmadi85bf70862019-10-11 00:03:32181 return default_provider &&
Gheorghe Comanici9a364f572018-01-24 17:22:00182 default_provider->GetEngineType(
Moe Ahmadi85bf70862019-10-11 00:03:32183 template_url_service->search_terms_data()) == SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01184}
185
[email protected]6ce7f612012-09-05 23:53:07186} // namespace
187
[email protected]a00008d42012-09-15 05:07:58188// static
Tommy C. Li0af09562019-06-11 23:58:51189const char ZeroSuggestProvider::kNoneVariant[] = "None";
190const char ZeroSuggestProvider::kRemoteNoUrlVariant[] = "RemoteNoUrl";
191const char ZeroSuggestProvider::kRemoteSendUrlVariant[] = "RemoteSendUrl";
192const char ZeroSuggestProvider::kMostVisitedVariant[] = "MostVisited";
193
194// static
[email protected]a00008d42012-09-15 05:07:58195ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18196 AutocompleteProviderClient* client,
blundelld130d592015-06-21 19:29:13197 AutocompleteProviderListener* listener) {
Tommy Liec99def2019-10-14 22:37:23198 return new ZeroSuggestProvider(client, listener);
[email protected]6ce7f612012-09-05 23:53:07199}
200
[email protected]855ebff2014-05-09 07:14:38201// static
Moe Ahmadi1266de82020-04-10 17:19:53202void ZeroSuggestProvider::RegisterProfilePrefs(PrefRegistrySimple* registry) {
blundelld130d592015-06-21 19:29:13203 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
204 std::string());
[email protected]855ebff2014-05-09 07:14:38205}
206
[email protected]6ce7f612012-09-05 23:53:07207void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18208 bool minimal_changes) {
a-v-ydd768d52016-03-25 21:07:46209 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54210 matches_.clear();
Kevin Bailey8642e612018-04-23 20:27:54211 Stop(true, false);
Jenny Zhang5bc2e3d2018-09-10 18:50:55212
Tomasz Wiszkowskifd790c02019-08-29 17:31:29213 current_page_classification_ = input.current_page_classification();
214
Tommy C. Li17b5ba112020-08-11 02:29:21215 if (input.focus_type() != OmniboxFocusType::DEFAULT &&
216 IsNTPPage(current_page_classification_)) {
Tomasz Wiszkowskifd790c02019-08-29 17:31:29217 LogOmniboxRemoteNoUrlEligibilityOnNTP(current_page_classification_, false,
218 client());
219 }
220
Tomasz Wiszkowski57492ab2019-05-29 21:28:27221 if (!AllowZeroSuggestSuggestions(input)) {
222 UMA_HISTOGRAM_ENUMERATION(kOmniboxZeroSuggestEligibleHistogramName,
223 ZeroSuggestEligibility::GENERALLY_INELIGIBLE,
224 ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE);
[email protected]f030c4d2014-03-25 01:05:54225 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27226 }
[email protected]bb1fb2b2013-05-31 00:21:01227
Kevin Bailey5fea4322018-03-21 22:36:05228 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13229 set_field_trial_triggered(false);
230 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54231 permanent_text_ = input.text();
232 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54233 current_title_ = input.current_title();
Tommy Liec99def2019-10-14 22:37:23234 current_text_match_ = MatchForCurrentText();
[email protected]9b9fa672013-11-07 06:04:52235
Tommy C. Li6aee62d52019-06-27 20:25:00236 TemplateURLRef::SearchTermsArgs search_terms_args;
237 search_terms_args.page_classification = current_page_classification_;
Tommy Li4b7c4182020-08-18 19:37:01238 search_terms_args.focus_type = input.focus_type();
Tommy C. Li06ec26a2019-06-10 18:01:42239 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Tommy C. Li6aee62d52019-06-27 20:25:00240 search_terms_args, client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41241 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07242 return;
[email protected]162c8d9fa2014-03-18 20:25:41243
Tommy Li26a9f582020-08-18 23:59:55244 result_type_running_ = TypeOfResultToRun(client(), input, suggest_url);
Kevin Bailey5fea4322018-03-21 22:36:05245 if (result_type_running_ == NONE)
[email protected]162c8d9fa2014-03-18 20:25:41246 return;
[email protected]162c8d9fa2014-03-18 20:25:41247
[email protected]6ce7f612012-09-05 23:53:07248 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11249
[email protected]855ebff2014-05-09 07:14:38250 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11251
Kevin Bailey5fea4322018-03-21 22:36:05252 if (result_type_running_ == MOST_VISITED) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11253 most_visited_urls_.clear();
254 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
Gheorghe Comanici9a364f572018-01-24 17:22:00255 if (!ts) {
256 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05257 result_type_running_ = NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00258 return;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11259 }
Gheorghe Comanici9a364f572018-01-24 17:22:00260
Kevin Bailey40bb0ac52019-04-02 00:43:32261 ts->GetMostVisitedURLs(base::BindRepeating(
262 &ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
263 weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11264 return;
265 }
266
Tommy C. Li6aee62d52019-06-27 20:25:00267 search_terms_args.current_page_url =
Tommy C. Lid77691a2019-06-05 02:07:46268 result_type_running_ == REMOTE_SEND_URL ? current_query_ : std::string();
Maks Orlovich1b208512018-06-13 21:08:17269 // Create a request for suggestions, routing completion to
Tommy C. Li06ec26a2019-06-10 18:01:42270 // OnRemoteSuggestionsLoaderAvailable.
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11271 client()
Tommy C. Li06ec26a2019-06-10 18:01:42272 ->GetRemoteSuggestionsService(/*create_if_necessary=*/true)
273 ->CreateSuggestionsRequest(
Tommy Li3fe83912019-08-23 17:33:11274 search_terms_args, client()->GetTemplateURLService(),
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11275 base::BindOnce(
Tommy C. Li06ec26a2019-06-10 18:01:42276 &ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable,
Maks Orlovich1b208512018-06-13 21:08:17277 weak_ptr_factory_.GetWeakPtr()),
278 base::BindOnce(
279 &ZeroSuggestProvider::OnURLLoadComplete,
280 base::Unretained(this) /* this owns SimpleURLLoader */));
[email protected]6ce7f612012-09-05 23:53:07281}
282
mpearson8a37c382015-03-07 05:58:57283void ZeroSuggestProvider::Stop(bool clear_cached_results,
284 bool due_to_user_inactivity) {
Maks Orlovich1b208512018-06-13 21:08:17285 if (loader_)
[email protected]ec3f679b2014-08-18 07:45:13286 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
Maks Orlovich1b208512018-06-13 21:08:17287 loader_.reset();
Tommy Li3fe83912019-08-23 17:33:11288
Kevin Bailey5fea4322018-03-21 22:36:05289 // TODO(krb): It would allow us to remove some guards if we could also cancel
290 // the TopSites::GetMostVisitedURLs request.
[email protected]ec3f679b2014-08-18 07:45:13291 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05292 result_type_running_ = NONE;
293 ++most_visited_request_num_;
[email protected]ec3f679b2014-08-18 07:45:13294
295 if (clear_cached_results) {
296 // We do not call Clear() on |results_| to retain |verbatim_relevance|
297 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18298 // beginning of the next call to Start() to determine the current url
299 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13300 results_.suggest_results.clear();
301 results_.navigation_results.clear();
Moe Ahmadib8a1daf2020-02-14 22:23:25302 results_.experiment_stats.clear();
Moe Ahmadid2704a7a2020-03-31 22:04:26303 results_.headers_map.clear();
[email protected]ec3f679b2014-08-18 07:45:13304 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54305 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45306 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13307 }
308}
309
[email protected]855ebff2014-05-09 07:14:38310void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Moe Ahmadi0993aec2019-09-19 21:42:30311 if (base::Contains(OmniboxFieldTrial::GetZeroSuggestVariants(
312 current_page_classification_),
313 kRemoteNoUrlVariant)) {
[email protected]855ebff2014-05-09 07:14:38314 // Remove the deleted match from the cache, so it is not shown to the user
315 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13316 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38317 std::string());
318 }
319 BaseSearchProvider::DeleteMatch(match);
320}
321
[email protected]ec3f679b2014-08-18 07:45:13322void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
323 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45324 if (!results_.suggest_results.empty() ||
325 !results_.navigation_results.empty() ||
326 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13327 provider_info->back().set_times_returned_results_in_session(1);
328}
329
[email protected]f030c4d2014-03-25 01:05:54330void ZeroSuggestProvider::ResetSession() {
331 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13332 // |field_trial_triggered_in_session| unchanged and set
333 // |field_trial_triggered| to false since zero suggest is inactive now.
334 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54335}
336
mpearson931028c2016-07-01 18:55:11337ZeroSuggestProvider::ZeroSuggestProvider(
338 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11339 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13340 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
[email protected]776ee5902014-08-11 09:15:19341 listener_(listener),
Jeremy Roman5c341f6d2019-07-15 15:56:10342 result_type_running_(NONE) {
Tommy C. Li06ec26a2019-06-10 18:01:42343 // Record whether remote zero suggest is possible for this user / profile.
mpearson3d89cdc2017-03-03 21:15:45344 const TemplateURLService* template_url_service =
345 client->GetTemplateURLService();
346 // Template URL service can be null in tests.
347 if (template_url_service != nullptr) {
Tommy C. Li06ec26a2019-06-10 18:01:42348 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Tommy C. Li6aee62d52019-06-27 20:25:00349 TemplateURLRef::SearchTermsArgs(), template_url_service);
mpearson3d89cdc2017-03-03 21:15:45350 // To check whether this is allowed, use an arbitrary insecure (http) URL
351 // as the URL we'd want suggestions for. The value of OTHER as the current
352 // page classification is to correspond with that URL.
353 UMA_HISTOGRAM_BOOLEAN(
354 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
355 suggest_url.is_valid() &&
356 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
357 template_url_service->GetDefaultSearchProvider(),
358 metrics::OmniboxEventProto::OTHER,
Tommy C. Li71171ee2019-06-20 17:11:47359 template_url_service->search_terms_data(), client,
360 false));
Tomasz Wiszkowskifd790c02019-08-29 17:31:29361
362 LogOmniboxRemoteNoUrlEligibilityOnNTP(
363 OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, true,
364 client);
mpearson3d89cdc2017-03-03 21:15:45365 }
[email protected]6ce7f612012-09-05 23:53:07366}
367
Tomasz Wiszkowskif2464a82020-02-18 22:42:25368ZeroSuggestProvider::~ZeroSuggestProvider() = default;
[email protected]6ce7f612012-09-05 23:53:07369
[email protected]776ee5902014-08-11 09:15:19370const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
371 // Zero suggest provider should not receive keyword results.
372 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13373 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19374}
375
376const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18377 // The callers of this method won't look at the AutocompleteInput's
378 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02379 AutocompleteInput input(base::string16(), current_page_classification_,
380 client()->GetSchemeClassifier());
381 input.set_current_url(GURL(current_query_));
382 input.set_current_title(current_title_);
383 input.set_prevent_inline_autocomplete(true);
384 input.set_allow_exact_keyword_match(false);
385 return input;
[email protected]776ee5902014-08-11 09:15:19386}
387
388bool ZeroSuggestProvider::ShouldAppendExtraParams(
389 const SearchSuggestionParser::SuggestResult& result) const {
390 // We always use the default provider for search, so append the params.
391 return true;
392}
393
[email protected]776ee5902014-08-11 09:15:19394void ZeroSuggestProvider::RecordDeletionResult(bool success) {
395 if (success) {
396 base::RecordAction(
397 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
398 } else {
399 base::RecordAction(
400 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
401 }
402}
403
Maks Orlovich1b208512018-06-13 21:08:17404void ZeroSuggestProvider::OnURLLoadComplete(
405 const network::SimpleURLLoader* source,
406 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19407 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17408 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19409
410 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
411
Gheorghe Comanici9a364f572018-01-24 17:22:00412 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17413 response_body && source->NetError() == net::OK &&
414 (source->ResponseInfo() && source->ResponseInfo()->headers &&
415 source->ResponseInfo()->headers->response_code() == 200) &&
416 UpdateResults(SearchSuggestionParser::ExtractJsonData(
417 source, std::move(response_body)));
418 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19419 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05420 result_type_running_ = NONE;
421 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19422 listener_->OnProviderUpdate(results_updated);
423}
424
Gheorghe Comanici9a364f572018-01-24 17:22:00425bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
426 std::unique_ptr<base::Value> data(
427 SearchSuggestionParser::DeserializeJsonData(json_data));
428 if (!data)
[email protected]855ebff2014-05-09 07:14:38429 return false;
430
Tommy C. Li467c1bcaa2019-06-20 20:48:05431 // When running the REMOTE_NO_URL variant, we want to store suggestion
Gheorghe Comanici9a364f572018-01-24 17:22:00432 // responses if non-empty.
Tommy C. Lid77691a2019-06-05 02:07:46433 if (result_type_running_ == REMOTE_NO_URL && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00434 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
435 json_data);
[email protected]855ebff2014-05-09 07:14:38436
Gheorghe Comanici9a364f572018-01-24 17:22:00437 // If we received an empty result list, we should update the display, as it
438 // may be showing cached results that should not be shown.
439 const base::ListValue* root_list = nullptr;
440 const base::ListValue* results_list = nullptr;
441 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
442 root_list->GetList(1, &results_list) &&
443 !results_list->empty();
444 const bool non_empty_cache = !results_.suggest_results.empty() ||
445 !results_.navigation_results.empty();
446 if (non_empty_parsed_list && non_empty_cache)
447 return false;
448 }
449 const bool results_updated = ParseSuggestResults(
450 *data, kDefaultZeroSuggestRelevance, false, &results_);
451 ConvertResultsToAutocompleteMatches();
452 return results_updated;
[email protected]855ebff2014-05-09 07:14:38453}
454
[email protected]bb1fb2b2013-05-31 00:21:01455AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37456 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01457 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47458 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01459 match.destination_url = navigation.url();
460
[email protected]bb1fb2b2013-05-31 00:21:01461 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13462 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26463 navigation.url(), url_formatter::FormatUrl(navigation.url()),
Kevin Bailey83e643d2018-03-08 16:01:41464 client()->GetSchemeClassifier(), nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01465
manuk3972b2f2019-04-19 14:22:56466 // Zero suggest results should always omit protocols and never appear bold.
467 auto format_types = AutocompleteMatch::GetFormatTypes(false, false);
468 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
469 net::UnescapeRule::SPACES, nullptr,
470 nullptr, nullptr);
471 match.contents_class = ClassifyTermMatches({}, match.contents.length(), 0,
472 ACMatchClassification::URL);
[email protected]9c97f89c2013-06-25 03:12:16473
474 match.description =
475 AutocompleteMatch::SanitizeString(navigation.description());
manuk3972b2f2019-04-19 14:22:56476 match.description_class = ClassifyTermMatches({}, match.description.length(),
477 0, ACMatchClassification::NONE);
478
Tomasz Wiszkowskiafe090f2020-07-10 22:45:36479 match.subtypes = navigation.subtypes();
[email protected]bb1fb2b2013-05-31 00:21:01480 return match;
481}
482
[email protected]8f064e52013-09-18 01:17:14483void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
Kevin Bailey5fea4322018-03-21 22:36:05484 size_t orig_request_num,
[email protected]8f064e52013-09-18 01:17:14485 const history::MostVisitedURLList& urls) {
Kevin Bailey5fea4322018-03-21 22:36:05486 if (result_type_running_ != MOST_VISITED ||
487 orig_request_num != most_visited_request_num_) {
Gheorghe Comanici9a364f572018-01-24 17:22:00488 return;
Kevin Bailey5fea4322018-03-21 22:36:05489 }
[email protected]8f064e52013-09-18 01:17:14490 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22491 done_ = true;
492 ConvertResultsToAutocompleteMatches();
Kevin Bailey5fea4322018-03-21 22:36:05493 result_type_running_ = NONE;
494 ++most_visited_request_num_;
mariakhomenkobfc3a2a2014-10-24 00:48:22495 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14496}
497
Tommy C. Li06ec26a2019-06-10 18:01:42498void ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable(
Maks Orlovich1b208512018-06-13 21:08:17499 std::unique_ptr<network::SimpleURLLoader> loader) {
Tommy C. Li06ec26a2019-06-10 18:01:42500 // RemoteSuggestionsService has already started |loader|, so here it's
Darwin Huang663c17d2020-02-06 01:10:01501 // only necessary to grab its ownership until results come in to
Maks Orlovich1b208512018-06-13 21:08:17502 // OnURLLoadComplete().
503 loader_ = std::move(loader);
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11504 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
505}
506
[email protected]9c97f89c2013-06-25 03:12:16507void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01508 matches_.clear();
509
blundelld130d592015-06-21 19:29:13510 TemplateURLService* template_url_service = client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49511 DCHECK(template_url_service);
[email protected]bb1fb2b2013-05-31 00:21:01512 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13513 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07514 // Fail if we can't set the clickthrough URL for query suggestions.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24515 if (default_provider == nullptr ||
blundelld130d592015-06-21 19:29:13516 !default_provider->SupportsReplacement(
517 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07518 return;
[email protected]6ce7f612012-09-05 23:53:07519
[email protected]00404742014-02-20 13:09:05520 MatchMap map;
Tommy Li823509e2019-12-06 22:42:47521
Tommy C. Li167a3682020-07-16 19:47:23522 // Add all the SuggestResults to the map. We display all ZeroSuggest search
523 // suggestions as unbolded.
Tommy Li823509e2019-12-06 22:42:47524 for (size_t i = 0; i < results_.suggest_results.size(); ++i) {
Tommy Li823509e2019-12-06 22:42:47525 AddMatchToMap(results_.suggest_results[i], std::string(), i, false, false,
526 &map);
527 }
[email protected]00404742014-02-20 13:09:05528
529 const int num_query_results = map.size();
530 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01531 const int num_results = num_query_results + num_nav_results;
Steven Holte95922222018-09-14 20:06:23532 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.QueryResults", num_query_results);
533 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.URLResults", num_nav_results);
534 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01535
[email protected]8f064e52013-09-18 01:17:14536 // Show Most Visited results after ZeroSuggest response is received.
Kevin Bailey5fea4322018-03-21 22:36:05537 if (result_type_running_ == MOST_VISITED) {
Tomasz Wiszkowski57492ab2019-05-29 21:28:27538 // Ensure we don't show most visited URL suggestions on NTP.
539 // This allows us to prevent undesired side outcome of presenting
540 // URL suggestions to users who are not in the personalized field trial for
541 // zero query suggestions.
542 if (IsNTPPage(current_page_classification_) ||
Tommy Liec99def2019-10-14 22:37:23543 !current_text_match_.destination_url.is_valid()) {
[email protected]3feb8b002013-10-14 23:50:13544 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27545 }
Tommy Liec99def2019-10-14 22:37:23546 matches_.push_back(current_text_match_);
[email protected]8f064e52013-09-18 01:17:14547 int relevance = 600;
[email protected]23db6492014-01-16 02:35:30548 const base::string16 current_query_string16(
549 base::ASCIIToUTF16(current_query_));
Tomasz Wiszkowskif2464a82020-02-18 22:42:25550 for (const auto& url : most_visited_urls_) {
[email protected]0b9575f2014-07-30 11:58:37551 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13552 client()->GetSchemeClassifier(), url.url,
Tomasz Wiszkowskiafe090f2020-07-10 22:45:36553 AutocompleteMatchType::NAVSUGGEST, {}, url.title, std::string(),
Tomasz Wiszkowski6a38a20b2020-07-09 01:36:18554 false, relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14555 matches_.push_back(NavigationToMatch(nav));
556 --relevance;
557 }
558 return;
559 }
560
[email protected]9c97f89c2013-06-25 03:12:16561 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01562 return;
563
Tommy Li0731aee2020-07-22 23:04:32564#if defined(OS_ANDROID) || defined(OS_IOS)
565 // Android needs the verbatim match on non-NTP surfaces to properly present
566 // the Search Ready Omnibox URL edit widget. Desktop specifically does NOT
567 // want to show verbatim matches in remotely-fetched ZeroSuggest anymore.
568 // iOS we are keeping the same as Android for now. No strong reason to change.
Tomasz Wiszkowski57492ab2019-05-29 21:28:27569 if (!IsNTPPage(current_page_classification_) &&
Tommy Liec99def2019-10-14 22:37:23570 current_text_match_.destination_url.is_valid()) {
571 matches_.push_back(current_text_match_);
Tomasz Wiszkowski57492ab2019-05-29 21:28:27572 }
Tommy Li0731aee2020-07-22 23:04:32573#endif
Tomasz Wiszkowski57492ab2019-05-29 21:28:27574
[email protected]00404742014-02-20 13:09:05575 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01576 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01577
[email protected]0b9575f2014-07-30 11:58:37578 const SearchSuggestionParser::NavigationResults& nav_results(
579 results_.navigation_results);
Tomasz Wiszkowskif2464a82020-02-18 22:42:25580 for (const auto& nav_result : nav_results) {
581 matches_.push_back(NavigationToMatch(nav_result));
gcomanici67d53ac2017-04-01 17:07:19582 }
[email protected]6ce7f612012-09-05 23:53:07583}
584
Tommy Liec99def2019-10-14 22:37:23585AutocompleteMatch ZeroSuggestProvider::MatchForCurrentText() {
[email protected]bb1fb2b2013-05-31 00:21:01586 // The placeholder suggestion for the current URL has high relevance so
587 // that it is in the first suggestion slot and inline autocompleted. It
588 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11589 AutocompleteInput tmp(GetInput(false));
590 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54591 const base::string16 description =
592 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
593 ? current_title_
594 : base::string16();
Tommy Liec99def2019-10-14 22:37:23595
596 // We pass a nullptr as the |history_url_provider| parameter now to force
597 // VerbatimMatch to do a classification, since the text can be a search query.
598 // TODO(tommycli): Simplify this - probably just bypass VerbatimMatchForURL.
599 AutocompleteMatch match = VerbatimMatchForURL(
600 client(), tmp, GURL(current_query_), description,
601 /*history_url_provider=*/nullptr, results_.verbatim_relevance);
602 match.provider = this;
603 return match;
[email protected]00404742014-02-20 13:09:05604}
[email protected]162c8d9fa2014-03-18 20:25:41605
Gheorghe Comanici9a364f572018-01-24 17:22:00606bool ZeroSuggestProvider::AllowZeroSuggestSuggestions(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27607 const AutocompleteInput& input) const {
608 const auto& page_url = input.current_url();
609 const auto page_class = input.current_page_classification();
610 const auto input_type = input.type();
Peter Kastingfb1a8ea2017-11-28 02:26:50611
Tommy C. Li17b5ba112020-08-11 02:29:21612 if (input.focus_type() == OmniboxFocusType::DEFAULT)
Tomasz Wiszkowski57492ab2019-05-29 21:28:27613 return false;
614
Tommy Li8bc9cd62020-08-18 02:37:28615 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41616 return false;
617
Tommy Li19e27ede2020-08-14 22:52:40618 if (input_type == metrics::OmniboxInputType::EMPTY) {
619 // Function that returns whether EMPTY input zero-suggest is allowed.
620 auto IsEmptyZeroSuggestAllowed = [&]() {
621 if (page_class == metrics::OmniboxEventProto::CHROMEOS_APP_LIST ||
622 IsNTPPage(page_class)) {
623 return true;
624 }
625
626 if (page_class == metrics::OmniboxEventProto::OTHER) {
627 return input.focus_type() == OmniboxFocusType::DELETED_PERMANENT_TEXT &&
628 base::FeatureList::IsEnabled(
629 omnibox::kClobberTriggersContextualWebZeroSuggest);
630 }
631
632 return false;
633 };
634
635 // Return false if disallowed. Otherwise, proceed down to further checks.
636 if (!IsEmptyZeroSuggestAllowed())
637 return false;
[email protected]5b899dc2018-10-02 09:20:01638 }
639
Tomasz Wiszkowski57492ab2019-05-29 21:28:27640 // When omnibox contains pre-populated content, only show zero suggest for
641 // pages with URLs the user will recognize.
642 //
Mark Pearson3fb0e3162018-08-27 21:53:57643 // This list intentionally does not include items such as ftp: and file:
Tommy C. Li06ec26a2019-06-10 18:01:42644 // because (a) these do not work on Android and iOS, where most visited
Mark Pearson3fb0e3162018-08-27 21:53:57645 // zero suggest is launched and (b) on desktop, where contextual zero suggest
646 // is running, these types of schemes aren't eligible to be sent to the
647 // server to ask for suggestions (and thus in practice we won't display zero
648 // suggest for them).
Yue Ru Sune31e1572019-07-19 17:09:57649 if (input_type != metrics::OmniboxInputType::EMPTY &&
Tomasz Wiszkowski57492ab2019-05-29 21:28:27650 !(page_url.is_valid() &&
651 ((page_url.scheme() == url::kHttpScheme) ||
652 (page_url.scheme() == url::kHttpsScheme) ||
653 (page_url.scheme() == url::kAboutScheme) ||
654 (page_url.scheme() ==
655 client()->GetEmbedderRepresentationOfAboutScheme())))) {
[email protected]162c8d9fa2014-03-18 20:25:41656 return false;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27657 }
[email protected]162c8d9fa2014-03-18 20:25:41658
659 return true;
660}
[email protected]855ebff2014-05-09 07:14:38661
662void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Tommy C. Lid77691a2019-06-05 02:07:46663 if (result_type_running_ != REMOTE_NO_URL)
[email protected]855ebff2014-05-09 07:14:38664 return;
665
blundelld130d592015-06-21 19:29:13666 std::string json_data =
667 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38668 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57669 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14670 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00671 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
672 &results_))
[email protected]855ebff2014-05-09 07:14:38673 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38674 }
675}
Gheorghe Comanici9a364f572018-01-24 17:22:00676
Tommy Li26a9f582020-08-18 23:59:55677// static
Gheorghe Comanici9a364f572018-01-24 17:22:00678ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
Tommy Li26a9f582020-08-18 23:59:55679 AutocompleteProviderClient* client,
680 const AutocompleteInput& input,
Gheorghe Comanici9a364f572018-01-24 17:22:00681 const GURL& suggest_url) {
Tommy Li26a9f582020-08-18 23:59:55682 DCHECK(client);
Gheorghe Comanici9a364f572018-01-24 17:22:00683 // Check if the URL can be sent in any suggest request.
684 const TemplateURLService* template_url_service =
Tommy Li26a9f582020-08-18 23:59:55685 client->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49686 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00687 const TemplateURL* default_provider =
688 template_url_service->GetDefaultSearchProvider();
Tommy Li26a9f582020-08-18 23:59:55689
690 GURL current_url = input.current_url();
691 metrics::OmniboxEventProto::PageClassification current_page_classification =
692 input.current_page_classification();
693
Gheorghe Comanici9a364f572018-01-24 17:22:00694 const bool can_send_current_url = CanSendURL(
Tommy Li26a9f582020-08-18 23:59:55695 current_url, suggest_url, default_provider, current_page_classification,
696 template_url_service->search_terms_data(), client, false);
Gheorghe Comanici9a364f572018-01-24 17:22:00697 // Collect metrics on eligibility.
698 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
699 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
700 if (!can_send_current_url) {
701 const bool can_send_ordinary_url =
702 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
Tommy Li26a9f582020-08-18 23:59:55703 current_page_classification,
704 template_url_service->search_terms_data(), client, false);
Gheorghe Comanici9a364f572018-01-24 17:22:00705 eligibility = can_send_ordinary_url
706 ? ZeroSuggestEligibility::URL_INELIGIBLE
707 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
708 }
709 UMA_HISTOGRAM_ENUMERATION(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27710 kOmniboxZeroSuggestEligibleHistogramName, static_cast<int>(eligibility),
Gheorghe Comanici9a364f572018-01-24 17:22:00711 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
712
Moe Ahmadi0993aec2019-09-19 21:42:30713 const auto field_trial_variants =
Tommy Li26a9f582020-08-18 23:59:55714 OmniboxFieldTrial::GetZeroSuggestVariants(current_page_classification);
Tommy C. Li0af09562019-06-11 23:58:51715
Stepan Khapugin5187a462020-01-07 13:16:26716 if (base::Contains(field_trial_variants, kNoneVariant))
Kevin Bailey5fea4322018-03-21 22:36:05717 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00718
Tommy Li26a9f582020-08-18 23:59:55719 if (current_page_classification == OmniboxEventProto::CHROMEOS_APP_LIST)
Tommy C. Lid77691a2019-06-05 02:07:46720 return REMOTE_NO_URL;
Jenny Zhang5bc2e3d2018-09-10 18:50:55721
Tommy Lie0a3a992020-05-29 21:32:05722 // Contextual Open Web - (same client side behavior for multiple variants).
723 bool contextual_web_suggestions_enabled =
724 base::FeatureList::IsEnabled(omnibox::kOnFocusSuggestionsContextualWeb) ||
725 base::FeatureList::IsEnabled(
726 omnibox::kOnFocusSuggestionsContextualWebOnContent);
Tommy Li26a9f582020-08-18 23:59:55727 if (current_page_classification == OmniboxEventProto::OTHER &&
Tommy Lie0a3a992020-05-29 21:32:05728 can_send_current_url && contextual_web_suggestions_enabled) {
Tommy Li55784022020-04-28 20:58:18729 return REMOTE_SEND_URL;
730 }
731
Moe Ahmadi31147bc2020-06-02 19:07:56732 // Reactive Zero-Prefix Suggestions (rZPS) on NTP cases.
Tommy Lib4b3bb5b2020-05-01 02:34:56733 bool remote_no_url_allowed =
Tommy Li26a9f582020-08-18 23:59:55734 RemoteNoUrlSuggestionsAreAllowed(client, template_url_service);
Tommy Lib4b3bb5b2020-05-01 02:34:56735 if (remote_no_url_allowed) {
736 // NTP Omnibox.
Tommy Li26a9f582020-08-18 23:59:55737 if ((current_page_classification == OmniboxEventProto::NTP ||
738 current_page_classification ==
Tommy Lib4b3bb5b2020-05-01 02:34:56739 OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS) &&
740 base::FeatureList::IsEnabled(
Moe Ahmadi31147bc2020-06-02 19:07:56741 omnibox::kReactiveZeroSuggestionsOnNTPOmnibox)) {
Tommy Lib4b3bb5b2020-05-01 02:34:56742 return REMOTE_NO_URL;
743 }
744 // NTP Realbox.
Tommy Li26a9f582020-08-18 23:59:55745 if (current_page_classification == OmniboxEventProto::NTP_REALBOX &&
Tommy Lib4b3bb5b2020-05-01 02:34:56746 base::FeatureList::IsEnabled(
Moe Ahmadi31147bc2020-06-02 19:07:56747 omnibox::kReactiveZeroSuggestionsOnNTPRealbox)) {
Tommy Lib4b3bb5b2020-05-01 02:34:56748 return REMOTE_NO_URL;
749 }
750 }
751
Tommy Li0801a8b2020-05-30 00:28:55752 if (base::Contains(field_trial_variants, kRemoteNoUrlVariant) &&
753 remote_no_url_allowed) {
754 return REMOTE_NO_URL;
Tommy C. Li393c1b142019-05-22 00:28:41755 }
Gheorghe Comanici9a364f572018-01-24 17:22:00756
Moe Ahmadi0993aec2019-09-19 21:42:30757 if (base::Contains(field_trial_variants, kRemoteSendUrlVariant) &&
758 can_send_current_url)
Tommy C. Li0af09562019-06-11 23:58:51759 return REMOTE_SEND_URL;
Gheorghe Comanici9a364f572018-01-24 17:22:00760
Moe Ahmadi0993aec2019-09-19 21:42:30761 if (base::Contains(field_trial_variants, kMostVisitedVariant))
Tommy C. Li0af09562019-06-11 23:58:51762 return MOST_VISITED;
763
Moe Ahmadi8ef94b72020-08-13 23:18:15764#if !defined(OS_IOS)
765 // For Desktop and Android, default to REMOTE_NO_URL on the NTP, if allowed.
Tommy Li26a9f582020-08-18 23:59:55766 if (IsNTPPage(current_page_classification) && remote_no_url_allowed)
Tommy Li0801a8b2020-05-30 00:28:55767 return REMOTE_NO_URL;
768#endif
769
Tommy C. Li0af09562019-06-11 23:58:51770#if defined(OS_ANDROID) || defined(OS_IOS)
Moe Ahmadi8ef94b72020-08-13 23:18:15771 // For Android and iOS, default to MOST_VISITED everywhere except on the SERP.
Tommy Li26a9f582020-08-18 23:59:55772 if (!IsSearchResultsPage(current_page_classification)) {
Kevin Bailey5fea4322018-03-21 22:36:05773 return MOST_VISITED;
Tommy C. Li393c1b142019-05-22 00:28:41774 }
Tommy C. Li0af09562019-06-11 23:58:51775#endif
Tommy C. Lic79d39ab2019-06-04 16:58:56776
777 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00778}