Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ntp_snippets/remote/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 309 << "Unknown value for " << kPersonalizationName << ": " | 309 << "Unknown value for " << kPersonalizationName << ": " |
| 310 << personalization; | 310 << personalization; |
| 311 } | 311 } |
| 312 } | 312 } |
| 313 | 313 |
| 314 NTPSnippetsFetcher::~NTPSnippetsFetcher() { | 314 NTPSnippetsFetcher::~NTPSnippetsFetcher() { |
| 315 if (waiting_for_refresh_token_) | 315 if (waiting_for_refresh_token_) |
| 316 token_service_->RemoveObserver(this); | 316 token_service_->RemoveObserver(this); |
| 317 } | 317 } |
| 318 | 318 |
| 319 void NTPSnippetsFetcher::SetCallback( | |
| 320 const SnippetsAvailableCallback& callback) { | |
| 321 snippets_available_callback_ = callback; | |
| 322 } | |
| 323 | |
| 324 void NTPSnippetsFetcher::FetchSnippets(const Params& params) { | 319 void NTPSnippetsFetcher::FetchSnippets(const Params& params) { |
| 325 if (!DemandQuotaForRequest(params.interactive_request)) { | 320 if (!DemandQuotaForRequest(params.interactive_request)) { |
| 326 FetchFinished(OptionalFetchedCategories(), | 321 FetchFinished(OptionalFetchedCategories(), |
| 327 params.interactive_request | 322 params.interactive_request |
| 328 ? FetchResult::INTERACTIVE_QUOTA_ERROR | 323 ? FetchResult::INTERACTIVE_QUOTA_ERROR |
| 329 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, | 324 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, |
| 330 /*extra_message=*/std::string()); | 325 /*extra_message=*/std::string()); |
| 331 return; | 326 return; |
| 332 } | 327 } |
| 333 | 328 |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 731 } | 726 } |
| 732 | 727 |
| 733 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { | 728 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { |
| 734 LOG(WARNING) << "Received invalid JSON (" << error | 729 LOG(WARNING) << "Received invalid JSON (" << error |
| 735 << "): " << last_fetch_json_; | 730 << "): " << last_fetch_json_; |
| 736 FetchFinished( | 731 FetchFinished( |
| 737 OptionalFetchedCategories(), FetchResult::JSON_PARSE_ERROR, | 732 OptionalFetchedCategories(), FetchResult::JSON_PARSE_ERROR, |
| 738 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); | 733 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); |
| 739 } | 734 } |
| 740 | 735 |
| 736 void NTPSnippetsFetcher::FilterCategories(OptionalFetchedCategories* fetched) | |
| 737 const { | |
| 738 if (!fetched->has_value()) | |
| 739 return; | |
| 740 if (!params_.exclusive_category.has_value()) | |
| 741 return; | |
| 742 FetchedCategoriesVector* categories = &fetched->value(); | |
| 743 Category exclusive = params_.exclusive_category.value(); | |
| 744 auto category_it = | |
| 745 std::find_if(categories->begin(), categories->end(), | |
| 746 [exclusive](const FetchedCategory& c) -> bool { | |
| 747 return c.category == exclusive; | |
| 748 }); | |
| 749 if (category_it == categories->end()) { | |
| 750 categories->clear(); | |
| 751 return; | |
| 752 } | |
| 753 categories->erase(categories->begin(), category_it); | |
| 754 categories->erase(category_it + 1, categories->end()); | |
| 755 } | |
| 756 | |
| 741 void NTPSnippetsFetcher::FetchFinished( | 757 void NTPSnippetsFetcher::FetchFinished( |
| 742 OptionalFetchedCategories fetched_categories, | 758 OptionalFetchedCategories fetched_categories, |
| 743 FetchResult result, | 759 FetchResult result, |
| 744 const std::string& extra_message) { | 760 const std::string& extra_message) { |
| 745 DCHECK(result == FetchResult::SUCCESS || !fetched_categories); | 761 DCHECK(result == FetchResult::SUCCESS || !fetched_categories); |
| 746 last_status_ = FetchResultToString(result) + extra_message; | 762 last_status_ = FetchResultToString(result) + extra_message; |
| 747 | 763 |
| 764 // TODO(fhorschig): Filter unwanted categories by sending the exclusive | |
| 765 // category to the backend. As soon as backends support this, there is no | |
| 766 // reason to overfetch and filter here. | |
| 767 FilterCategories(&fetched_categories); | |
| 768 | |
| 748 // Don't record FetchTimes if the result indicates that a precondition | 769 // Don't record FetchTimes if the result indicates that a precondition |
| 749 // failed and we never actually sent a network request | 770 // failed and we never actually sent a network request |
| 750 if (!IsFetchPreconditionFailed(result)) { | 771 if (!IsFetchPreconditionFailed(result)) { |
| 751 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", | 772 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", |
| 752 tick_clock_->NowTicks() - fetch_start_time_); | 773 tick_clock_->NowTicks() - fetch_start_time_); |
| 753 } | 774 } |
| 754 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 775 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 755 static_cast<int>(result), | 776 static_cast<int>(result), |
| 756 static_cast<int>(FetchResult::RESULT_MAX)); | 777 static_cast<int>(FetchResult::RESULT_MAX)); |
| 757 | 778 |
| 758 DVLOG(1) << "Fetch finished: " << last_status_; | 779 DVLOG(1) << "Fetch finished: " << last_status_; |
| 759 if (!snippets_available_callback_.is_null()) | 780 if (!params_.snippets_available_callback.is_null()) { |
| 760 snippets_available_callback_.Run(std::move(fetched_categories)); | 781 std::move(params_.snippets_available_callback).Run( |
|
vitaliii
2016/11/01 23:29:57
Why do you need this external |std::move|?
fhorschig
2016/11/02 05:05:27
It's a "OnceCallback" that is consumed upon call:
| |
| 782 std::move(fetched_categories)); | |
| 783 } | |
| 761 } | 784 } |
| 762 | 785 |
| 763 bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) { | 786 bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) { |
| 764 switch (user_classifier_->GetUserClass()) { | 787 switch (user_classifier_->GetUserClass()) { |
| 765 case UserClassifier::UserClass::RARE_NTP_USER: | 788 case UserClassifier::UserClass::RARE_NTP_USER: |
| 766 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( | 789 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( |
| 767 interactive_request); | 790 interactive_request); |
| 768 case UserClassifier::UserClass::ACTIVE_NTP_USER: | 791 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
| 769 return request_throttler_active_ntp_user_.DemandQuotaForRequest( | 792 return request_throttler_active_ntp_user_.DemandQuotaForRequest( |
| 770 interactive_request); | 793 interactive_request); |
| 771 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: | 794 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 772 return request_throttler_active_suggestions_consumer_ | 795 return request_throttler_active_suggestions_consumer_ |
| 773 .DemandQuotaForRequest(interactive_request); | 796 .DemandQuotaForRequest(interactive_request); |
| 774 } | 797 } |
| 775 NOTREACHED(); | 798 NOTREACHED(); |
| 776 return false; | 799 return false; |
| 777 } | 800 } |
| 778 | 801 |
| 779 } // namespace ntp_snippets | 802 } // namespace ntp_snippets |
| OLD | NEW |