| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/chromeos/policy/heartbeat_scheduler.h" | 5 #include "chrome/browser/chromeos/policy/heartbeat_scheduler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 12 #include "base/location.h" | 10 #include "base/location.h" |
| 13 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 16 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 17 #include "components/gcm_driver/gcm_driver.h" | 15 #include "components/gcm_driver/gcm_driver.h" |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 const int kMinHeartbeatIntervalMs = 30 * 1000; // 30 seconds | 18 const int kMinHeartbeatIntervalMs = 30 * 1000; // 30 seconds |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 case gcm::GCMClient::ASYNC_OPERATION_PENDING: | 153 case gcm::GCMClient::ASYNC_OPERATION_PENDING: |
| 156 case gcm::GCMClient::TTL_EXCEEDED: | 154 case gcm::GCMClient::TTL_EXCEEDED: |
| 157 default: | 155 default: |
| 158 NOTREACHED() << "Unexpected GCMDriver::Register() result: " << result; | 156 NOTREACHED() << "Unexpected GCMDriver::Register() result: " << result; |
| 159 break; | 157 break; |
| 160 } | 158 } |
| 161 } | 159 } |
| 162 | 160 |
| 163 HeartbeatScheduler::HeartbeatScheduler( | 161 HeartbeatScheduler::HeartbeatScheduler( |
| 164 gcm::GCMDriver* driver, | 162 gcm::GCMDriver* driver, |
| 165 policy::CloudPolicyClient* cloud_policy_client, | |
| 166 const std::string& enrollment_domain, | 163 const std::string& enrollment_domain, |
| 167 const std::string& device_id, | 164 const std::string& device_id, |
| 168 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 165 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 169 : task_runner_(task_runner), | 166 : task_runner_(task_runner), |
| 170 enrollment_domain_(enrollment_domain), | 167 enrollment_domain_(enrollment_domain), |
| 171 device_id_(device_id), | 168 device_id_(device_id), |
| 172 heartbeat_enabled_(false), | 169 heartbeat_enabled_(false), |
| 173 heartbeat_interval_( | 170 heartbeat_interval_(base::TimeDelta::FromMilliseconds( |
| 174 base::TimeDelta::FromMilliseconds(kDefaultHeartbeatIntervalMs)), | 171 kDefaultHeartbeatIntervalMs)), |
| 175 cloud_policy_client_(cloud_policy_client), | |
| 176 gcm_driver_(driver), | 172 gcm_driver_(driver), |
| 177 weak_factory_(this) { | 173 weak_factory_(this) { |
| 178 // If no GCMDriver (e.g. this is loaded as part of an unrelated unit test) | 174 // If no GCMDriver (e.g. this is loaded as part of an unrelated unit test) |
| 179 // do nothing as no heartbeats can be sent. | 175 // do nothing as no heartbeats can be sent. |
| 180 if (!gcm_driver_) | 176 if (!gcm_driver_) |
| 181 return; | 177 return; |
| 182 | 178 |
| 183 heartbeat_frequency_observer_ = | 179 heartbeat_frequency_observer_ = |
| 184 chromeos::CrosSettings::Get()->AddSettingsObserver( | 180 chromeos::CrosSettings::Get()->AddSettingsObserver( |
| 185 chromeos::kHeartbeatFrequency, | 181 chromeos::kHeartbeatFrequency, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 task_runner_->PostDelayedTask( | 290 task_runner_->PostDelayedTask( |
| 295 FROM_HERE, heartbeat_callback_.callback(), delay); | 291 FROM_HERE, heartbeat_callback_.callback(), delay); |
| 296 } | 292 } |
| 297 | 293 |
| 298 void HeartbeatScheduler::OnRegistrationComplete( | 294 void HeartbeatScheduler::OnRegistrationComplete( |
| 299 const std::string& registration_id) { | 295 const std::string& registration_id) { |
| 300 DCHECK(!registration_id.empty()); | 296 DCHECK(!registration_id.empty()); |
| 301 registration_helper_.reset(); | 297 registration_helper_.reset(); |
| 302 registration_id_ = registration_id; | 298 registration_id_ = registration_id; |
| 303 | 299 |
| 304 if (cloud_policy_client_) { | |
| 305 // TODO(binjin): Avoid sending the same GCM id to the server. | |
| 306 // See http://crbug.com/516375 | |
| 307 cloud_policy_client_->UpdateGcmId( | |
| 308 registration_id, | |
| 309 base::Bind(&HeartbeatScheduler::OnGcmIdUpdateRequestSent, | |
| 310 weak_factory_.GetWeakPtr())); | |
| 311 } | |
| 312 | |
| 313 // Now that GCM registration is complete, start sending heartbeats. | 300 // Now that GCM registration is complete, start sending heartbeats. |
| 314 ScheduleNextHeartbeat(); | 301 ScheduleNextHeartbeat(); |
| 315 } | 302 } |
| 316 | 303 |
| 317 void HeartbeatScheduler::SendHeartbeat() { | 304 void HeartbeatScheduler::SendHeartbeat() { |
| 318 DCHECK(!registration_id_.empty()); | 305 DCHECK(!registration_id_.empty()); |
| 319 if (!gcm_driver_ || !heartbeat_enabled_) | 306 if (!gcm_driver_ || !heartbeat_enabled_) |
| 320 return; | 307 return; |
| 321 | 308 |
| 322 gcm::OutgoingMessage message; | 309 gcm::OutgoingMessage message; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 const std::string& app_id, | 365 const std::string& app_id, |
| 379 const gcm::GCMClient::SendErrorDetails& details) { | 366 const gcm::GCMClient::SendErrorDetails& details) { |
| 380 // Ignore send errors - we already are notified above in OnHeartbeatSent(). | 367 // Ignore send errors - we already are notified above in OnHeartbeatSent(). |
| 381 } | 368 } |
| 382 | 369 |
| 383 void HeartbeatScheduler::OnSendAcknowledged(const std::string& app_id, | 370 void HeartbeatScheduler::OnSendAcknowledged(const std::string& app_id, |
| 384 const std::string& message_id) { | 371 const std::string& message_id) { |
| 385 DVLOG(1) << "Heartbeat sent with message_id: " << message_id; | 372 DVLOG(1) << "Heartbeat sent with message_id: " << message_id; |
| 386 } | 373 } |
| 387 | 374 |
| 388 void HeartbeatScheduler::OnGcmIdUpdateRequestSent(bool success) { | |
| 389 // TODO(binjin): Handle the failure, probably by exponential backoff. | |
| 390 LOG_IF(WARNING, !success) << "Failed to send GCM id to DM server"; | |
| 391 } | |
| 392 | |
| 393 } // namespace policy | 375 } // namespace policy |
| OLD | NEW |