blob: 4e689bc1535eba6ba2b355ae6896025afb50fa12 [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
[email protected]bb1fb2b2013-05-31 00:21:0172// TODO(hfung): The histogram code was copied and modified from
73// search_provider.cc. Refactor and consolidate the code.
74// We keep track in a histogram how many suggest requests we send, how
75// many suggest requests we invalidate (e.g., due to a user typing
76// another character), and how many replies we receive.
mpearson3d89cdc2017-03-03 21:15:4577// These values are written to logs. New enum values can be added, but existing
78// enums must never be renumbered or deleted and reused.
[email protected]bb1fb2b2013-05-31 00:21:0179enum ZeroSuggestRequestsHistogramValue {
80 ZERO_SUGGEST_REQUEST_SENT = 1,
mpearson3d89cdc2017-03-03 21:15:4581 ZERO_SUGGEST_REQUEST_INVALIDATED = 2,
82 ZERO_SUGGEST_REPLY_RECEIVED = 3,
[email protected]bb1fb2b2013-05-31 00:21:0183 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
84};
85
86void LogOmniboxZeroSuggestRequest(
87 ZeroSuggestRequestsHistogramValue request_value) {
88 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
89 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
90}
91
[email protected]bb1fb2b2013-05-31 00:21:0192// Relevance value to use if it was not set explicitly by the server.
93const int kDefaultZeroSuggestRelevance = 100;
Tomasz Wiszkowskie23c56b2020-09-24 20:08:3594// The relevance score for navsuggest tiles.
95// Navsuggest tiles should be positioned below the Query Tiles object.
96const int kMostVisitedTilesRelevance = 1500;
[email protected]bb1fb2b2013-05-31 00:21:0197
mpearson3d89cdc2017-03-03 21:15:4598// Used for testing whether zero suggest is ever available.
mpearsonc90c24b2017-03-04 00:11:2699constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/";
mpearson3d89cdc2017-03-03 21:15:45100
Tomasz Wiszkowski57492ab2019-05-29 21:28:27101// Metric name tracking the omnibox suggestion eligibility.
102constexpr char kOmniboxZeroSuggestEligibleHistogramName[] =
103 "Omnibox.ZeroSuggest.Eligible.OnFocusV2";
104
Moe Ahmadi85bf70862019-10-11 00:03:32105// Remote suggestions are allowed only if the user is signed-in and has Google
Moe Ahmadi189b6532020-09-02 17:57:12106// set up as their default search engine. The authentication state check is done
107// not for privacy reasons but to prevent signed-out users from querying the
108// server which does not have any suggestions for them. This check is skipped if
109// |check_authentication_state| is false.
110// This function only applies to kRemoteNoUrlVariant. For kRemoteSendUrlVariant,
111// most of these checks with the exception of the authentication state are done
112// in BaseSearchProvider::CanSendURL().
Moe Ahmadi85bf70862019-10-11 00:03:32113bool RemoteNoUrlSuggestionsAreAllowed(
Tomasz Wiszkowski5abea202019-06-06 22:51:14114 AutocompleteProviderClient* client,
Moe Ahmadi189b6532020-09-02 17:57:12115 const TemplateURLService* template_url_service,
116 bool check_authentication_state) {
Tomasz Wiszkowski5abea202019-06-06 22:51:14117 if (!client->SearchSuggestEnabled())
Moe Ahmadi85bf70862019-10-11 00:03:32118 return false;
Tomasz Wiszkowski5abea202019-06-06 22:51:14119
Moe Ahmadi189b6532020-09-02 17:57:12120 if (check_authentication_state && !client->IsAuthenticated())
Moe Ahmadi85bf70862019-10-11 00:03:32121 return false;
Gheorghe Comanici864725b2017-11-28 21:48:01122
Gheorghe Comanici9a364f572018-01-24 17:22:00123 if (template_url_service == nullptr)
124 return false;
Gheorghe Comanici682504102017-11-30 17:47:25125
Gheorghe Comanici9a364f572018-01-24 17:22:00126 const TemplateURL* default_provider =
127 template_url_service->GetDefaultSearchProvider();
Moe Ahmadi85bf70862019-10-11 00:03:32128 return default_provider &&
Gheorghe Comanici9a364f572018-01-24 17:22:00129 default_provider->GetEngineType(
Moe Ahmadi85bf70862019-10-11 00:03:32130 template_url_service->search_terms_data()) == SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01131}
132
[email protected]6ce7f612012-09-05 23:53:07133} // namespace
134
[email protected]a00008d42012-09-15 05:07:58135// static
Tommy C. Li0af09562019-06-11 23:58:51136const char ZeroSuggestProvider::kNoneVariant[] = "None";
137const char ZeroSuggestProvider::kRemoteNoUrlVariant[] = "RemoteNoUrl";
138const char ZeroSuggestProvider::kRemoteSendUrlVariant[] = "RemoteSendUrl";
139const char ZeroSuggestProvider::kMostVisitedVariant[] = "MostVisited";
140
141// static
[email protected]a00008d42012-09-15 05:07:58142ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18143 AutocompleteProviderClient* client,
blundelld130d592015-06-21 19:29:13144 AutocompleteProviderListener* listener) {
Tommy Liec99def2019-10-14 22:37:23145 return new ZeroSuggestProvider(client, listener);
[email protected]6ce7f612012-09-05 23:53:07146}
147
[email protected]855ebff2014-05-09 07:14:38148// static
Moe Ahmadi1266de82020-04-10 17:19:53149void ZeroSuggestProvider::RegisterProfilePrefs(PrefRegistrySimple* registry) {
blundelld130d592015-06-21 19:29:13150 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
151 std::string());
[email protected]855ebff2014-05-09 07:14:38152}
153
[email protected]6ce7f612012-09-05 23:53:07154void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18155 bool minimal_changes) {
a-v-ydd768d52016-03-25 21:07:46156 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54157 matches_.clear();
Kevin Bailey8642e612018-04-23 20:27:54158 Stop(true, false);
Jenny Zhang5bc2e3d2018-09-10 18:50:55159
Tomasz Wiszkowski57492ab2019-05-29 21:28:27160 if (!AllowZeroSuggestSuggestions(input)) {
161 UMA_HISTOGRAM_ENUMERATION(kOmniboxZeroSuggestEligibleHistogramName,
162 ZeroSuggestEligibility::GENERALLY_INELIGIBLE,
163 ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE);
[email protected]f030c4d2014-03-25 01:05:54164 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27165 }
[email protected]bb1fb2b2013-05-31 00:21:01166
Kevin Bailey5fea4322018-03-21 22:36:05167 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13168 set_field_trial_triggered(false);
169 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54170 permanent_text_ = input.text();
171 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54172 current_title_ = input.current_title();
Tommy Lic53b97e2020-09-03 04:02:40173 current_page_classification_ = input.current_page_classification();
Tommy Liec99def2019-10-14 22:37:23174 current_text_match_ = MatchForCurrentText();
[email protected]9b9fa672013-11-07 06:04:52175
Tommy C. Li6aee62d52019-06-27 20:25:00176 TemplateURLRef::SearchTermsArgs search_terms_args;
177 search_terms_args.page_classification = current_page_classification_;
Tommy Li4b7c4182020-08-18 19:37:01178 search_terms_args.focus_type = input.focus_type();
Tommy C. Li06ec26a2019-06-10 18:01:42179 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Tommy C. Li6aee62d52019-06-27 20:25:00180 search_terms_args, client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41181 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07182 return;
[email protected]162c8d9fa2014-03-18 20:25:41183
Tommy Li26a9f582020-08-18 23:59:55184 result_type_running_ = TypeOfResultToRun(client(), input, suggest_url);
Kevin Bailey5fea4322018-03-21 22:36:05185 if (result_type_running_ == NONE)
[email protected]162c8d9fa2014-03-18 20:25:41186 return;
[email protected]162c8d9fa2014-03-18 20:25:41187
[email protected]6ce7f612012-09-05 23:53:07188 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11189
[email protected]855ebff2014-05-09 07:14:38190 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11191
Kevin Bailey5fea4322018-03-21 22:36:05192 if (result_type_running_ == MOST_VISITED) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11193 most_visited_urls_.clear();
194 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
Gheorghe Comanici9a364f572018-01-24 17:22:00195 if (!ts) {
196 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05197 result_type_running_ = NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00198 return;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11199 }
Gheorghe Comanici9a364f572018-01-24 17:22:00200
Kevin Bailey40bb0ac52019-04-02 00:43:32201 ts->GetMostVisitedURLs(base::BindRepeating(
202 &ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
203 weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11204 return;
205 }
206
Tommy C. Li6aee62d52019-06-27 20:25:00207 search_terms_args.current_page_url =
Tommy C. Lid77691a2019-06-05 02:07:46208 result_type_running_ == REMOTE_SEND_URL ? current_query_ : std::string();
Maks Orlovich1b208512018-06-13 21:08:17209 // Create a request for suggestions, routing completion to
Tommy C. Li06ec26a2019-06-10 18:01:42210 // OnRemoteSuggestionsLoaderAvailable.
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11211 client()
Tommy C. Li06ec26a2019-06-10 18:01:42212 ->GetRemoteSuggestionsService(/*create_if_necessary=*/true)
213 ->CreateSuggestionsRequest(
Tommy Li3fe83912019-08-23 17:33:11214 search_terms_args, client()->GetTemplateURLService(),
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11215 base::BindOnce(
Tommy C. Li06ec26a2019-06-10 18:01:42216 &ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable,
Maks Orlovich1b208512018-06-13 21:08:17217 weak_ptr_factory_.GetWeakPtr()),
218 base::BindOnce(
219 &ZeroSuggestProvider::OnURLLoadComplete,
220 base::Unretained(this) /* this owns SimpleURLLoader */));
[email protected]6ce7f612012-09-05 23:53:07221}
222
mpearson8a37c382015-03-07 05:58:57223void ZeroSuggestProvider::Stop(bool clear_cached_results,
224 bool due_to_user_inactivity) {
Maks Orlovich1b208512018-06-13 21:08:17225 if (loader_)
[email protected]ec3f679b2014-08-18 07:45:13226 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
Maks Orlovich1b208512018-06-13 21:08:17227 loader_.reset();
Tommy Li3fe83912019-08-23 17:33:11228
Kevin Bailey5fea4322018-03-21 22:36:05229 // TODO(krb): It would allow us to remove some guards if we could also cancel
230 // the TopSites::GetMostVisitedURLs request.
[email protected]ec3f679b2014-08-18 07:45:13231 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05232 result_type_running_ = NONE;
233 ++most_visited_request_num_;
[email protected]ec3f679b2014-08-18 07:45:13234
235 if (clear_cached_results) {
236 // We do not call Clear() on |results_| to retain |verbatim_relevance|
237 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18238 // beginning of the next call to Start() to determine the current url
239 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13240 results_.suggest_results.clear();
241 results_.navigation_results.clear();
Moe Ahmadib8a1daf2020-02-14 22:23:25242 results_.experiment_stats.clear();
Moe Ahmadid2704a7a2020-03-31 22:04:26243 results_.headers_map.clear();
[email protected]ec3f679b2014-08-18 07:45:13244 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54245 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45246 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13247 }
248}
249
[email protected]855ebff2014-05-09 07:14:38250void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Moe Ahmadi0993aec2019-09-19 21:42:30251 if (base::Contains(OmniboxFieldTrial::GetZeroSuggestVariants(
252 current_page_classification_),
253 kRemoteNoUrlVariant)) {
[email protected]855ebff2014-05-09 07:14:38254 // Remove the deleted match from the cache, so it is not shown to the user
255 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13256 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38257 std::string());
258 }
259 BaseSearchProvider::DeleteMatch(match);
260}
261
[email protected]ec3f679b2014-08-18 07:45:13262void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
263 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45264 if (!results_.suggest_results.empty() ||
265 !results_.navigation_results.empty() ||
266 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13267 provider_info->back().set_times_returned_results_in_session(1);
268}
269
[email protected]f030c4d2014-03-25 01:05:54270void ZeroSuggestProvider::ResetSession() {
271 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13272 // |field_trial_triggered_in_session| unchanged and set
273 // |field_trial_triggered| to false since zero suggest is inactive now.
274 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54275}
276
mpearson931028c2016-07-01 18:55:11277ZeroSuggestProvider::ZeroSuggestProvider(
278 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11279 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13280 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
[email protected]776ee5902014-08-11 09:15:19281 listener_(listener),
Jeremy Roman5c341f6d2019-07-15 15:56:10282 result_type_running_(NONE) {
Tommy C. Li06ec26a2019-06-10 18:01:42283 // Record whether remote zero suggest is possible for this user / profile.
mpearson3d89cdc2017-03-03 21:15:45284 const TemplateURLService* template_url_service =
285 client->GetTemplateURLService();
286 // Template URL service can be null in tests.
287 if (template_url_service != nullptr) {
Tommy C. Li06ec26a2019-06-10 18:01:42288 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Tommy C. Li6aee62d52019-06-27 20:25:00289 TemplateURLRef::SearchTermsArgs(), template_url_service);
mpearson3d89cdc2017-03-03 21:15:45290 // To check whether this is allowed, use an arbitrary insecure (http) URL
291 // as the URL we'd want suggestions for. The value of OTHER as the current
292 // page classification is to correspond with that URL.
293 UMA_HISTOGRAM_BOOLEAN(
294 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
295 suggest_url.is_valid() &&
296 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
297 template_url_service->GetDefaultSearchProvider(),
298 metrics::OmniboxEventProto::OTHER,
Tommy C. Li71171ee2019-06-20 17:11:47299 template_url_service->search_terms_data(), client,
300 false));
mpearson3d89cdc2017-03-03 21:15:45301 }
[email protected]6ce7f612012-09-05 23:53:07302}
303
Tomasz Wiszkowskif2464a82020-02-18 22:42:25304ZeroSuggestProvider::~ZeroSuggestProvider() = default;
[email protected]6ce7f612012-09-05 23:53:07305
[email protected]776ee5902014-08-11 09:15:19306const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
307 // Zero suggest provider should not receive keyword results.
308 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13309 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19310}
311
312const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18313 // The callers of this method won't look at the AutocompleteInput's
314 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02315 AutocompleteInput input(base::string16(), current_page_classification_,
316 client()->GetSchemeClassifier());
317 input.set_current_url(GURL(current_query_));
318 input.set_current_title(current_title_);
319 input.set_prevent_inline_autocomplete(true);
320 input.set_allow_exact_keyword_match(false);
321 return input;
[email protected]776ee5902014-08-11 09:15:19322}
323
324bool ZeroSuggestProvider::ShouldAppendExtraParams(
325 const SearchSuggestionParser::SuggestResult& result) const {
326 // We always use the default provider for search, so append the params.
327 return true;
328}
329
[email protected]776ee5902014-08-11 09:15:19330void ZeroSuggestProvider::RecordDeletionResult(bool success) {
331 if (success) {
332 base::RecordAction(
333 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
334 } else {
335 base::RecordAction(
336 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
337 }
338}
339
Maks Orlovich1b208512018-06-13 21:08:17340void ZeroSuggestProvider::OnURLLoadComplete(
341 const network::SimpleURLLoader* source,
342 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19343 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17344 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19345
346 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
347
Gheorghe Comanici9a364f572018-01-24 17:22:00348 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17349 response_body && source->NetError() == net::OK &&
350 (source->ResponseInfo() && source->ResponseInfo()->headers &&
351 source->ResponseInfo()->headers->response_code() == 200) &&
352 UpdateResults(SearchSuggestionParser::ExtractJsonData(
353 source, std::move(response_body)));
354 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19355 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05356 result_type_running_ = NONE;
357 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19358 listener_->OnProviderUpdate(results_updated);
359}
360
Gheorghe Comanici9a364f572018-01-24 17:22:00361bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
362 std::unique_ptr<base::Value> data(
363 SearchSuggestionParser::DeserializeJsonData(json_data));
364 if (!data)
[email protected]855ebff2014-05-09 07:14:38365 return false;
366
Tommy C. Li467c1bcaa2019-06-20 20:48:05367 // When running the REMOTE_NO_URL variant, we want to store suggestion
Gheorghe Comanici9a364f572018-01-24 17:22:00368 // responses if non-empty.
Tommy C. Lid77691a2019-06-05 02:07:46369 if (result_type_running_ == REMOTE_NO_URL && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00370 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
371 json_data);
[email protected]855ebff2014-05-09 07:14:38372
Gheorghe Comanici9a364f572018-01-24 17:22:00373 // If we received an empty result list, we should update the display, as it
374 // may be showing cached results that should not be shown.
375 const base::ListValue* root_list = nullptr;
376 const base::ListValue* results_list = nullptr;
377 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
378 root_list->GetList(1, &results_list) &&
379 !results_list->empty();
380 const bool non_empty_cache = !results_.suggest_results.empty() ||
381 !results_.navigation_results.empty();
382 if (non_empty_parsed_list && non_empty_cache)
383 return false;
384 }
385 const bool results_updated = ParseSuggestResults(
386 *data, kDefaultZeroSuggestRelevance, false, &results_);
387 ConvertResultsToAutocompleteMatches();
388 return results_updated;
[email protected]855ebff2014-05-09 07:14:38389}
390
[email protected]bb1fb2b2013-05-31 00:21:01391AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37392 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01393 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47394 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01395 match.destination_url = navigation.url();
396
[email protected]bb1fb2b2013-05-31 00:21:01397 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13398 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26399 navigation.url(), url_formatter::FormatUrl(navigation.url()),
Kevin Bailey83e643d2018-03-08 16:01:41400 client()->GetSchemeClassifier(), nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01401
manuk3972b2f2019-04-19 14:22:56402 // Zero suggest results should always omit protocols and never appear bold.
403 auto format_types = AutocompleteMatch::GetFormatTypes(false, false);
404 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
405 net::UnescapeRule::SPACES, nullptr,
406 nullptr, nullptr);
407 match.contents_class = ClassifyTermMatches({}, match.contents.length(), 0,
408 ACMatchClassification::URL);
[email protected]9c97f89c2013-06-25 03:12:16409
410 match.description =
411 AutocompleteMatch::SanitizeString(navigation.description());
manuk3972b2f2019-04-19 14:22:56412 match.description_class = ClassifyTermMatches({}, match.description.length(),
413 0, ACMatchClassification::NONE);
414
Tomasz Wiszkowskiafe090f2020-07-10 22:45:36415 match.subtypes = navigation.subtypes();
[email protected]bb1fb2b2013-05-31 00:21:01416 return match;
417}
418
[email protected]8f064e52013-09-18 01:17:14419void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
Kevin Bailey5fea4322018-03-21 22:36:05420 size_t orig_request_num,
[email protected]8f064e52013-09-18 01:17:14421 const history::MostVisitedURLList& urls) {
Kevin Bailey5fea4322018-03-21 22:36:05422 if (result_type_running_ != MOST_VISITED ||
423 orig_request_num != most_visited_request_num_) {
Gheorghe Comanici9a364f572018-01-24 17:22:00424 return;
Kevin Bailey5fea4322018-03-21 22:36:05425 }
[email protected]8f064e52013-09-18 01:17:14426 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22427 done_ = true;
428 ConvertResultsToAutocompleteMatches();
Kevin Bailey5fea4322018-03-21 22:36:05429 result_type_running_ = NONE;
430 ++most_visited_request_num_;
mariakhomenkobfc3a2a2014-10-24 00:48:22431 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14432}
433
Tommy C. Li06ec26a2019-06-10 18:01:42434void ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable(
Maks Orlovich1b208512018-06-13 21:08:17435 std::unique_ptr<network::SimpleURLLoader> loader) {
Tommy C. Li06ec26a2019-06-10 18:01:42436 // RemoteSuggestionsService has already started |loader|, so here it's
Darwin Huang663c17d2020-02-06 01:10:01437 // only necessary to grab its ownership until results come in to
Maks Orlovich1b208512018-06-13 21:08:17438 // OnURLLoadComplete().
439 loader_ = std::move(loader);
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11440 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
441}
442
[email protected]9c97f89c2013-06-25 03:12:16443void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01444 matches_.clear();
445
blundelld130d592015-06-21 19:29:13446 TemplateURLService* template_url_service = client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49447 DCHECK(template_url_service);
[email protected]bb1fb2b2013-05-31 00:21:01448 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13449 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07450 // Fail if we can't set the clickthrough URL for query suggestions.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24451 if (default_provider == nullptr ||
blundelld130d592015-06-21 19:29:13452 !default_provider->SupportsReplacement(
453 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07454 return;
[email protected]6ce7f612012-09-05 23:53:07455
[email protected]00404742014-02-20 13:09:05456 MatchMap map;
Tommy Li823509e2019-12-06 22:42:47457
Tommy C. Li167a3682020-07-16 19:47:23458 // Add all the SuggestResults to the map. We display all ZeroSuggest search
459 // suggestions as unbolded.
Tommy Li823509e2019-12-06 22:42:47460 for (size_t i = 0; i < results_.suggest_results.size(); ++i) {
Tommy Li823509e2019-12-06 22:42:47461 AddMatchToMap(results_.suggest_results[i], std::string(), i, false, false,
462 &map);
463 }
[email protected]00404742014-02-20 13:09:05464
465 const int num_query_results = map.size();
466 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01467 const int num_results = num_query_results + num_nav_results;
Steven Holte95922222018-09-14 20:06:23468 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.QueryResults", num_query_results);
469 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.URLResults", num_nav_results);
470 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01471
[email protected]8f064e52013-09-18 01:17:14472 // Show Most Visited results after ZeroSuggest response is received.
Kevin Bailey5fea4322018-03-21 22:36:05473 if (result_type_running_ == MOST_VISITED) {
Tomasz Wiszkowski57492ab2019-05-29 21:28:27474 // Ensure we don't show most visited URL suggestions on NTP.
475 // This allows us to prevent undesired side outcome of presenting
476 // URL suggestions to users who are not in the personalized field trial for
477 // zero query suggestions.
478 if (IsNTPPage(current_page_classification_) ||
Tommy Liec99def2019-10-14 22:37:23479 !current_text_match_.destination_url.is_valid()) {
[email protected]3feb8b002013-10-14 23:50:13480 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27481 }
Tommy Liec99def2019-10-14 22:37:23482 matches_.push_back(current_text_match_);
Tomasz Wiszkowskie23c56b2020-09-24 20:08:35483
484 // Short-circuit in case we have no MOST_VISITED urls to show.
485 if (most_visited_urls_.empty())
486 return;
487
488 if (base::FeatureList::IsEnabled(omnibox::kMostVisitedTiles)) {
489 AutocompleteMatch match =
490 NavigationToMatch(SearchSuggestionParser::NavigationResult(
491 client()->GetSchemeClassifier(), GURL::EmptyGURL(),
492 AutocompleteMatchType::TILE_NAVSUGGEST, {}, base::string16(),
493 std::string(), false, kMostVisitedTilesRelevance, true,
494 base::ASCIIToUTF16(current_query_)));
495 match.navsuggest_tiles.reserve(most_visited_urls_.size());
496
497 for (const auto& url : most_visited_urls_) {
498 match.navsuggest_tiles.push_back({url.url, url.title});
499 }
500 matches_.push_back(std::move(match));
501 } else {
502 int relevance = 600;
503 for (const auto& url : most_visited_urls_) {
504 SearchSuggestionParser::NavigationResult nav(
505 client()->GetSchemeClassifier(), url.url,
506 AutocompleteMatchType::NAVSUGGEST, {}, url.title, std::string(),
507 false, relevance, true, base::ASCIIToUTF16(current_query_));
508 matches_.push_back(NavigationToMatch(nav));
509 --relevance;
510 }
[email protected]8f064e52013-09-18 01:17:14511 }
512 return;
513 }
514
[email protected]9c97f89c2013-06-25 03:12:16515 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01516 return;
517
Tommy Li0731aee2020-07-22 23:04:32518#if defined(OS_ANDROID) || defined(OS_IOS)
519 // Android needs the verbatim match on non-NTP surfaces to properly present
520 // the Search Ready Omnibox URL edit widget. Desktop specifically does NOT
521 // want to show verbatim matches in remotely-fetched ZeroSuggest anymore.
522 // iOS we are keeping the same as Android for now. No strong reason to change.
Tomasz Wiszkowski57492ab2019-05-29 21:28:27523 if (!IsNTPPage(current_page_classification_) &&
Tommy Liec99def2019-10-14 22:37:23524 current_text_match_.destination_url.is_valid()) {
525 matches_.push_back(current_text_match_);
Tomasz Wiszkowski57492ab2019-05-29 21:28:27526 }
Tommy Li0731aee2020-07-22 23:04:32527#endif
Tomasz Wiszkowski57492ab2019-05-29 21:28:27528
[email protected]00404742014-02-20 13:09:05529 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01530 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01531
[email protected]0b9575f2014-07-30 11:58:37532 const SearchSuggestionParser::NavigationResults& nav_results(
533 results_.navigation_results);
Tomasz Wiszkowskif2464a82020-02-18 22:42:25534 for (const auto& nav_result : nav_results) {
535 matches_.push_back(NavigationToMatch(nav_result));
gcomanici67d53ac2017-04-01 17:07:19536 }
[email protected]6ce7f612012-09-05 23:53:07537}
538
Tommy Liec99def2019-10-14 22:37:23539AutocompleteMatch ZeroSuggestProvider::MatchForCurrentText() {
[email protected]bb1fb2b2013-05-31 00:21:01540 // The placeholder suggestion for the current URL has high relevance so
541 // that it is in the first suggestion slot and inline autocompleted. It
542 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11543 AutocompleteInput tmp(GetInput(false));
544 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54545 const base::string16 description =
546 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
547 ? current_title_
548 : base::string16();
Tommy Liec99def2019-10-14 22:37:23549
550 // We pass a nullptr as the |history_url_provider| parameter now to force
551 // VerbatimMatch to do a classification, since the text can be a search query.
552 // TODO(tommycli): Simplify this - probably just bypass VerbatimMatchForURL.
553 AutocompleteMatch match = VerbatimMatchForURL(
554 client(), tmp, GURL(current_query_), description,
555 /*history_url_provider=*/nullptr, results_.verbatim_relevance);
556 match.provider = this;
557 return match;
[email protected]00404742014-02-20 13:09:05558}
[email protected]162c8d9fa2014-03-18 20:25:41559
Gheorghe Comanici9a364f572018-01-24 17:22:00560bool ZeroSuggestProvider::AllowZeroSuggestSuggestions(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27561 const AutocompleteInput& input) const {
562 const auto& page_url = input.current_url();
563 const auto page_class = input.current_page_classification();
564 const auto input_type = input.type();
Peter Kastingfb1a8ea2017-11-28 02:26:50565
Tommy C. Li17b5ba112020-08-11 02:29:21566 if (input.focus_type() == OmniboxFocusType::DEFAULT)
Tomasz Wiszkowski57492ab2019-05-29 21:28:27567 return false;
568
Tommy Li8bc9cd62020-08-18 02:37:28569 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41570 return false;
571
Tommy Li19e27ede2020-08-14 22:52:40572 if (input_type == metrics::OmniboxInputType::EMPTY) {
573 // Function that returns whether EMPTY input zero-suggest is allowed.
574 auto IsEmptyZeroSuggestAllowed = [&]() {
575 if (page_class == metrics::OmniboxEventProto::CHROMEOS_APP_LIST ||
576 IsNTPPage(page_class)) {
577 return true;
578 }
579
580 if (page_class == metrics::OmniboxEventProto::OTHER) {
581 return input.focus_type() == OmniboxFocusType::DELETED_PERMANENT_TEXT &&
582 base::FeatureList::IsEnabled(
583 omnibox::kClobberTriggersContextualWebZeroSuggest);
584 }
585
586 return false;
587 };
588
589 // Return false if disallowed. Otherwise, proceed down to further checks.
590 if (!IsEmptyZeroSuggestAllowed())
591 return false;
[email protected]5b899dc2018-10-02 09:20:01592 }
593
Tomasz Wiszkowski57492ab2019-05-29 21:28:27594 // When omnibox contains pre-populated content, only show zero suggest for
595 // pages with URLs the user will recognize.
596 //
Mark Pearson3fb0e3162018-08-27 21:53:57597 // This list intentionally does not include items such as ftp: and file:
Tommy C. Li06ec26a2019-06-10 18:01:42598 // because (a) these do not work on Android and iOS, where most visited
Mark Pearson3fb0e3162018-08-27 21:53:57599 // zero suggest is launched and (b) on desktop, where contextual zero suggest
600 // is running, these types of schemes aren't eligible to be sent to the
601 // server to ask for suggestions (and thus in practice we won't display zero
602 // suggest for them).
Yue Ru Sune31e1572019-07-19 17:09:57603 if (input_type != metrics::OmniboxInputType::EMPTY &&
Tomasz Wiszkowski57492ab2019-05-29 21:28:27604 !(page_url.is_valid() &&
605 ((page_url.scheme() == url::kHttpScheme) ||
606 (page_url.scheme() == url::kHttpsScheme) ||
607 (page_url.scheme() == url::kAboutScheme) ||
608 (page_url.scheme() ==
609 client()->GetEmbedderRepresentationOfAboutScheme())))) {
[email protected]162c8d9fa2014-03-18 20:25:41610 return false;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27611 }
[email protected]162c8d9fa2014-03-18 20:25:41612
613 return true;
614}
[email protected]855ebff2014-05-09 07:14:38615
616void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Tommy C. Lid77691a2019-06-05 02:07:46617 if (result_type_running_ != REMOTE_NO_URL)
[email protected]855ebff2014-05-09 07:14:38618 return;
619
blundelld130d592015-06-21 19:29:13620 std::string json_data =
621 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38622 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57623 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14624 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00625 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
626 &results_))
[email protected]855ebff2014-05-09 07:14:38627 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38628 }
629}
Gheorghe Comanici9a364f572018-01-24 17:22:00630
Tommy Li26a9f582020-08-18 23:59:55631// static
Gheorghe Comanici9a364f572018-01-24 17:22:00632ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
Tommy Li26a9f582020-08-18 23:59:55633 AutocompleteProviderClient* client,
634 const AutocompleteInput& input,
Gheorghe Comanici9a364f572018-01-24 17:22:00635 const GURL& suggest_url) {
Tommy Li26a9f582020-08-18 23:59:55636 DCHECK(client);
Gheorghe Comanici9a364f572018-01-24 17:22:00637 // Check if the URL can be sent in any suggest request.
638 const TemplateURLService* template_url_service =
Tommy Li26a9f582020-08-18 23:59:55639 client->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49640 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00641 const TemplateURL* default_provider =
642 template_url_service->GetDefaultSearchProvider();
Tommy Li26a9f582020-08-18 23:59:55643
644 GURL current_url = input.current_url();
645 metrics::OmniboxEventProto::PageClassification current_page_classification =
646 input.current_page_classification();
647
Gheorghe Comanici9a364f572018-01-24 17:22:00648 const bool can_send_current_url = CanSendURL(
Tommy Li26a9f582020-08-18 23:59:55649 current_url, suggest_url, default_provider, current_page_classification,
650 template_url_service->search_terms_data(), client, false);
Gheorghe Comanici9a364f572018-01-24 17:22:00651 // Collect metrics on eligibility.
652 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
653 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
654 if (!can_send_current_url) {
655 const bool can_send_ordinary_url =
656 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
Tommy Li26a9f582020-08-18 23:59:55657 current_page_classification,
658 template_url_service->search_terms_data(), client, false);
Gheorghe Comanici9a364f572018-01-24 17:22:00659 eligibility = can_send_ordinary_url
660 ? ZeroSuggestEligibility::URL_INELIGIBLE
661 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
662 }
663 UMA_HISTOGRAM_ENUMERATION(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27664 kOmniboxZeroSuggestEligibleHistogramName, static_cast<int>(eligibility),
Gheorghe Comanici9a364f572018-01-24 17:22:00665 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
666
Moe Ahmadi0993aec2019-09-19 21:42:30667 const auto field_trial_variants =
Tommy Li26a9f582020-08-18 23:59:55668 OmniboxFieldTrial::GetZeroSuggestVariants(current_page_classification);
Tommy C. Li0af09562019-06-11 23:58:51669
Stepan Khapugin5187a462020-01-07 13:16:26670 if (base::Contains(field_trial_variants, kNoneVariant))
Kevin Bailey5fea4322018-03-21 22:36:05671 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00672
Tommy Li26a9f582020-08-18 23:59:55673 if (current_page_classification == OmniboxEventProto::CHROMEOS_APP_LIST)
Tommy C. Lid77691a2019-06-05 02:07:46674 return REMOTE_NO_URL;
Jenny Zhang5bc2e3d2018-09-10 18:50:55675
Tommy Lie0a3a992020-05-29 21:32:05676 // Contextual Open Web - (same client side behavior for multiple variants).
Tommy Li26a9f582020-08-18 23:59:55677 if (current_page_classification == OmniboxEventProto::OTHER &&
Tommy Li31e27a632020-08-20 03:06:51678 can_send_current_url) {
679 if (input.focus_type() == OmniboxFocusType::ON_FOCUS &&
680 (base::FeatureList::IsEnabled(
681 omnibox::kOnFocusSuggestionsContextualWeb) ||
682 base::FeatureList::IsEnabled(
683 omnibox::kOnFocusSuggestionsContextualWebOnContent))) {
684 return REMOTE_SEND_URL;
685 }
686
687 if (input.focus_type() == OmniboxFocusType::DELETED_PERMANENT_TEXT &&
688 base::FeatureList::IsEnabled(
689 omnibox::kClobberTriggersContextualWebZeroSuggest)) {
690 return REMOTE_SEND_URL;
691 }
Tommy Li55784022020-04-28 20:58:18692 }
693
Moe Ahmadi31147bc2020-06-02 19:07:56694 // Reactive Zero-Prefix Suggestions (rZPS) on NTP cases.
Moe Ahmadi189b6532020-09-02 17:57:12695 bool check_authentication_state = !base::FeatureList::IsEnabled(
696 omnibox::kOmniboxTrendingZeroPrefixSuggestionsOnNTP);
697 bool remote_no_url_allowed = RemoteNoUrlSuggestionsAreAllowed(
698 client, template_url_service, check_authentication_state);
Tommy Lib4b3bb5b2020-05-01 02:34:56699 if (remote_no_url_allowed) {
700 // NTP Omnibox.
Tommy Li26a9f582020-08-18 23:59:55701 if ((current_page_classification == OmniboxEventProto::NTP ||
702 current_page_classification ==
Tommy Lib4b3bb5b2020-05-01 02:34:56703 OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS) &&
704 base::FeatureList::IsEnabled(
Moe Ahmadi31147bc2020-06-02 19:07:56705 omnibox::kReactiveZeroSuggestionsOnNTPOmnibox)) {
Tommy Lib4b3bb5b2020-05-01 02:34:56706 return REMOTE_NO_URL;
707 }
708 // NTP Realbox.
Tommy Li26a9f582020-08-18 23:59:55709 if (current_page_classification == OmniboxEventProto::NTP_REALBOX &&
Tommy Lib4b3bb5b2020-05-01 02:34:56710 base::FeatureList::IsEnabled(
Moe Ahmadi31147bc2020-06-02 19:07:56711 omnibox::kReactiveZeroSuggestionsOnNTPRealbox)) {
Tommy Lib4b3bb5b2020-05-01 02:34:56712 return REMOTE_NO_URL;
713 }
714 }
715
Tommy Li0801a8b2020-05-30 00:28:55716 if (base::Contains(field_trial_variants, kRemoteNoUrlVariant) &&
717 remote_no_url_allowed) {
718 return REMOTE_NO_URL;
Tommy C. Li393c1b142019-05-22 00:28:41719 }
Gheorghe Comanici9a364f572018-01-24 17:22:00720
Moe Ahmadi0993aec2019-09-19 21:42:30721 if (base::Contains(field_trial_variants, kRemoteSendUrlVariant) &&
722 can_send_current_url)
Tommy C. Li0af09562019-06-11 23:58:51723 return REMOTE_SEND_URL;
Gheorghe Comanici9a364f572018-01-24 17:22:00724
Moe Ahmadi0993aec2019-09-19 21:42:30725 if (base::Contains(field_trial_variants, kMostVisitedVariant))
Tommy C. Li0af09562019-06-11 23:58:51726 return MOST_VISITED;
727
Moe Ahmadi8ef94b72020-08-13 23:18:15728#if !defined(OS_IOS)
729 // For Desktop and Android, default to REMOTE_NO_URL on the NTP, if allowed.
Tommy Li26a9f582020-08-18 23:59:55730 if (IsNTPPage(current_page_classification) && remote_no_url_allowed)
Tommy Li0801a8b2020-05-30 00:28:55731 return REMOTE_NO_URL;
732#endif
733
Tommy C. Li0af09562019-06-11 23:58:51734#if defined(OS_ANDROID) || defined(OS_IOS)
Moe Ahmadi8ef94b72020-08-13 23:18:15735 // For Android and iOS, default to MOST_VISITED everywhere except on the SERP.
Tommy Li26a9f582020-08-18 23:59:55736 if (!IsSearchResultsPage(current_page_classification)) {
Kevin Bailey5fea4322018-03-21 22:36:05737 return MOST_VISITED;
Tommy C. Li393c1b142019-05-22 00:28:41738 }
Tommy C. Li0af09562019-06-11 23:58:51739#endif
Tommy C. Lic79d39ab2019-06-04 16:58:56740
741 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00742}