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..8d3df6acb49f9358b715324854a45ae1337412f1 100644 |
| --- a/google_apis/gcm/gcm_client_impl.cc |
| +++ b/google_apis/gcm/gcm_client_impl.cc |
| @@ -11,6 +11,7 @@ |
| #include "base/message_loop/message_loop.h" |
| #include "base/metrics/histogram.h" |
| #include "base/sequenced_task_runner.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "base/time/default_clock.h" |
| #include "google_apis/gcm/base/mcs_message.h" |
| #include "google_apis/gcm/base/mcs_util.h" |
| @@ -74,6 +75,7 @@ enum MessageType { |
| const char kMCSEndpointMain[] = "https://mtalk.google.com:5228"; |
| const char kMCSEndpointFallback[] = "https://mtalk.google.com:443"; |
| +const char kCheckinIntervalKey[] = "checkin_interval"; |
| const int kMaxRegistrationRetries = 5; |
| const char kMessageTypeDataMessage[] = "gcm"; |
| const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; |
| @@ -212,12 +214,14 @@ void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { |
| device_checkin_info_.android_id = result->device_android_id; |
| device_checkin_info_.secret = result->device_security_token; |
| + g_services_settings_ = result->g_services_settings; |
| InitializeMCSClient(result.Pass()); |
| if (!device_checkin_info_.IsValid()) { |
| device_checkin_info_.Reset(); |
| state_ = INITIAL_DEVICE_CHECKIN; |
| StartCheckin(device_checkin_info_); |
| return; |
| + } else { |
|
fgorski
2014/03/28 14:30:51
remove
fgorski
2014/03/29 01:09:32
Done.
|
| } |
| OnReady(); |
| @@ -294,20 +298,46 @@ void GCMClientImpl::StartCheckin(const CheckinInfo& checkin_info) { |
| checkin_request_->Start(); |
| } |
| -void GCMClientImpl::OnCheckinCompleted(uint64 android_id, |
| - uint64 security_token) { |
| +void GCMClientImpl::ScheduleNextCheckin( |
|
fgorski
2014/03/28 14:30:51
Consider renaming to SchedulePeriodicCheckin
fgorski
2014/03/29 01:09:32
Done.
|
| + const base::Time& last_checkin_time, |
| + const CheckinInfo& checkin_info) { |
| + GServicesSettingsMap::const_iterator iter = |
| + g_services_settings_.find(kCheckinIntervalKey); |
| + // If we don't have the setting or string parsing fails, 0 is a reasonable |
|
fgorski
2014/03/28 14:30:51
consider 2 days as should be the current default f
fgorski
2014/03/29 01:09:32
Done.
|
| + // value. |
| + int64 checkin_interval_sec = 0LL; |
| + if (iter != g_services_settings_.end()) |
| + base::StringToInt64(iter->second, &checkin_interval_sec); |
| + base::TimeDelta checkin_interval = |
| + base::TimeDelta::FromSeconds(checkin_interval_sec); |
| + base::TimeDelta time_to_next_checkin = |
| + last_checkin_time + checkin_interval - base::Time::Now(); |
| + base::MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&GCMClientImpl::StartCheckin, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + checkin_info), |
| + time_to_next_checkin); |
| +} |
| + |
| +void GCMClientImpl::OnCheckinCompleted( |
| + const checkin_proto::AndroidCheckinResponse& checkin_response) { |
| checkin_request_.reset(); |
| - CheckinInfo checkin_info; |
| - checkin_info.android_id = android_id; |
| - checkin_info.secret = security_token; |
| - |
| - if (!checkin_info.IsValid()) { |
| + if (!checkin_response.has_android_id() || |
| + !checkin_response.has_security_token()) { |
| // TODO(fgorski): I don't think a retry here will help, we should probalby |
| // start over. By checking in with (0, 0). |
| return; |
| } |
| + CheckinInfo checkin_info; |
| + checkin_info.android_id = checkin_response.android_id(); |
| + checkin_info.secret = checkin_response.security_token(); |
| + |
| + // Process the gservices to get the checkin_interval here. |
| + UpdateGServicesSettings(checkin_response); |
| + |
| if (state_ == INITIAL_DEVICE_CHECKIN) { |
| OnFirstTimeDeviceCheckinCompleted(checkin_info); |
| } else { |
| @@ -315,10 +345,11 @@ void GCMClientImpl::OnCheckinCompleted(uint64 android_id, |
| 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. |
| } |
| } |
| + |
| + if (device_checkin_info_.IsValid()) |
| + ScheduleNextCheckin(base::Time::Now(), device_checkin_info_); |
| } |
| void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { |
| @@ -664,4 +695,46 @@ void GCMClientImpl::HandleIncomingSendError( |
| send_error_details); |
| } |
| +void GCMClientImpl::UpdateGServicesSettings( |
| + const checkin_proto::AndroidCheckinResponse& checkin_response) { |
| + std::vector<std::string> settings_to_remove; |
| + std::map<std::string, std::string> settings_to_add; |
| + |
| + // Check if we are only dealing with a diff of settings or full update. |
| + if (checkin_response.has_settings_diff() && |
| + !checkin_response.settings_diff()) { |
| + for (GServicesSettingsMap::const_iterator it = g_services_settings_.begin(); |
| + it != g_services_settings_.end(); ++it) { |
| + settings_to_remove.push_back(it->first); |
| + } |
| + g_services_settings_.clear(); |
| + } |
| + |
| + for (int i = 0; i < checkin_response.setting_size(); ++i) { |
| + std::string name = checkin_response.setting(i).name(); |
|
fgorski
2014/03/28 14:30:51
add a comment explaining what the if statement doe
fgorski
2014/03/29 01:09:32
Done.
|
| + if (name.find("delete_") == 0) { |
|
fgorski
2014/03/28 14:30:51
use const
fgorski
2014/03/29 01:09:32
Done.
|
| + name = name.substr(7); |
|
fgorski
2014/03/28 14:30:51
use arraysize with the const above.
fgorski
2014/03/29 01:09:32
Done.
|
| + GServicesSettingsMap::iterator iter = g_services_settings_.find(name); |
| + if (iter != g_services_settings_.end()) { |
| + settings_to_remove.push_back(name); |
| + g_services_settings_.erase(iter); |
| + } |
| + } else { |
| + std::string value = checkin_response.setting(i).value(); |
| + g_services_settings_[name] = value; |
| + settings_to_add[name] = value; |
| + } |
| + } |
| + |
| + gcm_store_->UpdateGServicesSettings( |
|
fgorski
2014/03/28 14:30:51
Pass the current time based on the clock_
clock_-
fgorski
2014/03/29 01:09:32
Done.
|
| + settings_to_remove, |
| + settings_to_add, |
| + base::Bind(&GCMClientImpl::UpdateGServicesSettingsCallback, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void GCMClientImpl::UpdateGServicesSettingsCallback(bool success) { |
| + DCHECK(success); |
| +} |
| + |
| } // namespace gcm |