blob: 22c5a8dcb13dcd2818e7aa4c7f65cb5d0058e6be [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"
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"
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
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(
Gheorghe Comanici864725b2017-11-28 21:48:01100 PrefService* prefs,
Gheorghe Comanici682504102017-11-30 17:47:25101 bool is_authenticated,
Gheorghe Comanici864725b2017-11-28 21:48:01102 const TemplateURLService* template_url_service) {
Gheorghe Comanici9a364f572018-01-24 17:22:00103 if (!is_authenticated)
Gheorghe Comanici864725b2017-11-28 21:48:01104 return true;
105
Gheorghe Comanici9a364f572018-01-24 17:22:00106 if (template_url_service == nullptr)
107 return false;
Gheorghe Comanici682504102017-11-30 17:47:25108
Gheorghe Comanici9a364f572018-01-24 17:22:00109 const TemplateURL* default_provider =
110 template_url_service->GetDefaultSearchProvider();
111 return default_provider == nullptr ||
112 default_provider->GetEngineType(
113 template_url_service->search_terms_data()) != SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01114}
115
[email protected]6ce7f612012-09-05 23:53:07116} // namespace
117
[email protected]a00008d42012-09-15 05:07:58118// static
119ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18120 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11121 HistoryURLProvider* history_url_provider,
blundelld130d592015-06-21 19:29:13122 AutocompleteProviderListener* listener) {
mpearson931028c2016-07-01 18:55:11123 return new ZeroSuggestProvider(client, history_url_provider, listener);
[email protected]6ce7f612012-09-05 23:53:07124}
125
[email protected]855ebff2014-05-09 07:14:38126// static
127void ZeroSuggestProvider::RegisterProfilePrefs(
128 user_prefs::PrefRegistrySyncable* registry) {
blundelld130d592015-06-21 19:29:13129 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
130 std::string());
[email protected]855ebff2014-05-09 07:14:38131}
132
[email protected]6ce7f612012-09-05 23:53:07133void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18134 bool minimal_changes) {
a-v-ydd768d52016-03-25 21:07:46135 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54136 matches_.clear();
Kevin Bailey8642e612018-04-23 20:27:54137 Stop(true, false);
Jenny Zhang5bc2e3d2018-09-10 18:50:55138
Tomasz Wiszkowski57492ab2019-05-29 21:28:27139 if (!AllowZeroSuggestSuggestions(input)) {
140 UMA_HISTOGRAM_ENUMERATION(kOmniboxZeroSuggestEligibleHistogramName,
141 ZeroSuggestEligibility::GENERALLY_INELIGIBLE,
142 ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE);
[email protected]f030c4d2014-03-25 01:05:54143 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27144 }
[email protected]bb1fb2b2013-05-31 00:21:01145
Kevin Bailey5fea4322018-03-21 22:36:05146 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13147 set_field_trial_triggered(false);
148 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54149 permanent_text_ = input.text();
150 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54151 current_title_ = input.current_title();
[email protected]f030c4d2014-03-25 01:05:54152 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52153 current_url_match_ = MatchForCurrentURL();
154
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11155 GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl(
Jenny Zhang5bc2e3d2018-09-10 18:50:55156 /*current_url=*/"", input, client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41157 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07158 return;
[email protected]162c8d9fa2014-03-18 20:25:41159
Gheorghe Comanici9a364f572018-01-24 17:22:00160 result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url);
Kevin Bailey5fea4322018-03-21 22:36:05161 if (result_type_running_ == NONE)
[email protected]162c8d9fa2014-03-18 20:25:41162 return;
[email protected]162c8d9fa2014-03-18 20:25:41163
[email protected]6ce7f612012-09-05 23:53:07164 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11165
[email protected]855ebff2014-05-09 07:14:38166 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11167
Kevin Bailey5fea4322018-03-21 22:36:05168 if (result_type_running_ == MOST_VISITED) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11169 most_visited_urls_.clear();
170 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
Gheorghe Comanici9a364f572018-01-24 17:22:00171 if (!ts) {
172 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05173 result_type_running_ = NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00174 return;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11175 }
Gheorghe Comanici9a364f572018-01-24 17:22:00176
Kevin Bailey40bb0ac52019-04-02 00:43:32177 ts->GetMostVisitedURLs(base::BindRepeating(
178 &ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
179 weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11180 return;
181 }
182
Darren Shenf19140552019-06-03 01:46:21183 const std::string current_url = result_type_running_ == DEFAULT_SERP_FOR_URL
184 ? current_query_
185 : std::string();
Maks Orlovich1b208512018-06-13 21:08:17186 // Create a request for suggestions, routing completion to
187 // OnContextualSuggestionsLoaderAvailable.
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11188 client()
Gheorghe Comanici86bbdf62017-08-28 17:20:33189 ->GetContextualSuggestionsService(/*create_if_necessary=*/true)
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11190 ->CreateContextualSuggestionsRequest(
Jenny Zhang5bc2e3d2018-09-10 18:50:55191 current_url, client()->GetCurrentVisitTimestamp(), input,
Gheorghe Comanici034cff62018-01-27 03:34:00192 client()->GetTemplateURLService(),
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11193 base::BindOnce(
Maks Orlovich1b208512018-06-13 21:08:17194 &ZeroSuggestProvider::OnContextualSuggestionsLoaderAvailable,
195 weak_ptr_factory_.GetWeakPtr()),
196 base::BindOnce(
197 &ZeroSuggestProvider::OnURLLoadComplete,
198 base::Unretained(this) /* this owns SimpleURLLoader */));
[email protected]6ce7f612012-09-05 23:53:07199}
200
mpearson8a37c382015-03-07 05:58:57201void ZeroSuggestProvider::Stop(bool clear_cached_results,
202 bool due_to_user_inactivity) {
Maks Orlovich1b208512018-06-13 21:08:17203 if (loader_)
[email protected]ec3f679b2014-08-18 07:45:13204 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
Maks Orlovich1b208512018-06-13 21:08:17205 loader_.reset();
Gheorghe Comanici86bbdf62017-08-28 17:20:33206 auto* contextual_suggestions_service =
207 client()->GetContextualSuggestionsService(/*create_if_necessary=*/false);
208 // contextual_suggestions_service can be null if in incognito mode.
209 if (contextual_suggestions_service != nullptr) {
210 contextual_suggestions_service->StopCreatingContextualSuggestionsRequest();
211 }
Kevin Bailey5fea4322018-03-21 22:36:05212 // TODO(krb): It would allow us to remove some guards if we could also cancel
213 // the TopSites::GetMostVisitedURLs request.
[email protected]ec3f679b2014-08-18 07:45:13214 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05215 result_type_running_ = NONE;
216 ++most_visited_request_num_;
[email protected]ec3f679b2014-08-18 07:45:13217
218 if (clear_cached_results) {
219 // We do not call Clear() on |results_| to retain |verbatim_relevance|
220 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18221 // beginning of the next call to Start() to determine the current url
222 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13223 results_.suggest_results.clear();
224 results_.navigation_results.clear();
225 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54226 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45227 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13228 }
229}
230
[email protected]855ebff2014-05-09 07:14:38231void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Tommy C. Li393c1b142019-05-22 00:28:41232 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
233 current_page_classification_)) {
[email protected]855ebff2014-05-09 07:14:38234 // Remove the deleted match from the cache, so it is not shown to the user
235 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13236 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38237 std::string());
238 }
239 BaseSearchProvider::DeleteMatch(match);
240}
241
[email protected]ec3f679b2014-08-18 07:45:13242void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
243 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45244 if (!results_.suggest_results.empty() ||
245 !results_.navigation_results.empty() ||
246 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13247 provider_info->back().set_times_returned_results_in_session(1);
248}
249
[email protected]f030c4d2014-03-25 01:05:54250void ZeroSuggestProvider::ResetSession() {
251 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13252 // |field_trial_triggered_in_session| unchanged and set
253 // |field_trial_triggered| to false since zero suggest is inactive now.
254 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54255}
256
mpearson931028c2016-07-01 18:55:11257ZeroSuggestProvider::ZeroSuggestProvider(
258 AutocompleteProviderClient* client,
259 HistoryURLProvider* history_url_provider,
260 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13261 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
mpearson931028c2016-07-01 18:55:11262 history_url_provider_(history_url_provider),
[email protected]776ee5902014-08-11 09:15:19263 listener_(listener),
Kevin Bailey5fea4322018-03-21 22:36:05264 result_type_running_(NONE),
[email protected]8f064e52013-09-18 01:17:14265 weak_ptr_factory_(this) {
mpearson3d89cdc2017-03-03 21:15:45266 // Record whether contextual zero suggest is possible for this user / profile.
267 const TemplateURLService* template_url_service =
268 client->GetTemplateURLService();
269 // Template URL service can be null in tests.
270 if (template_url_service != nullptr) {
Jenny Zhang5bc2e3d2018-09-10 18:50:55271 AutocompleteInput empty_input;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11272 GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl(
Jenny Zhang5bc2e3d2018-09-10 18:50:55273 /*current_url=*/"", /*empty input*/ empty_input, template_url_service);
mpearson3d89cdc2017-03-03 21:15:45274 // To check whether this is allowed, use an arbitrary insecure (http) URL
275 // as the URL we'd want suggestions for. The value of OTHER as the current
276 // page classification is to correspond with that URL.
277 UMA_HISTOGRAM_BOOLEAN(
278 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
279 suggest_url.is_valid() &&
280 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
281 template_url_service->GetDefaultSearchProvider(),
282 metrics::OmniboxEventProto::OTHER,
283 template_url_service->search_terms_data(), client));
284 }
[email protected]6ce7f612012-09-05 23:53:07285}
286
287ZeroSuggestProvider::~ZeroSuggestProvider() {
288}
289
[email protected]776ee5902014-08-11 09:15:19290const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
291 // Zero suggest provider should not receive keyword results.
292 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13293 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19294}
295
296const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18297 // The callers of this method won't look at the AutocompleteInput's
298 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02299 AutocompleteInput input(base::string16(), current_page_classification_,
300 client()->GetSchemeClassifier());
301 input.set_current_url(GURL(current_query_));
302 input.set_current_title(current_title_);
303 input.set_prevent_inline_autocomplete(true);
304 input.set_allow_exact_keyword_match(false);
305 return input;
[email protected]776ee5902014-08-11 09:15:19306}
307
308bool ZeroSuggestProvider::ShouldAppendExtraParams(
309 const SearchSuggestionParser::SuggestResult& result) const {
310 // We always use the default provider for search, so append the params.
311 return true;
312}
313
[email protected]776ee5902014-08-11 09:15:19314void ZeroSuggestProvider::RecordDeletionResult(bool success) {
315 if (success) {
316 base::RecordAction(
317 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
318 } else {
319 base::RecordAction(
320 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
321 }
322}
323
Maks Orlovich1b208512018-06-13 21:08:17324void ZeroSuggestProvider::OnURLLoadComplete(
325 const network::SimpleURLLoader* source,
326 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19327 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17328 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19329
330 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
331
Gheorghe Comanici9a364f572018-01-24 17:22:00332 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17333 response_body && source->NetError() == net::OK &&
334 (source->ResponseInfo() && source->ResponseInfo()->headers &&
335 source->ResponseInfo()->headers->response_code() == 200) &&
336 UpdateResults(SearchSuggestionParser::ExtractJsonData(
337 source, std::move(response_body)));
338 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19339 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05340 result_type_running_ = NONE;
341 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19342 listener_->OnProviderUpdate(results_updated);
343}
344
Gheorghe Comanici9a364f572018-01-24 17:22:00345bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
346 std::unique_ptr<base::Value> data(
347 SearchSuggestionParser::DeserializeJsonData(json_data));
348 if (!data)
[email protected]855ebff2014-05-09 07:14:38349 return false;
350
Gheorghe Comanici9a364f572018-01-24 17:22:00351 // When running the personalized service, we want to store suggestion
352 // responses if non-empty.
Darren Shenf19140552019-06-03 01:46:21353 if (result_type_running_ == DEFAULT_SERP && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00354 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
355 json_data);
[email protected]855ebff2014-05-09 07:14:38356
Gheorghe Comanici9a364f572018-01-24 17:22:00357 // If we received an empty result list, we should update the display, as it
358 // may be showing cached results that should not be shown.
359 const base::ListValue* root_list = nullptr;
360 const base::ListValue* results_list = nullptr;
361 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
362 root_list->GetList(1, &results_list) &&
363 !results_list->empty();
364 const bool non_empty_cache = !results_.suggest_results.empty() ||
365 !results_.navigation_results.empty();
366 if (non_empty_parsed_list && non_empty_cache)
367 return false;
368 }
369 const bool results_updated = ParseSuggestResults(
370 *data, kDefaultZeroSuggestRelevance, false, &results_);
371 ConvertResultsToAutocompleteMatches();
372 return results_updated;
[email protected]855ebff2014-05-09 07:14:38373}
374
[email protected]bb1fb2b2013-05-31 00:21:01375void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37376 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30377 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22378 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11379 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01380}
381
[email protected]bb1fb2b2013-05-31 00:21:01382AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37383 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01384 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47385 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01386 match.destination_url = navigation.url();
387
[email protected]bb1fb2b2013-05-31 00:21:01388 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13389 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26390 navigation.url(), url_formatter::FormatUrl(navigation.url()),
Kevin Bailey83e643d2018-03-08 16:01:41391 client()->GetSchemeClassifier(), nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01392
manuk3972b2f2019-04-19 14:22:56393 // Zero suggest results should always omit protocols and never appear bold.
394 auto format_types = AutocompleteMatch::GetFormatTypes(false, false);
395 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
396 net::UnescapeRule::SPACES, nullptr,
397 nullptr, nullptr);
398 match.contents_class = ClassifyTermMatches({}, match.contents.length(), 0,
399 ACMatchClassification::URL);
[email protected]9c97f89c2013-06-25 03:12:16400
401 match.description =
402 AutocompleteMatch::SanitizeString(navigation.description());
manuk3972b2f2019-04-19 14:22:56403 match.description_class = ClassifyTermMatches({}, match.description.length(),
404 0, ACMatchClassification::NONE);
405
gcomanici67d53ac2017-04-01 17:07:19406 match.subtype_identifier = navigation.subtype_identifier();
[email protected]bb1fb2b2013-05-31 00:21:01407 return match;
408}
409
[email protected]8f064e52013-09-18 01:17:14410void ZeroSuggestProvider::OnMostVisitedUrlsAvailable(
Kevin Bailey5fea4322018-03-21 22:36:05411 size_t orig_request_num,
[email protected]8f064e52013-09-18 01:17:14412 const history::MostVisitedURLList& urls) {
Kevin Bailey5fea4322018-03-21 22:36:05413 if (result_type_running_ != MOST_VISITED ||
414 orig_request_num != most_visited_request_num_) {
Gheorghe Comanici9a364f572018-01-24 17:22:00415 return;
Kevin Bailey5fea4322018-03-21 22:36:05416 }
[email protected]8f064e52013-09-18 01:17:14417 most_visited_urls_ = urls;
mariakhomenkobfc3a2a2014-10-24 00:48:22418 done_ = true;
419 ConvertResultsToAutocompleteMatches();
Kevin Bailey5fea4322018-03-21 22:36:05420 result_type_running_ = NONE;
421 ++most_visited_request_num_;
mariakhomenkobfc3a2a2014-10-24 00:48:22422 listener_->OnProviderUpdate(true);
[email protected]8f064e52013-09-18 01:17:14423}
424
Maks Orlovich1b208512018-06-13 21:08:17425void ZeroSuggestProvider::OnContextualSuggestionsLoaderAvailable(
426 std::unique_ptr<network::SimpleURLLoader> loader) {
427 // ContextualSuggestionsService has already started |loader|, so here it's
428 // only neccessary to grab its ownership until results come in to
429 // OnURLLoadComplete().
430 loader_ = std::move(loader);
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11431 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT);
432}
433
[email protected]9c97f89c2013-06-25 03:12:16434void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() {
[email protected]bb1fb2b2013-05-31 00:21:01435 matches_.clear();
436
blundelld130d592015-06-21 19:29:13437 TemplateURLService* template_url_service = client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49438 DCHECK(template_url_service);
[email protected]bb1fb2b2013-05-31 00:21:01439 const TemplateURL* default_provider =
blundelld130d592015-06-21 19:29:13440 template_url_service->GetDefaultSearchProvider();
[email protected]6ce7f612012-09-05 23:53:07441 // Fail if we can't set the clickthrough URL for query suggestions.
Ivan Kotenkov75b1c3a2017-10-24 14:47:24442 if (default_provider == nullptr ||
blundelld130d592015-06-21 19:29:13443 !default_provider->SupportsReplacement(
444 template_url_service->search_terms_data()))
[email protected]6ce7f612012-09-05 23:53:07445 return;
[email protected]6ce7f612012-09-05 23:53:07446
[email protected]00404742014-02-20 13:09:05447 MatchMap map;
448 AddSuggestResultsToMap(results_.suggest_results, &map);
449
450 const int num_query_results = map.size();
451 const int num_nav_results = results_.navigation_results.size();
[email protected]bb1fb2b2013-05-31 00:21:01452 const int num_results = num_query_results + num_nav_results;
Steven Holte95922222018-09-14 20:06:23453 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.QueryResults", num_query_results);
454 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.URLResults", num_nav_results);
455 UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.AllResults", num_results);
[email protected]bb1fb2b2013-05-31 00:21:01456
[email protected]8f064e52013-09-18 01:17:14457 // Show Most Visited results after ZeroSuggest response is received.
Kevin Bailey5fea4322018-03-21 22:36:05458 if (result_type_running_ == MOST_VISITED) {
Tomasz Wiszkowski57492ab2019-05-29 21:28:27459 // Ensure we don't show most visited URL suggestions on NTP.
460 // This allows us to prevent undesired side outcome of presenting
461 // URL suggestions to users who are not in the personalized field trial for
462 // zero query suggestions.
463 if (IsNTPPage(current_page_classification_) ||
464 !current_url_match_.destination_url.is_valid()) {
[email protected]3feb8b002013-10-14 23:50:13465 return;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27466 }
[email protected]8f064e52013-09-18 01:17:14467 matches_.push_back(current_url_match_);
468 int relevance = 600;
469 if (num_results > 0) {
Steven Holte95922222018-09-14 20:06:23470 UMA_HISTOGRAM_COUNTS_1M(
[email protected]8f064e52013-09-18 01:17:14471 "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual",
472 most_visited_urls_.size());
473 }
[email protected]23db6492014-01-16 02:35:30474 const base::string16 current_query_string16(
475 base::ASCIIToUTF16(current_query_));
[email protected]8f064e52013-09-18 01:17:14476 for (size_t i = 0; i < most_visited_urls_.size(); i++) {
477 const history::MostVisitedURL& url = most_visited_urls_[i];
[email protected]0b9575f2014-07-30 11:58:37478 SearchSuggestionParser::NavigationResult nav(
blundelld130d592015-06-21 19:29:13479 client()->GetSchemeClassifier(), url.url,
gcomanici67d53ac2017-04-01 17:07:19480 AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false,
jshin1fb76462016-04-05 22:13:03481 relevance, true, current_query_string16);
[email protected]8f064e52013-09-18 01:17:14482 matches_.push_back(NavigationToMatch(nav));
483 --relevance;
484 }
485 return;
486 }
487
[email protected]9c97f89c2013-06-25 03:12:16488 if (num_results == 0)
[email protected]bb1fb2b2013-05-31 00:21:01489 return;
490
Tomasz Wiszkowski57492ab2019-05-29 21:28:27491 // Do not add the default URL match if we're on the NTP to prevent
492 // chrome-native://newtab or chrome://newtab from showing up on the list of
493 // suggestions.
494 if (!IsNTPPage(current_page_classification_) &&
495 current_url_match_.destination_url.is_valid()) {
Jenny Zhang5bc2e3d2018-09-10 18:50:55496 matches_.push_back(current_url_match_);
Tomasz Wiszkowski57492ab2019-05-29 21:28:27497 }
498
[email protected]00404742014-02-20 13:09:05499 for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it)
[email protected]bb1fb2b2013-05-31 00:21:01500 matches_.push_back(it->second);
[email protected]bb1fb2b2013-05-31 00:21:01501
[email protected]0b9575f2014-07-30 11:58:37502 const SearchSuggestionParser::NavigationResults& nav_results(
503 results_.navigation_results);
jdoerrie2e6a651d2018-10-04 17:09:08504 for (auto it = nav_results.begin(); it != nav_results.end(); ++it) {
[email protected]bb1fb2b2013-05-31 00:21:01505 matches_.push_back(NavigationToMatch(*it));
gcomanici67d53ac2017-04-01 17:07:19506 }
[email protected]6ce7f612012-09-05 23:53:07507}
508
[email protected]bb1fb2b2013-05-31 00:21:01509AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() {
[email protected]bb1fb2b2013-05-31 00:21:01510 // The placeholder suggestion for the current URL has high relevance so
511 // that it is in the first suggestion slot and inline autocompleted. It
512 // gets dropped as soon as the user types something.
mpearson931028c2016-07-01 18:55:11513 AutocompleteInput tmp(GetInput(false));
514 tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts());
gcomanici8cabc77f2017-04-27 20:04:54515 const base::string16 description =
516 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl))
517 ? current_title_
518 : base::string16();
519 return VerbatimMatchForURL(client(), tmp, GURL(current_query_), description,
mpearson931028c2016-07-01 18:55:11520 history_url_provider_,
sdefresne70948d62015-08-11 10:46:35521 results_.verbatim_relevance);
[email protected]00404742014-02-20 13:09:05522}
[email protected]162c8d9fa2014-03-18 20:25:41523
Gheorghe Comanici9a364f572018-01-24 17:22:00524bool ZeroSuggestProvider::AllowZeroSuggestSuggestions(
Tomasz Wiszkowski57492ab2019-05-29 21:28:27525 const AutocompleteInput& input) const {
526 const auto& page_url = input.current_url();
527 const auto page_class = input.current_page_classification();
528 const auto input_type = input.type();
Peter Kastingfb1a8ea2017-11-28 02:26:50529
Tomasz Wiszkowski57492ab2019-05-29 21:28:27530 if (!input.from_omnibox_focus())
531 return false;
532
Peter Kastingfb1a8ea2017-11-28 02:26:50533 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41534 return false;
535
Tomasz Wiszkowski57492ab2019-05-29 21:28:27536 // Check if ZeroSuggest is allowed in an empty state.
537 if (input_type == metrics::OmniboxInputType::INVALID &&
538 !(page_class == metrics::OmniboxEventProto::CHROMEOS_APP_LIST ||
539 (IsNTPPage(page_class) &&
540 base::FeatureList::IsEnabled(omnibox::kZeroSuggestionsOnNTP)))) {
[email protected]5b899dc2018-10-02 09:20:01541 return false;
542 }
543
Tomasz Wiszkowski57492ab2019-05-29 21:28:27544 // When omnibox contains pre-populated content, only show zero suggest for
545 // pages with URLs the user will recognize.
546 //
Mark Pearson3fb0e3162018-08-27 21:53:57547 // This list intentionally does not include items such as ftp: and file:
548 // because (a) these do not work on Android and iOS, where non-contextual
549 // zero suggest is launched and (b) on desktop, where contextual zero suggest
550 // is running, these types of schemes aren't eligible to be sent to the
551 // server to ask for suggestions (and thus in practice we won't display zero
552 // suggest for them).
Tomasz Wiszkowski57492ab2019-05-29 21:28:27553 if (input_type != metrics::OmniboxInputType::INVALID &&
554 !(page_url.is_valid() &&
555 ((page_url.scheme() == url::kHttpScheme) ||
556 (page_url.scheme() == url::kHttpsScheme) ||
557 (page_url.scheme() == url::kAboutScheme) ||
558 (page_url.scheme() ==
559 client()->GetEmbedderRepresentationOfAboutScheme())))) {
[email protected]162c8d9fa2014-03-18 20:25:41560 return false;
Tomasz Wiszkowski57492ab2019-05-29 21:28:27561 }
[email protected]162c8d9fa2014-03-18 20:25:41562
563 return true;
564}
[email protected]855ebff2014-05-09 07:14:38565
566void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Darren Shenf19140552019-06-03 01:46:21567 if (result_type_running_ != DEFAULT_SERP)
[email protected]855ebff2014-05-09 07:14:38568 return;
569
blundelld130d592015-06-21 19:29:13570 std::string json_data =
571 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38572 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57573 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14574 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00575 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
576 &results_))
[email protected]855ebff2014-05-09 07:14:38577 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38578 }
579}
Gheorghe Comanici9a364f572018-01-24 17:22:00580
581ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
582 const GURL& current_url,
583 const GURL& suggest_url) {
Gheorghe Comanici9a364f572018-01-24 17:22:00584 // Check if the URL can be sent in any suggest request.
585 const TemplateURLService* template_url_service =
586 client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49587 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00588 const TemplateURL* default_provider =
589 template_url_service->GetDefaultSearchProvider();
590 const bool can_send_current_url = CanSendURL(
591 current_url, suggest_url, default_provider, current_page_classification_,
592 template_url_service->search_terms_data(), client());
Darren Shenf19140552019-06-03 01:46:21593
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) {
Darren Shenf19140552019-06-03 01:46:21617 return DEFAULT_SERP;
Jenny Zhang5bc2e3d2018-09-10 18:50:55618 }
619
Tommy C. Li393c1b142019-05-22 00:28:41620 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial(
621 current_page_classification_)) {
Gheorghe Comanici9a364f572018-01-24 17:22:00622 return PersonalizedServiceShouldFallBackToMostVisited(
623 client()->GetPrefs(), client()->IsAuthenticated(),
624 template_url_service)
Kevin Bailey5fea4322018-03-21 22:36:05625 ? MOST_VISITED
Darren Shenf19140552019-06-03 01:46:21626 : DEFAULT_SERP;
Tommy C. Li393c1b142019-05-22 00:28:41627 }
Gheorghe Comanici9a364f572018-01-24 17:22:00628
Tommy C. Li393c1b142019-05-22 00:28:41629 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial(
630 current_page_classification_) &&
Gheorghe Comanici9a364f572018-01-24 17:22:00631 client()
632 ->GetTemplateURLService()
Tommy C. Li393c1b142019-05-22 00:28:41633 ->IsSearchResultsPageFromDefaultSearchProvider(current_url)) {
Kevin Bailey5fea4322018-03-21 22:36:05634 return NONE;
Tommy C. Li393c1b142019-05-22 00:28:41635 }
Gheorghe Comanici9a364f572018-01-24 17:22:00636
Tommy C. Li393c1b142019-05-22 00:28:41637 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial(
638 current_page_classification_)) {
Kevin Bailey5fea4322018-03-21 22:36:05639 return MOST_VISITED;
Tommy C. Li393c1b142019-05-22 00:28:41640 }
Gheorghe Comanici9a364f572018-01-24 17:22:00641
Tommy C. Lic79d39ab2019-06-04 16:58:56642 if (can_send_current_url &&
643 OmniboxFieldTrial::InZeroSuggestRemoteSendURLFieldTrial(
644 current_page_classification_)) {
645 return DEFAULT_SERP_FOR_URL;
646 }
647
648 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00649}