blob: 906ac845647bc96495f9a518c74ba7644b83625a [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"
blundell2102f7c2015-07-09 10:00:5332#include "components/omnibox/browser/autocomplete_provider_listener.h"
blundell2102f7c2015-07-09 10:00:5333#include "components/omnibox/browser/history_url_provider.h"
blundell2102f7c2015-07-09 10:00:5334#include "components/omnibox/browser/omnibox_pref_names.h"
Tommy C. Li06ec26a2019-06-10 18:01:4235#include "components/omnibox/browser/remote_suggestions_service.h"
blundell2102f7c2015-07-09 10:00:5336#include "components/omnibox/browser/search_provider.h"
sdefresne70948d62015-08-11 10:46:3537#include "components/omnibox/browser/verbatim_match.h"
Tomasz Wiszkowskid938a1112019-03-06 18:01:5738#include "components/omnibox/common/omnibox_features.h"
[email protected]f0c8c4992014-05-15 17:37:2639#include "components/pref_registry/pref_registry_syncable.h"
brettwf00b9b42016-02-01 22:11:3840#include "components/prefs/pref_service.h"
Gheorghe Comanici864725b2017-11-28 21:48:0141#include "components/search_engines/search_engine_type.h"
[email protected]bf5c532d2014-07-05 00:29:5342#include "components/search_engines/template_url_service.h"
rsleevi24f64dc22015-08-07 21:39:2143#include "components/url_formatter/url_formatter.h"
asvitkine9a279832015-12-18 02:35:5044#include "components/variations/net/variations_http_headers.h"
[email protected]bb1fb2b2013-05-31 00:21:0145#include "net/base/escape.h"
Maks Orlovich1b208512018-06-13 21:08:1746#include "services/network/public/cpp/resource_response.h"
47#include "services/network/public/cpp/shared_url_loader_factory.h"
48#include "services/network/public/cpp/simple_url_loader.h"
Steven Holtef9d5ed62017-10-21 02:02:3049#include "third_party/metrics_proto/omnibox_event.pb.h"
50#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]761fa4702013-07-02 15:25:1551#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0752
Tommy C. Li0af09562019-06-11 23:58:5153using metrics::OmniboxEventProto;
54
[email protected]6ce7f612012-09-05 23:53:0755namespace {
[email protected]bb1fb2b2013-05-31 00:21:0156
mpearson3d89cdc2017-03-03 21:15:4557// Represents whether ZeroSuggestProvider is allowed to display contextual
58// suggestions on focus, and if not, why not.
59// These values are written to logs. New enum values can be added, but existing
60// enums must never be renumbered or deleted and reused.
61enum class ZeroSuggestEligibility {
62 ELIGIBLE = 0,
63 // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL
64 // itself prevents ZeroSuggest from triggering.
65 URL_INELIGIBLE = 1,
66 GENERALLY_INELIGIBLE = 2,
67 ELIGIBLE_MAX_VALUE
68};
69
Tomasz Wiszkowskifd790c02019-08-29 17:31:2970// Histogram values describing client eligibility to receive RemoteNoURL
71// suggestions on NTP.
72// These values are persisted to logs. New values can be added, but existing
73// enums must never be renumbered or deleted and reused.
74enum class ZeroSuggestEligibilityForRemoteNoURL {
75 kEligible = 0,
76 kIneligibleUserOffTheRecord = 1,
77 kIneligibleSuggestionsDisabled = 2,
78 kIneligibleUserNotAuthenticated = 3,
79 // Used to report users ineligible for RemoteNoURL suggestions when the
80 // search and suggest server of their choice cannot be used to offer the
81 // RemoteNoURL suggestions.
82 kIneligibleWithUserSelectedServer = 4,
83 kIneligibleUserNotParticipating = 5,
84
85 kMaxValue = kIneligibleUserNotParticipating
86};
87
[email protected]bb1fb2b2013-05-31 00:21:0188// TODO(hfung): The histogram code was copied and modified from
89// search_provider.cc. Refactor and consolidate the code.
90// We keep track in a histogram how many suggest requests we send, how
91// many suggest requests we invalidate (e.g., due to a user typing
92// another character), and how many replies we receive.
mpearson3d89cdc2017-03-03 21:15:4593// These values are written to logs. New enum values can be added, but existing
94// enums must never be renumbered or deleted and reused.
[email protected]bb1fb2b2013-05-31 00:21:0195enum ZeroSuggestRequestsHistogramValue {
96 ZERO_SUGGEST_REQUEST_SENT = 1,
mpearson3d89cdc2017-03-03 21:15:4597 ZERO_SUGGEST_REQUEST_INVALIDATED = 2,
98 ZERO_SUGGEST_REPLY_RECEIVED = 3,
[email protected]bb1fb2b2013-05-31 00:21:0199 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
100};
101
102void LogOmniboxZeroSuggestRequest(
103 ZeroSuggestRequestsHistogramValue request_value) {
104 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
105 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
106}
107
Tomasz Wiszkowskifd790c02019-08-29 17:31:29108// Record user eligibility for RemoteNoUrl suggestions for supplied page class.
109// The |histogram_variant| is used to specify particular variant of the
110// Omnibox.ZeroSuggest.Eligible.RemoteNoUrl histogram that should be updated.
111void LogOmniboxRemoteNoUrlEligibilityOnNTP(
112 OmniboxEventProto::PageClassification page_class,
113 bool log_for_profile_open,
114 AutocompleteProviderClient* client) {
115 ZeroSuggestEligibilityForRemoteNoURL value =
116 ZeroSuggestEligibilityForRemoteNoURL::kEligible;
117
118 auto* service = client->GetTemplateURLService();
119 auto* provider = service ? service->GetDefaultSearchProvider() : nullptr;
120 auto engine = provider ? provider->GetEngineType(service->search_terms_data())
121 : SEARCH_ENGINE_UNKNOWN;
Moe Ahmadi0993aec2019-09-19 21:42:30122 const auto variants = OmniboxFieldTrial::GetZeroSuggestVariants(page_class);
Tomasz Wiszkowskifd790c02019-08-29 17:31:29123
Moe Ahmadi0993aec2019-09-19 21:42:30124 if (!base::Contains(variants, ZeroSuggestProvider::kRemoteNoUrlVariant)) {
Tomasz Wiszkowskifd790c02019-08-29 17:31:29125 value =
126 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserNotParticipating;
127 } else if (client->IsOffTheRecord()) {
128 value = ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserOffTheRecord;
129 } else if (!client->SearchSuggestEnabled()) {
130 value =
131 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleSuggestionsDisabled;
132 } else if (!client->IsAuthenticated()) {
133 value =
134 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserNotAuthenticated;
135 } else if (service == nullptr || provider == nullptr ||
136 engine != SEARCH_ENGINE_GOOGLE) {
137 value =
138 ZeroSuggestEligibilityForRemoteNoURL::kIneligibleWithUserSelectedServer;
139 }
140
141 if (log_for_profile_open) {
142 UMA_HISTOGRAM_ENUMERATION(
143 "Omnibox.ZeroSuggest.Eligible.RemoteNoUrl.OnNTP.OnProfileOpen", value);
144 } else {
145 UMA_HISTOGRAM_ENUMERATION(
146 "Omnibox.ZeroSuggest.Eligible.RemoteNoUrl.OnNTP.OnFocus", value);
147 }
148}
149
[email protected]bb1fb2b2013-05-31 00:21:01150// Relevance value to use if it was not set explicitly by the server.
151const int kDefaultZeroSuggestRelevance = 100;
152
mpearson3d89cdc2017-03-03 21:15:45153// Used for testing whether zero suggest is ever available.
mpearsonc90c24b2017-03-04 00:11:26154constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/";
mpearson3d89cdc2017-03-03 21:15:45155
Tomasz Wiszkowski57492ab2019-05-29 21:28:27156// Metric name tracking the omnibox suggestion eligibility.
157constexpr char kOmniboxZeroSuggestEligibleHistogramName[] =
158 "Omnibox.ZeroSuggest.Eligible.OnFocusV2";
159
Moe Ahmadi04206cd2019-08-23 18:03:22160#if defined(OS_ANDROID) || defined(OS_IOS)
Gheorghe Comanici9a364f572018-01-24 17:22:00161// If the user is not signed-in or the user does not have Google set up as their
Tommy C. Li467c1bcaa2019-06-20 20:48:05162// default search engine, the remote suggestions service is replaced with the
163// most visited service.
164bool RemoteSuggestionsShouldFallBackToMostVisited(
Tomasz Wiszkowski5abea202019-06-06 22:51:14165 AutocompleteProviderClient* client,
Gheorghe Comanici864725b2017-11-28 21:48:01166 const TemplateURLService* template_url_service) {
Tomasz Wiszkowski5abea202019-06-06 22:51:14167 if (!client->SearchSuggestEnabled())
168 return true;
169
170 if (!client->IsAuthenticated())
Gheorghe Comanici864725b2017-11-28 21:48:01171 return true;
172
Gheorghe Comanici9a364f572018-01-24 17:22:00173 if (template_url_service == nullptr)
174 return false;
Gheorghe Comanici682504102017-11-30 17:47:25175
Gheorghe Comanici9a364f572018-01-24 17:22:00176 const TemplateURL* default_provider =
177 template_url_service->GetDefaultSearchProvider();
178 return default_provider == nullptr ||
179 default_provider->GetEngineType(
180 template_url_service->search_terms_data()) != SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01181}
Moe Ahmadi04206cd2019-08-23 18:03:22182#endif
Gheorghe Comanici864725b2017-11-28 21:48:01183
[email protected]6ce7f612012-09-05 23:53:07184} // namespace
185
[email protected]a00008d42012-09-15 05:07:58186// static
Tommy C. Li0af09562019-06-11 23:58:51187const char ZeroSuggestProvider::kNoneVariant[] = "None";
188const char ZeroSuggestProvider::kRemoteNoUrlVariant[] = "RemoteNoUrl";
189const char ZeroSuggestProvider::kRemoteSendUrlVariant[] = "RemoteSendUrl";
190const char ZeroSuggestProvider::kMostVisitedVariant[] = "MostVisited";
191
192// static
[email protected]a00008d42012-09-15 05:07:58193ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18194 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11195 HistoryURLProvider* history_url_provider,
blundelld130d592015-06-21 19:29:13196 AutocompleteProviderListener* listener) {
mpearson931028c2016-07-01 18:55:11197 return new ZeroSuggestProvider(client, history_url_provider, listener);
[email protected]6ce7f612012-09-05 23:53:07198}
199
[email protected]855ebff2014-05-09 07:14:38200// static
201void ZeroSuggestProvider::RegisterProfilePrefs(
202 user_prefs::PrefRegistrySyncable* 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
215 if (input.from_omnibox_focus() && IsNTPPage(current_page_classification_)) {
216 LogOmniboxRemoteNoUrlEligibilityOnNTP(current_page_classification_, false,
217 client());
218 }
219
Tomasz Wiszkowski57492ab2019-05-29 21:28:27220 if (!AllowZeroSuggestSuggestions(input)) {
221 UMA_HISTOGRAM_ENUMERATION(kOmniboxZeroSuggestEligibleHistogramName,
222 ZeroSuggestEligibility::GENERALLY_INELIGIBLE,
223 ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE);
[email protected]f030c4d2014-03-25 01:05:54224 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27225 }
[email protected]bb1fb2b2013-05-31 00:21:01226
Kevin Bailey5fea4322018-03-21 22:36:05227 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13228 set_field_trial_triggered(false);
229 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54230 permanent_text_ = input.text();
231 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54232 current_title_ = input.current_title();
[email protected]9b9fa672013-11-07 06:04:52233 current_url_match_ = MatchForCurrentURL();
234
Tommy C. Li6aee62d52019-06-27 20:25:00235 TemplateURLRef::SearchTermsArgs search_terms_args;
236 search_terms_args.page_classification = current_page_classification_;
237 search_terms_args.omnibox_focus_type =
238 TemplateURLRef::SearchTermsArgs::OmniboxFocusType::ON_FOCUS;
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
Gheorghe Comanici9a364f572018-01-24 17:22:00244 result_type_running_ = TypeOfResultToRun(input.current_url(), 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();
302 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54303 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45304 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13305 }
306}
307
[email protected]855ebff2014-05-09 07:14:38308void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Moe Ahmadi0993aec2019-09-19 21:42:30309 if (base::Contains(OmniboxFieldTrial::GetZeroSuggestVariants(
310 current_page_classification_),
311 kRemoteNoUrlVariant)) {
[email protected]855ebff2014-05-09 07:14:38312 // Remove the deleted match from the cache, so it is not shown to the user
313 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13314 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38315 std::string());
316 }
317 BaseSearchProvider::DeleteMatch(match);
318}
319
[email protected]ec3f679b2014-08-18 07:45:13320void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
321 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45322 if (!results_.suggest_results.empty() ||
323 !results_.navigation_results.empty() ||
324 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13325 provider_info->back().set_times_returned_results_in_session(1);
326}
327
[email protected]f030c4d2014-03-25 01:05:54328void ZeroSuggestProvider::ResetSession() {
329 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13330 // |field_trial_triggered_in_session| unchanged and set
331 // |field_trial_triggered| to false since zero suggest is inactive now.
332 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54333}
334
mpearson931028c2016-07-01 18:55:11335ZeroSuggestProvider::ZeroSuggestProvider(
336 AutocompleteProviderClient* client,
337 HistoryURLProvider* history_url_provider,
338 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13339 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
mpearson931028c2016-07-01 18:55:11340 history_url_provider_(history_url_provider),
[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
368ZeroSuggestProvider::~ZeroSuggestProvider() {
369}
370
[email protected]776ee5902014-08-11 09:15:19371const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
372 // Zero suggest provider should not receive keyword results.
373 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13374 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19375}
376
377const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18378 // The callers of this method won't look at the AutocompleteInput's
379 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02380 AutocompleteInput input(base::string16(), current_page_classification_,
381 client()->GetSchemeClassifier());
382 input.set_current_url(GURL(current_query_));
383 input.set_current_title(current_title_);
384 input.set_prevent_inline_autocomplete(true);
385 input.set_allow_exact_keyword_match(false);
386 return input;
[email protected]776ee5902014-08-11 09:15:19387}
388
389bool ZeroSuggestProvider::ShouldAppendExtraParams(
390 const SearchSuggestionParser::SuggestResult& result) const {
391 // We always use the default provider for search, so append the params.
392 return true;
393}
394
[email protected]776ee5902014-08-11 09:15:19395void ZeroSuggestProvider::RecordDeletionResult(bool success) {
396 if (success) {
397 base::RecordAction(
398 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
399 } else {
400 base::RecordAction(
401 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
402 }
403}
404
Maks Orlovich1b208512018-06-13 21:08:17405void ZeroSuggestProvider::OnURLLoadComplete(
406 const network::SimpleURLLoader* source,
407 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19408 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17409 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19410
411 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
412
Gheorghe Comanici9a364f572018-01-24 17:22:00413 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17414 response_body && source->NetError() == net::OK &&
415 (source->ResponseInfo() && source->ResponseInfo()->headers &&
416 source->ResponseInfo()->headers->response_code() == 200) &&
417 UpdateResults(SearchSuggestionParser::ExtractJsonData(
418 source, std::move(response_body)));
419 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19420 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05421 result_type_running_ = NONE;
422 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19423 listener_->OnProviderUpdate(results_updated);
424}
425
Gheorghe Comanici9a364f572018-01-24 17:22:00426bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
427 std::unique_ptr<base::Value> data(
428 SearchSuggestionParser::DeserializeJsonData(json_data));
429 if (!data)
[email protected]855ebff2014-05-09 07:14:38430 return false;
431
Tommy C. Li467c1bcaa2019-06-20 20:48:05432 // When running the REMOTE_NO_URL variant, we want to store suggestion
Gheorghe Comanici9a364f572018-01-24 17:22:00433 // responses if non-empty.
Tommy C. Lid77691a2019-06-05 02:07:46434 if (result_type_running_ == REMOTE_NO_URL && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00435 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
436 json_data);
[email protected]855ebff2014-05-09 07:14:38437
Gheorghe Comanici9a364f572018-01-24 17:22:00438 // If we received an empty result list, we should update the display, as it
439 // may be showing cached results that should not be shown.
440 const base::ListValue* root_list = nullptr;
441 const base::ListValue* results_list = nullptr;
442 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
443 root_list->GetList(1, &results_list) &&
444 !results_list->empty();
445 const bool non_empty_cache = !results_.suggest_results.empty() ||
446 !results_.navigation_results.empty();
447 if (non_empty_parsed_list && non_empty_cache)
448 return false;
449 }
450 const bool results_updated = ParseSuggestResults(
451 *data, kDefaultZeroSuggestRelevance, false, &results_);
452 ConvertResultsToAutocompleteMatches();
453 return results_updated;
[email protected]855ebff2014-05-09 07:14:38454}
455
[email protected]bb1fb2b2013-05-31 00:21:01456void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37457 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30458 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22459 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11460 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01461}
462
[email protected]bb1fb2b2013-05-31 00:21:01463AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37464 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01465 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47466 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01467 match.destination_url = navigation.url();
468
[email protected]bb1fb2b2013-05-31 00:21:01469 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13470 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26471 navigation.url(), url_formatter::FormatUrl(navigation.url()),
Kevin Bailey83e643d2018-03-08 16:01:41472 client()->GetSchemeClassifier(), nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01473
manuk3972b2f2019-04-19 14:22:56474 // Zero suggest results should always omit protocols and never appear bold.
475 auto format_types = AutocompleteMatch::GetFormatTypes(false, false);
476 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
477 net::UnescapeRule::SPACES, nullptr,
478 nullptr, nullptr);
479 match.contents_class = ClassifyTermMatches({}, match.contents.length(), 0,
480 ACMatchClassification::URL);
[email protected]9c97f89c2013-06-25 03:12:16481
482 match.description =
483 AutocompleteMatch::SanitizeString(navigation.description());
manuk3972b2f2019-04-19 14:22:56484 match.description_class = ClassifyTermMatches({}, match.description.length(),
485 0, ACMatchClassification::NONE);
486
gcomanici67d53ac2017-04-01 17:07:19487 match.subtype_identifier = navigation.subtype_identifier();
[email protected]bb1fb2b2013-05-31 00:21:01488 return match;
489}
490
[email protected]8f064e52013-09-18 01:17:14491void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
Kevin Bailey5fea4322018-03-21 22:36:05492 size_t orig_request_num,
[email protected]8f064e52013-09-18 01:17:14493 const history::MostVisitedURLList& urls) {
Kevin Bailey5fea4322018-03-21 22:36:05494 if (result_type_running_ != MOST_VISITED ||
495 orig_request_num != most_visited_request_num_) {
Gheorghe Comanici9a364f572018-01-24 17:22:00496 return;
Kevin Bailey5fea4322018-03-21 22:36:05497 }
[email protected]8f064e52013-09-18 01:17:14498 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22499 done_ = true;
500 ConvertResultsToAutocompleteMatches();
Kevin Bailey5fea4322018-03-21 22:36:05501 result_type_running_ = NONE;
502 ++most_visited_request_num_;
mariakhomenkobfc3a2a2014-10-24 00:48:22503 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14504}
505
Tommy C. Li06ec26a2019-06-10 18:01:42506void ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable(
Maks Orlovich1b208512018-06-13 21:08:17507 std::unique_ptr<network::SimpleURLLoader> loader) {
Tommy C. Li06ec26a2019-06-10 18:01:42508 // RemoteSuggestionsService has already started |loader|, so here it's
Maks Orlovich1b208512018-06-13 21:08:17509 // only neccessary to grab its ownership until results come in to
510 // OnURLLoadComplete().
511 loader_ = std::move(loader);
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11512 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
513}
514
[email protected]9c97f89c2013-06-25 03:12:16515void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01516 matches_.clear();
517
blundelld130d592015-06-21 19:29:13518 TemplateURLService* template_url_service = client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49519 DCHECK(template_url_service);
[email protected]bb1fb2b2013-05-31 00:21:01520 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13521 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07522 // Fail if we can't set the clickthrough URL for query suggestions.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24523 if (default_provider == nullptr ||
blundelld130d592015-06-21 19:29:13524 !default_provider->SupportsReplacement(
525 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07526 return;
[email protected]6ce7f612012-09-05 23:53:07527
[email protected]00404742014-02-20 13:09:05528 MatchMap map;
529 AddSuggestResultsToMap(results_.suggest_results, &map);
530
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_) ||
545 !current_url_match_.destination_url.is_valid()) {
[email protected]3feb8b002013-10-14 23:50:13546 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27547 }
[email protected]8f064e52013-09-18 01:17:14548 matches_.push_back(current_url_match_);
549 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_));
[email protected]8f064e52013-09-18 01:17:14557 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
558 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37559 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13560 client()->GetSchemeClassifier(), url.url,
gcomanici67d53ac2017-04-01 17:07:19561 AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false,
jshin1fb76462016-04-05 22:13:03562 relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14563 matches_.push_back(NavigationToMatch(nav));
564 --relevance;
565 }
566 return;
567 }
568
[email protected]9c97f89c2013-06-25 03:12:16569 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01570 return;
571
Tomasz Wiszkowski57492ab2019-05-29 21:28:27572 // Do not add the default URL match if we're on the NTP to prevent
573 // chrome-native://newtab or chrome://newtab from showing up on the list of
574 // suggestions.
575 if (!IsNTPPage(current_page_classification_) &&
576 current_url_match_.destination_url.is_valid()) {
Jenny Zhang5bc2e3d2018-09-10 18:50:55577 matches_.push_back(current_url_match_);
Tomasz Wiszkowski57492ab2019-05-29 21:28:27578 }
579
[email protected]00404742014-02-20 13:09:05580 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01581 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01582
[email protected]0b9575f2014-07-30 11:58:37583 const SearchSuggestionParser::NavigationResults& nav_results(
584 results_.navigation_results);
jdoerrie2e6a651d2018-10-04 17:09:08585 for (auto it = nav_results.begin(); it != nav_results.end(); ++it) {
[email protected]bb1fb2b2013-05-31 00:21:01586 matches_.push_back(NavigationToMatch(*it));
gcomanici67d53ac2017-04-01 17:07:19587 }
[email protected]6ce7f612012-09-05 23:53:07588}
589
[email protected]bb1fb2b2013-05-31 00:21:01590AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]bb1fb2b2013-05-31 00:21:01591 // The placeholder suggestion for the current URL has high relevance so
592 // that it is in the first suggestion slot and inline autocompleted. It
593 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11594 AutocompleteInput tmp(GetInput(false));
595 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54596 const base::string16 description =
597 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
598 ? current_title_
599 : base::string16();
600 return VerbatimMatchForURL(client(), tmp, GURL(current_query_), description,
mpearson931028c2016-07-01 18:55:11601 history_url_provider_,
sdefresne70948d62015-08-11 10:46:35602 results_.verbatim_relevance);
[email protected]00404742014-02-20 13:09:05603}
[email protected]162c8d9fa2014-03-18 20:25:41604
Gheorghe Comanici9a364f572018-01-24 17:22:00605bool ZeroSuggestProvider::AllowZeroSuggestSuggestions(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27606 const AutocompleteInput& input) const {
607 const auto& page_url = input.current_url();
608 const auto page_class = input.current_page_classification();
609 const auto input_type = input.type();
Peter Kastingfb1a8ea2017-11-28 02:26:50610
Tomasz Wiszkowski57492ab2019-05-29 21:28:27611 if (!input.from_omnibox_focus())
612 return false;
613
Peter Kastingfb1a8ea2017-11-28 02:26:50614 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41615 return false;
616
Tomasz Wiszkowskia8780ff92019-07-25 21:46:05617 // When the omnibox is empty, only allow zero suggest for the ChromeOS
618 // Launcher and NTP.
Yue Ru Sune31e1572019-07-19 17:09:57619 if (input_type == metrics::OmniboxInputType::EMPTY &&
Tomasz Wiszkowski57492ab2019-05-29 21:28:27620 !(page_class == metrics::OmniboxEventProto::CHROMEOS_APP_LIST ||
Tomasz Wiszkowskia8780ff92019-07-25 21:46:05621 IsNTPPage(page_class))) {
[email protected]5b899dc2018-10-02 09:20:01622 return false;
623 }
624
Tomasz Wiszkowski57492ab2019-05-29 21:28:27625 // When omnibox contains pre-populated content, only show zero suggest for
626 // pages with URLs the user will recognize.
627 //
Mark Pearson3fb0e3162018-08-27 21:53:57628 // This list intentionally does not include items such as ftp: and file:
Tommy C. Li06ec26a2019-06-10 18:01:42629 // because (a) these do not work on Android and iOS, where most visited
Mark Pearson3fb0e3162018-08-27 21:53:57630 // zero suggest is launched and (b) on desktop, where contextual zero suggest
631 // is running, these types of schemes aren't eligible to be sent to the
632 // server to ask for suggestions (and thus in practice we won't display zero
633 // suggest for them).
Yue Ru Sune31e1572019-07-19 17:09:57634 if (input_type != metrics::OmniboxInputType::EMPTY &&
Tomasz Wiszkowski57492ab2019-05-29 21:28:27635 !(page_url.is_valid() &&
636 ((page_url.scheme() == url::kHttpScheme) ||
637 (page_url.scheme() == url::kHttpsScheme) ||
638 (page_url.scheme() == url::kAboutScheme) ||
639 (page_url.scheme() ==
640 client()->GetEmbedderRepresentationOfAboutScheme())))) {
[email protected]162c8d9fa2014-03-18 20:25:41641 return false;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27642 }
[email protected]162c8d9fa2014-03-18 20:25:41643
644 return true;
645}
[email protected]855ebff2014-05-09 07:14:38646
647void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Tommy C. Lid77691a2019-06-05 02:07:46648 if (result_type_running_ != REMOTE_NO_URL)
[email protected]855ebff2014-05-09 07:14:38649 return;
650
blundelld130d592015-06-21 19:29:13651 std::string json_data =
652 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38653 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57654 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14655 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00656 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
657 &results_))
[email protected]855ebff2014-05-09 07:14:38658 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38659 }
660}
Gheorghe Comanici9a364f572018-01-24 17:22:00661
662ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
663 const GURL& current_url,
664 const GURL& suggest_url) {
Gheorghe Comanici9a364f572018-01-24 17:22:00665 // Check if the URL can be sent in any suggest request.
666 const TemplateURLService* template_url_service =
667 client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49668 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00669 const TemplateURL* default_provider =
670 template_url_service->GetDefaultSearchProvider();
671 const bool can_send_current_url = CanSendURL(
672 current_url, suggest_url, default_provider, current_page_classification_,
Tommy C. Li71171ee2019-06-20 17:11:47673 template_url_service->search_terms_data(), client(), false);
Gheorghe Comanici9a364f572018-01-24 17:22:00674 // Collect metrics on eligibility.
675 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
676 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
677 if (!can_send_current_url) {
678 const bool can_send_ordinary_url =
679 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
680 current_page_classification_,
Tommy C. Li71171ee2019-06-20 17:11:47681 template_url_service->search_terms_data(), client(), false);
Gheorghe Comanici9a364f572018-01-24 17:22:00682 eligibility = can_send_ordinary_url
683 ? ZeroSuggestEligibility::URL_INELIGIBLE
684 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
685 }
686 UMA_HISTOGRAM_ENUMERATION(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27687 kOmniboxZeroSuggestEligibleHistogramName, static_cast<int>(eligibility),
Gheorghe Comanici9a364f572018-01-24 17:22:00688 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
689
Moe Ahmadi0993aec2019-09-19 21:42:30690 const auto field_trial_variants =
691 OmniboxFieldTrial::GetZeroSuggestVariants(current_page_classification_);
Tommy C. Li0af09562019-06-11 23:58:51692
Moe Ahmadi0993aec2019-09-19 21:42:30693 if (base::Contains(field_trial_variants, kNoneVariant) ||
Tommy C. Li0af09562019-06-11 23:58:51694 base::FeatureList::IsEnabled(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27695 omnibox::kOmniboxPopupShortcutIconsInZeroState)) {
Kevin Bailey5fea4322018-03-21 22:36:05696 return NONE;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27697 }
Gheorghe Comanici9a364f572018-01-24 17:22:00698
Tommy C. Li0af09562019-06-11 23:58:51699 // TODO(tommycli): Since this can be configured via ZeroSuggestVariant, we
700 // should eliminate this special case and use a field trial configuration.
701 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
Moe Ahmadi0993aec2019-09-19 21:42:30704 if (base::Contains(field_trial_variants, kRemoteNoUrlVariant)) {
Moe Ahmadi04206cd2019-08-23 18:03:22705#if defined(OS_ANDROID) || defined(OS_IOS)
Tommy C. Li467c1bcaa2019-06-20 20:48:05706 // TODO(tommycli): It's odd that this doesn't apply to kRemoteSendUrlVariant
707 // as well. Most likely this fallback concept should be replaced by
708 // a more general configuration setup.
Moe Ahmadi04206cd2019-08-23 18:03:22709 if (RemoteSuggestionsShouldFallBackToMostVisited(client(),
710 template_url_service)) {
711 return MOST_VISITED;
712 }
713#endif
714 return REMOTE_NO_URL;
Tommy C. Li393c1b142019-05-22 00:28:41715 }
Gheorghe Comanici9a364f572018-01-24 17:22:00716
Moe Ahmadi0993aec2019-09-19 21:42:30717 if (base::Contains(field_trial_variants, kRemoteSendUrlVariant) &&
718 can_send_current_url)
Tommy C. Li0af09562019-06-11 23:58:51719 return REMOTE_SEND_URL;
Gheorghe Comanici9a364f572018-01-24 17:22:00720
Moe Ahmadi0993aec2019-09-19 21:42:30721 if (base::Contains(field_trial_variants, kMostVisitedVariant))
Tommy C. Li0af09562019-06-11 23:58:51722 return MOST_VISITED;
723
724#if defined(OS_ANDROID) || defined(OS_IOS)
725 // For Android and iOS, default to MOST_VISITED so long as:
726 // - There is no configured variant for |page_classification| AND
727 // - The user is not on the search results page of the default search
728 // provider.
Moe Ahmadi0993aec2019-09-19 21:42:30729 if (field_trial_variants.empty() &&
Tommy C. Li0af09562019-06-11 23:58:51730 current_page_classification_ !=
731 OmniboxEventProto::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT &&
732 current_page_classification_ !=
733 OmniboxEventProto::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT) {
Kevin Bailey5fea4322018-03-21 22:36:05734 return MOST_VISITED;
Tommy C. Li393c1b142019-05-22 00:28:41735 }
Tommy C. Li0af09562019-06-11 23:58:51736#endif
Tommy C. Lic79d39ab2019-06-04 16:58:56737
738 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00739}