1 // Copyright 2014 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.
5 #include "components/gcm_driver/gcm_channel_status_syncer.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/rand_util.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "components/gcm_driver/gcm_channel_status_request.h"
17 #include "components/gcm_driver/gcm_driver.h"
18 #include "components/pref_registry/pref_registry_syncable.h"
24 // A small delay to avoid sending request at browser startup time for first-time
26 const int kFirstTimeDelaySeconds
= 1 * 60; // 1 minute.
28 // The fuzzing variation added to the polling delay.
29 const int kGCMChannelRequestTimeJitterSeconds
= 15 * 60; // 15 minues.
31 // The minimum poll interval that can be overridden to.
32 const int kMinCustomPollIntervalMinutes
= 2;
34 // Custom poll interval could not be used more than the limit below.
35 const int kMaxNumberToUseCustomPollInterval
= 10;
41 // The GCM channel's enabled state.
42 const char kGCMChannelStatus
[] = "gcm.channel_status";
44 // The GCM channel's polling interval (in seconds).
45 const char kGCMChannelPollIntervalSeconds
[] = "gcm.poll_interval";
47 // Last time when checking with the GCM channel status server is done.
48 const char kGCMChannelLastCheckTime
[] = "gcm.check_time";
54 // Override the default poll interval for testing purpose.
55 const char kCustomPollIntervalMinutes
[] = "gcm-channel-poll-interval";
57 } // namepsace switches
60 void GCMChannelStatusSyncer::RegisterPrefs(PrefRegistrySimple
* registry
) {
61 registry
->RegisterBooleanPref(prefs::kGCMChannelStatus
, true);
62 registry
->RegisterIntegerPref(
63 prefs::kGCMChannelPollIntervalSeconds
,
64 GCMChannelStatusRequest::default_poll_interval_seconds());
65 registry
->RegisterInt64Pref(prefs::kGCMChannelLastCheckTime
, 0);
69 void GCMChannelStatusSyncer::RegisterProfilePrefs(
70 user_prefs::PrefRegistrySyncable
* registry
) {
71 registry
->RegisterBooleanPref(prefs::kGCMChannelStatus
, true);
72 registry
->RegisterIntegerPref(
73 prefs::kGCMChannelPollIntervalSeconds
,
74 GCMChannelStatusRequest::default_poll_interval_seconds());
75 registry
->RegisterInt64Pref(prefs::kGCMChannelLastCheckTime
, 0);
79 int GCMChannelStatusSyncer::first_time_delay_seconds() {
80 return kFirstTimeDelaySeconds
;
83 GCMChannelStatusSyncer::GCMChannelStatusSyncer(
86 const std::string
& channel_status_request_url
,
87 const std::string
& user_agent
,
88 const scoped_refptr
<net::URLRequestContextGetter
>& request_context
)
91 channel_status_request_url_(channel_status_request_url
),
92 user_agent_(user_agent
),
93 request_context_(request_context
),
96 poll_interval_seconds_(
97 GCMChannelStatusRequest::default_poll_interval_seconds()),
98 custom_poll_interval_use_count_(0),
99 delay_removed_for_testing_(false),
100 weak_ptr_factory_(this) {
101 gcm_enabled_
= prefs_
->GetBoolean(prefs::kGCMChannelStatus
);
102 poll_interval_seconds_
= prefs_
->GetInteger(
103 prefs::kGCMChannelPollIntervalSeconds
);
104 if (poll_interval_seconds_
<
105 GCMChannelStatusRequest::min_poll_interval_seconds()) {
106 poll_interval_seconds_
=
107 GCMChannelStatusRequest::min_poll_interval_seconds();
109 const base::CommandLine
& command_line
=
110 *base::CommandLine::ForCurrentProcess();
111 if (command_line
.HasSwitch(switches::kCustomPollIntervalMinutes
)) {
112 std::string
value(command_line
.GetSwitchValueASCII(
113 switches::kCustomPollIntervalMinutes
));
115 if (base::StringToInt(value
, &minutes
)) {
116 DCHECK_GE(minutes
, kMinCustomPollIntervalMinutes
);
117 if (minutes
>= kMinCustomPollIntervalMinutes
) {
118 poll_interval_seconds_
= minutes
* 60;
119 custom_poll_interval_use_count_
= kMaxNumberToUseCustomPollInterval
;
123 last_check_time_
= base::Time::FromInternalValue(
124 prefs_
->GetInt64(prefs::kGCMChannelLastCheckTime
));
127 GCMChannelStatusSyncer::~GCMChannelStatusSyncer() {
130 void GCMChannelStatusSyncer::EnsureStarted() {
131 // Bail out if the request is already scheduled or started.
139 void GCMChannelStatusSyncer::Stop() {
142 weak_ptr_factory_
.InvalidateWeakPtrs();
145 void GCMChannelStatusSyncer::OnRequestCompleted(bool update_received
,
147 int poll_interval_seconds
) {
151 // Persist the current time as the last request complete time.
152 last_check_time_
= base::Time::Now();
153 prefs_
->SetInt64(prefs::kGCMChannelLastCheckTime
,
154 last_check_time_
.ToInternalValue());
156 if (update_received
) {
157 if (gcm_enabled_
!= enabled
) {
158 gcm_enabled_
= enabled
;
159 prefs_
->SetBoolean(prefs::kGCMChannelStatus
, enabled
);
166 // Skip updating poll interval if the custom one is still in effect.
167 if (!custom_poll_interval_use_count_
) {
168 DCHECK_GE(poll_interval_seconds
,
169 GCMChannelStatusRequest::min_poll_interval_seconds());
170 if (poll_interval_seconds_
!= poll_interval_seconds
) {
171 poll_interval_seconds_
= poll_interval_seconds
;
172 prefs_
->SetInteger(prefs::kGCMChannelPollIntervalSeconds
,
173 poll_interval_seconds_
);
178 // Do not schedule next request if syncer is stopped.
183 void GCMChannelStatusSyncer::ScheduleRequest() {
184 current_request_delay_interval_
= GetRequestDelayInterval();
185 base::MessageLoop::current()->PostDelayedTask(
187 base::Bind(&GCMChannelStatusSyncer::StartRequest
,
188 weak_ptr_factory_
.GetWeakPtr()),
189 current_request_delay_interval_
);
191 if (custom_poll_interval_use_count_
)
192 custom_poll_interval_use_count_
--;
195 void GCMChannelStatusSyncer::StartRequest() {
198 request_
.reset(new GCMChannelStatusRequest(
200 channel_status_request_url_
,
202 base::Bind(&GCMChannelStatusSyncer::OnRequestCompleted
,
203 weak_ptr_factory_
.GetWeakPtr())));
207 base::TimeDelta
GCMChannelStatusSyncer::GetRequestDelayInterval() const {
208 // No delay during testing.
209 if (delay_removed_for_testing_
)
210 return base::TimeDelta();
212 // Make sure that checking with server occurs at polling interval, regardless
213 // whether the browser restarts.
214 int64 delay_seconds
= poll_interval_seconds_
-
215 (base::Time::Now() - last_check_time_
).InSeconds();
216 if (delay_seconds
< 0)
219 if (last_check_time_
.is_null()) {
220 // For the first-time request, add a small delay to avoid sending request at
221 // browser startup time.
222 DCHECK(!delay_seconds
);
223 delay_seconds
= kFirstTimeDelaySeconds
;
225 // Otherwise, add a fuzzing variation to the delay.
226 // The fuzzing variation is off when the custom interval is used.
227 if (!custom_poll_interval_use_count_
)
228 delay_seconds
+= base::RandInt(0, kGCMChannelRequestTimeJitterSeconds
);
231 return base::TimeDelta::FromSeconds(delay_seconds
);