| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "components/ntp_snippets/category.h" | 12 #include "components/ntp_snippets/category.h" |
| 13 #include "components/ntp_snippets/category_status.h" | 13 #include "components/ntp_snippets/category_status.h" |
| 14 #include "components/ntp_snippets/content_suggestion.h" | 14 #include "components/ntp_snippets/content_suggestion.h" |
| 15 | 15 |
| 16 namespace gfx { | 16 namespace gfx { |
| 17 class Image; | 17 class Image; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace ntp_snippets { | 20 namespace ntp_snippets { |
| 21 | 21 |
| 22 // Provides content suggestions from one particular source. | 22 // Provides content suggestions from one particular source. |
| 23 // A provider can provide suggestions for multiple ContentSuggestionCategories, | 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 | 24 // but for every category that it provides, it will be the only provider in the |
| 25 // system which provides suggestions for that category. | 25 // system which provides suggestions for that category. |
| 26 // A provider can be a keyed service, in which case it should notify the | 26 // Providers are created by the ContentSuggestionsServiceFactory and owned and |
| 27 // ContentSuggestionsService through the observer before it shuts down. | 27 // shut down by the ContentSuggestionsService. |
| 28 class ContentSuggestionsProvider { | 28 class ContentSuggestionsProvider { |
| 29 public: | 29 public: |
| 30 using ImageFetchedCallback = | 30 using ImageFetchedCallback = |
| 31 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; | 31 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; |
| 32 | 32 |
| 33 // The observer of a provider is notified when new data is available. | 33 // The observer of a provider is notified when new data is available. |
| 34 class Observer { | 34 class Observer { |
| 35 public: | 35 public: |
| 36 // Called when the available content changed. | 36 // Called when the available content changed. |
| 37 // If a provider provides suggestions for multiple categories, this callback | 37 // If a provider provides suggestions for multiple categories, this callback |
| (...skipping 18 matching lines...) Expand all Loading... |
| 56 // If |new_status| is any other value, it must match the value that is | 56 // If |new_status| is any other value, it must match the value that is |
| 57 // currently returned from the provider's |GetCategoryStatus(category)|. In | 57 // currently returned from the provider's |GetCategoryStatus(category)|. In |
| 58 // case the given |category| is not known yet, the calling |provider| will | 58 // case the given |category| is not known yet, the calling |provider| will |
| 59 // be registered as its provider. Whenever the status changes to an | 59 // be registered as its provider. Whenever the status changes to an |
| 60 // unavailable status, all suggestions in that category must immediately be | 60 // unavailable status, all suggestions in that category must immediately be |
| 61 // removed from all caches and from the UI, but the provider remains | 61 // removed from all caches and from the UI, but the provider remains |
| 62 // registered. | 62 // registered. |
| 63 virtual void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 63 virtual void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
| 64 Category category, | 64 Category category, |
| 65 CategoryStatus new_status) = 0; | 65 CategoryStatus new_status) = 0; |
| 66 | |
| 67 // Called when the provider needs to shut down and will not deliver any | |
| 68 // suggestions anymore. | |
| 69 virtual void OnProviderShutdown(ContentSuggestionsProvider* provider) = 0; | |
| 70 }; | 66 }; |
| 71 | 67 |
| 72 // Sets an observer which is notified about changes to the available | 68 virtual ~ContentSuggestionsProvider(); |
| 73 // suggestions, or removes it by passing a nullptr. The provider does not take | |
| 74 // ownership of the observer and the observer must outlive this provider. | |
| 75 virtual void SetObserver(Observer* observer) = 0; | |
| 76 | 69 |
| 77 // Returns the categories provided by this provider. | 70 // Returns the categories provided by this provider. |
| 78 // When the set of provided categories changes, the Observer is notified | 71 // When the set of provided categories changes, the Observer is notified |
| 79 // through |OnNewSuggestions| or |OnCategoryStatusChanged| for added | 72 // through |OnNewSuggestions| or |OnCategoryStatusChanged| for added |
| 80 // categories, and through |OnCategoryStatusChanged| with parameter | 73 // categories, and through |OnCategoryStatusChanged| with parameter |
| 81 // NOT_PROVIDED for removed categories. | 74 // NOT_PROVIDED for removed categories. |
| 82 virtual std::vector<Category> GetProvidedCategories() = 0; | 75 virtual std::vector<Category> GetProvidedCategories() = 0; |
| 83 | 76 |
| 84 // Determines the status of the given |category|, see CategoryStatus. | 77 // Determines the status of the given |category|, see CategoryStatus. |
| 85 virtual CategoryStatus GetCategoryStatus(Category category) = 0; | 78 virtual CategoryStatus GetCategoryStatus(Category category) = 0; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 101 // fetch starts from scratch. | 94 // fetch starts from scratch. |
| 102 virtual void ClearCachedSuggestionsForDebugging() = 0; | 95 virtual void ClearCachedSuggestionsForDebugging() = 0; |
| 103 | 96 |
| 104 // Used only for debugging purposes. Clears the cache of dismissed | 97 // Used only for debugging purposes. Clears the cache of dismissed |
| 105 // suggestions, if present, so that no suggestions are suppressed. This does | 98 // suggestions, if present, so that no suggestions are suppressed. This does |
| 106 // not necessarily make previously dismissed suggestions reappear, as they may | 99 // not necessarily make previously dismissed suggestions reappear, as they may |
| 107 // have been permanently deleted, depending on the provider implementation. | 100 // have been permanently deleted, depending on the provider implementation. |
| 108 virtual void ClearDismissedSuggestionsForDebugging() = 0; | 101 virtual void ClearDismissedSuggestionsForDebugging() = 0; |
| 109 | 102 |
| 110 protected: | 103 protected: |
| 111 ContentSuggestionsProvider(CategoryFactory* category_factory); | 104 ContentSuggestionsProvider(Observer* observer, |
| 112 virtual ~ContentSuggestionsProvider(); | 105 CategoryFactory* category_factory); |
| 113 | 106 |
| 114 // Creates a unique ID. The given |within_category_id| must be unique among | 107 // Creates a unique ID. The given |within_category_id| must be unique among |
| 115 // all suggestion IDs from this provider for the given |category|. This method | 108 // all suggestion IDs from this provider for the given |category|. This method |
| 116 // combines it with the |category| to form an ID that is unique | 109 // combines it with the |category| to form an ID that is unique |
| 117 // application-wide, because this provider is the only one that provides | 110 // application-wide, because this provider is the only one that provides |
| 118 // suggestions for that category. | 111 // suggestions for that category. |
| 119 std::string MakeUniqueID(Category category, | 112 std::string MakeUniqueID(Category category, |
| 120 const std::string& within_category_id); | 113 const std::string& within_category_id); |
| 114 |
| 121 // Reverse functions for MakeUniqueID() | 115 // Reverse functions for MakeUniqueID() |
| 122 Category GetCategoryFromUniqueID(const std::string& unique_id); | 116 Category GetCategoryFromUniqueID(const std::string& unique_id); |
| 123 std::string GetWithinCategoryIDFromUniqueID(const std::string& unique_id); | 117 std::string GetWithinCategoryIDFromUniqueID(const std::string& unique_id); |
| 124 | 118 |
| 119 Observer* observer() const { return observer_; } |
| 125 CategoryFactory* category_factory() const { return category_factory_; } | 120 CategoryFactory* category_factory() const { return category_factory_; } |
| 126 | 121 |
| 127 private: | 122 private: |
| 123 Observer* observer_; |
| 128 CategoryFactory* category_factory_; | 124 CategoryFactory* category_factory_; |
| 129 }; | 125 }; |
| 130 | 126 |
| 131 } // namespace ntp_snippets | 127 } // namespace ntp_snippets |
| 132 | 128 |
| 133 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ | 129 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_PROVIDER_H_ |
| OLD | NEW |