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_category_status.h" |
| 18 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 19 |
| 20 namespace gfx { |
| 21 class Image; |
| 22 } |
| 23 |
| 24 namespace ntp_snippets { |
| 25 |
| 26 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
| 27 // them grouped into categories. There can be at most one provider per category. |
| 28 // NOTE: This class is not yet in use, please use NTPSnippetsService for now |
| 29 // (see ntp_snippets_service.h). |
| 30 class ContentSuggestionsService : public KeyedService, |
| 31 public ContentSuggestionsProvider::Observer { |
| 32 public: |
| 33 using ImageFetchedCallback = |
| 34 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; |
| 35 |
| 36 class Observer { |
| 37 public: |
| 38 // Fired every time the service receives a new set of data, replacing any |
| 39 // previously available data (though in most cases there will be an overlap |
| 40 // and only a few changes within the data). The new data is then available |
| 41 // through the getters of the service. |
| 42 virtual void OnNewSuggestions() = 0; |
| 43 |
| 44 // Fired when the status of a suggestions category changed. When the status |
| 45 // changes to an unavailable status, the suggestions of the respective |
| 46 // category have been invalidated, which means that they must no longer be |
| 47 // displayed to the user. The UI must immediately clear any suggestions of |
| 48 // that category. |
| 49 virtual void OnCategoryStatusChanged( |
| 50 ContentSuggestionsCategory category, |
| 51 ContentSuggestionsCategoryStatus new_status) = 0; |
| 52 |
| 53 // Sent when the service is shutting down. After the service has shut down, |
| 54 // it will not provide any data anymore, though calling the getters is still |
| 55 // safe. |
| 56 virtual void ContentSuggestionsServiceShutdown() = 0; |
| 57 |
| 58 protected: |
| 59 virtual ~Observer() {} |
| 60 }; |
| 61 |
| 62 enum State { |
| 63 ENABLED, |
| 64 DISABLED, |
| 65 }; |
| 66 |
| 67 ContentSuggestionsService(State state); |
| 68 ~ContentSuggestionsService() override; |
| 69 |
| 70 // Inherited from KeyedService. |
| 71 void Shutdown() override; |
| 72 |
| 73 State state() { return state_; } |
| 74 |
| 75 // Gets all categories for which a provider is registered. The categories |
| 76 // may or may not be available, see |GetCategoryStatus()|. |
| 77 const std::vector<ContentSuggestionsCategory>& GetCategories() const { |
| 78 return categories_; |
| 79 } |
| 80 |
| 81 // Gets the status of a category. |
| 82 ContentSuggestionsCategoryStatus GetCategoryStatus( |
| 83 ContentSuggestionsCategory category) const; |
| 84 |
| 85 // Gets the available suggestions for a category. The result is empty if the |
| 86 // category is available and empty, but also if the category is unavailable |
| 87 // for any reason, see |GetCategoryStatus()|. |
| 88 const std::vector<ContentSuggestion>& GetSuggestionsForCategory( |
| 89 ContentSuggestionsCategory category) const; |
| 90 |
| 91 // Fetches the image for the suggestion with the given |suggestion_id| and |
| 92 // runs the |callback|. If that suggestion doesn't exist or the fetch fails, |
| 93 // the callback gets an empty image. |
| 94 void FetchSuggestionImage(const std::string& suggestion_id, |
| 95 const ImageFetchedCallback& callback); |
| 96 |
| 97 // Discards the suggestion with the given |suggestion_id|, if it exists. |
| 98 // This will not trigger an update through the observers. |
| 99 void DiscardSuggestion(const std::string& suggestion_id); |
| 100 |
| 101 // Observer accessors. |
| 102 void AddObserver(Observer* observer); |
| 103 void RemoveObserver(Observer* observer); |
| 104 |
| 105 // Registers a new ContentSuggestionsProvider. It must be ensured that at most |
| 106 // one provider is registered for every category and that this method is |
| 107 // called only once per provider. |
| 108 void RegisterProvider(ContentSuggestionsProvider* provider); |
| 109 |
| 110 // Only for debugging use through the internals page. |
| 111 // Removes all suggestions from all caches or internal stores in all |
| 112 // providers. It does, however, not remove any suggestions from the provider's |
| 113 // sources, so if their configuration hasn't changed, they should return the |
| 114 // same results when they fetch the next time. In particular, calling this |
| 115 // method will not mark any suggestions as discarded. |
| 116 void ClearCachedSuggestionsForDebugging(); |
| 117 |
| 118 // Only for debugging use through the internals page. Some providers |
| 119 // internally store a list of discarded suggestions to prevent them from |
| 120 // reappearing. This function clears all such lists in all providers, making |
| 121 // discarded suggestions reappear (only for certain providers). |
| 122 void ClearDiscardedSuggestionsForDebugging(); |
| 123 |
| 124 private: |
| 125 // Implementation of ContentSuggestionsProvider::Observer. |
| 126 void OnNewSuggestions(ContentSuggestionsCategory changed_category, |
| 127 std::vector<ContentSuggestion> suggestions) override; |
| 128 void OnCategoryStatusChanged( |
| 129 ContentSuggestionsCategory changed_category, |
| 130 ContentSuggestionsCategoryStatus new_status) override; |
| 131 void OnProviderShutdown(ContentSuggestionsProvider* provider) override; |
| 132 |
| 133 // Checks whether a provider for the given |category| is registered. |
| 134 bool IsCategoryRegistered(ContentSuggestionsCategory category) const; |
| 135 |
| 136 // Whether the content suggestions feature is enabled. |
| 137 State state_; |
| 138 |
| 139 // All current suggestion categories, in an order determined by the service. |
| 140 // Currently, this is simply the order in which the providers were registered. |
| 141 // This vector contains exactly the same categories as |providers_|. |
| 142 // TODO(pke) Implement a useful and consistent ordering for categories. |
| 143 std::vector<ContentSuggestionsCategory> categories_; |
| 144 |
| 145 // All current suggestions grouped by category. This contains an entry for |
| 146 // every category in |categories_| whose status is an available status. It may |
| 147 // contain an empty vector if the category is available but empty (or still |
| 148 // loading). |
| 149 std::map<ContentSuggestionsCategory, std::vector<ContentSuggestion>> |
| 150 suggestions_by_category_; |
| 151 |
| 152 // All registered providers. A provider may be contained multiple times, if it |
| 153 // provides multiple categories. The keys of this map are exactly the entries |
| 154 // of |categories_|. |
| 155 std::map<ContentSuggestionsCategory, ContentSuggestionsProvider*> providers_; |
| 156 |
| 157 // Map used to determine the category of a suggestion (of which only the ID |
| 158 // is available). This also determines the provider that delivered the |
| 159 // suggestion. |
| 160 std::map<std::string, ContentSuggestionsCategory> id_category_map_; |
| 161 |
| 162 base::ObserverList<Observer> observers_; |
| 163 |
| 164 const std::vector<ContentSuggestion> no_suggestions_; |
| 165 |
| 166 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 167 }; |
| 168 |
| 169 } // namespace ntp_snippets |
| 170 |
| 171 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
OLD | NEW |