[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" |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 19 | #include "base/stl_util.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 20 | #include "base/strings/string16.h" |
| 21 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 22 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 4dcb797 | 2013-06-28 15:15:41 | [diff] [blame] | 23 | #include "base/time/time.h" |
a-v-y | dd768d5 | 2016-03-25 21:07:46 | [diff] [blame] | 24 | #include "base/trace_event/trace_event.h" |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 25 | #include "build/build_config.h" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 26 | #include "components/history/core/browser/history_types.h" |
sdefresne | 0da3bc0 | 2015-01-29 18:26:35 | [diff] [blame] | 27 | #include "components/history/core/browser/top_sites.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 28 | #include "components/omnibox/browser/autocomplete_classifier.h" |
| 29 | #include "components/omnibox/browser/autocomplete_input.h" |
| 30 | #include "components/omnibox/browser/autocomplete_match.h" |
manuk | 3972b2f | 2019-04-19 14:22:56 | [diff] [blame] | 31 | #include "components/omnibox/browser/autocomplete_match_classification.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 32 | #include "components/omnibox/browser/autocomplete_provider_listener.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 33 | #include "components/omnibox/browser/history_url_provider.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 34 | #include "components/omnibox/browser/omnibox_pref_names.h" |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 35 | #include "components/omnibox/browser/remote_suggestions_service.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 36 | #include "components/omnibox/browser/search_provider.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 37 | #include "components/omnibox/browser/verbatim_match.h" |
Tomasz Wiszkowski | d938a111 | 2019-03-06 18:01:57 | [diff] [blame] | 38 | #include "components/omnibox/common/omnibox_features.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame] | 39 | #include "components/pref_registry/pref_registry_syncable.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 40 | #include "components/prefs/pref_service.h" |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 41 | #include "components/search_engines/search_engine_type.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 42 | #include "components/search_engines/template_url_service.h" |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 43 | #include "components/url_formatter/url_formatter.h" |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 44 | #include "components/variations/net/variations_http_headers.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 45 | #include "net/base/escape.h" |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 46 | #include "services/network/public/cpp/resource_response.h" |
| 47 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
| 48 | #include "services/network/public/cpp/simple_url_loader.h" |
Steven Holte | f9d5ed6 | 2017-10-21 02:02:30 | [diff] [blame] | 49 | #include "third_party/metrics_proto/omnibox_event.pb.h" |
| 50 | #include "third_party/metrics_proto/omnibox_input_type.pb.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 51 | #include "url/gurl.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 52 | |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 53 | using metrics::OmniboxEventProto; |
| 54 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 55 | namespace { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 56 | |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 57 | // Represents whether ZeroSuggestProvider is allowed to display contextual |
| 58 | // suggestions on focus, and if not, why not. |
| 59 | // These values are written to logs. New enum values can be added, but existing |
| 60 | // enums must never be renumbered or deleted and reused. |
| 61 | enum class ZeroSuggestEligibility { |
| 62 | ELIGIBLE = 0, |
| 63 | // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL |
| 64 | // itself prevents ZeroSuggest from triggering. |
| 65 | URL_INELIGIBLE = 1, |
| 66 | GENERALLY_INELIGIBLE = 2, |
| 67 | ELIGIBLE_MAX_VALUE |
| 68 | }; |
| 69 | |
Tomasz Wiszkowski | fd790c0 | 2019-08-29 17:31:29 | [diff] [blame] | 70 | // Histogram values describing client eligibility to receive RemoteNoURL |
| 71 | // suggestions on NTP. |
| 72 | // These values are persisted to logs. New values can be added, but existing |
| 73 | // enums must never be renumbered or deleted and reused. |
| 74 | enum class ZeroSuggestEligibilityForRemoteNoURL { |
| 75 | kEligible = 0, |
| 76 | kIneligibleUserOffTheRecord = 1, |
| 77 | kIneligibleSuggestionsDisabled = 2, |
| 78 | kIneligibleUserNotAuthenticated = 3, |
| 79 | // Used to report users ineligible for RemoteNoURL suggestions when the |
| 80 | // search and suggest server of their choice cannot be used to offer the |
| 81 | // RemoteNoURL suggestions. |
| 82 | kIneligibleWithUserSelectedServer = 4, |
| 83 | kIneligibleUserNotParticipating = 5, |
| 84 | |
| 85 | kMaxValue = kIneligibleUserNotParticipating |
| 86 | }; |
| 87 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 88 | // TODO(hfung): The histogram code was copied and modified from |
| 89 | // search_provider.cc. Refactor and consolidate the code. |
| 90 | // We keep track in a histogram how many suggest requests we send, how |
| 91 | // many suggest requests we invalidate (e.g., due to a user typing |
| 92 | // another character), and how many replies we receive. |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 93 | // These values are written to logs. New enum values can be added, but existing |
| 94 | // enums must never be renumbered or deleted and reused. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 95 | enum ZeroSuggestRequestsHistogramValue { |
| 96 | ZERO_SUGGEST_REQUEST_SENT = 1, |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 97 | ZERO_SUGGEST_REQUEST_INVALIDATED = 2, |
| 98 | ZERO_SUGGEST_REPLY_RECEIVED = 3, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 99 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE |
| 100 | }; |
| 101 | |
| 102 | void LogOmniboxZeroSuggestRequest( |
| 103 | ZeroSuggestRequestsHistogramValue request_value) { |
| 104 | UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value, |
| 105 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE); |
| 106 | } |
| 107 | |
Tomasz Wiszkowski | fd790c0 | 2019-08-29 17:31:29 | [diff] [blame] | 108 | // Record user eligibility for RemoteNoUrl suggestions for supplied page class. |
| 109 | // The |histogram_variant| is used to specify particular variant of the |
| 110 | // Omnibox.ZeroSuggest.Eligible.RemoteNoUrl histogram that should be updated. |
| 111 | void LogOmniboxRemoteNoUrlEligibilityOnNTP( |
| 112 | OmniboxEventProto::PageClassification page_class, |
| 113 | bool log_for_profile_open, |
| 114 | AutocompleteProviderClient* client) { |
| 115 | ZeroSuggestEligibilityForRemoteNoURL value = |
| 116 | ZeroSuggestEligibilityForRemoteNoURL::kEligible; |
| 117 | |
| 118 | auto* service = client->GetTemplateURLService(); |
| 119 | auto* provider = service ? service->GetDefaultSearchProvider() : nullptr; |
| 120 | auto engine = provider ? provider->GetEngineType(service->search_terms_data()) |
| 121 | : SEARCH_ENGINE_UNKNOWN; |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 122 | const auto variants = OmniboxFieldTrial::GetZeroSuggestVariants(page_class); |
Tomasz Wiszkowski | fd790c0 | 2019-08-29 17:31:29 | [diff] [blame] | 123 | |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 124 | if (!base::Contains(variants, ZeroSuggestProvider::kRemoteNoUrlVariant)) { |
Tomasz Wiszkowski | fd790c0 | 2019-08-29 17:31:29 | [diff] [blame] | 125 | value = |
| 126 | ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserNotParticipating; |
| 127 | } else if (client->IsOffTheRecord()) { |
| 128 | value = ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserOffTheRecord; |
| 129 | } else if (!client->SearchSuggestEnabled()) { |
| 130 | value = |
| 131 | ZeroSuggestEligibilityForRemoteNoURL::kIneligibleSuggestionsDisabled; |
| 132 | } else if (!client->IsAuthenticated()) { |
| 133 | value = |
| 134 | ZeroSuggestEligibilityForRemoteNoURL::kIneligibleUserNotAuthenticated; |
| 135 | } else if (service == nullptr || provider == nullptr || |
| 136 | engine != SEARCH_ENGINE_GOOGLE) { |
| 137 | value = |
| 138 | ZeroSuggestEligibilityForRemoteNoURL::kIneligibleWithUserSelectedServer; |
| 139 | } |
| 140 | |
| 141 | if (log_for_profile_open) { |
| 142 | UMA_HISTOGRAM_ENUMERATION( |
| 143 | "Omnibox.ZeroSuggest.Eligible.RemoteNoUrl.OnNTP.OnProfileOpen", value); |
| 144 | } else { |
| 145 | UMA_HISTOGRAM_ENUMERATION( |
| 146 | "Omnibox.ZeroSuggest.Eligible.RemoteNoUrl.OnNTP.OnFocus", value); |
| 147 | } |
| 148 | } |
| 149 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 150 | // Relevance value to use if it was not set explicitly by the server. |
| 151 | const int kDefaultZeroSuggestRelevance = 100; |
| 152 | |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 153 | // Used for testing whether zero suggest is ever available. |
mpearson | c90c24b | 2017-03-04 00:11:26 | [diff] [blame] | 154 | constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/"; |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 155 | |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 156 | // Metric name tracking the omnibox suggestion eligibility. |
| 157 | constexpr char kOmniboxZeroSuggestEligibleHistogramName[] = |
| 158 | "Omnibox.ZeroSuggest.Eligible.OnFocusV2"; |
| 159 | |
Moe Ahmadi | 04206cd | 2019-08-23 18:03:22 | [diff] [blame] | 160 | #if defined(OS_ANDROID) || defined(OS_IOS) |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 161 | // If the user is not signed-in or the user does not have Google set up as their |
Tommy C. Li | 467c1bcaa | 2019-06-20 20:48:05 | [diff] [blame] | 162 | // default search engine, the remote suggestions service is replaced with the |
| 163 | // most visited service. |
| 164 | bool RemoteSuggestionsShouldFallBackToMostVisited( |
Tomasz Wiszkowski | 5abea20 | 2019-06-06 22:51:14 | [diff] [blame] | 165 | AutocompleteProviderClient* client, |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 166 | const TemplateURLService* template_url_service) { |
Tomasz Wiszkowski | 5abea20 | 2019-06-06 22:51:14 | [diff] [blame] | 167 | if (!client->SearchSuggestEnabled()) |
| 168 | return true; |
| 169 | |
| 170 | if (!client->IsAuthenticated()) |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 171 | return true; |
| 172 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 173 | if (template_url_service == nullptr) |
| 174 | return false; |
Gheorghe Comanici | 68250410 | 2017-11-30 17:47:25 | [diff] [blame] | 175 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 176 | const TemplateURL* default_provider = |
| 177 | template_url_service->GetDefaultSearchProvider(); |
| 178 | return default_provider == nullptr || |
| 179 | default_provider->GetEngineType( |
| 180 | template_url_service->search_terms_data()) != SEARCH_ENGINE_GOOGLE; |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 181 | } |
Moe Ahmadi | 04206cd | 2019-08-23 18:03:22 | [diff] [blame] | 182 | #endif |
Gheorghe Comanici | 864725b | 2017-11-28 21:48:01 | [diff] [blame] | 183 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 184 | } // namespace |
| 185 | |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 186 | // static |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 187 | const char ZeroSuggestProvider::kNoneVariant[] = "None"; |
| 188 | const char ZeroSuggestProvider::kRemoteNoUrlVariant[] = "RemoteNoUrl"; |
| 189 | const char ZeroSuggestProvider::kRemoteSendUrlVariant[] = "RemoteSendUrl"; |
| 190 | const char ZeroSuggestProvider::kMostVisitedVariant[] = "MostVisited"; |
| 191 | |
| 192 | // static |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 193 | ZeroSuggestProvider* ZeroSuggestProvider::Create( |
blundell | 55e35e8 | 2015-06-16 08:46:18 | [diff] [blame] | 194 | AutocompleteProviderClient* client, |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 195 | HistoryURLProvider* history_url_provider, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 196 | AutocompleteProviderListener* listener) { |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 197 | return new ZeroSuggestProvider(client, history_url_provider, listener); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 200 | // static |
| 201 | void ZeroSuggestProvider::RegisterProfilePrefs( |
| 202 | user_prefs::PrefRegistrySyncable* registry) { |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 203 | registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults, |
| 204 | std::string()); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 207 | void ZeroSuggestProvider::Start(const AutocompleteInput& input, |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 208 | bool minimal_changes) { |
a-v-y | dd768d5 | 2016-03-25 21:07:46 | [diff] [blame] | 209 | TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start"); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 210 | matches_.clear(); |
Kevin Bailey | 8642e61 | 2018-04-23 20:27:54 | [diff] [blame] | 211 | Stop(true, false); |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 212 | |
Tomasz Wiszkowski | fd790c0 | 2019-08-29 17:31:29 | [diff] [blame] | 213 | current_page_classification_ = input.current_page_classification(); |
| 214 | |
| 215 | if (input.from_omnibox_focus() && IsNTPPage(current_page_classification_)) { |
| 216 | LogOmniboxRemoteNoUrlEligibilityOnNTP(current_page_classification_, false, |
| 217 | client()); |
| 218 | } |
| 219 | |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 220 | if (!AllowZeroSuggestSuggestions(input)) { |
| 221 | UMA_HISTOGRAM_ENUMERATION(kOmniboxZeroSuggestEligibleHistogramName, |
| 222 | ZeroSuggestEligibility::GENERALLY_INELIGIBLE, |
| 223 | ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 224 | return; |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 225 | } |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 226 | |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 227 | result_type_running_ = NONE; |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 228 | set_field_trial_triggered(false); |
| 229 | set_field_trial_triggered_in_session(false); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 230 | permanent_text_ = input.text(); |
| 231 | current_query_ = input.current_url().spec(); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 232 | current_title_ = input.current_title(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 233 | current_url_match_ = MatchForCurrentURL(); |
| 234 | |
Tommy C. Li | 6aee62d5 | 2019-06-27 20:25:00 | [diff] [blame] | 235 | TemplateURLRef::SearchTermsArgs search_terms_args; |
| 236 | search_terms_args.page_classification = current_page_classification_; |
| 237 | search_terms_args.omnibox_focus_type = |
| 238 | TemplateURLRef::SearchTermsArgs::OmniboxFocusType::ON_FOCUS; |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 239 | GURL suggest_url = RemoteSuggestionsService::EndpointUrl( |
Tommy C. Li | 6aee62d5 | 2019-06-27 20:25:00 | [diff] [blame] | 240 | search_terms_args, client()->GetTemplateURLService()); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 241 | if (!suggest_url.is_valid()) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 242 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 243 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 244 | result_type_running_ = TypeOfResultToRun(input.current_url(), suggest_url); |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 245 | if (result_type_running_ == NONE) |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 246 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 247 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 248 | done_ = false; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 249 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 250 | MaybeUseCachedSuggestions(); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 251 | |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 252 | if (result_type_running_ == MOST_VISITED) { |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 253 | most_visited_urls_.clear(); |
| 254 | scoped_refptr<history::TopSites> ts = client()->GetTopSites(); |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 255 | if (!ts) { |
| 256 | done_ = true; |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 257 | result_type_running_ = NONE; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 258 | return; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 259 | } |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 260 | |
Kevin Bailey | 40bb0ac5 | 2019-04-02 00:43:32 | [diff] [blame] | 261 | ts->GetMostVisitedURLs(base::BindRepeating( |
| 262 | &ZeroSuggestProvider::OnMostVisitedUrlsAvailable, |
| 263 | weak_ptr_factory_.GetWeakPtr(), most_visited_request_num_)); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
Tommy C. Li | 6aee62d5 | 2019-06-27 20:25:00 | [diff] [blame] | 267 | search_terms_args.current_page_url = |
Tommy C. Li | d77691a | 2019-06-05 02:07:46 | [diff] [blame] | 268 | result_type_running_ == REMOTE_SEND_URL ? current_query_ : std::string(); |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 269 | // Create a request for suggestions, routing completion to |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 270 | // OnRemoteSuggestionsLoaderAvailable. |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 271 | client() |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 272 | ->GetRemoteSuggestionsService(/*create_if_necessary=*/true) |
| 273 | ->CreateSuggestionsRequest( |
Tommy Li | 3fe8391 | 2019-08-23 17:33:11 | [diff] [blame] | 274 | search_terms_args, client()->GetTemplateURLService(), |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 275 | base::BindOnce( |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 276 | &ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable, |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 277 | weak_ptr_factory_.GetWeakPtr()), |
| 278 | base::BindOnce( |
| 279 | &ZeroSuggestProvider::OnURLLoadComplete, |
| 280 | base::Unretained(this) /* this owns SimpleURLLoader */)); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 281 | } |
| 282 | |
mpearson | 8a37c38 | 2015-03-07 05:58:57 | [diff] [blame] | 283 | void ZeroSuggestProvider::Stop(bool clear_cached_results, |
| 284 | bool due_to_user_inactivity) { |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 285 | if (loader_) |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 286 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 287 | loader_.reset(); |
Tommy Li | 3fe8391 | 2019-08-23 17:33:11 | [diff] [blame] | 288 | |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 289 | // TODO(krb): It would allow us to remove some guards if we could also cancel |
| 290 | // the TopSites::GetMostVisitedURLs request. |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 291 | done_ = true; |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 292 | result_type_running_ = NONE; |
| 293 | ++most_visited_request_num_; |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 294 | |
| 295 | if (clear_cached_results) { |
| 296 | // We do not call Clear() on |results_| to retain |verbatim_relevance| |
| 297 | // value in the |results_| object. |verbatim_relevance| is used at the |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 298 | // beginning of the next call to Start() to determine the current url |
| 299 | // match relevance. |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 300 | results_.suggest_results.clear(); |
| 301 | results_.navigation_results.clear(); |
| 302 | current_query_.clear(); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 303 | current_title_.clear(); |
mariakhomenko | 1535e6a | 2015-03-20 07:48:45 | [diff] [blame] | 304 | most_visited_urls_.clear(); |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 308 | void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) { |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 309 | if (base::Contains(OmniboxFieldTrial::GetZeroSuggestVariants( |
| 310 | current_page_classification_), |
| 311 | kRemoteNoUrlVariant)) { |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 312 | // Remove the deleted match from the cache, so it is not shown to the user |
| 313 | // again. Since we cannot remove just one result, blow away the cache. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 314 | client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults, |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 315 | std::string()); |
| 316 | } |
| 317 | BaseSearchProvider::DeleteMatch(match); |
| 318 | } |
| 319 | |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 320 | void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 321 | BaseSearchProvider::AddProviderInfo(provider_info); |
mariakhomenko | 1535e6a | 2015-03-20 07:48:45 | [diff] [blame] | 322 | if (!results_.suggest_results.empty() || |
| 323 | !results_.navigation_results.empty() || |
| 324 | !most_visited_urls_.empty()) |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 325 | provider_info->back().set_times_returned_results_in_session(1); |
| 326 | } |
| 327 | |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 328 | void ZeroSuggestProvider::ResetSession() { |
| 329 | // The user has started editing in the omnibox, so leave |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 330 | // |field_trial_triggered_in_session| unchanged and set |
| 331 | // |field_trial_triggered| to false since zero suggest is inactive now. |
| 332 | set_field_trial_triggered(false); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 333 | } |
| 334 | |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 335 | ZeroSuggestProvider::ZeroSuggestProvider( |
| 336 | AutocompleteProviderClient* client, |
| 337 | HistoryURLProvider* history_url_provider, |
| 338 | AutocompleteProviderListener* listener) |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 339 | : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client), |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 340 | history_url_provider_(history_url_provider), |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 341 | listener_(listener), |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 342 | result_type_running_(NONE) { |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 343 | // Record whether remote zero suggest is possible for this user / profile. |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 344 | const TemplateURLService* template_url_service = |
| 345 | client->GetTemplateURLService(); |
| 346 | // Template URL service can be null in tests. |
| 347 | if (template_url_service != nullptr) { |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 348 | GURL suggest_url = RemoteSuggestionsService::EndpointUrl( |
Tommy C. Li | 6aee62d5 | 2019-06-27 20:25:00 | [diff] [blame] | 349 | TemplateURLRef::SearchTermsArgs(), template_url_service); |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 350 | // To check whether this is allowed, use an arbitrary insecure (http) URL |
| 351 | // as the URL we'd want suggestions for. The value of OTHER as the current |
| 352 | // page classification is to correspond with that URL. |
| 353 | UMA_HISTOGRAM_BOOLEAN( |
| 354 | "Omnibox.ZeroSuggest.Eligible.OnProfileOpen", |
| 355 | suggest_url.is_valid() && |
| 356 | CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url, |
| 357 | template_url_service->GetDefaultSearchProvider(), |
| 358 | metrics::OmniboxEventProto::OTHER, |
Tommy C. Li | 71171ee | 2019-06-20 17:11:47 | [diff] [blame] | 359 | template_url_service->search_terms_data(), client, |
| 360 | false)); |
Tomasz Wiszkowski | fd790c0 | 2019-08-29 17:31:29 | [diff] [blame] | 361 | |
| 362 | LogOmniboxRemoteNoUrlEligibilityOnNTP( |
| 363 | OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, true, |
| 364 | client); |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 365 | } |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | ZeroSuggestProvider::~ZeroSuggestProvider() { |
| 369 | } |
| 370 | |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 371 | const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const { |
| 372 | // Zero suggest provider should not receive keyword results. |
| 373 | DCHECK(!is_keyword); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 374 | return client()->GetTemplateURLService()->GetDefaultSearchProvider(); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const { |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 378 | // The callers of this method won't look at the AutocompleteInput's |
| 379 | // |from_omnibox_focus| member, so we can set its value to false. |
Kevin Bailey | bcc319e | 2017-10-01 21:53:02 | [diff] [blame] | 380 | AutocompleteInput input(base::string16(), current_page_classification_, |
| 381 | client()->GetSchemeClassifier()); |
| 382 | input.set_current_url(GURL(current_query_)); |
| 383 | input.set_current_title(current_title_); |
| 384 | input.set_prevent_inline_autocomplete(true); |
| 385 | input.set_allow_exact_keyword_match(false); |
| 386 | return input; |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | bool ZeroSuggestProvider::ShouldAppendExtraParams( |
| 390 | const SearchSuggestionParser::SuggestResult& result) const { |
| 391 | // We always use the default provider for search, so append the params. |
| 392 | return true; |
| 393 | } |
| 394 | |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 395 | void ZeroSuggestProvider::RecordDeletionResult(bool success) { |
| 396 | if (success) { |
| 397 | base::RecordAction( |
| 398 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success")); |
| 399 | } else { |
| 400 | base::RecordAction( |
| 401 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure")); |
| 402 | } |
| 403 | } |
| 404 | |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 405 | void ZeroSuggestProvider::OnURLLoadComplete( |
| 406 | const network::SimpleURLLoader* source, |
| 407 | std::unique_ptr<std::string> response_body) { |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 408 | DCHECK(!done_); |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 409 | DCHECK_EQ(loader_.get(), source); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 410 | |
| 411 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED); |
| 412 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 413 | const bool results_updated = |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 414 | response_body && source->NetError() == net::OK && |
| 415 | (source->ResponseInfo() && source->ResponseInfo()->headers && |
| 416 | source->ResponseInfo()->headers->response_code() == 200) && |
| 417 | UpdateResults(SearchSuggestionParser::ExtractJsonData( |
| 418 | source, std::move(response_body))); |
| 419 | loader_.reset(); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 420 | done_ = true; |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 421 | result_type_running_ = NONE; |
| 422 | ++most_visited_request_num_; |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 423 | listener_->OnProviderUpdate(results_updated); |
| 424 | } |
| 425 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 426 | bool ZeroSuggestProvider::UpdateResults(const std::string& json_data) { |
| 427 | std::unique_ptr<base::Value> data( |
| 428 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
| 429 | if (!data) |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 430 | return false; |
| 431 | |
Tommy C. Li | 467c1bcaa | 2019-06-20 20:48:05 | [diff] [blame] | 432 | // When running the REMOTE_NO_URL variant, we want to store suggestion |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 433 | // responses if non-empty. |
Tommy C. Li | d77691a | 2019-06-05 02:07:46 | [diff] [blame] | 434 | if (result_type_running_ == REMOTE_NO_URL && !json_data.empty()) { |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 435 | client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults, |
| 436 | json_data); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 437 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 438 | // If we received an empty result list, we should update the display, as it |
| 439 | // may be showing cached results that should not be shown. |
| 440 | const base::ListValue* root_list = nullptr; |
| 441 | const base::ListValue* results_list = nullptr; |
| 442 | const bool non_empty_parsed_list = data->GetAsList(&root_list) && |
| 443 | root_list->GetList(1, &results_list) && |
| 444 | !results_list->empty(); |
| 445 | const bool non_empty_cache = !results_.suggest_results.empty() || |
| 446 | !results_.navigation_results.empty(); |
| 447 | if (non_empty_parsed_list && non_empty_cache) |
| 448 | return false; |
| 449 | } |
| 450 | const bool results_updated = ParseSuggestResults( |
| 451 | *data, kDefaultZeroSuggestRelevance, false, &results_); |
| 452 | ConvertResultsToAutocompleteMatches(); |
| 453 | return results_updated; |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 454 | } |
| 455 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 456 | void ZeroSuggestProvider::AddSuggestResultsToMap( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 457 | const SearchSuggestionParser::SuggestResults& results, |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 458 | MatchMap* map) { |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 459 | for (size_t i = 0; i < results.size(); ++i) |
[email protected] | 7bc5e16 | 2014-08-15 19:41:11 | [diff] [blame] | 460 | AddMatchToMap(results[i], std::string(), i, false, false, map); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 461 | } |
| 462 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 463 | AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 464 | const SearchSuggestionParser::NavigationResult& navigation) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 465 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame] | 466 | navigation.type()); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 467 | match.destination_url = navigation.url(); |
| 468 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 469 | match.fill_into_edit += |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 470 | AutocompleteInput::FormattedStringWithEquivalentMeaning( |
Tommy C. Li | 0beb815 | 2017-08-25 18:30:26 | [diff] [blame] | 471 | navigation.url(), url_formatter::FormatUrl(navigation.url()), |
Kevin Bailey | 83e643d | 2018-03-08 16:01:41 | [diff] [blame] | 472 | client()->GetSchemeClassifier(), nullptr); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 473 | |
manuk | 3972b2f | 2019-04-19 14:22:56 | [diff] [blame] | 474 | // Zero suggest results should always omit protocols and never appear bold. |
| 475 | auto format_types = AutocompleteMatch::GetFormatTypes(false, false); |
| 476 | match.contents = url_formatter::FormatUrl(navigation.url(), format_types, |
| 477 | net::UnescapeRule::SPACES, nullptr, |
| 478 | nullptr, nullptr); |
| 479 | match.contents_class = ClassifyTermMatches({}, match.contents.length(), 0, |
| 480 | ACMatchClassification::URL); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 481 | |
| 482 | match.description = |
| 483 | AutocompleteMatch::SanitizeString(navigation.description()); |
manuk | 3972b2f | 2019-04-19 14:22:56 | [diff] [blame] | 484 | match.description_class = ClassifyTermMatches({}, match.description.length(), |
| 485 | 0, ACMatchClassification::NONE); |
| 486 | |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 487 | match.subtype_identifier = navigation.subtype_identifier(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 488 | return match; |
| 489 | } |
| 490 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 491 | void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 492 | size_t orig_request_num, |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 493 | const history::MostVisitedURLList& urls) { |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 494 | if (result_type_running_ != MOST_VISITED || |
| 495 | orig_request_num != most_visited_request_num_) { |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 496 | return; |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 497 | } |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 498 | most_visited_urls_ = urls; |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 499 | done_ = true; |
| 500 | ConvertResultsToAutocompleteMatches(); |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 501 | result_type_running_ = NONE; |
| 502 | ++most_visited_request_num_; |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 503 | listener_->OnProviderUpdate(true); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 504 | } |
| 505 | |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 506 | void ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable( |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 507 | std::unique_ptr<network::SimpleURLLoader> loader) { |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 508 | // RemoteSuggestionsService has already started |loader|, so here it's |
Maks Orlovich | 1b20851 | 2018-06-13 21:08:17 | [diff] [blame] | 509 | // only neccessary to grab its ownership until results come in to |
| 510 | // OnURLLoadComplete(). |
| 511 | loader_ = std::move(loader); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 512 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
| 513 | } |
| 514 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 515 | void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 516 | matches_.clear(); |
| 517 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 518 | TemplateURLService* template_url_service = client()->GetTemplateURLService(); |
Kevin Bailey | 9bdd15d | 2018-02-28 04:07:49 | [diff] [blame] | 519 | DCHECK(template_url_service); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 520 | const TemplateURL* default_provider = |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 521 | template_url_service->GetDefaultSearchProvider(); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 522 | // Fail if we can't set the clickthrough URL for query suggestions. |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 523 | if (default_provider == nullptr || |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 524 | !default_provider->SupportsReplacement( |
| 525 | template_url_service->search_terms_data())) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 526 | return; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 527 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 528 | MatchMap map; |
| 529 | AddSuggestResultsToMap(results_.suggest_results, &map); |
| 530 | |
| 531 | const int num_query_results = map.size(); |
| 532 | const int num_nav_results = results_.navigation_results.size(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 533 | const int num_results = num_query_results + num_nav_results; |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 534 | UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.QueryResults", num_query_results); |
| 535 | UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.URLResults", num_nav_results); |
| 536 | UMA_HISTOGRAM_COUNTS_1M("ZeroSuggest.AllResults", num_results); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 537 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 538 | // Show Most Visited results after ZeroSuggest response is received. |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 539 | if (result_type_running_ == MOST_VISITED) { |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 540 | // Ensure we don't show most visited URL suggestions on NTP. |
| 541 | // This allows us to prevent undesired side outcome of presenting |
| 542 | // URL suggestions to users who are not in the personalized field trial for |
| 543 | // zero query suggestions. |
| 544 | if (IsNTPPage(current_page_classification_) || |
| 545 | !current_url_match_.destination_url.is_valid()) { |
[email protected] | 3feb8b00 | 2013-10-14 23:50:13 | [diff] [blame] | 546 | return; |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 547 | } |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 548 | matches_.push_back(current_url_match_); |
| 549 | int relevance = 600; |
| 550 | if (num_results > 0) { |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 551 | UMA_HISTOGRAM_COUNTS_1M( |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 552 | "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual", |
| 553 | most_visited_urls_.size()); |
| 554 | } |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 555 | const base::string16 current_query_string16( |
| 556 | base::ASCIIToUTF16(current_query_)); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 557 | for (size_t i = 0; i < most_visited_urls_.size(); i++) { |
| 558 | const history::MostVisitedURL& url = most_visited_urls_[i]; |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 559 | SearchSuggestionParser::NavigationResult nav( |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 560 | client()->GetSchemeClassifier(), url.url, |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 561 | AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false, |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 562 | relevance, true, current_query_string16); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 563 | matches_.push_back(NavigationToMatch(nav)); |
| 564 | --relevance; |
| 565 | } |
| 566 | return; |
| 567 | } |
| 568 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 569 | if (num_results == 0) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 570 | return; |
| 571 | |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 572 | // Do not add the default URL match if we're on the NTP to prevent |
| 573 | // chrome-native://newtab or chrome://newtab from showing up on the list of |
| 574 | // suggestions. |
| 575 | if (!IsNTPPage(current_page_classification_) && |
| 576 | current_url_match_.destination_url.is_valid()) { |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 577 | matches_.push_back(current_url_match_); |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 578 | } |
| 579 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 580 | for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 581 | matches_.push_back(it->second); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 582 | |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 583 | const SearchSuggestionParser::NavigationResults& nav_results( |
| 584 | results_.navigation_results); |
jdoerrie | 2e6a651d | 2018-10-04 17:09:08 | [diff] [blame] | 585 | for (auto it = nav_results.begin(); it != nav_results.end(); ++it) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 586 | matches_.push_back(NavigationToMatch(*it)); |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 587 | } |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 588 | } |
| 589 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 590 | AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 591 | // The placeholder suggestion for the current URL has high relevance so |
| 592 | // that it is in the first suggestion slot and inline autocompleted. It |
| 593 | // gets dropped as soon as the user types something. |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 594 | AutocompleteInput tmp(GetInput(false)); |
| 595 | tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts()); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 596 | const base::string16 description = |
| 597 | (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl)) |
| 598 | ? current_title_ |
| 599 | : base::string16(); |
| 600 | return VerbatimMatchForURL(client(), tmp, GURL(current_query_), description, |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 601 | history_url_provider_, |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 602 | results_.verbatim_relevance); |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 603 | } |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 604 | |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 605 | bool ZeroSuggestProvider::AllowZeroSuggestSuggestions( |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 606 | const AutocompleteInput& input) const { |
| 607 | const auto& page_url = input.current_url(); |
| 608 | const auto page_class = input.current_page_classification(); |
| 609 | const auto input_type = input.type(); |
Peter Kasting | fb1a8ea | 2017-11-28 02:26:50 | [diff] [blame] | 610 | |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 611 | if (!input.from_omnibox_focus()) |
| 612 | return false; |
| 613 | |
Peter Kasting | fb1a8ea | 2017-11-28 02:26:50 | [diff] [blame] | 614 | if (client()->IsOffTheRecord()) |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 615 | return false; |
| 616 | |
Tomasz Wiszkowski | a8780ff9 | 2019-07-25 21:46:05 | [diff] [blame] | 617 | // When the omnibox is empty, only allow zero suggest for the ChromeOS |
| 618 | // Launcher and NTP. |
Yue Ru Sun | e31e157 | 2019-07-19 17:09:57 | [diff] [blame] | 619 | if (input_type == metrics::OmniboxInputType::EMPTY && |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 620 | !(page_class == metrics::OmniboxEventProto::CHROMEOS_APP_LIST || |
Tomasz Wiszkowski | a8780ff9 | 2019-07-25 21:46:05 | [diff] [blame] | 621 | IsNTPPage(page_class))) { |
[email protected] | 5b899dc | 2018-10-02 09:20:01 | [diff] [blame] | 622 | return false; |
| 623 | } |
| 624 | |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 625 | // When omnibox contains pre-populated content, only show zero suggest for |
| 626 | // pages with URLs the user will recognize. |
| 627 | // |
Mark Pearson | 3fb0e316 | 2018-08-27 21:53:57 | [diff] [blame] | 628 | // This list intentionally does not include items such as ftp: and file: |
Tommy C. Li | 06ec26a | 2019-06-10 18:01:42 | [diff] [blame] | 629 | // because (a) these do not work on Android and iOS, where most visited |
Mark Pearson | 3fb0e316 | 2018-08-27 21:53:57 | [diff] [blame] | 630 | // zero suggest is launched and (b) on desktop, where contextual zero suggest |
| 631 | // is running, these types of schemes aren't eligible to be sent to the |
| 632 | // server to ask for suggestions (and thus in practice we won't display zero |
| 633 | // suggest for them). |
Yue Ru Sun | e31e157 | 2019-07-19 17:09:57 | [diff] [blame] | 634 | if (input_type != metrics::OmniboxInputType::EMPTY && |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 635 | !(page_url.is_valid() && |
| 636 | ((page_url.scheme() == url::kHttpScheme) || |
| 637 | (page_url.scheme() == url::kHttpsScheme) || |
| 638 | (page_url.scheme() == url::kAboutScheme) || |
| 639 | (page_url.scheme() == |
| 640 | client()->GetEmbedderRepresentationOfAboutScheme())))) { |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 641 | return false; |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 642 | } |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 643 | |
| 644 | return true; |
| 645 | } |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 646 | |
| 647 | void ZeroSuggestProvider::MaybeUseCachedSuggestions() { |
Tommy C. Li | d77691a | 2019-06-05 02:07:46 | [diff] [blame] | 648 | if (result_type_running_ != REMOTE_NO_URL) |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 649 | return; |
| 650 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 651 | std::string json_data = |
| 652 | client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 653 | if (!json_data.empty()) { |
dcheng | 259570c | 2016-04-22 00:45:57 | [diff] [blame] | 654 | std::unique_ptr<base::Value> data( |
[email protected] | 2c802d1 | 2014-07-31 12:57:14 | [diff] [blame] | 655 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 656 | if (data && ParseSuggestResults(*data, kDefaultZeroSuggestRelevance, false, |
| 657 | &results_)) |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 658 | ConvertResultsToAutocompleteMatches(); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 659 | } |
| 660 | } |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 661 | |
| 662 | ZeroSuggestProvider::ResultType ZeroSuggestProvider::TypeOfResultToRun( |
| 663 | const GURL& current_url, |
| 664 | const GURL& suggest_url) { |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 665 | // Check if the URL can be sent in any suggest request. |
| 666 | const TemplateURLService* template_url_service = |
| 667 | client()->GetTemplateURLService(); |
Kevin Bailey | 9bdd15d | 2018-02-28 04:07:49 | [diff] [blame] | 668 | DCHECK(template_url_service); |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 669 | const TemplateURL* default_provider = |
| 670 | template_url_service->GetDefaultSearchProvider(); |
| 671 | const bool can_send_current_url = CanSendURL( |
| 672 | current_url, suggest_url, default_provider, current_page_classification_, |
Tommy C. Li | 71171ee | 2019-06-20 17:11:47 | [diff] [blame] | 673 | template_url_service->search_terms_data(), client(), false); |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 674 | // Collect metrics on eligibility. |
| 675 | GURL arbitrary_insecure_url(kArbitraryInsecureUrlString); |
| 676 | ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE; |
| 677 | if (!can_send_current_url) { |
| 678 | const bool can_send_ordinary_url = |
| 679 | CanSendURL(arbitrary_insecure_url, suggest_url, default_provider, |
| 680 | current_page_classification_, |
Tommy C. Li | 71171ee | 2019-06-20 17:11:47 | [diff] [blame] | 681 | template_url_service->search_terms_data(), client(), false); |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 682 | eligibility = can_send_ordinary_url |
| 683 | ? ZeroSuggestEligibility::URL_INELIGIBLE |
| 684 | : ZeroSuggestEligibility::GENERALLY_INELIGIBLE; |
| 685 | } |
| 686 | UMA_HISTOGRAM_ENUMERATION( |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 687 | kOmniboxZeroSuggestEligibleHistogramName, static_cast<int>(eligibility), |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 688 | static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE)); |
| 689 | |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 690 | const auto field_trial_variants = |
| 691 | OmniboxFieldTrial::GetZeroSuggestVariants(current_page_classification_); |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 692 | |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 693 | if (base::Contains(field_trial_variants, kNoneVariant) || |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 694 | base::FeatureList::IsEnabled( |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 695 | omnibox::kOmniboxPopupShortcutIconsInZeroState)) { |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 696 | return NONE; |
Tomasz Wiszkowski | 57492ab | 2019-05-29 21:28:27 | [diff] [blame] | 697 | } |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 698 | |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 699 | // TODO(tommycli): Since this can be configured via ZeroSuggestVariant, we |
| 700 | // should eliminate this special case and use a field trial configuration. |
| 701 | if (current_page_classification_ == OmniboxEventProto::CHROMEOS_APP_LIST) |
Tommy C. Li | d77691a | 2019-06-05 02:07:46 | [diff] [blame] | 702 | return REMOTE_NO_URL; |
Jenny Zhang | 5bc2e3d | 2018-09-10 18:50:55 | [diff] [blame] | 703 | |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 704 | if (base::Contains(field_trial_variants, kRemoteNoUrlVariant)) { |
Moe Ahmadi | 04206cd | 2019-08-23 18:03:22 | [diff] [blame] | 705 | #if defined(OS_ANDROID) || defined(OS_IOS) |
Tommy C. Li | 467c1bcaa | 2019-06-20 20:48:05 | [diff] [blame] | 706 | // TODO(tommycli): It's odd that this doesn't apply to kRemoteSendUrlVariant |
| 707 | // as well. Most likely this fallback concept should be replaced by |
| 708 | // a more general configuration setup. |
Moe Ahmadi | 04206cd | 2019-08-23 18:03:22 | [diff] [blame] | 709 | if (RemoteSuggestionsShouldFallBackToMostVisited(client(), |
| 710 | template_url_service)) { |
| 711 | return MOST_VISITED; |
| 712 | } |
| 713 | #endif |
| 714 | return REMOTE_NO_URL; |
Tommy C. Li | 393c1b14 | 2019-05-22 00:28:41 | [diff] [blame] | 715 | } |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 716 | |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 717 | if (base::Contains(field_trial_variants, kRemoteSendUrlVariant) && |
| 718 | can_send_current_url) |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 719 | return REMOTE_SEND_URL; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 720 | |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 721 | if (base::Contains(field_trial_variants, kMostVisitedVariant)) |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 722 | return MOST_VISITED; |
| 723 | |
| 724 | #if defined(OS_ANDROID) || defined(OS_IOS) |
| 725 | // For Android and iOS, default to MOST_VISITED so long as: |
| 726 | // - There is no configured variant for |page_classification| AND |
| 727 | // - The user is not on the search results page of the default search |
| 728 | // provider. |
Moe Ahmadi | 0993aec | 2019-09-19 21:42:30 | [diff] [blame] | 729 | if (field_trial_variants.empty() && |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 730 | current_page_classification_ != |
| 731 | OmniboxEventProto::SEARCH_RESULT_PAGE_NO_SEARCH_TERM_REPLACEMENT && |
| 732 | current_page_classification_ != |
| 733 | OmniboxEventProto::SEARCH_RESULT_PAGE_DOING_SEARCH_TERM_REPLACEMENT) { |
Kevin Bailey | 5fea432 | 2018-03-21 22:36:05 | [diff] [blame] | 734 | return MOST_VISITED; |
Tommy C. Li | 393c1b14 | 2019-05-22 00:28:41 | [diff] [blame] | 735 | } |
Tommy C. Li | 0af0956 | 2019-06-11 23:58:51 | [diff] [blame] | 736 | #endif |
Tommy C. Li | c79d39ab | 2019-06-04 16:58:56 | [diff] [blame] | 737 | |
| 738 | return NONE; |
Gheorghe Comanici | 9a364f57 | 2018-01-24 17:22:00 | [diff] [blame] | 739 | } |