| 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 return; | 104 return; |
| 105 | 105 |
| 106 ScheduleRequest(); | 106 ScheduleRequest(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void GCMChannelStatusSyncer::Stop() { | 109 void GCMChannelStatusSyncer::Stop() { |
| 110 request_.reset(); | 110 request_.reset(); |
| 111 weak_ptr_factory_.InvalidateWeakPtrs(); | 111 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void GCMChannelStatusSyncer::OnRequestCompleted(bool enabled, | 114 void GCMChannelStatusSyncer::OnRequestCompleted(bool update_received, |
| 115 bool enabled, |
| 115 int poll_interval_seconds) { | 116 int poll_interval_seconds) { |
| 116 DCHECK(request_); | 117 DCHECK(request_); |
| 117 request_.reset(); | 118 request_.reset(); |
| 118 | 119 |
| 119 // Persist the current time as the last request complete time. | 120 // Persist the current time as the last request complete time. |
| 120 last_check_time_ = base::Time::Now(); | 121 last_check_time_ = base::Time::Now(); |
| 121 prefs_->SetInt64(kGCMChannelLastCheckTime, | 122 prefs_->SetInt64(kGCMChannelLastCheckTime, |
| 122 last_check_time_.ToInternalValue()); | 123 last_check_time_.ToInternalValue()); |
| 123 | 124 |
| 124 if (gcm_enabled_ != enabled) { | 125 if (update_received) { |
| 125 gcm_enabled_ = enabled; | 126 if (gcm_enabled_ != enabled) { |
| 126 prefs_->SetBoolean(kGCMChannelStatus, enabled); | 127 gcm_enabled_ = enabled; |
| 127 if (gcm_enabled_) | 128 prefs_->SetBoolean(kGCMChannelStatus, enabled); |
| 128 driver_->Enable(); | 129 if (gcm_enabled_) |
| 129 else | 130 driver_->Enable(); |
| 130 driver_->Disable(); | 131 else |
| 131 } | 132 driver_->Disable(); |
| 133 } |
| 132 | 134 |
| 133 DCHECK_GE(poll_interval_seconds, | 135 DCHECK_GE(poll_interval_seconds, |
| 134 GCMChannelStatusRequest::min_poll_interval_seconds()); | 136 GCMChannelStatusRequest::min_poll_interval_seconds()); |
| 135 if (poll_interval_seconds_ != poll_interval_seconds) { | 137 if (poll_interval_seconds_ != poll_interval_seconds) { |
| 136 poll_interval_seconds_ = poll_interval_seconds; | 138 poll_interval_seconds_ = poll_interval_seconds; |
| 137 prefs_->SetInteger(kGCMChannelPollIntervalSeconds, poll_interval_seconds_); | 139 prefs_->SetInteger(kGCMChannelPollIntervalSeconds, |
| 140 poll_interval_seconds_); |
| 141 } |
| 138 } | 142 } |
| 139 | 143 |
| 140 ScheduleRequest(); | 144 ScheduleRequest(); |
| 141 } | 145 } |
| 142 | 146 |
| 143 void GCMChannelStatusSyncer::ScheduleRequest() { | 147 void GCMChannelStatusSyncer::ScheduleRequest() { |
| 144 current_request_delay_interval_ = GetRequestDelayInterval(); | 148 current_request_delay_interval_ = GetRequestDelayInterval(); |
| 145 base::MessageLoop::current()->PostDelayedTask( | 149 base::MessageLoop::current()->PostDelayedTask( |
| 146 FROM_HERE, | 150 FROM_HERE, |
| 147 base::Bind(&GCMChannelStatusSyncer::StartRequest, | 151 base::Bind(&GCMChannelStatusSyncer::StartRequest, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 delay_seconds = kFirstTimeDelaySeconds; | 184 delay_seconds = kFirstTimeDelaySeconds; |
| 181 } else { | 185 } else { |
| 182 // Otherwise, add a fuzzing variation to the delay. | 186 // Otherwise, add a fuzzing variation to the delay. |
| 183 delay_seconds += base::RandInt(0, kGCMChannelRequestTimeJitterSeconds); | 187 delay_seconds += base::RandInt(0, kGCMChannelRequestTimeJitterSeconds); |
| 184 } | 188 } |
| 185 | 189 |
| 186 return base::TimeDelta::FromSeconds(delay_seconds); | 190 return base::TimeDelta::FromSeconds(delay_seconds); |
| 187 } | 191 } |
| 188 | 192 |
| 189 } // namespace gcm | 193 } // namespace gcm |
| OLD | NEW |