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..07866f7f939a067a36dab181c3012bd5b9a2c24e 100644 |
--- a/components/ntp_snippets/remote/ntp_snippets_fetcher.cc |
+++ b/components/ntp_snippets/remote/ntp_snippets_fetcher.cc |
@@ -316,11 +316,6 @@ NTPSnippetsFetcher::~NTPSnippetsFetcher() { |
token_service_->RemoveObserver(this); |
} |
-void NTPSnippetsFetcher::SetCallback( |
- const SnippetsAvailableCallback& callback) { |
- snippets_available_callback_ = callback; |
-} |
- |
void NTPSnippetsFetcher::FetchSnippets(const Params& params) { |
if (!DemandQuotaForRequest(params.interactive_request)) { |
FetchFinished(OptionalFetchedCategories(), |
@@ -738,6 +733,27 @@ void NTPSnippetsFetcher::OnJsonError(const std::string& error) { |
/*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); |
} |
+void NTPSnippetsFetcher::FilterCategories(OptionalFetchedCategories* fetched) |
+ const { |
+ if (!fetched->has_value()) |
+ return; |
+ if (!params_.exclusive_category.has_value()) |
+ return; |
+ FetchedCategoriesVector* categories = &fetched->value(); |
+ 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 +761,11 @@ void NTPSnippetsFetcher::FetchFinished( |
DCHECK(result == FetchResult::SUCCESS || !fetched_categories); |
last_status_ = FetchResultToString(result) + extra_message; |
+ // TODO(fhorschig): Filter unwanted categories by sending the exclusive |
+ // category to the backend. As soon as backends support this, there is no |
+ // reason to overfetch and filter here. |
+ FilterCategories(&fetched_categories); |
+ |
// Don't record FetchTimes if the result indicates that a precondition |
// failed and we never actually sent a network request |
if (!IsFetchPreconditionFailed(result)) { |
@@ -756,8 +777,10 @@ void NTPSnippetsFetcher::FetchFinished( |
static_cast<int>(FetchResult::RESULT_MAX)); |
DVLOG(1) << "Fetch finished: " << last_status_; |
- if (!snippets_available_callback_.is_null()) |
- snippets_available_callback_.Run(std::move(fetched_categories)); |
+ if (!params_.snippets_available_callback.is_null()) { |
+ std::move(params_.snippets_available_callback).Run( |
vitaliii
2016/11/01 23:29:57
Why do you need this external |std::move|?
fhorschig
2016/11/02 05:05:27
It's a "OnceCallback" that is consumed upon call:
|
+ std::move(fetched_categories)); |
+ } |
} |
bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) { |