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 <string> | |
12 #include <vector> | |
13 | |
14 #include "base/callback_forward.h" | |
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 } | |
23 | |
24 namespace ntp_snippets { | |
25 | |
26 class ContentSuggestionsServiceObserver; | |
27 | |
28 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | |
29 // them grouped into categories. | |
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); | |
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_by_category_; | |
46 } | |
47 const std::vector<ContentSuggestion>& suggestions( | |
48 ContentSuggestionCategory category) { | |
49 return suggestions_by_category_[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(ContentSuggestionsProviderType provider_type, | |
56 const std::string& suggestion_id, | |
57 const ImageFetchedCallback& callback); | |
58 | |
59 // Only for debugging use through the internals page. | |
60 // Removes all suggestions from all caches or internal stores in all | |
61 // providers. It does, however, not remove any suggestions from the provider's | |
62 // sources, so if their configuration hasn't changed, they should return the | |
63 // same results when they fetch the next time. In particular, calling this | |
64 // method will not mark any suggestions as discarded. | |
65 void ClearCachedSuggestions(); | |
66 | |
67 // Only for debugging use through the internals page. Some providers | |
68 // internally store a list of discarded suggestions to prevent them from | |
69 // reappearing. This function clears all such lists in all providers, making | |
70 // discarded suggestions reappear (only for certain providers). | |
71 void ClearDiscardedSuggestions(); | |
72 | |
73 // Discards the suggestion with the given |suggestion_id|, if it exists. | |
74 void Discard(ContentSuggestionsProviderType provider_type, | |
75 const std::string& suggestion_id); | |
76 | |
77 // Observer accessors. | |
78 void AddObserver(ContentSuggestionsServiceObserver* observer); | |
79 void RemoveObserver(ContentSuggestionsServiceObserver* observer); | |
80 | |
81 // Registers a new ContentSuggestionsProvider. This method will fail when a | |
Marc Treib
2016/06/28 15:04:55
nit: s/when/if
Philipp Keck
2016/06/30 09:35:35
Done.
| |
82 // provider of the same type is already registered. | |
83 void RegisterProvider(ContentSuggestionsProvider* provider); | |
84 | |
85 // For use by the snippets-internals page only. | |
86 const std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*>& | |
87 providers() const { | |
88 return providers_; | |
89 } | |
90 | |
91 private: | |
92 // When |enabled_| is true the service will retrieve suggestions from the | |
93 // providers. | |
94 bool enabled_; | |
95 | |
96 // All current suggestions. | |
97 std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>> | |
98 suggestions_by_category_; | |
99 | |
100 std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*> | |
101 providers_; | |
102 | |
103 // The observers. | |
104 base::ObserverList<ContentSuggestionsServiceObserver> observers_; | |
105 | |
106 // Implementation of ContentSuggestionsProvider::Delegate. | |
107 void OnSuggestionsChanged( | |
108 const ContentSuggestionsProvider& source, | |
109 ContentSuggestionCategory changed_category, | |
110 std::vector<ContentSuggestion> suggestions) override; | |
111 | |
112 void OnProviderShutdown(const ContentSuggestionsProvider& source) override; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | |
115 }; | |
116 | |
117 class ContentSuggestionsServiceObserver { | |
118 public: | |
119 // Fired every time the service loads a new set of data. This event is fired | |
120 // per |changed_category|. The new data is then available through | |
121 // |suggestions()[changed_category]|. | |
122 virtual void OnSuggestionsChanged( | |
123 ContentSuggestionCategory changed_category) = 0; | |
Marc Treib
2016/06/28 15:04:55
Hm, does this even need to include the category? A
Philipp Keck
2016/06/30 09:35:35
Done.
| |
124 | |
125 // Sent when the service is shutting down. | |
126 virtual void ContentSuggestionsServiceShutdown() = 0; | |
127 | |
128 protected: | |
129 virtual ~ContentSuggestionsServiceObserver() {} | |
130 }; | |
131 | |
132 } // namespace ntp_snippets | |
133 | |
134 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | |
OLD | NEW |