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 "components/omnibox/base_search_provider.h" | 5 #include "components/omnibox/base_search_provider.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "components/metrics/proto/omnibox_event.pb.h" | 10 #include "components/metrics/proto/omnibox_event.pb.h" |
11 #include "components/metrics/proto/omnibox_input_type.pb.h" | 11 #include "components/metrics/proto/omnibox_input_type.pb.h" |
12 #include "components/omnibox/autocomplete_provider_client.h" | 12 #include "components/omnibox/autocomplete_provider_client.h" |
13 #include "components/omnibox/autocomplete_provider_listener.h" | 13 #include "components/omnibox/autocomplete_provider_listener.h" |
14 #include "components/omnibox/omnibox_field_trial.h" | 14 #include "components/omnibox/omnibox_field_trial.h" |
15 #include "components/omnibox/suggestion_answer.h" | |
15 #include "components/search_engines/template_url.h" | 16 #include "components/search_engines/template_url.h" |
16 #include "components/search_engines/template_url_prepopulate_data.h" | 17 #include "components/search_engines/template_url_prepopulate_data.h" |
17 #include "components/search_engines/template_url_service.h" | 18 #include "components/search_engines/template_url_service.h" |
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
19 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 21 #include "net/url_request/url_fetcher_delegate.h" |
21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
22 | 23 |
23 using metrics::OmniboxEventProto; | 24 using metrics::OmniboxEventProto; |
24 | 25 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 AutocompleteMatchType::Type type, | 105 AutocompleteMatchType::Type type, |
105 bool from_keyword_provider, | 106 bool from_keyword_provider, |
106 const TemplateURL* template_url, | 107 const TemplateURL* template_url, |
107 const SearchTermsData& search_terms_data) { | 108 const SearchTermsData& search_terms_data) { |
108 // These calls use a number of default values. For instance, they assume | 109 // These calls use a number of default values. For instance, they assume |
109 // that if this match is from a keyword provider, then the user is in keyword | 110 // that if this match is from a keyword provider, then the user is in keyword |
110 // mode. They also assume the caller knows what it's doing and we set | 111 // mode. They also assume the caller knows what it's doing and we set |
111 // this match to look as if it was received/created synchronously. | 112 // this match to look as if it was received/created synchronously. |
112 SearchSuggestionParser::SuggestResult suggest_result( | 113 SearchSuggestionParser::SuggestResult suggest_result( |
113 suggestion, type, suggestion, base::string16(), base::string16(), | 114 suggestion, type, suggestion, base::string16(), base::string16(), |
114 base::string16(), base::string16(), std::string(), std::string(), | 115 base::string16(), base::string16(), nullptr, std::string(), |
115 from_keyword_provider, 0, false, false, base::string16()); | 116 std::string(), from_keyword_provider, 0, false, false, base::string16()); |
116 suggest_result.set_received_after_last_keystroke(false); | 117 suggest_result.set_received_after_last_keystroke(false); |
117 return CreateSearchSuggestion( | 118 return CreateSearchSuggestion( |
118 NULL, AutocompleteInput(), from_keyword_provider, suggest_result, | 119 NULL, AutocompleteInput(), from_keyword_provider, suggest_result, |
119 template_url, search_terms_data, 0, false); | 120 template_url, search_terms_data, 0, false); |
120 } | 121 } |
121 | 122 |
122 void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) { | 123 void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) { |
123 DCHECK(match.deletable); | 124 DCHECK(match.deletable); |
124 if (!match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey).empty()) { | 125 if (!match.GetAdditionalInfo(BaseSearchProvider::kDeletionUrlKey).empty()) { |
125 deletion_handlers_.push_back(new SuggestionDeletionHandler( | 126 deletion_handlers_.push_back(new SuggestionDeletionHandler( |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false, | 202 AutocompleteMatch match(autocomplete_provider, suggestion.relevance(), false, |
202 suggestion.type()); | 203 suggestion.type()); |
203 | 204 |
204 if (!template_url) | 205 if (!template_url) |
205 return match; | 206 return match; |
206 match.keyword = template_url->keyword(); | 207 match.keyword = template_url->keyword(); |
207 match.contents = suggestion.match_contents(); | 208 match.contents = suggestion.match_contents(); |
208 match.contents_class = suggestion.match_contents_class(); | 209 match.contents_class = suggestion.match_contents_class(); |
209 match.answer_contents = suggestion.answer_contents(); | 210 match.answer_contents = suggestion.answer_contents(); |
210 match.answer_type = suggestion.answer_type(); | 211 match.answer_type = suggestion.answer_type(); |
212 match.answer = SuggestionAnswer::Copy(suggestion.answer()); | |
211 if (suggestion.type() == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE) { | 213 if (suggestion.type() == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE) { |
212 match.RecordAdditionalInfo( | 214 match.RecordAdditionalInfo( |
213 kACMatchPropertyInputText, base::UTF16ToUTF8(input.text())); | 215 kACMatchPropertyInputText, base::UTF16ToUTF8(input.text())); |
214 match.RecordAdditionalInfo( | 216 match.RecordAdditionalInfo( |
215 kACMatchPropertyContentsPrefix, | 217 kACMatchPropertyContentsPrefix, |
216 base::UTF16ToUTF8(suggestion.match_contents_prefix())); | 218 base::UTF16ToUTF8(suggestion.match_contents_prefix())); |
217 match.RecordAdditionalInfo( | 219 match.RecordAdditionalInfo( |
218 kACMatchPropertyContentsStartIndex, | 220 kACMatchPropertyContentsStartIndex, |
219 static_cast<int>( | 221 static_cast<int>( |
220 suggestion.suggestion().length() - match.contents.length())); | 222 suggestion.suggestion().length() - match.contents.length())); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 if (should_prefetch) | 421 if (should_prefetch) |
420 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); | 422 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); |
421 } | 423 } |
422 } | 424 } |
423 // Copy over answer data from lower-ranking item, if necessary. | 425 // Copy over answer data from lower-ranking item, if necessary. |
424 // This depends on the lower-ranking item always being added last - see | 426 // This depends on the lower-ranking item always being added last - see |
425 // use of push_back above. | 427 // use of push_back above. |
426 AutocompleteMatch& more_relevant_match = i.first->second; | 428 AutocompleteMatch& more_relevant_match = i.first->second; |
427 const AutocompleteMatch& less_relevant_match = | 429 const AutocompleteMatch& less_relevant_match = |
428 more_relevant_match.duplicate_matches.back(); | 430 more_relevant_match.duplicate_matches.back(); |
429 if (!less_relevant_match.answer_type.empty() && | 431 if (less_relevant_match.answer.get() && |
430 more_relevant_match.answer_type.empty()) { | 432 !more_relevant_match.answer.get()) { |
Peter Kasting
2014/10/30 03:43:29
Nit: No get() on scoped_ptrs used as bools
Justin Donnelly
2014/10/30 19:23:13
Done.
| |
431 more_relevant_match.answer_type = less_relevant_match.answer_type; | 433 more_relevant_match.answer_type = less_relevant_match.answer_type; |
432 more_relevant_match.answer_contents = less_relevant_match.answer_contents; | 434 more_relevant_match.answer_contents = less_relevant_match.answer_contents; |
435 more_relevant_match.answer = | |
436 SuggestionAnswer::Copy(less_relevant_match.answer.get()); | |
433 } | 437 } |
434 } | 438 } |
435 } | 439 } |
436 | 440 |
437 bool BaseSearchProvider::ParseSuggestResults( | 441 bool BaseSearchProvider::ParseSuggestResults( |
438 const base::Value& root_val, | 442 const base::Value& root_val, |
439 int default_result_relevance, | 443 int default_result_relevance, |
440 bool is_keyword_result, | 444 bool is_keyword_result, |
441 SearchSuggestionParser::Results* results) { | 445 SearchSuggestionParser::Results* results) { |
442 if (!SearchSuggestionParser::ParseSuggestResults( | 446 if (!SearchSuggestionParser::ParseSuggestResults( |
443 root_val, GetInput(is_keyword_result), | 447 root_val, GetInput(is_keyword_result), |
444 client_->SchemeClassifier(), default_result_relevance, | 448 client_->SchemeClassifier(), default_result_relevance, |
445 client_->AcceptLanguages(), is_keyword_result, results)) | 449 client_->AcceptLanguages(), is_keyword_result, results)) |
446 return false; | 450 return false; |
447 | 451 |
448 for (std::vector<GURL>::const_iterator it = | 452 for (const GURL& url : results->answers_image_urls) |
449 results->answers_image_urls.begin(); | 453 client_->PrefetchImage(url); |
450 it != results->answers_image_urls.end(); ++it) | |
451 client_->PrefetchImage(*it); | |
452 | 454 |
453 field_trial_triggered_ |= results->field_trial_triggered; | 455 field_trial_triggered_ |= results->field_trial_triggered; |
454 field_trial_triggered_in_session_ |= results->field_trial_triggered; | 456 field_trial_triggered_in_session_ |= results->field_trial_triggered; |
455 return true; | 457 return true; |
456 } | 458 } |
457 | 459 |
458 void BaseSearchProvider::DeleteMatchFromMatches( | 460 void BaseSearchProvider::DeleteMatchFromMatches( |
459 const AutocompleteMatch& match) { | 461 const AutocompleteMatch& match) { |
460 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { | 462 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { |
461 // Find the desired match to delete by checking the type and contents. | 463 // Find the desired match to delete by checking the type and contents. |
462 // We can't check the destination URL, because the autocomplete controller | 464 // We can't check the destination URL, because the autocomplete controller |
463 // may have reformulated that. Not that while checking for matching | 465 // may have reformulated that. Not that while checking for matching |
464 // contents works for personalized suggestions, if more match types gain | 466 // contents works for personalized suggestions, if more match types gain |
465 // deletion support, this algorithm may need to be re-examined. | 467 // deletion support, this algorithm may need to be re-examined. |
466 if (i->contents == match.contents && i->type == match.type) { | 468 if (i->contents == match.contents && i->type == match.type) { |
467 matches_.erase(i); | 469 matches_.erase(i); |
468 break; | 470 break; |
469 } | 471 } |
470 } | 472 } |
471 } | 473 } |
472 | 474 |
473 void BaseSearchProvider::OnDeletionComplete( | 475 void BaseSearchProvider::OnDeletionComplete( |
474 bool success, SuggestionDeletionHandler* handler) { | 476 bool success, SuggestionDeletionHandler* handler) { |
475 RecordDeletionResult(success); | 477 RecordDeletionResult(success); |
476 SuggestionDeletionHandlers::iterator it = std::find( | 478 SuggestionDeletionHandlers::iterator it = std::find( |
477 deletion_handlers_.begin(), deletion_handlers_.end(), handler); | 479 deletion_handlers_.begin(), deletion_handlers_.end(), handler); |
478 DCHECK(it != deletion_handlers_.end()); | 480 DCHECK(it != deletion_handlers_.end()); |
479 deletion_handlers_.erase(it); | 481 deletion_handlers_.erase(it); |
480 } | 482 } |
OLD | NEW |