blob: b3111e770bcb8bbd850ff4a61589e5f574d60cbd [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"
[email protected]98570e12013-06-10 19:54:2219#include "base/strings/string16.h"
20#include "base/strings/string_util.h"
[email protected]135cb802013-06-09 16:44:2021#include "base/strings/utf_string_conversions.h"
[email protected]4dcb7972013-06-28 15:15:4122#include "base/time/time.h"
a-v-ydd768d52016-03-25 21:07:4623#include "base/trace_event/trace_event.h"
Tommy C. Li0af09562019-06-11 23:58:5124#include "build/build_config.h"
sdefresnebc766ef2014-09-25 09:28:1325#include "components/history/core/browser/history_types.h"
sdefresne0da3bc02015-01-29 18:26:3526#include "components/history/core/browser/top_sites.h"
blundell2102f7c2015-07-09 10:00:5327#include "components/omnibox/browser/autocomplete_classifier.h"
28#include "components/omnibox/browser/autocomplete_input.h"
29#include "components/omnibox/browser/autocomplete_match.h"
manuk3972b2f2019-04-19 14:22:5630#include "components/omnibox/browser/autocomplete_match_classification.h"
blundell2102f7c2015-07-09 10:00:5331#include "components/omnibox/browser/autocomplete_provider_listener.h"
blundell2102f7c2015-07-09 10:00:5332#include "components/omnibox/browser/history_url_provider.h"
blundell2102f7c2015-07-09 10:00:5333#include "components/omnibox/browser/omnibox_pref_names.h"
Tommy C. Li06ec26a2019-06-10 18:01:4234#include "components/omnibox/browser/remote_suggestions_service.h"
blundell2102f7c2015-07-09 10:00:5335#include "components/omnibox/browser/search_provider.h"
sdefresne70948d62015-08-11 10:46:3536#include "components/omnibox/browser/verbatim_match.h"
Tomasz Wiszkowskid938a1112019-03-06 18:01:5737#include "components/omnibox/common/omnibox_features.h"
[email protected]f0c8c4992014-05-15 17:37:2638#include "components/pref_registry/pref_registry_syncable.h"
brettwf00b9b42016-02-01 22:11:3839#include "components/prefs/pref_service.h"
Gheorghe Comanici864725b2017-11-28 21:48:0140#include "components/search_engines/search_engine_type.h"
[email protected]bf5c532d2014-07-05 00:29:5341#include "components/search_engines/template_url_service.h"
rsleevi24f64dc22015-08-07 21:39:2142#include "components/url_formatter/url_formatter.h"
asvitkine9a279832015-12-18 02:35:5043#include "components/variations/net/variations_http_headers.h"
[email protected]bb1fb2b2013-05-31 00:21:0144#include "net/base/escape.h"
Maks Orlovich1b208512018-06-13 21:08:1745#include "services/network/public/cpp/resource_response.h"
46#include "services/network/public/cpp/shared_url_loader_factory.h"
47#include "services/network/public/cpp/simple_url_loader.h"
Steven Holtef9d5ed62017-10-21 02:02:3048#include "third_party/metrics_proto/omnibox_event.pb.h"
49#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]761fa4702013-07-02 15:25:1550#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0751
Tommy C. Li0af09562019-06-11 23:58:5152using metrics::OmniboxEventProto;
53
[email protected]6ce7f612012-09-05 23:53:0754namespace {
[email protected]bb1fb2b2013-05-31 00:21:0155
mpearson3d89cdc2017-03-03 21:15:4556// Represents whether ZeroSuggestProvider is allowed to display contextual
57// suggestions on focus, and if not, why not.
58// These values are written to logs. New enum values can be added, but existing
59// enums must never be renumbered or deleted and reused.
60enum class ZeroSuggestEligibility {
61 ELIGIBLE = 0,
62 // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL
63 // itself prevents ZeroSuggest from triggering.
64 URL_INELIGIBLE = 1,
65 GENERALLY_INELIGIBLE = 2,
66 ELIGIBLE_MAX_VALUE
67};
68
[email protected]bb1fb2b2013-05-31 00:21:0169// TODO(hfung): The histogram code was copied and modified from
70// search_provider.cc. Refactor and consolidate the code.
71// We keep track in a histogram how many suggest requests we send, how
72// many suggest requests we invalidate (e.g., due to a user typing
73// another character), and how many replies we receive.
mpearson3d89cdc2017-03-03 21:15:4574// These values are written to logs. New enum values can be added, but existing
75// enums must never be renumbered or deleted and reused.
[email protected]bb1fb2b2013-05-31 00:21:0176enum ZeroSuggestRequestsHistogramValue {
77 ZERO_SUGGEST_REQUEST_SENT = 1,
mpearson3d89cdc2017-03-03 21:15:4578 ZERO_SUGGEST_REQUEST_INVALIDATED = 2,
79 ZERO_SUGGEST_REPLY_RECEIVED = 3,
[email protected]bb1fb2b2013-05-31 00:21:0180 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
81};
82
83void LogOmniboxZeroSuggestRequest(
84 ZeroSuggestRequestsHistogramValue request_value) {
85 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
86 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
87}
88
[email protected]bb1fb2b2013-05-31 00:21:0189// Relevance value to use if it was not set explicitly by the server.
90const int kDefaultZeroSuggestRelevance = 100;
91
mpearson3d89cdc2017-03-03 21:15:4592// Used for testing whether zero suggest is ever available.
mpearsonc90c24b2017-03-04 00:11:2693constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/";
mpearson3d89cdc2017-03-03 21:15:4594
Tomasz Wiszkowski57492ab2019-05-29 21:28:2795// Metric name tracking the omnibox suggestion eligibility.
96constexpr char kOmniboxZeroSuggestEligibleHistogramName[] =
97 "Omnibox.ZeroSuggest.Eligible.OnFocusV2";
98
Moe Ahmadi04206cd2019-08-23 18:03:2299#if defined(OS_ANDROID) || defined(OS_IOS)
Gheorghe Comanici9a364f572018-01-24 17:22:00100// 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:05101// default search engine, the remote suggestions service is replaced with the
102// most visited service.
103bool RemoteSuggestionsShouldFallBackToMostVisited(
Tomasz Wiszkowski5abea202019-06-06 22:51:14104 AutocompleteProviderClient* client,
Gheorghe Comanici864725b2017-11-28 21:48:01105 const TemplateURLService* template_url_service) {
Tomasz Wiszkowski5abea202019-06-06 22:51:14106 if (!client->SearchSuggestEnabled())
107 return true;
108
109 if (!client->IsAuthenticated())
Gheorghe Comanici864725b2017-11-28 21:48:01110 return true;
111
Gheorghe Comanici9a364f572018-01-24 17:22:00112 if (template_url_service == nullptr)
113 return false;
Gheorghe Comanici682504102017-11-30 17:47:25114
Gheorghe Comanici9a364f572018-01-24 17:22:00115 const TemplateURL* default_provider =
116 template_url_service->GetDefaultSearchProvider();
117 return default_provider == nullptr ||
118 default_provider->GetEngineType(
119 template_url_service->search_terms_data()) != SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01120}
Moe Ahmadi04206cd2019-08-23 18:03:22121#endif
Gheorghe Comanici864725b2017-11-28 21:48:01122
[email protected]6ce7f612012-09-05 23:53:07123} // namespace
124
[email protected]a00008d42012-09-15 05:07:58125// static
Tommy C. Li0af09562019-06-11 23:58:51126const char ZeroSuggestProvider::kNoneVariant[] = "None";
127const char ZeroSuggestProvider::kRemoteNoUrlVariant[] = "RemoteNoUrl";
128const char ZeroSuggestProvider::kRemoteSendUrlVariant[] = "RemoteSendUrl";
129const char ZeroSuggestProvider::kMostVisitedVariant[] = "MostVisited";
130
131// static
[email protected]a00008d42012-09-15 05:07:58132ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18133 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11134 HistoryURLProvider* history_url_provider,
blundelld130d592015-06-21 19:29:13135 AutocompleteProviderListener* listener) {
mpearson931028c2016-07-01 18:55:11136 return new ZeroSuggestProvider(client, history_url_provider, listener);
[email protected]6ce7f612012-09-05 23:53:07137}
138
[email protected]855ebff2014-05-09 07:14:38139// static
140void ZeroSuggestProvider::RegisterProfilePrefs(
141 user_prefs::PrefRegistrySyncable* registry) {
blundelld130d592015-06-21 19:29:13142 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
143 std::string());
[email protected]855ebff2014-05-09 07:14:38144}
145
[email protected]6ce7f612012-09-05 23:53:07146void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18147 bool minimal_changes) {
a-v-ydd768d52016-03-25 21:07:46148 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54149 matches_.clear();
Kevin Bailey8642e612018-04-23 20:27:54150 Stop(true, false);
Jenny Zhang5bc2e3d2018-09-10 18:50:55151
Tomasz Wiszkowski57492ab2019-05-29 21:28:27152 if (!AllowZeroSuggestSuggestions(input)) {
153 UMA_HISTOGRAM_ENUMERATION(kOmniboxZeroSuggestEligibleHistogramName,
154 ZeroSuggestEligibility::GENERALLY_INELIGIBLE,
155 ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE);
[email protected]f030c4d2014-03-25 01:05:54156 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27157 }
[email protected]bb1fb2b2013-05-31 00:21:01158
Kevin Bailey5fea4322018-03-21 22:36:05159 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13160 set_field_trial_triggered(false);
161 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54162 permanent_text_ = input.text();
163 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54164 current_title_ = input.current_title();
[email protected]f030c4d2014-03-25 01:05:54165 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52166 current_url_match_ = MatchForCurrentURL();
167
Tommy C. Li6aee62d52019-06-27 20:25:00168 TemplateURLRef::SearchTermsArgs search_terms_args;
169 search_terms_args.page_classification = current_page_classification_;
170 search_terms_args.omnibox_focus_type =
171 TemplateURLRef::SearchTermsArgs::OmniboxFocusType::ON_FOCUS;
Tommy C. Li06ec26a2019-06-10 18:01:42172 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Tommy C. Li6aee62d52019-06-27 20:25:00173 search_terms_args, client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41174 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07175 return;
[email protected]162c8d9fa2014-03-18 20:25:41176
Gheorghe Comanici9a364f572018-01-24 17:22:00177 result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url);
Kevin Bailey5fea4322018-03-21 22:36:05178 if (result_type_running_ == NONE)
[email protected]162c8d9fa2014-03-18 20:25:41179 return;
[email protected]162c8d9fa2014-03-18 20:25:41180
[email protected]6ce7f612012-09-05 23:53:07181 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11182
[email protected]855ebff2014-05-09 07:14:38183 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11184
Kevin Bailey5fea4322018-03-21 22:36:05185 if (result_type_running_ == MOST_VISITED) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11186 most_visited_urls_.clear();
187 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
Gheorghe Comanici9a364f572018-01-24 17:22:00188 if (!ts) {
189 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05190 result_type_running_ = NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00191 return;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11192 }
Gheorghe Comanici9a364f572018-01-24 17:22:00193
Kevin Bailey40bb0ac52019-04-02 00:43:32194 ts->GetMostVisitedURLs(base::BindRepeating(
195 &ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
196 weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11197 return;
198 }
199
Tommy C. Li6aee62d52019-06-27 20:25:00200 search_terms_args.current_page_url =
Tommy C. Lid77691a2019-06-05 02:07:46201 result_type_running_ == REMOTE_SEND_URL ? current_query_ : std::string();
Maks Orlovich1b208512018-06-13 21:08:17202 // Create a request for suggestions, routing completion to
Tommy C. Li06ec26a2019-06-10 18:01:42203 // OnRemoteSuggestionsLoaderAvailable.
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11204 client()
Tommy C. Li06ec26a2019-06-10 18:01:42205 ->GetRemoteSuggestionsService(/*create_if_necessary=*/true)
206 ->CreateSuggestionsRequest(
Tommy Li3fe83912019-08-23 17:33:11207 search_terms_args, client()->GetTemplateURLService(),
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11208 base::BindOnce(
Tommy C. Li06ec26a2019-06-10 18:01:42209 &ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable,
Maks Orlovich1b208512018-06-13 21:08:17210 weak_ptr_factory_.GetWeakPtr()),
211 base::BindOnce(
212 &ZeroSuggestProvider::OnURLLoadComplete,
213 base::Unretained(this) /* this owns SimpleURLLoader */));
[email protected]6ce7f612012-09-05 23:53:07214}
215
mpearson8a37c382015-03-07 05:58:57216void ZeroSuggestProvider::Stop(bool clear_cached_results,
217 bool due_to_user_inactivity) {
Maks Orlovich1b208512018-06-13 21:08:17218 if (loader_)
[email protected]ec3f679b2014-08-18 07:45:13219 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
Maks Orlovich1b208512018-06-13 21:08:17220 loader_.reset();
Tommy Li3fe83912019-08-23 17:33:11221
Kevin Bailey5fea4322018-03-21 22:36:05222 // TODO(krb): It would allow us to remove some guards if we could also cancel
223 // the TopSites::GetMostVisitedURLs request.
[email protected]ec3f679b2014-08-18 07:45:13224 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05225 result_type_running_ = NONE;
226 ++most_visited_request_num_;
[email protected]ec3f679b2014-08-18 07:45:13227
228 if (clear_cached_results) {
229 // We do not call Clear() on |results_| to retain |verbatim_relevance|
230 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18231 // beginning of the next call to Start() to determine the current url
232 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13233 results_.suggest_results.clear();
234 results_.navigation_results.clear();
235 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54236 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45237 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13238 }
239}
240
[email protected]855ebff2014-05-09 07:14:38241void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Tommy C. Li0af09562019-06-11 23:58:51242 if (OmniboxFieldTrial::GetZeroSuggestVariant(current_page_classification_) ==
243 kRemoteNoUrlVariant) {
[email protected]855ebff2014-05-09 07:14:38244 // Remove the deleted match from the cache, so it is not shown to the user
245 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13246 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38247 std::string());
248 }
249 BaseSearchProvider::DeleteMatch(match);
250}
251
[email protected]ec3f679b2014-08-18 07:45:13252void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
253 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45254 if (!results_.suggest_results.empty() ||
255 !results_.navigation_results.empty() ||
256 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13257 provider_info->back().set_times_returned_results_in_session(1);
258}
259
[email protected]f030c4d2014-03-25 01:05:54260void ZeroSuggestProvider::ResetSession() {
261 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13262 // |field_trial_triggered_in_session| unchanged and set
263 // |field_trial_triggered| to false since zero suggest is inactive now.
264 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54265}
266
mpearson931028c2016-07-01 18:55:11267ZeroSuggestProvider::ZeroSuggestProvider(
268 AutocompleteProviderClient* client,
269 HistoryURLProvider* history_url_provider,
270 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13271 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
mpearson931028c2016-07-01 18:55:11272 history_url_provider_(history_url_provider),
[email protected]776ee5902014-08-11 09:15:19273 listener_(listener),
Jeremy Roman5c341f6d2019-07-15 15:56:10274 result_type_running_(NONE) {
Tommy C. Li06ec26a2019-06-10 18:01:42275 // Record whether remote zero suggest is possible for this user / profile.
mpearson3d89cdc2017-03-03 21:15:45276 const TemplateURLService* template_url_service =
277 client->GetTemplateURLService();
278 // Template URL service can be null in tests.
279 if (template_url_service != nullptr) {
Tommy C. Li06ec26a2019-06-10 18:01:42280 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Tommy C. Li6aee62d52019-06-27 20:25:00281 TemplateURLRef::SearchTermsArgs(), template_url_service);
mpearson3d89cdc2017-03-03 21:15:45282 // To check whether this is allowed, use an arbitrary insecure (http) URL
283 // as the URL we'd want suggestions for. The value of OTHER as the current
284 // page classification is to correspond with that URL.
285 UMA_HISTOGRAM_BOOLEAN(
286 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
287 suggest_url.is_valid() &&
288 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
289 template_url_service->GetDefaultSearchProvider(),
290 metrics::OmniboxEventProto::OTHER,
Tommy C. Li71171ee2019-06-20 17:11:47291 template_url_service->search_terms_data(), client,
292 false));
mpearson3d89cdc2017-03-03 21:15:45293 }
[email protected]6ce7f612012-09-05 23:53:07294}
295
296ZeroSuggestProvider::~ZeroSuggestProvider() {
297}
298
[email protected]776ee5902014-08-11 09:15:19299const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
300 // Zero suggest provider should not receive keyword results.
301 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13302 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19303}
304
305const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18306 // The callers of this method won't look at the AutocompleteInput's
307 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02308 AutocompleteInput input(base::string16(), current_page_classification_,
309 client()->GetSchemeClassifier());
310 input.set_current_url(GURL(current_query_));
311 input.set_current_title(current_title_);
312 input.set_prevent_inline_autocomplete(true);
313 input.set_allow_exact_keyword_match(false);
314 return input;
[email protected]776ee5902014-08-11 09:15:19315}
316
317bool ZeroSuggestProvider::ShouldAppendExtraParams(
318 const SearchSuggestionParser::SuggestResult& result) const {
319 // We always use the default provider for search, so append the params.
320 return true;
321}
322
[email protected]776ee5902014-08-11 09:15:19323void ZeroSuggestProvider::RecordDeletionResult(bool success) {
324 if (success) {
325 base::RecordAction(
326 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
327 } else {
328 base::RecordAction(
329 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
330 }
331}
332
Maks Orlovich1b208512018-06-13 21:08:17333void ZeroSuggestProvider::OnURLLoadComplete(
334 const network::SimpleURLLoader* source,
335 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19336 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17337 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19338
339 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
340
Gheorghe Comanici9a364f572018-01-24 17:22:00341 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17342 response_body && source->NetError() == net::OK &&
343 (source->ResponseInfo() && source->ResponseInfo()->headers &&
344 source->ResponseInfo()->headers->response_code() == 200) &&
345 UpdateResults(SearchSuggestionParser::ExtractJsonData(
346 source, std::move(response_body)));
347 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19348 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05349 result_type_running_ = NONE;
350 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19351 listener_->OnProviderUpdate(results_updated);
352}
353
Gheorghe Comanici9a364f572018-01-24 17:22:00354bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
355 std::unique_ptr<base::Value> data(
356 SearchSuggestionParser::DeserializeJsonData(json_data));
357 if (!data)
[email protected]855ebff2014-05-09 07:14:38358 return false;
359
Tommy C. Li467c1bcaa2019-06-20 20:48:05360 // When running the REMOTE_NO_URL variant, we want to store suggestion
Gheorghe Comanici9a364f572018-01-24 17:22:00361 // responses if non-empty.
Tommy C. Lid77691a2019-06-05 02:07:46362 if (result_type_running_ == REMOTE_NO_URL && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00363 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
364 json_data);
[email protected]855ebff2014-05-09 07:14:38365
Gheorghe Comanici9a364f572018-01-24 17:22:00366 // If we received an empty result list, we should update the display, as it
367 // may be showing cached results that should not be shown.
368 const base::ListValue* root_list = nullptr;
369 const base::ListValue* results_list = nullptr;
370 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
371 root_list->GetList(1, &results_list) &&
372 !results_list->empty();
373 const bool non_empty_cache = !results_.suggest_results.empty() ||
374 !results_.navigation_results.empty();
375 if (non_empty_parsed_list && non_empty_cache)
376 return false;
377 }
378 const bool results_updated = ParseSuggestResults(
379 *data, kDefaultZeroSuggestRelevance, false, &results_);
380 ConvertResultsToAutocompleteMatches();
381 return results_updated;
[email protected]855ebff2014-05-09 07:14:38382}
383
[email protected]bb1fb2b2013-05-31 00:21:01384void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37385 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30386 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22387 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11388 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01389}
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
gcomanici67d53ac2017-04-01 17:07:19415 match.subtype_identifier = navigation.subtype_identifier();
[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
Maks Orlovich1b208512018-06-13 21:08:17437 // only neccessary to grab its ownership until results come in to
438 // 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;
457 AddSuggestResultsToMap(results_.suggest_results, &map);
458
459 const int num_query_results = map.size();
460 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01461 const int num_results = num_query_results + num_nav_results;
Steven Holte95922222018-09-14 20:06:23462 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.QueryResults", num_query_results);
463 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.URLResults", num_nav_results);
464 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01465
[email protected]8f064e52013-09-18 01:17:14466 // Show Most Visited results after ZeroSuggest response is received.
Kevin Bailey5fea4322018-03-21 22:36:05467 if (result_type_running_ == MOST_VISITED) {
Tomasz Wiszkowski57492ab2019-05-29 21:28:27468 // Ensure we don't show most visited URL suggestions on NTP.
469 // This allows us to prevent undesired side outcome of presenting
470 // URL suggestions to users who are not in the personalized field trial for
471 // zero query suggestions.
472 if (IsNTPPage(current_page_classification_) ||
473 !current_url_match_.destination_url.is_valid()) {
[email protected]3feb8b002013-10-14 23:50:13474 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27475 }
[email protected]8f064e52013-09-18 01:17:14476 matches_.push_back(current_url_match_);
477 int relevance = 600;
478 if (num_results > 0) {
Steven Holte95922222018-09-14 20:06:23479 UMA_HISTOGRAM_COUNTS_1M(
[email protected]8f064e52013-09-18 01:17:14480 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
481 most_visited_urls_.size());
482 }
[email protected]23db6492014-01-16 02:35:30483 const base::string16 current_query_string16(
484 base::ASCIIToUTF16(current_query_));
[email protected]8f064e52013-09-18 01:17:14485 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
486 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37487 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13488 client()->GetSchemeClassifier(), url.url,
gcomanici67d53ac2017-04-01 17:07:19489 AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false,
jshin1fb76462016-04-05 22:13:03490 relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14491 matches_.push_back(NavigationToMatch(nav));
492 --relevance;
493 }
494 return;
495 }
496
[email protected]9c97f89c2013-06-25 03:12:16497 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01498 return;
499
Tomasz Wiszkowski57492ab2019-05-29 21:28:27500 // Do not add the default URL match if we're on the NTP to prevent
501 // chrome-native://newtab or chrome://newtab from showing up on the list of
502 // suggestions.
503 if (!IsNTPPage(current_page_classification_) &&
504 current_url_match_.destination_url.is_valid()) {
Jenny Zhang5bc2e3d2018-09-10 18:50:55505 matches_.push_back(current_url_match_);
Tomasz Wiszkowski57492ab2019-05-29 21:28:27506 }
507
[email protected]00404742014-02-20 13:09:05508 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01509 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01510
[email protected]0b9575f2014-07-30 11:58:37511 const SearchSuggestionParser::NavigationResults& nav_results(
512 results_.navigation_results);
jdoerrie2e6a651d2018-10-04 17:09:08513 for (auto it = nav_results.begin(); it != nav_results.end(); ++it) {
[email protected]bb1fb2b2013-05-31 00:21:01514 matches_.push_back(NavigationToMatch(*it));
gcomanici67d53ac2017-04-01 17:07:19515 }
[email protected]6ce7f612012-09-05 23:53:07516}
517
[email protected]bb1fb2b2013-05-31 00:21:01518AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]bb1fb2b2013-05-31 00:21:01519 // The placeholder suggestion for the current URL has high relevance so
520 // that it is in the first suggestion slot and inline autocompleted. It
521 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11522 AutocompleteInput tmp(GetInput(false));
523 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54524 const base::string16 description =
525 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
526 ? current_title_
527 : base::string16();
528 return VerbatimMatchForURL(client(), tmp, GURL(current_query_), description,
mpearson931028c2016-07-01 18:55:11529 history_url_provider_,
sdefresne70948d62015-08-11 10:46:35530 results_.verbatim_relevance);
[email protected]00404742014-02-20 13:09:05531}
[email protected]162c8d9fa2014-03-18 20:25:41532
Gheorghe Comanici9a364f572018-01-24 17:22:00533bool ZeroSuggestProvider::AllowZeroSuggestSuggestions(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27534 const AutocompleteInput& input) const {
535 const auto& page_url = input.current_url();
536 const auto page_class = input.current_page_classification();
537 const auto input_type = input.type();
Peter Kastingfb1a8ea2017-11-28 02:26:50538
Tomasz Wiszkowski57492ab2019-05-29 21:28:27539 if (!input.from_omnibox_focus())
540 return false;
541
Peter Kastingfb1a8ea2017-11-28 02:26:50542 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41543 return false;
544
Tomasz Wiszkowskia8780ff92019-07-25 21:46:05545 // When the omnibox is empty, only allow zero suggest for the ChromeOS
546 // Launcher and NTP.
Yue Ru Sune31e1572019-07-19 17:09:57547 if (input_type == metrics::OmniboxInputType::EMPTY &&
Tomasz Wiszkowski57492ab2019-05-29 21:28:27548 !(page_class == metrics::OmniboxEventProto::CHROMEOS_APP_LIST ||
Tomasz Wiszkowskia8780ff92019-07-25 21:46:05549 IsNTPPage(page_class))) {
[email protected]5b899dc2018-10-02 09:20:01550 return false;
551 }
552
Tomasz Wiszkowski57492ab2019-05-29 21:28:27553 // When omnibox contains pre-populated content, only show zero suggest for
554 // pages with URLs the user will recognize.
555 //
Mark Pearson3fb0e3162018-08-27 21:53:57556 // This list intentionally does not include items such as ftp: and file:
Tommy C. Li06ec26a2019-06-10 18:01:42557 // because (a) these do not work on Android and iOS, where most visited
Mark Pearson3fb0e3162018-08-27 21:53:57558 // zero suggest is launched and (b) on desktop, where contextual zero suggest
559 // is running, these types of schemes aren't eligible to be sent to the
560 // server to ask for suggestions (and thus in practice we won't display zero
561 // suggest for them).
Yue Ru Sune31e1572019-07-19 17:09:57562 if (input_type != metrics::OmniboxInputType::EMPTY &&
Tomasz Wiszkowski57492ab2019-05-29 21:28:27563 !(page_url.is_valid() &&
564 ((page_url.scheme() == url::kHttpScheme) ||
565 (page_url.scheme() == url::kHttpsScheme) ||
566 (page_url.scheme() == url::kAboutScheme) ||
567 (page_url.scheme() ==
568 client()->GetEmbedderRepresentationOfAboutScheme())))) {
[email protected]162c8d9fa2014-03-18 20:25:41569 return false;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27570 }
[email protected]162c8d9fa2014-03-18 20:25:41571
572 return true;
573}
[email protected]855ebff2014-05-09 07:14:38574
575void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Tommy C. Lid77691a2019-06-05 02:07:46576 if (result_type_running_ != REMOTE_NO_URL)
[email protected]855ebff2014-05-09 07:14:38577 return;
578
blundelld130d592015-06-21 19:29:13579 std::string json_data =
580 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38581 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57582 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14583 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00584 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
585 &results_))
[email protected]855ebff2014-05-09 07:14:38586 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38587 }
588}
Gheorghe Comanici9a364f572018-01-24 17:22:00589
590ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
591 const GURL& current_url,
592 const GURL& suggest_url) {
Gheorghe Comanici9a364f572018-01-24 17:22:00593 // Check if the URL can be sent in any suggest request.
594 const TemplateURLService* template_url_service =
595 client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49596 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00597 const TemplateURL* default_provider =
598 template_url_service->GetDefaultSearchProvider();
599 const bool can_send_current_url = CanSendURL(
600 current_url, suggest_url, default_provider, current_page_classification_,
Tommy C. Li71171ee2019-06-20 17:11:47601 template_url_service->search_terms_data(), client(), false);
Gheorghe Comanici9a364f572018-01-24 17:22:00602 // Collect metrics on eligibility.
603 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
604 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
605 if (!can_send_current_url) {
606 const bool can_send_ordinary_url =
607 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
608 current_page_classification_,
Tommy C. Li71171ee2019-06-20 17:11:47609 template_url_service->search_terms_data(), client(), false);
Gheorghe Comanici9a364f572018-01-24 17:22:00610 eligibility = can_send_ordinary_url
611 ? ZeroSuggestEligibility::URL_INELIGIBLE
612 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
613 }
614 UMA_HISTOGRAM_ENUMERATION(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27615 kOmniboxZeroSuggestEligibleHistogramName, static_cast<int>(eligibility),
Gheorghe Comanici9a364f572018-01-24 17:22:00616 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
617
Tommy C. Li0af09562019-06-11 23:58:51618 std::string field_trial_variant =
619 OmniboxFieldTrial::GetZeroSuggestVariant(current_page_classification_);
620
621 if (field_trial_variant == kNoneVariant ||
622 base::FeatureList::IsEnabled(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27623 omnibox::kOmniboxPopupShortcutIconsInZeroState)) {
Kevin Bailey5fea4322018-03-21 22:36:05624 return NONE;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27625 }
Gheorghe Comanici9a364f572018-01-24 17:22:00626
Tommy C. Li0af09562019-06-11 23:58:51627 // TODO(tommycli): Since this can be configured via ZeroSuggestVariant, we
628 // should eliminate this special case and use a field trial configuration.
629 if (current_page_classification_ == OmniboxEventProto::CHROMEOS_APP_LIST)
Tommy C. Lid77691a2019-06-05 02:07:46630 return REMOTE_NO_URL;
Jenny Zhang5bc2e3d2018-09-10 18:50:55631
Tommy C. Li0af09562019-06-11 23:58:51632 if (field_trial_variant == kRemoteNoUrlVariant) {
Moe Ahmadi04206cd2019-08-23 18:03:22633#if defined(OS_ANDROID) || defined(OS_IOS)
Tommy C. Li467c1bcaa2019-06-20 20:48:05634 // TODO(tommycli): It's odd that this doesn't apply to kRemoteSendUrlVariant
635 // as well. Most likely this fallback concept should be replaced by
636 // a more general configuration setup.
Moe Ahmadi04206cd2019-08-23 18:03:22637 if (RemoteSuggestionsShouldFallBackToMostVisited(client(),
638 template_url_service)) {
639 return MOST_VISITED;
640 }
641#endif
642 return REMOTE_NO_URL;
Tommy C. Li393c1b142019-05-22 00:28:41643 }
Gheorghe Comanici9a364f572018-01-24 17:22:00644
Tommy C. Li0af09562019-06-11 23:58:51645 if (field_trial_variant == kRemoteSendUrlVariant && can_send_current_url)
646 return REMOTE_SEND_URL;
Gheorghe Comanici9a364f572018-01-24 17:22:00647
Tommy C. Li0af09562019-06-11 23:58:51648 if (field_trial_variant == kMostVisitedVariant)
649 return MOST_VISITED;
650
651#if defined(OS_ANDROID) || defined(OS_IOS)
652 // For Android and iOS, default to MOST_VISITED so long as:
653 // - There is no configured variant for |page_classification| AND
654 // - The user is not on the search results page of the default search
655 // provider.
656 if (field_trial_variant.empty() &&
657 current_page_classification_ !=
658 OmniboxEventProto::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT &&
659 current_page_classification_ !=
660 OmniboxEventProto::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT) {
Kevin Bailey5fea4322018-03-21 22:36:05661 return MOST_VISITED;
Tommy C. Li393c1b142019-05-22 00:28:41662 }
Tommy C. Li0af09562019-06-11 23:58:51663#endif
Tommy C. Lic79d39ab2019-06-04 16:58:56664
665 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00666}