blob: df6e08e5c8be323bc815bb626b00edd6894d5a95 [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"
amohammadkhanf76ae112015-09-14 17:34:4324#include "components/data_use_measurement/core/data_use_user_data.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"
30#include "components/omnibox/browser/autocomplete_provider_listener.h"
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:1131#include "components/omnibox/browser/contextual_suggestions_service.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"
34#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
Gheorghe Comanici9a364f572018-01-24 17:22:0092// If the user is not signed-in or the user does not have Google set up as their
93// default search engine, the personalized service is replaced with the most
94// visited service.
95bool PersonalizedServiceShouldFallBackToMostVisited(
Gheorghe Comanici864725b2017-11-28 21:48:0196 PrefService* prefs,
Gheorghe Comanici682504102017-11-30 17:47:2597 bool is_authenticated,
Gheorghe Comanici864725b2017-11-28 21:48:0198 const TemplateURLService* template_url_service) {
Gheorghe Comanici9a364f572018-01-24 17:22:0099 if (!is_authenticated)
Gheorghe Comanici864725b2017-11-28 21:48:01100 return true;
101
Gheorghe Comanici9a364f572018-01-24 17:22:00102 if (template_url_service == nullptr)
103 return false;
Gheorghe Comanici682504102017-11-30 17:47:25104
Gheorghe Comanici9a364f572018-01-24 17:22:00105 const TemplateURL* default_provider =
106 template_url_service->GetDefaultSearchProvider();
107 return default_provider == nullptr ||
108 default_provider->GetEngineType(
109 template_url_service->search_terms_data()) != SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01110}
111
[email protected]6ce7f612012-09-05 23:53:07112} // namespace
113
[email protected]a00008d42012-09-15 05:07:58114// static
115ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18116 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11117 HistoryURLProvider* history_url_provider,
blundelld130d592015-06-21 19:29:13118 AutocompleteProviderListener* listener) {
mpearson931028c2016-07-01 18:55:11119 return new ZeroSuggestProvider(client, history_url_provider, listener);
[email protected]6ce7f612012-09-05 23:53:07120}
121
[email protected]855ebff2014-05-09 07:14:38122// static
123void ZeroSuggestProvider::RegisterProfilePrefs(
124 user_prefs::PrefRegistrySyncable* registry) {
blundelld130d592015-06-21 19:29:13125 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
126 std::string());
[email protected]855ebff2014-05-09 07:14:38127}
128
[email protected]6ce7f612012-09-05 23:53:07129void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18130 bool minimal_changes) {
a-v-ydd768d52016-03-25 21:07:46131 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54132 matches_.clear();
Kevin Bailey8642e612018-04-23 20:27:54133 Stop(true, false);
Jenny Zhang5bc2e3d2018-09-10 18:50:55134 if (!input.from_omnibox_focus() || client()->IsOffTheRecord())
135 return;
136
137 // Zero suggest is allowed to run in the Chrome OS app_list context
138 // with invalid (empty) input.
139 if (input.type() == metrics::OmniboxInputType::INVALID &&
140 input.current_page_classification() !=
141 metrics::OmniboxEventProto::CHROMEOS_APP_LIST)
[email protected]f030c4d2014-03-25 01:05:54142 return;
[email protected]bb1fb2b2013-05-31 00:21:01143
Kevin Bailey5fea4322018-03-21 22:36:05144 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13145 set_field_trial_triggered(false);
146 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54147 permanent_text_ = input.text();
148 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54149 current_title_ = input.current_title();
[email protected]f030c4d2014-03-25 01:05:54150 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52151 current_url_match_ = MatchForCurrentURL();
152
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11153 GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl(
Jenny Zhang5bc2e3d2018-09-10 18:50:55154 /*current_url=*/"", input, client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41155 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07156 return;
[email protected]162c8d9fa2014-03-18 20:25:41157
Gheorghe Comanici9a364f572018-01-24 17:22:00158 result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url);
Kevin Bailey5fea4322018-03-21 22:36:05159 if (result_type_running_ == NONE)
[email protected]162c8d9fa2014-03-18 20:25:41160 return;
[email protected]162c8d9fa2014-03-18 20:25:41161
[email protected]6ce7f612012-09-05 23:53:07162 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11163
[email protected]855ebff2014-05-09 07:14:38164 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11165
Kevin Bailey5fea4322018-03-21 22:36:05166 if (result_type_running_ == MOST_VISITED) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11167 most_visited_urls_.clear();
168 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
Gheorghe Comanici9a364f572018-01-24 17:22:00169 if (!ts) {
170 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05171 result_type_running_ = NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00172 return;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11173 }
Gheorghe Comanici9a364f572018-01-24 17:22:00174
175 ts->GetMostVisitedURLs(
176 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
Kristi Parkac1a89a2018-12-14 22:20:00177 weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11178 return;
179 }
180
Kevin Bailey5fea4322018-03-21 22:36:05181 const std::string current_url = result_type_running_ == DEFAULT_SERP_FOR_URL
182 ? current_query_
183 : std::string();
Maks Orlovich1b208512018-06-13 21:08:17184 // Create a request for suggestions, routing completion to
185 // OnContextualSuggestionsLoaderAvailable.
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11186 client()
Gheorghe Comanici86bbdf62017-08-28 17:20:33187 ->GetContextualSuggestionsService(/*create_if_necessary=*/true)
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11188 ->CreateContextualSuggestionsRequest(
Jenny Zhang5bc2e3d2018-09-10 18:50:55189 current_url, client()->GetCurrentVisitTimestamp(), input,
Gheorghe Comanici034cff62018-01-27 03:34:00190 client()->GetTemplateURLService(),
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11191 base::BindOnce(
Maks Orlovich1b208512018-06-13 21:08:17192 &ZeroSuggestProvider::OnContextualSuggestionsLoaderAvailable,
193 weak_ptr_factory_.GetWeakPtr()),
194 base::BindOnce(
195 &ZeroSuggestProvider::OnURLLoadComplete,
196 base::Unretained(this) /* this owns SimpleURLLoader */));
[email protected]6ce7f612012-09-05 23:53:07197}
198
mpearson8a37c382015-03-07 05:58:57199void ZeroSuggestProvider::Stop(bool clear_cached_results,
200 bool due_to_user_inactivity) {
Maks Orlovich1b208512018-06-13 21:08:17201 if (loader_)
[email protected]ec3f679b2014-08-18 07:45:13202 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
Maks Orlovich1b208512018-06-13 21:08:17203 loader_.reset();
Gheorghe Comanici86bbdf62017-08-28 17:20:33204 auto* contextual_suggestions_service =
205 client()->GetContextualSuggestionsService(/*create_if_necessary=*/false);
206 // contextual_suggestions_service can be null if in incognito mode.
207 if (contextual_suggestions_service != nullptr) {
208 contextual_suggestions_service->StopCreatingContextualSuggestionsRequest();
209 }
Kevin Bailey5fea4322018-03-21 22:36:05210 // TODO(krb): It would allow us to remove some guards if we could also cancel
211 // the TopSites::GetMostVisitedURLs request.
[email protected]ec3f679b2014-08-18 07:45:13212 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05213 result_type_running_ = NONE;
214 ++most_visited_request_num_;
[email protected]ec3f679b2014-08-18 07:45:13215
216 if (clear_cached_results) {
217 // We do not call Clear() on |results_| to retain |verbatim_relevance|
218 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18219 // beginning of the next call to Start() to determine the current url
220 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13221 results_.suggest_results.clear();
222 results_.navigation_results.clear();
223 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54224 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45225 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13226 }
227}
228
[email protected]855ebff2014-05-09 07:14:38229void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Theresa3db4ae22018-03-05 18:47:40230 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) {
[email protected]855ebff2014-05-09 07:14:38231 // Remove the deleted match from the cache, so it is not shown to the user
232 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13233 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38234 std::string());
235 }
236 BaseSearchProvider::DeleteMatch(match);
237}
238
[email protected]ec3f679b2014-08-18 07:45:13239void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
240 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45241 if (!results_.suggest_results.empty() ||
242 !results_.navigation_results.empty() ||
243 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13244 provider_info->back().set_times_returned_results_in_session(1);
245}
246
[email protected]f030c4d2014-03-25 01:05:54247void ZeroSuggestProvider::ResetSession() {
248 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13249 // |field_trial_triggered_in_session| unchanged and set
250 // |field_trial_triggered| to false since zero suggest is inactive now.
251 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54252}
253
mpearson931028c2016-07-01 18:55:11254ZeroSuggestProvider::ZeroSuggestProvider(
255 AutocompleteProviderClient* client,
256 HistoryURLProvider* history_url_provider,
257 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13258 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
mpearson931028c2016-07-01 18:55:11259 history_url_provider_(history_url_provider),
[email protected]776ee5902014-08-11 09:15:19260 listener_(listener),
Kevin Bailey5fea4322018-03-21 22:36:05261 result_type_running_(NONE),
[email protected]8f064e52013-09-18 01:17:14262 weak_ptr_factory_(this) {
mpearson3d89cdc2017-03-03 21:15:45263 // Record whether contextual zero suggest is possible for this user / profile.
264 const TemplateURLService* template_url_service =
265 client->GetTemplateURLService();
266 // Template URL service can be null in tests.
267 if (template_url_service != nullptr) {
Jenny Zhang5bc2e3d2018-09-10 18:50:55268 AutocompleteInput empty_input;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11269 GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl(
Jenny Zhang5bc2e3d2018-09-10 18:50:55270 /*current_url=*/"", /*empty input*/ empty_input, template_url_service);
mpearson3d89cdc2017-03-03 21:15:45271 // To check whether this is allowed, use an arbitrary insecure (http) URL
272 // as the URL we'd want suggestions for. The value of OTHER as the current
273 // page classification is to correspond with that URL.
274 UMA_HISTOGRAM_BOOLEAN(
275 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
276 suggest_url.is_valid() &&
277 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
278 template_url_service->GetDefaultSearchProvider(),
279 metrics::OmniboxEventProto::OTHER,
280 template_url_service->search_terms_data(), client));
281 }
[email protected]6ce7f612012-09-05 23:53:07282}
283
284ZeroSuggestProvider::~ZeroSuggestProvider() {
285}
286
[email protected]776ee5902014-08-11 09:15:19287const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
288 // Zero suggest provider should not receive keyword results.
289 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13290 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19291}
292
293const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18294 // The callers of this method won't look at the AutocompleteInput's
295 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02296 AutocompleteInput input(base::string16(), current_page_classification_,
297 client()->GetSchemeClassifier());
298 input.set_current_url(GURL(current_query_));
299 input.set_current_title(current_title_);
300 input.set_prevent_inline_autocomplete(true);
301 input.set_allow_exact_keyword_match(false);
302 return input;
[email protected]776ee5902014-08-11 09:15:19303}
304
305bool ZeroSuggestProvider::ShouldAppendExtraParams(
306 const SearchSuggestionParser::SuggestResult& result) const {
307 // We always use the default provider for search, so append the params.
308 return true;
309}
310
[email protected]776ee5902014-08-11 09:15:19311void ZeroSuggestProvider::RecordDeletionResult(bool success) {
312 if (success) {
313 base::RecordAction(
314 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
315 } else {
316 base::RecordAction(
317 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
318 }
319}
320
Maks Orlovich1b208512018-06-13 21:08:17321void ZeroSuggestProvider::OnURLLoadComplete(
322 const network::SimpleURLLoader* source,
323 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19324 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17325 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19326
327 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
328
Gheorghe Comanici9a364f572018-01-24 17:22:00329 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17330 response_body && source->NetError() == net::OK &&
331 (source->ResponseInfo() && source->ResponseInfo()->headers &&
332 source->ResponseInfo()->headers->response_code() == 200) &&
333 UpdateResults(SearchSuggestionParser::ExtractJsonData(
334 source, std::move(response_body)));
335 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19336 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05337 result_type_running_ = NONE;
338 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19339 listener_->OnProviderUpdate(results_updated);
340}
341
Gheorghe Comanici9a364f572018-01-24 17:22:00342bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
343 std::unique_ptr<base::Value> data(
344 SearchSuggestionParser::DeserializeJsonData(json_data));
345 if (!data)
[email protected]855ebff2014-05-09 07:14:38346 return false;
347
Gheorghe Comanici9a364f572018-01-24 17:22:00348 // When running the personalized service, we want to store suggestion
349 // responses if non-empty.
Kevin Bailey5fea4322018-03-21 22:36:05350 if (result_type_running_ == DEFAULT_SERP && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00351 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
352 json_data);
[email protected]855ebff2014-05-09 07:14:38353
Gheorghe Comanici9a364f572018-01-24 17:22:00354 // If we received an empty result list, we should update the display, as it
355 // may be showing cached results that should not be shown.
356 const base::ListValue* root_list = nullptr;
357 const base::ListValue* results_list = nullptr;
358 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
359 root_list->GetList(1, &results_list) &&
360 !results_list->empty();
361 const bool non_empty_cache = !results_.suggest_results.empty() ||
362 !results_.navigation_results.empty();
363 if (non_empty_parsed_list && non_empty_cache)
364 return false;
365 }
366 const bool results_updated = ParseSuggestResults(
367 *data, kDefaultZeroSuggestRelevance, false, &results_);
368 ConvertResultsToAutocompleteMatches();
369 return results_updated;
[email protected]855ebff2014-05-09 07:14:38370}
371
[email protected]bb1fb2b2013-05-31 00:21:01372void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37373 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30374 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22375 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11376 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01377}
378
[email protected]bb1fb2b2013-05-31 00:21:01379AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37380 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01381 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47382 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01383 match.destination_url = navigation.url();
384
[email protected]23db6492014-01-16 02:35:30385 // Zero suggest results should always omit protocols and never appear bold.
Tommy C. Li21da43522018-11-20 16:35:28386 auto format_types = AutocompleteMatch::GetFormatTypes(false, false);
tommycli72014f62017-06-29 21:42:16387 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
388 net::UnescapeRule::SPACES, nullptr,
389 nullptr, nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01390 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13391 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26392 navigation.url(), url_formatter::FormatUrl(navigation.url()),
Kevin Bailey83e643d2018-03-08 16:01:41393 client()->GetSchemeClassifier(), nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01394
[email protected]b959d7d42013-12-13 17:26:37395 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]bb1fb2b2013-05-31 00:21:01396 match.contents.length(), ACMatchClassification::URL,
397 &match.contents_class);
[email protected]9c97f89c2013-06-25 03:12:16398
399 match.description =
400 AutocompleteMatch::SanitizeString(navigation.description());
[email protected]b959d7d42013-12-13 17:26:37401 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
[email protected]9c97f89c2013-06-25 03:12:16402 match.description.length(), ACMatchClassification::NONE,
403 &match.description_class);
gcomanici67d53ac2017-04-01 17:07:19404 match.subtype_identifier = navigation.subtype_identifier();
[email protected]bb1fb2b2013-05-31 00:21:01405 return match;
406}
407
[email protected]8f064e52013-09-18 01:17:14408void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
Kevin Bailey5fea4322018-03-21 22:36:05409 size_t orig_request_num,
[email protected]8f064e52013-09-18 01:17:14410 const history::MostVisitedURLList& urls) {
Kevin Bailey5fea4322018-03-21 22:36:05411 if (result_type_running_ != MOST_VISITED ||
412 orig_request_num != most_visited_request_num_) {
Gheorghe Comanici9a364f572018-01-24 17:22:00413 return;
Kevin Bailey5fea4322018-03-21 22:36:05414 }
[email protected]8f064e52013-09-18 01:17:14415 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22416 done_ = true;
417 ConvertResultsToAutocompleteMatches();
Kevin Bailey5fea4322018-03-21 22:36:05418 result_type_running_ = NONE;
419 ++most_visited_request_num_;
mariakhomenkobfc3a2a2014-10-24 00:48:22420 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14421}
422
Maks Orlovich1b208512018-06-13 21:08:17423void ZeroSuggestProvider::OnContextualSuggestionsLoaderAvailable(
424 std::unique_ptr<network::SimpleURLLoader> loader) {
425 // ContextualSuggestionsService has already started |loader|, so here it's
426 // only neccessary to grab its ownership until results come in to
427 // OnURLLoadComplete().
428 loader_ = std::move(loader);
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11429 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
430}
431
[email protected]9c97f89c2013-06-25 03:12:16432void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01433 matches_.clear();
434
blundelld130d592015-06-21 19:29:13435 TemplateURLService* template_url_service = client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49436 DCHECK(template_url_service);
[email protected]bb1fb2b2013-05-31 00:21:01437 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13438 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07439 // Fail if we can't set the clickthrough URL for query suggestions.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24440 if (default_provider == nullptr ||
blundelld130d592015-06-21 19:29:13441 !default_provider->SupportsReplacement(
442 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07443 return;
[email protected]6ce7f612012-09-05 23:53:07444
[email protected]00404742014-02-20 13:09:05445 MatchMap map;
446 AddSuggestResultsToMap(results_.suggest_results, &map);
447
448 const int num_query_results = map.size();
449 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01450 const int num_results = num_query_results + num_nav_results;
Steven Holte95922222018-09-14 20:06:23451 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.QueryResults", num_query_results);
452 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.URLResults", num_nav_results);
453 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01454
[email protected]8f064e52013-09-18 01:17:14455 // Show Most Visited results after ZeroSuggest response is received.
Kevin Bailey5fea4322018-03-21 22:36:05456 if (result_type_running_ == MOST_VISITED) {
[email protected]3feb8b002013-10-14 23:50:13457 if (!current_url_match_.destination_url.is_valid())
458 return;
[email protected]8f064e52013-09-18 01:17:14459 matches_.push_back(current_url_match_);
460 int relevance = 600;
461 if (num_results > 0) {
Steven Holte95922222018-09-14 20:06:23462 UMA_HISTOGRAM_COUNTS_1M(
[email protected]8f064e52013-09-18 01:17:14463 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
464 most_visited_urls_.size());
465 }
[email protected]23db6492014-01-16 02:35:30466 const base::string16 current_query_string16(
467 base::ASCIIToUTF16(current_query_));
[email protected]8f064e52013-09-18 01:17:14468 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
469 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37470 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13471 client()->GetSchemeClassifier(), url.url,
gcomanici67d53ac2017-04-01 17:07:19472 AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false,
jshin1fb76462016-04-05 22:13:03473 relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14474 matches_.push_back(NavigationToMatch(nav));
475 --relevance;
476 }
477 return;
478 }
479
[email protected]9c97f89c2013-06-25 03:12:16480 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01481 return;
482
Jenny Zhang5bc2e3d2018-09-10 18:50:55483 // Normally |current_url_match_.destination_url| should be valid unless it is
484 // under particular page context.
485 DCHECK(current_page_classification_ ==
486 metrics::OmniboxEventProto::CHROMEOS_APP_LIST ||
487 current_url_match_.destination_url.is_valid());
488 if (current_url_match_.destination_url.is_valid())
489 matches_.push_back(current_url_match_);
[email protected]00404742014-02-20 13:09:05490 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01491 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01492
[email protected]0b9575f2014-07-30 11:58:37493 const SearchSuggestionParser::NavigationResults& nav_results(
494 results_.navigation_results);
jdoerrie2e6a651d2018-10-04 17:09:08495 for (auto it = nav_results.begin(); it != nav_results.end(); ++it) {
[email protected]bb1fb2b2013-05-31 00:21:01496 matches_.push_back(NavigationToMatch(*it));
gcomanici67d53ac2017-04-01 17:07:19497 }
[email protected]6ce7f612012-09-05 23:53:07498}
499
[email protected]bb1fb2b2013-05-31 00:21:01500AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]bb1fb2b2013-05-31 00:21:01501 // The placeholder suggestion for the current URL has high relevance so
502 // that it is in the first suggestion slot and inline autocompleted. It
503 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11504 AutocompleteInput tmp(GetInput(false));
505 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54506 const base::string16 description =
507 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
508 ? current_title_
509 : base::string16();
510 return VerbatimMatchForURL(client(), tmp, GURL(current_query_), description,
mpearson931028c2016-07-01 18:55:11511 history_url_provider_,
sdefresne70948d62015-08-11 10:46:35512 results_.verbatim_relevance);
[email protected]00404742014-02-20 13:09:05513}
[email protected]162c8d9fa2014-03-18 20:25:41514
Gheorghe Comanici9a364f572018-01-24 17:22:00515bool ZeroSuggestProvider::AllowZeroSuggestSuggestions(
[email protected]162c8d9fa2014-03-18 20:25:41516 const GURL& current_page_url) const {
Peter Kastingfb1a8ea2017-11-28 02:26:50517 // Don't show zero suggest on the NTP.
518 // TODO(hfung): Experiment with showing MostVisited zero suggest on NTP
519 // under the conditions described in crbug.com/305366.
520 if (IsNTPPage(current_page_classification_))
521 return false;
522
523 // Don't run if in incognito mode.
524 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41525 return false;
526
[email protected]5b899dc2018-10-02 09:20:01527 if (base::FeatureList::IsEnabled(
528 omnibox::kOmniboxPopupShortcutIconsInZeroState)) {
529 return false;
530 }
531
Jenny Zhang5bc2e3d2018-09-10 18:50:55532 // Only show zero suggest for pages with URLs the user will recognize
533 // if it is not running in ChromeOS app_list context.
Mark Pearson3fb0e3162018-08-27 21:53:57534 // This list intentionally does not include items such as ftp: and file:
535 // because (a) these do not work on Android and iOS, where non-contextual
536 // zero suggest is launched and (b) on desktop, where contextual zero suggest
537 // is running, these types of schemes aren't eligible to be sent to the
538 // server to ask for suggestions (and thus in practice we won't display zero
539 // suggest for them).
Jenny Zhang5bc2e3d2018-09-10 18:50:55540 if (current_page_classification_ !=
541 metrics::OmniboxEventProto::CHROMEOS_APP_LIST &&
542 (!current_page_url.is_valid() ||
543 ((current_page_url.scheme() != url::kHttpScheme) &&
544 (current_page_url.scheme() != url::kHttpsScheme) &&
545 (current_page_url.scheme() != url::kAboutScheme) &&
546 (current_page_url.scheme() !=
547 client()->GetEmbedderRepresentationOfAboutScheme()))))
[email protected]162c8d9fa2014-03-18 20:25:41548 return false;
549
[email protected]162c8d9fa2014-03-18 20:25:41550 return true;
551}
[email protected]855ebff2014-05-09 07:14:38552
553void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Kevin Bailey5fea4322018-03-21 22:36:05554 if (result_type_running_ != DEFAULT_SERP)
[email protected]855ebff2014-05-09 07:14:38555 return;
556
blundelld130d592015-06-21 19:29:13557 std::string json_data =
558 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38559 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57560 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14561 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00562 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
563 &results_))
[email protected]855ebff2014-05-09 07:14:38564 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38565 }
566}
Gheorghe Comanici9a364f572018-01-24 17:22:00567
568ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
569 const GURL& current_url,
570 const GURL& suggest_url) {
Gheorghe Comanici9a364f572018-01-24 17:22:00571 // Check if the URL can be sent in any suggest request.
572 const TemplateURLService* template_url_service =
573 client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49574 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00575 const TemplateURL* default_provider =
576 template_url_service->GetDefaultSearchProvider();
577 const bool can_send_current_url = CanSendURL(
578 current_url, suggest_url, default_provider, current_page_classification_,
579 template_url_service->search_terms_data(), client());
580
581 // Collect metrics on eligibility.
582 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
583 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
584 if (!can_send_current_url) {
585 const bool can_send_ordinary_url =
586 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
587 current_page_classification_,
588 template_url_service->search_terms_data(), client());
589 eligibility = can_send_ordinary_url
590 ? ZeroSuggestEligibility::URL_INELIGIBLE
591 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
592 }
593 UMA_HISTOGRAM_ENUMERATION(
594 "Omnibox.ZeroSuggest.Eligible.OnFocus", static_cast<int>(eligibility),
595 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
596
597 // Check if zero suggestions are allowed in the current context.
598 if (!AllowZeroSuggestSuggestions(current_url))
Kevin Bailey5fea4322018-03-21 22:36:05599 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00600
Jenny Zhang5bc2e3d2018-09-10 18:50:55601 if (current_page_classification_ ==
602 metrics::OmniboxEventProto::CHROMEOS_APP_LIST) {
603 return DEFAULT_SERP;
604 }
605
Theresa3db4ae22018-03-05 18:47:40606 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
Gheorghe Comanici9a364f572018-01-24 17:22:00607 return PersonalizedServiceShouldFallBackToMostVisited(
608 client()->GetPrefs(), client()->IsAuthenticated(),
609 template_url_service)
Kevin Bailey5fea4322018-03-21 22:36:05610 ? MOST_VISITED
611 : DEFAULT_SERP;
Gheorghe Comanici9a364f572018-01-24 17:22:00612
Theresa3db4ae22018-03-05 18:47:40613 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() &&
Gheorghe Comanici9a364f572018-01-24 17:22:00614 client()
615 ->GetTemplateURLService()
616 ->IsSearchResultsPageFromDefaultSearchProvider(current_url))
Kevin Bailey5fea4322018-03-21 22:36:05617 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00618
Theresa3db4ae22018-03-05 18:47:40619 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial())
Kevin Bailey5fea4322018-03-21 22:36:05620 return MOST_VISITED;
Gheorghe Comanici9a364f572018-01-24 17:22:00621
Kevin Bailey5fea4322018-03-21 22:36:05622 return can_send_current_url ? DEFAULT_SERP_FOR_URL : NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00623}