Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_NTP_SNIPPETS_SERVICE_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 // (Re)schedules the periodic fetching of snippets. This is necessary because | 129 // (Re)schedules the periodic fetching of snippets. This is necessary because |
| 130 // the schedule depends on the time of day. | 130 // the schedule depends on the time of day. |
| 131 void RescheduleFetching(); | 131 void RescheduleFetching(); |
| 132 | 132 |
| 133 // ContentSuggestionsProvider implementation | 133 // ContentSuggestionsProvider implementation |
| 134 CategoryStatus GetCategoryStatus(Category category) override; | 134 CategoryStatus GetCategoryStatus(Category category) override; |
| 135 CategoryInfo GetCategoryInfo(Category category) override; | 135 CategoryInfo GetCategoryInfo(Category category) override; |
| 136 void DismissSuggestion(const std::string& suggestion_id) override; | 136 void DismissSuggestion(const std::string& suggestion_id) override; |
| 137 void FetchSuggestionImage(const std::string& suggestion_id, | 137 void FetchSuggestionImage(const std::string& suggestion_id, |
| 138 const ImageFetchedCallback& callback) override; | 138 const ImageFetchedCallback& callback) override; |
| 139 void ClearCachedSuggestionsForDebugging(Category category) override; | 139 void ClearCachedSuggestionsForDebugging(Category category_id) override; |
|
Marc Treib
2016/08/22 15:06:47
?
sfiera
2016/08/24 14:35:57
Done.
| |
| 140 std::vector<ContentSuggestion> GetDismissedSuggestionsForDebugging( | 140 std::vector<ContentSuggestion> GetDismissedSuggestionsForDebugging( |
| 141 Category category) override; | 141 Category category) override; |
| 142 void ClearDismissedSuggestionsForDebugging(Category category) override; | 142 void ClearDismissedSuggestionsForDebugging(Category category) override; |
| 143 | 143 |
| 144 // Returns the lists of suggestion hosts the snippets are restricted to. | 144 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 145 std::set<std::string> GetSuggestionsHosts() const; | 145 std::set<std::string> GetSuggestionsHosts() const; |
| 146 | 146 |
| 147 // Returns the maximum number of snippets that will be shown at once. | 147 // Returns the maximum number of snippets that will be shown at once. |
| 148 static int GetMaxSnippetCountForTesting(); | 148 static int GetMaxSnippetCountForTesting(); |
| 149 | 149 |
| 150 // Available snippets, only for unit tests. | 150 // Available snippets, only for unit tests. |
| 151 const NTPSnippet::PtrVector& GetSnippetsForTesting() const { | 151 const NTPSnippet::PtrVector& GetSnippetsForTesting(Category category) const { |
| 152 return snippets_; | 152 return categories_.find(category)->second.snippets; |
| 153 } | 153 } |
| 154 | 154 |
| 155 private: | 155 private: |
| 156 friend class NTPSnippetsServiceTest; | 156 friend class NTPSnippetsServiceTest; |
| 157 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); | 157 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); |
| 158 | 158 |
| 159 // Possible state transitions: | 159 // Possible state transitions: |
| 160 // NOT_INITED --------+ | 160 // NOT_INITED --------+ |
| 161 // / \ | | 161 // / \ | |
| 162 // v v | | 162 // v v | |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 201 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 202 void OnDatabaseError(); | 202 void OnDatabaseError(); |
| 203 | 203 |
| 204 // Callback for the SuggestionsService. | 204 // Callback for the SuggestionsService. |
| 205 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 205 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 206 | 206 |
| 207 // Callback for the NTPSnippetsFetcher. | 207 // Callback for the NTPSnippetsFetcher. |
| 208 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 208 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 209 | 209 |
| 210 // Merges newly available snippets with the previously available list. | 210 // Merges newly available snippets with the previously available list. |
| 211 void MergeSnippets(NTPSnippet::PtrVector new_snippets); | 211 void MergeSnippets(Category category, NTPSnippet::PtrVector new_snippets); |
| 212 | 212 |
| 213 std::set<std::string> GetSnippetHostsFromPrefs() const; | 213 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 214 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 214 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 215 | 215 |
| 216 // Removes the expired snippets (including dismissed) from the service and the | 216 // Removes the expired snippets (including dismissed) from the service and the |
| 217 // database, and schedules another pass for the next expiration. | 217 // database, and schedules another pass for the next expiration. |
| 218 void ClearExpiredSnippets(); | 218 void ClearExpiredSnippets(Category category); |
| 219 | 219 |
| 220 // Completes the initialization phase of the service, registering the last | 220 // Completes the initialization phase of the service, registering the last |
| 221 // observers. This is done after construction, once the database is loaded. | 221 // observers. This is done after construction, once the database is loaded. |
| 222 void FinishInitialization(); | 222 void FinishInitialization(); |
| 223 | 223 |
| 224 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, | 224 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, |
| 225 const std::string& snippet_id, | 225 const std::string& suggestion_id, |
| 226 std::string data); | 226 std::string data); |
| 227 | 227 |
| 228 void OnSnippetImageDecodedFromDatabase(const ImageFetchedCallback& callback, | 228 void OnSnippetImageDecodedFromDatabase(const ImageFetchedCallback& callback, |
| 229 const std::string& snippet_id, | 229 const std::string& suggestion_id, |
| 230 const gfx::Image& image); | 230 const gfx::Image& image); |
| 231 | 231 |
| 232 void FetchSnippetImageFromNetwork(const std::string& snippet_id, | 232 void FetchSnippetImageFromNetwork(const std::string& suggestion_id, |
| 233 const ImageFetchedCallback& callback); | 233 const ImageFetchedCallback& callback); |
| 234 | 234 |
| 235 void OnSnippetImageDecodedFromNetwork(const ImageFetchedCallback& callback, | 235 void OnSnippetImageDecodedFromNetwork(const ImageFetchedCallback& callback, |
| 236 const std::string& snippet_id, | 236 const std::string& snippet_id, |
| 237 const gfx::Image& image); | 237 const gfx::Image& image); |
| 238 | 238 |
| 239 // Triggers a state transition depending on the provided reason to be | 239 // Triggers a state transition depending on the provided reason to be |
| 240 // disabled (or lack thereof). This method is called when a change is detected | 240 // disabled (or lack thereof). This method is called when a change is detected |
| 241 // by |snippets_status_service_|. | 241 // by |snippets_status_service_|. |
| 242 void OnDisabledReasonChanged(DisabledReason disabled_reason); | 242 void OnDisabledReasonChanged(DisabledReason disabled_reason); |
| 243 | 243 |
| 244 // Verifies state transitions (see |State|'s documentation) and applies them. | 244 // Verifies state transitions (see |State|'s documentation) and applies them. |
| 245 // Also updates the provider status. Does nothing except updating the provider | 245 // Also updates the provider status. Does nothing except updating the provider |
| 246 // status if called with the current state. | 246 // status if called with the current state. |
| 247 void EnterState(State state, CategoryStatus status); | 247 void EnterState(State state); |
| 248 | 248 |
| 249 // Enables the service and triggers a fetch if required. Do not call directly, | 249 // Enables the service and triggers a fetch if required. Do not call directly, |
| 250 // use |EnterState| instead. | 250 // use |EnterState| instead. |
| 251 void EnterStateEnabled(bool fetch_snippets); | 251 void EnterStateEnabled(bool fetch_snippets); |
| 252 | 252 |
| 253 // Disables the service. Do not call directly, use |EnterState| instead. | 253 // Disables the service. Do not call directly, use |EnterState| instead. |
| 254 void EnterStateDisabled(); | 254 void EnterStateDisabled(); |
| 255 | 255 |
| 256 // Disables the service permanently because an unrecoverable error occurred. | 256 // Disables the service permanently because an unrecoverable error occurred. |
| 257 // Do not call directly, use |EnterState| instead. | 257 // Do not call directly, use |EnterState| instead. |
| 258 void EnterStateError(); | 258 void EnterStateError(); |
| 259 | 259 |
| 260 // Converts the cached snippets to article content suggestions and notifies | 260 // Converts the cached snippets to article content suggestions and notifies |
| 261 // the observers. | 261 // the observers. |
| 262 void NotifyNewSuggestions(); | 262 void NotifyNewSuggestions(); |
| 263 | 263 |
| 264 // Updates the internal status |category_status_| and notifies the content | 264 // Updates the internal status for |category| to |category_status_| and |
| 265 // suggestions observer if it changed. | 265 // notifies the content suggestions observer if it changed. |
| 266 void UpdateCategoryStatus(CategoryStatus status); | 266 void UpdateCategoryStatus(Category category, CategoryStatus status); |
| 267 void UpdateAllCategoryStatus(CategoryStatus status); | |
|
Marc Treib
2016/08/22 15:06:47
Add comment?
sfiera
2016/08/24 14:35:57
Done.
| |
| 267 | 268 |
| 268 State state_; | 269 State state_; |
| 269 | 270 |
| 270 CategoryStatus category_status_; | |
| 271 | |
| 272 PrefService* pref_service_; | 271 PrefService* pref_service_; |
| 273 | 272 |
| 274 suggestions::SuggestionsService* suggestions_service_; | 273 suggestions::SuggestionsService* suggestions_service_; |
| 275 | 274 |
| 276 // All current suggestions (i.e. not dismissed ones). | 275 const Category articles_category_; |
| 277 NTPSnippet::PtrVector snippets_; | |
| 278 | 276 |
| 279 // Suggestions that the user dismissed. We keep these around until they expire | 277 struct CategoryContent { |
| 280 // so we won't re-add them on the next fetch. | 278 CategoryStatus status = CategoryStatus::INITIALIZING; |
| 281 NTPSnippet::PtrVector dismissed_snippets_; | 279 |
| 280 // All current suggestions (i.e. not dismissed ones). | |
| 281 NTPSnippet::PtrVector snippets; | |
| 282 | |
| 283 // Suggestions that the user dismissed. We keep these around until they | |
| 284 // expire so we won't re-add them on the next fetch. | |
| 285 NTPSnippet::PtrVector dismissed; | |
| 286 }; | |
| 287 std::map<Category, CategoryContent, Category::CompareByID> categories_; | |
| 282 | 288 |
| 283 // The ISO 639-1 code of the language used by the application. | 289 // The ISO 639-1 code of the language used by the application. |
| 284 const std::string application_language_code_; | 290 const std::string application_language_code_; |
| 285 | 291 |
| 286 // Scheduler for fetching snippets. Not owned. | 292 // Scheduler for fetching snippets. Not owned. |
| 287 NTPSnippetsScheduler* scheduler_; | 293 NTPSnippetsScheduler* scheduler_; |
| 288 | 294 |
| 289 // The subscription to the SuggestionsService. When the suggestions change, | 295 // The subscription to the SuggestionsService. When the suggestions change, |
| 290 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 296 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
| 291 // update to the set of snippets. | 297 // update to the set of snippets. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 305 // The database for persisting snippets. | 311 // The database for persisting snippets. |
| 306 std::unique_ptr<NTPSnippetsDatabase> database_; | 312 std::unique_ptr<NTPSnippetsDatabase> database_; |
| 307 | 313 |
| 308 // The service that provides events and data about the signin and sync state. | 314 // The service that provides events and data about the signin and sync state. |
| 309 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; | 315 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; |
| 310 | 316 |
| 311 // Set to true if FetchSnippets is called before the database has been loaded. | 317 // Set to true if FetchSnippets is called before the database has been loaded. |
| 312 // The fetch will be executed after the database load finishes. | 318 // The fetch will be executed after the database load finishes. |
| 313 bool fetch_after_load_; | 319 bool fetch_after_load_; |
| 314 | 320 |
| 315 const Category provided_category_; | |
| 316 | |
| 317 // Request throttler for limiting requests to thumbnail images. | 321 // Request throttler for limiting requests to thumbnail images. |
| 318 RequestThrottler thumbnail_requests_throttler_; | 322 RequestThrottler thumbnail_requests_throttler_; |
| 319 | 323 |
| 320 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 324 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 321 }; | 325 }; |
| 322 | 326 |
| 323 } // namespace ntp_snippets | 327 } // namespace ntp_snippets |
| 324 | 328 |
| 325 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 329 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |