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

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_scheduler.cc

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Created 4 years 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h"
6
7 #include <string>
8
9 #include "components/ntp_snippets/features.h"
10 #include "components/ntp_snippets/pref_names.h"
11 #include "components/ntp_snippets/remote/remote_suggestions_hard_scheduler.h"
12 #include "components/ntp_snippets/user_classifier.h"
13 #include "components/prefs/pref_registry_simple.h"
14 #include "components/prefs/pref_service.h"
15 #include "components/variations/variations_associated_data.h"
16
17 namespace ntp_snippets {
18
19 namespace {
20
21 // Default values for fetching intervals, fallback and wifi.
22 const double kDefaultFetchingIntervalRareNtpUser[] = {48.0, 24.0};
23 const double kDefaultFetchingIntervalActiveNtpUser[] = {24.0, 6.0};
24 const double kDefaultFetchingIntervalActiveSuggestionsConsumer[] = {24.0, 6.0};
25
26 // Variation parameters than can override the default fetching intervals.
27 const char* kFetchingIntervalParamNameRareNtpUser[] = {
28 "fetching_interval_hours-fallback-rare_ntp_user",
29 "fetching_interval_hours-wifi-rare_ntp_user"};
30 const char* kFetchingIntervalParamNameActiveNtpUser[] = {
31 "fetching_interval_hours-fallback-active_ntp_user",
32 "fetching_interval_hours-wifi-active_ntp_user"};
33 const char* kFetchingIntervalParamNameActiveSuggestionsConsumer[] = {
34 "fetching_interval_hours-fallback-active_suggestions_consumer",
35 "fetching_interval_hours-wifi-active_suggestions_consumer"};
36
37 base::TimeDelta GetCurrentUpdateInterval(
38 bool is_wifi,
39 UserClassifier::UserClass user_class) {
40 double default_value_hours = 0.0;
41
42 const int index = is_wifi ? 1 : 0;
43 const char* param_name = "";
44 switch (user_class) {
45 case UserClassifier::UserClass::RARE_NTP_USER:
46 default_value_hours = kDefaultFetchingIntervalRareNtpUser[index];
47 param_name = kFetchingIntervalParamNameRareNtpUser[index];
48 break;
49 case UserClassifier::UserClass::ACTIVE_NTP_USER:
50 default_value_hours = kDefaultFetchingIntervalActiveNtpUser[index];
51 param_name = kFetchingIntervalParamNameActiveNtpUser[index];
52 break;
53 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER:
54 default_value_hours =
55 kDefaultFetchingIntervalActiveSuggestionsConsumer[index];
56 param_name = kFetchingIntervalParamNameActiveSuggestionsConsumer[index];
57 break;
58 }
59
60 double value_hours = variations::GetVariationParamByFeatureAsDouble(
61 ntp_snippets::kArticleSuggestionsFeature, param_name,
62 default_value_hours);
63
64 return base::TimeDelta::FromSecondsD(value_hours * 3600.0);
65 }
66
67 } // namespace
68
69 RemoteSuggestionsScheduler::RemoteSuggestionsScheduler(
70 RemoteSuggestionsHardScheduler* hard_scheduler,
71 Updater* updater,
72 const UserClassifier* user_classifier,
73 PrefService* pref_service)
74 : hard_scheduler_(hard_scheduler),
75 updater_(updater),
76 pref_service_(pref_service) {
77 DCHECK(updater) << "non-null Updater must be provided";
78 DCHECK(user_classifier) << "non-null UserClassifier must be provided";
79 DCHECK(pref_service) << "non-null pref service is needed";
80 }
81
82 RemoteSuggestionsScheduler::~RemoteSuggestionsScheduler() = default;
83
84 // static
85 void RemoteSuggestionsScheduler::RegisterProfilePrefs(
86 PrefRegistrySimple* registry) {
87 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalWifi, 0);
88 registry->RegisterInt64Pref(prefs::kSnippetBackgroundFetchingIntervalFallback,
89 0);
90 }
91
92 void RemoteSuggestionsScheduler::PerformHardUpdate() {
93 updater_->UpdateRemoteSuggestionsBySchedule();
94 }
95
96 void RemoteSuggestionsScheduler::Schedule() {
97 // The scheduler only exists on Android so far, it's null on other platforms.
98 if (!hard_scheduler_) {
99 return;
100 }
101
102 UpdateSchedule last_schedule = GetLastUpdateSchedule();
103 UpdateSchedule schedule = GetCurrentUpdateSchedule();
104
105 // Reset the schedule only if the parameters have changed.
106 if (last_schedule.interval_wifi != schedule.interval_wifi ||
107 last_schedule.interval_fallback != schedule.interval_fallback) {
108 ResetUpdateScheduleFromNow(schedule);
109 }
110 }
111
112 void RemoteSuggestionsScheduler::Unschedule() {
113 // The scheduler only exists on Android so far, it's null on other platforms.
114 if (!hard_scheduler_) {
115 return;
116 }
117
118 // Do not unschedule if already switched off
119 UpdateSchedule last_schedule = GetLastUpdateSchedule();
120 if (last_schedule.interval_fallback.is_zero() &&
121 last_schedule.interval_fallback.is_zero()) {
122 return;
123 }
124
125 hard_scheduler_->Unschedule();
126
127 StoreLastUpdateSchedule(UpdateSchedule{base::TimeDelta::FromHours(0),
128 base::TimeDelta::FromHours(0)});
129 }
130
131 void RemoteSuggestionsScheduler::OnSuccessfulUpdate() {
132 // The scheduler only exists on Android so far, it's null on other platforms.
133 if (!hard_scheduler_) {
134 return;
135 }
136
137 ResetUpdateScheduleFromNow(GetLastUpdateSchedule());
138 }
139
140 void RemoteSuggestionsScheduler::ResetUpdateScheduleFromNow(
141 UpdateSchedule schedule) {
142 hard_scheduler_->Schedule(schedule.interval_wifi, schedule.interval_fallback);
143
144 StoreLastUpdateSchedule(schedule);
145 }
146
147 RemoteSuggestionsScheduler::UpdateSchedule
148 RemoteSuggestionsScheduler::GetCurrentUpdateSchedule() {
149 UserClassifier::UserClass user_class = user_classifier_->GetUserClass();
150
151 UpdateSchedule schedule;
152 schedule.interval_wifi =
153 GetCurrentUpdateInterval(/*is_wifi=*/true, user_class);
154 schedule.interval_fallback =
155 GetCurrentUpdateInterval(/*is_wifi=*/false, user_class);
156 return schedule;
157 }
158
159 RemoteSuggestionsScheduler::UpdateSchedule
160 RemoteSuggestionsScheduler::GetLastUpdateSchedule() {
161 UpdateSchedule schedule;
162 schedule.interval_wifi = base::TimeDelta::FromInternalValue(
163 pref_service_->GetInt64(prefs::kSnippetBackgroundFetchingIntervalWifi));
164 schedule.interval_fallback =
165 base::TimeDelta::FromInternalValue(pref_service_->GetInt64(
166 prefs::kSnippetBackgroundFetchingIntervalFallback));
167 return schedule;
168 }
169
170 void RemoteSuggestionsScheduler::StoreLastUpdateSchedule(
171 RemoteSuggestionsScheduler::UpdateSchedule schedule) {
172 pref_service_->SetInt64(
173 prefs::kSnippetBackgroundFetchingIntervalWifi,
174 schedule.interval_wifi.ToInternalValue());
175 pref_service_->SetInt64(
176 prefs::kSnippetBackgroundFetchingIntervalFallback,
177 schedule.interval_fallback.ToInternalValue());
178 }
179
180 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698