Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "google_apis/gcm/gcm_client_impl.h" | 5 #include "google_apis/gcm/gcm_client_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 SEND_ERROR, // Error sending a message. | 67 SEND_ERROR, // Error sending a message. |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 // MCS endpoints. SSL Key pinning is done automatically due to the *.google.com | 70 // MCS endpoints. SSL Key pinning is done automatically due to the *.google.com |
| 71 // pinning rule. | 71 // pinning rule. |
| 72 // Note: modifying the endpoints will affect the ability to compare the | 72 // Note: modifying the endpoints will affect the ability to compare the |
| 73 // GCM.CurrentEnpoint histogram across versions. | 73 // GCM.CurrentEnpoint histogram across versions. |
| 74 const char kMCSEndpointMain[] = "https://mtalk.google.com:5228"; | 74 const char kMCSEndpointMain[] = "https://mtalk.google.com:5228"; |
| 75 const char kMCSEndpointFallback[] = "https://mtalk.google.com:443"; | 75 const char kMCSEndpointFallback[] = "https://mtalk.google.com:443"; |
| 76 | 76 |
| 77 const int64 kDefaultCheckinInterval = 172800LL; // seconds - 2 days. | |
|
Nicolas Zea
2014/04/02 21:53:55
nit: prefer 60 * 60 * 24 * 2 for readability
fgorski
2014/04/02 22:37:21
Done.
| |
| 77 const int kMaxRegistrationRetries = 5; | 78 const int kMaxRegistrationRetries = 5; |
| 78 const char kMessageTypeDataMessage[] = "gcm"; | 79 const char kMessageTypeDataMessage[] = "gcm"; |
| 79 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; | 80 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; |
| 80 const char kMessageTypeKey[] = "message_type"; | 81 const char kMessageTypeKey[] = "message_type"; |
| 81 const char kMessageTypeSendErrorKey[] = "send_error"; | 82 const char kMessageTypeSendErrorKey[] = "send_error"; |
| 82 const char kSendErrorMessageIdKey[] = "google.message_id"; | 83 const char kSendErrorMessageIdKey[] = "google.message_id"; |
| 83 const char kSendMessageFromValue[] = "gcm@chrome.com"; | 84 const char kSendMessageFromValue[] = "gcm@chrome.com"; |
| 84 const int64 kDefaultUserSerialNumber = 0LL; | 85 const int64 kDefaultUserSerialNumber = 0LL; |
| 85 | 86 |
| 86 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) { | 87 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 } | 210 } |
| 210 | 211 |
| 211 registrations_ = result->registrations; | 212 registrations_ = result->registrations; |
| 212 | 213 |
| 213 device_checkin_info_.android_id = result->device_android_id; | 214 device_checkin_info_.android_id = result->device_android_id; |
| 214 device_checkin_info_.secret = result->device_security_token; | 215 device_checkin_info_.secret = result->device_security_token; |
| 215 InitializeMCSClient(result.Pass()); | 216 InitializeMCSClient(result.Pass()); |
| 216 if (!device_checkin_info_.IsValid()) { | 217 if (!device_checkin_info_.IsValid()) { |
| 217 device_checkin_info_.Reset(); | 218 device_checkin_info_.Reset(); |
| 218 state_ = INITIAL_DEVICE_CHECKIN; | 219 state_ = INITIAL_DEVICE_CHECKIN; |
| 219 StartCheckin(device_checkin_info_); | 220 StartCheckin(); |
| 220 return; | 221 return; |
| 222 } else { | |
|
jianli
2014/04/02 22:00:18
nit: revert these two parts
fgorski
2014/04/02 22:37:21
Done.
| |
| 223 SchedulePeriodicCheckin(result->last_checkin_time); | |
| 221 } | 224 } |
| 222 | 225 |
| 223 OnReady(); | 226 OnReady(); |
| 224 } | 227 } |
| 225 | 228 |
| 226 void GCMClientImpl::InitializeMCSClient( | 229 void GCMClientImpl::InitializeMCSClient( |
| 227 scoped_ptr<GCMStore::LoadResult> result) { | 230 scoped_ptr<GCMStore::LoadResult> result) { |
| 228 std::vector<GURL> endpoints; | 231 std::vector<GURL> endpoints; |
| 229 endpoints.push_back(GURL(kMCSEndpointMain)); | 232 endpoints.push_back(GURL(kMCSEndpointMain)); |
| 230 endpoints.push_back(GURL(kMCSEndpointFallback)); | 233 endpoints.push_back(GURL(kMCSEndpointFallback)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 DCHECK(device_checkin_info_.IsValid()); | 277 DCHECK(device_checkin_info_.IsValid()); |
| 275 mcs_client_->Login(device_checkin_info_.android_id, | 278 mcs_client_->Login(device_checkin_info_.android_id, |
| 276 device_checkin_info_.secret); | 279 device_checkin_info_.secret); |
| 277 } | 280 } |
| 278 | 281 |
| 279 void GCMClientImpl::ResetState() { | 282 void GCMClientImpl::ResetState() { |
| 280 state_ = UNINITIALIZED; | 283 state_ = UNINITIALIZED; |
| 281 // TODO(fgorski): reset all of the necessart objects and start over. | 284 // TODO(fgorski): reset all of the necessart objects and start over. |
| 282 } | 285 } |
| 283 | 286 |
| 284 void GCMClientImpl::StartCheckin(const CheckinInfo& checkin_info) { | 287 void GCMClientImpl::StartCheckin() { |
| 285 checkin_request_.reset( | 288 checkin_request_.reset( |
| 286 new CheckinRequest(base::Bind(&GCMClientImpl::OnCheckinCompleted, | 289 new CheckinRequest(base::Bind(&GCMClientImpl::OnCheckinCompleted, |
| 287 weak_ptr_factory_.GetWeakPtr()), | 290 weak_ptr_factory_.GetWeakPtr()), |
| 288 kDefaultBackoffPolicy, | 291 kDefaultBackoffPolicy, |
| 289 chrome_build_proto_, | 292 chrome_build_proto_, |
| 290 checkin_info.android_id, | 293 device_checkin_info_.android_id, |
| 291 checkin_info.secret, | 294 device_checkin_info_.secret, |
| 292 account_ids_, | 295 account_ids_, |
| 293 url_request_context_getter_)); | 296 url_request_context_getter_)); |
| 294 checkin_request_->Start(); | 297 checkin_request_->Start(); |
| 295 } | 298 } |
| 296 | 299 |
| 297 void GCMClientImpl::OnCheckinCompleted(uint64 android_id, | 300 void GCMClientImpl::OnCheckinCompleted(uint64 android_id, |
| 298 uint64 security_token) { | 301 uint64 security_token) { |
| 299 checkin_request_.reset(); | 302 checkin_request_.reset(); |
| 300 | 303 |
| 301 CheckinInfo checkin_info; | 304 CheckinInfo checkin_info; |
| 302 checkin_info.android_id = android_id; | 305 checkin_info.android_id = android_id; |
| 303 checkin_info.secret = security_token; | 306 checkin_info.secret = security_token; |
| 304 | 307 |
| 305 if (!checkin_info.IsValid()) { | 308 if (!checkin_info.IsValid()) { |
| 306 // TODO(fgorski): I don't think a retry here will help, we should probalby | 309 // TODO(fgorski): I don't think a retry here will help, we should probalby |
| 307 // start over. By checking in with (0, 0). | 310 // start over. By checking in with (0, 0). |
| 308 return; | 311 return; |
| 309 } | 312 } |
| 310 | 313 |
| 311 if (state_ == INITIAL_DEVICE_CHECKIN) { | 314 if (state_ == INITIAL_DEVICE_CHECKIN) { |
| 312 OnFirstTimeDeviceCheckinCompleted(checkin_info); | 315 OnFirstTimeDeviceCheckinCompleted(checkin_info); |
| 313 } else { | 316 } else { |
| 314 DCHECK_EQ(READY, state_); | 317 DCHECK_EQ(READY, state_); |
| 315 if (device_checkin_info_.android_id != checkin_info.android_id || | 318 if (device_checkin_info_.android_id != checkin_info.android_id || |
| 316 device_checkin_info_.secret != checkin_info.secret) { | 319 device_checkin_info_.secret != checkin_info.secret) { |
| 317 ResetState(); | 320 ResetState(); |
|
jianli
2014/04/02 22:00:18
When the updated checkin info is different, we cal
fgorski
2014/04/02 22:37:21
Done.
| |
| 318 } else { | |
| 319 // TODO(fgorski): Reset the checkin timer. | |
| 320 } | 321 } |
| 321 } | 322 } |
| 323 | |
| 324 if (device_checkin_info_.IsValid()) { | |
| 325 base::Time last_checkin_time = clock_->Now(); | |
| 326 gcm_store_->SetLastCheckinTime( | |
| 327 last_checkin_time, | |
| 328 base::Bind(&GCMClientImpl::SetLastCheckinTimeCallback, | |
| 329 weak_ptr_factory_.GetWeakPtr())); | |
| 330 SchedulePeriodicCheckin(last_checkin_time); | |
| 331 } | |
| 322 } | 332 } |
| 323 | 333 |
| 334 void GCMClientImpl::SchedulePeriodicCheckin( | |
| 335 const base::Time& last_checkin_time) { | |
| 336 base::TimeDelta time_to_next_checkin = last_checkin_time | |
| 337 + base::TimeDelta::FromSeconds(kDefaultCheckinInterval) | |
|
jianli
2014/04/02 22:00:18
nit: + and - should be placed at the end of previo
fgorski
2014/04/02 22:37:21
Done.
| |
| 338 - clock_->Now(); | |
| 339 if (time_to_next_checkin < base::TimeDelta::FromSeconds(0L)) | |
| 340 time_to_next_checkin = base::TimeDelta::FromSeconds(0L); | |
| 341 // TODO(fgorski): Make sure that once dynamic events (like accounts list | |
| 342 // change) trigger checkin we reset the timer. | |
| 343 base::MessageLoop::current()->PostDelayedTask( | |
| 344 FROM_HERE, | |
| 345 base::Bind(&GCMClientImpl::StartCheckin, | |
| 346 weak_ptr_factory_.GetWeakPtr()), | |
| 347 time_to_next_checkin); | |
| 348 } | |
| 349 | |
| 350 void GCMClientImpl::SetLastCheckinTimeCallback(bool success) { | |
| 351 // TODO(fgorski): This is one of the signals that store needs a rebuild. | |
| 352 DCHECK(success); | |
| 353 } | |
| 354 | |
| 324 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { | 355 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { |
| 325 // TODO(fgorski): This is one of the signals that store needs a rebuild. | 356 // TODO(fgorski): This is one of the signals that store needs a rebuild. |
| 326 DCHECK(success); | 357 DCHECK(success); |
| 327 } | 358 } |
| 328 | 359 |
| 329 void GCMClientImpl::UpdateRegistrationCallback(bool success) { | 360 void GCMClientImpl::UpdateRegistrationCallback(bool success) { |
| 330 // TODO(fgorski): This is one of the signals that store needs a rebuild. | 361 // TODO(fgorski): This is one of the signals that store needs a rebuild. |
| 331 DCHECK(success); | 362 DCHECK(success); |
| 332 } | 363 } |
| 333 | 364 |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 658 if (iter != send_error_details.additional_data.end()) { | 689 if (iter != send_error_details.additional_data.end()) { |
| 659 send_error_details.message_id = iter->second; | 690 send_error_details.message_id = iter->second; |
| 660 send_error_details.additional_data.erase(iter); | 691 send_error_details.additional_data.erase(iter); |
| 661 } | 692 } |
| 662 | 693 |
| 663 delegate_->OnMessageSendError(data_message_stanza.category(), | 694 delegate_->OnMessageSendError(data_message_stanza.category(), |
| 664 send_error_details); | 695 send_error_details); |
| 665 } | 696 } |
| 666 | 697 |
| 667 } // namespace gcm | 698 } // namespace gcm |
| OLD | NEW |