[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 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 9 | #include "base/callback.h" |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 10 | #include "base/feature_list.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 11 | #include "base/i18n/case_conversion.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 12 | #include "base/json/json_string_value_serializer.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 13 | #include "base/metrics/histogram_macros.h" |
[email protected] | f7f41c0e | 2014-08-11 04:22:23 | [diff] [blame] | 14 | #include "base/metrics/user_metrics.h" |
[email protected] | 98570e1 | 2013-06-10 19:54:22 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
| 16 | #include "base/strings/string_util.h" |
[email protected] | 135cb80 | 2013-06-09 16:44:20 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 4dcb797 | 2013-06-28 15:15:41 | [diff] [blame] | 18 | #include "base/time/time.h" |
a-v-y | dd768d5 | 2016-03-25 21:07:46 | [diff] [blame] | 19 | #include "base/trace_event/trace_event.h" |
amohammadkhan | f76ae11 | 2015-09-14 17:34:43 | [diff] [blame] | 20 | #include "components/data_use_measurement/core/data_use_user_data.h" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 21 | #include "components/history/core/browser/history_types.h" |
sdefresne | 0da3bc0 | 2015-01-29 18:26:35 | [diff] [blame] | 22 | #include "components/history/core/browser/top_sites.h" |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 23 | #include "components/metrics/proto/omnibox_event.pb.h" |
[email protected] | 3dc75b1 | 2014-06-08 00:02:22 | [diff] [blame] | 24 | #include "components/metrics/proto/omnibox_input_type.pb.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 25 | #include "components/omnibox/browser/autocomplete_classifier.h" |
| 26 | #include "components/omnibox/browser/autocomplete_input.h" |
| 27 | #include "components/omnibox/browser/autocomplete_match.h" |
| 28 | #include "components/omnibox/browser/autocomplete_provider_listener.h" |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 29 | #include "components/omnibox/browser/contextual_suggestions_service.h" |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 30 | #include "components/omnibox/browser/history_url_provider.h" |
| 31 | #include "components/omnibox/browser/omnibox_field_trial.h" |
| 32 | #include "components/omnibox/browser/omnibox_pref_names.h" |
| 33 | #include "components/omnibox/browser/search_provider.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 34 | #include "components/omnibox/browser/verbatim_match.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame] | 35 | #include "components/pref_registry/pref_registry_syncable.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 36 | #include "components/prefs/pref_service.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 37 | #include "components/search_engines/template_url_service.h" |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 38 | #include "components/url_formatter/url_formatter.h" |
asvitkine | 9a27983 | 2015-12-18 02:35:50 | [diff] [blame] | 39 | #include "components/variations/net/variations_http_headers.h" |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 40 | #include "net/base/escape.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 41 | #include "net/url_request/url_fetcher.h" |
| 42 | #include "net/url_request/url_request_status.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 43 | #include "url/gurl.h" |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 44 | |
| 45 | namespace { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 46 | |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 47 | // Represents whether ZeroSuggestProvider is allowed to display contextual |
| 48 | // suggestions on focus, and if not, why not. |
| 49 | // These values are written to logs. New enum values can be added, but existing |
| 50 | // enums must never be renumbered or deleted and reused. |
| 51 | enum class ZeroSuggestEligibility { |
| 52 | ELIGIBLE = 0, |
| 53 | // URL_INELIGIBLE would be ELIGIBLE except some property of the current URL |
| 54 | // itself prevents ZeroSuggest from triggering. |
| 55 | URL_INELIGIBLE = 1, |
| 56 | GENERALLY_INELIGIBLE = 2, |
| 57 | ELIGIBLE_MAX_VALUE |
| 58 | }; |
| 59 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 60 | // TODO(hfung): The histogram code was copied and modified from |
| 61 | // search_provider.cc. Refactor and consolidate the code. |
| 62 | // We keep track in a histogram how many suggest requests we send, how |
| 63 | // many suggest requests we invalidate (e.g., due to a user typing |
| 64 | // another character), and how many replies we receive. |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 65 | // These values are written to logs. New enum values can be added, but existing |
| 66 | // enums must never be renumbered or deleted and reused. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 67 | enum ZeroSuggestRequestsHistogramValue { |
| 68 | ZERO_SUGGEST_REQUEST_SENT = 1, |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 69 | ZERO_SUGGEST_REQUEST_INVALIDATED = 2, |
| 70 | ZERO_SUGGEST_REPLY_RECEIVED = 3, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 71 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE |
| 72 | }; |
| 73 | |
| 74 | void LogOmniboxZeroSuggestRequest( |
| 75 | ZeroSuggestRequestsHistogramValue request_value) { |
| 76 | UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value, |
| 77 | ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE); |
| 78 | } |
| 79 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 80 | // Relevance value to use if it was not set explicitly by the server. |
| 81 | const int kDefaultZeroSuggestRelevance = 100; |
| 82 | |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 83 | // Used for testing whether zero suggest is ever available. |
mpearson | c90c24b | 2017-03-04 00:11:26 | [diff] [blame] | 84 | constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/"; |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 85 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 86 | } // namespace |
| 87 | |
[email protected] | a00008d4 | 2012-09-15 05:07:58 | [diff] [blame] | 88 | // static |
| 89 | ZeroSuggestProvider* ZeroSuggestProvider::Create( |
blundell | 55e35e8 | 2015-06-16 08:46:18 | [diff] [blame] | 90 | AutocompleteProviderClient* client, |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 91 | HistoryURLProvider* history_url_provider, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 92 | AutocompleteProviderListener* listener) { |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 93 | return new ZeroSuggestProvider(client, history_url_provider, listener); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 96 | // static |
| 97 | void ZeroSuggestProvider::RegisterProfilePrefs( |
| 98 | user_prefs::PrefRegistrySyncable* registry) { |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 99 | registry->RegisterStringPref(omnibox::kZeroSuggestCachedResults, |
| 100 | std::string()); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 103 | void ZeroSuggestProvider::Start(const AutocompleteInput& input, |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 104 | bool minimal_changes) { |
a-v-y | dd768d5 | 2016-03-25 21:07:46 | [diff] [blame] | 105 | TRACE_EVENT0("omnibox", "ZeroSuggestProvider::Start"); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 106 | matches_.clear(); |
mpearson | 6eaf7fc | 2017-04-11 04:09:57 | [diff] [blame] | 107 | if (!input.from_omnibox_focus() || client()->IsOffTheRecord() || |
mariakhomenko | 3ef531d7 | 2015-01-10 00:03:43 | [diff] [blame] | 108 | input.type() == metrics::OmniboxInputType::INVALID) |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 109 | return; |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 110 | |
mpearson | 8a37c38 | 2015-03-07 05:58:57 | [diff] [blame] | 111 | Stop(true, false); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 112 | set_field_trial_triggered(false); |
| 113 | set_field_trial_triggered_in_session(false); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 114 | results_from_cache_ = false; |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 115 | permanent_text_ = input.text(); |
| 116 | current_query_ = input.current_url().spec(); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 117 | current_title_ = input.current_title(); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 118 | current_page_classification_ = input.current_page_classification(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 119 | current_url_match_ = MatchForCurrentURL(); |
[email protected] | 9b9fa67 | 2013-11-07 06:04:52 | [diff] [blame] | 120 | |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 121 | GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl( |
| 122 | /*current_url=*/"", client()->GetTemplateURLService()); |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 123 | if (!suggest_url.is_valid()) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 124 | return; |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 125 | |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 126 | // No need to send the current page URL in personalized suggest or |
| 127 | // most visited field trials. |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 128 | const TemplateURLService* template_url_service = |
| 129 | client()->GetTemplateURLService(); |
| 130 | const TemplateURL* default_provider = |
| 131 | template_url_service->GetDefaultSearchProvider(); |
| 132 | const bool can_send_current_url = |
| 133 | CanSendURL(input.current_url(), suggest_url, default_provider, |
[email protected] | e6477f1 | 2014-08-05 07:59:54 | [diff] [blame] | 134 | current_page_classification_, |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 135 | template_url_service->search_terms_data(), client()); |
| 136 | GURL arbitrary_insecure_url(kArbitraryInsecureUrlString); |
| 137 | ZeroSuggestEligibility eligibility = ZeroSuggestEligibility::ELIGIBLE; |
| 138 | if (!can_send_current_url) { |
| 139 | const bool can_send_ordinary_url = |
| 140 | CanSendURL(arbitrary_insecure_url, suggest_url, default_provider, |
| 141 | current_page_classification_, |
| 142 | template_url_service->search_terms_data(), client()); |
| 143 | eligibility = can_send_ordinary_url |
| 144 | ? ZeroSuggestEligibility::URL_INELIGIBLE |
| 145 | : ZeroSuggestEligibility::GENERALLY_INELIGIBLE; |
| 146 | } |
| 147 | UMA_HISTOGRAM_ENUMERATION( |
| 148 | "Omnibox.ZeroSuggest.Eligible.OnFocus", static_cast<int>(eligibility), |
| 149 | static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE)); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 150 | |
| 151 | bool can_attach_current_url = |
| 152 | can_send_current_url && |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 153 | !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() && |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 154 | !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial(); |
| 155 | |
| 156 | if (!can_attach_current_url && |
| 157 | !ShouldShowNonContextualZeroSuggest(input.current_url())) { |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 161 | done_ = false; |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 162 | |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 163 | // TODO(jered): Consider adding locally-sourced zero-suggestions here too. |
| 164 | // These may be useful on the NTP or more relevant to the user than server |
| 165 | // suggestions, if based on local browsing history. |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 166 | MaybeUseCachedSuggestions(); |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 167 | |
| 168 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
| 169 | most_visited_urls_.clear(); |
| 170 | scoped_refptr<history::TopSites> ts = client()->GetTopSites(); |
| 171 | if (ts) { |
| 172 | waiting_for_most_visited_urls_request_ = true; |
| 173 | ts->GetMostVisitedURLs( |
| 174 | base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, |
| 175 | weak_ptr_factory_.GetWeakPtr()), |
| 176 | false); |
| 177 | } |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | // Create a request for suggestions with |this| as the fetcher delegate. |
| 182 | client() |
Gheorghe Comanici | 86bbdf6 | 2017-08-28 17:20:33 | [diff] [blame] | 183 | ->GetContextualSuggestionsService(/*create_if_necessary=*/true) |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 184 | ->CreateContextualSuggestionsRequest( |
| 185 | can_attach_current_url ? current_query_ : std::string(), |
| 186 | client()->GetTemplateURLService(), |
| 187 | /*fetcher_delegate=*/this, |
| 188 | base::BindOnce( |
| 189 | &ZeroSuggestProvider::OnContextualSuggestionsFetcherAvailable, |
| 190 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 191 | } |
| 192 | |
mpearson | 8a37c38 | 2015-03-07 05:58:57 | [diff] [blame] | 193 | void ZeroSuggestProvider::Stop(bool clear_cached_results, |
| 194 | bool due_to_user_inactivity) { |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 195 | if (fetcher_) |
| 196 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); |
| 197 | fetcher_.reset(); |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 198 | waiting_for_most_visited_urls_request_ = false; |
Gheorghe Comanici | 86bbdf6 | 2017-08-28 17:20:33 | [diff] [blame] | 199 | auto* contextual_suggestions_service = |
| 200 | client()->GetContextualSuggestionsService(/*create_if_necessary=*/false); |
| 201 | // contextual_suggestions_service can be null if in incognito mode. |
| 202 | if (contextual_suggestions_service != nullptr) { |
| 203 | contextual_suggestions_service->StopCreatingContextualSuggestionsRequest(); |
| 204 | } |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 205 | done_ = true; |
| 206 | |
| 207 | if (clear_cached_results) { |
| 208 | // We do not call Clear() on |results_| to retain |verbatim_relevance| |
| 209 | // value in the |results_| object. |verbatim_relevance| is used at the |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 210 | // beginning of the next call to Start() to determine the current url |
| 211 | // match relevance. |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 212 | results_.suggest_results.clear(); |
| 213 | results_.navigation_results.clear(); |
| 214 | current_query_.clear(); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 215 | current_title_.clear(); |
mariakhomenko | 1535e6a | 2015-03-20 07:48:45 | [diff] [blame] | 216 | most_visited_urls_.clear(); |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 220 | void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) { |
| 221 | if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { |
| 222 | // Remove the deleted match from the cache, so it is not shown to the user |
| 223 | // again. Since we cannot remove just one result, blow away the cache. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 224 | client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults, |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 225 | std::string()); |
| 226 | } |
| 227 | BaseSearchProvider::DeleteMatch(match); |
| 228 | } |
| 229 | |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 230 | void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 231 | BaseSearchProvider::AddProviderInfo(provider_info); |
mariakhomenko | 1535e6a | 2015-03-20 07:48:45 | [diff] [blame] | 232 | if (!results_.suggest_results.empty() || |
| 233 | !results_.navigation_results.empty() || |
| 234 | !most_visited_urls_.empty()) |
[email protected] | ec3f679b | 2014-08-18 07:45:13 | [diff] [blame] | 235 | provider_info->back().set_times_returned_results_in_session(1); |
| 236 | } |
| 237 | |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 238 | void ZeroSuggestProvider::ResetSession() { |
| 239 | // The user has started editing in the omnibox, so leave |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 240 | // |field_trial_triggered_in_session| unchanged and set |
| 241 | // |field_trial_triggered| to false since zero suggest is inactive now. |
| 242 | set_field_trial_triggered(false); |
[email protected] | f030c4d | 2014-03-25 01:05:54 | [diff] [blame] | 243 | } |
| 244 | |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 245 | ZeroSuggestProvider::ZeroSuggestProvider( |
| 246 | AutocompleteProviderClient* client, |
| 247 | HistoryURLProvider* history_url_provider, |
| 248 | AutocompleteProviderListener* listener) |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 249 | : BaseSearchProvider(AutocompleteProvider::TYPE_ZERO_SUGGEST, client), |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 250 | history_url_provider_(history_url_provider), |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 251 | listener_(listener), |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 252 | results_from_cache_(false), |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 253 | waiting_for_most_visited_urls_request_(false), |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 254 | weak_ptr_factory_(this) { |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 255 | // Record whether contextual zero suggest is possible for this user / profile. |
| 256 | const TemplateURLService* template_url_service = |
| 257 | client->GetTemplateURLService(); |
| 258 | // Template URL service can be null in tests. |
| 259 | if (template_url_service != nullptr) { |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 260 | GURL suggest_url = ContextualSuggestionsService::ContextualSuggestionsUrl( |
| 261 | /*current_url=*/"", template_url_service); |
mpearson | 3d89cdc | 2017-03-03 21:15:45 | [diff] [blame] | 262 | // To check whether this is allowed, use an arbitrary insecure (http) URL |
| 263 | // as the URL we'd want suggestions for. The value of OTHER as the current |
| 264 | // page classification is to correspond with that URL. |
| 265 | UMA_HISTOGRAM_BOOLEAN( |
| 266 | "Omnibox.ZeroSuggest.Eligible.OnProfileOpen", |
| 267 | suggest_url.is_valid() && |
| 268 | CanSendURL(GURL(kArbitraryInsecureUrlString), suggest_url, |
| 269 | template_url_service->GetDefaultSearchProvider(), |
| 270 | metrics::OmniboxEventProto::OTHER, |
| 271 | template_url_service->search_terms_data(), client)); |
| 272 | } |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | ZeroSuggestProvider::~ZeroSuggestProvider() { |
| 276 | } |
| 277 | |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 278 | const TemplateURL* ZeroSuggestProvider::GetTemplateURL(bool is_keyword) const { |
| 279 | // Zero suggest provider should not receive keyword results. |
| 280 | DCHECK(!is_keyword); |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 281 | return client()->GetTemplateURLService()->GetDefaultSearchProvider(); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | const AutocompleteInput ZeroSuggestProvider::GetInput(bool is_keyword) const { |
jif | cf322cd | 2015-06-17 11:01:18 | [diff] [blame] | 285 | // The callers of this method won't look at the AutocompleteInput's |
| 286 | // |from_omnibox_focus| member, so we can set its value to false. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 287 | return AutocompleteInput(base::string16(), base::string16::npos, |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 288 | std::string(), GURL(current_query_), current_title_, |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 289 | current_page_classification_, true, false, false, |
| 290 | true, false, client()->GetSchemeClassifier()); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool ZeroSuggestProvider::ShouldAppendExtraParams( |
| 294 | const SearchSuggestionParser::SuggestResult& result) const { |
| 295 | // We always use the default provider for search, so append the params. |
| 296 | return true; |
| 297 | } |
| 298 | |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 299 | void ZeroSuggestProvider::RecordDeletionResult(bool success) { |
| 300 | if (success) { |
| 301 | base::RecordAction( |
| 302 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Success")); |
| 303 | } else { |
| 304 | base::RecordAction( |
| 305 | base::UserMetricsAction("Omnibox.ZeroSuggestDelete.Failure")); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void ZeroSuggestProvider::OnURLFetchComplete(const net::URLFetcher* source) { |
| 310 | DCHECK(!done_); |
| 311 | DCHECK_EQ(fetcher_.get(), source); |
| 312 | |
| 313 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REPLY_RECEIVED); |
| 314 | |
| 315 | bool results_updated = false; |
| 316 | if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { |
| 317 | std::string json_data = SearchSuggestionParser::ExtractJsonData(source); |
dcheng | 259570c | 2016-04-22 00:45:57 | [diff] [blame] | 318 | std::unique_ptr<base::Value> data( |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 319 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
| 320 | if (data) { |
| 321 | if (StoreSuggestionResponse(json_data, *data)) |
| 322 | return; |
| 323 | results_updated = ParseSuggestResults( |
| 324 | *data, kDefaultZeroSuggestRelevance, false, &results_); |
| 325 | } |
| 326 | } |
| 327 | fetcher_.reset(); |
| 328 | done_ = true; |
| 329 | ConvertResultsToAutocompleteMatches(); |
| 330 | listener_->OnProviderUpdate(results_updated); |
| 331 | } |
| 332 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 333 | bool ZeroSuggestProvider::StoreSuggestionResponse( |
| 334 | const std::string& json_data, |
| 335 | const base::Value& parsed_data) { |
| 336 | if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() || |
| 337 | json_data.empty()) |
| 338 | return false; |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 339 | client()->GetPrefs()->SetString(omnibox::kZeroSuggestCachedResults, |
| 340 | json_data); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 341 | |
| 342 | // If we received an empty result list, we should update the display, as it |
| 343 | // may be showing cached results that should not be shown. |
| 344 | const base::ListValue* root_list = NULL; |
| 345 | const base::ListValue* results_list = NULL; |
| 346 | if (parsed_data.GetAsList(&root_list) && |
| 347 | root_list->GetList(1, &results_list) && |
| 348 | results_list->empty()) |
| 349 | return false; |
| 350 | |
| 351 | // We are finished with the request and want to bail early. |
| 352 | if (results_from_cache_) |
| 353 | done_ = true; |
| 354 | |
| 355 | return results_from_cache_; |
| 356 | } |
| 357 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 358 | void ZeroSuggestProvider::AddSuggestResultsToMap( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 359 | const SearchSuggestionParser::SuggestResults& results, |
[email protected] | 0234620 | 2014-02-05 05:18:30 | [diff] [blame] | 360 | MatchMap* map) { |
[email protected] | d4a94b9 | 2014-03-04 01:35:22 | [diff] [blame] | 361 | for (size_t i = 0; i < results.size(); ++i) |
[email protected] | 7bc5e16 | 2014-08-15 19:41:11 | [diff] [blame] | 362 | AddMatchToMap(results[i], std::string(), i, false, false, map); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 365 | AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 366 | const SearchSuggestionParser::NavigationResult& navigation) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 367 | AutocompleteMatch match(this, navigation.relevance(), false, |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame] | 368 | navigation.type()); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 369 | match.destination_url = navigation.url(); |
| 370 | |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 371 | // Zero suggest results should always omit protocols and never appear bold. |
Tommy C. Li | a46e638 | 2017-08-01 23:26:27 | [diff] [blame] | 372 | auto format_types = AutocompleteMatch::GetFormatTypes(false, false, false); |
tommycli | 72014f6 | 2017-06-29 21:42:16 | [diff] [blame] | 373 | match.contents = url_formatter::FormatUrl(navigation.url(), format_types, |
| 374 | net::UnescapeRule::SPACES, nullptr, |
| 375 | nullptr, nullptr); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 376 | match.fill_into_edit += |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 377 | AutocompleteInput::FormattedStringWithEquivalentMeaning( |
Tommy C. Li | 0beb815 | 2017-08-25 18:30:26 | [diff] [blame] | 378 | navigation.url(), url_formatter::FormatUrl(navigation.url()), |
| 379 | client()->GetSchemeClassifier()); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 380 | |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 381 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 382 | match.contents.length(), ACMatchClassification::URL, |
| 383 | &match.contents_class); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 384 | |
| 385 | match.description = |
| 386 | AutocompleteMatch::SanitizeString(navigation.description()); |
[email protected] | b959d7d4 | 2013-12-13 17:26:37 | [diff] [blame] | 387 | AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 388 | match.description.length(), ACMatchClassification::NONE, |
| 389 | &match.description_class); |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 390 | match.subtype_identifier = navigation.subtype_identifier(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 391 | return match; |
| 392 | } |
| 393 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 394 | void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
| 395 | const history::MostVisitedURLList& urls) { |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 396 | if (!waiting_for_most_visited_urls_request_) return; |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 397 | most_visited_urls_ = urls; |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 398 | waiting_for_most_visited_urls_request_ = false; |
| 399 | done_ = true; |
| 400 | ConvertResultsToAutocompleteMatches(); |
| 401 | listener_->OnProviderUpdate(true); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 402 | } |
| 403 | |
Daniel Kenji Toyama | f1e4b57 | 2017-08-03 16:31:11 | [diff] [blame] | 404 | void ZeroSuggestProvider::OnContextualSuggestionsFetcherAvailable( |
| 405 | std::unique_ptr<net::URLFetcher> fetcher) { |
| 406 | fetcher_ = std::move(fetcher); |
| 407 | fetcher_->Start(); |
| 408 | LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
| 409 | } |
| 410 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 411 | void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 412 | matches_.clear(); |
| 413 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 414 | TemplateURLService* template_url_service = client()->GetTemplateURLService(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 415 | const TemplateURL* default_provider = |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 416 | template_url_service->GetDefaultSearchProvider(); |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 417 | // Fail if we can't set the clickthrough URL for query suggestions. |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 418 | if (default_provider == NULL || |
| 419 | !default_provider->SupportsReplacement( |
| 420 | template_url_service->search_terms_data())) |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 421 | return; |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 422 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 423 | MatchMap map; |
| 424 | AddSuggestResultsToMap(results_.suggest_results, &map); |
| 425 | |
| 426 | const int num_query_results = map.size(); |
| 427 | const int num_nav_results = results_.navigation_results.size(); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 428 | const int num_results = num_query_results + num_nav_results; |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 429 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.QueryResults", num_query_results); |
[email protected] | 78981d8c | 2014-05-09 15:05:47 | [diff] [blame] | 430 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.URLResults", num_nav_results); |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 431 | UMA_HISTOGRAM_COUNTS("ZeroSuggest.AllResults", num_results); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 432 | |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 433 | // Show Most Visited results after ZeroSuggest response is received. |
| 434 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
[email protected] | 3feb8b00 | 2013-10-14 23:50:13 | [diff] [blame] | 435 | if (!current_url_match_.destination_url.is_valid()) |
| 436 | return; |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 437 | matches_.push_back(current_url_match_); |
| 438 | int relevance = 600; |
| 439 | if (num_results > 0) { |
| 440 | UMA_HISTOGRAM_COUNTS( |
| 441 | "Omnibox.ZeroSuggest.MostVisitedResultsCounterfactual", |
| 442 | most_visited_urls_.size()); |
| 443 | } |
[email protected] | 23db649 | 2014-01-16 02:35:30 | [diff] [blame] | 444 | const base::string16 current_query_string16( |
| 445 | base::ASCIIToUTF16(current_query_)); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 446 | for (size_t i = 0; i < most_visited_urls_.size(); i++) { |
| 447 | const history::MostVisitedURL& url = most_visited_urls_[i]; |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 448 | SearchSuggestionParser::NavigationResult nav( |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 449 | client()->GetSchemeClassifier(), url.url, |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 450 | AutocompleteMatchType::NAVSUGGEST, 0, url.title, std::string(), false, |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 451 | relevance, true, current_query_string16); |
[email protected] | 8f064e5 | 2013-09-18 01:17:14 | [diff] [blame] | 452 | matches_.push_back(NavigationToMatch(nav)); |
| 453 | --relevance; |
| 454 | } |
| 455 | return; |
| 456 | } |
| 457 | |
[email protected] | 9c97f89c | 2013-06-25 03:12:16 | [diff] [blame] | 458 | if (num_results == 0) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 459 | return; |
| 460 | |
| 461 | // TODO(jered): Rip this out once the first match is decoupled from the |
| 462 | // current typing in the omnibox. |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 463 | matches_.push_back(current_url_match_); |
| 464 | |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 465 | for (MatchMap::const_iterator it(map.begin()); it != map.end(); ++it) |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 466 | matches_.push_back(it->second); |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 467 | |
[email protected] | 0b9575f | 2014-07-30 11:58:37 | [diff] [blame] | 468 | const SearchSuggestionParser::NavigationResults& nav_results( |
| 469 | results_.navigation_results); |
| 470 | for (SearchSuggestionParser::NavigationResults::const_iterator it( |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 471 | nav_results.begin()); |
| 472 | it != nav_results.end(); ++it) { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 473 | matches_.push_back(NavigationToMatch(*it)); |
gcomanici | 67d53ac | 2017-04-01 17:07:19 | [diff] [blame] | 474 | } |
[email protected] | 6ce7f61 | 2012-09-05 23:53:07 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 477 | AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { |
[email protected] | bb1fb2b | 2013-05-31 00:21:01 | [diff] [blame] | 478 | // The placeholder suggestion for the current URL has high relevance so |
| 479 | // that it is in the first suggestion slot and inline autocompleted. It |
| 480 | // gets dropped as soon as the user types something. |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 481 | AutocompleteInput tmp(GetInput(false)); |
| 482 | tmp.UpdateText(permanent_text_, base::string16::npos, tmp.parts()); |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 483 | const base::string16 description = |
| 484 | (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl)) |
| 485 | ? current_title_ |
| 486 | : base::string16(); |
| 487 | return VerbatimMatchForURL(client(), tmp, GURL(current_query_), description, |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 488 | history_url_provider_, |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 489 | results_.verbatim_relevance); |
[email protected] | 0040474 | 2014-02-20 13:09:05 | [diff] [blame] | 490 | } |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 491 | |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 492 | bool ZeroSuggestProvider::ShouldShowNonContextualZeroSuggest( |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 493 | const GURL& current_page_url) const { |
gcomanici | de29a436 | 2017-06-02 15:44:08 | [diff] [blame] | 494 | if (!ZeroSuggestEnabled(current_page_classification_, client())) |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 495 | return false; |
| 496 | |
| 497 | // If we cannot send URLs, then only the MostVisited and Personalized |
| 498 | // variations can be shown. |
| 499 | if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() && |
| 500 | !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 501 | return false; |
| 502 | |
| 503 | // Only show zero suggest for HTTP[S] pages. |
| 504 | // TODO(mariakhomenko): We may be able to expand this set to include pages |
| 505 | // with other schemes (e.g. chrome://). That may require improvements to |
| 506 | // the formatting of the verbatim result returned by MatchForCurrentURL(). |
| 507 | if (!current_page_url.is_valid() || |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 508 | ((current_page_url.scheme() != url::kHttpScheme) && |
| 509 | (current_page_url.scheme() != url::kHttpsScheme))) |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 510 | return false; |
| 511 | |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 512 | if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() && |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 513 | client() |
| 514 | ->GetTemplateURLService() |
| 515 | ->IsSearchResultsPageFromDefaultSearchProvider(current_page_url)) |
mariakhomenko | bfc3a2a | 2014-10-24 00:48:22 | [diff] [blame] | 516 | return false; |
| 517 | |
[email protected] | 162c8d9fa | 2014-03-18 20:25:41 | [diff] [blame] | 518 | return true; |
| 519 | } |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 520 | |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 521 | void ZeroSuggestProvider::MaybeUseCachedSuggestions() { |
| 522 | if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 523 | return; |
| 524 | |
blundell | d130d59 | 2015-06-21 19:29:13 | [diff] [blame] | 525 | std::string json_data = |
| 526 | client()->GetPrefs()->GetString(omnibox::kZeroSuggestCachedResults); |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 527 | if (!json_data.empty()) { |
dcheng | 259570c | 2016-04-22 00:45:57 | [diff] [blame] | 528 | std::unique_ptr<base::Value> data( |
[email protected] | 2c802d1 | 2014-07-31 12:57:14 | [diff] [blame] | 529 | SearchSuggestionParser::DeserializeJsonData(json_data)); |
[email protected] | 776ee590 | 2014-08-11 09:15:19 | [diff] [blame] | 530 | if (data && ParseSuggestResults( |
| 531 | *data, kDefaultZeroSuggestRelevance, false, &results_)) { |
[email protected] | 855ebff | 2014-05-09 07:14:38 | [diff] [blame] | 532 | ConvertResultsToAutocompleteMatches(); |
| 533 | results_from_cache_ = !matches_.empty(); |
| 534 | } |
| 535 | } |
| 536 | } |