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( | 319 void NTPSnippetsFetcher::FetchSnippets(const Params& params, |
320 const SnippetsAvailableCallback& callback) { | 320 SnippetsAvailableCallback callback) { |
321 snippets_available_callback_ = callback; | |
322 } | |
323 | |
324 void NTPSnippetsFetcher::FetchSnippets(const Params& params) { | |
325 if (!DemandQuotaForRequest(params.interactive_request)) { | 321 if (!DemandQuotaForRequest(params.interactive_request)) { |
326 FetchFinished(OptionalFetchedCategories(), | 322 FetchFinished(OptionalFetchedCategories(), |
327 params.interactive_request | 323 params.interactive_request |
328 ? FetchResult::INTERACTIVE_QUOTA_ERROR | 324 ? FetchResult::INTERACTIVE_QUOTA_ERROR |
329 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, | 325 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, |
330 /*extra_message=*/std::string()); | 326 /*extra_message=*/std::string()); |
331 return; | 327 return; |
332 } | 328 } |
333 | 329 |
334 params_ = params; | 330 params_ = params; |
335 fetch_start_time_ = tick_clock_->NowTicks(); | 331 fetch_start_time_ = tick_clock_->NowTicks(); |
336 | 332 |
| 333 snippets_available_callback_ = std::move(callback); |
| 334 |
337 bool use_authentication = UsesAuthentication(); | 335 bool use_authentication = UsesAuthentication(); |
338 if (use_authentication && signin_manager_->IsAuthenticated()) { | 336 if (use_authentication && signin_manager_->IsAuthenticated()) { |
339 // Signed-in: get OAuth token --> fetch snippets. | 337 // Signed-in: get OAuth token --> fetch snippets. |
340 oauth_token_retried_ = false; | 338 oauth_token_retried_ = false; |
341 StartTokenRequest(); | 339 StartTokenRequest(); |
342 } else if (use_authentication && signin_manager_->AuthInProgress()) { | 340 } else if (use_authentication && signin_manager_->AuthInProgress()) { |
343 // Currently signing in: wait for auth to finish (the refresh token) --> | 341 // Currently signing in: wait for auth to finish (the refresh token) --> |
344 // get OAuth token --> fetch snippets. | 342 // get OAuth token --> fetch snippets. |
345 if (!waiting_for_refresh_token_) { | 343 if (!waiting_for_refresh_token_) { |
346 // Wait until we get a refresh token. | 344 // Wait until we get a refresh token. |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 } | 729 } |
732 | 730 |
733 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { | 731 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { |
734 LOG(WARNING) << "Received invalid JSON (" << error | 732 LOG(WARNING) << "Received invalid JSON (" << error |
735 << "): " << last_fetch_json_; | 733 << "): " << last_fetch_json_; |
736 FetchFinished( | 734 FetchFinished( |
737 OptionalFetchedCategories(), FetchResult::JSON_PARSE_ERROR, | 735 OptionalFetchedCategories(), FetchResult::JSON_PARSE_ERROR, |
738 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); | 736 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); |
739 } | 737 } |
740 | 738 |
| 739 void NTPSnippetsFetcher::FilterCategories(FetchedCategoriesVector* categories) { |
| 740 if (!params_.exclusive_category.has_value()) |
| 741 return; |
| 742 Category exclusive = params_.exclusive_category.value(); |
| 743 auto category_it = |
| 744 std::find_if(categories->begin(), categories->end(), |
| 745 [exclusive](const FetchedCategory& c) -> bool { |
| 746 return c.category == exclusive; |
| 747 }); |
| 748 if (category_it == categories->end()) { |
| 749 categories->clear(); |
| 750 return; |
| 751 } |
| 752 categories->erase(categories->begin(), category_it); |
| 753 categories->erase(category_it + 1, categories->end()); |
| 754 } |
| 755 |
741 void NTPSnippetsFetcher::FetchFinished( | 756 void NTPSnippetsFetcher::FetchFinished( |
742 OptionalFetchedCategories fetched_categories, | 757 OptionalFetchedCategories fetched_categories, |
743 FetchResult result, | 758 FetchResult result, |
744 const std::string& extra_message) { | 759 const std::string& extra_message) { |
745 DCHECK(result == FetchResult::SUCCESS || !fetched_categories); | 760 DCHECK(result == FetchResult::SUCCESS || !fetched_categories); |
746 last_status_ = FetchResultToString(result) + extra_message; | 761 last_status_ = FetchResultToString(result) + extra_message; |
747 | 762 |
| 763 // Filter out unwanted categories if necessary. |
| 764 // TODO(fhorschig): As soon as the server supports filtering by category, do |
| 765 // that instead of over-fetching and filtering here. |
| 766 if (fetched_categories.has_value()) |
| 767 FilterCategories(&fetched_categories.value()); |
| 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 (!snippets_available_callback_.is_null()) |
760 snippets_available_callback_.Run(std::move(fetched_categories)); | 781 std::move(snippets_available_callback_).Run(std::move(fetched_categories)); |
761 } | 782 } |
762 | 783 |
763 bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) { | 784 bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) { |
764 switch (user_classifier_->GetUserClass()) { | 785 switch (user_classifier_->GetUserClass()) { |
765 case UserClassifier::UserClass::RARE_NTP_USER: | 786 case UserClassifier::UserClass::RARE_NTP_USER: |
766 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( | 787 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( |
767 interactive_request); | 788 interactive_request); |
768 case UserClassifier::UserClass::ACTIVE_NTP_USER: | 789 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
769 return request_throttler_active_ntp_user_.DemandQuotaForRequest( | 790 return request_throttler_active_ntp_user_.DemandQuotaForRequest( |
770 interactive_request); | 791 interactive_request); |
771 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: | 792 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
772 return request_throttler_active_suggestions_consumer_ | 793 return request_throttler_active_suggestions_consumer_ |
773 .DemandQuotaForRequest(interactive_request); | 794 .DemandQuotaForRequest(interactive_request); |
774 } | 795 } |
775 NOTREACHED(); | 796 NOTREACHED(); |
776 return false; | 797 return false; |
777 } | 798 } |
778 | 799 |
779 } // namespace ntp_snippets | 800 } // namespace ntp_snippets |
OLD | NEW |