blob: 8f6468ef6b10372de46ec52ed691e6d66be22a81 [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-ydd768d54b2016-03-25 21:07:4623#include "base/trace_event/trace_event.h"
sdefresnebc766ef2014-09-25 09:28:1324#include "components/history/core/browser/history_types.h"
sdefresne0da3bc02015-01-29 18:26:3525#include "components/history/core/browser/top_sites.h"
blundell2102f7c2015-07-09 10:00:5326#include "components/omnibox/browser/autocomplete_classifier.h"
27#include "components/omnibox/browser/autocomplete_input.h"
28#include "components/omnibox/browser/autocomplete_match.h"
manuk3972b2f2019-04-19 14:22:5629#include "components/omnibox/browser/autocomplete_match_classification.h"
blundell2102f7c2015-07-09 10:00:5330#include "components/omnibox/browser/autocomplete_provider_listener.h"
blundell2102f7c2015-07-09 10:00:5331#include "components/omnibox/browser/history_url_provider.h"
blundell2102f7c2015-07-09 10:00:5332#include "components/omnibox/browser/omnibox_pref_names.h"
Tommy C. Li06ec26a2019-06-10 18:01:4233#include "components/omnibox/browser/remote_suggestions_service.h"
blundell2102f7c2015-07-09 10:00:5334#include "components/omnibox/browser/search_provider.h"
sdefresne70948d62015-08-11 10:46:3535#include "components/omnibox/browser/verbatim_match.h"
Tomasz Wiszkowskid938a1112019-03-06 18:01:5736#include "components/omnibox/common/omnibox_features.h"
[email protected]f0c8c4992014-05-15 17:37:2637#include "components/pref_registry/pref_registry_syncable.h"
brettwf00b9b42016-02-01 22:11:3838#include "components/prefs/pref_service.h"
Gheorghe Comanici864725b2017-11-28 21:48:0139#include "components/search_engines/search_engine_type.h"
[email protected]bf5c532d2014-07-05 00:29:5340#include "components/search_engines/template_url_service.h"
rsleevi24f64dc22015-08-07 21:39:2141#include "components/url_formatter/url_formatter.h"
asvitkine9a279832015-12-18 02:35:5042#include "components/variations/net/variations_http_headers.h"
[email protected]bb1fb2b2013-05-31 00:21:0143#include "net/base/escape.h"
Maks Orlovich1b208512018-06-13 21:08:1744#include "services/network/public/cpp/resource_response.h"
45#include "services/network/public/cpp/shared_url_loader_factory.h"
46#include "services/network/public/cpp/simple_url_loader.h"
Steven Holtef9d5ed62017-10-21 02:02:3047#include "third_party/metrics_proto/omnibox_event.pb.h"
48#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]761fa4702013-07-02 15:25:1549#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0750
51namespace {
[email protected]bb1fb2b2013-05-31 00:21:0152
mpearson3d89cdc2017-03-03 21:15:4553// Represents whether ZeroSuggestProvider is allowed to display contextual
54// suggestions on focus, and if not, why not.
55// These values are written to logs. New enum values can be added, but existing
56// enums must never be renumbered or deleted and reused.
57enum class ZeroSuggestEligibility {
58 ELIGIBLE = 0,
59 // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL
60 // itself prevents ZeroSuggest from triggering.
61 URL_INELIGIBLE = 1,
62 GENERALLY_INELIGIBLE = 2,
63 ELIGIBLE_MAX_VALUE
64};
65
[email protected]bb1fb2b2013-05-31 00:21:0166// TODO(hfung): The histogram code was copied and modified from
67// search_provider.cc. Refactor and consolidate the code.
68// We keep track in a histogram how many suggest requests we send, how
69// many suggest requests we invalidate (e.g., due to a user typing
70// another character), and how many replies we receive.
mpearson3d89cdc2017-03-03 21:15:4571// These values are written to logs. New enum values can be added, but existing
72// enums must never be renumbered or deleted and reused.
[email protected]bb1fb2b2013-05-31 00:21:0173enum ZeroSuggestRequestsHistogramValue {
74 ZERO_SUGGEST_REQUEST_SENT = 1,
mpearson3d89cdc2017-03-03 21:15:4575 ZERO_SUGGEST_REQUEST_INVALIDATED = 2,
76 ZERO_SUGGEST_REPLY_RECEIVED = 3,
[email protected]bb1fb2b2013-05-31 00:21:0177 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
78};
79
80void LogOmniboxZeroSuggestRequest(
81 ZeroSuggestRequestsHistogramValue request_value) {
82 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
83 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
84}
85
[email protected]bb1fb2b2013-05-31 00:21:0186// Relevance value to use if it was not set explicitly by the server.
87const int kDefaultZeroSuggestRelevance = 100;
88
mpearson3d89cdc2017-03-03 21:15:4589// Used for testing whether zero suggest is ever available.
mpearsonc90c24b2017-03-04 00:11:2690constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/";
mpearson3d89cdc2017-03-03 21:15:4591
Tomasz Wiszkowski57492ab2019-05-29 21:28:2792// Metric name tracking the omnibox suggestion eligibility.
93constexpr char kOmniboxZeroSuggestEligibleHistogramName[] =
94 "Omnibox.ZeroSuggest.Eligible.OnFocusV2";
95
Gheorghe Comanici9a364f572018-01-24 17:22:0096// If the user is not signed-in or the user does not have Google set up as their
97// default search engine, the personalized service is replaced with the most
98// visited service.
99bool PersonalizedServiceShouldFallBackToMostVisited(
Tomasz Wiszkowski5abea202019-06-06 22:51:14100 AutocompleteProviderClient* client,
Gheorghe Comanici864725b2017-11-28 21:48:01101 const TemplateURLService* template_url_service) {
Tomasz Wiszkowski5abea202019-06-06 22:51:14102 if (!client->SearchSuggestEnabled())
103 return true;
104
105 if (!client->IsAuthenticated())
Gheorghe Comanici864725b2017-11-28 21:48:01106 return true;
107
Gheorghe Comanici9a364f572018-01-24 17:22:00108 if (template_url_service == nullptr)
109 return false;
Gheorghe Comanici682504102017-11-30 17:47:25110
Gheorghe Comanici9a364f572018-01-24 17:22:00111 const TemplateURL* default_provider =
112 template_url_service->GetDefaultSearchProvider();
113 return default_provider == nullptr ||
114 default_provider->GetEngineType(
115 template_url_service->search_terms_data()) != SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01116}
117
[email protected]6ce7f612012-09-05 23:53:07118} // namespace
119
[email protected]a00008d42012-09-15 05:07:58120// static
121ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18122 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11123 HistoryURLProvider* history_url_provider,
blundelld130d592015-06-21 19:29:13124 AutocompleteProviderListener* listener) {
mpearson931028c2016-07-01 18:55:11125 return new ZeroSuggestProvider(client, history_url_provider, listener);
[email protected]6ce7f612012-09-05 23:53:07126}
127
[email protected]855ebff2014-05-09 07:14:38128// static
129void ZeroSuggestProvider::RegisterProfilePrefs(
130 user_prefs::PrefRegistrySyncable* registry) {
blundelld130d592015-06-21 19:29:13131 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
132 std::string());
[email protected]855ebff2014-05-09 07:14:38133}
134
[email protected]6ce7f612012-09-05 23:53:07135void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18136 bool minimal_changes) {
a-v-ydd768d54b2016-03-25 21:07:46137 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54138 matches_.clear();
Kevin Bailey8642e612018-04-23 20:27:54139 Stop(true, false);
Jenny Zhang5bc2e3d2018-09-10 18:50:55140
Tomasz Wiszkowski57492ab2019-05-29 21:28:27141 if (!AllowZeroSuggestSuggestions(input)) {
142 UMA_HISTOGRAM_ENUMERATION(kOmniboxZeroSuggestEligibleHistogramName,
143 ZeroSuggestEligibility::GENERALLY_INELIGIBLE,
144 ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE);
[email protected]f030c4d2014-03-25 01:05:54145 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27146 }
[email protected]bb1fb2b2013-05-31 00:21:01147
Kevin Bailey5fea4322018-03-21 22:36:05148 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13149 set_field_trial_triggered(false);
150 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54151 permanent_text_ = input.text();
152 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54153 current_title_ = input.current_title();
[email protected]f030c4d2014-03-25 01:05:54154 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52155 current_url_match_ = MatchForCurrentURL();
156
Tommy C. Li06ec26a2019-06-10 18:01:42157 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Jenny Zhang5bc2e3d2018-09-10 18:50:55158 /*current_url=*/"", input, client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41159 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07160 return;
[email protected]162c8d9fa2014-03-18 20:25:41161
Gheorghe Comanici9a364f572018-01-24 17:22:00162 result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url);
Kevin Bailey5fea4322018-03-21 22:36:05163 if (result_type_running_ == NONE)
[email protected]162c8d9fa2014-03-18 20:25:41164 return;
[email protected]162c8d9fa2014-03-18 20:25:41165
[email protected]6ce7f612012-09-05 23:53:07166 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11167
[email protected]855ebff2014-05-09 07:14:38168 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11169
Kevin Bailey5fea4322018-03-21 22:36:05170 if (result_type_running_ == MOST_VISITED) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11171 most_visited_urls_.clear();
172 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
Gheorghe Comanici9a364f572018-01-24 17:22:00173 if (!ts) {
174 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05175 result_type_running_ = NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00176 return;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11177 }
Gheorghe Comanici9a364f572018-01-24 17:22:00178
Kevin Bailey40bb0ac52019-04-02 00:43:32179 ts->GetMostVisitedURLs(base::BindRepeating(
180 &ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
181 weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11182 return;
183 }
184
Tommy C. Lid77691a2019-06-05 02:07:46185 const std::string current_url =
186 result_type_running_ == REMOTE_SEND_URL ? current_query_ : std::string();
Maks Orlovich1b208512018-06-13 21:08:17187 // Create a request for suggestions, routing completion to
Tommy C. Li06ec26a2019-06-10 18:01:42188 // OnRemoteSuggestionsLoaderAvailable.
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11189 client()
Tommy C. Li06ec26a2019-06-10 18:01:42190 ->GetRemoteSuggestionsService(/*create_if_necessary=*/true)
191 ->CreateSuggestionsRequest(
Jenny Zhang5bc2e3d2018-09-10 18:50:55192 current_url, client()->GetCurrentVisitTimestamp(), input,
Gheorghe Comanici034cff62018-01-27 03:34:00193 client()->GetTemplateURLService(),
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11194 base::BindOnce(
Tommy C. Li06ec26a2019-06-10 18:01:42195 &ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable,
Maks Orlovich1b208512018-06-13 21:08:17196 weak_ptr_factory_.GetWeakPtr()),
197 base::BindOnce(
198 &ZeroSuggestProvider::OnURLLoadComplete,
199 base::Unretained(this) /* this owns SimpleURLLoader */));
[email protected]6ce7f612012-09-05 23:53:07200}
201
mpearson8a37c382015-03-07 05:58:57202void ZeroSuggestProvider::Stop(bool clear_cached_results,
203 bool due_to_user_inactivity) {
Maks Orlovich1b208512018-06-13 21:08:17204 if (loader_)
[email protected]ec3f679b2014-08-18 07:45:13205 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
Maks Orlovich1b208512018-06-13 21:08:17206 loader_.reset();
Tommy C. Li06ec26a2019-06-10 18:01:42207 auto* remote_suggestions_service =
208 client()->GetRemoteSuggestionsService(/*create_if_necessary=*/false);
209 // remote_suggestions_service can be null if in incognito mode.
210 if (remote_suggestions_service != nullptr) {
211 remote_suggestions_service->StopCreatingSuggestionsRequest();
Gheorghe Comanici86bbdf62017-08-28 17:20:33212 }
Kevin Bailey5fea4322018-03-21 22:36:05213 // TODO(krb): It would allow us to remove some guards if we could also cancel
214 // the TopSites::GetMostVisitedURLs request.
[email protected]ec3f679b2014-08-18 07:45:13215 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05216 result_type_running_ = NONE;
217 ++most_visited_request_num_;
[email protected]ec3f679b2014-08-18 07:45:13218
219 if (clear_cached_results) {
220 // We do not call Clear() on |results_| to retain |verbatim_relevance|
221 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18222 // beginning of the next call to Start() to determine the current url
223 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13224 results_.suggest_results.clear();
225 results_.navigation_results.clear();
226 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54227 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45228 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13229 }
230}
231
[email protected]855ebff2014-05-09 07:14:38232void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Tommy C. Li393c1b142019-05-22 00:28:41233 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
234 current_page_classification_)) {
[email protected]855ebff2014-05-09 07:14:38235 // Remove the deleted match from the cache, so it is not shown to the user
236 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13237 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38238 std::string());
239 }
240 BaseSearchProvider::DeleteMatch(match);
241}
242
[email protected]ec3f679b2014-08-18 07:45:13243void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
244 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45245 if (!results_.suggest_results.empty() ||
246 !results_.navigation_results.empty() ||
247 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13248 provider_info->back().set_times_returned_results_in_session(1);
249}
250
[email protected]f030c4d2014-03-25 01:05:54251void ZeroSuggestProvider::ResetSession() {
252 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13253 // |field_trial_triggered_in_session| unchanged and set
254 // |field_trial_triggered| to false since zero suggest is inactive now.
255 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54256}
257
mpearson931028c2016-07-01 18:55:11258ZeroSuggestProvider::ZeroSuggestProvider(
259 AutocompleteProviderClient* client,
260 HistoryURLProvider* history_url_provider,
261 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13262 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
mpearson931028c2016-07-01 18:55:11263 history_url_provider_(history_url_provider),
[email protected]776ee5902014-08-11 09:15:19264 listener_(listener),
Kevin Bailey5fea4322018-03-21 22:36:05265 result_type_running_(NONE),
[email protected]8f064e52013-09-18 01:17:14266 weak_ptr_factory_(this) {
Tommy C. Li06ec26a2019-06-10 18:01:42267 // Record whether remote zero suggest is possible for this user / profile.
mpearson3d89cdc2017-03-03 21:15:45268 const TemplateURLService* template_url_service =
269 client->GetTemplateURLService();
270 // Template URL service can be null in tests.
271 if (template_url_service != nullptr) {
Jenny Zhang5bc2e3d2018-09-10 18:50:55272 AutocompleteInput empty_input;
Tommy C. Li06ec26a2019-06-10 18:01:42273 GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
Jenny Zhang5bc2e3d2018-09-10 18:50:55274 /*current_url=*/"", /*empty input*/ empty_input, template_url_service);
mpearson3d89cdc2017-03-03 21:15:45275 // To check whether this is allowed, use an arbitrary insecure (http) URL
276 // as the URL we'd want suggestions for. The value of OTHER as the current
277 // page classification is to correspond with that URL.
278 UMA_HISTOGRAM_BOOLEAN(
279 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
280 suggest_url.is_valid() &&
281 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
282 template_url_service->GetDefaultSearchProvider(),
283 metrics::OmniboxEventProto::OTHER,
284 template_url_service->search_terms_data(), client));
285 }
[email protected]6ce7f612012-09-05 23:53:07286}
287
288ZeroSuggestProvider::~ZeroSuggestProvider() {
289}
290
[email protected]776ee5902014-08-11 09:15:19291const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
292 // Zero suggest provider should not receive keyword results.
293 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13294 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19295}
296
297const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18298 // The callers of this method won't look at the AutocompleteInput's
299 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02300 AutocompleteInput input(base::string16(), current_page_classification_,
301 client()->GetSchemeClassifier());
302 input.set_current_url(GURL(current_query_));
303 input.set_current_title(current_title_);
304 input.set_prevent_inline_autocomplete(true);
305 input.set_allow_exact_keyword_match(false);
306 return input;
[email protected]776ee5902014-08-11 09:15:19307}
308
309bool ZeroSuggestProvider::ShouldAppendExtraParams(
310 const SearchSuggestionParser::SuggestResult& result) const {
311 // We always use the default provider for search, so append the params.
312 return true;
313}
314
[email protected]776ee5902014-08-11 09:15:19315void ZeroSuggestProvider::RecordDeletionResult(bool success) {
316 if (success) {
317 base::RecordAction(
318 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
319 } else {
320 base::RecordAction(
321 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
322 }
323}
324
Maks Orlovich1b208512018-06-13 21:08:17325void ZeroSuggestProvider::OnURLLoadComplete(
326 const network::SimpleURLLoader* source,
327 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19328 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17329 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19330
331 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
332
Gheorghe Comanici9a364f572018-01-24 17:22:00333 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17334 response_body && source->NetError() == net::OK &&
335 (source->ResponseInfo() && source->ResponseInfo()->headers &&
336 source->ResponseInfo()->headers->response_code() == 200) &&
337 UpdateResults(SearchSuggestionParser::ExtractJsonData(
338 source, std::move(response_body)));
339 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19340 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05341 result_type_running_ = NONE;
342 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19343 listener_->OnProviderUpdate(results_updated);
344}
345
Gheorghe Comanici9a364f572018-01-24 17:22:00346bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
347 std::unique_ptr<base::Value> data(
348 SearchSuggestionParser::DeserializeJsonData(json_data));
349 if (!data)
[email protected]855ebff2014-05-09 07:14:38350 return false;
351
Gheorghe Comanici9a364f572018-01-24 17:22:00352 // When running the personalized service, we want to store suggestion
353 // responses if non-empty.
Tommy C. Lid77691a2019-06-05 02:07:46354 if (result_type_running_ == REMOTE_NO_URL && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00355 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
356 json_data);
[email protected]855ebff2014-05-09 07:14:38357
Gheorghe Comanici9a364f572018-01-24 17:22:00358 // If we received an empty result list, we should update the display, as it
359 // may be showing cached results that should not be shown.
360 const base::ListValue* root_list = nullptr;
361 const base::ListValue* results_list = nullptr;
362 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
363 root_list->GetList(1, &results_list) &&
364 !results_list->empty();
365 const bool non_empty_cache = !results_.suggest_results.empty() ||
366 !results_.navigation_results.empty();
367 if (non_empty_parsed_list && non_empty_cache)
368 return false;
369 }
370 const bool results_updated = ParseSuggestResults(
371 *data, kDefaultZeroSuggestRelevance, false, &results_);
372 ConvertResultsToAutocompleteMatches();
373 return results_updated;
[email protected]855ebff2014-05-09 07:14:38374}
375
[email protected]bb1fb2b2013-05-31 00:21:01376void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37377 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30378 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22379 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11380 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01381}
382
[email protected]bb1fb2b2013-05-31 00:21:01383AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37384 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01385 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47386 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01387 match.destination_url = navigation.url();
388
[email protected]bb1fb2b2013-05-31 00:21:01389 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13390 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26391 navigation.url(), url_formatter::FormatUrl(navigation.url()),
Kevin Bailey83e643d2018-03-08 16:01:41392 client()->GetSchemeClassifier(), nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01393
manuk3972b2f2019-04-19 14:22:56394 // Zero suggest results should always omit protocols and never appear bold.
395 auto format_types = AutocompleteMatch::GetFormatTypes(false, false);
396 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
397 net::UnescapeRule::SPACES, nullptr,
398 nullptr, nullptr);
399 match.contents_class = ClassifyTermMatches({}, match.contents.length(), 0,
400 ACMatchClassification::URL);
[email protected]9c97f89c2013-06-25 03:12:16401
402 match.description =
403 AutocompleteMatch::SanitizeString(navigation.description());
manuk3972b2f2019-04-19 14:22:56404 match.description_class = ClassifyTermMatches({}, match.description.length(),
405 0, ACMatchClassification::NONE);
406
gcomanici67d53ac2017-04-01 17:07:19407 match.subtype_identifier = navigation.subtype_identifier();
[email protected]bb1fb2b2013-05-31 00:21:01408 return match;
409}
410
[email protected]8f064e52013-09-18 01:17:14411void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
Kevin Bailey5fea4322018-03-21 22:36:05412 size_t orig_request_num,
[email protected]8f064e52013-09-18 01:17:14413 const history::MostVisitedURLList& urls) {
Kevin Bailey5fea4322018-03-21 22:36:05414 if (result_type_running_ != MOST_VISITED ||
415 orig_request_num != most_visited_request_num_) {
Gheorghe Comanici9a364f572018-01-24 17:22:00416 return;
Kevin Bailey5fea4322018-03-21 22:36:05417 }
[email protected]8f064e52013-09-18 01:17:14418 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22419 done_ = true;
420 ConvertResultsToAutocompleteMatches();
Kevin Bailey5fea4322018-03-21 22:36:05421 result_type_running_ = NONE;
422 ++most_visited_request_num_;
mariakhomenkobfc3a2a2014-10-24 00:48:22423 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14424}
425
Tommy C. Li06ec26a2019-06-10 18:01:42426void ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable(
Maks Orlovich1b208512018-06-13 21:08:17427 std::unique_ptr<network::SimpleURLLoader> loader) {
Tommy C. Li06ec26a2019-06-10 18:01:42428 // RemoteSuggestionsService has already started |loader|, so here it's
Maks Orlovich1b208512018-06-13 21:08:17429 // only neccessary to grab its ownership until results come in to
430 // OnURLLoadComplete().
431 loader_ = std::move(loader);
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11432 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
433}
434
[email protected]9c97f89c2013-06-25 03:12:16435void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01436 matches_.clear();
437
blundelld130d592015-06-21 19:29:13438 TemplateURLService* template_url_service = client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49439 DCHECK(template_url_service);
[email protected]bb1fb2b2013-05-31 00:21:01440 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13441 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07442 // Fail if we can't set the clickthrough URL for query suggestions.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24443 if (default_provider == nullptr ||
blundelld130d592015-06-21 19:29:13444 !default_provider->SupportsReplacement(
445 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07446 return;
[email protected]6ce7f612012-09-05 23:53:07447
[email protected]00404742014-02-20 13:09:05448 MatchMap map;
449 AddSuggestResultsToMap(results_.suggest_results, &map);
450
451 const int num_query_results = map.size();
452 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01453 const int num_results = num_query_results + num_nav_results;
Steven Holte95922222018-09-14 20:06:23454 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.QueryResults", num_query_results);
455 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.URLResults", num_nav_results);
456 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01457
[email protected]8f064e52013-09-18 01:17:14458 // Show Most Visited results after ZeroSuggest response is received.
Kevin Bailey5fea4322018-03-21 22:36:05459 if (result_type_running_ == MOST_VISITED) {
Tomasz Wiszkowski57492ab2019-05-29 21:28:27460 // Ensure we don't show most visited URL suggestions on NTP.
461 // This allows us to prevent undesired side outcome of presenting
462 // URL suggestions to users who are not in the personalized field trial for
463 // zero query suggestions.
464 if (IsNTPPage(current_page_classification_) ||
465 !current_url_match_.destination_url.is_valid()) {
[email protected]3feb8b002013-10-14 23:50:13466 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27467 }
[email protected]8f064e52013-09-18 01:17:14468 matches_.push_back(current_url_match_);
469 int relevance = 600;
470 if (num_results > 0) {
Steven Holte95922222018-09-14 20:06:23471 UMA_HISTOGRAM_COUNTS_1M(
[email protected]8f064e52013-09-18 01:17:14472 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
473 most_visited_urls_.size());
474 }
[email protected]23db6492014-01-16 02:35:30475 const base::string16 current_query_string16(
476 base::ASCIIToUTF16(current_query_));
[email protected]8f064e52013-09-18 01:17:14477 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
478 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37479 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13480 client()->GetSchemeClassifier(), url.url,
gcomanici67d53ac2017-04-01 17:07:19481 AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false,
jshin1fb76462016-04-05 22:13:03482 relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14483 matches_.push_back(NavigationToMatch(nav));
484 --relevance;
485 }
486 return;
487 }
488
[email protected]9c97f89c2013-06-25 03:12:16489 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01490 return;
491
Tomasz Wiszkowski57492ab2019-05-29 21:28:27492 // Do not add the default URL match if we're on the NTP to prevent
493 // chrome-native://newtab or chrome://newtab from showing up on the list of
494 // suggestions.
495 if (!IsNTPPage(current_page_classification_) &&
496 current_url_match_.destination_url.is_valid()) {
Jenny Zhang5bc2e3d2018-09-10 18:50:55497 matches_.push_back(current_url_match_);
Tomasz Wiszkowski57492ab2019-05-29 21:28:27498 }
499
[email protected]00404742014-02-20 13:09:05500 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01501 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01502
[email protected]0b9575f2014-07-30 11:58:37503 const SearchSuggestionParser::NavigationResults& nav_results(
504 results_.navigation_results);
jdoerrie2e6a651d2018-10-04 17:09:08505 for (auto it = nav_results.begin(); it != nav_results.end(); ++it) {
[email protected]bb1fb2b2013-05-31 00:21:01506 matches_.push_back(NavigationToMatch(*it));
gcomanici67d53ac2017-04-01 17:07:19507 }
[email protected]6ce7f612012-09-05 23:53:07508}
509
[email protected]bb1fb2b2013-05-31 00:21:01510AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]bb1fb2b2013-05-31 00:21:01511 // The placeholder suggestion for the current URL has high relevance so
512 // that it is in the first suggestion slot and inline autocompleted. It
513 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11514 AutocompleteInput tmp(GetInput(false));
515 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54516 const base::string16 description =
517 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
518 ? current_title_
519 : base::string16();
520 return VerbatimMatchForURL(client(), tmp, GURL(current_query_), description,
mpearson931028c2016-07-01 18:55:11521 history_url_provider_,
sdefresne70948d62015-08-11 10:46:35522 results_.verbatim_relevance);
[email protected]00404742014-02-20 13:09:05523}
[email protected]162c8d9fa2014-03-18 20:25:41524
Gheorghe Comanici9a364f572018-01-24 17:22:00525bool ZeroSuggestProvider::AllowZeroSuggestSuggestions(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27526 const AutocompleteInput& input) const {
527 const auto& page_url = input.current_url();
528 const auto page_class = input.current_page_classification();
529 const auto input_type = input.type();
Peter Kastingfb1a8ea2017-11-28 02:26:50530
Tomasz Wiszkowski57492ab2019-05-29 21:28:27531 if (!input.from_omnibox_focus())
532 return false;
533
Peter Kastingfb1a8ea2017-11-28 02:26:50534 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41535 return false;
536
Tomasz Wiszkowski57492ab2019-05-29 21:28:27537 // Check if ZeroSuggest is allowed in an empty state.
538 if (input_type == metrics::OmniboxInputType::INVALID &&
539 !(page_class == metrics::OmniboxEventProto::CHROMEOS_APP_LIST ||
540 (IsNTPPage(page_class) &&
541 base::FeatureList::IsEnabled(omnibox::kZeroSuggestionsOnNTP)))) {
[email protected]5b899dc2018-10-02 09:20:01542 return false;
543 }
544
Tomasz Wiszkowski57492ab2019-05-29 21:28:27545 // When omnibox contains pre-populated content, only show zero suggest for
546 // pages with URLs the user will recognize.
547 //
Mark Pearson3fb0e3162018-08-27 21:53:57548 // This list intentionally does not include items such as ftp: and file:
Tommy C. Li06ec26a2019-06-10 18:01:42549 // because (a) these do not work on Android and iOS, where most visited
Mark Pearson3fb0e3162018-08-27 21:53:57550 // zero suggest is launched and (b) on desktop, where contextual zero suggest
551 // is running, these types of schemes aren't eligible to be sent to the
552 // server to ask for suggestions (and thus in practice we won't display zero
553 // suggest for them).
Tomasz Wiszkowski57492ab2019-05-29 21:28:27554 if (input_type != metrics::OmniboxInputType::INVALID &&
555 !(page_url.is_valid() &&
556 ((page_url.scheme() == url::kHttpScheme) ||
557 (page_url.scheme() == url::kHttpsScheme) ||
558 (page_url.scheme() == url::kAboutScheme) ||
559 (page_url.scheme() ==
560 client()->GetEmbedderRepresentationOfAboutScheme())))) {
[email protected]162c8d9fa2014-03-18 20:25:41561 return false;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27562 }
[email protected]162c8d9fa2014-03-18 20:25:41563
564 return true;
565}
[email protected]855ebff2014-05-09 07:14:38566
567void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Tommy C. Lid77691a2019-06-05 02:07:46568 if (result_type_running_ != REMOTE_NO_URL)
[email protected]855ebff2014-05-09 07:14:38569 return;
570
blundelld130d592015-06-21 19:29:13571 std::string json_data =
572 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38573 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57574 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14575 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00576 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
577 &results_))
[email protected]855ebff2014-05-09 07:14:38578 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38579 }
580}
Gheorghe Comanici9a364f572018-01-24 17:22:00581
582ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
583 const GURL& current_url,
584 const GURL& suggest_url) {
Gheorghe Comanici9a364f572018-01-24 17:22:00585 // Check if the URL can be sent in any suggest request.
586 const TemplateURLService* template_url_service =
587 client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49588 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00589 const TemplateURL* default_provider =
590 template_url_service->GetDefaultSearchProvider();
591 const bool can_send_current_url = CanSendURL(
592 current_url, suggest_url, default_provider, current_page_classification_,
593 template_url_service->search_terms_data(), client());
Gheorghe Comanici9a364f572018-01-24 17:22:00594 // Collect metrics on eligibility.
595 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
596 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
597 if (!can_send_current_url) {
598 const bool can_send_ordinary_url =
599 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
600 current_page_classification_,
601 template_url_service->search_terms_data(), client());
602 eligibility = can_send_ordinary_url
603 ? ZeroSuggestEligibility::URL_INELIGIBLE
604 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
605 }
606 UMA_HISTOGRAM_ENUMERATION(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27607 kOmniboxZeroSuggestEligibleHistogramName, static_cast<int>(eligibility),
Gheorghe Comanici9a364f572018-01-24 17:22:00608 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
609
Tomasz Wiszkowski57492ab2019-05-29 21:28:27610 if (base::FeatureList::IsEnabled(
611 omnibox::kOmniboxPopupShortcutIconsInZeroState)) {
Kevin Bailey5fea4322018-03-21 22:36:05612 return NONE;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27613 }
Gheorghe Comanici9a364f572018-01-24 17:22:00614
Jenny Zhang5bc2e3d2018-09-10 18:50:55615 if (current_page_classification_ ==
616 metrics::OmniboxEventProto::CHROMEOS_APP_LIST) {
Tommy C. Lid77691a2019-06-05 02:07:46617 return REMOTE_NO_URL;
Jenny Zhang5bc2e3d2018-09-10 18:50:55618 }
619
Tommy C. Li393c1b142019-05-22 00:28:41620 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
621 current_page_classification_)) {
Tomasz Wiszkowski5abea202019-06-06 22:51:14622 return PersonalizedServiceShouldFallBackToMostVisited(client(),
623 template_url_service)
Kevin Bailey5fea4322018-03-21 22:36:05624 ? MOST_VISITED
Tommy C. Lid77691a2019-06-05 02:07:46625 : REMOTE_NO_URL;
Tommy C. Li393c1b142019-05-22 00:28:41626 }
Gheorghe Comanici9a364f572018-01-24 17:22:00627
Tommy C. Li393c1b142019-05-22 00:28:41628 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial(
629 current_page_classification_) &&
Gheorghe Comanici9a364f572018-01-24 17:22:00630 client()
631 ->GetTemplateURLService()
Tommy C. Li393c1b142019-05-22 00:28:41632 ->IsSearchResultsPageFromDefaultSearchProvider(current_url)) {
Kevin Bailey5fea4322018-03-21 22:36:05633 return NONE;
Tommy C. Li393c1b142019-05-22 00:28:41634 }
Gheorghe Comanici9a364f572018-01-24 17:22:00635
Tommy C. Li393c1b142019-05-22 00:28:41636 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial(
637 current_page_classification_)) {
Kevin Bailey5fea4322018-03-21 22:36:05638 return MOST_VISITED;
Tommy C. Li393c1b142019-05-22 00:28:41639 }
Gheorghe Comanici9a364f572018-01-24 17:22:00640
Tommy C. Lic79d39ab2019-06-04 16:58:56641 if (can_send_current_url &&
642 OmniboxFieldTrial::InZeroSuggestRemoteSendURLFieldTrial(
643 current_page_classification_)) {
Tommy C. Lid77691a2019-06-05 02:07:46644 return REMOTE_SEND_URL;
Tommy C. Lic79d39ab2019-06-04 16:58:56645 }
646
647 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00648}