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( | |
tschumann
2016/10/24 06:38:25
it's nice to see this method gone. However, i'm st
Marc Treib
2016/10/24 09:47:40
Yes, we actually discussed this, and agree it's a
| |
320 const SnippetsAvailableCallback& callback) { | |
321 snippets_available_callback_ = callback; | |
322 } | |
323 | |
324 void NTPSnippetsFetcher::FetchSnippetsFromHosts( | 319 void NTPSnippetsFetcher::FetchSnippetsFromHosts( |
325 const std::set<std::string>& hosts, | 320 const std::set<std::string>& hosts, |
326 const std::string& language_code, | 321 const std::string& language_code, |
327 const std::set<std::string>& excluded_ids, | 322 const std::set<std::string>& excluded_ids, |
328 int count, | 323 int count, |
329 bool interactive_request) { | 324 bool interactive_request, |
325 SnippetsAvailableCallback callback, | |
326 base::Optional<Category> exclusive_category) { | |
330 if (!DemandQuotaForRequest(interactive_request)) { | 327 if (!DemandQuotaForRequest(interactive_request)) { |
331 FetchFinished(OptionalFetchedCategories(), | 328 FetchFinished(OptionalFetchedCategories(), |
332 interactive_request | 329 interactive_request |
333 ? FetchResult::INTERACTIVE_QUOTA_ERROR | 330 ? FetchResult::INTERACTIVE_QUOTA_ERROR |
334 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, | 331 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, |
335 /*extra_message=*/std::string()); | 332 /*extra_message=*/std::string()); |
336 return; | 333 return; |
337 } | 334 } |
338 | 335 |
339 hosts_ = hosts; | 336 hosts_ = hosts; |
340 fetch_start_time_ = tick_clock_->NowTicks(); | 337 fetch_start_time_ = tick_clock_->NowTicks(); |
341 excluded_ids_ = excluded_ids; | 338 excluded_ids_ = excluded_ids; |
339 exclusive_category_ = exclusive_category; | |
340 snippets_available_callback_ = std::move(callback); | |
342 | 341 |
343 locale_ = PosixLocaleFromBCP47Language(language_code); | 342 locale_ = PosixLocaleFromBCP47Language(language_code); |
344 count_to_fetch_ = count; | 343 count_to_fetch_ = count; |
345 | 344 |
346 bool use_authentication = UsesAuthentication(); | 345 bool use_authentication = UsesAuthentication(); |
347 interactive_request_ = interactive_request; | 346 interactive_request_ = interactive_request; |
348 | 347 |
349 if (use_authentication && signin_manager_->IsAuthenticated()) { | 348 if (use_authentication && signin_manager_->IsAuthenticated()) { |
350 // Signed-in: get OAuth token --> fetch snippets. | 349 // Signed-in: get OAuth token --> fetch snippets. |
351 oauth_token_retried_ = false; | 350 oauth_token_retried_ = false; |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
751 } | 750 } |
752 | 751 |
753 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { | 752 void NTPSnippetsFetcher::OnJsonError(const std::string& error) { |
754 LOG(WARNING) << "Received invalid JSON (" << error | 753 LOG(WARNING) << "Received invalid JSON (" << error |
755 << "): " << last_fetch_json_; | 754 << "): " << last_fetch_json_; |
756 FetchFinished( | 755 FetchFinished( |
757 OptionalFetchedCategories(), FetchResult::JSON_PARSE_ERROR, | 756 OptionalFetchedCategories(), FetchResult::JSON_PARSE_ERROR, |
758 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); | 757 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); |
759 } | 758 } |
760 | 759 |
760 void NTPSnippetsFetcher::FilterCategories(OptionalFetchedCategories* fetched) { | |
761 if (!fetched->has_value()) | |
762 return; | |
763 if (!exclusive_category_.has_value()) | |
764 return; | |
765 FetchedCategoriesVector* categories = &fetched->value(); | |
766 Category exclusive = exclusive_category_.value(); | |
767 auto category_it = | |
768 std::find_if(categories->begin(), categories->end(), | |
769 [exclusive](const FetchedCategory& c) -> bool { | |
770 return c.category == exclusive; | |
771 }); | |
772 if (category_it == categories->end()) | |
Marc Treib
2016/10/20 16:51:39
Shouldn't we erase everything in this case?
fhorschig
2016/11/02 05:05:27
Correct. Changed and tested in CL 2466863003.
| |
773 return; | |
774 categories->erase(categories->begin(), category_it); | |
775 categories->erase(category_it + 1, categories->end()); | |
776 } | |
777 | |
761 void NTPSnippetsFetcher::FetchFinished( | 778 void NTPSnippetsFetcher::FetchFinished( |
762 OptionalFetchedCategories fetched_categories, | 779 OptionalFetchedCategories fetched_categories, |
763 FetchResult result, | 780 FetchResult result, |
764 const std::string& extra_message) { | 781 const std::string& extra_message) { |
765 DCHECK(result == FetchResult::SUCCESS || !fetched_categories); | 782 DCHECK(result == FetchResult::SUCCESS || !fetched_categories); |
766 last_status_ = FetchResultToString(result) + extra_message; | 783 last_status_ = FetchResultToString(result) + extra_message; |
767 | 784 |
785 // TODO(fhorschig): Filter (un)wanted categories by modifying fetch request. | |
786 // As soo as backends support the parameter, there is no | |
787 // no reason to overfetch and filter here. | |
Marc Treib
2016/10/20 16:51:39
nit: We don't align after the TODO.
wanted or unwa
Marc Treib
2016/10/28 14:49:49
Done.
| |
788 FilterCategories(&fetched_categories); | |
789 | |
768 // Don't record FetchTimes if the result indicates that a precondition | 790 // Don't record FetchTimes if the result indicates that a precondition |
769 // failed and we never actually sent a network request | 791 // failed and we never actually sent a network request |
770 if (!IsFetchPreconditionFailed(result)) { | 792 if (!IsFetchPreconditionFailed(result)) { |
771 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", | 793 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", |
772 tick_clock_->NowTicks() - fetch_start_time_); | 794 tick_clock_->NowTicks() - fetch_start_time_); |
773 } | 795 } |
774 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 796 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
775 static_cast<int>(result), | 797 static_cast<int>(result), |
776 static_cast<int>(FetchResult::RESULT_MAX)); | 798 static_cast<int>(FetchResult::RESULT_MAX)); |
777 | 799 |
778 DVLOG(1) << "Fetch finished: " << last_status_; | 800 DVLOG(1) << "Fetch finished: " << last_status_; |
779 if (!snippets_available_callback_.is_null()) | 801 if (!snippets_available_callback_.is_null()) |
780 snippets_available_callback_.Run(std::move(fetched_categories)); | 802 std::move(snippets_available_callback_).Run(std::move(fetched_categories)); |
781 } | 803 } |
782 | 804 |
783 bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) { | 805 bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) { |
784 switch (user_classifier_->GetUserClass()) { | 806 switch (user_classifier_->GetUserClass()) { |
785 case UserClassifier::UserClass::RARE_NTP_USER: | 807 case UserClassifier::UserClass::RARE_NTP_USER: |
786 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( | 808 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( |
787 interactive_request); | 809 interactive_request); |
788 case UserClassifier::UserClass::ACTIVE_NTP_USER: | 810 case UserClassifier::UserClass::ACTIVE_NTP_USER: |
789 return request_throttler_active_ntp_user_.DemandQuotaForRequest( | 811 return request_throttler_active_ntp_user_.DemandQuotaForRequest( |
790 interactive_request); | 812 interactive_request); |
791 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: | 813 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
792 return request_throttler_active_suggestions_consumer_ | 814 return request_throttler_active_suggestions_consumer_ |
793 .DemandQuotaForRequest(interactive_request); | 815 .DemandQuotaForRequest(interactive_request); |
794 } | 816 } |
795 NOTREACHED(); | 817 NOTREACHED(); |
796 return false; | 818 return false; |
797 } | 819 } |
798 | 820 |
799 } // namespace ntp_snippets | 821 } // namespace ntp_snippets |
OLD | NEW |