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 "chrome/browser/autocomplete/zero_suggest_provider.h" | 5 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 listener_->OnProviderUpdate(true); | 149 listener_->OnProviderUpdate(true); |
150 } | 150 } |
151 | 151 |
152 void ZeroSuggestProvider::StartZeroSuggest( | 152 void ZeroSuggestProvider::StartZeroSuggest( |
153 const GURL& url, | 153 const GURL& url, |
154 AutocompleteInput::PageClassification page_classification, | 154 AutocompleteInput::PageClassification page_classification, |
155 const string16& permanent_text) { | 155 const string16& permanent_text) { |
156 Stop(true); | 156 Stop(true); |
157 field_trial_triggered_ = false; | 157 field_trial_triggered_ = false; |
158 field_trial_triggered_in_session_ = false; | 158 field_trial_triggered_in_session_ = false; |
159 if (!ShouldRunZeroSuggest(url)) | 159 if (!SearchProvider::CanSendURL(url, profile_) || |
| 160 !OmniboxFieldTrial::InZeroSuggestFieldTrial()) |
160 return; | 161 return; |
161 verbatim_relevance_ = kDefaultVerbatimZeroSuggestRelevance; | 162 verbatim_relevance_ = kDefaultVerbatimZeroSuggestRelevance; |
162 done_ = false; | 163 done_ = false; |
163 permanent_text_ = permanent_text; | 164 permanent_text_ = permanent_text; |
164 current_query_ = url.spec(); | 165 current_query_ = url.spec(); |
165 current_page_classification_ = page_classification; | 166 current_page_classification_ = page_classification; |
166 current_url_match_ = MatchForCurrentURL(); | 167 current_url_match_ = MatchForCurrentURL(); |
167 // TODO(jered): Consider adding locally-sourced zero-suggestions here too. | 168 // TODO(jered): Consider adding locally-sourced zero-suggestions here too. |
168 // These may be useful on the NTP or more relevant to the user than server | 169 // These may be useful on the NTP or more relevant to the user than server |
169 // suggestions, if based on local browsing history. | 170 // suggestions, if based on local browsing history. |
170 Run(); | 171 Run(); |
171 } | 172 } |
172 | 173 |
173 ZeroSuggestProvider::ZeroSuggestProvider( | 174 ZeroSuggestProvider::ZeroSuggestProvider( |
174 AutocompleteProviderListener* listener, | 175 AutocompleteProviderListener* listener, |
175 Profile* profile) | 176 Profile* profile) |
176 : AutocompleteProvider(listener, profile, | 177 : AutocompleteProvider(listener, profile, |
177 AutocompleteProvider::TYPE_ZERO_SUGGEST), | 178 AutocompleteProvider::TYPE_ZERO_SUGGEST), |
178 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), | 179 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), |
179 have_pending_request_(false), | 180 have_pending_request_(false), |
180 verbatim_relevance_(kDefaultVerbatimZeroSuggestRelevance), | 181 verbatim_relevance_(kDefaultVerbatimZeroSuggestRelevance), |
181 field_trial_triggered_(false), | 182 field_trial_triggered_(false), |
182 field_trial_triggered_in_session_(false), | 183 field_trial_triggered_in_session_(false), |
183 weak_ptr_factory_(this) { | 184 weak_ptr_factory_(this) { |
184 } | 185 } |
185 | 186 |
186 ZeroSuggestProvider::~ZeroSuggestProvider() { | 187 ZeroSuggestProvider::~ZeroSuggestProvider() { |
187 } | 188 } |
188 | 189 |
189 bool ZeroSuggestProvider::ShouldRunZeroSuggest(const GURL& url) const { | |
190 if (!ShouldSendURL(url)) | |
191 return false; | |
192 | |
193 // Don't run if there's no profile or in incognito mode. | |
194 if (profile_ == NULL || profile_->IsOffTheRecord()) | |
195 return false; | |
196 | |
197 // Don't run if we can't get preferences or search suggest is not enabled. | |
198 PrefService* prefs = profile_->GetPrefs(); | |
199 if (prefs == NULL || !prefs->GetBoolean(prefs::kSearchSuggestEnabled)) | |
200 return false; | |
201 | |
202 ProfileSyncService* service = | |
203 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); | |
204 browser_sync::SyncPrefs sync_prefs(prefs); | |
205 // The user has needs to have Chrome Sync enabled (for permissions to | |
206 // transmit their current URL) and be in the field trial. | |
207 if (!OmniboxFieldTrial::InZeroSuggestFieldTrial() || | |
208 service == NULL || | |
209 !service->IsSyncEnabledAndLoggedIn() || | |
210 !sync_prefs.HasKeepEverythingSynced()) { | |
211 return false; | |
212 } | |
213 return true; | |
214 } | |
215 | |
216 bool ZeroSuggestProvider::ShouldSendURL(const GURL& url) const { | |
217 if (!url.is_valid()) | |
218 return false; | |
219 | |
220 // Only allow HTTP URLs or Google HTTPS URLs (including Google search | |
221 // result pages). For the latter case, Google was already sent the HTTPS | |
222 // URLs when requesting the page, so the information is just re-sent. | |
223 return (url.scheme() == content::kHttpScheme) || | |
224 google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | |
225 google_util::ALLOW_NON_STANDARD_PORTS); | |
226 } | |
227 | |
228 void ZeroSuggestProvider::FillResults( | 190 void ZeroSuggestProvider::FillResults( |
229 const Value& root_val, | 191 const Value& root_val, |
230 int* verbatim_relevance, | 192 int* verbatim_relevance, |
231 SearchProvider::SuggestResults* suggest_results, | 193 SearchProvider::SuggestResults* suggest_results, |
232 SearchProvider::NavigationResults* navigation_results) { | 194 SearchProvider::NavigationResults* navigation_results) { |
233 string16 query; | 195 string16 query; |
234 const ListValue* root_list = NULL; | 196 const ListValue* root_list = NULL; |
235 const ListValue* results = NULL; | 197 const ListValue* results = NULL; |
236 const ListValue* relevances = NULL; | 198 const ListValue* relevances = NULL; |
237 // The response includes the query, which should be empty for ZeroSuggest | 199 // The response includes the query, which should be empty for ZeroSuggest |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 // TODO(hfung): Generalize if the default provider supports zero suggest. | 340 // TODO(hfung): Generalize if the default provider supports zero suggest. |
379 // Only make the request if we know that the provider supports zero suggest | 341 // Only make the request if we know that the provider supports zero suggest |
380 // (currently only the prepopulated Google provider). | 342 // (currently only the prepopulated Google provider). |
381 if (default_provider == NULL || !default_provider->SupportsReplacement() || | 343 if (default_provider == NULL || !default_provider->SupportsReplacement() || |
382 default_provider->prepopulate_id() != 1) { | 344 default_provider->prepopulate_id() != 1) { |
383 Stop(true); | 345 Stop(true); |
384 return; | 346 return; |
385 } | 347 } |
386 string16 prefix; | 348 string16 prefix; |
387 TemplateURLRef::SearchTermsArgs search_term_args(prefix); | 349 TemplateURLRef::SearchTermsArgs search_term_args(prefix); |
388 search_term_args.zero_prefix_url = current_query_; | 350 search_term_args.current_page_url = current_query_; |
389 std::string req_url = default_provider->suggestions_url_ref(). | 351 std::string req_url = default_provider->suggestions_url_ref(). |
390 ReplaceSearchTerms(search_term_args); | 352 ReplaceSearchTerms(search_term_args); |
391 GURL suggest_url(req_url); | 353 GURL suggest_url(req_url); |
392 // Make sure we are sending the suggest request through HTTPS. | 354 // Make sure we are sending the suggest request through HTTPS. |
393 if (!suggest_url.SchemeIs(content::kHttpsScheme)) { | 355 if (!suggest_url.SchemeIs(content::kHttpsScheme)) { |
394 Stop(true); | 356 Stop(true); |
395 return; | 357 return; |
396 } | 358 } |
397 | 359 |
398 fetcher_.reset( | 360 fetcher_.reset( |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 match.is_history_what_you_typed_match = false; | 462 match.is_history_what_you_typed_match = false; |
501 match.allowed_to_be_default_match = true; | 463 match.allowed_to_be_default_match = true; |
502 | 464 |
503 // The placeholder suggestion for the current URL has high relevance so | 465 // The placeholder suggestion for the current URL has high relevance so |
504 // that it is in the first suggestion slot and inline autocompleted. It | 466 // that it is in the first suggestion slot and inline autocompleted. It |
505 // gets dropped as soon as the user types something. | 467 // gets dropped as soon as the user types something. |
506 match.relevance = verbatim_relevance_; | 468 match.relevance = verbatim_relevance_; |
507 | 469 |
508 return match; | 470 return match; |
509 } | 471 } |
OLD | NEW |