Chromium Code Reviews| Index: google_apis/gcm/gcm_client_impl.cc |
| diff --git a/google_apis/gcm/gcm_client_impl.cc b/google_apis/gcm/gcm_client_impl.cc |
| index 4cc0fdd38b0acf28280c6df8b7cef754703a3db2..86dcb584be0a1b5f1e5da719de850f12a8ea6be7 100644 |
| --- a/google_apis/gcm/gcm_client_impl.cc |
| +++ b/google_apis/gcm/gcm_client_impl.cc |
| @@ -74,6 +74,7 @@ enum MessageType { |
| const char kMCSEndpointMain[] = "https://mtalk.google.com:5228"; |
| const char kMCSEndpointFallback[] = "https://mtalk.google.com:443"; |
| +const int64 kDefaultCheckinInterval = 2 * 24 * 60 * 60LL; // seconds = 2 days. |
| const int kMaxRegistrationRetries = 5; |
| const char kMessageTypeDataMessage[] = "gcm"; |
| const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; |
| @@ -209,18 +210,20 @@ void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { |
| } |
| registrations_ = result->registrations; |
| - |
| device_checkin_info_.android_id = result->device_android_id; |
| device_checkin_info_.secret = result->device_security_token; |
| + base::Time last_checkin_time = result->last_checkin_time; |
| InitializeMCSClient(result.Pass()); |
| - if (!device_checkin_info_.IsValid()) { |
| - device_checkin_info_.Reset(); |
| - state_ = INITIAL_DEVICE_CHECKIN; |
| - StartCheckin(device_checkin_info_); |
| + |
| + if (device_checkin_info_.IsValid()) { |
| + SchedulePeriodicCheckin(last_checkin_time); |
| + OnReady(); |
| return; |
| } |
| - OnReady(); |
| + state_ = INITIAL_DEVICE_CHECKIN; |
| + device_checkin_info_.Reset(); |
| + StartCheckin(); |
| } |
| void GCMClientImpl::InitializeMCSClient( |
| @@ -281,14 +284,14 @@ void GCMClientImpl::ResetState() { |
| // TODO(fgorski): reset all of the necessart objects and start over. |
| } |
| -void GCMClientImpl::StartCheckin(const CheckinInfo& checkin_info) { |
| +void GCMClientImpl::StartCheckin() { |
| checkin_request_.reset( |
| new CheckinRequest(base::Bind(&GCMClientImpl::OnCheckinCompleted, |
| weak_ptr_factory_.GetWeakPtr()), |
| kDefaultBackoffPolicy, |
| chrome_build_proto_, |
| - checkin_info.android_id, |
| - checkin_info.secret, |
| + device_checkin_info_.android_id, |
| + device_checkin_info_.secret, |
| account_ids_, |
| url_request_context_getter_)); |
| checkin_request_->Start(); |
| @@ -312,15 +315,40 @@ void GCMClientImpl::OnCheckinCompleted(uint64 android_id, |
| OnFirstTimeDeviceCheckinCompleted(checkin_info); |
| } else { |
| DCHECK_EQ(READY, state_); |
| - if (device_checkin_info_.android_id != checkin_info.android_id || |
| - device_checkin_info_.secret != checkin_info.secret) { |
| - ResetState(); |
| - } else { |
| - // TODO(fgorski): Reset the checkin timer. |
| - } |
| + DCHECK_EQ(device_checkin_info_.android_id, checkin_info.android_id); |
|
jianli
2014/04/02 23:02:51
Please comment on the fact that the checkin info i
|
| + DCHECK_EQ(device_checkin_info_.secret, checkin_info.secret); |
| + } |
| + |
| + if (device_checkin_info_.IsValid()) { |
| + base::Time last_checkin_time = clock_->Now(); |
| + gcm_store_->SetLastCheckinTime( |
| + last_checkin_time, |
| + base::Bind(&GCMClientImpl::SetLastCheckinTimeCallback, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + SchedulePeriodicCheckin(last_checkin_time); |
| } |
| } |
| +void GCMClientImpl::SchedulePeriodicCheckin( |
| + const base::Time& last_checkin_time) { |
| + base::TimeDelta time_to_next_checkin = last_checkin_time + |
| + base::TimeDelta::FromSeconds(kDefaultCheckinInterval) - clock_->Now(); |
| + if (time_to_next_checkin < base::TimeDelta::FromSeconds(0L)) |
| + time_to_next_checkin = base::TimeDelta::FromSeconds(0L); |
| + // TODO(fgorski): Make sure that once dynamic events (like accounts list |
| + // change) trigger checkin we reset the timer. |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&GCMClientImpl::StartCheckin, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + time_to_next_checkin); |
| +} |
| + |
| +void GCMClientImpl::SetLastCheckinTimeCallback(bool success) { |
| + // TODO(fgorski): This is one of the signals that store needs a rebuild. |
| + DCHECK(success); |
| +} |
| + |
| void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { |
| // TODO(fgorski): This is one of the signals that store needs a rebuild. |
| DCHECK(success); |