blob: 87e5315819306a8a199778c940725030c25194e9 [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"
Gheorghe Comanici864725b2017-11-28 21:48:0143#include "components/search_engines/search_engine_type.h"
[email protected]bf5c532d2014-07-05 00:29:5344#include "components/search_engines/template_url_service.h"
rsleevi24f64dc22015-08-07 21:39:2145#include "components/url_formatter/url_formatter.h"
asvitkine9a279832015-12-18 02:35:5046#include "components/variations/net/variations_http_headers.h"
[email protected]bb1fb2b2013-05-31 00:21:0147#include "net/base/escape.h"
Maks Orlovich1b208512018-06-13 21:08:1748#include "services/network/public/cpp/shared_url_loader_factory.h"
49#include "services/network/public/cpp/simple_url_loader.h"
Steven Holtef9d5ed62017-10-21 02:02:3050#include "third_party/metrics_proto/omnibox_event.pb.h"
51#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]761fa4702013-07-02 15:25:1552#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0753
Tommy C. Li0af09562019-06-11 23:58:5154using metrics::OmniboxEventProto;
55
[email protected]6ce7f612012-09-05 23:53:0756namespace {
[email protected]bb1fb2b2013-05-31 00:21:0157
mpearson3d89cdc2017-03-03 21:15:4558// Represents whether ZeroSuggestProvider is allowed to display contextual
59// suggestions on focus, and if not, why not.
60// These values are written to logs. New enum values can be added, but existing
61// enums must never be renumbered or deleted and reused.
62enum class ZeroSuggestEligibility {
63 ELIGIBLE = 0,
64 // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL
65 // itself prevents ZeroSuggest from triggering.
66 URL_INELIGIBLE = 1,
67 GENERALLY_INELIGIBLE = 2,
68 ELIGIBLE_MAX_VALUE
69};
70
Tomasz Wiszkowskifd790c02019-08-29 17:31:2971// Histogram values describing client eligibility to receive RemoteNoURL
72// suggestions on NTP.
73// These values are persisted to logs. New values can be added, but existing
74// enums must never be renumbered or deleted and reused.
75enum class ZeroSuggestEligibilityForRemoteNoURL {
76 kEligible = 0,
77 kIneligibleUserOffTheRecord = 1,
78 kIneligibleSuggestionsDisabled = 2,
79 kIneligibleUserNotAuthenticated = 3,
80 // Used to report users ineligible for RemoteNoURL suggestions when the
81 // search and suggest server of their choice cannot be used to offer the
82 // RemoteNoURL suggestions.
83 kIneligibleWithUserSelectedServer = 4,
84 kIneligibleUserNotParticipating = 5,
85
86 kMaxValue = kIneligibleUserNotParticipating
87};
88
[email protected]bb1fb2b2013-05-31 00:21:0189// TODO(hfung): The histogram code was copied and modified from
90// search_provider.cc. Refactor and consolidate the code.
91// We keep track in a histogram how many suggest requests we send, how
92// many suggest requests we invalidate (e.g., due to a user typing
93// another character), and how many replies we receive.
mpearson3d89cdc2017-03-03 21:15:4594// These values are written to logs. New enum values can be added, but existing
95// enums must never be renumbered or deleted and reused.
[email protected]bb1fb2b2013-05-31 00:21:0196enum ZeroSuggestRequestsHistogramValue {
97 ZERO_SUGGEST_REQUEST_SENT = 1,
mpearson3d89cdc2017-03-03 21:15:4598 ZERO_SUGGEST_REQUEST_INVALIDATED = 2,
99 ZERO_SUGGEST_REPLY_RECEIVED = 3,
[email protected]bb1fb2b2013-05-31 00:21:01100 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
101};
102
103void LogOmniboxZeroSuggestRequest(
104 ZeroSuggestRequestsHistogramValue request_value) {
105 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
106 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
107}
108
Tomasz Wiszkowskifd790c02019-08-29 17:31:29109// Record user eligibility for RemoteNoUrl suggestions for supplied page class.
110// The |histogram_variant| is used to specify particular variant of the
111// Omnibox.ZeroSuggest.Eligible.RemoteNoUrl histogram that should be updated.
112void LogOmniboxRemoteNoUrlEligibilityOnNTP(
113 OmniboxEventProto::PageClassification page_class,
114 bool log_for_profile_open,
115 AutocompleteProviderClient* client) {
116 ZeroSuggestEligibilityForRemoteNoURL value =
117 ZeroSuggestEligibilityForRemoteNoURL::kEligible;
118
119 auto* service = client->GetTemplateURLService();
120 auto* provider = service ? service->GetDefaultSearchProvider() : nullptr;
121 auto engine = provider ? provider->GetEngineType(service->search_terms_data())
122 : SEARCH_ENGINE_UNKNOWN;
Moe Ahmadi0993aec2019-09-19 21:42:30123 const auto variants = OmniboxFieldTrial::GetZeroSuggestVariants(page_class);
Tomasz Wiszkowskifd790c02019-08-29 17:31:29124
Moe Ahmadi0993aec2019-09-19 21:42:30125 if (!base::Contains(variants, ZeroSuggestProvider::kRemoteNoUrlVariant)) {
Tomasz Wiszkowskifd790c02019-08-29 17:31:29126 value =
127 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserNotParticipating;
128 } else if (client->IsOffTheRecord()) {
129 value = ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserOffTheRecord;
130 } else if (!client->SearchSuggestEnabled()) {
131 value =
132 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleSuggestionsDisabled;
133 } else if (!client->IsAuthenticated()) {
134 value =
135 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserNotAuthenticated;
136 } else if (service == nullptr || provider == nullptr ||
137 engine != SEARCH_ENGINE_GOOGLE) {
138 value =
139 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleWithUserSelectedServer;
140 }
141
142 if (log_for_profile_open) {
143 UMA_HISTOGRAM_ENUMERATION(
144 "Omnibox.ZeroSuggest.Eligible.RemoteNoUrl.OnNTP.OnProfileOpen", value);
145 } else {
146 UMA_HISTOGRAM_ENUMERATION(
147 "Omnibox.ZeroSuggest.Eligible.RemoteNoUrl.OnNTP.OnFocus", value);
148 }
149}
150
[email protected]bb1fb2b2013-05-31 00:21:01151// Relevance value to use if it was not set explicitly by the server.
152const int kDefaultZeroSuggestRelevance = 100;
153
mpearson3d89cdc2017-03-03 21:15:45154// Used for testing whether zero suggest is ever available.
mpearsonc90c24b2017-03-04 00:11:26155constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/";
mpearson3d89cdc2017-03-03 21:15:45156
Tomasz Wiszkowski57492ab2019-05-29 21:28:27157// Metric name tracking the omnibox suggestion eligibility.
158constexpr char kOmniboxZeroSuggestEligibleHistogramName[] =
159 "Omnibox.ZeroSuggest.Eligible.OnFocusV2";
160
Moe Ahmadi85bf70862019-10-11 00:03:32161// Remote suggestions are allowed only if the user is signed-in and has Google
162// set up as their default search engine. This only applies to
163// kRemoteNoUrlVariant since most of these checks are done in
164// BaseSearchProvider::CanSendURL (with the exception of the authentication
165// state) which applies to kRemoteSendUrlVariant.
166bool RemoteNoUrlSuggestionsAreAllowed(
Tomasz Wiszkowski5abea202019-06-06 22:51:14167 AutocompleteProviderClient* client,
Gheorghe Comanici864725b2017-11-28 21:48:01168 const TemplateURLService* template_url_service) {
Tomasz Wiszkowski5abea202019-06-06 22:51:14169 if (!client->SearchSuggestEnabled())
Moe Ahmadi85bf70862019-10-11 00:03:32170 return false;
Tomasz Wiszkowski5abea202019-06-06 22:51:14171
172 if (!client->IsAuthenticated())
Moe Ahmadi85bf70862019-10-11 00:03:32173 return false;
Gheorghe Comanici864725b2017-11-28 21:48:01174
Gheorghe Comanici9a364f572018-01-24 17:22:00175 if (template_url_service == nullptr)
176 return false;
Gheorghe Comanici682504102017-11-30 17:47:25177
Gheorghe Comanici9a364f572018-01-24 17:22:00178 const TemplateURL* default_provider =
179 template_url_service->GetDefaultSearchProvider();
Moe Ahmadi85bf70862019-10-11 00:03:32180 return default_provider &&
Gheorghe Comanici9a364f572018-01-24 17:22:00181 default_provider->GetEngineType(
Moe Ahmadi85bf70862019-10-11 00:03:32182 template_url_service->search_terms_data()) == SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01183}
184
[email protected]6ce7f612012-09-05 23:53:07185} // namespace
186
[email protected]a00008d42012-09-15 05:07:58187// static
Tommy C. Li0af09562019-06-11 23:58:51188const char ZeroSuggestProvider::kNoneVariant[] = "None";
189const char ZeroSuggestProvider::kRemoteNoUrlVariant[] = "RemoteNoUrl";
190const char ZeroSuggestProvider::kRemoteSendUrlVariant[] = "RemoteSendUrl";
191const char ZeroSuggestProvider::kMostVisitedVariant[] = "MostVisited";
192
193// static
[email protected]a00008d42012-09-15 05:07:58194ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18195 AutocompleteProviderClient* client,
blundelld130d592015-06-21 19:29:13196 AutocompleteProviderListener* listener) {
Tommy Liec99def2019-10-14 22:37:23197 return new ZeroSuggestProvider(client, listener);
[email protected]6ce7f612012-09-05 23:53:07198}
199
[email protected]855ebff2014-05-09 07:14:38200// static
Moe Ahmadi1266de82020-04-10 17:19:53201void ZeroSuggestProvider::RegisterProfilePrefs(PrefRegistrySimple* registry) {
blundelld130d592015-06-21 19:29:13202 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
203 std::string());
[email protected]855ebff2014-05-09 07:14:38204}
205
[email protected]6ce7f612012-09-05 23:53:07206void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18207 bool minimal_changes) {
a-v-ydd768d52016-03-25 21:07:46208 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54209 matches_.clear();
Kevin Bailey8642e612018-04-23 20:27:54210 Stop(true, false);
Jenny Zhang5bc2e3d2018-09-10 18:50:55211
Tomasz Wiszkowskifd790c02019-08-29 17:31:29212 current_page_classification_ = input.current_page_classification();
213
214 if (input.from_omnibox_focus() && IsNTPPage(current_page_classification_)) {
215 LogOmniboxRemoteNoUrlEligibilityOnNTP(current_page_classification_, false,
216 client());
217 }
218
Tomasz Wiszkowski57492ab2019-05-29 21:28:27219 if (!AllowZeroSuggestSuggestions(input)) {
220 UMA_HISTOGRAM_ENUMERATION(kOmniboxZeroSuggestEligibleHistogramName,
221 ZeroSuggestEligibility::GENERALLY_INELIGIBLE,
222 ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE);
[email protected]f030c4d2014-03-25 01:05:54223 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27224 }
[email protected]bb1fb2b2013-05-31 00:21:01225
Kevin Bailey5fea4322018-03-21 22:36:05226 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13227 set_field_trial_triggered(false);
228 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54229 permanent_text_ = input.text();
230 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54231 current_title_ = input.current_title();
Tommy Liec99def2019-10-14 22:37:23232 current_text_match_ = MatchForCurrentText();
[email protected]9b9fa672013-11-07 06:04:52233
Tommy C. Li6aee62d52019-06-27 20:25:00234 TemplateURLRef::SearchTermsArgs search_terms_args;
235 search_terms_args.page_classification = current_page_classification_;
236 search_terms_args.omnibox_focus_type =
237 TemplateURLRef::SearchTermsArgs::OmniboxFocusType::ON_FOCUS;
Tommy C. Li06ec26a2019-06-10 18:01:42238 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Tommy C. Li6aee62d52019-06-27 20:25:00239 search_terms_args, client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41240 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07241 return;
[email protected]162c8d9fa2014-03-18 20:25:41242
Gheorghe Comanici9a364f572018-01-24 17:22:00243 result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url);
Kevin Bailey5fea4322018-03-21 22:36:05244 if (result_type_running_ == NONE)
[email protected]162c8d9fa2014-03-18 20:25:41245 return;
[email protected]162c8d9fa2014-03-18 20:25:41246
[email protected]6ce7f612012-09-05 23:53:07247 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11248
[email protected]855ebff2014-05-09 07:14:38249 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11250
Kevin Bailey5fea4322018-03-21 22:36:05251 if (result_type_running_ == MOST_VISITED) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11252 most_visited_urls_.clear();
253 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
Gheorghe Comanici9a364f572018-01-24 17:22:00254 if (!ts) {
255 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05256 result_type_running_ = NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00257 return;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11258 }
Gheorghe Comanici9a364f572018-01-24 17:22:00259
Kevin Bailey40bb0ac52019-04-02 00:43:32260 ts->GetMostVisitedURLs(base::BindRepeating(
261 &ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
262 weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11263 return;
264 }
265
Tommy C. Li6aee62d52019-06-27 20:25:00266 search_terms_args.current_page_url =
Tommy C. Lid77691a2019-06-05 02:07:46267 result_type_running_ == REMOTE_SEND_URL ? current_query_ : std::string();
Maks Orlovich1b208512018-06-13 21:08:17268 // Create a request for suggestions, routing completion to
Tommy C. Li06ec26a2019-06-10 18:01:42269 // OnRemoteSuggestionsLoaderAvailable.
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11270 client()
Tommy C. Li06ec26a2019-06-10 18:01:42271 ->GetRemoteSuggestionsService(/*create_if_necessary=*/true)
272 ->CreateSuggestionsRequest(
Tommy Li3fe83912019-08-23 17:33:11273 search_terms_args, client()->GetTemplateURLService(),
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11274 base::BindOnce(
Tommy C. Li06ec26a2019-06-10 18:01:42275 &ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable,
Maks Orlovich1b208512018-06-13 21:08:17276 weak_ptr_factory_.GetWeakPtr()),
277 base::BindOnce(
278 &ZeroSuggestProvider::OnURLLoadComplete,
279 base::Unretained(this) /* this owns SimpleURLLoader */));
[email protected]6ce7f612012-09-05 23:53:07280}
281
mpearson8a37c382015-03-07 05:58:57282void ZeroSuggestProvider::Stop(bool clear_cached_results,
283 bool due_to_user_inactivity) {
Maks Orlovich1b208512018-06-13 21:08:17284 if (loader_)
[email protected]ec3f679b2014-08-18 07:45:13285 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
Maks Orlovich1b208512018-06-13 21:08:17286 loader_.reset();
Tommy Li3fe83912019-08-23 17:33:11287
Kevin Bailey5fea4322018-03-21 22:36:05288 // TODO(krb): It would allow us to remove some guards if we could also cancel
289 // the TopSites::GetMostVisitedURLs request.
[email protected]ec3f679b2014-08-18 07:45:13290 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05291 result_type_running_ = NONE;
292 ++most_visited_request_num_;
[email protected]ec3f679b2014-08-18 07:45:13293
294 if (clear_cached_results) {
295 // We do not call Clear() on |results_| to retain |verbatim_relevance|
296 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18297 // beginning of the next call to Start() to determine the current url
298 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13299 results_.suggest_results.clear();
300 results_.navigation_results.clear();
Moe Ahmadib8a1daf2020-02-14 22:23:25301 results_.experiment_stats.clear();
Moe Ahmadid2704a7a2020-03-31 22:04:26302 results_.headers_map.clear();
[email protected]ec3f679b2014-08-18 07:45:13303 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54304 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45305 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13306 }
307}
308
[email protected]855ebff2014-05-09 07:14:38309void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Moe Ahmadi0993aec2019-09-19 21:42:30310 if (base::Contains(OmniboxFieldTrial::GetZeroSuggestVariants(
311 current_page_classification_),
312 kRemoteNoUrlVariant)) {
[email protected]855ebff2014-05-09 07:14:38313 // Remove the deleted match from the cache, so it is not shown to the user
314 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13315 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38316 std::string());
317 }
318 BaseSearchProvider::DeleteMatch(match);
319}
320
[email protected]ec3f679b2014-08-18 07:45:13321void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
322 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45323 if (!results_.suggest_results.empty() ||
324 !results_.navigation_results.empty() ||
325 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13326 provider_info->back().set_times_returned_results_in_session(1);
327}
328
[email protected]f030c4d2014-03-25 01:05:54329void ZeroSuggestProvider::ResetSession() {
330 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13331 // |field_trial_triggered_in_session| unchanged and set
332 // |field_trial_triggered| to false since zero suggest is inactive now.
333 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54334}
335
mpearson931028c2016-07-01 18:55:11336ZeroSuggestProvider::ZeroSuggestProvider(
337 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11338 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13339 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
[email protected]776ee5902014-08-11 09:15:19340 listener_(listener),
Jeremy Roman5c341f6d2019-07-15 15:56:10341 result_type_running_(NONE) {
Tommy C. Li06ec26a2019-06-10 18:01:42342 // Record whether remote zero suggest is possible for this user / profile.
mpearson3d89cdc2017-03-03 21:15:45343 const TemplateURLService* template_url_service =
344 client->GetTemplateURLService();
345 // Template URL service can be null in tests.
346 if (template_url_service != nullptr) {
Tommy C. Li06ec26a2019-06-10 18:01:42347 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Tommy C. Li6aee62d52019-06-27 20:25:00348 TemplateURLRef::SearchTermsArgs(), template_url_service);
mpearson3d89cdc2017-03-03 21:15:45349 // To check whether this is allowed, use an arbitrary insecure (http) URL
350 // as the URL we'd want suggestions for. The value of OTHER as the current
351 // page classification is to correspond with that URL.
352 UMA_HISTOGRAM_BOOLEAN(
353 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
354 suggest_url.is_valid() &&
355 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
356 template_url_service->GetDefaultSearchProvider(),
357 metrics::OmniboxEventProto::OTHER,
Tommy C. Li71171ee2019-06-20 17:11:47358 template_url_service->search_terms_data(), client,
359 false));
Tomasz Wiszkowskifd790c02019-08-29 17:31:29360
361 LogOmniboxRemoteNoUrlEligibilityOnNTP(
362 OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, true,
363 client);
mpearson3d89cdc2017-03-03 21:15:45364 }
[email protected]6ce7f612012-09-05 23:53:07365}
366
Tomasz Wiszkowskif2464a82020-02-18 22:42:25367ZeroSuggestProvider::~ZeroSuggestProvider() = default;
[email protected]6ce7f612012-09-05 23:53:07368
[email protected]776ee5902014-08-11 09:15:19369const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
370 // Zero suggest provider should not receive keyword results.
371 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13372 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19373}
374
375const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18376 // The callers of this method won't look at the AutocompleteInput's
377 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02378 AutocompleteInput input(base::string16(), current_page_classification_,
379 client()->GetSchemeClassifier());
380 input.set_current_url(GURL(current_query_));
381 input.set_current_title(current_title_);
382 input.set_prevent_inline_autocomplete(true);
383 input.set_allow_exact_keyword_match(false);
384 return input;
[email protected]776ee5902014-08-11 09:15:19385}
386
387bool ZeroSuggestProvider::ShouldAppendExtraParams(
388 const SearchSuggestionParser::SuggestResult& result) const {
389 // We always use the default provider for search, so append the params.
390 return true;
391}
392
[email protected]776ee5902014-08-11 09:15:19393void ZeroSuggestProvider::RecordDeletionResult(bool success) {
394 if (success) {
395 base::RecordAction(
396 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
397 } else {
398 base::RecordAction(
399 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
400 }
401}
402
Maks Orlovich1b208512018-06-13 21:08:17403void ZeroSuggestProvider::OnURLLoadComplete(
404 const network::SimpleURLLoader* source,
405 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19406 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17407 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19408
409 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
410
Gheorghe Comanici9a364f572018-01-24 17:22:00411 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17412 response_body && source->NetError() == net::OK &&
413 (source->ResponseInfo() && source->ResponseInfo()->headers &&
414 source->ResponseInfo()->headers->response_code() == 200) &&
415 UpdateResults(SearchSuggestionParser::ExtractJsonData(
416 source, std::move(response_body)));
417 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19418 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05419 result_type_running_ = NONE;
420 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19421 listener_->OnProviderUpdate(results_updated);
422}
423
Gheorghe Comanici9a364f572018-01-24 17:22:00424bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
425 std::unique_ptr<base::Value> data(
426 SearchSuggestionParser::DeserializeJsonData(json_data));
427 if (!data)
[email protected]855ebff2014-05-09 07:14:38428 return false;
429
Tommy C. Li467c1bcaa2019-06-20 20:48:05430 // When running the REMOTE_NO_URL variant, we want to store suggestion
Gheorghe Comanici9a364f572018-01-24 17:22:00431 // responses if non-empty.
Tommy C. Lid77691a2019-06-05 02:07:46432 if (result_type_running_ == REMOTE_NO_URL && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00433 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
434 json_data);
[email protected]855ebff2014-05-09 07:14:38435
Gheorghe Comanici9a364f572018-01-24 17:22:00436 // If we received an empty result list, we should update the display, as it
437 // may be showing cached results that should not be shown.
438 const base::ListValue* root_list = nullptr;
439 const base::ListValue* results_list = nullptr;
440 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
441 root_list->GetList(1, &results_list) &&
442 !results_list->empty();
443 const bool non_empty_cache = !results_.suggest_results.empty() ||
444 !results_.navigation_results.empty();
445 if (non_empty_parsed_list && non_empty_cache)
446 return false;
447 }
448 const bool results_updated = ParseSuggestResults(
449 *data, kDefaultZeroSuggestRelevance, false, &results_);
450 ConvertResultsToAutocompleteMatches();
451 return results_updated;
[email protected]855ebff2014-05-09 07:14:38452}
453
[email protected]bb1fb2b2013-05-31 00:21:01454AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37455 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01456 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47457 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01458 match.destination_url = navigation.url();
459
[email protected]bb1fb2b2013-05-31 00:21:01460 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13461 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26462 navigation.url(), url_formatter::FormatUrl(navigation.url()),
Kevin Bailey83e643d2018-03-08 16:01:41463 client()->GetSchemeClassifier(), nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01464
manuk3972b2f2019-04-19 14:22:56465 // Zero suggest results should always omit protocols and never appear bold.
466 auto format_types = AutocompleteMatch::GetFormatTypes(false, false);
467 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
468 net::UnescapeRule::SPACES, nullptr,
469 nullptr, nullptr);
470 match.contents_class = ClassifyTermMatches({}, match.contents.length(), 0,
471 ACMatchClassification::URL);
[email protected]9c97f89c2013-06-25 03:12:16472
473 match.description =
474 AutocompleteMatch::SanitizeString(navigation.description());
manuk3972b2f2019-04-19 14:22:56475 match.description_class = ClassifyTermMatches({}, match.description.length(),
476 0, ACMatchClassification::NONE);
477
gcomanici67d53ac2017-04-01 17:07:19478 match.subtype_identifier = navigation.subtype_identifier();
[email protected]bb1fb2b2013-05-31 00:21:01479 return match;
480}
481
[email protected]8f064e52013-09-18 01:17:14482void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
Kevin Bailey5fea4322018-03-21 22:36:05483 size_t orig_request_num,
[email protected]8f064e52013-09-18 01:17:14484 const history::MostVisitedURLList& urls) {
Kevin Bailey5fea4322018-03-21 22:36:05485 if (result_type_running_ != MOST_VISITED ||
486 orig_request_num != most_visited_request_num_) {
Gheorghe Comanici9a364f572018-01-24 17:22:00487 return;
Kevin Bailey5fea4322018-03-21 22:36:05488 }
[email protected]8f064e52013-09-18 01:17:14489 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22490 done_ = true;
491 ConvertResultsToAutocompleteMatches();
Kevin Bailey5fea4322018-03-21 22:36:05492 result_type_running_ = NONE;
493 ++most_visited_request_num_;
mariakhomenkobfc3a2a2014-10-24 00:48:22494 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14495}
496
Tommy C. Li06ec26a2019-06-10 18:01:42497void ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable(
Maks Orlovich1b208512018-06-13 21:08:17498 std::unique_ptr<network::SimpleURLLoader> loader) {
Tommy C. Li06ec26a2019-06-10 18:01:42499 // RemoteSuggestionsService has already started |loader|, so here it's
Darwin Huang663c17d2020-02-06 01:10:01500 // only necessary to grab its ownership until results come in to
Maks Orlovich1b208512018-06-13 21:08:17501 // OnURLLoadComplete().
502 loader_ = std::move(loader);
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11503 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
504}
505
[email protected]9c97f89c2013-06-25 03:12:16506void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01507 matches_.clear();
508
blundelld130d592015-06-21 19:29:13509 TemplateURLService* template_url_service = client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49510 DCHECK(template_url_service);
[email protected]bb1fb2b2013-05-31 00:21:01511 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13512 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07513 // Fail if we can't set the clickthrough URL for query suggestions.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24514 if (default_provider == nullptr ||
blundelld130d592015-06-21 19:29:13515 !default_provider->SupportsReplacement(
516 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07517 return;
[email protected]6ce7f612012-09-05 23:53:07518
[email protected]00404742014-02-20 13:09:05519 MatchMap map;
Tommy Li823509e2019-12-06 22:42:47520
521 // Add all the SuggestResults to the map, re-classifying based on the
522 // permanent text as we go. This is to make ZeroSuggest results formatted in
523 // a congruent way with as-you-type search suggestions.
524 for (size_t i = 0; i < results_.suggest_results.size(); ++i) {
525 results_.suggest_results[i].ClassifyMatchContents(true, permanent_text_);
526
527 AddMatchToMap(results_.suggest_results[i], std::string(), i, false, false,
528 &map);
529 }
[email protected]00404742014-02-20 13:09:05530
531 const int num_query_results = map.size();
532 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01533 const int num_results = num_query_results + num_nav_results;
Steven Holte95922222018-09-14 20:06:23534 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.QueryResults", num_query_results);
535 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.URLResults", num_nav_results);
536 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01537
[email protected]8f064e52013-09-18 01:17:14538 // Show Most Visited results after ZeroSuggest response is received.
Kevin Bailey5fea4322018-03-21 22:36:05539 if (result_type_running_ == MOST_VISITED) {
Tomasz Wiszkowski57492ab2019-05-29 21:28:27540 // Ensure we don't show most visited URL suggestions on NTP.
541 // This allows us to prevent undesired side outcome of presenting
542 // URL suggestions to users who are not in the personalized field trial for
543 // zero query suggestions.
544 if (IsNTPPage(current_page_classification_) ||
Tommy Liec99def2019-10-14 22:37:23545 !current_text_match_.destination_url.is_valid()) {
[email protected]3feb8b002013-10-14 23:50:13546 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27547 }
Tommy Liec99def2019-10-14 22:37:23548 matches_.push_back(current_text_match_);
[email protected]8f064e52013-09-18 01:17:14549 int relevance = 600;
550 if (num_results > 0) {
Steven Holte95922222018-09-14 20:06:23551 UMA_HISTOGRAM_COUNTS_1M(
[email protected]8f064e52013-09-18 01:17:14552 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
553 most_visited_urls_.size());
554 }
[email protected]23db6492014-01-16 02:35:30555 const base::string16 current_query_string16(
556 base::ASCIIToUTF16(current_query_));
Tomasz Wiszkowskif2464a82020-02-18 22:42:25557 for (const auto& url : most_visited_urls_) {
[email protected]0b9575f2014-07-30 11:58:37558 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13559 client()->GetSchemeClassifier(), url.url,
gcomanici67d53ac2017-04-01 17:07:19560 AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false,
jshin1fb76462016-04-05 22:13:03561 relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14562 matches_.push_back(NavigationToMatch(nav));
563 --relevance;
564 }
565 return;
566 }
567
[email protected]9c97f89c2013-06-25 03:12:16568 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01569 return;
570
Tommy Liec99def2019-10-14 22:37:23571 // Do not add the default text match if we're on the NTP to prevent
Tomasz Wiszkowski57492ab2019-05-29 21:28:27572 // chrome-native://newtab or chrome://newtab from showing up on the list of
573 // suggestions.
574 if (!IsNTPPage(current_page_classification_) &&
Tommy Liec99def2019-10-14 22:37:23575 current_text_match_.destination_url.is_valid()) {
576 matches_.push_back(current_text_match_);
Tomasz Wiszkowski57492ab2019-05-29 21:28:27577 }
578
[email protected]00404742014-02-20 13:09:05579 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01580 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01581
[email protected]0b9575f2014-07-30 11:58:37582 const SearchSuggestionParser::NavigationResults& nav_results(
583 results_.navigation_results);
Tomasz Wiszkowskif2464a82020-02-18 22:42:25584 for (const auto& nav_result : nav_results) {
585 matches_.push_back(NavigationToMatch(nav_result));
gcomanici67d53ac2017-04-01 17:07:19586 }
[email protected]6ce7f612012-09-05 23:53:07587}
588
Tommy Liec99def2019-10-14 22:37:23589AutocompleteMatch ZeroSuggestProvider::MatchForCurrentText() {
[email protected]bb1fb2b2013-05-31 00:21:01590 // The placeholder suggestion for the current URL has high relevance so
591 // that it is in the first suggestion slot and inline autocompleted. It
592 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11593 AutocompleteInput tmp(GetInput(false));
594 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54595 const base::string16 description =
596 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
597 ? current_title_
598 : base::string16();
Tommy Liec99def2019-10-14 22:37:23599
600 // We pass a nullptr as the |history_url_provider| parameter now to force
601 // VerbatimMatch to do a classification, since the text can be a search query.
602 // TODO(tommycli): Simplify this - probably just bypass VerbatimMatchForURL.
603 AutocompleteMatch match = VerbatimMatchForURL(
604 client(), tmp, GURL(current_query_), description,
605 /*history_url_provider=*/nullptr, results_.verbatim_relevance);
606 match.provider = this;
607 return match;
[email protected]00404742014-02-20 13:09:05608}
[email protected]162c8d9fa2014-03-18 20:25:41609
Gheorghe Comanici9a364f572018-01-24 17:22:00610bool ZeroSuggestProvider::AllowZeroSuggestSuggestions(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27611 const AutocompleteInput& input) const {
612 const auto& page_url = input.current_url();
613 const auto page_class = input.current_page_classification();
614 const auto input_type = input.type();
Peter Kastingfb1a8ea2017-11-28 02:26:50615
Tomasz Wiszkowski57492ab2019-05-29 21:28:27616 if (!input.from_omnibox_focus())
617 return false;
618
Peter Kastingfb1a8ea2017-11-28 02:26:50619 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41620 return false;
621
Tomasz Wiszkowskia8780ff92019-07-25 21:46:05622 // When the omnibox is empty, only allow zero suggest for the ChromeOS
623 // Launcher and NTP.
Yue Ru Sune31e1572019-07-19 17:09:57624 if (input_type == metrics::OmniboxInputType::EMPTY &&
Tomasz Wiszkowski57492ab2019-05-29 21:28:27625 !(page_class == metrics::OmniboxEventProto::CHROMEOS_APP_LIST ||
Tomasz Wiszkowskia8780ff92019-07-25 21:46:05626 IsNTPPage(page_class))) {
[email protected]5b899dc2018-10-02 09:20:01627 return false;
628 }
629
Tomasz Wiszkowski57492ab2019-05-29 21:28:27630 // When omnibox contains pre-populated content, only show zero suggest for
631 // pages with URLs the user will recognize.
632 //
Mark Pearson3fb0e3162018-08-27 21:53:57633 // This list intentionally does not include items such as ftp: and file:
Tommy C. Li06ec26a2019-06-10 18:01:42634 // because (a) these do not work on Android and iOS, where most visited
Mark Pearson3fb0e3162018-08-27 21:53:57635 // zero suggest is launched and (b) on desktop, where contextual zero suggest
636 // is running, these types of schemes aren't eligible to be sent to the
637 // server to ask for suggestions (and thus in practice we won't display zero
638 // suggest for them).
Yue Ru Sune31e1572019-07-19 17:09:57639 if (input_type != metrics::OmniboxInputType::EMPTY &&
Tomasz Wiszkowski57492ab2019-05-29 21:28:27640 !(page_url.is_valid() &&
641 ((page_url.scheme() == url::kHttpScheme) ||
642 (page_url.scheme() == url::kHttpsScheme) ||
643 (page_url.scheme() == url::kAboutScheme) ||
644 (page_url.scheme() ==
645 client()->GetEmbedderRepresentationOfAboutScheme())))) {
[email protected]162c8d9fa2014-03-18 20:25:41646 return false;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27647 }
[email protected]162c8d9fa2014-03-18 20:25:41648
649 return true;
650}
[email protected]855ebff2014-05-09 07:14:38651
652void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Tommy C. Lid77691a2019-06-05 02:07:46653 if (result_type_running_ != REMOTE_NO_URL)
[email protected]855ebff2014-05-09 07:14:38654 return;
655
blundelld130d592015-06-21 19:29:13656 std::string json_data =
657 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38658 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57659 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14660 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00661 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
662 &results_))
[email protected]855ebff2014-05-09 07:14:38663 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38664 }
665}
Gheorghe Comanici9a364f572018-01-24 17:22:00666
667ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
668 const GURL& current_url,
669 const GURL& suggest_url) {
Gheorghe Comanici9a364f572018-01-24 17:22:00670 // Check if the URL can be sent in any suggest request.
671 const TemplateURLService* template_url_service =
672 client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49673 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00674 const TemplateURL* default_provider =
675 template_url_service->GetDefaultSearchProvider();
676 const bool can_send_current_url = CanSendURL(
677 current_url, suggest_url, default_provider, current_page_classification_,
Tommy C. Li71171ee2019-06-20 17:11:47678 template_url_service->search_terms_data(), client(), false);
Gheorghe Comanici9a364f572018-01-24 17:22:00679 // Collect metrics on eligibility.
680 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
681 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
682 if (!can_send_current_url) {
683 const bool can_send_ordinary_url =
684 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
685 current_page_classification_,
Tommy C. Li71171ee2019-06-20 17:11:47686 template_url_service->search_terms_data(), client(), false);
Gheorghe Comanici9a364f572018-01-24 17:22:00687 eligibility = can_send_ordinary_url
688 ? ZeroSuggestEligibility::URL_INELIGIBLE
689 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
690 }
691 UMA_HISTOGRAM_ENUMERATION(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27692 kOmniboxZeroSuggestEligibleHistogramName, static_cast<int>(eligibility),
Gheorghe Comanici9a364f572018-01-24 17:22:00693 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
694
Moe Ahmadi0993aec2019-09-19 21:42:30695 const auto field_trial_variants =
696 OmniboxFieldTrial::GetZeroSuggestVariants(current_page_classification_);
Tommy C. Li0af09562019-06-11 23:58:51697
Stepan Khapugin5187a462020-01-07 13:16:26698 if (base::Contains(field_trial_variants, kNoneVariant))
Kevin Bailey5fea4322018-03-21 22:36:05699 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00700
Tommy C. Li0af09562019-06-11 23:58:51701 if (current_page_classification_ == OmniboxEventProto::CHROMEOS_APP_LIST)
Tommy C. Lid77691a2019-06-05 02:07:46702 return REMOTE_NO_URL;
Jenny Zhang5bc2e3d2018-09-10 18:50:55703
Tommy Li55784022020-04-28 20:58:18704 if (current_page_classification_ == OmniboxEventProto::OTHER &&
705 base::FeatureList::IsEnabled(omnibox::kOnFocusSuggestionsContextualWeb) &&
706 can_send_current_url) {
707 return REMOTE_SEND_URL;
708 }
709
Moe Ahmadi0993aec2019-09-19 21:42:30710 if (base::Contains(field_trial_variants, kRemoteNoUrlVariant)) {
Moe Ahmadi85bf70862019-10-11 00:03:32711 if (RemoteNoUrlSuggestionsAreAllowed(client(), template_url_service))
712 return REMOTE_NO_URL;
713
Tomasz Wiszkowskif2464a82020-02-18 22:42:25714#if defined(OS_ANDROID)
715 // Android defaults to presenting Zero-prefix recent query suggestions on
716 // new tab page.
717 return (IsNTPPage(current_page_classification_)) ? REMOTE_NO_URL
718 : MOST_VISITED;
719#elif defined(OS_IOS)
Moe Ahmadi85bf70862019-10-11 00:03:32720 // Remote suggestions are replaced with the most visited ones.
721 // TODO(tommycli): Most likely this fallback concept should be replaced by
Tommy C. Li467c1bcaa2019-06-20 20:48:05722 // a more general configuration setup.
Moe Ahmadi85bf70862019-10-11 00:03:32723 return MOST_VISITED;
724#else
725 return NONE;
Tomasz Wiszkowskif2464a82020-02-18 22:42:25726#endif
Tommy C. Li393c1b142019-05-22 00:28:41727 }
Gheorghe Comanici9a364f572018-01-24 17:22:00728
Moe Ahmadi0993aec2019-09-19 21:42:30729 if (base::Contains(field_trial_variants, kRemoteSendUrlVariant) &&
730 can_send_current_url)
Tommy C. Li0af09562019-06-11 23:58:51731 return REMOTE_SEND_URL;
Gheorghe Comanici9a364f572018-01-24 17:22:00732
Moe Ahmadi0993aec2019-09-19 21:42:30733 if (base::Contains(field_trial_variants, kMostVisitedVariant))
Tommy C. Li0af09562019-06-11 23:58:51734 return MOST_VISITED;
735
736#if defined(OS_ANDROID) || defined(OS_IOS)
737 // For Android and iOS, default to MOST_VISITED so long as:
738 // - There is no configured variant for |page_classification| AND
739 // - The user is not on the search results page of the default search
740 // provider.
Moe Ahmadi0993aec2019-09-19 21:42:30741 if (field_trial_variants.empty() &&
Tommy C. Li0af09562019-06-11 23:58:51742 current_page_classification_ !=
743 OmniboxEventProto::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT &&
744 current_page_classification_ !=
745 OmniboxEventProto::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT) {
Kevin Bailey5fea4322018-03-21 22:36:05746 return MOST_VISITED;
Tommy C. Li393c1b142019-05-22 00:28:41747 }
Tommy C. Li0af09562019-06-11 23:58:51748#endif
Tommy C. Lic79d39ab2019-06-04 16:58:56749
750 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00751}