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/prefs/pref_registry_simple.h" | 8 #include "base/prefs/pref_registry_simple.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 this); | 87 this); |
88 } | 88 } |
89 | 89 |
90 // BaseSearchProvider --------------------------------------------------------- | 90 // BaseSearchProvider --------------------------------------------------------- |
91 | 91 |
92 // static | 92 // static |
93 const int BaseSearchProvider::kDefaultProviderURLFetcherID = 1; | 93 const int BaseSearchProvider::kDefaultProviderURLFetcherID = 1; |
94 const int BaseSearchProvider::kKeywordProviderURLFetcherID = 2; | 94 const int BaseSearchProvider::kKeywordProviderURLFetcherID = 2; |
95 const int BaseSearchProvider::kDeletionURLFetcherID = 3; | 95 const int BaseSearchProvider::kDeletionURLFetcherID = 3; |
96 | 96 |
97 BaseSearchProvider::BaseSearchProvider(AutocompleteProviderListener* listener, | 97 BaseSearchProvider::BaseSearchProvider(TemplateURLService* template_url_service, |
98 TemplateURLService* template_url_service, | |
99 Profile* profile, | 98 Profile* profile, |
100 AutocompleteProvider::Type type) | 99 AutocompleteProvider::Type type) |
101 : AutocompleteProvider(type), | 100 : AutocompleteProvider(type), |
102 listener_(listener), | |
103 template_url_service_(template_url_service), | 101 template_url_service_(template_url_service), |
104 profile_(profile), | 102 profile_(profile), |
105 field_trial_triggered_(false), | 103 field_trial_triggered_(false), |
106 field_trial_triggered_in_session_(false), | 104 field_trial_triggered_in_session_(false) { |
107 suggest_results_pending_(0) { | |
108 } | 105 } |
109 | 106 |
110 // static | 107 // static |
111 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { | 108 bool BaseSearchProvider::ShouldPrefetch(const AutocompleteMatch& match) { |
112 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; | 109 return match.GetAdditionalInfo(kShouldPrefetchKey) == kTrue; |
113 } | 110 } |
114 | 111 |
115 // static | 112 // static |
116 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( | 113 AutocompleteMatch BaseSearchProvider::CreateSearchSuggestion( |
117 const base::string16& suggestion, | 114 const base::string16& suggestion, |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 if (service == NULL || | 358 if (service == NULL || |
362 !service->IsSyncEnabledAndLoggedIn() || | 359 !service->IsSyncEnabledAndLoggedIn() || |
363 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( | 360 !sync_prefs.GetPreferredDataTypes(syncer::UserTypes()).Has( |
364 syncer::PROXY_TABS) || | 361 syncer::PROXY_TABS) || |
365 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) | 362 service->GetEncryptedDataTypes().Has(syncer::SESSIONS)) |
366 return false; | 363 return false; |
367 | 364 |
368 return true; | 365 return true; |
369 } | 366 } |
370 | 367 |
371 void BaseSearchProvider::OnURLFetchComplete(const net::URLFetcher* source) { | |
372 DCHECK(!done_); | |
373 suggest_results_pending_--; | |
374 DCHECK_GE(suggest_results_pending_, 0); // Should never go negative. | |
375 | |
376 const bool is_keyword = IsKeywordFetcher(source); | |
377 | |
378 // Ensure the request succeeded and that the provider used is still available. | |
379 // A verbatim match cannot be generated without this provider, causing errors. | |
380 const bool request_succeeded = | |
381 source->GetStatus().is_success() && (source->GetResponseCode() == 200) && | |
382 GetTemplateURL(is_keyword); | |
383 | |
384 LogFetchComplete(request_succeeded, is_keyword); | |
385 | |
386 bool results_updated = false; | |
387 if (request_succeeded) { | |
388 std::string json_data = SearchSuggestionParser::ExtractJsonData(source); | |
389 scoped_ptr<base::Value> data( | |
390 SearchSuggestionParser::DeserializeJsonData(json_data)); | |
391 if (data && StoreSuggestionResponse(json_data, *data.get())) | |
392 return; | |
393 | |
394 results_updated = data.get() && ParseSuggestResults( | |
395 *data.get(), is_keyword, GetResultsToFill(is_keyword)); | |
396 } | |
397 | |
398 UpdateMatches(); | |
399 if (done_ || results_updated) | |
400 listener_->OnProviderUpdate(results_updated); | |
401 } | |
402 | |
403 void BaseSearchProvider::AddMatchToMap( | 368 void BaseSearchProvider::AddMatchToMap( |
404 const SearchSuggestionParser::SuggestResult& result, | 369 const SearchSuggestionParser::SuggestResult& result, |
405 const std::string& metadata, | 370 const std::string& metadata, |
406 int accepted_suggestion, | 371 int accepted_suggestion, |
407 bool mark_as_deletable, | 372 bool mark_as_deletable, |
408 MatchMap* map) { | 373 MatchMap* map) { |
409 AutocompleteMatch match = CreateSearchSuggestion( | 374 AutocompleteMatch match = CreateSearchSuggestion( |
410 this, GetInput(result.from_keyword_provider()), result, | 375 this, GetInput(result.from_keyword_provider()), result, |
411 GetTemplateURL(result.from_keyword_provider()), | 376 GetTemplateURL(result.from_keyword_provider()), |
412 template_url_service_->search_terms_data(), accepted_suggestion, | 377 template_url_service_->search_terms_data(), accepted_suggestion, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 should_prefetch ? kTrue : kFalse); | 437 should_prefetch ? kTrue : kFalse); |
473 if (should_prefetch) | 438 if (should_prefetch) |
474 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); | 439 i.first->second.RecordAdditionalInfo(kSuggestMetadataKey, metadata); |
475 } | 440 } |
476 } | 441 } |
477 } | 442 } |
478 } | 443 } |
479 | 444 |
480 bool BaseSearchProvider::ParseSuggestResults( | 445 bool BaseSearchProvider::ParseSuggestResults( |
481 const base::Value& root_val, | 446 const base::Value& root_val, |
| 447 int default_result_relevance, |
482 bool is_keyword_result, | 448 bool is_keyword_result, |
483 SearchSuggestionParser::Results* results) { | 449 SearchSuggestionParser::Results* results) { |
484 if (!SearchSuggestionParser::ParseSuggestResults( | 450 if (!SearchSuggestionParser::ParseSuggestResults( |
485 root_val, GetInput(is_keyword_result), | 451 root_val, GetInput(is_keyword_result), |
486 ChromeAutocompleteSchemeClassifier(profile_), | 452 ChromeAutocompleteSchemeClassifier(profile_), default_result_relevance, |
487 GetDefaultResultRelevance(), | |
488 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), | 453 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), |
489 is_keyword_result, results)) | 454 is_keyword_result, results)) |
490 return false; | 455 return false; |
491 | 456 |
492 BitmapFetcherService* image_service = | 457 BitmapFetcherService* image_service = |
493 BitmapFetcherServiceFactory::GetForBrowserContext(profile_); | 458 BitmapFetcherServiceFactory::GetForBrowserContext(profile_); |
494 DCHECK(image_service); | 459 DCHECK(image_service); |
495 for (std::vector<GURL>::const_iterator it = | 460 for (std::vector<GURL>::const_iterator it = |
496 results->answers_image_urls.begin(); | 461 results->answers_image_urls.begin(); |
497 it != results->answers_image_urls.end(); ++it) | 462 it != results->answers_image_urls.end(); ++it) |
498 image_service->Prefetch(*it); | 463 image_service->Prefetch(*it); |
499 | 464 |
500 field_trial_triggered_ |= results->field_trial_triggered; | 465 field_trial_triggered_ |= results->field_trial_triggered; |
501 field_trial_triggered_in_session_ |= results->field_trial_triggered; | 466 field_trial_triggered_in_session_ |= results->field_trial_triggered; |
502 SortResults(is_keyword_result, results); | |
503 return true; | 467 return true; |
504 } | 468 } |
505 | 469 |
506 void BaseSearchProvider::SortResults(bool is_keyword, | |
507 SearchSuggestionParser::Results* results) { | |
508 } | |
509 | |
510 bool BaseSearchProvider::StoreSuggestionResponse( | |
511 const std::string& json_data, | |
512 const base::Value& parsed_data) { | |
513 return false; | |
514 } | |
515 | |
516 void BaseSearchProvider::ModifyProviderInfo( | 470 void BaseSearchProvider::ModifyProviderInfo( |
517 metrics::OmniboxEventProto_ProviderInfo* provider_info) const { | 471 metrics::OmniboxEventProto_ProviderInfo* provider_info) const { |
518 } | 472 } |
519 | 473 |
520 void BaseSearchProvider::DeleteMatchFromMatches( | 474 void BaseSearchProvider::DeleteMatchFromMatches( |
521 const AutocompleteMatch& match) { | 475 const AutocompleteMatch& match) { |
522 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { | 476 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) { |
523 // Find the desired match to delete by checking the type and contents. | 477 // Find the desired match to delete by checking the type and contents. |
524 // We can't check the destination URL, because the autocomplete controller | 478 // We can't check the destination URL, because the autocomplete controller |
525 // may have reformulated that. Not that while checking for matching | 479 // may have reformulated that. Not that while checking for matching |
526 // contents works for personalized suggestions, if more match types gain | 480 // contents works for personalized suggestions, if more match types gain |
527 // deletion support, this algorithm may need to be re-examined. | 481 // deletion support, this algorithm may need to be re-examined. |
528 if (i->contents == match.contents && i->type == match.type) { | 482 if (i->contents == match.contents && i->type == match.type) { |
529 matches_.erase(i); | 483 matches_.erase(i); |
530 break; | 484 break; |
531 } | 485 } |
532 } | 486 } |
533 } | 487 } |
534 | 488 |
535 void BaseSearchProvider::OnDeletionComplete( | 489 void BaseSearchProvider::OnDeletionComplete( |
536 bool success, SuggestionDeletionHandler* handler) { | 490 bool success, SuggestionDeletionHandler* handler) { |
537 RecordDeletionResult(success); | 491 RecordDeletionResult(success); |
538 SuggestionDeletionHandlers::iterator it = std::find( | 492 SuggestionDeletionHandlers::iterator it = std::find( |
539 deletion_handlers_.begin(), deletion_handlers_.end(), handler); | 493 deletion_handlers_.begin(), deletion_handlers_.end(), handler); |
540 DCHECK(it != deletion_handlers_.end()); | 494 DCHECK(it != deletion_handlers_.end()); |
541 deletion_handlers_.erase(it); | 495 deletion_handlers_.erase(it); |
542 } | 496 } |
OLD | NEW |