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 #include "components/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 225 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
226 base::Unretained(this))); | 226 base::Unretained(this))); |
227 } | 227 } |
228 | 228 |
229 NTPSnippetsService::~NTPSnippetsService() { | 229 NTPSnippetsService::~NTPSnippetsService() { |
230 } | 230 } |
231 | 231 |
232 // static | 232 // static |
233 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 233 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
234 registry->RegisterListPref(prefs::kSnippetHosts); | 234 registry->RegisterListPref(prefs::kSnippetHosts); |
| 235 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0); |
| 236 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback, |
| 237 0); |
235 | 238 |
236 NTPSnippetsStatusService::RegisterProfilePrefs(registry); | 239 NTPSnippetsStatusService::RegisterProfilePrefs(registry); |
237 } | 240 } |
238 | 241 |
239 void NTPSnippetsService::FetchSnippets(bool interactive_request) { | 242 void NTPSnippetsService::FetchSnippets(bool interactive_request) { |
240 if (ready()) | 243 if (ready()) |
241 FetchSnippetsFromHosts(GetSuggestionsHosts(), interactive_request); | 244 FetchSnippetsFromHosts(GetSuggestionsHosts(), interactive_request); |
242 else | 245 else |
243 fetch_when_ready_ = true; | 246 fetch_when_ready_ = true; |
244 } | 247 } |
(...skipping 16 matching lines...) Expand all Loading... |
261 for (const auto& item : categories_) { | 264 for (const auto& item : categories_) { |
262 const CategoryContent& content = item.second; | 265 const CategoryContent& content = item.second; |
263 for (const auto& snippet : content.dismissed) | 266 for (const auto& snippet : content.dismissed) |
264 excluded_ids.insert(snippet->id()); | 267 excluded_ids.insert(snippet->id()); |
265 } | 268 } |
266 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, | 269 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, |
267 excluded_ids, kMaxSnippetCount, | 270 excluded_ids, kMaxSnippetCount, |
268 interactive_request); | 271 interactive_request); |
269 } | 272 } |
270 | 273 |
271 void NTPSnippetsService::RescheduleFetching() { | 274 void NTPSnippetsService::RescheduleFetching(bool force) { |
272 // The scheduler only exists on Android so far, it's null on other platforms. | 275 // The scheduler only exists on Android so far, it's null on other platforms. |
273 if (!scheduler_) | 276 if (!scheduler_) |
274 return; | 277 return; |
275 | 278 |
276 // If we're NOT_INITED, we don't know whether to schedule or un-schedule. | |
277 // We'll reschedule on the next state change anyway, so do nothing here. | |
278 if (state_ == State::NOT_INITED) | |
279 return; | |
280 | |
281 if (ready()) { | 279 if (ready()) { |
282 scheduler_->Schedule(GetFetchingIntervalWifi(), | 280 base::TimeDelta old_interval_wifi = |
283 GetFetchingIntervalFallback()); | 281 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( |
| 282 prefs::kSnippetBackgroundFetchingIntervalWifi)); |
| 283 base::TimeDelta old_interval_fallback = |
| 284 base::TimeDelta::FromInternalValue(pref_service_->GetInt64( |
| 285 prefs::kSnippetBackgroundFetchingIntervalFallback)); |
| 286 base::TimeDelta interval_wifi = GetFetchingIntervalWifi(); |
| 287 base::TimeDelta interval_fallback = GetFetchingIntervalFallback(); |
| 288 if (force || interval_wifi != old_interval_wifi || |
| 289 interval_fallback != old_interval_fallback) { |
| 290 scheduler_->Schedule(interval_wifi, interval_fallback); |
| 291 pref_service_->SetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi, |
| 292 interval_wifi.ToInternalValue()); |
| 293 pref_service_->SetInt64( |
| 294 prefs::kSnippetBackgroundFetchingIntervalFallback, |
| 295 interval_fallback.ToInternalValue()); |
| 296 } |
284 } else { | 297 } else { |
285 scheduler_->Unschedule(); | 298 // If we're NOT_INITED, we don't know whether to schedule or un-schedule. |
| 299 // If |force| is false, all is well: We'll reschedule on the next state |
| 300 // change anyway. If it's true, then unschedule here, to make sure that the |
| 301 // next reschedule actually happens. |
| 302 if (state_ != State::NOT_INITED || force) { |
| 303 scheduler_->Unschedule(); |
| 304 pref_service_->ClearPref(prefs::kSnippetBackgroundFetchingIntervalWifi); |
| 305 pref_service_->ClearPref( |
| 306 prefs::kSnippetBackgroundFetchingIntervalFallback); |
| 307 } |
286 } | 308 } |
287 } | 309 } |
288 | 310 |
289 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { | 311 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { |
290 DCHECK(categories_.find(category) != categories_.end()); | 312 DCHECK(categories_.find(category) != categories_.end()); |
291 return categories_[category].status; | 313 return categories_[category].status; |
292 } | 314 } |
293 | 315 |
294 CategoryInfo NTPSnippetsService::GetCategoryInfo(Category category) { | 316 CategoryInfo NTPSnippetsService::GetCategoryInfo(Category category) { |
295 DCHECK(categories_.find(category) != categories_.end()); | 317 DCHECK(categories_.find(category) != categories_.end()); |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 } | 625 } |
604 | 626 |
605 // TODO(sfiera): notify only when a category changed above. | 627 // TODO(sfiera): notify only when a category changed above. |
606 NotifyNewSuggestions(); | 628 NotifyNewSuggestions(); |
607 | 629 |
608 // Reschedule after a successful fetch. This resets all currently scheduled | 630 // Reschedule after a successful fetch. This resets all currently scheduled |
609 // fetches, to make sure the fallback interval triggers only if no wifi fetch | 631 // fetches, to make sure the fallback interval triggers only if no wifi fetch |
610 // succeeded, and also that we don't do a background fetch immediately after | 632 // succeeded, and also that we don't do a background fetch immediately after |
611 // a user-initiated one. | 633 // a user-initiated one. |
612 if (snippets) | 634 if (snippets) |
613 RescheduleFetching(); | 635 RescheduleFetching(true); |
614 } | 636 } |
615 | 637 |
616 void NTPSnippetsService::ArchiveSnippets(Category category, | 638 void NTPSnippetsService::ArchiveSnippets(Category category, |
617 NTPSnippet::PtrVector* to_archive) { | 639 NTPSnippet::PtrVector* to_archive) { |
618 CategoryContent* content = &categories_[category]; | 640 CategoryContent* content = &categories_[category]; |
619 | 641 |
620 // TODO(sfiera): handle DB for non-articles too. | 642 // TODO(sfiera): handle DB for non-articles too. |
621 if (category == articles_category_) { | 643 if (category == articles_category_) { |
622 database_->DeleteSnippets(*to_archive); | 644 database_->DeleteSnippets(*to_archive); |
623 // Do not delete the thumbnail images as they are still handy on open NTPs. | 645 // Do not delete the thumbnail images as they are still handy on open NTPs. |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
983 break; | 1005 break; |
984 | 1006 |
985 case State::ERROR_OCCURRED: | 1007 case State::ERROR_OCCURRED: |
986 DVLOG(1) << "Entering state: ERROR_OCCURRED"; | 1008 DVLOG(1) << "Entering state: ERROR_OCCURRED"; |
987 state_ = State::ERROR_OCCURRED; | 1009 state_ = State::ERROR_OCCURRED; |
988 EnterStateError(); | 1010 EnterStateError(); |
989 break; | 1011 break; |
990 } | 1012 } |
991 | 1013 |
992 // Schedule or un-schedule background fetching after each state change. | 1014 // Schedule or un-schedule background fetching after each state change. |
993 // TODO(treib): This resets all currently scheduled fetches on each Chrome | 1015 RescheduleFetching(false); |
994 // start. Maybe store the currently scheduled values in prefs, and only | |
995 // reschedule if they have changed? crbug.com/646842 | |
996 RescheduleFetching(); | |
997 } | 1016 } |
998 | 1017 |
999 void NTPSnippetsService::NotifyNewSuggestions() { | 1018 void NTPSnippetsService::NotifyNewSuggestions() { |
1000 for (const auto& item : categories_) { | 1019 for (const auto& item : categories_) { |
1001 Category category = item.first; | 1020 Category category = item.first; |
1002 const CategoryContent& content = item.second; | 1021 const CategoryContent& content = item.second; |
1003 | 1022 |
1004 std::vector<ContentSuggestion> result; | 1023 std::vector<ContentSuggestion> result; |
1005 for (const std::unique_ptr<NTPSnippet>& snippet : content.snippets) { | 1024 for (const std::unique_ptr<NTPSnippet>& snippet : content.snippets) { |
1006 // TODO(sfiera): if a snippet is not going to be displayed, move it | 1025 // TODO(sfiera): if a snippet is not going to be displayed, move it |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1048 } | 1067 } |
1049 | 1068 |
1050 NTPSnippetsService::CategoryContent::CategoryContent() = default; | 1069 NTPSnippetsService::CategoryContent::CategoryContent() = default; |
1051 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = | 1070 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = |
1052 default; | 1071 default; |
1053 NTPSnippetsService::CategoryContent::~CategoryContent() = default; | 1072 NTPSnippetsService::CategoryContent::~CategoryContent() = default; |
1054 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: | 1073 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: |
1055 operator=(CategoryContent&&) = default; | 1074 operator=(CategoryContent&&) = default; |
1056 | 1075 |
1057 } // namespace ntp_snippets | 1076 } // namespace ntp_snippets |
OLD | NEW |