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..f43adb0b87c796d8b5c0dcc50fcf27f83de1b63f |
--- /dev/null |
+++ b/components/ntp_snippets/content_suggestions_service.cc |
@@ -0,0 +1,134 @@ |
+// 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 <algorithm> |
+#include <iterator> |
+ |
+#include "ui/gfx/image/image.h" |
+ |
+namespace ntp_snippets { |
+ |
+namespace { |
+ |
+// Helper method to remove all suggestions of the given |provider_type| from |
+// the given |suggestions|. Returns true if anything changed. |
+bool RemoveSuggestionsOfProvider(std::vector<ContentSuggestion>* suggestions, |
+ ContentSuggestionsProviderType provider_type) { |
+ auto old_end = suggestions->end(); |
+ return old_end != |
+ suggestions->erase( |
+ std::remove_if(suggestions->begin(), suggestions->end(), |
+ [provider_type](const ContentSuggestion& s) { |
+ return s.provider() == provider_type; |
+ }), |
+ suggestions->end()); |
+} |
+ |
+} // namespace |
+ |
+ContentSuggestionsService::ContentSuggestionsService(bool enabled) |
+ : enabled_(enabled) { |
+ if (enabled_) { |
+ } |
+} |
+ |
+ContentSuggestionsService::~ContentSuggestionsService() {} |
+ |
+void ContentSuggestionsService::RegisterProvider( |
+ ContentSuggestionsProvider* provider) { |
+ DCHECK_EQ(0ul, providers_.count(provider->GetProviderType())); |
+ 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 " << int(provider_type); |
+ callback.Run(suggestion_id, gfx::Image()); |
+ } |
+} |
+ |
+void ContentSuggestionsService::ClearCachedSuggestions() { |
+ for (auto& provider : providers_) { |
+ provider.second->ClearCachedSuggestions(); |
+ } |
+} |
+ |
+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); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Private methods |
+ |
+void ContentSuggestionsService::OnSuggestionsChanged( |
+ const ContentSuggestionsProvider& source, |
+ ContentSuggestionCategory changed_category, |
+ std::vector<ContentSuggestion> new_suggestions) { |
+ RemoveSuggestionsOfProvider(&suggestions_by_category_[changed_category], |
+ source.GetProviderType()); |
+ |
+ std::move(new_suggestions.begin(), new_suggestions.end(), |
+ std::back_inserter(suggestions_by_category_[changed_category])); |
+ |
+ FOR_EACH_OBSERVER(ContentSuggestionsServiceObserver, observers_, |
+ OnSuggestionsChanged(changed_category)); |
+} |
+ |
+void ContentSuggestionsService::OnProviderShutdown( |
+ const ContentSuggestionsProvider& source) { |
+ for (auto& pair : suggestions_by_category_) { |
+ if (RemoveSuggestionsOfProvider(&pair.second, source.GetProviderType())) { |
Marc Treib
2016/06/28 15:04:55
If all suggestions in that category were removed,
Philipp Keck
2016/06/30 09:35:35
Done.
|
+ FOR_EACH_OBSERVER(ContentSuggestionsServiceObserver, observers_, |
+ OnSuggestionsChanged(pair.first)); |
+ } |
+ } |
+ providers_.erase(source.GetProviderType()); |
+} |
+ |
+} // namespace ntp_snippets |