OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/base_search_provider.h" | 5 #include "chrome/browser/autocomplete/base_search_provider.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
| 8 #include "base/metrics/user_metrics.h" |
8 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
9 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 13 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
13 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h" | 14 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h" |
14 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h" | 15 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h" |
15 #include "chrome/browser/history/history_service.h" | 16 #include "chrome/browser/history/history_service.h" |
16 #include "chrome/browser/history/history_service_factory.h" | 17 #include "chrome/browser/history/history_service_factory.h" |
17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 this); | 88 this); |
88 } | 89 } |
89 | 90 |
90 // BaseSearchProvider --------------------------------------------------------- | 91 // BaseSearchProvider --------------------------------------------------------- |
91 | 92 |
92 // static | 93 // static |
93 const int BaseSearchProvider::kDefaultProviderURLFetcherID = 1; | 94 const int BaseSearchProvider::kDefaultProviderURLFetcherID = 1; |
94 const int BaseSearchProvider::kKeywordProviderURLFetcherID = 2; | 95 const int BaseSearchProvider::kKeywordProviderURLFetcherID = 2; |
95 const int BaseSearchProvider::kDeletionURLFetcherID = 3; | 96 const int BaseSearchProvider::kDeletionURLFetcherID = 3; |
96 | 97 |
97 BaseSearchProvider::BaseSearchProvider(TemplateURLService* template_url_service, | 98 BaseSearchProvider::BaseSearchProvider( |
98 Profile* profile, | 99 TemplateURLService* template_url_service, |
99 AutocompleteProvider::Type type) | 100 Profile* profile, |
| 101 const base::UserMetricsAction& uma_deletion_success, |
| 102 const base::UserMetricsAction& uma_deletion_failure, |
| 103 AutocompleteProvider::Type type) |
100 : AutocompleteProvider(type), | 104 : AutocompleteProvider(type), |
101 template_url_service_(template_url_service), | 105 template_url_service_(template_url_service), |
102 profile_(profile), | 106 profile_(profile), |
103 field_trial_triggered_(false), | 107 field_trial_triggered_(false), |
104 field_trial_triggered_in_session_(false) { | 108 field_trial_triggered_in_session_(false), |
| 109 uma_deletion_success_(uma_deletion_success), |
| 110 uma_deletion_failure_(uma_deletion_failure) { |
105 } | 111 } |
106 | 112 |
107 // static | 113 // static |
108 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { | 114 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { |
109 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; | 115 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; |
110 } | 116 } |
111 | 117 |
112 // static | 118 // static |
113 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( | 119 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( |
114 const base::string16& suggestion, | 120 const base::string16& suggestion, |
115 AutocompleteMatchType::Type type, | 121 AutocompleteMatchType::Type type, |
116 bool from_keyword_provider, | 122 bool from_keyword_provider, |
117 const TemplateURL* template_url, | 123 const TemplateURL* template_url, |
118 const SearchTermsData& search_terms_data) { | 124 const SearchTermsData& search_terms_data) { |
119 return CreateSearchSuggestion( | 125 return CreateSearchSuggestion( |
120 NULL, AutocompleteInput(), SearchSuggestionParser::SuggestResult( | 126 NULL, AutocompleteInput(), SearchSuggestionParser::SuggestResult( |
121 suggestion, type, suggestion, base::string16(), base::string16(), | 127 suggestion, type, suggestion, base::string16(), base::string16(), |
122 base::string16(), base::string16(), std::string(), std::string(), | 128 base::string16(), base::string16(), std::string(), std::string(), |
123 from_keyword_provider, 0, false, false, base::string16()), | 129 from_keyword_provider, 0, false, false, base::string16()), |
124 template_url, search_terms_data, 0, false); | 130 template_url, search_terms_data, 0, false); |
125 } | 131 } |
126 | 132 |
127 void BaseSearchProvider::Stop(bool clear_cached_results) { | |
128 StopSuggest(); | |
129 done_ = true; | |
130 | |
131 if (clear_cached_results) | |
132 ClearAllResults(); | |
133 } | |
134 | |
135 void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) { | 133 void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) { |
136 DCHECK(match.deletable); | 134 DCHECK(match.deletable); |
137 if (!match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey).empty()) { | 135 if (!match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey).empty()) { |
138 deletion_handlers_.push_back(new SuggestionDeletionHandler( | 136 deletion_handlers_.push_back(new SuggestionDeletionHandler( |
139 match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey), | 137 match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey), |
140 profile_, | 138 profile_, |
141 base::Bind(&BaseSearchProvider::OnDeletionComplete, | 139 base::Bind(&BaseSearchProvider::OnDeletionComplete, |
142 base::Unretained(this)))); | 140 base::Unretained(this)))); |
143 } | 141 } |
144 | 142 |
(...skipping 21 matching lines...) Expand all Loading... |
166 std::vector<uint32> field_trial_hashes; | 164 std::vector<uint32> field_trial_hashes; |
167 OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes); | 165 OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes); |
168 for (size_t i = 0; i < field_trial_hashes.size(); ++i) { | 166 for (size_t i = 0; i < field_trial_hashes.size(); ++i) { |
169 if (field_trial_triggered_) | 167 if (field_trial_triggered_) |
170 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); | 168 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); |
171 if (field_trial_triggered_in_session_) { | 169 if (field_trial_triggered_in_session_) { |
172 new_entry.mutable_field_trial_triggered_in_session()->Add( | 170 new_entry.mutable_field_trial_triggered_in_session()->Add( |
173 field_trial_hashes[i]); | 171 field_trial_hashes[i]); |
174 } | 172 } |
175 } | 173 } |
176 ModifyProviderInfo(&new_entry); | |
177 } | 174 } |
178 | 175 |
179 // static | 176 // static |
180 const char BaseSearchProvider::kRelevanceFromServerKey[] = | 177 const char BaseSearchProvider::kRelevanceFromServerKey[] = |
181 "relevance_from_server"; | 178 "relevance_from_server"; |
182 const char BaseSearchProvider::kShouldPrefetchKey[] = "should_prefetch"; | 179 const char BaseSearchProvider::kShouldPrefetchKey[] = "should_prefetch"; |
183 const char BaseSearchProvider::kSuggestMetadataKey[] = "suggest_metadata"; | 180 const char BaseSearchProvider::kSuggestMetadataKey[] = "suggest_metadata"; |
184 const char BaseSearchProvider::kDeletionUrlKey[] = "deletion_url"; | 181 const char BaseSearchProvider::kDeletionUrlKey[] = "deletion_url"; |
185 const char BaseSearchProvider::kTrue[] = "true"; | 182 const char BaseSearchProvider::kTrue[] = "true"; |
186 const char BaseSearchProvider::kFalse[] = "false"; | 183 const char BaseSearchProvider::kFalse[] = "false"; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 !service->IsSyncEnabledAndLoggedIn() || | 356 !service->IsSyncEnabledAndLoggedIn() || |
360 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( | 357 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( |
361 syncer::PROXY_TABS) || | 358 syncer::PROXY_TABS) || |
362 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) | 359 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) |
363 return false; | 360 return false; |
364 | 361 |
365 return true; | 362 return true; |
366 } | 363 } |
367 | 364 |
368 void BaseSearchProvider::AddMatchToMap( | 365 void BaseSearchProvider::AddMatchToMap( |
| 366 const AutocompleteInput& input, |
369 const SearchSuggestionParser::SuggestResult& result, | 367 const SearchSuggestionParser::SuggestResult& result, |
| 368 const TemplateURL* template_url, |
370 const std::string& metadata, | 369 const std::string& metadata, |
371 int accepted_suggestion, | 370 int accepted_suggestion, |
| 371 bool should_append_extra_params, |
372 bool mark_as_deletable, | 372 bool mark_as_deletable, |
373 MatchMap* map) { | 373 MatchMap* map) { |
374 AutocompleteMatch match = CreateSearchSuggestion( | 374 AutocompleteMatch match = CreateSearchSuggestion( |
375 this, GetInput(result.from_keyword_provider()), result, | 375 this, input, result, template_url, |
376 GetTemplateURL(result.from_keyword_provider()), | |
377 template_url_service_->search_terms_data(), accepted_suggestion, | 376 template_url_service_->search_terms_data(), accepted_suggestion, |
378 ShouldAppendExtraParams(result)); | 377 should_append_extra_params); |
379 if (!match.destination_url.is_valid()) | 378 if (!match.destination_url.is_valid()) |
380 return; | 379 return; |
381 match.search_terms_args->bookmark_bar_pinned = | 380 match.search_terms_args->bookmark_bar_pinned = |
382 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); | 381 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
383 match.RecordAdditionalInfo(kRelevanceFromServerKey, | 382 match.RecordAdditionalInfo(kRelevanceFromServerKey, |
384 result.relevance_from_server() ? kTrue : kFalse); | 383 result.relevance_from_server() ? kTrue : kFalse); |
385 match.RecordAdditionalInfo(kShouldPrefetchKey, | 384 match.RecordAdditionalInfo(kShouldPrefetchKey, |
386 result.should_prefetch() ? kTrue : kFalse); | 385 result.should_prefetch() ? kTrue : kFalse); |
387 SetDeletionURL(result.deletion_url(), &match); | 386 SetDeletionURL(result.deletion_url(), &match); |
388 if (mark_as_deletable) | 387 if (mark_as_deletable) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 should_prefetch ? kTrue : kFalse); | 436 should_prefetch ? kTrue : kFalse); |
438 if (should_prefetch) | 437 if (should_prefetch) |
439 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); | 438 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); |
440 } | 439 } |
441 } | 440 } |
442 } | 441 } |
443 } | 442 } |
444 | 443 |
445 bool BaseSearchProvider::ParseSuggestResults( | 444 bool BaseSearchProvider::ParseSuggestResults( |
446 const base::Value& root_val, | 445 const base::Value& root_val, |
| 446 const AutocompleteInput& input, |
447 int default_result_relevance, | 447 int default_result_relevance, |
448 bool is_keyword_result, | 448 bool is_keyword_result, |
449 SearchSuggestionParser::Results* results) { | 449 SearchSuggestionParser::Results* results) { |
450 if (!SearchSuggestionParser::ParseSuggestResults( | 450 if (!SearchSuggestionParser::ParseSuggestResults( |
451 root_val, GetInput(is_keyword_result), | 451 root_val, input, |
452 ChromeAutocompleteSchemeClassifier(profile_), default_result_relevance, | 452 ChromeAutocompleteSchemeClassifier(profile_), default_result_relevance, |
453 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), | 453 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), |
454 is_keyword_result, results)) | 454 is_keyword_result, results)) |
455 return false; | 455 return false; |
456 | 456 |
457 BitmapFetcherService* image_service = | 457 BitmapFetcherService* image_service = |
458 BitmapFetcherServiceFactory::GetForBrowserContext(profile_); | 458 BitmapFetcherServiceFactory::GetForBrowserContext(profile_); |
459 DCHECK(image_service); | 459 DCHECK(image_service); |
460 for (std::vector<GURL>::const_iterator it = | 460 for (std::vector<GURL>::const_iterator it = |
461 results->answers_image_urls.begin(); | 461 results->answers_image_urls.begin(); |
462 it != results->answers_image_urls.end(); ++it) | 462 it != results->answers_image_urls.end(); ++it) |
463 image_service->Prefetch(*it); | 463 image_service->Prefetch(*it); |
464 | 464 |
465 field_trial_triggered_ |= results->field_trial_triggered; | 465 field_trial_triggered_ |= results->field_trial_triggered; |
466 field_trial_triggered_in_session_ |= results->field_trial_triggered; | 466 field_trial_triggered_in_session_ |= results->field_trial_triggered; |
467 return true; | 467 return true; |
468 } | 468 } |
469 | 469 |
470 void BaseSearchProvider::ModifyProviderInfo( | |
471 metrics::OmniboxEventProto_ProviderInfo* provider_info) const { | |
472 } | |
473 | |
474 void BaseSearchProvider::DeleteMatchFromMatches( | 470 void BaseSearchProvider::DeleteMatchFromMatches( |
475 const AutocompleteMatch& match) { | 471 const AutocompleteMatch& match) { |
476 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { | 472 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { |
477 // Find the desired match to delete by checking the type and contents. | 473 // Find the desired match to delete by checking the type and contents. |
478 // We can't check the destination URL, because the autocomplete controller | 474 // We can't check the destination URL, because the autocomplete controller |
479 // may have reformulated that. Not that while checking for matching | 475 // may have reformulated that. Not that while checking for matching |
480 // contents works for personalized suggestions, if more match types gain | 476 // contents works for personalized suggestions, if more match types gain |
481 // deletion support, this algorithm may need to be re-examined. | 477 // deletion support, this algorithm may need to be re-examined. |
482 if (i->contents == match.contents && i->type == match.type) { | 478 if (i->contents == match.contents && i->type == match.type) { |
483 matches_.erase(i); | 479 matches_.erase(i); |
484 break; | 480 break; |
485 } | 481 } |
486 } | 482 } |
487 } | 483 } |
488 | 484 |
489 void BaseSearchProvider::OnDeletionComplete( | 485 void BaseSearchProvider::OnDeletionComplete( |
490 bool success, SuggestionDeletionHandler* handler) { | 486 bool success, SuggestionDeletionHandler* handler) { |
491 RecordDeletionResult(success); | 487 base::RecordAction(success ? uma_deletion_success_ : uma_deletion_failure_); |
492 SuggestionDeletionHandlers::iterator it = std::find( | 488 SuggestionDeletionHandlers::iterator it = std::find( |
493 deletion_handlers_.begin(), deletion_handlers_.end(), handler); | 489 deletion_handlers_.begin(), deletion_handlers_.end(), handler); |
494 DCHECK(it != deletion_handlers_.end()); | 490 DCHECK(it != deletion_handlers_.end()); |
495 deletion_handlers_.erase(it); | 491 deletion_handlers_.erase(it); |
496 } | 492 } |
OLD | NEW |