blob: 9fa7588844e05152df1675332f52cbe29a844329 [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"
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"
manuk3972b2f2019-04-19 14:22:5630#include "components/omnibox/browser/autocomplete_match_classification.h"
blundell2102f7c2015-07-09 10:00:5331#include "components/omnibox/browser/autocomplete_provider_listener.h"
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:1132#include "components/omnibox/browser/contextual_suggestions_service.h"
blundell2102f7c2015-07-09 10:00:5333#include "components/omnibox/browser/history_url_provider.h"
blundell2102f7c2015-07-09 10:00:5334#include "components/omnibox/browser/omnibox_pref_names.h"
35#include "components/omnibox/browser/search_provider.h"
sdefresne70948d62015-08-11 10:46:3536#include "components/omnibox/browser/verbatim_match.h"
Tomasz Wiszkowskid938a1112019-03-06 18:01:5737#include "components/omnibox/common/omnibox_features.h"
[email protected]f0c8c4992014-05-15 17:37:2638#include "components/pref_registry/pref_registry_syncable.h"
brettwf00b9b42016-02-01 22:11:3839#include "components/prefs/pref_service.h"
Gheorghe Comanici864725b2017-11-28 21:48:0140#include "components/search_engines/search_engine_type.h"
[email protected]bf5c532d2014-07-05 00:29:5341#include "components/search_engines/template_url_service.h"
rsleevi24f64dc22015-08-07 21:39:2142#include "components/url_formatter/url_formatter.h"
asvitkine9a279832015-12-18 02:35:5043#include "components/variations/net/variations_http_headers.h"
[email protected]bb1fb2b2013-05-31 00:21:0144#include "net/base/escape.h"
Maks Orlovich1b208512018-06-13 21:08:1745#include "services/network/public/cpp/resource_response.h"
46#include "services/network/public/cpp/shared_url_loader_factory.h"
47#include "services/network/public/cpp/simple_url_loader.h"
Steven Holtef9d5ed62017-10-21 02:02:3048#include "third_party/metrics_proto/omnibox_event.pb.h"
49#include "third_party/metrics_proto/omnibox_input_type.pb.h"
[email protected]761fa4702013-07-02 15:25:1550#include "url/gurl.h"
[email protected]6ce7f612012-09-05 23:53:0751
52namespace {
[email protected]bb1fb2b2013-05-31 00:21:0153
mpearson3d89cdc2017-03-03 21:15:4554// Represents whether ZeroSuggestProvider is allowed to display contextual
55// suggestions on focus, and if not, why not.
56// These values are written to logs. New enum values can be added, but existing
57// enums must never be renumbered or deleted and reused.
58enum class ZeroSuggestEligibility {
59 ELIGIBLE = 0,
60 // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL
61 // itself prevents ZeroSuggest from triggering.
62 URL_INELIGIBLE = 1,
63 GENERALLY_INELIGIBLE = 2,
64 ELIGIBLE_MAX_VALUE
65};
66
[email protected]bb1fb2b2013-05-31 00:21:0167// TODO(hfung): The histogram code was copied and modified from
68// search_provider.cc. Refactor and consolidate the code.
69// We keep track in a histogram how many suggest requests we send, how
70// many suggest requests we invalidate (e.g., due to a user typing
71// another character), and how many replies we receive.
mpearson3d89cdc2017-03-03 21:15:4572// These values are written to logs. New enum values can be added, but existing
73// enums must never be renumbered or deleted and reused.
[email protected]bb1fb2b2013-05-31 00:21:0174enum ZeroSuggestRequestsHistogramValue {
75 ZERO_SUGGEST_REQUEST_SENT = 1,
mpearson3d89cdc2017-03-03 21:15:4576 ZERO_SUGGEST_REQUEST_INVALIDATED = 2,
77 ZERO_SUGGEST_REPLY_RECEIVED = 3,
[email protected]bb1fb2b2013-05-31 00:21:0178 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE
79};
80
81void LogOmniboxZeroSuggestRequest(
82 ZeroSuggestRequestsHistogramValue request_value) {
83 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value,
84 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE);
85}
86
[email protected]bb1fb2b2013-05-31 00:21:0187// Relevance value to use if it was not set explicitly by the server.
88const int kDefaultZeroSuggestRelevance = 100;
89
mpearson3d89cdc2017-03-03 21:15:4590// Used for testing whether zero suggest is ever available.
mpearsonc90c24b2017-03-04 00:11:2691constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/";
mpearson3d89cdc2017-03-03 21:15:4592
Gheorghe Comanici9a364f572018-01-24 17:22:0093// If the user is not signed-in or the user does not have Google set up as their
94// default search engine, the personalized service is replaced with the most
95// visited service.
96bool PersonalizedServiceShouldFallBackToMostVisited(
Gheorghe Comanici864725b2017-11-28 21:48:0197 PrefService* prefs,
Gheorghe Comanici682504102017-11-30 17:47:2598 bool is_authenticated,
Gheorghe Comanici864725b2017-11-28 21:48:0199 const TemplateURLService* template_url_service) {
Gheorghe Comanici9a364f572018-01-24 17:22:00100 if (!is_authenticated)
Gheorghe Comanici864725b2017-11-28 21:48:01101 return true;
102
Gheorghe Comanici9a364f572018-01-24 17:22:00103 if (template_url_service == nullptr)
104 return false;
Gheorghe Comanici682504102017-11-30 17:47:25105
Gheorghe Comanici9a364f572018-01-24 17:22:00106 const TemplateURL* default_provider =
107 template_url_service->GetDefaultSearchProvider();
108 return default_provider == nullptr ||
109 default_provider->GetEngineType(
110 template_url_service->search_terms_data()) != SEARCH_ENGINE_GOOGLE;
Gheorghe Comanici864725b2017-11-28 21:48:01111}
112
[email protected]6ce7f612012-09-05 23:53:07113} // namespace
114
[email protected]a00008d42012-09-15 05:07:58115// static
116ZeroSuggestProvider* ZeroSuggestProvider::Create(
blundell55e35e82015-06-16 08:46:18117 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:11118 HistoryURLProvider* history_url_provider,
blundelld130d592015-06-21 19:29:13119 AutocompleteProviderListener* listener) {
mpearson931028c2016-07-01 18:55:11120 return new ZeroSuggestProvider(client, history_url_provider, listener);
[email protected]6ce7f612012-09-05 23:53:07121}
122
[email protected]855ebff2014-05-09 07:14:38123// static
124void ZeroSuggestProvider::RegisterProfilePrefs(
125 user_prefs::PrefRegistrySyncable* registry) {
blundelld130d592015-06-21 19:29:13126 registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults,
127 std::string());
[email protected]855ebff2014-05-09 07:14:38128}
129
[email protected]6ce7f612012-09-05 23:53:07130void ZeroSuggestProvider::Start(const AutocompleteInput& input,
jifcf322cd2015-06-17 11:01:18131 bool minimal_changes) {
a-v-ydd768d54b2016-03-25 21:07:46132 TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start");
[email protected]f030c4d2014-03-25 01:05:54133 matches_.clear();
Kevin Bailey8642e612018-04-23 20:27:54134 Stop(true, false);
Jenny Zhang5bc2e3d2018-09-10 18:50:55135 if (!input.from_omnibox_focus() || client()->IsOffTheRecord())
136 return;
137
138 // Zero suggest is allowed to run in the Chrome OS app_list context
139 // with invalid (empty) input.
140 if (input.type() == metrics::OmniboxInputType::INVALID &&
141 input.current_page_classification() !=
142 metrics::OmniboxEventProto::CHROMEOS_APP_LIST)
[email protected]f030c4d2014-03-25 01:05:54143 return;
[email protected]bb1fb2b2013-05-31 00:21:01144
Kevin Bailey5fea4322018-03-21 22:36:05145 result_type_running_ = NONE;
blundelld130d592015-06-21 19:29:13146 set_field_trial_triggered(false);
147 set_field_trial_triggered_in_session(false);
[email protected]f030c4d2014-03-25 01:05:54148 permanent_text_ = input.text();
149 current_query_ = input.current_url().spec();
gcomanici8cabc77f2017-04-27 20:04:54150 current_title_ = input.current_title();
[email protected]f030c4d2014-03-25 01:05:54151 current_page_classification_ = input.current_page_classification();
[email protected]9b9fa672013-11-07 06:04:52152 current_url_match_ = MatchForCurrentURL();
153
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11154 GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl(
Jenny Zhang5bc2e3d2018-09-10 18:50:55155 /*current_url=*/"", input, client()->GetTemplateURLService());
[email protected]162c8d9fa2014-03-18 20:25:41156 if (!suggest_url.is_valid())
[email protected]6ce7f612012-09-05 23:53:07157 return;
[email protected]162c8d9fa2014-03-18 20:25:41158
Gheorghe Comanici9a364f572018-01-24 17:22:00159 result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url);
Kevin Bailey5fea4322018-03-21 22:36:05160 if (result_type_running_ == NONE)
[email protected]162c8d9fa2014-03-18 20:25:41161 return;
[email protected]162c8d9fa2014-03-18 20:25:41162
[email protected]6ce7f612012-09-05 23:53:07163 done_ = false;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11164
[email protected]855ebff2014-05-09 07:14:38165 MaybeUseCachedSuggestions();
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11166
Kevin Bailey5fea4322018-03-21 22:36:05167 if (result_type_running_ == MOST_VISITED) {
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11168 most_visited_urls_.clear();
169 scoped_refptr<history::TopSites> ts = client()->GetTopSites();
Gheorghe Comanici9a364f572018-01-24 17:22:00170 if (!ts) {
171 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05172 result_type_running_ = NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00173 return;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11174 }
Gheorghe Comanici9a364f572018-01-24 17:22:00175
Kevin Bailey40bb0ac52019-04-02 00:43:32176 ts->GetMostVisitedURLs(base::BindRepeating(
177 &ZeroSuggestProvider::OnMostVisitedUrlsAvailable,
178 weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_));
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11179 return;
180 }
181
Kevin Bailey5fea4322018-03-21 22:36:05182 const std::string current_url = result_type_running_ == DEFAULT_SERP_FOR_URL
183 ? current_query_
184 : std::string();
Maks Orlovich1b208512018-06-13 21:08:17185 // Create a request for suggestions, routing completion to
186 // OnContextualSuggestionsLoaderAvailable.
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11187 client()
Gheorghe Comanici86bbdf62017-08-28 17:20:33188 ->GetContextualSuggestionsService(/*create_if_necessary=*/true)
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11189 ->CreateContextualSuggestionsRequest(
Jenny Zhang5bc2e3d2018-09-10 18:50:55190 current_url, client()->GetCurrentVisitTimestamp(), input,
Gheorghe Comanici034cff62018-01-27 03:34:00191 client()->GetTemplateURLService(),
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11192 base::BindOnce(
Maks Orlovich1b208512018-06-13 21:08:17193 &ZeroSuggestProvider::OnContextualSuggestionsLoaderAvailable,
194 weak_ptr_factory_.GetWeakPtr()),
195 base::BindOnce(
196 &ZeroSuggestProvider::OnURLLoadComplete,
197 base::Unretained(this) /* this owns SimpleURLLoader */));
[email protected]6ce7f612012-09-05 23:53:07198}
199
mpearson8a37c382015-03-07 05:58:57200void ZeroSuggestProvider::Stop(bool clear_cached_results,
201 bool due_to_user_inactivity) {
Maks Orlovich1b208512018-06-13 21:08:17202 if (loader_)
[email protected]ec3f679b2014-08-18 07:45:13203 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED);
Maks Orlovich1b208512018-06-13 21:08:17204 loader_.reset();
Gheorghe Comanici86bbdf62017-08-28 17:20:33205 auto* contextual_suggestions_service =
206 client()->GetContextualSuggestionsService(/*create_if_necessary=*/false);
207 // contextual_suggestions_service can be null if in incognito mode.
208 if (contextual_suggestions_service != nullptr) {
209 contextual_suggestions_service->StopCreatingContextualSuggestionsRequest();
210 }
Kevin Bailey5fea4322018-03-21 22:36:05211 // TODO(krb): It would allow us to remove some guards if we could also cancel
212 // the TopSites::GetMostVisitedURLs request.
[email protected]ec3f679b2014-08-18 07:45:13213 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05214 result_type_running_ = NONE;
215 ++most_visited_request_num_;
[email protected]ec3f679b2014-08-18 07:45:13216
217 if (clear_cached_results) {
218 // We do not call Clear() on |results_| to retain |verbatim_relevance|
219 // value in the |results_| object. |verbatim_relevance| is used at the
jifcf322cd2015-06-17 11:01:18220 // beginning of the next call to Start() to determine the current url
221 // match relevance.
[email protected]ec3f679b2014-08-18 07:45:13222 results_.suggest_results.clear();
223 results_.navigation_results.clear();
224 current_query_.clear();
gcomanici8cabc77f2017-04-27 20:04:54225 current_title_.clear();
mariakhomenko1535e6a2015-03-20 07:48:45226 most_visited_urls_.clear();
[email protected]ec3f679b2014-08-18 07:45:13227 }
228}
229
[email protected]855ebff2014-05-09 07:14:38230void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) {
Theresa3db4ae22018-03-05 18:47:40231 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) {
[email protected]855ebff2014-05-09 07:14:38232 // Remove the deleted match from the cache, so it is not shown to the user
233 // again. Since we cannot remove just one result, blow away the cache.
blundelld130d592015-06-21 19:29:13234 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
[email protected]855ebff2014-05-09 07:14:38235 std::string());
236 }
237 BaseSearchProvider::DeleteMatch(match);
238}
239
[email protected]ec3f679b2014-08-18 07:45:13240void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const {
241 BaseSearchProvider::AddProviderInfo(provider_info);
mariakhomenko1535e6a2015-03-20 07:48:45242 if (!results_.suggest_results.empty() ||
243 !results_.navigation_results.empty() ||
244 !most_visited_urls_.empty())
[email protected]ec3f679b2014-08-18 07:45:13245 provider_info->back().set_times_returned_results_in_session(1);
246}
247
[email protected]f030c4d2014-03-25 01:05:54248void ZeroSuggestProvider::ResetSession() {
249 // The user has started editing in the omnibox, so leave
blundelld130d592015-06-21 19:29:13250 // |field_trial_triggered_in_session| unchanged and set
251 // |field_trial_triggered| to false since zero suggest is inactive now.
252 set_field_trial_triggered(false);
[email protected]f030c4d2014-03-25 01:05:54253}
254
mpearson931028c2016-07-01 18:55:11255ZeroSuggestProvider::ZeroSuggestProvider(
256 AutocompleteProviderClient* client,
257 HistoryURLProvider* history_url_provider,
258 AutocompleteProviderListener* listener)
blundelld130d592015-06-21 19:29:13259 : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client),
mpearson931028c2016-07-01 18:55:11260 history_url_provider_(history_url_provider),
[email protected]776ee5902014-08-11 09:15:19261 listener_(listener),
Kevin Bailey5fea4322018-03-21 22:36:05262 result_type_running_(NONE),
[email protected]8f064e52013-09-18 01:17:14263 weak_ptr_factory_(this) {
mpearson3d89cdc2017-03-03 21:15:45264 // Record whether contextual zero suggest is possible for this user / profile.
265 const TemplateURLService* template_url_service =
266 client->GetTemplateURLService();
267 // Template URL service can be null in tests.
268 if (template_url_service != nullptr) {
Jenny Zhang5bc2e3d2018-09-10 18:50:55269 AutocompleteInput empty_input;
Daniel Kenji Toyamaf1e4b572017-08-03 16:31:11270 GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl(
Jenny Zhang5bc2e3d2018-09-10 18:50:55271 /*current_url=*/"", /*empty input*/ empty_input, template_url_service);
mpearson3d89cdc2017-03-03 21:15:45272 // To check whether this is allowed, use an arbitrary insecure (http) URL
273 // as the URL we'd want suggestions for. The value of OTHER as the current
274 // page classification is to correspond with that URL.
275 UMA_HISTOGRAM_BOOLEAN(
276 "Omnibox.ZeroSuggest.Eligible.OnProfileOpen",
277 suggest_url.is_valid() &&
278 CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url,
279 template_url_service->GetDefaultSearchProvider(),
280 metrics::OmniboxEventProto::OTHER,
281 template_url_service->search_terms_data(), client));
282 }
[email protected]6ce7f612012-09-05 23:53:07283}
284
285ZeroSuggestProvider::~ZeroSuggestProvider() {
286}
287
[email protected]776ee5902014-08-11 09:15:19288const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const {
289 // Zero suggest provider should not receive keyword results.
290 DCHECK(!is_keyword);
blundelld130d592015-06-21 19:29:13291 return client()->GetTemplateURLService()->GetDefaultSearchProvider();
[email protected]776ee5902014-08-11 09:15:19292}
293
294const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const {
jifcf322cd2015-06-17 11:01:18295 // The callers of this method won't look at the AutocompleteInput's
296 // |from_omnibox_focus| member, so we can set its value to false.
Kevin Baileybcc319e2017-10-01 21:53:02297 AutocompleteInput input(base::string16(), current_page_classification_,
298 client()->GetSchemeClassifier());
299 input.set_current_url(GURL(current_query_));
300 input.set_current_title(current_title_);
301 input.set_prevent_inline_autocomplete(true);
302 input.set_allow_exact_keyword_match(false);
303 return input;
[email protected]776ee5902014-08-11 09:15:19304}
305
306bool ZeroSuggestProvider::ShouldAppendExtraParams(
307 const SearchSuggestionParser::SuggestResult& result) const {
308 // We always use the default provider for search, so append the params.
309 return true;
310}
311
[email protected]776ee5902014-08-11 09:15:19312void ZeroSuggestProvider::RecordDeletionResult(bool success) {
313 if (success) {
314 base::RecordAction(
315 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success"));
316 } else {
317 base::RecordAction(
318 base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure"));
319 }
320}
321
Maks Orlovich1b208512018-06-13 21:08:17322void ZeroSuggestProvider::OnURLLoadComplete(
323 const network::SimpleURLLoader* source,
324 std::unique_ptr<std::string> response_body) {
[email protected]776ee5902014-08-11 09:15:19325 DCHECK(!done_);
Maks Orlovich1b208512018-06-13 21:08:17326 DCHECK_EQ(loader_.get(), source);
[email protected]776ee5902014-08-11 09:15:19327
328 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED);
329
Gheorghe Comanici9a364f572018-01-24 17:22:00330 const bool results_updated =
Maks Orlovich1b208512018-06-13 21:08:17331 response_body && source->NetError() == net::OK &&
332 (source->ResponseInfo() && source->ResponseInfo()->headers &&
333 source->ResponseInfo()->headers->response_code() == 200) &&
334 UpdateResults(SearchSuggestionParser::ExtractJsonData(
335 source, std::move(response_body)));
336 loader_.reset();
[email protected]776ee5902014-08-11 09:15:19337 done_ = true;
Kevin Bailey5fea4322018-03-21 22:36:05338 result_type_running_ = NONE;
339 ++most_visited_request_num_;
[email protected]776ee5902014-08-11 09:15:19340 listener_->OnProviderUpdate(results_updated);
341}
342
Gheorghe Comanici9a364f572018-01-24 17:22:00343bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) {
344 std::unique_ptr<base::Value> data(
345 SearchSuggestionParser::DeserializeJsonData(json_data));
346 if (!data)
[email protected]855ebff2014-05-09 07:14:38347 return false;
348
Gheorghe Comanici9a364f572018-01-24 17:22:00349 // When running the personalized service, we want to store suggestion
350 // responses if non-empty.
Kevin Bailey5fea4322018-03-21 22:36:05351 if (result_type_running_ == DEFAULT_SERP && !json_data.empty()) {
Gheorghe Comanici9a364f572018-01-24 17:22:00352 client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults,
353 json_data);
[email protected]855ebff2014-05-09 07:14:38354
Gheorghe Comanici9a364f572018-01-24 17:22:00355 // If we received an empty result list, we should update the display, as it
356 // may be showing cached results that should not be shown.
357 const base::ListValue* root_list = nullptr;
358 const base::ListValue* results_list = nullptr;
359 const bool non_empty_parsed_list = data->GetAsList(&root_list) &&
360 root_list->GetList(1, &results_list) &&
361 !results_list->empty();
362 const bool non_empty_cache = !results_.suggest_results.empty() ||
363 !results_.navigation_results.empty();
364 if (non_empty_parsed_list && non_empty_cache)
365 return false;
366 }
367 const bool results_updated = ParseSuggestResults(
368 *data, kDefaultZeroSuggestRelevance, false, &results_);
369 ConvertResultsToAutocompleteMatches();
370 return results_updated;
[email protected]855ebff2014-05-09 07:14:38371}
372
[email protected]bb1fb2b2013-05-31 00:21:01373void ZeroSuggestProvider::AddSuggestResultsToMap(
[email protected]0b9575f2014-07-30 11:58:37374 const SearchSuggestionParser::SuggestResults& results,
[email protected]02346202014-02-05 05:18:30375 MatchMap* map) {
[email protected]d4a94b92014-03-04 01:35:22376 for (size_t i = 0; i < results.size(); ++i)
[email protected]7bc5e162014-08-15 19:41:11377 AddMatchToMap(results[i], std::string(), i, false, false, map);
[email protected]bb1fb2b2013-05-31 00:21:01378}
379
[email protected]bb1fb2b2013-05-31 00:21:01380AutocompleteMatch ZeroSuggestProvider::NavigationToMatch(
[email protected]0b9575f2014-07-30 11:58:37381 const SearchSuggestionParser::NavigationResult& navigation) {
[email protected]bb1fb2b2013-05-31 00:21:01382 AutocompleteMatch match(this, navigation.relevance(), false,
[email protected]78981d8c2014-05-09 15:05:47383 navigation.type());
[email protected]bb1fb2b2013-05-31 00:21:01384 match.destination_url = navigation.url();
385
[email protected]bb1fb2b2013-05-31 00:21:01386 match.fill_into_edit +=
blundelld130d592015-06-21 19:29:13387 AutocompleteInput::FormattedStringWithEquivalentMeaning(
Tommy C. Li0beb8152017-08-25 18:30:26388 navigation.url(), url_formatter::FormatUrl(navigation.url()),
Kevin Bailey83e643d2018-03-08 16:01:41389 client()->GetSchemeClassifier(), nullptr);
[email protected]bb1fb2b2013-05-31 00:21:01390
manuk3972b2f2019-04-19 14:22:56391 // Zero suggest results should always omit protocols and never appear bold.
392 auto format_types = AutocompleteMatch::GetFormatTypes(false, false);
393 match.contents = url_formatter::FormatUrl(navigation.url(), format_types,
394 net::UnescapeRule::SPACES, nullptr,
395 nullptr, nullptr);
396 match.contents_class = ClassifyTermMatches({}, match.contents.length(), 0,
397 ACMatchClassification::URL);
[email protected]9c97f89c2013-06-25 03:12:16398
399 match.description =
400 AutocompleteMatch::SanitizeString(navigation.description());
manuk3972b2f2019-04-19 14:22:56401 match.description_class = ClassifyTermMatches({}, match.description.length(),
402 0, ACMatchClassification::NONE);
403
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 {
Tomasz Wiszkowski947f6d0d2019-03-15 22:27:25517 if (IsNTPPage(current_page_classification_) &&
518 !base::FeatureList::IsEnabled(omnibox::kZeroSuggestionsOnNTP)) {
Peter Kastingfb1a8ea2017-11-28 02:26:50519 return false;
Tomasz Wiszkowski947f6d0d2019-03-15 22:27:25520 }
Peter Kastingfb1a8ea2017-11-28 02:26:50521
522 // Don't run if in incognito mode.
523 if (client()->IsOffTheRecord())
[email protected]162c8d9fa2014-03-18 20:25:41524 return false;
525
[email protected]5b899dc2018-10-02 09:20:01526 if (base::FeatureList::IsEnabled(
527 omnibox::kOmniboxPopupShortcutIconsInZeroState)) {
528 return false;
529 }
530
Jenny Zhang5bc2e3d2018-09-10 18:50:55531 // Only show zero suggest for pages with URLs the user will recognize
532 // if it is not running in ChromeOS app_list context.
Mark Pearson3fb0e3162018-08-27 21:53:57533 // This list intentionally does not include items such as ftp: and file:
534 // because (a) these do not work on Android and iOS, where non-contextual
535 // zero suggest is launched and (b) on desktop, where contextual zero suggest
536 // is running, these types of schemes aren't eligible to be sent to the
537 // server to ask for suggestions (and thus in practice we won't display zero
538 // suggest for them).
Jenny Zhang5bc2e3d2018-09-10 18:50:55539 if (current_page_classification_ !=
540 metrics::OmniboxEventProto::CHROMEOS_APP_LIST &&
541 (!current_page_url.is_valid() ||
542 ((current_page_url.scheme() != url::kHttpScheme) &&
543 (current_page_url.scheme() != url::kHttpsScheme) &&
544 (current_page_url.scheme() != url::kAboutScheme) &&
545 (current_page_url.scheme() !=
546 client()->GetEmbedderRepresentationOfAboutScheme()))))
[email protected]162c8d9fa2014-03-18 20:25:41547 return false;
548
[email protected]162c8d9fa2014-03-18 20:25:41549 return true;
550}
[email protected]855ebff2014-05-09 07:14:38551
552void ZeroSuggestProvider::MaybeUseCachedSuggestions() {
Kevin Bailey5fea4322018-03-21 22:36:05553 if (result_type_running_ != DEFAULT_SERP)
[email protected]855ebff2014-05-09 07:14:38554 return;
555
blundelld130d592015-06-21 19:29:13556 std::string json_data =
557 client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults);
[email protected]855ebff2014-05-09 07:14:38558 if (!json_data.empty()) {
dcheng259570c2016-04-22 00:45:57559 std::unique_ptr<base::Value> data(
[email protected]2c802d12014-07-31 12:57:14560 SearchSuggestionParser::DeserializeJsonData(json_data));
Gheorghe Comanici9a364f572018-01-24 17:22:00561 if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false,
562 &results_))
[email protected]855ebff2014-05-09 07:14:38563 ConvertResultsToAutocompleteMatches();
[email protected]855ebff2014-05-09 07:14:38564 }
565}
Gheorghe Comanici9a364f572018-01-24 17:22:00566
567ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun(
568 const GURL& current_url,
569 const GURL& suggest_url) {
Gheorghe Comanici9a364f572018-01-24 17:22:00570 // Check if the URL can be sent in any suggest request.
571 const TemplateURLService* template_url_service =
572 client()->GetTemplateURLService();
Kevin Bailey9bdd15d2018-02-28 04:07:49573 DCHECK(template_url_service);
Gheorghe Comanici9a364f572018-01-24 17:22:00574 const TemplateURL* default_provider =
575 template_url_service->GetDefaultSearchProvider();
576 const bool can_send_current_url = CanSendURL(
577 current_url, suggest_url, default_provider, current_page_classification_,
578 template_url_service->search_terms_data(), client());
579
580 // Collect metrics on eligibility.
581 GURL arbitrary_insecure_url(kArbitraryInsecureUrlString);
582 ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE;
583 if (!can_send_current_url) {
584 const bool can_send_ordinary_url =
585 CanSendURL(arbitrary_insecure_url, suggest_url, default_provider,
586 current_page_classification_,
587 template_url_service->search_terms_data(), client());
588 eligibility = can_send_ordinary_url
589 ? ZeroSuggestEligibility::URL_INELIGIBLE
590 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE;
591 }
592 UMA_HISTOGRAM_ENUMERATION(
593 "Omnibox.ZeroSuggest.Eligible.OnFocus", static_cast<int>(eligibility),
594 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE));
595
596 // Check if zero suggestions are allowed in the current context.
597 if (!AllowZeroSuggestSuggestions(current_url))
Kevin Bailey5fea4322018-03-21 22:36:05598 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00599
Jenny Zhang5bc2e3d2018-09-10 18:50:55600 if (current_page_classification_ ==
601 metrics::OmniboxEventProto::CHROMEOS_APP_LIST) {
602 return DEFAULT_SERP;
603 }
604
Theresa3db4ae22018-03-05 18:47:40605 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial())
Gheorghe Comanici9a364f572018-01-24 17:22:00606 return PersonalizedServiceShouldFallBackToMostVisited(
607 client()->GetPrefs(), client()->IsAuthenticated(),
608 template_url_service)
Kevin Bailey5fea4322018-03-21 22:36:05609 ? MOST_VISITED
610 : DEFAULT_SERP;
Gheorghe Comanici9a364f572018-01-24 17:22:00611
Theresa3db4ae22018-03-05 18:47:40612 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() &&
Gheorghe Comanici9a364f572018-01-24 17:22:00613 client()
614 ->GetTemplateURLService()
615 ->IsSearchResultsPageFromDefaultSearchProvider(current_url))
Kevin Bailey5fea4322018-03-21 22:36:05616 return NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00617
Theresa3db4ae22018-03-05 18:47:40618 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial())
Kevin Bailey5fea4322018-03-21 22:36:05619 return MOST_VISITED;
Gheorghe Comanici9a364f572018-01-24 17:22:00620
Kevin Bailey5fea4322018-03-21 22:36:05621 return can_send_current_url ? DEFAULT_SERP_FOR_URL : NONE;
Gheorghe Comanici9a364f572018-01-24 17:22:00622}