jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "components/gcm_driver/gcm_channel_status_syncer.h" |
| 6 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 9 | #include "base/bind.h" |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 10 | #include "base/command_line.h" |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 11 | #include "base/location.h" |
| 12 | #include "base/logging.h" |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 13 | #include "base/rand_util.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 14 | #include "base/single_thread_task_runner.h" |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 16 | #include "base/threading/thread_task_runner_handle.h" |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 17 | #include "components/gcm_driver/gcm_channel_status_request.h" |
| 18 | #include "components/gcm_driver/gcm_driver.h" |
| 19 | #include "components/pref_registry/pref_registry_syncable.h" |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 20 | #include "components/prefs/pref_registry_simple.h" |
| 21 | #include "components/prefs/pref_service.h" |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 22 | |
| 23 | namespace gcm { |
| 24 | |
| 25 | namespace { |
| 26 | |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 27 | // A small delay to avoid sending request at browser startup time for first-time |
| 28 | // request. |
| 29 | const int kFirstTimeDelaySeconds = 1 * 60; // 1 minute. |
| 30 | |
| 31 | // The fuzzing variation added to the polling delay. |
| 32 | const int kGCMChannelRequestTimeJitterSeconds = 15 * 60; // 15 minues. |
| 33 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 34 | // The minimum poll interval that can be overridden to. |
| 35 | const int kMinCustomPollIntervalMinutes = 2; |
| 36 | |
| 37 | // Custom poll interval could not be used more than the limit below. |
| 38 | const int kMaxNumberToUseCustomPollInterval = 10; |
| 39 | |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 40 | } // namespace |
| 41 | |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 42 | namespace prefs { |
| 43 | |
| 44 | // The GCM channel's enabled state. |
| 45 | const char kGCMChannelStatus[] = "gcm.channel_status"; |
| 46 | |
| 47 | // The GCM channel's polling interval (in seconds). |
| 48 | const char kGCMChannelPollIntervalSeconds[] = "gcm.poll_interval"; |
| 49 | |
| 50 | // Last time when checking with the GCM channel status server is done. |
| 51 | const char kGCMChannelLastCheckTime[] = "gcm.check_time"; |
| 52 | |
| 53 | } // namepsace prefs |
| 54 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 55 | namespace switches { |
| 56 | |
| 57 | // Override the default poll interval for testing purpose. |
| 58 | const char kCustomPollIntervalMinutes[] = "gcm-channel-poll-interval"; |
| 59 | |
| 60 | } // namepsace switches |
| 61 | |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 62 | // static |
| 63 | void GCMChannelStatusSyncer::RegisterPrefs(PrefRegistrySimple* registry) { |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 64 | registry->RegisterBooleanPref(prefs::kGCMChannelStatus, true); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 65 | registry->RegisterIntegerPref( |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 66 | prefs::kGCMChannelPollIntervalSeconds, |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 67 | GCMChannelStatusRequest::default_poll_interval_seconds()); |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 68 | registry->RegisterInt64Pref(prefs::kGCMChannelLastCheckTime, 0); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // static |
| 72 | void GCMChannelStatusSyncer::RegisterProfilePrefs( |
| 73 | user_prefs::PrefRegistrySyncable* registry) { |
raymes | aa60872 | 2015-04-27 03:00:25 | [diff] [blame] | 74 | registry->RegisterBooleanPref(prefs::kGCMChannelStatus, true); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 75 | registry->RegisterIntegerPref( |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 76 | prefs::kGCMChannelPollIntervalSeconds, |
raymes | aa60872 | 2015-04-27 03:00:25 | [diff] [blame] | 77 | GCMChannelStatusRequest::default_poll_interval_seconds()); |
| 78 | registry->RegisterInt64Pref(prefs::kGCMChannelLastCheckTime, 0); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // static |
| 82 | int GCMChannelStatusSyncer::first_time_delay_seconds() { |
| 83 | return kFirstTimeDelaySeconds; |
| 84 | } |
| 85 | |
| 86 | GCMChannelStatusSyncer::GCMChannelStatusSyncer( |
| 87 | GCMDriver* driver, |
| 88 | PrefService* prefs, |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 89 | const std::string& channel_status_request_url, |
| 90 | const std::string& user_agent, |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 91 | const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 92 | : driver_(driver), |
| 93 | prefs_(prefs), |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 94 | channel_status_request_url_(channel_status_request_url), |
| 95 | user_agent_(user_agent), |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 96 | request_context_(request_context), |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 97 | started_(false), |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 98 | gcm_enabled_(true), |
| 99 | poll_interval_seconds_( |
| 100 | GCMChannelStatusRequest::default_poll_interval_seconds()), |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 101 | custom_poll_interval_use_count_(0), |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 102 | delay_removed_for_testing_(false), |
| 103 | weak_ptr_factory_(this) { |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 104 | gcm_enabled_ = prefs_->GetBoolean(prefs::kGCMChannelStatus); |
| 105 | poll_interval_seconds_ = prefs_->GetInteger( |
| 106 | prefs::kGCMChannelPollIntervalSeconds); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 107 | if (poll_interval_seconds_ < |
| 108 | GCMChannelStatusRequest::min_poll_interval_seconds()) { |
| 109 | poll_interval_seconds_ = |
| 110 | GCMChannelStatusRequest::min_poll_interval_seconds(); |
| 111 | } |
kkosztyo.u-szeged | b33617c | 2014-12-04 09:54:36 | [diff] [blame] | 112 | const base::CommandLine& command_line = |
| 113 | *base::CommandLine::ForCurrentProcess(); |
| 114 | if (command_line.HasSwitch(switches::kCustomPollIntervalMinutes)) { |
| 115 | std::string value(command_line.GetSwitchValueASCII( |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 116 | switches::kCustomPollIntervalMinutes)); |
| 117 | int minutes = 0; |
| 118 | if (base::StringToInt(value, &minutes)) { |
| 119 | DCHECK_GE(minutes, kMinCustomPollIntervalMinutes); |
| 120 | if (minutes >= kMinCustomPollIntervalMinutes) { |
| 121 | poll_interval_seconds_ = minutes * 60; |
| 122 | custom_poll_interval_use_count_ = kMaxNumberToUseCustomPollInterval; |
| 123 | } |
| 124 | } |
| 125 | } |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 126 | last_check_time_ = base::Time::FromInternalValue( |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 127 | prefs_->GetInt64(prefs::kGCMChannelLastCheckTime)); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | GCMChannelStatusSyncer::~GCMChannelStatusSyncer() { |
| 131 | } |
| 132 | |
| 133 | void GCMChannelStatusSyncer::EnsureStarted() { |
| 134 | // Bail out if the request is already scheduled or started. |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 135 | if (started_) |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 136 | return; |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 137 | started_ = true; |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 138 | |
| 139 | ScheduleRequest(); |
| 140 | } |
| 141 | |
| 142 | void GCMChannelStatusSyncer::Stop() { |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 143 | started_ = false; |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 144 | request_.reset(); |
| 145 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 146 | } |
| 147 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 148 | void GCMChannelStatusSyncer::OnRequestCompleted(bool update_received, |
| 149 | bool enabled, |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 150 | int poll_interval_seconds) { |
| 151 | DCHECK(request_); |
| 152 | request_.reset(); |
| 153 | |
| 154 | // Persist the current time as the last request complete time. |
| 155 | last_check_time_ = base::Time::Now(); |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 156 | prefs_->SetInt64(prefs::kGCMChannelLastCheckTime, |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 157 | last_check_time_.ToInternalValue()); |
| 158 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 159 | if (update_received) { |
| 160 | if (gcm_enabled_ != enabled) { |
| 161 | gcm_enabled_ = enabled; |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 162 | prefs_->SetBoolean(prefs::kGCMChannelStatus, enabled); |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 163 | if (gcm_enabled_) |
| 164 | driver_->Enable(); |
| 165 | else |
| 166 | driver_->Disable(); |
| 167 | } |
| 168 | |
| 169 | // Skip updating poll interval if the custom one is still in effect. |
| 170 | if (!custom_poll_interval_use_count_) { |
| 171 | DCHECK_GE(poll_interval_seconds, |
| 172 | GCMChannelStatusRequest::min_poll_interval_seconds()); |
| 173 | if (poll_interval_seconds_ != poll_interval_seconds) { |
| 174 | poll_interval_seconds_ = poll_interval_seconds; |
jianli | fe3a8bd | 2015-02-05 00:55:25 | [diff] [blame] | 175 | prefs_->SetInteger(prefs::kGCMChannelPollIntervalSeconds, |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 176 | poll_interval_seconds_); |
| 177 | } |
| 178 | } |
jianli | cd88641 | 2014-10-14 23:16:14 | [diff] [blame] | 179 | } |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 180 | |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 181 | // Do not schedule next request if syncer is stopped. |
| 182 | if (started_) |
| 183 | ScheduleRequest(); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void GCMChannelStatusSyncer::ScheduleRequest() { |
| 187 | current_request_delay_interval_ = GetRequestDelayInterval(); |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 188 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 189 | FROM_HERE, base::Bind(&GCMChannelStatusSyncer::StartRequest, |
| 190 | weak_ptr_factory_.GetWeakPtr()), |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 191 | current_request_delay_interval_); |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 192 | |
| 193 | if (custom_poll_interval_use_count_) |
| 194 | custom_poll_interval_use_count_--; |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void GCMChannelStatusSyncer::StartRequest() { |
| 198 | DCHECK(!request_); |
| 199 | |
| 200 | request_.reset(new GCMChannelStatusRequest( |
| 201 | request_context_, |
fgorski | 3f6b84cd | 2014-10-10 21:04:18 | [diff] [blame] | 202 | channel_status_request_url_, |
| 203 | user_agent_, |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 204 | base::Bind(&GCMChannelStatusSyncer::OnRequestCompleted, |
| 205 | weak_ptr_factory_.GetWeakPtr()))); |
| 206 | request_->Start(); |
| 207 | } |
| 208 | |
| 209 | base::TimeDelta GCMChannelStatusSyncer::GetRequestDelayInterval() const { |
| 210 | // No delay during testing. |
| 211 | if (delay_removed_for_testing_) |
| 212 | return base::TimeDelta(); |
| 213 | |
| 214 | // Make sure that checking with server occurs at polling interval, regardless |
| 215 | // whether the browser restarts. |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 216 | int64_t delay_seconds = poll_interval_seconds_ - |
| 217 | (base::Time::Now() - last_check_time_).InSeconds(); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 218 | if (delay_seconds < 0) |
| 219 | delay_seconds = 0; |
| 220 | |
| 221 | if (last_check_time_.is_null()) { |
| 222 | // For the first-time request, add a small delay to avoid sending request at |
| 223 | // browser startup time. |
| 224 | DCHECK(!delay_seconds); |
| 225 | delay_seconds = kFirstTimeDelaySeconds; |
| 226 | } else { |
| 227 | // Otherwise, add a fuzzing variation to the delay. |
Jian Li | 74eadb0 | 2014-10-16 05:53:56 | [diff] [blame] | 228 | // The fuzzing variation is off when the custom interval is used. |
| 229 | if (!custom_poll_interval_use_count_) |
| 230 | delay_seconds += base::RandInt(0, kGCMChannelRequestTimeJitterSeconds); |
jianli | 2dc910b0 | 2014-09-19 02:42:46 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return base::TimeDelta::FromSeconds(delay_seconds); |
| 234 | } |
| 235 | |
| 236 | } // namespace gcm |