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

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_fetcher.cc

Issue 2421463002: FetchMore functionality backend (Closed)
Patch Set: ID set reference, Optional callback, ... (2466863003 comments). Created 4 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 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
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();
332 snippets_available_callback_ = std::move(callback);
336 333
337 bool use_authentication = UsesAuthentication(); 334 bool use_authentication = UsesAuthentication();
338 if (use_authentication && signin_manager_->IsAuthenticated()) { 335 if (use_authentication && signin_manager_->IsAuthenticated()) {
339 // Signed-in: get OAuth token --> fetch snippets. 336 // Signed-in: get OAuth token --> fetch snippets.
340 oauth_token_retried_ = false; 337 oauth_token_retried_ = false;
341 StartTokenRequest(); 338 StartTokenRequest();
342 } else if (use_authentication && signin_manager_->AuthInProgress()) { 339 } else if (use_authentication && signin_manager_->AuthInProgress()) {
343 // Currently signing in: wait for auth to finish (the refresh token) --> 340 // Currently signing in: wait for auth to finish (the refresh token) -->
344 // get OAuth token --> fetch snippets. 341 // get OAuth token --> fetch snippets.
345 if (!waiting_for_refresh_token_) { 342 if (!waiting_for_refresh_token_) {
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 } 728 }
732 729
733 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { 730 void NTPSnippetsFetcher::OnJsonError(const std::string& error) {
734 LOG(WARNING) << "Received invalid JSON (" << error 731 LOG(WARNING) << "Received invalid JSON (" << error
735 << "): " << last_fetch_json_; 732 << "): " << last_fetch_json_;
736 FetchFinished( 733 FetchFinished(
737 OptionalFetchedCategories(), FetchResult::JSON_PARSE_ERROR, 734 OptionalFetchedCategories(), FetchResult::JSON_PARSE_ERROR,
738 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); 735 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str()));
739 } 736 }
740 737
738 void NTPSnippetsFetcher::FilterCategories(FetchedCategoriesVector* categories) {
739 if (!params_.exclusive_category.has_value())
740 return;
741 Category exclusive = params_.exclusive_category.value();
742 auto category_it =
743 std::find_if(categories->begin(), categories->end(),
744 [exclusive](const FetchedCategory& c) -> bool {
tschumann 2016/11/03 09:20:57 this should capture by reference.
745 return c.category == exclusive;
746 });
747 if (category_it == categories->end()) {
748 categories->clear();
749 return;
750 }
751 categories->erase(categories->begin(), category_it);
752 categories->erase(category_it + 1, categories->end());
753 }
754
741 void NTPSnippetsFetcher::FetchFinished( 755 void NTPSnippetsFetcher::FetchFinished(
742 OptionalFetchedCategories fetched_categories, 756 OptionalFetchedCategories fetched_categories,
743 FetchResult result, 757 FetchResult result,
744 const std::string& extra_message) { 758 const std::string& extra_message) {
745 DCHECK(result == FetchResult::SUCCESS || !fetched_categories); 759 DCHECK(result == FetchResult::SUCCESS || !fetched_categories);
746 last_status_ = FetchResultToString(result) + extra_message; 760 last_status_ = FetchResultToString(result) + extra_message;
747 761
762 // TODO(fhorschig): Filter (un)wanted categories by modifying fetch request.
763 // As soo as backends support the parameter, there is no
tschumann 2016/11/03 09:20:57 fix formatting. (and soo->soon)
764 // no reason to overfetch and filter here.
765 if (fetched_categories.has_value())
766 FilterCategories(&fetched_categories.value());
767
748 // Don't record FetchTimes if the result indicates that a precondition 768 // Don't record FetchTimes if the result indicates that a precondition
749 // failed and we never actually sent a network request 769 // failed and we never actually sent a network request
750 if (!IsFetchPreconditionFailed(result)) { 770 if (!IsFetchPreconditionFailed(result)) {
751 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", 771 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime",
752 tick_clock_->NowTicks() - fetch_start_time_); 772 tick_clock_->NowTicks() - fetch_start_time_);
753 } 773 }
754 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", 774 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult",
755 static_cast<int>(result), 775 static_cast<int>(result),
756 static_cast<int>(FetchResult::RESULT_MAX)); 776 static_cast<int>(FetchResult::RESULT_MAX));
757 777
758 DVLOG(1) << "Fetch finished: " << last_status_; 778 DVLOG(1) << "Fetch finished: " << last_status_;
759 if (!snippets_available_callback_.is_null()) 779 if (!snippets_available_callback_.is_null())
760 snippets_available_callback_.Run(std::move(fetched_categories)); 780 std::move(snippets_available_callback_).Run(std::move(fetched_categories));
761 } 781 }
762 782
763 bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) { 783 bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) {
764 switch (user_classifier_->GetUserClass()) { 784 switch (user_classifier_->GetUserClass()) {
765 case UserClassifier::UserClass::RARE_NTP_USER: 785 case UserClassifier::UserClass::RARE_NTP_USER:
766 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( 786 return request_throttler_rare_ntp_user_.DemandQuotaForRequest(
767 interactive_request); 787 interactive_request);
768 case UserClassifier::UserClass::ACTIVE_NTP_USER: 788 case UserClassifier::UserClass::ACTIVE_NTP_USER:
769 return request_throttler_active_ntp_user_.DemandQuotaForRequest( 789 return request_throttler_active_ntp_user_.DemandQuotaForRequest(
770 interactive_request); 790 interactive_request);
771 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: 791 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER:
772 return request_throttler_active_suggestions_consumer_ 792 return request_throttler_active_suggestions_consumer_
773 .DemandQuotaForRequest(interactive_request); 793 .DemandQuotaForRequest(interactive_request);
774 } 794 }
775 NOTREACHED(); 795 NOTREACHED();
776 return false; 796 return false;
777 } 797 }
778 798
779 } // namespace ntp_snippets 799 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698