[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 1 | // 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 | |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 5 | #include "components/omnibox/browser/zero_suggest_provider.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 6 | |
avi | f57136c1 | 2015-12-25 23:27:45 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
Kevin Bailey | 1e2a90e | 2017-10-27 21:02:05 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | |
Sebastien Marchand | 53801a3 | 2019-01-25 16:26:11 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 13 | #include "base/callback.h" |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 14 | #include "base/feature_list.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 15 | #include "base/i18n/case_conversion.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 16 | #include "base/json/json_string_value_serializer.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 17 | #include "base/metrics/histogram_macros.h" |
[email protected] | f7f41c0e | 2014-08-11 04:22:23 | [diff] [blame] | 18 | #include "base/metrics/user_metrics.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 19 | #include "base/strings/string16.h" |
| 20 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 4dcb797 | 2013-06-28 15:15:41 | [diff] [blame] | 22 | #include "base/time/time.h" |
a-v-y | dd768d5 | 2016-03-25 21:07:46 | [diff] [blame] | 23 | #include "base/trace_event/trace_event.h" |
amohammadkhan | f76ae11 | 2015-09-14 17:34:43 | [diff] [blame] | 24 | #include "components/data_use_measurement/core/data_use_user_data.h" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 25 | #include "components/history/core/browser/history_types.h" |
sdefresne | 0da3bc0 | 2015-01-29 18:26:35 | [diff] [blame] | 26 | #include "components/history/core/browser/top_sites.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 27 | #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 Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 31 | #include "components/omnibox/browser/contextual_suggestions_service.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 32 | #include "components/omnibox/browser/history_url_provider.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 33 | #include "components/omnibox/browser/omnibox_pref_names.h" |
| 34 | #include "components/omnibox/browser/search_provider.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 35 | #include "components/omnibox/browser/verbatim_match.h" |
Tomasz Wiszkowski | d938a111 | 2019-03-06 18:01:57 | [diff] [blame^] | 36 | #include "components/omnibox/common/omnibox_features.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame] | 37 | #include "components/pref_registry/pref_registry_syncable.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 38 | #include "components/prefs/pref_service.h" |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 39 | #include "components/search_engines/search_engine_type.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 40 | #include "components/search_engines/template_url_service.h" |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 41 | #include "components/url_formatter/url_formatter.h" |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 42 | #include "components/variations/net/variations_http_headers.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 43 | #include "net/base/escape.h" |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 44 | #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 Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame] | 47 | #include "third_party/metrics_proto/omnibox_event.pb.h" |
| 48 | #include "third_party/metrics_proto/omnibox_input_type.pb.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 49 | #include "url/gurl.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 50 | |
| 51 | namespace { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 52 | |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 53 | // 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. |
| 57 | enum 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] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 66 | // 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. |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 71 | // 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] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 73 | enum ZeroSuggestRequestsHistogramValue { |
| 74 | ZERO_SUGGEST_REQUEST_SENT = 1, |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 75 | ZERO_SUGGEST_REQUEST_INVALIDATED = 2, |
| 76 | ZERO_SUGGEST_REPLY_RECEIVED = 3, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 77 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE |
| 78 | }; |
| 79 | |
| 80 | void LogOmniboxZeroSuggestRequest( |
| 81 | ZeroSuggestRequestsHistogramValue request_value) { |
| 82 | UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value, |
| 83 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE); |
| 84 | } |
| 85 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 86 | // Relevance value to use if it was not set explicitly by the server. |
| 87 | const int kDefaultZeroSuggestRelevance = 100; |
| 88 | |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 89 | // Used for testing whether zero suggest is ever available. |
mpearson | c90c24b | 2017-03-04 00:11:26 | [diff] [blame] | 90 | constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/"; |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 91 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 92 | // 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. |
| 95 | bool PersonalizedServiceShouldFallBackToMostVisited( |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 96 | PrefService* prefs, |
Gheorghe Comanici | 68250410 | 2017-11-30 17:47:25 | [diff] [blame] | 97 | bool is_authenticated, |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 98 | const TemplateURLService* template_url_service) { |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 99 | if (!is_authenticated) |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 100 | return true; |
| 101 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 102 | if (template_url_service == nullptr) |
| 103 | return false; |
Gheorghe Comanici | 68250410 | 2017-11-30 17:47:25 | [diff] [blame] | 104 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 105 | 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 Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 112 | } // namespace |
| 113 | |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 114 | // static |
| 115 | ZeroSuggestProvider* ZeroSuggestProvider::Create( |
blundell | 55e35e8 | 2015-06-16 08:46:18 | [diff] [blame] | 116 | AutocompleteProviderClient* client, |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 117 | HistoryURLProvider* history_url_provider, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 118 | AutocompleteProviderListener* listener) { |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 119 | return new ZeroSuggestProvider(client, history_url_provider, listener); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 122 | // static |
| 123 | void ZeroSuggestProvider::RegisterProfilePrefs( |
| 124 | user_prefs::PrefRegistrySyncable* registry) { |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 125 | registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults, |
| 126 | std::string()); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 129 | void ZeroSuggestProvider::Start(const AutocompleteInput& input, |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 130 | bool minimal_changes) { |
a-v-y | dd768d5 | 2016-03-25 21:07:46 | [diff] [blame] | 131 | TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start"); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 132 | matches_.clear(); |
Kevin Bailey | 8642e61 | 2018-04-23 20:27:54 | [diff] [blame] | 133 | Stop(true, false); |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 134 | 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] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 142 | return; |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 143 | |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 144 | result_type_running_ = NONE; |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 145 | set_field_trial_triggered(false); |
| 146 | set_field_trial_triggered_in_session(false); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 147 | permanent_text_ = input.text(); |
| 148 | current_query_ = input.current_url().spec(); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 149 | current_title_ = input.current_title(); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 150 | current_page_classification_ = input.current_page_classification(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 151 | current_url_match_ = MatchForCurrentURL(); |
| 152 | |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 153 | GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl( |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 154 | /*current_url=*/"", input, client()->GetTemplateURLService()); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 155 | if (!suggest_url.is_valid()) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 156 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 157 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 158 | result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url); |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 159 | if (result_type_running_ == NONE) |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 160 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 161 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 162 | done_ = false; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 163 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 164 | MaybeUseCachedSuggestions(); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 165 | |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 166 | if (result_type_running_ == MOST_VISITED) { |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 167 | most_visited_urls_.clear(); |
| 168 | scoped_refptr<history::TopSites> ts = client()->GetTopSites(); |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 169 | if (!ts) { |
| 170 | done_ = true; |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 171 | result_type_running_ = NONE; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 172 | return; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 173 | } |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 174 | |
| 175 | ts->GetMostVisitedURLs( |
| 176 | base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, |
Kristi Park | ac1a89a | 2018-12-14 22:20:00 | [diff] [blame] | 177 | weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_)); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 181 | const std::string current_url = result_type_running_ == DEFAULT_SERP_FOR_URL |
| 182 | ? current_query_ |
| 183 | : std::string(); |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 184 | // Create a request for suggestions, routing completion to |
| 185 | // OnContextualSuggestionsLoaderAvailable. |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 186 | client() |
Gheorghe Comanici | 86bbdf6 | 2017-08-28 17:20:33 | [diff] [blame] | 187 | ->GetContextualSuggestionsService(/*create_if_necessary=*/true) |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 188 | ->CreateContextualSuggestionsRequest( |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 189 | current_url, client()->GetCurrentVisitTimestamp(), input, |
Gheorghe Comanici | 034cff6 | 2018-01-27 03:34:00 | [diff] [blame] | 190 | client()->GetTemplateURLService(), |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 191 | base::BindOnce( |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 192 | &ZeroSuggestProvider::OnContextualSuggestionsLoaderAvailable, |
| 193 | weak_ptr_factory_.GetWeakPtr()), |
| 194 | base::BindOnce( |
| 195 | &ZeroSuggestProvider::OnURLLoadComplete, |
| 196 | base::Unretained(this) /* this owns SimpleURLLoader */)); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 197 | } |
| 198 | |
mpearson | 8a37c38 | 2015-03-07 05:58:57 | [diff] [blame] | 199 | void ZeroSuggestProvider::Stop(bool clear_cached_results, |
| 200 | bool due_to_user_inactivity) { |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 201 | if (loader_) |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 202 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 203 | loader_.reset(); |
Gheorghe Comanici | 86bbdf6 | 2017-08-28 17:20:33 | [diff] [blame] | 204 | 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 Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 210 | // TODO(krb): It would allow us to remove some guards if we could also cancel |
| 211 | // the TopSites::GetMostVisitedURLs request. |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 212 | done_ = true; |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 213 | result_type_running_ = NONE; |
| 214 | ++most_visited_request_num_; |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 215 | |
| 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 |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 219 | // beginning of the next call to Start() to determine the current url |
| 220 | // match relevance. |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 221 | results_.suggest_results.clear(); |
| 222 | results_.navigation_results.clear(); |
| 223 | current_query_.clear(); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 224 | current_title_.clear(); |
mariakhomenko | 1535e6a | 2015-03-20 07:48:45 | [diff] [blame] | 225 | most_visited_urls_.clear(); |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 229 | void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) { |
Theresa | 3db4ae2 | 2018-03-05 18:47:40 | [diff] [blame] | 230 | if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 231 | // 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. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 233 | client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults, |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 234 | std::string()); |
| 235 | } |
| 236 | BaseSearchProvider::DeleteMatch(match); |
| 237 | } |
| 238 | |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 239 | void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 240 | BaseSearchProvider::AddProviderInfo(provider_info); |
mariakhomenko | 1535e6a | 2015-03-20 07:48:45 | [diff] [blame] | 241 | if (!results_.suggest_results.empty() || |
| 242 | !results_.navigation_results.empty() || |
| 243 | !most_visited_urls_.empty()) |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 244 | provider_info->back().set_times_returned_results_in_session(1); |
| 245 | } |
| 246 | |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 247 | void ZeroSuggestProvider::ResetSession() { |
| 248 | // The user has started editing in the omnibox, so leave |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 249 | // |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] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 252 | } |
| 253 | |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 254 | ZeroSuggestProvider::ZeroSuggestProvider( |
| 255 | AutocompleteProviderClient* client, |
| 256 | HistoryURLProvider* history_url_provider, |
| 257 | AutocompleteProviderListener* listener) |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 258 | : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client), |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 259 | history_url_provider_(history_url_provider), |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 260 | listener_(listener), |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 261 | result_type_running_(NONE), |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 262 | weak_ptr_factory_(this) { |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 263 | // 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 Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 268 | AutocompleteInput empty_input; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 269 | GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl( |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 270 | /*current_url=*/"", /*empty input*/ empty_input, template_url_service); |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 271 | // 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] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | ZeroSuggestProvider::~ZeroSuggestProvider() { |
| 285 | } |
| 286 | |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 287 | const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const { |
| 288 | // Zero suggest provider should not receive keyword results. |
| 289 | DCHECK(!is_keyword); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 290 | return client()->GetTemplateURLService()->GetDefaultSearchProvider(); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const { |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 294 | // 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 Bailey | bcc319e | 2017-10-01 21:53:02 | [diff] [blame] | 296 | 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] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | bool 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] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 311 | void 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 Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 321 | void ZeroSuggestProvider::OnURLLoadComplete( |
| 322 | const network::SimpleURLLoader* source, |
| 323 | std::unique_ptr<std::string> response_body) { |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 324 | DCHECK(!done_); |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 325 | DCHECK_EQ(loader_.get(), source); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 326 | |
| 327 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED); |
| 328 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 329 | const bool results_updated = |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 330 | 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] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 336 | done_ = true; |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 337 | result_type_running_ = NONE; |
| 338 | ++most_visited_request_num_; |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 339 | listener_->OnProviderUpdate(results_updated); |
| 340 | } |
| 341 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 342 | bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) { |
| 343 | std::unique_ptr<base::Value> data( |
| 344 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
| 345 | if (!data) |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 346 | return false; |
| 347 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 348 | // When running the personalized service, we want to store suggestion |
| 349 | // responses if non-empty. |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 350 | if (result_type_running_ == DEFAULT_SERP && !json_data.empty()) { |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 351 | client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults, |
| 352 | json_data); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 353 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 354 | // 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] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 370 | } |
| 371 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 372 | void ZeroSuggestProvider::AddSuggestResultsToMap( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 373 | const SearchSuggestionParser::SuggestResults& results, |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 374 | MatchMap* map) { |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 375 | for (size_t i = 0; i < results.size(); ++i) |
[email protected] | 7bc5e16 | 2014-08-15 19:41:11 | [diff] [blame] | 376 | AddMatchToMap(results[i], std::string(), i, false, false, map); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 379 | AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 380 | const SearchSuggestionParser::NavigationResult& navigation) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 381 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame] | 382 | navigation.type()); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 383 | match.destination_url = navigation.url(); |
| 384 | |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 385 | // Zero suggest results should always omit protocols and never appear bold. |
Tommy C. Li | 21da4352 | 2018-11-20 16:35:28 | [diff] [blame] | 386 | auto format_types = AutocompleteMatch::GetFormatTypes(false, false); |
tommycli | 72014f6 | 2017-06-29 21:42:16 | [diff] [blame] | 387 | match.contents = url_formatter::FormatUrl(navigation.url(), format_types, |
| 388 | net::UnescapeRule::SPACES, nullptr, |
| 389 | nullptr, nullptr); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 390 | match.fill_into_edit += |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 391 | AutocompleteInput::FormattedStringWithEquivalentMeaning( |
Tommy C. Li | 0beb815 | 2017-08-25 18:30:26 | [diff] [blame] | 392 | navigation.url(), url_formatter::FormatUrl(navigation.url()), |
Kevin Bailey | 83e643d | 2018-03-08 16:01:41 | [diff] [blame] | 393 | client()->GetSchemeClassifier(), nullptr); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 394 | |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 395 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 396 | match.contents.length(), ACMatchClassification::URL, |
| 397 | &match.contents_class); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 398 | |
| 399 | match.description = |
| 400 | AutocompleteMatch::SanitizeString(navigation.description()); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 401 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 402 | match.description.length(), ACMatchClassification::NONE, |
| 403 | &match.description_class); |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 404 | match.subtype_identifier = navigation.subtype_identifier(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 405 | return match; |
| 406 | } |
| 407 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 408 | void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 409 | size_t orig_request_num, |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 410 | const history::MostVisitedURLList& urls) { |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 411 | if (result_type_running_ != MOST_VISITED || |
| 412 | orig_request_num != most_visited_request_num_) { |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 413 | return; |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 414 | } |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 415 | most_visited_urls_ = urls; |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 416 | done_ = true; |
| 417 | ConvertResultsToAutocompleteMatches(); |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 418 | result_type_running_ = NONE; |
| 419 | ++most_visited_request_num_; |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 420 | listener_->OnProviderUpdate(true); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 421 | } |
| 422 | |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 423 | void 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 Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 429 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
| 430 | } |
| 431 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 432 | void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 433 | matches_.clear(); |
| 434 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 435 | TemplateURLService* template_url_service = client()->GetTemplateURLService(); |
Kevin Bailey | 9bdd15d | 2018-02-28 04:07:49 | [diff] [blame] | 436 | DCHECK(template_url_service); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 437 | const TemplateURL* default_provider = |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 438 | template_url_service->GetDefaultSearchProvider(); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 439 | // Fail if we can't set the clickthrough URL for query suggestions. |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 440 | if (default_provider == nullptr || |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 441 | !default_provider->SupportsReplacement( |
| 442 | template_url_service->search_terms_data())) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 443 | return; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 444 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 445 | 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] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 450 | const int num_results = num_query_results + num_nav_results; |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 451 | 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] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 454 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 455 | // Show Most Visited results after ZeroSuggest response is received. |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 456 | if (result_type_running_ == MOST_VISITED) { |
[email protected] | 3feb8b00 | 2013-10-14 23:50:13 | [diff] [blame] | 457 | if (!current_url_match_.destination_url.is_valid()) |
| 458 | return; |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 459 | matches_.push_back(current_url_match_); |
| 460 | int relevance = 600; |
| 461 | if (num_results > 0) { |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 462 | UMA_HISTOGRAM_COUNTS_1M( |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 463 | "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual", |
| 464 | most_visited_urls_.size()); |
| 465 | } |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 466 | const base::string16 current_query_string16( |
| 467 | base::ASCIIToUTF16(current_query_)); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 468 | for (size_t i = 0; i < most_visited_urls_.size(); i++) { |
| 469 | const history::MostVisitedURL& url = most_visited_urls_[i]; |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 470 | SearchSuggestionParser::NavigationResult nav( |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 471 | client()->GetSchemeClassifier(), url.url, |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 472 | AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false, |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 473 | relevance, true, current_query_string16); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 474 | matches_.push_back(NavigationToMatch(nav)); |
| 475 | --relevance; |
| 476 | } |
| 477 | return; |
| 478 | } |
| 479 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 480 | if (num_results == 0) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 481 | return; |
| 482 | |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 483 | // 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] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 490 | for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 491 | matches_.push_back(it->second); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 492 | |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 493 | const SearchSuggestionParser::NavigationResults& nav_results( |
| 494 | results_.navigation_results); |
jdoerrie | 2e6a651d | 2018-10-04 17:09:08 | [diff] [blame] | 495 | for (auto it = nav_results.begin(); it != nav_results.end(); ++it) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 496 | matches_.push_back(NavigationToMatch(*it)); |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 497 | } |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 498 | } |
| 499 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 500 | AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 501 | // 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. |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 504 | AutocompleteInput tmp(GetInput(false)); |
| 505 | tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts()); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 506 | 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, |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 511 | history_url_provider_, |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 512 | results_.verbatim_relevance); |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 513 | } |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 514 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 515 | bool ZeroSuggestProvider::AllowZeroSuggestSuggestions( |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 516 | const GURL& current_page_url) const { |
Peter Kasting | fb1a8ea | 2017-11-28 02:26:50 | [diff] [blame] | 517 | // 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] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 525 | return false; |
| 526 | |
[email protected] | 5b899dc | 2018-10-02 09:20:01 | [diff] [blame] | 527 | if (base::FeatureList::IsEnabled( |
| 528 | omnibox::kOmniboxPopupShortcutIconsInZeroState)) { |
| 529 | return false; |
| 530 | } |
| 531 | |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 532 | // Only show zero suggest for pages with URLs the user will recognize |
| 533 | // if it is not running in ChromeOS app_list context. |
Mark Pearson | 3fb0e316 | 2018-08-27 21:53:57 | [diff] [blame] | 534 | // 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 Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 540 | 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] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 548 | return false; |
| 549 | |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 550 | return true; |
| 551 | } |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 552 | |
| 553 | void ZeroSuggestProvider::MaybeUseCachedSuggestions() { |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 554 | if (result_type_running_ != DEFAULT_SERP) |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 555 | return; |
| 556 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 557 | std::string json_data = |
| 558 | client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 559 | if (!json_data.empty()) { |
dcheng | 259570c | 2016-04-22 00:45:57 | [diff] [blame] | 560 | std::unique_ptr<base::Value> data( |
[email protected] | 2c802d1 | 2014-07-31 12:57:14 | [diff] [blame] | 561 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 562 | if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false, |
| 563 | &results_)) |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 564 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 565 | } |
| 566 | } |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 567 | |
| 568 | ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun( |
| 569 | const GURL& current_url, |
| 570 | const GURL& suggest_url) { |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 571 | // Check if the URL can be sent in any suggest request. |
| 572 | const TemplateURLService* template_url_service = |
| 573 | client()->GetTemplateURLService(); |
Kevin Bailey | 9bdd15d | 2018-02-28 04:07:49 | [diff] [blame] | 574 | DCHECK(template_url_service); |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 575 | 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 Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 599 | return NONE; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 600 | |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 601 | if (current_page_classification_ == |
| 602 | metrics::OmniboxEventProto::CHROMEOS_APP_LIST) { |
| 603 | return DEFAULT_SERP; |
| 604 | } |
| 605 | |
Theresa | 3db4ae2 | 2018-03-05 18:47:40 | [diff] [blame] | 606 | if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 607 | return PersonalizedServiceShouldFallBackToMostVisited( |
| 608 | client()->GetPrefs(), client()->IsAuthenticated(), |
| 609 | template_url_service) |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 610 | ? MOST_VISITED |
| 611 | : DEFAULT_SERP; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 612 | |
Theresa | 3db4ae2 | 2018-03-05 18:47:40 | [diff] [blame] | 613 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() && |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 614 | client() |
| 615 | ->GetTemplateURLService() |
| 616 | ->IsSearchResultsPageFromDefaultSearchProvider(current_url)) |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 617 | return NONE; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 618 | |
Theresa | 3db4ae2 | 2018-03-05 18:47:40 | [diff] [blame] | 619 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 620 | return MOST_VISITED; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 621 | |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 622 | return can_send_current_url ? DEFAULT_SERP_FOR_URL : NONE; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 623 | } |