| Index: components/ntp_snippets/remote/ntp_snippets_fetcher.cc
|
| diff --git a/components/ntp_snippets/remote/ntp_snippets_fetcher.cc b/components/ntp_snippets/remote/ntp_snippets_fetcher.cc
|
| index b40fd95633023caf30ae67a9d24121b769fad285..7e31f8dedcf6fbb9650eb3814c71f27e4573aed1 100644
|
| --- a/components/ntp_snippets/remote/ntp_snippets_fetcher.cc
|
| +++ b/components/ntp_snippets/remote/ntp_snippets_fetcher.cc
|
| @@ -316,12 +316,8 @@ NTPSnippetsFetcher::~NTPSnippetsFetcher() {
|
| token_service_->RemoveObserver(this);
|
| }
|
|
|
| -void NTPSnippetsFetcher::SetCallback(
|
| - const SnippetsAvailableCallback& callback) {
|
| - snippets_available_callback_ = callback;
|
| -}
|
| -
|
| -void NTPSnippetsFetcher::FetchSnippets(const Params& params) {
|
| +void NTPSnippetsFetcher::FetchSnippets(const Params& params,
|
| + SnippetsAvailableCallback callback) {
|
| if (!DemandQuotaForRequest(params.interactive_request)) {
|
| FetchFinished(OptionalFetchedCategories(),
|
| params.interactive_request
|
| @@ -334,6 +330,8 @@ void NTPSnippetsFetcher::FetchSnippets(const Params& params) {
|
| params_ = params;
|
| fetch_start_time_ = tick_clock_->NowTicks();
|
|
|
| + snippets_available_callback_ = std::move(callback);
|
| +
|
| bool use_authentication = UsesAuthentication();
|
| if (use_authentication && signin_manager_->IsAuthenticated()) {
|
| // Signed-in: get OAuth token --> fetch snippets.
|
| @@ -738,6 +736,23 @@ void NTPSnippetsFetcher::OnJsonError(const std::string& error) {
|
| /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str()));
|
| }
|
|
|
| +void NTPSnippetsFetcher::FilterCategories(FetchedCategoriesVector* categories) {
|
| + if (!params_.exclusive_category.has_value())
|
| + return;
|
| + Category exclusive = params_.exclusive_category.value();
|
| + auto category_it =
|
| + std::find_if(categories->begin(), categories->end(),
|
| + [exclusive](const FetchedCategory& c) -> bool {
|
| + return c.category == exclusive;
|
| + });
|
| + if (category_it == categories->end()) {
|
| + categories->clear();
|
| + return;
|
| + }
|
| + categories->erase(categories->begin(), category_it);
|
| + categories->erase(category_it + 1, categories->end());
|
| +}
|
| +
|
| void NTPSnippetsFetcher::FetchFinished(
|
| OptionalFetchedCategories fetched_categories,
|
| FetchResult result,
|
| @@ -745,6 +760,12 @@ void NTPSnippetsFetcher::FetchFinished(
|
| DCHECK(result == FetchResult::SUCCESS || !fetched_categories);
|
| last_status_ = FetchResultToString(result) + extra_message;
|
|
|
| + // Filter out unwanted categories if necessary.
|
| + // TODO(fhorschig): As soon as the server supports filtering by category, do
|
| + // that instead of over-fetching and filtering here.
|
| + if (fetched_categories.has_value())
|
| + FilterCategories(&fetched_categories.value());
|
| +
|
| // Don't record FetchTimes if the result indicates that a precondition
|
| // failed and we never actually sent a network request
|
| if (!IsFetchPreconditionFailed(result)) {
|
| @@ -757,7 +778,7 @@ void NTPSnippetsFetcher::FetchFinished(
|
|
|
| DVLOG(1) << "Fetch finished: " << last_status_;
|
| if (!snippets_available_callback_.is_null())
|
| - snippets_available_callback_.Run(std::move(fetched_categories));
|
| + std::move(snippets_available_callback_).Run(std::move(fetched_categories));
|
| }
|
|
|
| bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) {
|
|
|