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