Chromium Code Reviews| 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" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "components/gcm_driver/gcm_driver.h" | 17 #include "components/gcm_driver/gcm_driver.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 const int kMinHeartbeatIntervalMs = 30 * 1000; // 30 seconds | 20 const int kMinHeartbeatIntervalMs = 30 * 1000; // 30 seconds |
| 21 const int kMaxHeartbeatIntervalMs = 24 * 60 * 60 * 1000; // 24 hours | 21 const int kMaxHeartbeatIntervalMs = 24 * 60 * 60 * 1000; // 24 hours |
| 22 const int kMinimumSignupIntervalMs = 3 * 60 * 1000; // 3 minutes | |
|
Andrew T Wilson (Slow)
2015/11/18 11:43:32
Would be good to add a comment for what this is.
binjin
2015/11/19 05:50:27
Done.
| |
| 22 | 23 |
| 23 // Our sender ID we send up with all of our GCM messages. | 24 // Our sender ID we send up with all of our GCM messages. |
| 24 const char* kHeartbeatGCMAppID = "com.google.chromeos.monitoring"; | 25 const char* kHeartbeatGCMAppID = "com.google.chromeos.monitoring"; |
| 25 | 26 |
| 26 // The default destination we send our GCM messages to. | 27 // The default destination we send our GCM messages to. |
| 27 const char* kHeartbeatGCMDestinationID = "1013309121859"; | 28 const char* kHeartbeatGCMDestinationID = "1013309121859"; |
| 28 const char* kHeartbeatGCMSenderSuffix = "@google.com"; | 29 const char* kHeartbeatGCMSenderSuffix = "@google.com"; |
| 29 | 30 |
| 30 const char* kMonitoringMessageTypeKey = "type"; | 31 // Destination of upstream notification sign up message. |
| 32 const char* kUpstreamNotificationSignUpDestinationID = | |
| 33 "https://gcm.googleapis.com/gcm/gcm.event_tracker"; | |
| 34 | |
| 35 // A bit mask, listening events of upstream notification. | |
| 36 const char* kUpstreamNotificationSignUpListeningEvents = | |
| 37 "3"; // START | DISCONNECTED | |
| 38 | |
| 39 const char* kGcmMessageTypeKey = "type"; | |
| 31 const char* kHeartbeatTimestampKey = "timestamp"; | 40 const char* kHeartbeatTimestampKey = "timestamp"; |
| 32 const char* kHeartbeatDomainNameKey = "domain_name"; | 41 const char* kHeartbeatDomainNameKey = "domain_name"; |
| 33 const char* kHeartbeatDeviceIDKey = "device_id"; | 42 const char* kHeartbeatDeviceIDKey = "device_id"; |
| 34 const char* kHeartbeatTypeValue = "hb"; | 43 const char* kHeartbeatTypeValue = "hb"; |
| 44 const char* kUpstreamNotificationNotifyKey = "notify"; | |
| 35 | 45 |
| 36 // If we get an error registering with GCM, try again in two minutes. | 46 // If we get an error registering with GCM, try again in two minutes. |
| 37 const int64 kRegistrationRetryDelayMs = 2 * 60 * 1000; | 47 const int64 kRegistrationRetryDelayMs = 2 * 60 * 1000; |
| 38 | 48 |
| 39 // Returns the destination ID for GCM heartbeats. | 49 // Returns the destination ID for GCM heartbeats. |
| 40 std::string GetDestinationID() { | 50 std::string GetDestinationID() { |
| 41 std::string receiver_id = kHeartbeatGCMDestinationID; | 51 std::string receiver_id = kHeartbeatGCMDestinationID; |
| 42 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 52 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 43 switches::kMonitoringDestinationID)) { | 53 switches::kMonitoringDestinationID)) { |
| 44 receiver_id = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 54 receiver_id = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 bool enabled; | 229 bool enabled; |
| 220 if (settings->GetBoolean(chromeos::kHeartbeatEnabled, &enabled)) | 230 if (settings->GetBoolean(chromeos::kHeartbeatEnabled, &enabled)) |
| 221 heartbeat_enabled_ = enabled; | 231 heartbeat_enabled_ = enabled; |
| 222 | 232 |
| 223 if (!heartbeat_enabled_) { | 233 if (!heartbeat_enabled_) { |
| 224 // Heartbeats are no longer enabled - cancel our callback and any | 234 // Heartbeats are no longer enabled - cancel our callback and any |
| 225 // outstanding registration attempts and disconnect from GCM so the | 235 // outstanding registration attempts and disconnect from GCM so the |
| 226 // connection can be shut down. If heartbeats are re-enabled later, we | 236 // connection can be shut down. If heartbeats are re-enabled later, we |
| 227 // will re-register with GCM. | 237 // will re-register with GCM. |
| 228 heartbeat_callback_.Cancel(); | 238 heartbeat_callback_.Cancel(); |
| 229 ShutdownGCM(); | 239 ShutdownGCM(); |
|
Andrew T Wilson (Slow)
2015/11/18 11:43:32
Do we need to do something here to stop the upstre
binjin
2015/11/19 05:50:27
I didn't find any API to opt-out the upstream noti
| |
| 230 } else { | 240 } else { |
| 231 // Schedule a new upload with the new frequency. | 241 // Schedule a new upload with the new frequency. |
| 232 ScheduleNextHeartbeat(); | 242 ScheduleNextHeartbeat(); |
| 233 } | 243 } |
| 234 | 244 |
| 235 DVLOG(1) << "heartbeat enabled: " << heartbeat_enabled_; | 245 DVLOG(1) << "heartbeat enabled: " << heartbeat_enabled_; |
| 236 DVLOG(1) << "heartbeat frequency: " << heartbeat_interval_; | 246 DVLOG(1) << "heartbeat frequency: " << heartbeat_interval_; |
| 237 } | 247 } |
| 238 | 248 |
| 239 void HeartbeatScheduler::ShutdownGCM() { | 249 void HeartbeatScheduler::ShutdownGCM() { |
| 240 registration_helper_.reset(); | 250 registration_helper_.reset(); |
| 241 registration_id_.clear(); | 251 registration_id_.clear(); |
| 242 if (registered_app_handler_) { | 252 if (registered_app_handler_) { |
| 243 registered_app_handler_ = false; | 253 registered_app_handler_ = false; |
| 244 gcm_driver_->RemoveAppHandler(kHeartbeatGCMAppID); | 254 gcm_driver_->RemoveAppHandler(kHeartbeatGCMAppID); |
| 255 gcm_driver_->RemoveConnectionObserver(this); | |
| 245 } | 256 } |
| 246 } | 257 } |
| 247 | 258 |
| 248 base::TimeDelta HeartbeatScheduler::EnsureValidHeartbeatInterval( | 259 base::TimeDelta HeartbeatScheduler::EnsureValidHeartbeatInterval( |
| 249 const base::TimeDelta& interval) { | 260 const base::TimeDelta& interval) { |
| 250 const base::TimeDelta min = base::TimeDelta::FromMilliseconds( | 261 const base::TimeDelta min = base::TimeDelta::FromMilliseconds( |
| 251 kMinHeartbeatIntervalMs); | 262 kMinHeartbeatIntervalMs); |
| 252 const base::TimeDelta max = base::TimeDelta::FromMilliseconds( | 263 const base::TimeDelta max = base::TimeDelta::FromMilliseconds( |
| 253 kMaxHeartbeatIntervalMs); | 264 kMaxHeartbeatIntervalMs); |
| 254 if (interval < min) { | 265 if (interval < min) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 267 if (!heartbeat_enabled_) | 278 if (!heartbeat_enabled_) |
| 268 return; | 279 return; |
| 269 | 280 |
| 270 if (registration_id_.empty()) { | 281 if (registration_id_.empty()) { |
| 271 // We are not registered with the GCM service yet, so kick off registration. | 282 // We are not registered with the GCM service yet, so kick off registration. |
| 272 if (!registration_helper_) { | 283 if (!registration_helper_) { |
| 273 // Add ourselves as an AppHandler - this is required in order to setup | 284 // Add ourselves as an AppHandler - this is required in order to setup |
| 274 // a GCM connection. | 285 // a GCM connection. |
| 275 registered_app_handler_ = true; | 286 registered_app_handler_ = true; |
| 276 gcm_driver_->AddAppHandler(kHeartbeatGCMAppID, this); | 287 gcm_driver_->AddAppHandler(kHeartbeatGCMAppID, this); |
| 288 gcm_driver_->AddConnectionObserver(this); | |
| 277 registration_helper_.reset(new HeartbeatRegistrationHelper( | 289 registration_helper_.reset(new HeartbeatRegistrationHelper( |
| 278 gcm_driver_, task_runner_)); | 290 gcm_driver_, task_runner_)); |
| 279 registration_helper_->Register( | 291 registration_helper_->Register( |
| 280 base::Bind(&HeartbeatScheduler::OnRegistrationComplete, | 292 base::Bind(&HeartbeatScheduler::OnRegistrationComplete, |
| 281 weak_factory_.GetWeakPtr())); | 293 weak_factory_.GetWeakPtr())); |
| 282 } | 294 } |
| 283 return; | 295 return; |
| 284 } | 296 } |
| 285 | 297 |
| 286 // Calculate when to fire off the next update (if it should have already | 298 // Calculate when to fire off the next update (if it should have already |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 301 registration_helper_.reset(); | 313 registration_helper_.reset(); |
| 302 registration_id_ = registration_id; | 314 registration_id_ = registration_id; |
| 303 | 315 |
| 304 if (cloud_policy_client_) { | 316 if (cloud_policy_client_) { |
| 305 // TODO(binjin): Avoid sending the same GCM id to the server. | 317 // TODO(binjin): Avoid sending the same GCM id to the server. |
| 306 // See http://crbug.com/516375 | 318 // See http://crbug.com/516375 |
| 307 cloud_policy_client_->UpdateGcmId( | 319 cloud_policy_client_->UpdateGcmId( |
| 308 registration_id, | 320 registration_id, |
| 309 base::Bind(&HeartbeatScheduler::OnGcmIdUpdateRequestSent, | 321 base::Bind(&HeartbeatScheduler::OnGcmIdUpdateRequestSent, |
| 310 weak_factory_.GetWeakPtr())); | 322 weak_factory_.GetWeakPtr())); |
| 323 SignUpUpstreamNotification(); | |
| 311 } | 324 } |
| 312 | 325 |
| 313 // Now that GCM registration is complete, start sending heartbeats. | 326 // Now that GCM registration is complete, start sending heartbeats. |
| 314 ScheduleNextHeartbeat(); | 327 ScheduleNextHeartbeat(); |
| 315 } | 328 } |
| 316 | 329 |
| 317 void HeartbeatScheduler::SendHeartbeat() { | 330 void HeartbeatScheduler::SendHeartbeat() { |
| 318 DCHECK(!registration_id_.empty()); | 331 DCHECK(!registration_id_.empty()); |
| 319 if (!gcm_driver_ || !heartbeat_enabled_) | 332 if (!gcm_driver_ || !heartbeat_enabled_) |
| 320 return; | 333 return; |
| 321 | 334 |
| 322 gcm::OutgoingMessage message; | 335 gcm::OutgoingMessage message; |
| 323 message.time_to_live = heartbeat_interval_.InSeconds(); | 336 message.time_to_live = heartbeat_interval_.InSeconds(); |
| 324 // Just use the current timestamp as the message ID - if the user changes the | 337 // Just use the current timestamp as the message ID - if the user changes the |
| 325 // time and we send a message with the same ID that we previously used, no | 338 // time and we send a message with the same ID that we previously used, no |
| 326 // big deal (the new message will replace the old, which is the behavior we | 339 // big deal (the new message will replace the old, which is the behavior we |
| 327 // want anyway, per: | 340 // want anyway, per: |
| 328 // https://developer.chrome.com/apps/cloudMessaging#send_messages | 341 // https://developer.chrome.com/apps/cloudMessaging#send_messages |
| 329 message.id = base::Int64ToString( | 342 message.id = base::Int64ToString( |
| 330 base::Time::NowFromSystemTime().ToInternalValue()); | 343 base::Time::NowFromSystemTime().ToInternalValue()); |
| 331 message.data[kMonitoringMessageTypeKey] = kHeartbeatTypeValue; | 344 message.data[kGcmMessageTypeKey] = kHeartbeatTypeValue; |
| 332 message.data[kHeartbeatTimestampKey] = base::Int64ToString( | 345 message.data[kHeartbeatTimestampKey] = base::Int64ToString( |
| 333 base::Time::NowFromSystemTime().ToJavaTime()); | 346 base::Time::NowFromSystemTime().ToJavaTime()); |
| 334 message.data[kHeartbeatDomainNameKey] = enrollment_domain_; | 347 message.data[kHeartbeatDomainNameKey] = enrollment_domain_; |
| 335 message.data[kHeartbeatDeviceIDKey] = device_id_; | 348 message.data[kHeartbeatDeviceIDKey] = device_id_; |
| 336 gcm_driver_->Send(kHeartbeatGCMAppID, | 349 gcm_driver_->Send(kHeartbeatGCMAppID, |
| 337 GetDestinationID() + kHeartbeatGCMSenderSuffix, | 350 GetDestinationID() + kHeartbeatGCMSenderSuffix, |
| 338 message, | 351 message, |
| 339 base::Bind(&HeartbeatScheduler::OnHeartbeatSent, | 352 base::Bind(&HeartbeatScheduler::OnHeartbeatSent, |
| 340 weak_factory_.GetWeakPtr())); | 353 weak_factory_.GetWeakPtr())); |
| 341 } | 354 } |
| 342 | 355 |
| 356 void HeartbeatScheduler::SignUpUpstreamNotification() { | |
|
jinzhang1
2015/11/16 19:10:49
Hey Bin, I don't see you use AddHeartbeatInterval(
binjin
2015/11/16 19:13:15
This will be in a separated CL.
| |
| 357 DCHECK(gcm_driver_); | |
| 358 | |
| 359 if (registration_id_.empty()) | |
| 360 return; | |
| 361 | |
| 362 base::TimeDelta since_last_sign_up = | |
| 363 base::TimeTicks::Now() - last_upstream_notification_signup_time_; | |
| 364 | |
| 365 if (!last_upstream_notification_signup_time_.is_null() && | |
|
Andrew T Wilson (Slow)
2015/11/18 11:43:32
What's this doing? Seems like you are making sure
binjin
2015/11/19 05:50:27
The concern is over the GCM driver OnConnected eve
| |
| 366 since_last_sign_up < | |
| 367 base::TimeDelta::FromMilliseconds(kMinimumSignupIntervalMs)) { | |
| 368 return; | |
| 369 } | |
| 370 | |
| 371 gcm::OutgoingMessage message; | |
| 372 message.id = | |
| 373 base::Int64ToString(base::Time::NowFromSystemTime().ToInternalValue()); | |
| 374 message.data[kGcmMessageTypeKey] = kUpstreamNotificationSignUpListeningEvents; | |
| 375 message.data[kUpstreamNotificationNotifyKey] = | |
| 376 GetDestinationID() + kHeartbeatGCMSenderSuffix; | |
| 377 gcm_driver_->Send(kHeartbeatGCMAppID, | |
| 378 kUpstreamNotificationSignUpDestinationID, message, | |
|
Andrew T Wilson (Slow)
2015/11/18 11:43:32
Do we need to be able to specify a different desti
binjin
2015/11/19 05:50:27
The current destination ID (https://gcm.googleapis
| |
| 379 base::Bind(&HeartbeatScheduler::OnUpstreamNotificationSent, | |
| 380 weak_factory_.GetWeakPtr())); | |
| 381 } | |
| 382 | |
| 343 void HeartbeatScheduler::OnHeartbeatSent(const std::string& message_id, | 383 void HeartbeatScheduler::OnHeartbeatSent(const std::string& message_id, |
| 344 gcm::GCMClient::Result result) { | 384 gcm::GCMClient::Result result) { |
| 345 DVLOG(1) << "Monitoring heartbeat sent - result = " << result; | 385 DVLOG(1) << "Monitoring heartbeat sent - result = " << result; |
| 346 // Don't care if the result was successful or not - just schedule the next | 386 // Don't care if the result was successful or not - just schedule the next |
| 347 // heartbeat. | 387 // heartbeat. |
| 348 DLOG_IF(ERROR, result != gcm::GCMClient::SUCCESS) << | 388 DLOG_IF(ERROR, result != gcm::GCMClient::SUCCESS) << |
| 349 "Error sending monitoring heartbeat: " << result; | 389 "Error sending monitoring heartbeat: " << result; |
| 350 last_heartbeat_ = base::Time::NowFromSystemTime(); | 390 last_heartbeat_ = base::Time::NowFromSystemTime(); |
| 351 ScheduleNextHeartbeat(); | 391 ScheduleNextHeartbeat(); |
| 352 } | 392 } |
| 353 | 393 |
| 394 void HeartbeatScheduler::OnUpstreamNotificationSent( | |
| 395 const std::string& message_id, | |
| 396 gcm::GCMClient::Result result) { | |
| 397 DVLOG(1) << "Upstream notification signup message sent - result = " << result; | |
| 398 DLOG_IF(ERROR, result != gcm::GCMClient::SUCCESS) | |
| 399 << "Error sending upstream notification signup message: " << result; | |
| 400 last_upstream_notification_signup_time_ = base::TimeTicks::Now(); | |
| 401 } | |
| 402 | |
| 354 HeartbeatScheduler::~HeartbeatScheduler() { | 403 HeartbeatScheduler::~HeartbeatScheduler() { |
| 355 ShutdownGCM(); | 404 ShutdownGCM(); |
| 356 } | 405 } |
| 357 | 406 |
| 358 void HeartbeatScheduler::ShutdownHandler() { | 407 void HeartbeatScheduler::ShutdownHandler() { |
| 359 // This should never be called, because BrowserProcessImpl::StartTearDown() | 408 // This should never be called, because BrowserProcessImpl::StartTearDown() |
| 360 // should shutdown the BrowserPolicyConnector (which destroys this object) | 409 // should shutdown the BrowserPolicyConnector (which destroys this object) |
| 361 // before the GCMDriver. Our goal is to make sure that this object is always | 410 // before the GCMDriver. Our goal is to make sure that this object is always |
| 362 // shutdown before GCMDriver is shut down, rather than trying to handle the | 411 // shutdown before GCMDriver is shut down, rather than trying to handle the |
| 363 // case when GCMDriver goes away. | 412 // case when GCMDriver goes away. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 378 const std::string& app_id, | 427 const std::string& app_id, |
| 379 const gcm::GCMClient::SendErrorDetails& details) { | 428 const gcm::GCMClient::SendErrorDetails& details) { |
| 380 // Ignore send errors - we already are notified above in OnHeartbeatSent(). | 429 // Ignore send errors - we already are notified above in OnHeartbeatSent(). |
| 381 } | 430 } |
| 382 | 431 |
| 383 void HeartbeatScheduler::OnSendAcknowledged(const std::string& app_id, | 432 void HeartbeatScheduler::OnSendAcknowledged(const std::string& app_id, |
| 384 const std::string& message_id) { | 433 const std::string& message_id) { |
| 385 DVLOG(1) << "Heartbeat sent with message_id: " << message_id; | 434 DVLOG(1) << "Heartbeat sent with message_id: " << message_id; |
| 386 } | 435 } |
| 387 | 436 |
| 437 void HeartbeatScheduler::OnConnected(const net::IPEndPoint&) { | |
| 438 SignUpUpstreamNotification(); | |
| 439 } | |
| 440 | |
| 388 void HeartbeatScheduler::OnGcmIdUpdateRequestSent(bool success) { | 441 void HeartbeatScheduler::OnGcmIdUpdateRequestSent(bool success) { |
| 389 // TODO(binjin): Handle the failure, probably by exponential backoff. | 442 // TODO(binjin): Handle the failure, probably by exponential backoff. |
| 390 LOG_IF(WARNING, !success) << "Failed to send GCM id to DM server"; | 443 LOG_IF(WARNING, !success) << "Failed to send GCM id to DM server"; |
| 391 } | 444 } |
| 392 | 445 |
| 393 } // namespace policy | 446 } // namespace policy |
| OLD | NEW |