| 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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 const DismissedSuggestionsCallback& callback) override; | 146 const DismissedSuggestionsCallback& callback) override; |
| 147 void ClearDismissedSuggestionsForDebugging(Category category) override; | 147 void ClearDismissedSuggestionsForDebugging(Category category) override; |
| 148 | 148 |
| 149 // Returns the lists of suggestion hosts the snippets are restricted to. | 149 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 150 std::set<std::string> GetSuggestionsHosts() const; | 150 std::set<std::string> GetSuggestionsHosts() const; |
| 151 | 151 |
| 152 // Returns the maximum number of snippets that will be shown at once. | 152 // Returns the maximum number of snippets that will be shown at once. |
| 153 static int GetMaxSnippetCountForTesting(); | 153 static int GetMaxSnippetCountForTesting(); |
| 154 | 154 |
| 155 // Available snippets, only for unit tests. | 155 // Available snippets, only for unit tests. |
| 156 const NTPSnippet::PtrVector& GetSnippetsForTesting(Category category) const { | 156 const NTPSnippet::PtrVector& GetSnippetsForTesting() const { |
| 157 return categories_.find(category)->second.snippets; | 157 return snippets_; |
| 158 } | 158 } |
| 159 | 159 |
| 160 private: | 160 private: |
| 161 friend class NTPSnippetsServiceTest; | 161 friend class NTPSnippetsServiceTest; |
| 162 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); | 162 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); |
| 163 | 163 |
| 164 // Possible state transitions: | 164 // Possible state transitions: |
| 165 // NOT_INITED --------+ | 165 // NOT_INITED --------+ |
| 166 // / \ | | 166 // / \ | |
| 167 // v v | | 167 // v v | |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 215 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 216 void OnDatabaseError(); | 216 void OnDatabaseError(); |
| 217 | 217 |
| 218 // Callback for the SuggestionsService. | 218 // Callback for the SuggestionsService. |
| 219 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 219 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 220 | 220 |
| 221 // Callback for the NTPSnippetsFetcher. | 221 // Callback for the NTPSnippetsFetcher. |
| 222 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 222 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 223 | 223 |
| 224 // Merges newly available snippets with the previously available list. | 224 // Merges newly available snippets with the previously available list. |
| 225 void MergeSnippets(Category category, NTPSnippet::PtrVector new_snippets); | 225 void MergeSnippets(NTPSnippet::PtrVector new_snippets); |
| 226 | 226 |
| 227 std::set<std::string> GetSnippetHostsFromPrefs() const; | 227 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 228 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 228 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 229 | 229 |
| 230 // Removes the expired snippets (including dismissed) from the service and the | 230 // Removes the expired snippets (including dismissed) from the service and the |
| 231 // database, and schedules another pass for the next expiration. | 231 // database, and schedules another pass for the next expiration. |
| 232 void ClearExpiredSnippets(); | 232 void ClearExpiredSnippets(); |
| 233 | 233 |
| 234 // Clears all stored snippets and updates the observer. | 234 // Clears all stored snippets and updates the observer. |
| 235 void NukeAllSnippets(); | 235 void NukeAllSnippets(); |
| 236 | 236 |
| 237 // Completes the initialization phase of the service, registering the last | 237 // Completes the initialization phase of the service, registering the last |
| 238 // observers. This is done after construction, once the database is loaded. | 238 // observers. This is done after construction, once the database is loaded. |
| 239 void FinishInitialization(); | 239 void FinishInitialization(); |
| 240 | 240 |
| 241 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, | 241 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, |
| 242 const std::string& suggestion_id, | 242 const std::string& snippet_id, |
| 243 std::string data); | 243 std::string data); |
| 244 | 244 |
| 245 void OnSnippetImageDecodedFromDatabase(const ImageFetchedCallback& callback, | 245 void OnSnippetImageDecodedFromDatabase(const ImageFetchedCallback& callback, |
| 246 const std::string& suggestion_id, | 246 const std::string& snippet_id, |
| 247 const gfx::Image& image); | 247 const gfx::Image& image); |
| 248 | 248 |
| 249 void FetchSnippetImageFromNetwork(const std::string& suggestion_id, | 249 void FetchSnippetImageFromNetwork(const std::string& snippet_id, |
| 250 const ImageFetchedCallback& callback); | 250 const ImageFetchedCallback& callback); |
| 251 | 251 |
| 252 void OnSnippetImageDecodedFromNetwork(const ImageFetchedCallback& callback, | 252 void OnSnippetImageDecodedFromNetwork(const ImageFetchedCallback& callback, |
| 253 const std::string& suggestion_id, | 253 const std::string& snippet_id, |
| 254 const gfx::Image& image); | 254 const gfx::Image& image); |
| 255 | 255 |
| 256 // Triggers a state transition depending on the provided reason to be | 256 // Triggers a state transition depending on the provided reason to be |
| 257 // disabled (or lack thereof). This method is called when a change is detected | 257 // disabled (or lack thereof). This method is called when a change is detected |
| 258 // by |snippets_status_service_|. | 258 // by |snippets_status_service_|. |
| 259 void OnDisabledReasonChanged(DisabledReason disabled_reason); | 259 void OnDisabledReasonChanged(DisabledReason disabled_reason); |
| 260 | 260 |
| 261 // Verifies state transitions (see |State|'s documentation) and applies them. | 261 // Verifies state transitions (see |State|'s documentation) and applies them. |
| 262 // Also updates the provider status. Does nothing except updating the provider | 262 // Also updates the provider status. Does nothing except updating the provider |
| 263 // status if called with the current state. | 263 // status if called with the current state. |
| 264 void EnterState(State state); | 264 void EnterState(State state, CategoryStatus status); |
| 265 | 265 |
| 266 // Enables the service and triggers a fetch if required. Do not call directly, | 266 // Enables the service and triggers a fetch if required. Do not call directly, |
| 267 // use |EnterState| instead. | 267 // use |EnterState| instead. |
| 268 void EnterStateEnabled(bool fetch_snippets); | 268 void EnterStateEnabled(bool fetch_snippets); |
| 269 | 269 |
| 270 // Disables the service. Do not call directly, use |EnterState| instead. | 270 // Disables the service. Do not call directly, use |EnterState| instead. |
| 271 void EnterStateDisabled(); | 271 void EnterStateDisabled(); |
| 272 | 272 |
| 273 // Disables the service permanently because an unrecoverable error occurred. | 273 // Disables the service permanently because an unrecoverable error occurred. |
| 274 // Do not call directly, use |EnterState| instead. | 274 // Do not call directly, use |EnterState| instead. |
| 275 void EnterStateError(); | 275 void EnterStateError(); |
| 276 | 276 |
| 277 // Converts the cached snippets to article content suggestions and notifies | 277 // Converts the cached snippets to article content suggestions and notifies |
| 278 // the observers. | 278 // the observers. |
| 279 void NotifyNewSuggestions(); | 279 void NotifyNewSuggestions(); |
| 280 | 280 |
| 281 // Updates the internal status for |category| to |category_status_| and | 281 // Updates the internal status |category_status_| and notifies the content |
| 282 // notifies the content suggestions observer if it changed. | 282 // suggestions observer if it changed. |
| 283 void UpdateCategoryStatus(Category category, CategoryStatus status); | 283 void UpdateCategoryStatus(CategoryStatus status); |
| 284 // Calls UpdateCategoryStatus() for all provided categories. | |
| 285 void UpdateAllCategoryStatus(CategoryStatus status); | |
| 286 | 284 |
| 287 State state_; | 285 State state_; |
| 288 | 286 |
| 287 CategoryStatus category_status_; |
| 288 |
| 289 PrefService* pref_service_; | 289 PrefService* pref_service_; |
| 290 | 290 |
| 291 suggestions::SuggestionsService* suggestions_service_; | 291 suggestions::SuggestionsService* suggestions_service_; |
| 292 | 292 |
| 293 const Category articles_category_; | 293 // All current suggestions (i.e. not dismissed ones). |
| 294 NTPSnippet::PtrVector snippets_; |
| 294 | 295 |
| 295 struct CategoryContent { | 296 // Suggestions that the user dismissed. We keep these around until they expire |
| 296 CategoryStatus status = CategoryStatus::INITIALIZING; | 297 // so we won't re-add them on the next fetch. |
| 297 | 298 NTPSnippet::PtrVector dismissed_snippets_; |
| 298 // True iff the server returned results in this category in the last fetch. | |
| 299 // We never remove categories that the server still provides, but if the | |
| 300 // server stops providing a category, we won't yet report it as NOT_PROVIDED | |
| 301 // while we still have non-expired snippets in it. | |
| 302 bool provided_by_server = true; | |
| 303 | |
| 304 // All current suggestions (i.e. not dismissed ones). | |
| 305 NTPSnippet::PtrVector snippets; | |
| 306 | |
| 307 // Suggestions that the user dismissed. We keep these around until they | |
| 308 // expire so we won't re-add them on the next fetch. | |
| 309 NTPSnippet::PtrVector dismissed; | |
| 310 | |
| 311 CategoryContent(); // = default, in .cc | |
| 312 CategoryContent(CategoryContent&&); // = default, in .cc | |
| 313 ~CategoryContent(); // = default, in .cc | |
| 314 CategoryContent& operator=(CategoryContent&&); // = default, in .cc | |
| 315 }; | |
| 316 std::map<Category, CategoryContent, Category::CompareByID> categories_; | |
| 317 | 299 |
| 318 // The ISO 639-1 code of the language used by the application. | 300 // The ISO 639-1 code of the language used by the application. |
| 319 const std::string application_language_code_; | 301 const std::string application_language_code_; |
| 320 | 302 |
| 321 // Scheduler for fetching snippets. Not owned. | 303 // Scheduler for fetching snippets. Not owned. |
| 322 NTPSnippetsScheduler* scheduler_; | 304 NTPSnippetsScheduler* scheduler_; |
| 323 | 305 |
| 324 // Observer for the HistoryService. We clear all cached suggestions when | 306 // Observer for the HistoryService. We clear all cached suggestions when |
| 325 // history gets deleted. | 307 // history gets deleted. |
| 326 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 308 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 349 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; | 331 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; |
| 350 | 332 |
| 351 // Set to true if FetchSnippets is called before the database has been loaded. | 333 // Set to true if FetchSnippets is called before the database has been loaded. |
| 352 // The fetch will be executed after the database load finishes. | 334 // The fetch will be executed after the database load finishes. |
| 353 bool fetch_after_load_; | 335 bool fetch_after_load_; |
| 354 | 336 |
| 355 // Set to true if NukeAllSnippets is called before the database has been | 337 // Set to true if NukeAllSnippets is called before the database has been |
| 356 // loaded. The nuke will be executed after the database load finishes. | 338 // loaded. The nuke will be executed after the database load finishes. |
| 357 bool nuke_after_load_; | 339 bool nuke_after_load_; |
| 358 | 340 |
| 341 const Category provided_category_; |
| 342 |
| 359 // Request throttler for limiting requests to thumbnail images. | 343 // Request throttler for limiting requests to thumbnail images. |
| 360 RequestThrottler thumbnail_requests_throttler_; | 344 RequestThrottler thumbnail_requests_throttler_; |
| 361 | 345 |
| 362 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 346 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 363 }; | 347 }; |
| 364 | 348 |
| 365 } // namespace ntp_snippets | 349 } // namespace ntp_snippets |
| 366 | 350 |
| 367 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 351 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |