| 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" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.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/time/default_clock.h" | 15 #include "base/time/default_clock.h" |
| 15 #include "google_apis/gcm/base/mcs_message.h" | 16 #include "google_apis/gcm/base/mcs_message.h" |
| 16 #include "google_apis/gcm/base/mcs_util.h" | 17 #include "google_apis/gcm/base/mcs_util.h" |
| 17 #include "google_apis/gcm/engine/checkin_request.h" | 18 #include "google_apis/gcm/engine/checkin_request.h" |
| 18 #include "google_apis/gcm/engine/connection_factory_impl.h" | 19 #include "google_apis/gcm/engine/connection_factory_impl.h" |
| 19 #include "google_apis/gcm/engine/gcm_store_impl.h" | 20 #include "google_apis/gcm/engine/gcm_store_impl.h" |
| 20 #include "google_apis/gcm/engine/mcs_client.h" | 21 #include "google_apis/gcm/engine/mcs_client.h" |
| 21 #include "google_apis/gcm/protocol/mcs.pb.h" | 22 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 22 #include "net/http/http_network_session.h" | 23 #include "net/http/http_network_session.h" |
| 23 #include "net/url_request/url_request_context.h" | 24 #include "net/url_request/url_request_context.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 SEND_ERROR, // Error sending a message. | 68 SEND_ERROR, // Error sending a message. |
| 68 }; | 69 }; |
| 69 | 70 |
| 70 // MCS endpoints. SSL Key pinning is done automatically due to the *.google.com | 71 // MCS endpoints. SSL Key pinning is done automatically due to the *.google.com |
| 71 // pinning rule. | 72 // pinning rule. |
| 72 // Note: modifying the endpoints will affect the ability to compare the | 73 // Note: modifying the endpoints will affect the ability to compare the |
| 73 // GCM.CurrentEnpoint histogram across versions. | 74 // GCM.CurrentEnpoint histogram across versions. |
| 74 const char kMCSEndpointMain[] = "https://mtalk.google.com:5228"; | 75 const char kMCSEndpointMain[] = "https://mtalk.google.com:5228"; |
| 75 const char kMCSEndpointFallback[] = "https://mtalk.google.com:443"; | 76 const char kMCSEndpointFallback[] = "https://mtalk.google.com:443"; |
| 76 | 77 |
| 78 const char kCheckinIntervalKey[] = "checkin_interval"; |
| 79 const int64 kDefaultCheckinInterval = 172800LL; // seconds = 2 days. |
| 77 const int kMaxRegistrationRetries = 5; | 80 const int kMaxRegistrationRetries = 5; |
| 78 const char kMessageTypeDataMessage[] = "gcm"; | 81 const char kMessageTypeDataMessage[] = "gcm"; |
| 79 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; | 82 const char kMessageTypeDeletedMessagesKey[] = "deleted_messages"; |
| 80 const char kMessageTypeKey[] = "message_type"; | 83 const char kMessageTypeKey[] = "message_type"; |
| 81 const char kMessageTypeSendErrorKey[] = "send_error"; | 84 const char kMessageTypeSendErrorKey[] = "send_error"; |
| 82 const char kSendErrorMessageIdKey[] = "google.message_id"; | 85 const char kSendErrorMessageIdKey[] = "google.message_id"; |
| 83 const char kSendMessageFromValue[] = "gcm@chrome.com"; | 86 const char kSendMessageFromValue[] = "gcm@chrome.com"; |
| 84 const int64 kDefaultUserSerialNumber = 0LL; | 87 const int64 kDefaultUserSerialNumber = 0LL; |
| 85 | 88 |
| 86 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) { | 89 GCMClient::Result ToGCMClientResult(MCSClient::MessageSendStatus status) { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 208 |
| 206 if (!result->success) { | 209 if (!result->success) { |
| 207 ResetState(); | 210 ResetState(); |
| 208 return; | 211 return; |
| 209 } | 212 } |
| 210 | 213 |
| 211 registrations_ = result->registrations; | 214 registrations_ = result->registrations; |
| 212 | 215 |
| 213 device_checkin_info_.android_id = result->device_android_id; | 216 device_checkin_info_.android_id = result->device_android_id; |
| 214 device_checkin_info_.secret = result->device_security_token; | 217 device_checkin_info_.secret = result->device_security_token; |
| 218 gservices_settings_ = result->gservices_settings; |
| 219 gservices_digest_ = result->gservices_digest; |
| 215 InitializeMCSClient(result.Pass()); | 220 InitializeMCSClient(result.Pass()); |
| 216 if (!device_checkin_info_.IsValid()) { | 221 if (!device_checkin_info_.IsValid()) { |
| 217 device_checkin_info_.Reset(); | 222 device_checkin_info_.Reset(); |
| 218 state_ = INITIAL_DEVICE_CHECKIN; | 223 state_ = INITIAL_DEVICE_CHECKIN; |
| 219 StartCheckin(device_checkin_info_); | 224 StartCheckin(); |
| 220 return; | 225 return; |
| 226 } else { |
| 227 SchedulePeriodicCheckin(result->last_checkin_time); |
| 221 } | 228 } |
| 222 | 229 |
| 223 OnReady(); | 230 OnReady(); |
| 224 } | 231 } |
| 225 | 232 |
| 226 void GCMClientImpl::InitializeMCSClient( | 233 void GCMClientImpl::InitializeMCSClient( |
| 227 scoped_ptr<GCMStore::LoadResult> result) { | 234 scoped_ptr<GCMStore::LoadResult> result) { |
| 228 std::vector<GURL> endpoints; | 235 std::vector<GURL> endpoints; |
| 229 endpoints.push_back(GURL(kMCSEndpointMain)); | 236 endpoints.push_back(GURL(kMCSEndpointMain)); |
| 230 endpoints.push_back(GURL(kMCSEndpointFallback)); | 237 endpoints.push_back(GURL(kMCSEndpointFallback)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 DCHECK(device_checkin_info_.IsValid()); | 281 DCHECK(device_checkin_info_.IsValid()); |
| 275 mcs_client_->Login(device_checkin_info_.android_id, | 282 mcs_client_->Login(device_checkin_info_.android_id, |
| 276 device_checkin_info_.secret); | 283 device_checkin_info_.secret); |
| 277 } | 284 } |
| 278 | 285 |
| 279 void GCMClientImpl::ResetState() { | 286 void GCMClientImpl::ResetState() { |
| 280 state_ = UNINITIALIZED; | 287 state_ = UNINITIALIZED; |
| 281 // TODO(fgorski): reset all of the necessart objects and start over. | 288 // TODO(fgorski): reset all of the necessart objects and start over. |
| 282 } | 289 } |
| 283 | 290 |
| 284 void GCMClientImpl::StartCheckin(const CheckinInfo& checkin_info) { | 291 void GCMClientImpl::StartCheckin() { |
| 292 CheckinRequest::RequestInfo request_info( |
| 293 device_checkin_info_.android_id, |
| 294 device_checkin_info_.secret, |
| 295 gservices_digest_, |
| 296 account_ids_, |
| 297 chrome_build_proto_); |
| 285 checkin_request_.reset( | 298 checkin_request_.reset( |
| 286 new CheckinRequest(base::Bind(&GCMClientImpl::OnCheckinCompleted, | 299 new CheckinRequest(request_info, |
| 300 kDefaultBackoffPolicy, |
| 301 base::Bind(&GCMClientImpl::OnCheckinCompleted, |
| 287 weak_ptr_factory_.GetWeakPtr()), | 302 weak_ptr_factory_.GetWeakPtr()), |
| 288 kDefaultBackoffPolicy, | |
| 289 chrome_build_proto_, | |
| 290 checkin_info.android_id, | |
| 291 checkin_info.secret, | |
| 292 account_ids_, | |
| 293 url_request_context_getter_)); | 303 url_request_context_getter_)); |
| 294 checkin_request_->Start(); | 304 checkin_request_->Start(); |
| 295 } | 305 } |
| 296 | 306 |
| 297 void GCMClientImpl::OnCheckinCompleted(uint64 android_id, | 307 void GCMClientImpl::SchedulePeriodicCheckin( |
| 298 uint64 security_token) { | 308 const base::Time& last_checkin_time) { |
| 309 base::TimeDelta time_to_next_checkin = |
| 310 last_checkin_time + GetCheckinInterval() - clock_->Now(); |
| 311 if (time_to_next_checkin < base::TimeDelta::FromSeconds(0LL)) |
| 312 time_to_next_checkin = base::TimeDelta::FromSeconds(0LL); |
| 313 // TODO(fgorski): Make sure that once dynamic events (like accounts list |
| 314 // change) trigger checkin we reset the timer. |
| 315 base::MessageLoop::current()->PostDelayedTask( |
| 316 FROM_HERE, |
| 317 base::Bind(&GCMClientImpl::StartCheckin, |
| 318 weak_ptr_factory_.GetWeakPtr()), |
| 319 time_to_next_checkin); |
| 320 } |
| 321 |
| 322 base::TimeDelta GCMClientImpl::GetCheckinInterval() const { |
| 323 GServicesSettingsMap::const_iterator iter = |
| 324 gservices_settings_.find(kCheckinIntervalKey); |
| 325 int64 checkin_interval_sec = kDefaultCheckinInterval; |
| 326 if (iter != gservices_settings_.end()) |
| 327 base::StringToInt64(iter->second, &checkin_interval_sec); |
| 328 return base::TimeDelta::FromSeconds(checkin_interval_sec); |
| 329 } |
| 330 |
| 331 void GCMClientImpl::OnCheckinCompleted( |
| 332 const checkin_proto::AndroidCheckinResponse& checkin_response) { |
| 299 checkin_request_.reset(); | 333 checkin_request_.reset(); |
| 300 | 334 |
| 301 CheckinInfo checkin_info; | 335 if (!checkin_response.has_android_id() || |
| 302 checkin_info.android_id = android_id; | 336 !checkin_response.has_security_token()) { |
| 303 checkin_info.secret = security_token; | 337 // TODO(fgorski): I don't think a retry here will help, we should probably |
| 304 | |
| 305 if (!checkin_info.IsValid()) { | |
| 306 // TODO(fgorski): I don't think a retry here will help, we should probalby | |
| 307 // start over. By checking in with (0, 0). | 338 // start over. By checking in with (0, 0). |
| 308 return; | 339 return; |
| 309 } | 340 } |
| 310 | 341 |
| 342 CheckinInfo checkin_info; |
| 343 checkin_info.android_id = checkin_response.android_id(); |
| 344 checkin_info.secret = checkin_response.security_token(); |
| 345 |
| 311 if (state_ == INITIAL_DEVICE_CHECKIN) { | 346 if (state_ == INITIAL_DEVICE_CHECKIN) { |
| 312 OnFirstTimeDeviceCheckinCompleted(checkin_info); | 347 OnFirstTimeDeviceCheckinCompleted(checkin_info); |
| 313 } else { | 348 } else { |
| 314 DCHECK_EQ(READY, state_); | 349 DCHECK_EQ(READY, state_); |
| 315 if (device_checkin_info_.android_id != checkin_info.android_id || | 350 if (device_checkin_info_.android_id != checkin_info.android_id || |
| 316 device_checkin_info_.secret != checkin_info.secret) { | 351 device_checkin_info_.secret != checkin_info.secret) { |
| 317 ResetState(); | 352 ResetState(); |
| 318 } else { | |
| 319 // TODO(fgorski): Reset the checkin timer. | |
| 320 } | 353 } |
| 321 } | 354 } |
| 355 |
| 356 if (device_checkin_info_.IsValid()) { |
| 357 UpdateGServicesSettings(checkin_response); |
| 358 SchedulePeriodicCheckin(clock_->Now()); |
| 359 } |
| 322 } | 360 } |
| 323 | 361 |
| 324 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { | 362 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { |
| 325 // TODO(fgorski): This is one of the signals that store needs a rebuild. | 363 // TODO(fgorski): This is one of the signals that store needs a rebuild. |
| 326 DCHECK(success); | 364 DCHECK(success); |
| 327 } | 365 } |
| 328 | 366 |
| 329 void GCMClientImpl::UpdateRegistrationCallback(bool success) { | 367 void GCMClientImpl::UpdateRegistrationCallback(bool success) { |
| 330 // TODO(fgorski): This is one of the signals that store needs a rebuild. | 368 // TODO(fgorski): This is one of the signals that store needs a rebuild. |
| 331 DCHECK(success); | 369 DCHECK(success); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 send_error_details.additional_data.find(kSendErrorMessageIdKey); | 695 send_error_details.additional_data.find(kSendErrorMessageIdKey); |
| 658 if (iter != send_error_details.additional_data.end()) { | 696 if (iter != send_error_details.additional_data.end()) { |
| 659 send_error_details.message_id = iter->second; | 697 send_error_details.message_id = iter->second; |
| 660 send_error_details.additional_data.erase(iter); | 698 send_error_details.additional_data.erase(iter); |
| 661 } | 699 } |
| 662 | 700 |
| 663 delegate_->OnMessageSendError(data_message_stanza.category(), | 701 delegate_->OnMessageSendError(data_message_stanza.category(), |
| 664 send_error_details); | 702 send_error_details); |
| 665 } | 703 } |
| 666 | 704 |
| 705 void GCMClientImpl::UpdateGServicesSettings( |
| 706 const checkin_proto::AndroidCheckinResponse& checkin_response) { |
| 707 if (!checkin_response.has_digest() || |
| 708 checkin_response.digest() == gservices_digest_) { |
| 709 return; |
| 710 } |
| 711 |
| 712 gservices_digest_ = checkin_response.digest(); |
| 713 gservices_settings_.clear(); |
| 714 |
| 715 for (int i = 0; i < checkin_response.setting_size(); ++i) { |
| 716 std::string name = checkin_response.setting(i).name(); |
| 717 std::string value = checkin_response.setting(i).value(); |
| 718 gservices_settings_[name] = value; |
| 719 } |
| 720 |
| 721 gcm_store_->SetGServicesSettings( |
| 722 gservices_settings_, |
| 723 gservices_digest_, |
| 724 clock_->Now(), |
| 725 base::Bind(&GCMClientImpl::UpdateGServicesSettingsCallback, |
| 726 weak_ptr_factory_.GetWeakPtr())); |
| 727 } |
| 728 |
| 729 void GCMClientImpl::UpdateGServicesSettingsCallback(bool success) { |
| 730 DCHECK(success); |
| 731 } |
| 732 |
| 667 } // namespace gcm | 733 } // namespace gcm |
| OLD | NEW |