Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(373)

Side by Side Diff: components/omnibox/base_search_provider.cc

Issue 669573005: Add a class to parse answer json. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix case on "copyright" Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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 && !more_relevant_match.answer) {
430 more_relevant_match.answer_type.empty()) {
431 more_relevant_match.answer_type = less_relevant_match.answer_type; 432 more_relevant_match.answer_type = less_relevant_match.answer_type;
432 more_relevant_match.answer_contents = less_relevant_match.answer_contents; 433 more_relevant_match.answer_contents = less_relevant_match.answer_contents;
434 more_relevant_match.answer =
435 SuggestionAnswer::copy(less_relevant_match.answer.get());
433 } 436 }
434 } 437 }
435 } 438 }
436 439
437 bool BaseSearchProvider::ParseSuggestResults( 440 bool BaseSearchProvider::ParseSuggestResults(
438 const base::Value& root_val, 441 const base::Value& root_val,
439 int default_result_relevance, 442 int default_result_relevance,
440 bool is_keyword_result, 443 bool is_keyword_result,
441 SearchSuggestionParser::Results* results) { 444 SearchSuggestionParser::Results* results) {
442 if (!SearchSuggestionParser::ParseSuggestResults( 445 if (!SearchSuggestionParser::ParseSuggestResults(
443 root_val, GetInput(is_keyword_result), 446 root_val, GetInput(is_keyword_result),
444 client_->SchemeClassifier(), default_result_relevance, 447 client_->SchemeClassifier(), default_result_relevance,
445 client_->AcceptLanguages(), is_keyword_result, results)) 448 client_->AcceptLanguages(), is_keyword_result, results))
446 return false; 449 return false;
447 450
448 for (std::vector<GURL>::const_iterator it = 451 for (const GURL& url : results->answers_image_urls)
449 results->answers_image_urls.begin(); 452 client_->PrefetchImage(url);
450 it != results->answers_image_urls.end(); ++it)
451 client_->PrefetchImage(*it);
452 453
453 field_trial_triggered_ |= results->field_trial_triggered; 454 field_trial_triggered_ |= results->field_trial_triggered;
454 field_trial_triggered_in_session_ |= results->field_trial_triggered; 455 field_trial_triggered_in_session_ |= results->field_trial_triggered;
455 return true; 456 return true;
456 } 457 }
457 458
458 void BaseSearchProvider::DeleteMatchFromMatches( 459 void BaseSearchProvider::DeleteMatchFromMatches(
459 const AutocompleteMatch& match) { 460 const AutocompleteMatch& match) {
460 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { 461 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) {
461 // Find the desired match to delete by checking the type and contents. 462 // Find the desired match to delete by checking the type and contents.
462 // We can't check the destination URL, because the autocomplete controller 463 // We can't check the destination URL, because the autocomplete controller
463 // may have reformulated that. Not that while checking for matching 464 // may have reformulated that. Not that while checking for matching
464 // contents works for personalized suggestions, if more match types gain 465 // contents works for personalized suggestions, if more match types gain
465 // deletion support, this algorithm may need to be re-examined. 466 // deletion support, this algorithm may need to be re-examined.
466 if (i->contents == match.contents && i->type == match.type) { 467 if (i->contents == match.contents && i->type == match.type) {
467 matches_.erase(i); 468 matches_.erase(i);
468 break; 469 break;
469 } 470 }
470 } 471 }
471 } 472 }
472 473
473 void BaseSearchProvider::OnDeletionComplete( 474 void BaseSearchProvider::OnDeletionComplete(
474 bool success, SuggestionDeletionHandler* handler) { 475 bool success, SuggestionDeletionHandler* handler) {
475 RecordDeletionResult(success); 476 RecordDeletionResult(success);
476 SuggestionDeletionHandlers::iterator it = std::find( 477 SuggestionDeletionHandlers::iterator it = std::find(
477 deletion_handlers_.begin(), deletion_handlers_.end(), handler); 478 deletion_handlers_.begin(), deletion_handlers_.end(), handler);
478 DCHECK(it != deletion_handlers_.end()); 479 DCHECK(it != deletion_handlers_.end());
479 deletion_handlers_.erase(it); 480 deletion_handlers_.erase(it);
480 } 481 }
OLDNEW
« no previous file with comments | « components/omnibox/autocomplete_match.cc ('k') | components/omnibox/base_search_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698