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_PROVIDER_H_ | |
6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/callback_forward.h" | |
12 #include "components/ntp_snippets/content_suggestion.h" | |
13 #include "components/ntp_snippets/content_suggestions_category.h" | |
14 #include "components/ntp_snippets/content_suggestions_category_status.h" | |
15 | |
16 namespace gfx { | |
17 class Image; | |
18 } | |
19 | |
20 namespace ntp_snippets { | |
21 | |
22 // Provides content suggestions from one particular source. | |
23 // A provider can provide suggestions for multiple ContentSuggestionCategories, | |
24 // but for every category that it provides, it will be the only provider in the | |
25 // system which provides suggestions for that category. | |
26 // A provider can be a keyed service, in which case it should notify the | |
27 // ContentSuggestionsService through the observer before it shuts down. | |
28 class ContentSuggestionsProvider { | |
29 public: | |
30 using ImageFetchedCallback = | |
31 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; | |
32 | |
33 // The observer of a provider is notified when new data is available. | |
34 class Observer { | |
35 public: | |
36 // Called when the available content changed. | |
37 // If a provider provides suggestions of multiple categories, it must call | |
38 // this callback for every category individually. The |suggestions| | |
39 // parameter should always contain the full list of currently available | |
40 // suggestions of that category, i.e., passing an empty list will remove | |
41 // all suggestions of the given category for the provider. Note that to | |
42 // clear them from the UI immediately, the provider needs to change the | |
43 // status of the respective category. | |
44 // The list must not contain ContentSuggestions assigned to a category other | |
45 // than the passed |changed_category|. | |
46 virtual void OnNewSuggestions( | |
47 ContentSuggestionsCategory changed_category, | |
48 std::vector<ContentSuggestion> suggestions) = 0; | |
tschumann
2016/07/06 06:36:12
const vector<>& ?
Marc Treib
2016/07/06 08:20:44
This is on purpose, to avoid a copy - it'll be mov
tschumann
2016/07/06 14:49:44
given it's an interface, I have a slight tendency
Philipp Keck
2016/07/06 15:53:20
const-ref wouldn't work because ContentSuggestion
| |
49 | |
50 // Called when the status of a category changed. | |
51 // Whenever the status changes to an unavailable status, all suggestions in | |
52 // that category must immediately be removed from all caches and from the | |
53 // UI. | |
54 virtual void OnCategoryStatusChanged( | |
55 ContentSuggestionsCategory changed_category, | |
56 ContentSuggestionsCategoryStatus new_status) = 0; | |
57 | |
58 // Called when the provider needs to shut down and will not deliver any | |
59 // suggestions anymore. This unregisters the provider, removes the observer | |
60 // and deactivates all provided categories. | |
61 virtual void OnProviderShutdown(ContentSuggestionsProvider* provider) = 0; | |
62 }; | |
63 | |
64 // Sets an observer which is notified about changes to the available | |
65 // suggestions, or removes it by passing a nullptr. | |
66 virtual void SetObserver(Observer* observer) = 0; | |
67 | |
68 // Determines the status of the given |category|, see | |
69 // ContentSuggestionsCategoryStatus. | |
70 virtual ContentSuggestionsCategoryStatus GetCategoryStatus( | |
71 ContentSuggestionsCategory category) = 0; | |
72 | |
73 // Discards the suggestion with the given ID. A provider needs to ensure that | |
74 // a once-discarded suggestion is never delivered again (through the | |
75 // Observer). The provider must not call Observer::OnSuggestionsChanged if the | |
76 // removal of the discarded suggestion is the only change. | |
77 // Note that the |suggestion_id| to the provider here is the same as the | |
78 // |original_id| that was passed to the |ContentSuggestion| constructor. | |
79 virtual void DiscardSuggestion(const std::string& suggestion_id) = 0; | |
80 | |
81 // Fetches the image for the suggestion with the given ID and returns it | |
82 // through the callback. This fetch may occur locally or from the internet. | |
83 // Note that the |suggestion_id| to the provider here is the same as the | |
84 // |original_id| that was passed to the |ContentSuggestion| constructor. | |
85 virtual void FetchSuggestionImage(const std::string& suggestion_id, | |
86 const ImageFetchedCallback& callback) = 0; | |
87 | |
88 // Used only for debugging purposes. Clears all caches so that the next | |
89 // fetch starts from scratch. | |
90 virtual void ClearCachedSuggestionsForDebugging() = 0; | |
91 | |
92 // Used only for debugging purposes. Clears the cache of discarded | |
93 // suggestions, if present, so that no suggestions are suppressed. This does | |
94 // not necessarily make previously discarded suggestions reappear, as they may | |
95 // have been permanently deleted, depending on the provider implementation. | |
96 virtual void ClearDiscardedSuggestionsForDebugging() = 0; | |
97 | |
98 // Creates a unique ID. The given |within_category_id| must be unique among | |
99 // all suggestion IDs from this provider for the given |category|. This method | |
100 // combines it with the |category| to form an ID that is unique | |
101 // application-wide, because this provider is the only one that provides | |
102 // suggestions for that category. | |
103 static std::string MakeUniqueID(ContentSuggestionsCategory category, | |
tschumann
2016/07/06 06:36:12
should this be protected?
Philipp Keck
2016/07/06 08:34:23
No, NTPSnippet also calls it. If the NTPSnippet cl
tschumann
2016/07/06 14:49:44
As discussed offline, give that the provider is ca
Philipp Keck
2016/07/06 15:53:20
This method is now protected. It will be used by p
| |
104 const std::string& within_category_id); | |
105 | |
106 const std::vector<ContentSuggestionsCategory> provided_categories() const { | |
107 return provided_categories_; | |
108 } | |
109 | |
110 protected: | |
111 ContentSuggestionsProvider( | |
112 const std::vector<ContentSuggestionsCategory>& provided_categories); | |
113 virtual ~ContentSuggestionsProvider(); | |
114 | |
115 private: | |
116 const std::vector<ContentSuggestionsCategory> provided_categories_; | |
117 }; | |
118 | |
119 } // namespace ntp_snippets | |
120 | |
121 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ | |
OLD | NEW |