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