Chromium Code Reviews| Index: components/ntp_snippets/content_suggestions_service.cc |
| diff --git a/components/ntp_snippets/content_suggestions_service.cc b/components/ntp_snippets/content_suggestions_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..96983fc781ff1df83615605afb0d38a7f07f0d46 |
| --- /dev/null |
| +++ b/components/ntp_snippets/content_suggestions_service.cc |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/ntp_snippets/content_suggestions_service.h" |
| + |
| +#include "base/observer_list.h" |
|
Marc Treib
2016/06/28 11:29:09
Not needed, since it's already included in the hea
Philipp Keck
2016/06/28 14:18:56
Done.
|
| +#include "ui/gfx/image/image.h" |
| + |
| +namespace ntp_snippets { |
| + |
| +ContentSuggestionsService::ContentSuggestionsService(bool enabled) |
| + : enabled_(enabled) { |
| + if (enabled_) { |
|
Marc Treib
2016/06/28 11:29:09
Remove empty if.
It looks like the enabled_ flag c
Philipp Keck
2016/06/28 14:18:56
The reason that there is no code is not that the e
Marc Treib
2016/06/28 15:04:55
Alright, but then remove the empty if.
Philipp Keck
2016/06/30 09:35:35
I removed the empty if for now. The question how t
Marc Treib
2016/06/30 10:36:53
Hm, right, we need "StateChanged" notifications bo
Philipp Keck
2016/06/30 17:14:07
Yes, but I'm not implementing that (now), right?
Marc Treib
2016/07/01 09:15:32
Yeah, another CL is fine, but we'll need at least
Philipp Keck
2016/07/01 13:00:03
Yes. I'm thinking about extending the new Enabled
Marc Treib
2016/07/01 13:29:17
The category -> provider mapping sounds like a goo
|
| + } |
| +} |
| + |
| +ContentSuggestionsService::~ContentSuggestionsService() {} |
| + |
| +void ContentSuggestionsService::RegisterProvider( |
| + ContentSuggestionsProvider* provider) { |
| + DCHECK(providers_.count(provider->GetProviderType()) == 0); |
|
Marc Treib
2016/06/28 11:29:09
DCHECK_EQ(0, ...)
Philipp Keck
2016/06/28 14:18:55
Done.
|
| + providers_[provider->GetProviderType()] = provider; |
| + provider->SetDelegate(this); |
| +} |
| + |
| +void ContentSuggestionsService::Shutdown() { |
| + FOR_EACH_OBSERVER(ContentSuggestionsServiceObserver, observers_, |
| + ContentSuggestionsServiceShutdown()); |
| + observers_.Clear(); |
| + for (auto& provider : providers_) { |
| + provider.second->SetDelegate(nullptr); |
| + } |
| + providers_.clear(); |
| + enabled_ = false; |
| +} |
| + |
| +void ContentSuggestionsService::FetchImage( |
| + const ContentSuggestionsProviderType provider_type, |
| + const std::string& suggestion_id, |
| + const ImageFetchedCallback& callback) { |
| + if (providers_.count(provider_type)) { |
| + providers_[provider_type]->FetchImage(suggestion_id, callback); |
| + } else { |
| + LOG(WARNING) << "Requested image for suggestion" << suggestion_id |
| + << " for unavailable provider type " |
| + << static_cast<base::underlying_type< |
| + ContentSuggestionsProviderType>::type>(provider_type); |
|
Marc Treib
2016/06/28 11:29:09
Argh, this is ugly - I guess it doesn't compile wi
Philipp Keck
2016/06/28 14:18:56
Done.
|
| + callback.Run(suggestion_id, gfx::Image()); |
| + } |
| +} |
| + |
| +void ContentSuggestionsService::ClearSuggestions() { |
| + for (auto& provider : providers_) { |
| + provider.second->ClearSuggestions(); |
| + } |
| +} |
| + |
| +void ContentSuggestionsService::ClearDiscardedSuggestions() { |
| + for (auto& provider : providers_) { |
| + provider.second->ClearDiscardedSuggestions(); |
| + } |
| +} |
| + |
| +void ContentSuggestionsService::Discard( |
| + const ContentSuggestionsProviderType provider_type, |
| + const std::string& suggestion_id) { |
| + if (providers_.count(provider_type)) { |
| + providers_[provider_type]->Discard(suggestion_id); |
| + } else { |
| + LOG(WARNING) << "Discarded suggestion " << suggestion_id |
| + << " for unavailable provider type " |
| + << static_cast<base::underlying_type< |
| + ContentSuggestionsProviderType>::type>(provider_type); |
| + } |
| +} |
| + |
| +void ContentSuggestionsService::AddObserver( |
| + ContentSuggestionsServiceObserver* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void ContentSuggestionsService::RemoveObserver( |
| + ContentSuggestionsServiceObserver* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +void ContentSuggestionsService::OnContentChanged( |
| + const ContentSuggestionsProvider& source, |
| + ContentSuggestionCategory changed_category, |
| + std::vector<ContentSuggestion> new_suggestions) { |
| + RemoveSuggestionsOfProvider(&suggestions_[changed_category], |
| + source.GetProviderType()); |
| + |
| + std::move(new_suggestions.begin(), new_suggestions.end(), |
|
Marc Treib
2016/06/28 11:29:09
#include <algorithm>
Philipp Keck
2016/06/28 14:18:56
Done.
|
| + std::back_inserter(suggestions_[changed_category])); |
|
Marc Treib
2016/06/28 11:29:09
#include <iterator>
Philipp Keck
2016/06/28 14:18:56
Done.
|
| + |
| + FOR_EACH_OBSERVER(ContentSuggestionsServiceObserver, observers_, |
| + OnSuggestionsChanged(changed_category)); |
| +} |
| + |
| +void ContentSuggestionsService::OnProviderShutdown( |
| + const ContentSuggestionsProvider& source) { |
| + for (auto& pair : suggestions_) { |
| + if (RemoveSuggestionsOfProvider(&pair.second, source.GetProviderType())) { |
| + FOR_EACH_OBSERVER(ContentSuggestionsServiceObserver, observers_, |
| + OnSuggestionsChanged(pair.first)); |
| + } |
| + } |
| + providers_.erase(source.GetProviderType()); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// Private methods |
| + |
| +bool ContentSuggestionsService::RemoveSuggestionsOfProvider( |
| + std::vector<ContentSuggestion>* list, |
|
Marc Treib
2016/06/28 11:29:09
Call this "suggestions" maybe? It's not actually a
Philipp Keck
2016/06/28 14:18:55
Done.
|
| + ContentSuggestionsProviderType provider_type) { |
| + auto provider_matcher = [provider_type](const ContentSuggestion& s) { |
|
Marc Treib
2016/06/28 11:29:09
Inline this into the remove_if?
Philipp Keck
2016/06/28 14:18:56
Done.
|
| + return s.provider() == provider_type; |
| + }; |
| + auto remove_from = |
| + std::remove_if(list->begin(), list->end(), provider_matcher); |
| + if (remove_from == list->end()) { |
| + return false; |
| + } else { |
| + list->erase(remove_from, list->end()); |
|
Marc Treib
2016/06/28 11:29:09
You could also use the common erase(remove_if, end
Philipp Keck
2016/06/28 14:18:56
Done.
Philipp Keck
2016/06/30 09:35:35
Undone. Reverted to old code because checking whet
|
| + return true; |
| + } |
| +} |
| + |
| +} // namespace ntp_snippets |