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 for multiple categories, this callback |
| 38 // is called once per category. The |suggestions| parameter always contains |
| 39 // the full list of currently available suggestions for that category, i.e., |
| 40 // an empty list will remove all suggestions from the given category. Note |
| 41 // that to clear them from the UI immediately, the provider needs to change |
| 42 // the status of the respective category. |
| 43 // IDs for the ContentSuggestions should be generated with |
| 44 // |MakeUniqueID(..)| below. |
| 45 virtual void OnNewSuggestions( |
| 46 ContentSuggestionsCategory changed_category, |
| 47 std::vector<ContentSuggestion> suggestions) = 0; |
| 48 |
| 49 // Called when the status of a category changed. |
| 50 // |new_status| must be the value that is currently returned from the |
| 51 // provider's |GetCategoryStatus(category)| below. |
| 52 // Whenever the status changes to an unavailable status, all suggestions in |
| 53 // that category must immediately be removed from all caches and from the |
| 54 // UI. |
| 55 virtual void OnCategoryStatusChanged( |
| 56 ContentSuggestionsCategory changed_category, |
| 57 ContentSuggestionsCategoryStatus new_status) = 0; |
| 58 |
| 59 // Called when the provider needs to shut down and will not deliver any |
| 60 // suggestions anymore. |
| 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 virtual void DiscardSuggestion(const std::string& suggestion_id) = 0; |
| 78 |
| 79 // Fetches the image for the suggestion with the given ID and returns it |
| 80 // through the callback. This fetch may occur locally or from the internet. |
| 81 virtual void FetchSuggestionImage(const std::string& suggestion_id, |
| 82 const ImageFetchedCallback& callback) = 0; |
| 83 |
| 84 // Used only for debugging purposes. Clears all caches so that the next |
| 85 // fetch starts from scratch. |
| 86 virtual void ClearCachedSuggestionsForDebugging() = 0; |
| 87 |
| 88 // Used only for debugging purposes. Clears the cache of discarded |
| 89 // suggestions, if present, so that no suggestions are suppressed. This does |
| 90 // not necessarily make previously discarded suggestions reappear, as they may |
| 91 // have been permanently deleted, depending on the provider implementation. |
| 92 virtual void ClearDiscardedSuggestionsForDebugging() = 0; |
| 93 |
| 94 const std::vector<ContentSuggestionsCategory>& provided_categories() const { |
| 95 return provided_categories_; |
| 96 } |
| 97 |
| 98 protected: |
| 99 ContentSuggestionsProvider( |
| 100 const std::vector<ContentSuggestionsCategory>& provided_categories); |
| 101 virtual ~ContentSuggestionsProvider(); |
| 102 |
| 103 // Creates a unique ID. The given |within_category_id| must be unique among |
| 104 // all suggestion IDs from this provider for the given |category|. This method |
| 105 // combines it with the |category| to form an ID that is unique |
| 106 // application-wide, because this provider is the only one that provides |
| 107 // suggestions for that category. |
| 108 static std::string MakeUniqueID(ContentSuggestionsCategory category, |
| 109 const std::string& within_category_id); |
| 110 |
| 111 private: |
| 112 const std::vector<ContentSuggestionsCategory> provided_categories_; |
| 113 }; |
| 114 |
| 115 } // namespace ntp_snippets |
| 116 |
| 117 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ |
OLD | NEW |