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

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

Issue 2473483006: Adds a Fetch() method to the ContentSuggestionService which asks any provider to provide more conte… (Closed)
Patch Set: comments by Markus. 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 // The response from the backend might include suggestions from multiple
739 // categories. If only fetches for a single category were requested, this
740 // function filters them out.
741 void NTPSnippetsFetcher::FilterCategories(FetchedCategoriesVector* categories) {
742 if (!params_.exclusive_category.has_value())
743 return;
744 Category exclusive = params_.exclusive_category.value();
745 auto category_it =
746 std::find_if(categories->begin(), categories->end(),
747 [&exclusive](const FetchedCategory& c) -> bool {
748 return c.category == exclusive;
749 });
750 if (category_it == categories->end()) {
751 categories->clear();
752 return;
753 }
754 categories->erase(categories->begin(), category_it);
755 categories->erase(category_it + 1, categories->end());
Marc Treib 2016/11/07 14:28:23 After looking at this for the 10th time, I'm now c
tschumann 2016/11/08 16:57:35 Good catch! I'll fix it.
756 }
757
741 void NTPSnippetsFetcher::FetchFinished( 758 void NTPSnippetsFetcher::FetchFinished(
742 OptionalFetchedCategories fetched_categories, 759 OptionalFetchedCategories fetched_categories,
743 FetchResult result, 760 FetchResult result,
744 const std::string& extra_message) { 761 const std::string& extra_message) {
745 DCHECK(result == FetchResult::SUCCESS || !fetched_categories); 762 DCHECK(result == FetchResult::SUCCESS || !fetched_categories);
746 last_status_ = FetchResultToString(result) + extra_message; 763 last_status_ = FetchResultToString(result) + extra_message;
747 764
765 // TODO(fhorschig): Filter (un)wanted categories by modifying fetch request.
766 // As soon as backends support the parameter, there is no reason to overfetch
767 // and filter here.
Marc Treib 2016/11/07 14:28:23 I find this comment super confusing. In my version
tschumann 2016/11/08 16:57:35 Done.
768 if (fetched_categories.has_value())
769 FilterCategories(&fetched_categories.value());
770
748 // Don't record FetchTimes if the result indicates that a precondition 771 // Don't record FetchTimes if the result indicates that a precondition
749 // failed and we never actually sent a network request 772 // failed and we never actually sent a network request
750 if (!IsFetchPreconditionFailed(result)) { 773 if (!IsFetchPreconditionFailed(result)) {
751 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", 774 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime",
752 tick_clock_->NowTicks() - fetch_start_time_); 775 tick_clock_->NowTicks() - fetch_start_time_);
753 } 776 }
754 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", 777 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult",
755 static_cast<int>(result), 778 static_cast<int>(result),
756 static_cast<int>(FetchResult::RESULT_MAX)); 779 static_cast<int>(FetchResult::RESULT_MAX));
757 780
758 DVLOG(1) << "Fetch finished: " << last_status_; 781 DVLOG(1) << "Fetch finished: " << last_status_;
759 if (!snippets_available_callback_.is_null()) 782 if (!snippets_available_callback_.is_null())
760 snippets_available_callback_.Run(std::move(fetched_categories)); 783 std::move(snippets_available_callback_).Run(std::move(fetched_categories));
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698