| 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/timer/timer.h" | |
| 19 #include "components/image_fetcher/image_fetcher_delegate.h" | 19 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 20 #include "components/ntp_snippets/category.h" | 20 #include "components/ntp_snippets/category.h" |
| 21 #include "components/ntp_snippets/category_factory.h" | 21 #include "components/ntp_snippets/category_factory.h" |
| 22 #include "components/ntp_snippets/category_status.h" | 22 #include "components/ntp_snippets/category_status.h" |
| 23 #include "components/ntp_snippets/content_suggestion.h" | 23 #include "components/ntp_snippets/content_suggestion.h" |
| 24 #include "components/ntp_snippets/content_suggestions_provider.h" | 24 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 25 #include "components/ntp_snippets/ntp_snippet.h" | 25 #include "components/ntp_snippets/ntp_snippet.h" |
| 26 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 26 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 27 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 27 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 28 #include "components/ntp_snippets/ntp_snippets_status_service.h" | 28 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 84 |
| 85 // Returns whether the service is ready. While this is false, the list of | 85 // Returns whether the service is ready. While this is false, the list of |
| 86 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) | 86 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) |
| 87 // will be ignored. | 87 // will be ignored. |
| 88 bool ready() const { return state_ == State::READY; } | 88 bool ready() const { return state_ == State::READY; } |
| 89 | 89 |
| 90 // Returns whether the service is initialized. While this is false, some | 90 // Returns whether the service is initialized. While this is false, some |
| 91 // calls may trigger DCHECKs. | 91 // calls may trigger DCHECKs. |
| 92 bool initialized() const { return ready() || state_ == State::DISABLED; } | 92 bool initialized() const { return ready() || state_ == State::DISABLED; } |
| 93 | 93 |
| 94 // Fetches snippets from the server and adds them to the current ones. | 94 // Fetches snippets from the server and replaces old snippets by the new ones. |
| 95 // Requests can be marked more important by setting |interactive_request| to | 95 // Requests can be marked more important by setting |interactive_request| to |
| 96 // true (such request might circumvent the daily quota for requests, etc.) | 96 // true (such request might circumvent the daily quota for requests, etc.) |
| 97 // Useful for requests triggered by the user. | 97 // Useful for requests triggered by the user. |
| 98 void FetchSnippets(bool interactive_request); | 98 void FetchSnippets(bool interactive_request); |
| 99 | 99 |
| 100 // Fetches snippets from the server for specified hosts (overriding | 100 // Fetches snippets from the server for specified hosts (overriding |
| 101 // suggestions from the suggestion service) and adds them to the current ones. | 101 // suggestions from the suggestion service) and adds them to the current ones. |
| 102 // Only called from chrome://snippets-internals, DO NOT USE otherwise! | 102 // Only called from chrome://snippets-internals, DO NOT USE otherwise! |
| 103 // Ignored while |loaded()| is false. | 103 // Ignored while |loaded()| is false. |
| 104 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, | 104 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 131 std::set<std::string> GetSuggestionsHosts() const; | 131 std::set<std::string> GetSuggestionsHosts() const; |
| 132 | 132 |
| 133 // Returns the maximum number of snippets that will be shown at once. | 133 // Returns the maximum number of snippets that will be shown at once. |
| 134 static int GetMaxSnippetCountForTesting(); | 134 static int GetMaxSnippetCountForTesting(); |
| 135 | 135 |
| 136 // Available snippets, only for unit tests. | 136 // Available snippets, only for unit tests. |
| 137 const NTPSnippet::PtrVector& GetSnippetsForTesting(Category category) const { | 137 const NTPSnippet::PtrVector& GetSnippetsForTesting(Category category) const { |
| 138 return categories_.find(category)->second.snippets; | 138 return categories_.find(category)->second.snippets; |
| 139 } | 139 } |
| 140 | 140 |
| 141 // Available snippets, only for unit tests. |
| 142 const NTPSnippet::PtrVector& GetArchivedSnippetsForTesting( |
| 143 Category category) const { |
| 144 return categories_.find(category)->second.archived; |
| 145 } |
| 146 |
| 141 // Dismissed snippets, only for unit tests. | 147 // Dismissed snippets, only for unit tests. |
| 142 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting( | 148 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting( |
| 143 Category category) const { | 149 Category category) const { |
| 144 return categories_.find(category)->second.dismissed; | 150 return categories_.find(category)->second.dismissed; |
| 145 } | 151 } |
| 146 | 152 |
| 147 private: | 153 private: |
| 148 friend class NTPSnippetsServiceTest; | 154 friend class NTPSnippetsServiceTest; |
| 155 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, |
| 156 RemoveExpiredDismissedContent); |
| 149 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, RescheduleOnStateChange); | 157 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, RescheduleOnStateChange); |
| 150 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); | 158 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); |
| 151 | 159 |
| 152 // Possible state transitions: | 160 // Possible state transitions: |
| 153 // NOT_INITED --------+ | 161 // NOT_INITED --------+ |
| 154 // / \ | | 162 // / \ | |
| 155 // v v | | 163 // v v | |
| 156 // READY <--> DISABLED | | 164 // READY <--> DISABLED | |
| 157 // \ / | | 165 // \ / | |
| 158 // v v | | 166 // v v | |
| (...skipping 20 matching lines...) Expand all Loading... |
| 179 // - READY: when the global Chrome state changes, for example after | 187 // - READY: when the global Chrome state changes, for example after |
| 180 // |OnStateChanged| is called and sync is enabled. | 188 // |OnStateChanged| is called and sync is enabled. |
| 181 // - ERROR_OCCURRED: when an unrecoverable error occurred. | 189 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
| 182 DISABLED, | 190 DISABLED, |
| 183 | 191 |
| 184 // The service or one of its dependencies encountered an unrecoverable error | 192 // The service or one of its dependencies encountered an unrecoverable error |
| 185 // and the service can't be used anymore. | 193 // and the service can't be used anymore. |
| 186 ERROR_OCCURRED | 194 ERROR_OCCURRED |
| 187 }; | 195 }; |
| 188 | 196 |
| 197 // Returns the URL of the image of a snippet if it is among the current or |
| 198 // among the archived snippets in |category|. Returns an empty URL, otherwise. |
| 199 GURL FindSnippetImageUrl(Category category, |
| 200 const std::string& snippet_id) const; |
| 201 |
| 189 // image_fetcher::ImageFetcherDelegate implementation. | 202 // image_fetcher::ImageFetcherDelegate implementation. |
| 190 void OnImageDataFetched(const std::string& snippet_id, | 203 void OnImageDataFetched(const std::string& snippet_id, |
| 191 const std::string& image_data) override; | 204 const std::string& image_data) override; |
| 192 | 205 |
| 193 // Callbacks for the NTPSnippetsDatabase. | 206 // Callbacks for the NTPSnippetsDatabase. |
| 194 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 207 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 195 void OnDatabaseError(); | 208 void OnDatabaseError(); |
| 196 | 209 |
| 197 // Callback for the SuggestionsService. | 210 // Callback for the SuggestionsService. |
| 198 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 211 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 199 | 212 |
| 200 // Callback for the NTPSnippetsFetcher. | 213 // Callback for the NTPSnippetsFetcher. |
| 201 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 214 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 202 | 215 |
| 203 // Merges newly available snippets with the previously available list. | 216 // Moves all snippets from |to_archive| into the archive of the |category|. |
| 204 void MergeSnippets(Category category, NTPSnippet::PtrVector new_snippets); | 217 // It also deletes the snippets from the DB and keeps the archive reasonably |
| 218 // short. |
| 219 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); |
| 220 |
| 221 // Replace old snippets in |category| by newly available snippets. |
| 222 void ReplaceSnippets(Category category, NTPSnippet::PtrVector new_snippets); |
| 205 | 223 |
| 206 std::set<std::string> GetSnippetHostsFromPrefs() const; | 224 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 207 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 225 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 208 | 226 |
| 209 // Removes the expired snippets (including dismissed) from the service and the | 227 // Removes expired dismissed snippets from the service and the database. |
| 210 // database, and schedules another pass for the next expiration. | 228 void ClearExpiredDismissedSnippets(); |
| 211 void ClearExpiredSnippets(); | 229 |
| 230 // Removes images from the DB that do not have any corresponding snippet |
| 231 // (neither in the current set, nor in the archived set). |
| 232 void ClearOrphanedImages(); |
| 212 | 233 |
| 213 // Clears all stored snippets and updates the observer. | 234 // Clears all stored snippets and updates the observer. |
| 214 void NukeAllSnippets(); | 235 void NukeAllSnippets(); |
| 215 | 236 |
| 216 // Completes the initialization phase of the service, registering the last | 237 // Completes the initialization phase of the service, registering the last |
| 217 // observers. This is done after construction, once the database is loaded. | 238 // observers. This is done after construction, once the database is loaded. |
| 218 void FinishInitialization(); | 239 void FinishInitialization(); |
| 219 | 240 |
| 220 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, | 241 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, |
| 221 const std::string& suggestion_id, | 242 const std::string& suggestion_id, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 297 |
| 277 // The title of the section, localized to the running UI language. | 298 // The title of the section, localized to the running UI language. |
| 278 base::string16 localized_title; | 299 base::string16 localized_title; |
| 279 | 300 |
| 280 // True iff the server returned results in this category in the last fetch. | 301 // True iff the server returned results in this category in the last fetch. |
| 281 // We never remove categories that the server still provides, but if the | 302 // We never remove categories that the server still provides, but if the |
| 282 // server stops providing a category, we won't yet report it as NOT_PROVIDED | 303 // server stops providing a category, we won't yet report it as NOT_PROVIDED |
| 283 // while we still have non-expired snippets in it. | 304 // while we still have non-expired snippets in it. |
| 284 bool provided_by_server = true; | 305 bool provided_by_server = true; |
| 285 | 306 |
| 286 // All current suggestions (i.e. not dismissed ones). | 307 // All currently active suggestions (excl. the dismissed ones). |
| 287 NTPSnippet::PtrVector snippets; | 308 NTPSnippet::PtrVector snippets; |
| 288 | 309 |
| 310 // All previous suggestions that we keep around in memory because they can |
| 311 // be on some open NTP. We do not persist this list so that on a new start |
| 312 // of Chrome, this is empty. |
| 313 NTPSnippet::PtrVector archived; |
| 314 |
| 289 // Suggestions that the user dismissed. We keep these around until they | 315 // Suggestions that the user dismissed. We keep these around until they |
| 290 // expire so we won't re-add them on the next fetch. | 316 // expire so we won't re-add them to |snippets| on the next fetch. |
| 291 NTPSnippet::PtrVector dismissed; | 317 NTPSnippet::PtrVector dismissed; |
| 292 | 318 |
| 293 CategoryContent(); | 319 CategoryContent(); |
| 294 CategoryContent(CategoryContent&&); | 320 CategoryContent(CategoryContent&&); |
| 295 ~CategoryContent(); | 321 ~CategoryContent(); |
| 296 CategoryContent& operator=(CategoryContent&&); | 322 CategoryContent& operator=(CategoryContent&&); |
| 297 }; | 323 }; |
| 298 std::map<Category, CategoryContent, Category::CompareByID> categories_; | 324 std::map<Category, CategoryContent, Category::CompareByID> categories_; |
| 299 | 325 |
| 300 // The ISO 639-1 code of the language used by the application. | 326 // The ISO 639-1 code of the language used by the application. |
| 301 const std::string application_language_code_; | 327 const std::string application_language_code_; |
| 302 | 328 |
| 303 // Scheduler for fetching snippets. Not owned. | 329 // Scheduler for fetching snippets. Not owned. |
| 304 NTPSnippetsScheduler* scheduler_; | 330 NTPSnippetsScheduler* scheduler_; |
| 305 | 331 |
| 306 // The subscription to the SuggestionsService. When the suggestions change, | 332 // The subscription to the SuggestionsService. When the suggestions change, |
| 307 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 333 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
| 308 // update to the set of snippets. | 334 // update to the set of snippets. |
| 309 using SuggestionsSubscription = | 335 using SuggestionsSubscription = |
| 310 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 336 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 311 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 337 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 312 | 338 |
| 313 // The snippets fetcher. | 339 // The snippets fetcher. |
| 314 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 340 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 315 | 341 |
| 316 // Timer that calls us back when the next snippet expires. | |
| 317 base::OneShotTimer expiry_timer_; | |
| 318 | |
| 319 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 342 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
| 320 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; | 343 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; |
| 321 | 344 |
| 322 // The database for persisting snippets. | 345 // The database for persisting snippets. |
| 323 std::unique_ptr<NTPSnippetsDatabase> database_; | 346 std::unique_ptr<NTPSnippetsDatabase> database_; |
| 324 | 347 |
| 325 // The service that provides events and data about the signin and sync state. | 348 // The service that provides events and data about the signin and sync state. |
| 326 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; | 349 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; |
| 327 | 350 |
| 328 // Set to true if FetchSnippets is called while the service isn't ready. | 351 // Set to true if FetchSnippets is called while the service isn't ready. |
| 329 // The fetch will be executed once the service enters the READY state. | 352 // The fetch will be executed once the service enters the READY state. |
| 330 bool fetch_when_ready_; | 353 bool fetch_when_ready_; |
| 331 | 354 |
| 332 // Set to true if NukeAllSnippets is called while the service isn't ready. | 355 // Set to true if NukeAllSnippets is called while the service isn't ready. |
| 333 // The nuke will be executed once the service finishes initialization or | 356 // The nuke will be executed once the service finishes initialization or |
| 334 // enters the READY state. | 357 // enters the READY state. |
| 335 bool nuke_when_initialized_; | 358 bool nuke_when_initialized_; |
| 336 | 359 |
| 337 // Request throttler for limiting requests to thumbnail images. | 360 // Request throttler for limiting requests to thumbnail images. |
| 338 RequestThrottler thumbnail_requests_throttler_; | 361 RequestThrottler thumbnail_requests_throttler_; |
| 339 | 362 |
| 340 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 363 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 341 }; | 364 }; |
| 342 | 365 |
| 343 } // namespace ntp_snippets | 366 } // namespace ntp_snippets |
| 344 | 367 |
| 345 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 368 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |