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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 // - READY: when the global Chrome state changes, for example after | 179 // - READY: when the global Chrome state changes, for example after |
180 // |OnStateChanged| is called and sync is enabled. | 180 // |OnStateChanged| is called and sync is enabled. |
181 // - ERROR_OCCURRED: when an unrecoverable error occurred. | 181 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
182 DISABLED, | 182 DISABLED, |
183 | 183 |
184 // The service or one of its dependencies encountered an unrecoverable error | 184 // The service or one of its dependencies encountered an unrecoverable error |
185 // and the service can't be used anymore. | 185 // and the service can't be used anymore. |
186 ERROR_OCCURRED | 186 ERROR_OCCURRED |
187 }; | 187 }; |
188 | 188 |
| 189 // Returns the URL of the image of a snippet if it is among the current or |
| 190 // among the archived snippets in |category|. Returns an empty URL, otherwise. |
| 191 GURL FindSnippetImageUrl(Category category, |
| 192 const std::string& snippet_id) const; |
| 193 |
189 // image_fetcher::ImageFetcherDelegate implementation. | 194 // image_fetcher::ImageFetcherDelegate implementation. |
190 void OnImageDataFetched(const std::string& snippet_id, | 195 void OnImageDataFetched(const std::string& snippet_id, |
191 const std::string& image_data) override; | 196 const std::string& image_data) override; |
192 | 197 |
193 // Callbacks for the NTPSnippetsDatabase. | 198 // Callbacks for the NTPSnippetsDatabase. |
194 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 199 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
195 void OnDatabaseError(); | 200 void OnDatabaseError(); |
196 | 201 |
197 // Callback for the SuggestionsService. | 202 // Callback for the SuggestionsService. |
198 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 203 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
199 | 204 |
200 // Callback for the NTPSnippetsFetcher. | 205 // Callback for the NTPSnippetsFetcher. |
201 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 206 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
202 | 207 |
203 // Merges newly available snippets with the previously available list. | 208 // Moves all snippets from |to_archive| into the archive of the |category|. |
204 void MergeSnippets(Category category, NTPSnippet::PtrVector new_snippets); | 209 // It also deletes the snippets from the DB and keeps the archive reasonably |
| 210 // short. |
| 211 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); |
| 212 |
| 213 // Replace old snippets in |category| by newly available snippets. |
| 214 void ReplaceSnippets(Category category, NTPSnippet::PtrVector new_snippets); |
205 | 215 |
206 std::set<std::string> GetSnippetHostsFromPrefs() const; | 216 std::set<std::string> GetSnippetHostsFromPrefs() const; |
207 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 217 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
208 | 218 |
209 // Removes the expired snippets (including dismissed) from the service and the | 219 // Removes expired dismissed snippets from the service and the database. |
210 // database, and schedules another pass for the next expiration. | 220 void ClearExpiredDismissedSnippets(); |
211 void ClearExpiredSnippets(); | 221 |
| 222 // Removes images from the DB that do not have any corresponding snippet |
| 223 // (neither in the current set, nor in the archived set). |
| 224 void ClearOrphanedImages(); |
212 | 225 |
213 // Clears all stored snippets and updates the observer. | 226 // Clears all stored snippets and updates the observer. |
214 void NukeAllSnippets(); | 227 void NukeAllSnippets(); |
215 | 228 |
216 // Completes the initialization phase of the service, registering the last | 229 // Completes the initialization phase of the service, registering the last |
217 // observers. This is done after construction, once the database is loaded. | 230 // observers. This is done after construction, once the database is loaded. |
218 void FinishInitialization(); | 231 void FinishInitialization(); |
219 | 232 |
220 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, | 233 void OnSnippetImageFetchedFromDatabase(const ImageFetchedCallback& callback, |
221 const std::string& suggestion_id, | 234 const std::string& suggestion_id, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 | 289 |
277 // The title of the section, localized to the running UI language. | 290 // The title of the section, localized to the running UI language. |
278 base::string16 localized_title; | 291 base::string16 localized_title; |
279 | 292 |
280 // True iff the server returned results in this category in the last fetch. | 293 // 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 | 294 // 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 | 295 // server stops providing a category, we won't yet report it as NOT_PROVIDED |
283 // while we still have non-expired snippets in it. | 296 // while we still have non-expired snippets in it. |
284 bool provided_by_server = true; | 297 bool provided_by_server = true; |
285 | 298 |
286 // All current suggestions (i.e. not dismissed ones). | 299 // All currently active suggestions (excl. the dismissed ones). |
287 NTPSnippet::PtrVector snippets; | 300 NTPSnippet::PtrVector snippets; |
288 | 301 |
| 302 // All previous suggestions that we keep around in memory because they can |
| 303 // be on some open NTP. We do not persist this list so that on a new start |
| 304 // of Chrome, this is empty. |
| 305 NTPSnippet::PtrVector archived; |
| 306 |
289 // Suggestions that the user dismissed. We keep these around until they | 307 // Suggestions that the user dismissed. We keep these around until they |
290 // expire so we won't re-add them on the next fetch. | 308 // expire so we won't re-add them to |snippets| on the next fetch. |
291 NTPSnippet::PtrVector dismissed; | 309 NTPSnippet::PtrVector dismissed; |
292 | 310 |
293 CategoryContent(); | 311 CategoryContent(); |
294 CategoryContent(CategoryContent&&); | 312 CategoryContent(CategoryContent&&); |
295 ~CategoryContent(); | 313 ~CategoryContent(); |
296 CategoryContent& operator=(CategoryContent&&); | 314 CategoryContent& operator=(CategoryContent&&); |
297 }; | 315 }; |
298 std::map<Category, CategoryContent, Category::CompareByID> categories_; | 316 std::map<Category, CategoryContent, Category::CompareByID> categories_; |
299 | 317 |
300 // The ISO 639-1 code of the language used by the application. | 318 // The ISO 639-1 code of the language used by the application. |
301 const std::string application_language_code_; | 319 const std::string application_language_code_; |
302 | 320 |
303 // Scheduler for fetching snippets. Not owned. | 321 // Scheduler for fetching snippets. Not owned. |
304 NTPSnippetsScheduler* scheduler_; | 322 NTPSnippetsScheduler* scheduler_; |
305 | 323 |
306 // The subscription to the SuggestionsService. When the suggestions change, | 324 // The subscription to the SuggestionsService. When the suggestions change, |
307 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 325 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
308 // update to the set of snippets. | 326 // update to the set of snippets. |
309 using SuggestionsSubscription = | 327 using SuggestionsSubscription = |
310 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 328 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
311 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 329 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
312 | 330 |
313 // The snippets fetcher. | 331 // The snippets fetcher. |
314 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 332 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
315 | 333 |
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_; | 334 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
320 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; | 335 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; |
321 | 336 |
322 // The database for persisting snippets. | 337 // The database for persisting snippets. |
323 std::unique_ptr<NTPSnippetsDatabase> database_; | 338 std::unique_ptr<NTPSnippetsDatabase> database_; |
324 | 339 |
325 // The service that provides events and data about the signin and sync state. | 340 // The service that provides events and data about the signin and sync state. |
326 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; | 341 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; |
327 | 342 |
328 // Set to true if FetchSnippets is called while the service isn't ready. | 343 // 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. | 344 // The fetch will be executed once the service enters the READY state. |
330 bool fetch_when_ready_; | 345 bool fetch_when_ready_; |
331 | 346 |
332 // Set to true if NukeAllSnippets is called while the service isn't ready. | 347 // 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 | 348 // The nuke will be executed once the service finishes initialization or |
334 // enters the READY state. | 349 // enters the READY state. |
335 bool nuke_when_initialized_; | 350 bool nuke_when_initialized_; |
336 | 351 |
337 // Request throttler for limiting requests to thumbnail images. | 352 // Request throttler for limiting requests to thumbnail images. |
338 RequestThrottler thumbnail_requests_throttler_; | 353 RequestThrottler thumbnail_requests_throttler_; |
339 | 354 |
340 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 355 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
341 }; | 356 }; |
342 | 357 |
343 } // namespace ntp_snippets | 358 } // namespace ntp_snippets |
344 | 359 |
345 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 360 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
OLD | NEW |