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 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 | 303 |
| 303 match.description = | 304 match.description = |
| 304 AutocompleteMatch::SanitizeString(navigation.description()); | 305 AutocompleteMatch::SanitizeString(navigation.description()); |
| 305 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, | 306 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
| 306 match.description.length(), ACMatchClassification::NONE, | 307 match.description.length(), ACMatchClassification::NONE, |
| 307 &match.description_class); | 308 &match.description_class); |
| 308 return match; | 309 return match; |
| 309 } | 310 } |
| 310 | 311 |
| 311 void ZeroSuggestProvider::Run(const GURL& suggest_url) { | 312 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()) { | 313 if (OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial()) { |
| 327 most_visited_urls_.clear(); | 314 most_visited_urls_.clear(); |
| 328 history::TopSites* ts = profile_->GetTopSites(); | 315 history::TopSites* ts = profile_->GetTopSites(); |
| 329 if (ts) { | 316 if (ts) { |
| 330 ts->GetMostVisitedURLs( | 317 ts->GetMostVisitedURLs( |
| 331 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, | 318 base::Bind(&ZeroSuggestProvider::OnMostVisitedUrlsAvailable, |
| 332 weak_ptr_factory_.GetWeakPtr()), false); | 319 weak_ptr_factory_.GetWeakPtr()), false); |
| 333 } | 320 } |
| 321 } else { | |
| 322 const int kFetcherID = 1; | |
| 323 fetcher_.reset( | |
| 324 net::URLFetcher::Create(kFetcherID, | |
| 325 suggest_url, | |
| 326 net::URLFetcher::GET, this)); | |
| 327 fetcher_->SetRequestContext(profile_->GetRequestContext()); | |
| 328 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 329 // Add Chrome experiment state to the request headers. | |
| 330 net::HttpRequestHeaders headers; | |
| 331 variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( | |
| 332 fetcher_->GetOriginalURL(), profile_->IsOffTheRecord(), false, | |
| 333 &headers); | |
| 334 fetcher_->SetExtraRequestHeaders(headers.ToString()); | |
| 335 fetcher_->Start(); | |
| 334 } | 336 } |
| 335 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); | 337 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
|
H Fung
2014/10/21 01:20:56
I would have thought there would be a flag to cont
Maria
2014/10/21 18:04:11
I could add a flag if you are still using MV varia
H Fung
2014/10/22 19:48:28
OK, I don't plan on running any more most visited
| |
| 336 } | 338 } |
| 337 | 339 |
| 338 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( | 340 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
| 339 const history::MostVisitedURLList& urls) { | 341 const history::MostVisitedURLList& urls) { |
| 340 most_visited_urls_ = urls; | 342 most_visited_urls_ = urls; |
| 343 done_ = true; | |
| 344 ConvertResultsToAutocompleteMatches(); | |
| 345 listener_->OnProviderUpdate(true); | |
| 341 } | 346 } |
| 342 | 347 |
| 343 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { | 348 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
| 344 matches_.clear(); | 349 matches_.clear(); |
| 345 | 350 |
| 346 const TemplateURL* default_provider = | 351 const TemplateURL* default_provider = |
| 347 template_url_service_->GetDefaultSearchProvider(); | 352 template_url_service_->GetDefaultSearchProvider(); |
| 348 // Fail if we can't set the clickthrough URL for query suggestions. | 353 // Fail if we can't set the clickthrough URL for query suggestions. |
| 349 if (default_provider == NULL || !default_provider->SupportsReplacement( | 354 if (default_provider == NULL || !default_provider->SupportsReplacement( |
| 350 template_url_service_->search_terms_data())) | 355 template_url_service_->search_terms_data())) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 match.relevance = GetVerbatimRelevance(); | 422 match.relevance = GetVerbatimRelevance(); |
| 418 | 423 |
| 419 return match; | 424 return match; |
| 420 } | 425 } |
| 421 | 426 |
| 422 int ZeroSuggestProvider::GetVerbatimRelevance() const { | 427 int ZeroSuggestProvider::GetVerbatimRelevance() const { |
| 423 return results_.verbatim_relevance >= 0 ? | 428 return results_.verbatim_relevance >= 0 ? |
| 424 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance; | 429 results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance; |
| 425 } | 430 } |
| 426 | 431 |
| 427 bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL( | 432 bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL( |
|
H Fung
2014/10/21 01:20:55
This function name isn't accurate anymore (since i
Maria
2014/10/21 18:04:11
Done.
| |
| 428 const GURL& suggest_url, | 433 const GURL& suggest_url, |
| 429 const GURL& current_page_url) const { | 434 const GURL& current_page_url) const { |
| 430 if (!ZeroSuggestEnabled(suggest_url, | 435 if (!ZeroSuggestEnabled(suggest_url, |
| 431 template_url_service_->GetDefaultSearchProvider(), | 436 template_url_service_->GetDefaultSearchProvider(), |
| 432 current_page_classification_, | 437 current_page_classification_, |
| 433 template_url_service_->search_terms_data(), | 438 template_url_service_->search_terms_data(), |
| 434 client_.get())) | 439 client_.get())) |
| 435 return false; | 440 return false; |
| 436 | 441 |
| 437 // If we cannot send URLs, then only the MostVisited and Personalized | 442 // If we cannot send URLs, then only the MostVisited and Personalized |
| 438 // variations can be shown. | 443 // variations can be shown. |
| 439 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() && | 444 if (!OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() && |
| 440 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) | 445 !OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 441 return false; | 446 return false; |
| 442 | 447 |
| 443 // Only show zero suggest for HTTP[S] pages. | 448 // Only show zero suggest for HTTP[S] pages. |
| 444 // TODO(mariakhomenko): We may be able to expand this set to include pages | 449 // 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 | 450 // with other schemes (e.g. chrome://). That may require improvements to |
| 446 // the formatting of the verbatim result returned by MatchForCurrentURL(). | 451 // the formatting of the verbatim result returned by MatchForCurrentURL(). |
| 447 if (!current_page_url.is_valid() || | 452 if (!current_page_url.is_valid() || |
| 448 ((current_page_url.scheme() != url::kHttpScheme) && | 453 ((current_page_url.scheme() != url::kHttpScheme) && |
| 449 (current_page_url.scheme() != url::kHttpsScheme))) | 454 (current_page_url.scheme() != url::kHttpsScheme))) |
| 450 return false; | 455 return false; |
| 451 | 456 |
| 457 if (OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() && | |
| 458 TemplateURLServiceFactory::GetForProfile(profile_)-> | |
| 459 IsSearchResultsPageFromDefaultSearchProvider(current_page_url)) | |
| 460 return false; | |
| 461 | |
| 452 return true; | 462 return true; |
| 453 } | 463 } |
| 454 | 464 |
| 455 void ZeroSuggestProvider::MaybeUseCachedSuggestions() { | 465 void ZeroSuggestProvider::MaybeUseCachedSuggestions() { |
| 456 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) | 466 if (!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) |
| 457 return; | 467 return; |
| 458 | 468 |
| 459 std::string json_data = profile_->GetPrefs()->GetString( | 469 std::string json_data = profile_->GetPrefs()->GetString( |
| 460 prefs::kZeroSuggestCachedResults); | 470 prefs::kZeroSuggestCachedResults); |
| 461 if (!json_data.empty()) { | 471 if (!json_data.empty()) { |
| 462 scoped_ptr<base::Value> data( | 472 scoped_ptr<base::Value> data( |
| 463 SearchSuggestionParser::DeserializeJsonData(json_data)); | 473 SearchSuggestionParser::DeserializeJsonData(json_data)); |
| 464 if (data && ParseSuggestResults( | 474 if (data && ParseSuggestResults( |
| 465 *data, kDefaultZeroSuggestRelevance, false, &results_)) { | 475 *data, kDefaultZeroSuggestRelevance, false, &results_)) { |
| 466 ConvertResultsToAutocompleteMatches(); | 476 ConvertResultsToAutocompleteMatches(); |
| 467 results_from_cache_ = !matches_.empty(); | 477 results_from_cache_ = !matches_.empty(); |
| 468 } | 478 } |
| 469 } | 479 } |
| 470 } | 480 } |
| OLD | NEW |