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