| 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" | |
| 15 #include "base/time/default_clock.h" | 14 #include "base/time/default_clock.h" |
| 16 #include "google_apis/gcm/base/mcs_message.h" | 15 #include "google_apis/gcm/base/mcs_message.h" |
| 17 #include "google_apis/gcm/base/mcs_util.h" | 16 #include "google_apis/gcm/base/mcs_util.h" |
| 18 #include "google_apis/gcm/engine/checkin_request.h" | 17 #include "google_apis/gcm/engine/checkin_request.h" |
| 19 #include "google_apis/gcm/engine/connection_factory_impl.h" | 18 #include "google_apis/gcm/engine/connection_factory_impl.h" |
| 20 #include "google_apis/gcm/engine/gcm_store_impl.h" | 19 #include "google_apis/gcm/engine/gcm_store_impl.h" |
| 21 #include "google_apis/gcm/engine/mcs_client.h" | 20 #include "google_apis/gcm/engine/mcs_client.h" |
| 22 #include "google_apis/gcm/protocol/mcs.pb.h" | 21 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 23 #include "net/http/http_network_session.h" | 22 #include "net/http/http_network_session.h" |
| 24 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 206 |
| 208 if (!result->success) { | 207 if (!result->success) { |
| 209 ResetState(); | 208 ResetState(); |
| 210 return; | 209 return; |
| 211 } | 210 } |
| 212 | 211 |
| 213 registrations_ = result->registrations; | 212 registrations_ = result->registrations; |
| 214 device_checkin_info_.android_id = result->device_android_id; | 213 device_checkin_info_.android_id = result->device_android_id; |
| 215 device_checkin_info_.secret = result->device_security_token; | 214 device_checkin_info_.secret = result->device_security_token; |
| 216 base::Time last_checkin_time = result->last_checkin_time; | 215 base::Time last_checkin_time = result->last_checkin_time; |
| 217 gservices_settings_ = result->gservices_settings; | |
| 218 gservices_digest_ = result->gservices_digest; | |
| 219 InitializeMCSClient(result.Pass()); | 216 InitializeMCSClient(result.Pass()); |
| 220 | 217 |
| 221 if (device_checkin_info_.IsValid()) { | 218 if (device_checkin_info_.IsValid()) { |
| 222 SchedulePeriodicCheckin(last_checkin_time); | 219 SchedulePeriodicCheckin(last_checkin_time); |
| 223 OnReady(); | 220 OnReady(); |
| 224 return; | 221 return; |
| 225 } | 222 } |
| 226 | 223 |
| 227 state_ = INITIAL_DEVICE_CHECKIN; | 224 state_ = INITIAL_DEVICE_CHECKIN; |
| 228 device_checkin_info_.Reset(); | 225 device_checkin_info_.Reset(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 mcs_client_->Login(device_checkin_info_.android_id, | 278 mcs_client_->Login(device_checkin_info_.android_id, |
| 282 device_checkin_info_.secret); | 279 device_checkin_info_.secret); |
| 283 } | 280 } |
| 284 | 281 |
| 285 void GCMClientImpl::ResetState() { | 282 void GCMClientImpl::ResetState() { |
| 286 state_ = UNINITIALIZED; | 283 state_ = UNINITIALIZED; |
| 287 // TODO(fgorski): reset all of the necessart objects and start over. | 284 // TODO(fgorski): reset all of the necessart objects and start over. |
| 288 } | 285 } |
| 289 | 286 |
| 290 void GCMClientImpl::StartCheckin() { | 287 void GCMClientImpl::StartCheckin() { |
| 291 CheckinRequest::RequestInfo request_info( | |
| 292 device_checkin_info_.android_id, | |
| 293 device_checkin_info_.secret, | |
| 294 gservices_digest_, | |
| 295 account_ids_, | |
| 296 chrome_build_proto_); | |
| 297 checkin_request_.reset( | 288 checkin_request_.reset( |
| 298 new CheckinRequest(request_info, | 289 new CheckinRequest(base::Bind(&GCMClientImpl::OnCheckinCompleted, |
| 290 weak_ptr_factory_.GetWeakPtr()), |
| 299 kDefaultBackoffPolicy, | 291 kDefaultBackoffPolicy, |
| 300 base::Bind(&GCMClientImpl::OnCheckinCompleted, | 292 chrome_build_proto_, |
| 301 weak_ptr_factory_.GetWeakPtr()), | 293 device_checkin_info_.android_id, |
| 294 device_checkin_info_.secret, |
| 295 account_ids_, |
| 302 url_request_context_getter_)); | 296 url_request_context_getter_)); |
| 303 checkin_request_->Start(); | 297 checkin_request_->Start(); |
| 304 } | 298 } |
| 305 | 299 |
| 306 void GCMClientImpl::OnCheckinCompleted( | 300 void GCMClientImpl::OnCheckinCompleted(uint64 android_id, |
| 307 const checkin_proto::AndroidCheckinResponse& checkin_response) { | 301 uint64 security_token) { |
| 308 checkin_request_.reset(); | 302 checkin_request_.reset(); |
| 309 | 303 |
| 310 if (!checkin_response.has_android_id() || | 304 CheckinInfo checkin_info; |
| 311 !checkin_response.has_security_token()) { | 305 checkin_info.android_id = android_id; |
| 312 // TODO(fgorski): I don't think a retry here will help, we should probably | 306 checkin_info.secret = security_token; |
| 307 |
| 308 if (!checkin_info.IsValid()) { |
| 309 // TODO(fgorski): I don't think a retry here will help, we should probalby |
| 313 // start over. By checking in with (0, 0). | 310 // start over. By checking in with (0, 0). |
| 314 return; | 311 return; |
| 315 } | 312 } |
| 316 | 313 |
| 317 CheckinInfo checkin_info; | |
| 318 checkin_info.android_id = checkin_response.android_id(); | |
| 319 checkin_info.secret = checkin_response.security_token(); | |
| 320 | |
| 321 if (state_ == INITIAL_DEVICE_CHECKIN) { | 314 if (state_ == INITIAL_DEVICE_CHECKIN) { |
| 322 OnFirstTimeDeviceCheckinCompleted(checkin_info); | 315 OnFirstTimeDeviceCheckinCompleted(checkin_info); |
| 323 } else { | 316 } else { |
| 324 // checkin_info is not expected to change after a periodic checkin as it | 317 // checkin_info is not expected to change after a periodic checkin as it |
| 325 // would invalidate the registratoin IDs. | 318 // would invalidate the registratoin IDs. |
| 326 DCHECK_EQ(READY, state_); | 319 DCHECK_EQ(READY, state_); |
| 327 DCHECK_EQ(device_checkin_info_.android_id, checkin_info.android_id); | 320 DCHECK_EQ(device_checkin_info_.android_id, checkin_info.android_id); |
| 328 DCHECK_EQ(device_checkin_info_.secret, checkin_info.secret); | 321 DCHECK_EQ(device_checkin_info_.secret, checkin_info.secret); |
| 329 } | 322 } |
| 330 | 323 |
| 331 if (device_checkin_info_.IsValid()) { | 324 if (device_checkin_info_.IsValid()) { |
| 332 base::Time last_checkin_time = clock_->Now(); | 325 base::Time last_checkin_time = clock_->Now(); |
| 333 gcm_store_->SetLastCheckinTime( | 326 gcm_store_->SetLastCheckinTime( |
| 334 last_checkin_time, | 327 last_checkin_time, |
| 335 base::Bind(&GCMClientImpl::SetLastCheckinTimeCallback, | 328 base::Bind(&GCMClientImpl::SetLastCheckinTimeCallback, |
| 336 weak_ptr_factory_.GetWeakPtr())); | 329 weak_ptr_factory_.GetWeakPtr())); |
| 337 UpdateGServicesSettings(checkin_response); | |
| 338 SchedulePeriodicCheckin(last_checkin_time); | 330 SchedulePeriodicCheckin(last_checkin_time); |
| 339 } | 331 } |
| 340 } | 332 } |
| 341 | 333 |
| 342 void GCMClientImpl::SchedulePeriodicCheckin( | 334 void GCMClientImpl::SchedulePeriodicCheckin( |
| 343 const base::Time& last_checkin_time) { | 335 const base::Time& last_checkin_time) { |
| 344 base::TimeDelta time_to_next_checkin = last_checkin_time + | 336 base::TimeDelta time_to_next_checkin = last_checkin_time + |
| 345 base::TimeDelta::FromSeconds(kDefaultCheckinInterval) - clock_->Now(); | 337 base::TimeDelta::FromSeconds(kDefaultCheckinInterval) - clock_->Now(); |
| 346 if (time_to_next_checkin < base::TimeDelta::FromSeconds(0L)) | 338 if (time_to_next_checkin < base::TimeDelta::FromSeconds(0L)) |
| 347 time_to_next_checkin = base::TimeDelta::FromSeconds(0L); | 339 time_to_next_checkin = base::TimeDelta::FromSeconds(0L); |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 send_error_details.additional_data.find(kSendErrorMessageIdKey); | 687 send_error_details.additional_data.find(kSendErrorMessageIdKey); |
| 696 if (iter != send_error_details.additional_data.end()) { | 688 if (iter != send_error_details.additional_data.end()) { |
| 697 send_error_details.message_id = iter->second; | 689 send_error_details.message_id = iter->second; |
| 698 send_error_details.additional_data.erase(iter); | 690 send_error_details.additional_data.erase(iter); |
| 699 } | 691 } |
| 700 | 692 |
| 701 delegate_->OnMessageSendError(data_message_stanza.category(), | 693 delegate_->OnMessageSendError(data_message_stanza.category(), |
| 702 send_error_details); | 694 send_error_details); |
| 703 } | 695 } |
| 704 | 696 |
| 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 base::Bind(&GCMClientImpl::UpdateGServicesSettingsCallback, | |
| 725 weak_ptr_factory_.GetWeakPtr())); | |
| 726 } | |
| 727 | |
| 728 void GCMClientImpl::UpdateGServicesSettingsCallback(bool success) { | |
| 729 DCHECK(success); | |
| 730 } | |
| 731 | |
| 732 } // namespace gcm | 697 } // namespace gcm |
| OLD | NEW |