Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: components/ntp_snippets/content_suggestions_service.cc

Issue 2102023002: Add ContentSuggestionsService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/ntp_snippets/content_suggestions_service.h"
6
7 #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.
8 #include "ui/gfx/image/image.h"
9
10 namespace ntp_snippets {
11
12 ContentSuggestionsService::ContentSuggestionsService(bool enabled)
13 : enabled_(enabled) {
14 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
15 }
16 }
17
18 ContentSuggestionsService::~ContentSuggestionsService() {}
19
20 void ContentSuggestionsService::RegisterProvider(
21 ContentSuggestionsProvider* provider) {
22 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.
23 providers_[provider->GetProviderType()] = provider;
24 provider->SetDelegate(this);
25 }
26
27 void ContentSuggestionsService::Shutdown() {
28 FOR_EACH_OBSERVER(ContentSuggestionsServiceObserver, observers_,
29 ContentSuggestionsServiceShutdown());
30 observers_.Clear();
31 for (auto& provider : providers_) {
32 provider.second->SetDelegate(nullptr);
33 }
34 providers_.clear();
35 enabled_ = false;
36 }
37
38 void ContentSuggestionsService::FetchImage(
39 const ContentSuggestionsProviderType provider_type,
40 const std::string& suggestion_id,
41 const ImageFetchedCallback& callback) {
42 if (providers_.count(provider_type)) {
43 providers_[provider_type]->FetchImage(suggestion_id, callback);
44 } else {
45 LOG(WARNING) << "Requested image for suggestion" << suggestion_id
46 << " for unavailable provider type "
47 << static_cast<base::underlying_type<
48 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.
49 callback.Run(suggestion_id, gfx::Image());
50 }
51 }
52
53 void ContentSuggestionsService::ClearSuggestions() {
54 for (auto& provider : providers_) {
55 provider.second->ClearSuggestions();
56 }
57 }
58
59 void ContentSuggestionsService::ClearDiscardedSuggestions() {
60 for (auto& provider : providers_) {
61 provider.second->ClearDiscardedSuggestions();
62 }
63 }
64
65 void ContentSuggestionsService::Discard(
66 const ContentSuggestionsProviderType provider_type,
67 const std::string& suggestion_id) {
68 if (providers_.count(provider_type)) {
69 providers_[provider_type]->Discard(suggestion_id);
70 } else {
71 LOG(WARNING) << "Discarded suggestion " << suggestion_id
72 << " for unavailable provider type "
73 << static_cast<base::underlying_type<
74 ContentSuggestionsProviderType>::type>(provider_type);
75 }
76 }
77
78 void ContentSuggestionsService::AddObserver(
79 ContentSuggestionsServiceObserver* observer) {
80 observers_.AddObserver(observer);
81 }
82
83 void ContentSuggestionsService::RemoveObserver(
84 ContentSuggestionsServiceObserver* observer) {
85 observers_.RemoveObserver(observer);
86 }
87
88 void ContentSuggestionsService::OnContentChanged(
89 const ContentSuggestionsProvider& source,
90 ContentSuggestionCategory changed_category,
91 std::vector<ContentSuggestion> new_suggestions) {
92 RemoveSuggestionsOfProvider(&suggestions_[changed_category],
93 source.GetProviderType());
94
95 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.
96 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.
97
98 FOR_EACH_OBSERVER(ContentSuggestionsServiceObserver, observers_,
99 OnSuggestionsChanged(changed_category));
100 }
101
102 void ContentSuggestionsService::OnProviderShutdown(
103 const ContentSuggestionsProvider& source) {
104 for (auto& pair : suggestions_) {
105 if (RemoveSuggestionsOfProvider(&pair.second, source.GetProviderType())) {
106 FOR_EACH_OBSERVER(ContentSuggestionsServiceObserver, observers_,
107 OnSuggestionsChanged(pair.first));
108 }
109 }
110 providers_.erase(source.GetProviderType());
111 }
112
113 ////////////////////////////////////////////////////////////////////////////////
114 // Private methods
115
116 bool ContentSuggestionsService::RemoveSuggestionsOfProvider(
117 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.
118 ContentSuggestionsProviderType provider_type) {
119 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.
120 return s.provider() == provider_type;
121 };
122 auto remove_from =
123 std::remove_if(list->begin(), list->end(), provider_matcher);
124 if (remove_from == list->end()) {
125 return false;
126 } else {
127 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
128 return true;
129 }
130 }
131
132 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698