Chromium Code Reviews| 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_SERVICE_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
| 17 #include "components/ntp_snippets/content_suggestions_category_factory.h" | |
| 17 #include "components/ntp_snippets/content_suggestions_category_status.h" | 18 #include "components/ntp_snippets/content_suggestions_category_status.h" |
| 18 #include "components/ntp_snippets/content_suggestions_provider.h" | 19 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 19 | 20 |
| 20 namespace gfx { | 21 namespace gfx { |
| 21 class Image; | 22 class Image; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace ntp_snippets { | 25 namespace ntp_snippets { |
| 25 | 26 |
| 26 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | 27 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 55 | 56 |
| 56 protected: | 57 protected: |
| 57 virtual ~Observer() {} | 58 virtual ~Observer() {} |
| 58 }; | 59 }; |
| 59 | 60 |
| 60 enum State { | 61 enum State { |
| 61 ENABLED, | 62 ENABLED, |
| 62 DISABLED, | 63 DISABLED, |
| 63 }; | 64 }; |
| 64 | 65 |
| 66 // This is just an arbitrary ordering by ID, used by the maps in this class, | |
| 67 // because the ordering needs to be constant for maps. | |
| 68 struct CompareCategoriesByID { | |
|
Marc Treib
2016/07/28 11:41:46
This should be private.
Philipp Keck
2016/07/28 13:50:55
Done.
| |
| 69 bool operator()(const ContentSuggestionsCategory& left, | |
| 70 const ContentSuggestionsCategory& right) const; | |
| 71 }; | |
| 72 | |
| 65 ContentSuggestionsService(State state); | 73 ContentSuggestionsService(State state); |
| 66 ~ContentSuggestionsService() override; | 74 ~ContentSuggestionsService() override; |
| 67 | 75 |
| 68 // Inherited from KeyedService. | 76 // Inherited from KeyedService. |
| 69 void Shutdown() override; | 77 void Shutdown() override; |
| 70 | 78 |
| 71 State state() { return state_; } | 79 State state() { return state_; } |
| 72 | 80 |
| 73 // Gets all categories for which a provider is registered. The categories | 81 // Gets all categories for which a provider is registered. The categories |
| 74 // may or may not be available, see |GetCategoryStatus()|. | 82 // may or may not be available, see |GetCategoryStatus()|. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 // same results when they fetch the next time. In particular, calling this | 120 // same results when they fetch the next time. In particular, calling this |
| 113 // method will not mark any suggestions as dismissed. | 121 // method will not mark any suggestions as dismissed. |
| 114 void ClearCachedSuggestionsForDebugging(); | 122 void ClearCachedSuggestionsForDebugging(); |
| 115 | 123 |
| 116 // Only for debugging use through the internals page. Some providers | 124 // Only for debugging use through the internals page. Some providers |
| 117 // internally store a list of dismissed suggestions to prevent them from | 125 // internally store a list of dismissed suggestions to prevent them from |
| 118 // reappearing. This function clears all such lists in all providers, making | 126 // reappearing. This function clears all such lists in all providers, making |
| 119 // dismissed suggestions reappear (only for certain providers). | 127 // dismissed suggestions reappear (only for certain providers). |
| 120 void ClearDismissedSuggestionsForDebugging(); | 128 void ClearDismissedSuggestionsForDebugging(); |
| 121 | 129 |
| 130 ContentSuggestionsCategoryFactory* category_factory() { | |
|
Marc Treib
2016/07/28 11:41:46
Clients aren't supposed to register new categories
Philipp Keck
2016/07/28 13:50:54
Yes, the providers use this to register categories
Marc Treib
2016/07/28 14:31:17
Ah - technically the providers just get a Factory
Philipp Keck
2016/07/28 14:55:47
Acknowledged.
| |
| 131 return &category_factory_; | |
| 132 } | |
| 133 | |
| 122 private: | 134 private: |
| 123 friend class ContentSuggestionsServiceTest; | 135 friend class ContentSuggestionsServiceTest; |
| 124 | 136 |
| 125 // Implementation of ContentSuggestionsProvider::Observer. | 137 // Implementation of ContentSuggestionsProvider::Observer. |
| 126 void OnNewSuggestions(ContentSuggestionsCategory changed_category, | 138 void OnNewSuggestions(ContentSuggestionsCategory changed_category, |
| 127 std::vector<ContentSuggestion> suggestions) override; | 139 std::vector<ContentSuggestion> suggestions) override; |
| 128 void OnCategoryStatusChanged( | 140 void OnCategoryStatusChanged( |
| 129 ContentSuggestionsCategory changed_category, | 141 ContentSuggestionsCategory changed_category, |
| 130 ContentSuggestionsCategoryStatus new_status) override; | 142 ContentSuggestionsCategoryStatus new_status) override; |
| 131 void OnProviderShutdown(ContentSuggestionsProvider* provider) override; | 143 void OnProviderShutdown(ContentSuggestionsProvider* provider) override; |
| 132 | 144 |
| 133 // Checks whether a provider for the given |category| is registered. | 145 // Checks whether a provider for the given |category| is registered. |
| 134 bool IsCategoryRegistered(ContentSuggestionsCategory category) const; | 146 bool IsCategoryRegistered(ContentSuggestionsCategory category) const; |
| 135 | 147 |
| 136 // Fires the OnCategoryStatusChanged event for the given |category|. | 148 // Fires the OnCategoryStatusChanged event for the given |category|. |
| 137 void NotifyCategoryStatusChanged(ContentSuggestionsCategory category); | 149 void NotifyCategoryStatusChanged(ContentSuggestionsCategory category); |
| 138 | 150 |
| 139 // Whether the content suggestions feature is enabled. | 151 // Whether the content suggestions feature is enabled. |
| 140 State state_; | 152 State state_; |
| 141 | 153 |
| 154 // Provides new categories and an order for them. | |
|
Marc Treib
2016/07/28 11:41:46
Provides all categories really, no?
Philipp Keck
2016/07/28 13:50:55
True. I put "new and existing" because "all" could
| |
| 155 ContentSuggestionsCategoryFactory category_factory_; | |
| 156 | |
| 142 // All registered providers. A provider may be contained multiple times, if it | 157 // All registered providers. A provider may be contained multiple times, if it |
| 143 // provides multiple categories. The keys of this map are exactly the entries | 158 // provides multiple categories. The keys of this map are exactly the entries |
| 144 // of |categories_|. | 159 // of |categories_|. |
| 145 std::map<ContentSuggestionsCategory, ContentSuggestionsProvider*> providers_; | 160 std::map<ContentSuggestionsCategory, |
| 161 ContentSuggestionsProvider*, | |
| 162 CompareCategoriesByID> | |
| 163 providers_; | |
| 146 | 164 |
| 147 // All current suggestion categories, in an order determined by the service. | 165 // All current suggestion categories, in an order determined by the |
| 148 // Currently, this is simply the order in which the providers were registered. | 166 // |category_factory_|. This vector contains exactly the same categories as |
| 149 // This vector contains exactly the same categories as |providers_|. | 167 // |providers_|. |
| 150 // TODO(pke): Implement a useful and consistent ordering for categories. | |
| 151 std::vector<ContentSuggestionsCategory> categories_; | 168 std::vector<ContentSuggestionsCategory> categories_; |
| 152 | 169 |
| 153 // All current suggestions grouped by category. This contains an entry for | 170 // All current suggestions grouped by category. This contains an entry for |
| 154 // every category in |categories_| whose status is an available status. It may | 171 // every category in |categories_| whose status is an available status. It may |
| 155 // contain an empty vector if the category is available but empty (or still | 172 // contain an empty vector if the category is available but empty (or still |
| 156 // loading). | 173 // loading). |
| 157 std::map<ContentSuggestionsCategory, std::vector<ContentSuggestion>> | 174 std::map<ContentSuggestionsCategory, |
| 175 std::vector<ContentSuggestion>, | |
| 176 CompareCategoriesByID> | |
| 158 suggestions_by_category_; | 177 suggestions_by_category_; |
| 159 | 178 |
| 160 // Map used to determine the category of a suggestion (of which only the ID | 179 // Map used to determine the category of a suggestion (of which only the ID |
| 161 // is available). This also determines the provider that delivered the | 180 // is available). This also determines the provider that delivered the |
| 162 // suggestion. | 181 // suggestion. |
| 163 std::map<std::string, ContentSuggestionsCategory> id_category_map_; | 182 std::map<std::string, ContentSuggestionsCategory> id_category_map_; |
| 164 | 183 |
| 165 base::ObserverList<Observer> observers_; | 184 base::ObserverList<Observer> observers_; |
| 166 | 185 |
| 167 const std::vector<ContentSuggestion> no_suggestions_; | 186 const std::vector<ContentSuggestion> no_suggestions_; |
| 168 | 187 |
| 169 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 188 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 170 }; | 189 }; |
| 171 | 190 |
| 172 } // namespace ntp_snippets | 191 } // namespace ntp_snippets |
| 173 | 192 |
| 174 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 193 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |