OLD | NEW |
---|---|
(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 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | |
6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <map> | |
11 #include <set> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/observer_list.h" | |
16 #include "components/keyed_service/core/keyed_service.h" | |
17 #include "components/ntp_snippets/content_suggestions_provider.h" | |
18 #include "components/ntp_snippets/content_suggestions_provider_type.h" | |
19 | |
20 namespace gfx { | |
21 class Image; | |
22 } | |
tschumann
2016/06/28 11:47:17
necessary?
tschumann
2016/06/28 12:32:40
Done.
| |
23 | |
24 namespace ntp_snippets { | |
25 | |
26 class ContentSuggestionsServiceObserver; | |
27 | |
28 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | |
29 // them to the NTP. | |
30 class ContentSuggestionsService : public KeyedService, | |
31 public ContentSuggestionsProvider::Delegate { | |
32 public: | |
33 using ImageFetchedCallback = | |
34 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; | |
35 | |
36 ContentSuggestionsService(bool enabled); | |
tschumann
2016/06/28 12:32:40
nit: usually enums are better to read here.
Compar
Philipp Keck
2016/06/28 14:18:57
True, but wouldn't that question pretty much every
tschumann
2016/06/30 10:20:06
It essentially applies to every boolean parameter
Philipp Keck
2016/06/30 10:41:53
Done. Unlike your code example above, I used an ac
| |
37 ~ContentSuggestionsService() override; | |
38 | |
39 // Inherited from KeyedService. | |
40 void Shutdown() override; | |
41 | |
42 // Available suggestions | |
43 const std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>>& | |
44 suggestions() const { | |
45 return suggestions_; | |
46 } | |
47 const std::vector<ContentSuggestion>& suggestions( | |
48 ContentSuggestionCategory category) { | |
49 return suggestions_[category]; | |
50 } | |
51 | |
52 // Fetches the image for the suggestion with the given |suggestion_id| and | |
53 // runs the |callback|. If that suggestion doesn't exist or the fetch fails, | |
54 // the callback gets an empty image. | |
55 void FetchImage(const ContentSuggestionsProviderType provider_type, | |
56 const std::string& suggestion_id, | |
57 const ImageFetchedCallback& callback); | |
58 | |
59 // Removes all suggestions from all caches or internal stores in all | |
60 // providers. It does, however, not remove any suggestions from the provider's | |
61 // sources, so if their configuration hasn't changed, they should return the | |
62 // same results when they fetch the next time. In particular, calling this | |
63 // method will not mark any suggestions as discarded. | |
64 void ClearSuggestions(); | |
65 | |
66 // Only for debugging use through the internals page. Some providers | |
67 // internally store a list of discarded suggestions to prevent them from | |
68 // reappearing. This function clears all such lists in all providers, making | |
69 // discarded suggestions reappear (only for certain providers). | |
70 void ClearDiscardedSuggestions(); | |
71 | |
72 // Discards the suggestion with the given |suggestion_id|, if it exists. | |
73 void Discard(const ContentSuggestionsProviderType provider_type, | |
74 const std::string& suggestion_id); | |
75 | |
76 // Observer accessors (outgoing observers, from this service towards UI) | |
77 void AddObserver(ContentSuggestionsServiceObserver* observer); | |
78 void RemoveObserver(ContentSuggestionsServiceObserver* observer); | |
79 | |
80 // Implementation of ContentSuggestionsProvider::Delegate (incoming data) | |
81 void OnSuggestionsChanged( | |
82 const ContentSuggestionsProvider& source, | |
83 ContentSuggestionCategory changed_category, | |
84 std::vector<ContentSuggestion> suggestions) override; | |
85 void OnProviderShutdown(const ContentSuggestionsProvider& source) override; | |
tschumann
2016/06/28 12:32:40
nit: please insert an empty line between methods.
Philipp Keck
2016/06/28 14:18:57
Ok. What are the criteria for doing that? Because
Marc Treib
2016/06/28 15:04:55
AFAIK there aren't any hard rules. Some people pre
tschumann
2016/06/30 10:20:06
Good point!
I agree that in the AddObserver/Remove
Philipp Keck
2016/06/30 10:41:53
This is already resolved, see
https://codereview.
| |
86 | |
87 // Registers a new ContentSuggestionsProvider. The service will call | |
88 // ContentSuggestionsProvider::setDelegate with a delegate object, through | |
89 // which the registered provider should report its new suggestions. This | |
90 // method will fail when a provider of the same type is already registered. | |
91 void RegisterProvider(ContentSuggestionsProvider* provider); | |
92 | |
93 // For use by the snippets-internals page only. | |
94 const std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*>& | |
95 providers() const { | |
96 return providers_; | |
97 } | |
98 | |
99 private: | |
100 // When |enabled_| is true the service will retrieve suggestions from the | |
101 // providers. | |
102 bool enabled_; | |
103 | |
104 // All current suggestions. | |
105 std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>> | |
106 suggestions_; | |
107 | |
108 std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*> | |
109 providers_; | |
110 | |
111 // The observers. | |
112 base::ObserverList<ContentSuggestionsServiceObserver> observers_; | |
113 | |
114 // Helper method to remove all suggestions of the given |provider_type| from | |
115 // the given |list|. Returns true if anything changed. | |
116 bool RemoveSuggestionsOfProvider( | |
tschumann
2016/06/28 12:32:40
shouldn't functions go before members? (declaratio
Philipp Keck
2016/06/28 14:18:57
Acknowledged, this member was removed anyway.
| |
117 std::vector<ContentSuggestion>* list, | |
118 ContentSuggestionsProviderType provider_type); | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | |
121 }; | |
122 | |
123 class ContentSuggestionsServiceObserver { | |
124 public: | |
125 // Fired every time the service loads a new set of data. This event is fired | |
126 // per |changed_category|. The new data is then available through | |
127 // |suggestions()[changed_category]|. | |
128 virtual void OnSuggestionsChanged( | |
129 ContentSuggestionCategory changed_category) = 0; | |
130 // Sent when the service is shutting down. | |
tschumann
2016/06/28 12:32:40
please insert empty line.
Philipp Keck
2016/06/28 14:18:57
Done.
| |
131 virtual void ContentSuggestionsServiceShutdown() = 0; | |
132 | |
133 protected: | |
134 virtual ~ContentSuggestionsServiceObserver() {} | |
135 }; | |
136 | |
137 } // namespace ntp_snippets | |
138 | |
139 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | |
OLD | NEW |