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