| 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 <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 private: | 119 private: |
| 120 friend class ContentSuggestionsServiceTest; | 120 friend class ContentSuggestionsServiceTest; |
| 121 | 121 |
| 122 // This is just an arbitrary ordering by ID, used by the maps in this class, | 122 // This is just an arbitrary ordering by ID, used by the maps in this class, |
| 123 // because the ordering needs to be constant for maps. | 123 // because the ordering needs to be constant for maps. |
| 124 struct CompareCategoriesByID { | 124 struct CompareCategoriesByID { |
| 125 bool operator()(const Category& left, const Category& right) const; | 125 bool operator()(const Category& left, const Category& right) const; |
| 126 }; | 126 }; |
| 127 | 127 |
| 128 // Implementation of ContentSuggestionsProvider::Observer. | 128 // Implementation of ContentSuggestionsProvider::Observer. |
| 129 void OnNewSuggestions(Category changed_category, | 129 void OnNewSuggestions(ContentSuggestionsProvider* provider, |
| 130 Category category, |
| 130 std::vector<ContentSuggestion> suggestions) override; | 131 std::vector<ContentSuggestion> suggestions) override; |
| 131 void OnCategoryStatusChanged(Category changed_category, | 132 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
| 133 Category category, |
| 132 CategoryStatus new_status) override; | 134 CategoryStatus new_status) override; |
| 133 void OnProviderShutdown(ContentSuggestionsProvider* provider) override; | 135 void OnProviderShutdown(ContentSuggestionsProvider* provider) override; |
| 134 | 136 |
| 135 // Checks whether a provider for the given |category| is registered. | 137 // Registers the given |provider| for the given |category|, unless it is |
| 136 bool IsCategoryRegistered(Category category) const; | 138 // already registered. Returns true if the category was newly registered or |
| 139 // false if it was present before. |
| 140 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, |
| 141 Category category); |
| 137 | 142 |
| 138 // Fires the OnCategoryStatusChanged event for the given |category|. | 143 // Fires the OnCategoryStatusChanged event for the given |category|. |
| 139 void NotifyCategoryStatusChanged(Category category); | 144 void NotifyCategoryStatusChanged(Category category); |
| 140 | 145 |
| 141 // Whether the content suggestions feature is enabled. | 146 // Whether the content suggestions feature is enabled. |
| 142 State state_; | 147 State state_; |
| 143 | 148 |
| 144 // Provides new and existing categories and an order for them. | 149 // Provides new and existing categories and an order for them. |
| 145 CategoryFactory category_factory_; | 150 CategoryFactory category_factory_; |
| 146 | 151 |
| 147 // All registered providers. A provider may be contained multiple times, if it | 152 // All registered providers. A provider may be contained multiple times, if it |
| 148 // provides multiple categories. The keys of this map are exactly the entries | 153 // provides multiple categories. The keys of this map are exactly the entries |
| 149 // of |categories_|. | 154 // of |categories_|. |
| 150 std::map<Category, ContentSuggestionsProvider*, CompareCategoriesByID> | 155 std::map<Category, ContentSuggestionsProvider*, CompareCategoriesByID> |
| 151 providers_; | 156 providers_by_category_; |
| 152 | 157 |
| 153 // All current suggestion categories, in an order determined by the | 158 // All current suggestion categories, in an order determined by the |
| 154 // |category_factory_|. This vector contains exactly the same categories as | 159 // |category_factory_|. This vector contains exactly the same categories as |
| 155 // |providers_|. | 160 // |providers_|. |
| 156 std::vector<Category> categories_; | 161 std::vector<Category> categories_; |
| 157 | 162 |
| 158 // All current suggestions grouped by category. This contains an entry for | 163 // All current suggestions grouped by category. This contains an entry for |
| 159 // every category in |categories_| whose status is an available status. It may | 164 // every category in |categories_| whose status is an available status. It may |
| 160 // contain an empty vector if the category is available but empty (or still | 165 // contain an empty vector if the category is available but empty (or still |
| 161 // loading). | 166 // loading). |
| 162 std::map<Category, std::vector<ContentSuggestion>, CompareCategoriesByID> | 167 std::map<Category, std::vector<ContentSuggestion>, CompareCategoriesByID> |
| 163 suggestions_by_category_; | 168 suggestions_by_category_; |
| 164 | 169 |
| 165 // Map used to determine the category of a suggestion (of which only the ID | 170 // Map used to determine the category of a suggestion (of which only the ID |
| 166 // is available). This also determines the provider that delivered the | 171 // is available). This also determines the provider that delivered the |
| 167 // suggestion. | 172 // suggestion. |
| 168 std::map<std::string, Category> id_category_map_; | 173 std::map<std::string, Category> id_category_map_; |
| 169 | 174 |
| 170 base::ObserverList<Observer> observers_; | 175 base::ObserverList<Observer> observers_; |
| 171 | 176 |
| 172 const std::vector<ContentSuggestion> no_suggestions_; | 177 const std::vector<ContentSuggestion> no_suggestions_; |
| 173 | 178 |
| 174 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 179 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 175 }; | 180 }; |
| 176 | 181 |
| 177 } // namespace ntp_snippets | 182 } // namespace ntp_snippets |
| 178 | 183 |
| 179 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 184 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |