| 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 <map> |
| 10 #include <memory> | 11 #include <memory> |
| 11 #include <set> | 12 #include <set> |
| 12 #include <string> | 13 #include <string> |
| 13 #include <vector> | 14 #include <vector> |
| 14 | 15 |
| 15 #include "base/callback_forward.h" | 16 #include "base/callback_forward.h" |
| 16 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 17 #include "base/macros.h" | 18 #include "base/macros.h" |
| 18 #include "base/scoped_observer.h" | 19 #include "base/scoped_observer.h" |
| 19 #include "base/timer/timer.h" | |
| 20 #include "components/image_fetcher/image_fetcher_delegate.h" | 20 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 21 #include "components/ntp_snippets/category.h" | 21 #include "components/ntp_snippets/category.h" |
| 22 #include "components/ntp_snippets/category_factory.h" | 22 #include "components/ntp_snippets/category_factory.h" |
| 23 #include "components/ntp_snippets/category_status.h" | 23 #include "components/ntp_snippets/category_status.h" |
| 24 #include "components/ntp_snippets/content_suggestion.h" | 24 #include "components/ntp_snippets/content_suggestion.h" |
| 25 #include "components/ntp_snippets/content_suggestions_provider.h" | 25 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 26 #include "components/ntp_snippets/ntp_snippet.h" | 26 #include "components/ntp_snippets/ntp_snippet.h" |
| 27 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 27 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 28 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 28 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 29 #include "components/ntp_snippets/ntp_snippets_status_service.h" | 29 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 // Returns whether the service is ready. While this is false, the list of | 98 // Returns whether the service is ready. While this is false, the list of |
| 99 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) | 99 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) |
| 100 // will be ignored. | 100 // will be ignored. |
| 101 bool ready() const { return state_ == State::READY; } | 101 bool ready() const { return state_ == State::READY; } |
| 102 | 102 |
| 103 // Returns whether the service is initialized. While this is false, some | 103 // Returns whether the service is initialized. While this is false, some |
| 104 // calls may trigger DCHECKs. | 104 // calls may trigger DCHECKs. |
| 105 bool initialized() const { return ready() || state_ == State::DISABLED; } | 105 bool initialized() const { return ready() || state_ == State::DISABLED; } |
| 106 | 106 |
| 107 // Fetches snippets from the server and adds them to the current ones. | 107 // Fetches snippets from the server and replaces old snippets by the new ones. |
| 108 // Requests can be marked more important by setting |interactive_request| to | 108 // Requests can be marked more important by setting |interactive_request| to |
| 109 // true (such request might circumvent the daily quota for requests, etc.) | 109 // true (such request might circumvent the daily quota for requests, etc.) |
| 110 // Useful for requests triggered by the user. | 110 // Useful for requests triggered by the user. |
| 111 void FetchSnippets(bool interactive_request); | 111 void FetchSnippets(bool interactive_request); |
| 112 | 112 |
| 113 // Fetches snippets from the server for specified hosts (overriding | 113 // Fetches snippets from the server for specified hosts (overriding |
| 114 // suggestions from the suggestion service) and adds them to the current ones. | 114 // suggestions from the suggestion service) and adds them to the current ones. |
| 115 // Only called from chrome://snippets-internals, DO NOT USE otherwise! | 115 // Only called from chrome://snippets-internals, DO NOT USE otherwise! |
| 116 // Ignored while |loaded()| is false. | 116 // Ignored while |loaded()| is false. |
| 117 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, | 117 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // - READY: when the global Chrome state changes, for example after | 192 // - READY: when the global Chrome state changes, for example after |
| 193 // |OnStateChanged| is called and sync is enabled. | 193 // |OnStateChanged| is called and sync is enabled. |
| 194 // - ERROR_OCCURRED: when an unrecoverable error occurred. | 194 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
| 195 DISABLED, | 195 DISABLED, |
| 196 | 196 |
| 197 // The service or one of its dependencies encountered an unrecoverable error | 197 // The service or one of its dependencies encountered an unrecoverable error |
| 198 // and the service can't be used anymore. | 198 // and the service can't be used anymore. |
| 199 ERROR_OCCURRED | 199 ERROR_OCCURRED |
| 200 }; | 200 }; |
| 201 | 201 |
| 202 // Returns the URL of the image of a snippet if it is among the current or |
| 203 // among the archived snippets in |category|. Returns an empty URL, otherwise. |
| 204 GURL FindSnippetImageUrl(Category category, |
| 205 const std::string& snippet_id) const; |
| 206 |
| 202 // image_fetcher::ImageFetcherDelegate implementation. | 207 // image_fetcher::ImageFetcherDelegate implementation. |
| 203 void OnImageDataFetched(const std::string& snippet_id, | 208 void OnImageDataFetched(const std::string& snippet_id, |
| 204 const std::string& image_data) override; | 209 const std::string& image_data) override; |
| 205 | 210 |
| 206 // Callbacks for the NTPSnippetsDatabase. | 211 // Callbacks for the NTPSnippetsDatabase. |
| 207 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 212 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 208 void OnDatabaseError(); | 213 void OnDatabaseError(); |
| 209 | 214 |
| 210 // Callback for the SuggestionsService. | 215 // Callback for the SuggestionsService. |
| 211 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 216 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 212 | 217 |
| 213 // Callback for the NTPSnippetsFetcher. | 218 // Callback for the NTPSnippetsFetcher. |
| 214 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 219 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 215 | 220 |
| 216 // Merges newly available snippets with the previously available list. | 221 // Moves all snippets from |to_archive| into the archive of the |category|. |
| 217 void MergeSnippets(Category category, NTPSnippet::PtrVector new_snippets); | 222 // It also deletes the snippets from the DB and keeps the archive reasonably |
| 223 // short. |
| 224 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); |
| 225 |
| 226 // Replace old snippets in |category| by newly available snippets. |
| 227 void ReplaceSnippets(Category category, NTPSnippet::PtrVector new_snippets); |
| 218 | 228 |
| 219 std::set<std::string> GetSnippetHostsFromPrefs() const; | 229 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 220 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 230 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 221 | 231 |
| 222 // Removes the expired snippets (including dismissed) from the service and the | 232 // Removes expired dismissed snippets from the service and the database. |
| 223 // database, and schedules another pass for the next expiration. | 233 void ClearExpiredDismissedSnippets(); |
| 224 void ClearExpiredSnippets(); | 234 |
| 235 // Removes images from the DB that do not have any corresponding snippet |
| 236 // (neither in the current set, nor in the archived set). |
| 237 void ClearOrphanedImages(); |
| 225 | 238 |
| 226 // Clears all stored snippets and updates the observer. | 239 // Clears all stored snippets and updates the observer. |
| 227 void NukeAllSnippets(); | 240 void NukeAllSnippets(); |
| 228 | 241 |
| 229 // Completes the initialization phase of the service, registering the last | 242 // Completes the initialization phase of the service, registering the last |
| 230 // observers. This is done after construction, once the database is loaded. | 243 // observers. This is done after construction, once the database is loaded. |
| 231 void FinishInitialization(); | 244 void FinishInitialization(); |
| 232 | 245 |
| 233 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, | 246 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, |
| 234 const std::string& suggestion_id, | 247 const std::string& suggestion_id, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 | 303 |
| 291 // The title of the section, localized to the running UI language. | 304 // The title of the section, localized to the running UI language. |
| 292 base::string16 localized_title; | 305 base::string16 localized_title; |
| 293 | 306 |
| 294 // True iff the server returned results in this category in the last fetch. | 307 // True iff the server returned results in this category in the last fetch. |
| 295 // We never remove categories that the server still provides, but if the | 308 // We never remove categories that the server still provides, but if the |
| 296 // server stops providing a category, we won't yet report it as NOT_PROVIDED | 309 // server stops providing a category, we won't yet report it as NOT_PROVIDED |
| 297 // while we still have non-expired snippets in it. | 310 // while we still have non-expired snippets in it. |
| 298 bool provided_by_server = true; | 311 bool provided_by_server = true; |
| 299 | 312 |
| 300 // All current suggestions (i.e. not dismissed ones). | 313 // All currently active suggestions (excl. the dismissed ones). |
| 301 NTPSnippet::PtrVector snippets; | 314 NTPSnippet::PtrVector snippets; |
| 302 | 315 |
| 316 // All previous suggestions that we keep around in memory because they can |
| 317 // be on some open NTP. We do not persist this list so that on a new start |
| 318 // of Chrome, this is empty. |
| 319 NTPSnippet::PtrVector archived; |
| 320 |
| 303 // Suggestions that the user dismissed. We keep these around until they | 321 // Suggestions that the user dismissed. We keep these around until they |
| 304 // expire so we won't re-add them on the next fetch. | 322 // expire so we won't re-add them to |snippets| on the next fetch. |
| 305 NTPSnippet::PtrVector dismissed; | 323 NTPSnippet::PtrVector dismissed; |
| 306 | 324 |
| 307 CategoryContent(); | 325 CategoryContent(); |
| 308 CategoryContent(CategoryContent&&); | 326 CategoryContent(CategoryContent&&); |
| 309 ~CategoryContent(); | 327 ~CategoryContent(); |
| 310 CategoryContent& operator=(CategoryContent&&); | 328 CategoryContent& operator=(CategoryContent&&); |
| 311 }; | 329 }; |
| 312 std::map<Category, CategoryContent, Category::CompareByID> categories_; | 330 std::map<Category, CategoryContent, Category::CompareByID> categories_; |
| 313 | 331 |
| 314 // The ISO 639-1 code of the language used by the application. | 332 // The ISO 639-1 code of the language used by the application. |
| 315 const std::string application_language_code_; | 333 const std::string application_language_code_; |
| 316 | 334 |
| 317 // Scheduler for fetching snippets. Not owned. | 335 // Scheduler for fetching snippets. Not owned. |
| 318 NTPSnippetsScheduler* scheduler_; | 336 NTPSnippetsScheduler* scheduler_; |
| 319 | 337 |
| 320 // The subscription to the SuggestionsService. When the suggestions change, | 338 // The subscription to the SuggestionsService. When the suggestions change, |
| 321 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 339 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
| 322 // update to the set of snippets. | 340 // update to the set of snippets. |
| 323 using SuggestionsSubscription = | 341 using SuggestionsSubscription = |
| 324 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 342 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 325 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 343 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 326 | 344 |
| 327 // The snippets fetcher. | 345 // The snippets fetcher. |
| 328 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 346 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 329 | 347 |
| 330 // Timer that calls us back when the next snippet expires. | |
| 331 base::OneShotTimer expiry_timer_; | |
| 332 | |
| 333 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 348 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
| 334 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; | 349 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; |
| 335 | 350 |
| 336 // The database for persisting snippets. | 351 // The database for persisting snippets. |
| 337 std::unique_ptr<NTPSnippetsDatabase> database_; | 352 std::unique_ptr<NTPSnippetsDatabase> database_; |
| 338 | 353 |
| 339 // The service that provides events and data about the signin and sync state. | 354 // The service that provides events and data about the signin and sync state. |
| 340 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; | 355 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; |
| 341 | 356 |
| 342 // Set to true if FetchSnippets is called before the database has been loaded. | 357 // Set to true if FetchSnippets is called before the database has been loaded. |
| 343 // The fetch will be executed after the database load finishes. | 358 // The fetch will be executed after the database load finishes. |
| 344 bool fetch_after_load_; | 359 bool fetch_after_load_; |
| 345 | 360 |
| 346 // Set to true if NukeAllSnippets is called before the database has been | 361 // Set to true if NukeAllSnippets is called before the database has been |
| 347 // loaded. The nuke will be executed after the database load finishes. | 362 // loaded. The nuke will be executed after the database load finishes. |
| 348 bool nuke_after_load_; | 363 bool nuke_after_load_; |
| 349 | 364 |
| 350 // Request throttler for limiting requests to thumbnail images. | 365 // Request throttler for limiting requests to thumbnail images. |
| 351 RequestThrottler thumbnail_requests_throttler_; | 366 RequestThrottler thumbnail_requests_throttler_; |
| 352 | 367 |
| 353 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 368 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 354 }; | 369 }; |
| 355 | 370 |
| 356 } // namespace ntp_snippets | 371 } // namespace ntp_snippets |
| 357 | 372 |
| 358 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 373 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |