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

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

Issue 1869323003: [NTP Snippets] Scheduler: Fetch on Wifi (without charging) only 6AM-10PM (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_merge_controller
Patch Set: remove fudge Created 4 years, 8 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
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <utility> 8 #include <utility>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 17 matching lines...) Expand all
28 using suggestions::SuggestionsService; 28 using suggestions::SuggestionsService;
29 29
30 namespace ntp_snippets { 30 namespace ntp_snippets {
31 31
32 namespace { 32 namespace {
33 33
34 const int kFetchingIntervalWifiChargingSeconds = 30 * 60; 34 const int kFetchingIntervalWifiChargingSeconds = 30 * 60;
35 const int kFetchingIntervalWifiSeconds = 2 * 60 * 60; 35 const int kFetchingIntervalWifiSeconds = 2 * 60 * 60;
36 const int kFetchingIntervalFallbackSeconds = 24 * 60 * 60; 36 const int kFetchingIntervalFallbackSeconds = 24 * 60 * 60;
37 37
38 // These define the times of day during which we will fetch via Wifi (without
39 // charging) - 6 AM to 10 PM.
40 const int kWifiFetchingHourMin = 6;
41 const int kWifiFetchingHourMax = 22;
42
38 const int kDefaultExpiryTimeMins = 24 * 60; 43 const int kDefaultExpiryTimeMins = 24 * 60;
39 44
40 base::TimeDelta GetFetchingInterval(const char* switch_name, 45 base::TimeDelta GetFetchingInterval(const char* switch_name,
41 int default_value_seconds) { 46 int default_value_seconds) {
42 int value_seconds = default_value_seconds; 47 int value_seconds = default_value_seconds;
43 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess(); 48 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
44 if (cmdline.HasSwitch(switch_name)) { 49 if (cmdline.HasSwitch(switch_name)) {
45 std::string str = cmdline.GetSwitchValueASCII(switch_name); 50 std::string str = cmdline.GetSwitchValueASCII(switch_name);
46 int switch_value_seconds = 0; 51 int switch_value_seconds = 0;
47 if (base::StringToInt(str, &switch_value_seconds)) 52 if (base::StringToInt(str, &switch_value_seconds))
48 value_seconds = switch_value_seconds; 53 value_seconds = switch_value_seconds;
49 else 54 else
50 LOG(WARNING) << "Invalid value for switch " << switch_name; 55 LOG(WARNING) << "Invalid value for switch " << switch_name;
51 } 56 }
52 return base::TimeDelta::FromSeconds(value_seconds); 57 return base::TimeDelta::FromSeconds(value_seconds);
53 } 58 }
54 59
55 base::TimeDelta GetFetchingIntervalWifiCharging() { 60 base::TimeDelta GetFetchingIntervalWifiCharging() {
56 return GetFetchingInterval(switches::kFetchingIntervalWifiChargingSeconds, 61 return GetFetchingInterval(switches::kFetchingIntervalWifiChargingSeconds,
57 kFetchingIntervalWifiChargingSeconds); 62 kFetchingIntervalWifiChargingSeconds);
58 } 63 }
59 64
60 base::TimeDelta GetFetchingIntervalWifi() { 65 base::TimeDelta GetFetchingIntervalWifi(const base::Time& now) {
61 return GetFetchingInterval(switches::kFetchingIntervalWifiSeconds, 66 // Only fetch via Wifi (without charging) during the proper times of day.
62 kFetchingIntervalWifiSeconds); 67 base::Time::Exploded exploded;
68 now.LocalExplode(&exploded);
69 if (kWifiFetchingHourMin <= exploded.hour &&
70 exploded.hour < kWifiFetchingHourMax) {
71 return GetFetchingInterval(switches::kFetchingIntervalWifiSeconds,
72 kFetchingIntervalWifiSeconds);
73 }
74 return base::TimeDelta();
63 } 75 }
64 76
65 base::TimeDelta GetFetchingIntervalFallback() { 77 base::TimeDelta GetFetchingIntervalFallback() {
66 return GetFetchingInterval(switches::kFetchingIntervalFallbackSeconds, 78 return GetFetchingInterval(switches::kFetchingIntervalFallbackSeconds,
67 kFetchingIntervalFallbackSeconds); 79 kFetchingIntervalFallbackSeconds);
68 } 80 }
69 81
82 base::Time GetRescheduleTime(const base::Time& now) {
83 base::Time::Exploded exploded;
84 now.LocalExplode(&exploded);
85 // The scheduling changes at both |kWifiFetchingHourMin| and
86 // |kWifiFetchingHourMax|. Find the time of the next one that we'll hit.
87 bool next_day = false;
88 if (exploded.hour < kWifiFetchingHourMin) {
89 exploded.hour = kWifiFetchingHourMin;
90 } else if (exploded.hour < kWifiFetchingHourMax) {
91 exploded.hour = kWifiFetchingHourMax;
92 } else {
93 next_day = true;
94 exploded.hour = kWifiFetchingHourMin;
95 }
96 // In any case, reschedule at the full hour.
97 exploded.minute = 0;
98 exploded.second = 0;
99 exploded.millisecond = 0;
100 base::Time reschedule = base::Time::FromLocalExploded(exploded);
101 if (next_day)
102 reschedule += base::TimeDelta::FromDays(1);
103
104 return reschedule;
105 }
106
70 // Extracts the hosts from |suggestions| and returns them in a set. 107 // Extracts the hosts from |suggestions| and returns them in a set.
71 std::set<std::string> GetSuggestionsHostsImpl( 108 std::set<std::string> GetSuggestionsHostsImpl(
72 const SuggestionsProfile& suggestions) { 109 const SuggestionsProfile& suggestions) {
73 std::set<std::string> hosts; 110 std::set<std::string> hosts;
74 for (int i = 0; i < suggestions.suggestions_size(); ++i) { 111 for (int i = 0; i < suggestions.suggestions_size(); ++i) {
75 const ChromeSuggestion& suggestion = suggestions.suggestions(i); 112 const ChromeSuggestion& suggestion = suggestions.suggestions(i);
76 GURL url(suggestion.url()); 113 GURL url(suggestion.url());
77 if (url.is_valid()) 114 if (url.is_valid())
78 hosts.insert(url.host()); 115 hosts.insert(url.host());
79 } 116 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 } // namespace 154 } // namespace
118 155
119 NTPSnippetsService::NTPSnippetsService( 156 NTPSnippetsService::NTPSnippetsService(
120 PrefService* pref_service, 157 PrefService* pref_service,
121 SuggestionsService* suggestions_service, 158 SuggestionsService* suggestions_service,
122 scoped_refptr<base::SequencedTaskRunner> file_task_runner, 159 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
123 const std::string& application_language_code, 160 const std::string& application_language_code,
124 NTPSnippetsScheduler* scheduler, 161 NTPSnippetsScheduler* scheduler,
125 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher, 162 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher,
126 const ParseJSONCallback& parse_json_callback) 163 const ParseJSONCallback& parse_json_callback)
127 : pref_service_(pref_service), 164 : enabled_(false),
165 pref_service_(pref_service),
128 suggestions_service_(suggestions_service), 166 suggestions_service_(suggestions_service),
129 file_task_runner_(file_task_runner), 167 file_task_runner_(file_task_runner),
130 application_language_code_(application_language_code), 168 application_language_code_(application_language_code),
131 scheduler_(scheduler), 169 scheduler_(scheduler),
132 snippets_fetcher_(std::move(snippets_fetcher)), 170 snippets_fetcher_(std::move(snippets_fetcher)),
133 parse_json_callback_(parse_json_callback), 171 parse_json_callback_(parse_json_callback),
134 weak_ptr_factory_(this) { 172 weak_ptr_factory_(this) {
135 snippets_fetcher_subscription_ = snippets_fetcher_->AddCallback(base::Bind( 173 snippets_fetcher_subscription_ = snippets_fetcher_->AddCallback(base::Bind(
136 &NTPSnippetsService::OnSnippetsDownloaded, base::Unretained(this))); 174 &NTPSnippetsService::OnSnippetsDownloaded, base::Unretained(this)));
137 } 175 }
138 176
139 NTPSnippetsService::~NTPSnippetsService() {} 177 NTPSnippetsService::~NTPSnippetsService() {}
140 178
141 // static 179 // static
142 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { 180 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
143 registry->RegisterListPref(prefs::kSnippets); 181 registry->RegisterListPref(prefs::kSnippets);
144 registry->RegisterListPref(prefs::kDiscardedSnippets); 182 registry->RegisterListPref(prefs::kDiscardedSnippets);
145 registry->RegisterListPref(prefs::kSnippetHosts); 183 registry->RegisterListPref(prefs::kSnippetHosts);
146 } 184 }
147 185
148 void NTPSnippetsService::Init(bool enabled) { 186 void NTPSnippetsService::Init(bool enabled) {
149 if (enabled) { 187 enabled_ = enabled;
188 if (enabled_) {
150 // |suggestions_service_| can be null in tests. 189 // |suggestions_service_| can be null in tests.
151 if (suggestions_service_) { 190 if (suggestions_service_) {
152 suggestions_service_subscription_ = suggestions_service_->AddCallback( 191 suggestions_service_subscription_ = suggestions_service_->AddCallback(
153 base::Bind(&NTPSnippetsService::OnSuggestionsChanged, 192 base::Bind(&NTPSnippetsService::OnSuggestionsChanged,
154 base::Unretained(this))); 193 base::Unretained(this)));
155 } 194 }
156 195
157 // Get any existing snippets immediately from prefs. 196 // Get any existing snippets immediately from prefs.
158 LoadDiscardedSnippetsFromPrefs(); 197 LoadDiscardedSnippetsFromPrefs();
159 LoadSnippetsFromPrefs(); 198 LoadSnippetsFromPrefs();
160 199
161 // If we don't have any snippets yet, start a fetch. 200 // If we don't have any snippets yet, start a fetch.
162 if (snippets_.empty()) 201 if (snippets_.empty())
163 FetchSnippets(); 202 FetchSnippets();
164 } 203 }
165 204
166 // The scheduler only exists on Android so far, it's null on other platforms. 205 RescheduleFetching();
167 if (!scheduler_)
168 return;
169
170 if (enabled) {
171 scheduler_->Schedule(GetFetchingIntervalWifiCharging(),
172 GetFetchingIntervalWifi(),
173 GetFetchingIntervalFallback());
174 } else {
175 scheduler_->Unschedule();
176 }
177 } 206 }
178 207
179 void NTPSnippetsService::Shutdown() { 208 void NTPSnippetsService::Shutdown() {
180 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 209 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
181 NTPSnippetsServiceShutdown()); 210 NTPSnippetsServiceShutdown());
211 enabled_ = false;
182 } 212 }
183 213
184 void NTPSnippetsService::FetchSnippets() { 214 void NTPSnippetsService::FetchSnippets() {
185 FetchSnippetsFromHosts(GetSuggestionsHosts()); 215 FetchSnippetsFromHosts(GetSuggestionsHosts());
186 } 216 }
187 217
188 void NTPSnippetsService::FetchSnippetsFromHosts( 218 void NTPSnippetsService::FetchSnippetsFromHosts(
189 const std::set<std::string>& hosts) { 219 const std::set<std::string>& hosts) {
190 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 220 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
191 switches::kDontRestrict)) { 221 switches::kDontRestrict)) {
192 snippets_fetcher_->FetchSnippets(std::set<std::string>()); 222 snippets_fetcher_->FetchSnippets(std::set<std::string>());
193 return; 223 return;
194 } 224 }
195 if (!hosts.empty()) 225 if (!hosts.empty())
196 snippets_fetcher_->FetchSnippets(hosts); 226 snippets_fetcher_->FetchSnippets(hosts);
197 } 227 }
198 228
229 void NTPSnippetsService::RescheduleFetching() {
230 // The scheduler only exists on Android so far, it's null on other platforms.
231 if (!scheduler_)
232 return;
233
234 if (enabled_) {
235 base::Time now = base::Time::Now();
236 scheduler_->Schedule(
237 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now),
238 GetFetchingIntervalFallback(), GetRescheduleTime(now));
239 } else {
240 scheduler_->Unschedule();
241 }
242 }
243
199 void NTPSnippetsService::ClearSnippets() { 244 void NTPSnippetsService::ClearSnippets() {
200 snippets_.clear(); 245 snippets_.clear();
201 246
202 StoreSnippetsToPrefs(); 247 StoreSnippetsToPrefs();
203 248
204 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 249 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
205 NTPSnippetsServiceLoaded()); 250 NTPSnippetsServiceLoaded());
206 } 251 }
207 252
208 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { 253 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const {
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 if (snippet->expiry_date() < next_expiry) 469 if (snippet->expiry_date() < next_expiry)
425 next_expiry = snippet->expiry_date(); 470 next_expiry = snippet->expiry_date();
426 } 471 }
427 DCHECK_GT(next_expiry, expiry); 472 DCHECK_GT(next_expiry, expiry);
428 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 473 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
429 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, 474 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets,
430 base::Unretained(this))); 475 base::Unretained(this)));
431 } 476 }
432 477
433 } // namespace ntp_snippets 478 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698