Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1265)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 2363753002: [NTP Snippets] Don't reschedule background fetching on every startup (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, 187 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded,
188 base::Unretained(this))); 188 base::Unretained(this)));
189 } 189 }
190 190
191 NTPSnippetsService::~NTPSnippetsService() { 191 NTPSnippetsService::~NTPSnippetsService() {
192 } 192 }
193 193
194 // static 194 // static
195 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { 195 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
196 registry->RegisterListPref(prefs::kSnippetHosts); 196 registry->RegisterListPref(prefs::kSnippetHosts);
197 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0);
198 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback,
199 0);
197 200
198 NTPSnippetsStatusService::RegisterProfilePrefs(registry); 201 NTPSnippetsStatusService::RegisterProfilePrefs(registry);
199 } 202 }
200 203
201 void NTPSnippetsService::FetchSnippets(bool interactive_request) { 204 void NTPSnippetsService::FetchSnippets(bool interactive_request) {
202 if (ready()) 205 if (ready())
203 FetchSnippetsFromHosts(GetSuggestionsHosts(), interactive_request); 206 FetchSnippetsFromHosts(GetSuggestionsHosts(), interactive_request);
204 else 207 else
205 fetch_when_ready_ = true; 208 fetch_when_ready_ = true;
206 } 209 }
(...skipping 16 matching lines...) Expand all
223 for (const auto& item : categories_) { 226 for (const auto& item : categories_) {
224 const CategoryContent& content = item.second; 227 const CategoryContent& content = item.second;
225 for (const auto& snippet : content.dismissed) 228 for (const auto& snippet : content.dismissed)
226 excluded_ids.insert(snippet->id()); 229 excluded_ids.insert(snippet->id());
227 } 230 }
228 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, 231 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_,
229 excluded_ids, kMaxSnippetCount, 232 excluded_ids, kMaxSnippetCount,
230 interactive_request); 233 interactive_request);
231 } 234 }
232 235
233 void NTPSnippetsService::RescheduleFetching() { 236 void NTPSnippetsService::RescheduleFetching(bool force) {
234 // The scheduler only exists on Android so far, it's null on other platforms. 237 // The scheduler only exists on Android so far, it's null on other platforms.
235 if (!scheduler_) 238 if (!scheduler_)
236 return; 239 return;
237 240
238 // If we're NOT_INITED, we don't know whether to schedule or un-schedule.
239 // We'll reschedule on the next state change anyway, so do nothing here.
240 if (state_ == State::NOT_INITED)
241 return;
242
243 if (ready()) { 241 if (ready()) {
244 scheduler_->Schedule(GetFetchingIntervalWifi(), 242 base::TimeDelta old_interval_wifi =
245 GetFetchingIntervalFallback()); 243 base::TimeDelta::FromInternalValue(pref_service_->GetInt64(
244 prefs::kSnippetBackgroundFetchingIntervalWifi));
245 base::TimeDelta old_interval_fallback =
246 base::TimeDelta::FromInternalValue(pref_service_->GetInt64(
247 prefs::kSnippetBackgroundFetchingIntervalFallback));
248 base::TimeDelta interval_wifi = GetFetchingIntervalWifi();
249 base::TimeDelta interval_fallback = GetFetchingIntervalFallback();
250 if (force || interval_wifi != old_interval_wifi ||
251 interval_fallback != old_interval_fallback) {
jkrcal 2016/09/23 10:28:34 What if: - the current fallback is scheduled, say
Marc Treib 2016/09/23 11:19:13 Prefs do survive the upgrade, so they won't be res
jkrcal 2016/09/23 11:24:53 I see, thanks!
252 scheduler_->Schedule(interval_wifi, interval_fallback);
253 pref_service_->SetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi,
254 interval_wifi.ToInternalValue());
255 pref_service_->SetInt64(
256 prefs::kSnippetBackgroundFetchingIntervalFallback,
257 interval_fallback.ToInternalValue());
258 }
246 } else { 259 } else {
247 scheduler_->Unschedule(); 260 // If we're NOT_INITED, we don't know whether to schedule or un-schedule.
261 // If |force| is false, all is well: We'll reschedule on the next state
262 // change anyway. If it's true, then unschedule here, to make sure that the
263 // next reschedule actually happens.
264 if (state_ != State::NOT_INITED || force) {
265 scheduler_->Unschedule();
266 pref_service_->ClearPref(prefs::kSnippetBackgroundFetchingIntervalWifi);
267 pref_service_->ClearPref(
268 prefs::kSnippetBackgroundFetchingIntervalFallback);
269 }
248 } 270 }
249 } 271 }
250 272
251 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { 273 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) {
252 DCHECK(categories_.find(category) != categories_.end()); 274 DCHECK(categories_.find(category) != categories_.end());
253 return categories_[category].status; 275 return categories_[category].status;
254 } 276 }
255 277
256 CategoryInfo NTPSnippetsService::GetCategoryInfo(Category category) { 278 CategoryInfo NTPSnippetsService::GetCategoryInfo(Category category) {
257 DCHECK(categories_.find(category) != categories_.end()); 279 DCHECK(categories_.find(category) != categories_.end());
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 } 576 }
555 577
556 // TODO(sfiera): notify only when a category changed above. 578 // TODO(sfiera): notify only when a category changed above.
557 NotifyNewSuggestions(); 579 NotifyNewSuggestions();
558 580
559 // Reschedule after a successful fetch. This resets all currently scheduled 581 // Reschedule after a successful fetch. This resets all currently scheduled
560 // fetches, to make sure the fallback interval triggers only if no wifi fetch 582 // fetches, to make sure the fallback interval triggers only if no wifi fetch
561 // succeeded, and also that we don't do a background fetch immediately after 583 // succeeded, and also that we don't do a background fetch immediately after
562 // a user-initiated one. 584 // a user-initiated one.
563 if (snippets) 585 if (snippets)
564 RescheduleFetching(); 586 RescheduleFetching(true);
565 } 587 }
566 588
567 void NTPSnippetsService::MergeSnippets(Category category, 589 void NTPSnippetsService::MergeSnippets(Category category,
568 NTPSnippet::PtrVector new_snippets) { 590 NTPSnippet::PtrVector new_snippets) {
569 DCHECK(ready()); 591 DCHECK(ready());
570 CategoryContent* content = &categories_[category]; 592 CategoryContent* content = &categories_[category];
571 593
572 // Remove new snippets that we already have, or that have been dismissed. 594 // Remove new snippets that we already have, or that have been dismissed.
573 std::set<std::string> old_snippet_ids; 595 std::set<std::string> old_snippet_ids;
574 InsertAllIDs(content->dismissed, &old_snippet_ids); 596 InsertAllIDs(content->dismissed, &old_snippet_ids);
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
946 break; 968 break;
947 969
948 case State::ERROR_OCCURRED: 970 case State::ERROR_OCCURRED:
949 DVLOG(1) << "Entering state: ERROR_OCCURRED"; 971 DVLOG(1) << "Entering state: ERROR_OCCURRED";
950 state_ = State::ERROR_OCCURRED; 972 state_ = State::ERROR_OCCURRED;
951 EnterStateError(); 973 EnterStateError();
952 break; 974 break;
953 } 975 }
954 976
955 // Schedule or un-schedule background fetching after each state change. 977 // Schedule or un-schedule background fetching after each state change.
956 // TODO(treib): This resets all currently scheduled fetches on each Chrome 978 RescheduleFetching(false);
957 // start. Maybe store the currently scheduled values in prefs, and only
958 // reschedule if they have changed? crbug.com/646842
959 RescheduleFetching();
960 } 979 }
961 980
962 void NTPSnippetsService::NotifyNewSuggestions() { 981 void NTPSnippetsService::NotifyNewSuggestions() {
963 for (const auto& item : categories_) { 982 for (const auto& item : categories_) {
964 Category category = item.first; 983 Category category = item.first;
965 const CategoryContent& content = item.second; 984 const CategoryContent& content = item.second;
966 985
967 std::vector<ContentSuggestion> result; 986 std::vector<ContentSuggestion> result;
968 for (const std::unique_ptr<NTPSnippet>& snippet : content.snippets) { 987 for (const std::unique_ptr<NTPSnippet>& snippet : content.snippets) {
969 // TODO(sfiera): if a snippet is not going to be displayed, move it 988 // 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
1011 } 1030 }
1012 1031
1013 NTPSnippetsService::CategoryContent::CategoryContent() = default; 1032 NTPSnippetsService::CategoryContent::CategoryContent() = default;
1014 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) = 1033 NTPSnippetsService::CategoryContent::CategoryContent(CategoryContent&&) =
1015 default; 1034 default;
1016 NTPSnippetsService::CategoryContent::~CategoryContent() = default; 1035 NTPSnippetsService::CategoryContent::~CategoryContent() = default;
1017 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent:: 1036 NTPSnippetsService::CategoryContent& NTPSnippetsService::CategoryContent::
1018 operator=(CategoryContent&&) = default; 1037 operator=(CategoryContent&&) = default;
1019 1038
1020 } // namespace ntp_snippets 1039 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698