Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/gcm_driver/gcm_channel_status_syncer.h" | 5 #include "components/gcm_driver/gcm_channel_status_syncer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | |
| 8 #include "base/location.h" | 9 #include "base/location.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 11 #include "base/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
| 12 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | |
| 14 #include "components/gcm_driver/gcm_channel_status_request.h" | 16 #include "components/gcm_driver/gcm_channel_status_request.h" |
| 15 #include "components/gcm_driver/gcm_driver.h" | 17 #include "components/gcm_driver/gcm_driver.h" |
| 16 #include "components/pref_registry/pref_registry_syncable.h" | 18 #include "components/pref_registry/pref_registry_syncable.h" |
| 17 | 19 |
| 18 namespace gcm { | 20 namespace gcm { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 // The GCM channel's enabled state. | 24 // The GCM channel's enabled state. |
| 23 const char kGCMChannelStatus[] = "gcm.channel_status"; | 25 const char kGCMChannelStatus[] = "gcm.channel_status"; |
| 24 | 26 |
| 25 // The GCM channel's polling interval (in seconds). | 27 // The GCM channel's polling interval (in seconds). |
| 26 const char kGCMChannelPollIntervalSeconds[] = "gcm.poll_interval"; | 28 const char kGCMChannelPollIntervalSeconds[] = "gcm.poll_interval"; |
| 27 | 29 |
| 28 // Last time when checking with the GCM channel status server is done. | 30 // Last time when checking with the GCM channel status server is done. |
| 29 const char kGCMChannelLastCheckTime[] = "gcm.check_time"; | 31 const char kGCMChannelLastCheckTime[] = "gcm.check_time"; |
| 30 | 32 |
| 33 // Override the default poll interval for testing purpose. | |
| 34 const char kCustomPollIntervalMinutes[] = "gcm-channel-poll-interval"; | |
| 35 | |
| 31 // A small delay to avoid sending request at browser startup time for first-time | 36 // A small delay to avoid sending request at browser startup time for first-time |
| 32 // request. | 37 // request. |
| 33 const int kFirstTimeDelaySeconds = 1 * 60; // 1 minute. | 38 const int kFirstTimeDelaySeconds = 1 * 60; // 1 minute. |
| 34 | 39 |
| 35 // The fuzzing variation added to the polling delay. | 40 // The fuzzing variation added to the polling delay. |
| 36 const int kGCMChannelRequestTimeJitterSeconds = 15 * 60; // 15 minues. | 41 const int kGCMChannelRequestTimeJitterSeconds = 15 * 60; // 15 minues. |
| 37 | 42 |
| 38 } // namespace | 43 } // namespace |
| 39 | 44 |
| 40 // static | 45 // static |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 PrefService* prefs, | 78 PrefService* prefs, |
| 74 const std::string& channel_status_request_url, | 79 const std::string& channel_status_request_url, |
| 75 const std::string& user_agent, | 80 const std::string& user_agent, |
| 76 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 81 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 77 : driver_(driver), | 82 : driver_(driver), |
| 78 prefs_(prefs), | 83 prefs_(prefs), |
| 79 channel_status_request_url_(channel_status_request_url), | 84 channel_status_request_url_(channel_status_request_url), |
| 80 user_agent_(user_agent), | 85 user_agent_(user_agent), |
| 81 request_context_(request_context), | 86 request_context_(request_context), |
| 82 gcm_enabled_(true), | 87 gcm_enabled_(true), |
| 88 jitter_enabled_(true), | |
| 83 poll_interval_seconds_( | 89 poll_interval_seconds_( |
| 84 GCMChannelStatusRequest::default_poll_interval_seconds()), | 90 GCMChannelStatusRequest::default_poll_interval_seconds()), |
| 85 delay_removed_for_testing_(false), | 91 delay_removed_for_testing_(false), |
| 86 weak_ptr_factory_(this) { | 92 weak_ptr_factory_(this) { |
| 87 gcm_enabled_ = prefs_->GetBoolean(kGCMChannelStatus); | 93 gcm_enabled_ = prefs_->GetBoolean(kGCMChannelStatus); |
| 88 poll_interval_seconds_ = prefs_->GetInteger(kGCMChannelPollIntervalSeconds); | 94 poll_interval_seconds_ = prefs_->GetInteger(kGCMChannelPollIntervalSeconds); |
| 89 if (poll_interval_seconds_ < | 95 if (poll_interval_seconds_ < |
| 90 GCMChannelStatusRequest::min_poll_interval_seconds()) { | 96 GCMChannelStatusRequest::min_poll_interval_seconds()) { |
| 91 poll_interval_seconds_ = | 97 poll_interval_seconds_ = |
| 92 GCMChannelStatusRequest::min_poll_interval_seconds(); | 98 GCMChannelStatusRequest::min_poll_interval_seconds(); |
| 93 } | 99 } |
| 100 if (CommandLine::ForCurrentProcess()->HasSwitch(kCustomPollIntervalMinutes)) { | |
| 101 std::string value(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 102 kCustomPollIntervalMinutes)); | |
| 103 int minutes = 0; | |
| 104 if (base::StringToInt(value, &minutes)) { | |
| 105 poll_interval_seconds_ = minutes * 60; | |
|
fgorski
2014/10/15 17:01:52
we should make sure it is >0, right?
| |
| 106 // The fuzzing variation is off when the custom interval is used. | |
| 107 jitter_enabled_ = false; | |
| 108 } | |
| 109 } | |
| 94 last_check_time_ = base::Time::FromInternalValue( | 110 last_check_time_ = base::Time::FromInternalValue( |
| 95 prefs_->GetInt64(kGCMChannelLastCheckTime)); | 111 prefs_->GetInt64(kGCMChannelLastCheckTime)); |
| 96 } | 112 } |
| 97 | 113 |
| 98 GCMChannelStatusSyncer::~GCMChannelStatusSyncer() { | 114 GCMChannelStatusSyncer::~GCMChannelStatusSyncer() { |
| 99 } | 115 } |
| 100 | 116 |
| 101 void GCMChannelStatusSyncer::EnsureStarted() { | 117 void GCMChannelStatusSyncer::EnsureStarted() { |
| 102 // Bail out if the request is already scheduled or started. | 118 // Bail out if the request is already scheduled or started. |
| 103 if (weak_ptr_factory_.HasWeakPtrs() || request_) | 119 if (weak_ptr_factory_.HasWeakPtrs() || request_) |
| 104 return; | 120 return; |
| 105 | 121 |
| 106 ScheduleRequest(); | 122 ScheduleRequest(); |
| 107 } | 123 } |
| 108 | 124 |
| 109 void GCMChannelStatusSyncer::Stop() { | 125 void GCMChannelStatusSyncer::Stop() { |
| 110 request_.reset(); | 126 request_.reset(); |
| 111 weak_ptr_factory_.InvalidateWeakPtrs(); | 127 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 112 } | 128 } |
| 113 | 129 |
| 114 void GCMChannelStatusSyncer::OnRequestCompleted(bool enabled, | 130 void GCMChannelStatusSyncer::OnRequestCompleted(bool update_received, |
| 131 bool enabled, | |
| 115 int poll_interval_seconds) { | 132 int poll_interval_seconds) { |
| 116 DCHECK(request_); | 133 DCHECK(request_); |
| 117 request_.reset(); | 134 request_.reset(); |
| 118 | 135 |
| 119 // Persist the current time as the last request complete time. | 136 // Persist the current time as the last request complete time. |
| 120 last_check_time_ = base::Time::Now(); | 137 last_check_time_ = base::Time::Now(); |
| 121 prefs_->SetInt64(kGCMChannelLastCheckTime, | 138 prefs_->SetInt64(kGCMChannelLastCheckTime, |
| 122 last_check_time_.ToInternalValue()); | 139 last_check_time_.ToInternalValue()); |
| 123 | 140 |
| 124 if (gcm_enabled_ != enabled) { | 141 if (update_received) { |
| 125 gcm_enabled_ = enabled; | 142 if (gcm_enabled_ != enabled) { |
| 126 prefs_->SetBoolean(kGCMChannelStatus, enabled); | 143 gcm_enabled_ = enabled; |
| 127 if (gcm_enabled_) | 144 prefs_->SetBoolean(kGCMChannelStatus, enabled); |
| 128 driver_->Enable(); | 145 if (gcm_enabled_) |
| 129 else | 146 driver_->Enable(); |
| 130 driver_->Disable(); | 147 else |
| 131 } | 148 driver_->Disable(); |
| 149 } | |
| 132 | 150 |
| 133 DCHECK_GE(poll_interval_seconds, | 151 DCHECK_GE(poll_interval_seconds, |
| 134 GCMChannelStatusRequest::min_poll_interval_seconds()); | 152 GCMChannelStatusRequest::min_poll_interval_seconds()); |
| 135 if (poll_interval_seconds_ != poll_interval_seconds) { | 153 if (poll_interval_seconds_ != poll_interval_seconds) { |
| 136 poll_interval_seconds_ = poll_interval_seconds; | 154 poll_interval_seconds_ = poll_interval_seconds; |
| 137 prefs_->SetInteger(kGCMChannelPollIntervalSeconds, poll_interval_seconds_); | 155 prefs_->SetInteger(kGCMChannelPollIntervalSeconds, |
| 156 poll_interval_seconds_); | |
| 157 } | |
| 138 } | 158 } |
| 139 | 159 |
| 140 ScheduleRequest(); | 160 ScheduleRequest(); |
| 141 } | 161 } |
| 142 | 162 |
| 143 void GCMChannelStatusSyncer::ScheduleRequest() { | 163 void GCMChannelStatusSyncer::ScheduleRequest() { |
| 144 current_request_delay_interval_ = GetRequestDelayInterval(); | 164 current_request_delay_interval_ = GetRequestDelayInterval(); |
| 145 base::MessageLoop::current()->PostDelayedTask( | 165 base::MessageLoop::current()->PostDelayedTask( |
| 146 FROM_HERE, | 166 FROM_HERE, |
| 147 base::Bind(&GCMChannelStatusSyncer::StartRequest, | 167 base::Bind(&GCMChannelStatusSyncer::StartRequest, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 173 if (delay_seconds < 0) | 193 if (delay_seconds < 0) |
| 174 delay_seconds = 0; | 194 delay_seconds = 0; |
| 175 | 195 |
| 176 if (last_check_time_.is_null()) { | 196 if (last_check_time_.is_null()) { |
| 177 // For the first-time request, add a small delay to avoid sending request at | 197 // For the first-time request, add a small delay to avoid sending request at |
| 178 // browser startup time. | 198 // browser startup time. |
| 179 DCHECK(!delay_seconds); | 199 DCHECK(!delay_seconds); |
| 180 delay_seconds = kFirstTimeDelaySeconds; | 200 delay_seconds = kFirstTimeDelaySeconds; |
| 181 } else { | 201 } else { |
| 182 // Otherwise, add a fuzzing variation to the delay. | 202 // Otherwise, add a fuzzing variation to the delay. |
| 183 delay_seconds += base::RandInt(0, kGCMChannelRequestTimeJitterSeconds); | 203 if (jitter_enabled_) |
| 204 delay_seconds += base::RandInt(0, kGCMChannelRequestTimeJitterSeconds); | |
| 184 } | 205 } |
| 185 | 206 |
| 186 return base::TimeDelta::FromSeconds(delay_seconds); | 207 return base::TimeDelta::FromSeconds(delay_seconds); |
| 187 } | 208 } |
| 188 | 209 |
| 189 } // namespace gcm | 210 } // namespace gcm |
| OLD | NEW |