OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/omnibox/browser/zero_suggest_provider.h" | 5 #include "components/omnibox/browser/zero_suggest_provider.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/feature_list.h" | 10 #include "base/feature_list.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value, | 78 UMA_HISTOGRAM_ENUMERATION("Omnibox.ZeroSuggestRequests", request_value, |
79 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE); | 79 ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE); |
80 } | 80 } |
81 | 81 |
82 // Relevance value to use if it was not set explicitly by the server. | 82 // Relevance value to use if it was not set explicitly by the server. |
83 const int kDefaultZeroSuggestRelevance = 100; | 83 const int kDefaultZeroSuggestRelevance = 100; |
84 | 84 |
85 // Used for testing whether zero suggest is ever available. | 85 // Used for testing whether zero suggest is ever available. |
86 constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/"; | 86 constexpr char kArbitraryInsecureUrlString[] = "http://www.google.com/"; |
87 | 87 |
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 | |
114 } // namespace | 88 } // namespace |
115 | 89 |
116 // static | 90 // static |
117 ZeroSuggestProvider* ZeroSuggestProvider::Create( | 91 ZeroSuggestProvider* ZeroSuggestProvider::Create( |
118 AutocompleteProviderClient* client, | 92 AutocompleteProviderClient* client, |
119 HistoryURLProvider* history_url_provider, | 93 HistoryURLProvider* history_url_provider, |
120 AutocompleteProviderListener* listener) { | 94 AutocompleteProviderListener* listener) { |
121 return new ZeroSuggestProvider(client, history_url_provider, listener); | 95 return new ZeroSuggestProvider(client, history_url_provider, listener); |
122 } | 96 } |
123 | 97 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 eligibility = can_send_ordinary_url | 144 eligibility = can_send_ordinary_url |
171 ? ZeroSuggestEligibility::URL_INELIGIBLE | 145 ? ZeroSuggestEligibility::URL_INELIGIBLE |
172 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE; | 146 : ZeroSuggestEligibility::GENERALLY_INELIGIBLE; |
173 } | 147 } |
174 UMA_HISTOGRAM_ENUMERATION( | 148 UMA_HISTOGRAM_ENUMERATION( |
175 "Omnibox.ZeroSuggest.Eligible.OnFocus", static_cast<int>(eligibility), | 149 "Omnibox.ZeroSuggest.Eligible.OnFocus", static_cast<int>(eligibility), |
176 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE)); | 150 static_cast<int>(ZeroSuggestEligibility::ELIGIBLE_MAX_VALUE)); |
177 if (can_send_current_url && | 151 if (can_send_current_url && |
178 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() && | 152 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() && |
179 !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { | 153 !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
154 // Maybe redirect the request to the service maintained by Chrome, which | |
155 // provides experimental contextual suggestions. | |
156 contextual_suggestions::ContextualSuggestionsService* | |
157 contextual_suggestions_service = | |
158 client()->GetContextualSuggestionsService(); | |
159 if (contextual_suggestions_service != nullptr && | |
160 contextual_suggestions_service | |
161 ->UseExperimentalZeroSuggestSuggestions()) { | |
162 // Create a request for experimental suggestions with |this| as the | |
163 // fetcher delegate. | |
164 contextual_suggestions_service->CreateContextualSuggestionsRequest( | |
165 current_query_, /*fetcher_delegate=*/this, | |
166 base::BindOnce( | |
167 &ZeroSuggestProvider::OnContextualSuggestionsFetcherAvailable, | |
168 weak_ptr_factory_.GetWeakPtr())); | |
169 return; | |
170 } | |
171 | |
180 // Update suggest_url to include the current_page_url. | 172 // Update suggest_url to include the current_page_url. |
181 if (UseExperimentalSuggestService(*template_url_service)) { | 173 base::string16 prefix; |
182 suggest_url = GURL( | 174 TemplateURLRef::SearchTermsArgs search_term_args(prefix); |
183 OmniboxFieldTrial::ZeroSuggestRedirectToChromeServerAddress() + | 175 search_term_args.current_page_url = current_query_; |
184 "/url=" + net::EscapePath(current_query_) + | 176 suggest_url = |
185 OmniboxFieldTrial::ZeroSuggestRedirectToChromeAdditionalFields()); | 177 GURL(default_provider->suggestions_url_ref().ReplaceSearchTerms( |
186 } else { | 178 search_term_args, template_url_service->search_terms_data())); |
187 base::string16 prefix; | |
188 TemplateURLRef::SearchTermsArgs search_term_args(prefix); | |
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 } | |
194 } else if (!ShouldShowNonContextualZeroSuggest(input.current_url())) { | 179 } else if (!ShouldShowNonContextualZeroSuggest(input.current_url())) { |
195 return; | 180 return; |
196 } | 181 } |
197 | 182 |
198 done_ = false; | 183 done_ = false; |
199 // TODO(jered): Consider adding locally-sourced zero-suggestions here too. | 184 // 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 | 185 // These may be useful on the NTP or more relevant to the user than server |
201 // suggestions, if based on local browsing history. | 186 // suggestions, if based on local browsing history. |
202 MaybeUseCachedSuggestions(); | 187 MaybeUseCachedSuggestions(); |
203 Run(suggest_url); | 188 Run(suggest_url); |
204 } | 189 } |
205 | 190 |
191 void ZeroSuggestProvider::OnContextualSuggestionsFetcherAvailable( | |
192 std::unique_ptr<net::URLFetcher> fetcher) { | |
193 if (fetcher == nullptr) { | |
194 return; | |
195 } | |
196 fetcher_ = std::move(fetcher); | |
197 done_ = false; | |
198 fetcher_->Start(); | |
199 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); | |
Marc Treib
2017/07/06 16:02:46
Is this change intended?
gcomanici
2017/07/06 16:54:17
Yes. I forgot to add it before. It is quite import
| |
200 } | |
201 | |
206 void ZeroSuggestProvider::Stop(bool clear_cached_results, | 202 void ZeroSuggestProvider::Stop(bool clear_cached_results, |
207 bool due_to_user_inactivity) { | 203 bool due_to_user_inactivity) { |
208 if (fetcher_) | 204 if (fetcher_) |
209 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); | 205 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); |
210 fetcher_.reset(); | 206 fetcher_.reset(); |
211 waiting_for_most_visited_urls_request_ = false; | 207 waiting_for_most_visited_urls_request_ = false; |
212 done_ = true; | 208 done_ = true; |
213 | 209 |
214 if (clear_cached_results) { | 210 if (clear_cached_results) { |
215 // We do not call Clear() on |results_| to retain |verbatim_relevance| | 211 // We do not call Clear() on |results_| to retain |verbatim_relevance| |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
601 if (!json_data.empty()) { | 597 if (!json_data.empty()) { |
602 std::unique_ptr<base::Value> data( | 598 std::unique_ptr<base::Value> data( |
603 SearchSuggestionParser::DeserializeJsonData(json_data)); | 599 SearchSuggestionParser::DeserializeJsonData(json_data)); |
604 if (data && ParseSuggestResults( | 600 if (data && ParseSuggestResults( |
605 *data, kDefaultZeroSuggestRelevance, false, &results_)) { | 601 *data, kDefaultZeroSuggestRelevance, false, &results_)) { |
606 ConvertResultsToAutocompleteMatches(); | 602 ConvertResultsToAutocompleteMatches(); |
607 results_from_cache_ = !matches_.empty(); | 603 results_from_cache_ = !matches_.empty(); |
608 } | 604 } |
609 } | 605 } |
610 } | 606 } |
OLD | NEW |