Chromium Code Reviews| 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" |
| 11 #include "base/metrics/user_metrics.h" | 11 #include "base/metrics/user_metrics.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 17 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| 18 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 18 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 19 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" | 19 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" |
| 20 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 20 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
| 21 #include "chrome/browser/autocomplete/history_url_provider.h" | 21 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 22 #include "chrome/browser/history/top_sites.h" | 22 #include "chrome/browser/history/top_sites.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 24 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 25 #include "components/history/core/browser/history_types.h" | 26 #include "components/history/core/browser/history_types.h" |
| 26 #include "components/metrics/proto/omnibox_input_type.pb.h" | 27 #include "components/metrics/proto/omnibox_input_type.pb.h" |
| 27 #include "components/omnibox/autocomplete_input.h" | 28 #include "components/omnibox/autocomplete_input.h" |
| 28 #include "components/omnibox/autocomplete_match.h" | 29 #include "components/omnibox/autocomplete_match.h" |
| 29 #include "components/omnibox/autocomplete_provider_listener.h" | 30 #include "components/omnibox/autocomplete_provider_listener.h" |
| 30 #include "components/omnibox/omnibox_field_trial.h" | 31 #include "components/omnibox/omnibox_field_trial.h" |
| 31 #include "components/omnibox/search_provider.h" | 32 #include "components/omnibox/search_provider.h" |
| 32 #include "components/pref_registry/pref_registry_syncable.h" | 33 #include "components/pref_registry/pref_registry_syncable.h" |
| 33 #include "components/search_engines/template_url_service.h" | 34 #include "components/search_engines/template_url_service.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 if (default_provider == NULL) | 110 if (default_provider == NULL) |
| 110 return; | 111 return; |
| 111 | 112 |
| 112 base::string16 prefix; | 113 base::string16 prefix; |
| 113 TemplateURLRef::SearchTermsArgs search_term_args(prefix); | 114 TemplateURLRef::SearchTermsArgs search_term_args(prefix); |
| 114 GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms( | 115 GURL suggest_url(default_provider->suggestions_url_ref().ReplaceSearchTerms( |
| 115 search_term_args, template_url_service_->search_terms_data())); | 116 search_term_args, template_url_service_->search_terms_data())); |
| 116 if (!suggest_url.is_valid()) | 117 if (!suggest_url.is_valid()) |
| 117 return; | 118 return; |
| 118 | 119 |
| 119 // No need to send the current page URL in personalized suggest field trial. | 120 // No need to send the current page URL in personalized suggest or |
| 121 // most visited field trials. | |
| 120 if (CanSendURL(input.current_url(), suggest_url, default_provider, | 122 if (CanSendURL(input.current_url(), suggest_url, default_provider, |
| 121 current_page_classification_, | 123 current_page_classification_, |
| 122 template_url_service_->search_terms_data(), client_.get()) && | 124 template_url_service_->search_terms_data(), client_.get()) && |
| 123 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { | 125 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() && |
| 126 !OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { | |
| 124 // Update suggest_url to include the current_page_url. | 127 // Update suggest_url to include the current_page_url. |
| 125 search_term_args.current_page_url = current_query_; | 128 search_term_args.current_page_url = current_query_; |
| 126 suggest_url = GURL(default_provider->suggestions_url_ref(). | 129 suggest_url = GURL(default_provider->suggestions_url_ref(). |
| 127 ReplaceSearchTerms( | 130 ReplaceSearchTerms( |
| 128 search_term_args, | 131 search_term_args, |
| 129 template_url_service_->search_terms_data())); | 132 template_url_service_->search_terms_data())); |
| 130 } else if (!CanShowZeroSuggestWithoutSendingURL(suggest_url, | 133 } else if (!ShouldShowNonContextualZeroSuggest(suggest_url, |
| 131 input.current_url())) { | 134 input.current_url())) { |
| 132 return; | 135 return; |
| 133 } | 136 } |
| 134 | 137 |
| 135 done_ = false; | 138 done_ = false; |
| 136 // TODO(jered): Consider adding locally-sourced zero-suggestions here too. | 139 // TODO(jered): Consider adding locally-sourced zero-suggestions here too. |
| 137 // These may be useful on the NTP or more relevant to the user than server | 140 // These may be useful on the NTP or more relevant to the user than server |
| 138 // suggestions, if based on local browsing history. | 141 // suggestions, if based on local browsing history. |
| 139 MaybeUseCachedSuggestions(); | 142 MaybeUseCachedSuggestions(); |
| 140 Run(suggest_url); | 143 Run(suggest_url); |
| 141 } | 144 } |
| 142 | 145 |
| 143 void ZeroSuggestProvider::Stop(bool clear_cached_results) { | 146 void ZeroSuggestProvider::Stop(bool clear_cached_results) { |
| 144 if (fetcher_) | 147 if (fetcher_) |
| 145 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); | 148 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_INVALIDATED); |
| 146 fetcher_.reset(); | 149 fetcher_.reset(); |
| 147 done_ = true; | 150 done_ = true; |
| 148 | 151 |
|
Mark P
2014/10/21 19:08:06
Forgive me that I don't fully understand the weak
Maria
2014/10/21 23:57:39
You are right. Added code to cover this case and a
| |
| 149 if (clear_cached_results) { | 152 if (clear_cached_results) { |
| 150 // We do not call Clear() on |results_| to retain |verbatim_relevance| | 153 // We do not call Clear() on |results_| to retain |verbatim_relevance| |
| 151 // value in the |results_| object. |verbatim_relevance| is used at the | 154 // value in the |results_| object. |verbatim_relevance| is used at the |
| 152 // beginning of the next StartZeroSuggest() call to determine the current | 155 // beginning of the next StartZeroSuggest() call to determine the current |
| 153 // url match relevance. | 156 // url match relevance. |
| 154 results_.suggest_results.clear(); | 157 results_.suggest_results.clear(); |
| 155 results_.navigation_results.clear(); | 158 results_.navigation_results.clear(); |
| 156 current_query_.clear(); | 159 current_query_.clear(); |
| 157 } | 160 } |
| 158 } | 161 } |
| 159 | 162 |
| 160 void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) { | 163 void ZeroSuggestProvider::DeleteMatch(const AutocompleteMatch& match) { |
|
Mark P
2014/10/21 19:08:06
Can we do something here in the Most Visited case?
Maria
2014/10/21 23:57:39
Currently the matches are not marked as deletable.
Mark P
2014/10/22 19:11:23
That sounds fine. I just wanted to make sure you
| |
| 161 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { | 164 if (OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) { |
| 162 // Remove the deleted match from the cache, so it is not shown to the user | 165 // Remove the deleted match from the cache, so it is not shown to the user |
| 163 // again. Since we cannot remove just one result, blow away the cache. | 166 // again. Since we cannot remove just one result, blow away the cache. |
| 164 profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults, | 167 profile_->GetPrefs()->SetString(prefs::kZeroSuggestCachedResults, |
| 165 std::string()); | 168 std::string()); |
| 166 } | 169 } |
| 167 BaseSearchProvider::DeleteMatch(match); | 170 BaseSearchProvider::DeleteMatch(match); |
| 168 } | 171 } |
| 169 | 172 |
| 170 void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const { | 173 void ZeroSuggestProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 | 305 |
| 303 match.description = | 306 match.description = |
| 304 AutocompleteMatch::SanitizeString(navigation.description()); | 307 AutocompleteMatch::SanitizeString(navigation.description()); |
| 305 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, | 308 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
| 306 match.description.length(), ACMatchClassification::NONE, | 309 match.description.length(), ACMatchClassification::NONE, |
| 307 &match.description_class); | 310 &match.description_class); |
| 308 return match; | 311 return match; |
| 309 } | 312 } |
| 310 | 313 |
| 311 void ZeroSuggestProvider::Run(const GURL& suggest_url) { | 314 void ZeroSuggestProvider::Run(const GURL& suggest_url) { |
| 312 const int kFetcherID = 1; | |
| 313 fetcher_.reset( | |
| 314 net::URLFetcher::Create(kFetcherID, | |
| 315 suggest_url, | |
| 316 net::URLFetcher::GET, this)); | |
| 317 fetcher_->SetRequestContext(profile_->GetRequestContext()); | |
| 318 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 319 // Add Chrome experiment state to the request headers. | |
| 320 net::HttpRequestHeaders headers; | |
| 321 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( | |
| 322 fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); | |
| 323 fetcher_->SetExtraRequestHeaders(headers.ToString()); | |
| 324 fetcher_->Start(); | |
| 325 | |
| 326 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { | 315 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
| 327 most_visited_urls_.clear(); | 316 most_visited_urls_.clear(); |
| 328 history::TopSites* ts = profile_->GetTopSites(); | 317 history::TopSites* ts = profile_->GetTopSites(); |
| 329 if (ts) { | 318 if (ts) { |
| 330 ts->GetMostVisitedURLs( | 319 ts->GetMostVisitedURLs( |
| 331 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, | 320 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, |
| 332 weak_ptr_factory_.GetWeakPtr()), false); | 321 weak_ptr_factory_.GetWeakPtr()), false); |
| 333 } | 322 } |
| 323 } else { | |
| 324 const int kFetcherID = 1; | |
| 325 fetcher_.reset( | |
| 326 net::URLFetcher::Create(kFetcherID, | |
| 327 suggest_url, | |
| 328 net::URLFetcher::GET, this)); | |
| 329 fetcher_->SetRequestContext(profile_->GetRequestContext()); | |
| 330 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 331 // Add Chrome experiment state to the request headers. | |
| 332 net::HttpRequestHeaders headers; | |
| 333 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( | |
| 334 fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, | |
| 335 &headers); | |
| 336 fetcher_->SetExtraRequestHeaders(headers.ToString()); | |
| 337 fetcher_->Start(); | |
| 338 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); | |
| 334 } | 339 } |
| 335 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); | |
| 336 } | 340 } |
| 337 | 341 |
| 338 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( | 342 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
| 339 const history::MostVisitedURLList& urls) { | 343 const history::MostVisitedURLList& urls) { |
| 340 most_visited_urls_ = urls; | 344 most_visited_urls_ = urls; |
| 345 done_ = true; | |
| 346 ConvertResultsToAutocompleteMatches(); | |
| 347 listener_->OnProviderUpdate(true); | |
| 341 } | 348 } |
| 342 | 349 |
| 343 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { | 350 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
| 344 matches_.clear(); | 351 matches_.clear(); |
| 345 | 352 |
| 346 const TemplateURL* default_provider = | 353 const TemplateURL* default_provider = |
| 347 template_url_service_->GetDefaultSearchProvider(); | 354 template_url_service_->GetDefaultSearchProvider(); |
| 348 // Fail if we can't set the clickthrough URL for query suggestions. | 355 // Fail if we can't set the clickthrough URL for query suggestions. |
| 349 if (default_provider == NULL || !default_provider->SupportsReplacement( | 356 if (default_provider == NULL || !default_provider->SupportsReplacement( |
| 350 template_url_service_->search_terms_data())) | 357 template_url_service_->search_terms_data())) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 match.relevance = GetVerbatimRelevance(); | 424 match.relevance = GetVerbatimRelevance(); |
| 418 | 425 |
| 419 return match; | 426 return match; |
| 420 } | 427 } |
| 421 | 428 |
| 422 int ZeroSuggestProvider::GetVerbatimRelevance() const { | 429 int ZeroSuggestProvider::GetVerbatimRelevance() const { |
| 423 return results_.verbatim_relevance >= 0 ? | 430 return results_.verbatim_relevance >= 0 ? |
| 424 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance; | 431 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance; |
| 425 } | 432 } |
| 426 | 433 |
| 427 bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL( | 434 bool ZeroSuggestProvider::ShouldShowNonContextualZeroSuggest( |
| 428 const GURL& suggest_url, | 435 const GURL& suggest_url, |
| 429 const GURL& current_page_url) const { | 436 const GURL& current_page_url) const { |
| 430 if (!ZeroSuggestEnabled(suggest_url, | 437 if (!ZeroSuggestEnabled(suggest_url, |
| 431 template_url_service_->GetDefaultSearchProvider(), | 438 template_url_service_->GetDefaultSearchProvider(), |
| 432 current_page_classification_, | 439 current_page_classification_, |
| 433 template_url_service_->search_terms_data(), | 440 template_url_service_->search_terms_data(), |
| 434 client_.get())) | 441 client_.get())) |
| 435 return false; | 442 return false; |
| 436 | 443 |
| 437 // If we cannot send URLs, then only the MostVisited and Personalized | 444 // If we cannot send URLs, then only the MostVisited and Personalized |
| 438 // variations can be shown. | 445 // variations can be shown. |
| 439 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() && | 446 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() && |
| 440 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) | 447 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 441 return false; | 448 return false; |
| 442 | 449 |
| 443 // Only show zero suggest for HTTP[S] pages. | 450 // Only show zero suggest for HTTP[S] pages. |
| 444 // TODO(mariakhomenko): We may be able to expand this set to include pages | 451 // TODO(mariakhomenko): We may be able to expand this set to include pages |
| 445 // with other schemes (e.g. chrome://). That may require improvements to | 452 // with other schemes (e.g. chrome://). That may require improvements to |
| 446 // the formatting of the verbatim result returned by MatchForCurrentURL(). | 453 // the formatting of the verbatim result returned by MatchForCurrentURL(). |
| 447 if (!current_page_url.is_valid() || | 454 if (!current_page_url.is_valid() || |
| 448 ((current_page_url.scheme() != url::kHttpScheme) && | 455 ((current_page_url.scheme() != url::kHttpScheme) && |
| 449 (current_page_url.scheme() != url::kHttpsScheme))) | 456 (current_page_url.scheme() != url::kHttpsScheme))) |
| 450 return false; | 457 return false; |
| 451 | 458 |
| 459 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() && | |
| 460 TemplateURLServiceFactory::GetForProfile(profile_)-> | |
| 461 IsSearchResultsPageFromDefaultSearchProvider(current_page_url)) | |
|
Mark P
2014/10/21 19:08:06
Generally I understand this works (at least on And
Maria
2014/10/21 23:57:39
Actually, I thought this is exactly what we discus
Mark P
2014/10/22 19:11:23
Okay, good to know. Is there a bug I can follow a
Maria
2014/10/23 20:34:26
Tracked in https://code.google.com/p/chromium/issu
| |
| 462 return false; | |
| 463 | |
| 452 return true; | 464 return true; |
| 453 } | 465 } |
| 454 | 466 |
| 455 void ZeroSuggestProvider::MaybeUseCachedSuggestions() { | 467 void ZeroSuggestProvider::MaybeUseCachedSuggestions() { |
| 456 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) | 468 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 457 return; | 469 return; |
| 458 | 470 |
| 459 std::string json_data = profile_->GetPrefs()->GetString( | 471 std::string json_data = profile_->GetPrefs()->GetString( |
| 460 prefs::kZeroSuggestCachedResults); | 472 prefs::kZeroSuggestCachedResults); |
| 461 if (!json_data.empty()) { | 473 if (!json_data.empty()) { |
| 462 scoped_ptr<base::Value> data( | 474 scoped_ptr<base::Value> data( |
| 463 SearchSuggestionParser::DeserializeJsonData(json_data)); | 475 SearchSuggestionParser::DeserializeJsonData(json_data)); |
| 464 if (data && ParseSuggestResults( | 476 if (data && ParseSuggestResults( |
| 465 *data, kDefaultZeroSuggestRelevance, false, &results_)) { | 477 *data, kDefaultZeroSuggestRelevance, false, &results_)) { |
| 466 ConvertResultsToAutocompleteMatches(); | 478 ConvertResultsToAutocompleteMatches(); |
| 467 results_from_cache_ = !matches_.empty(); | 479 results_from_cache_ = !matches_.empty(); |
| 468 } | 480 } |
| 469 } | 481 } |
| 470 } | 482 } |
| OLD | NEW |