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 = 2 * 24 * 60 * 60LL; // seconds = 2 days. | |
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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 | 203 |
203 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { | 204 void GCMClientImpl::OnLoadCompleted(scoped_ptr<GCMStore::LoadResult> result) { |
204 DCHECK_EQ(LOADING, state_); | 205 DCHECK_EQ(LOADING, state_); |
205 | 206 |
206 if (!result->success) { | 207 if (!result->success) { |
207 ResetState(); | 208 ResetState(); |
208 return; | 209 return; |
209 } | 210 } |
210 | 211 |
211 registrations_ = result->registrations; | 212 registrations_ = result->registrations; |
212 | |
213 device_checkin_info_.android_id = result->device_android_id; | 213 device_checkin_info_.android_id = result->device_android_id; |
214 device_checkin_info_.secret = result->device_security_token; | 214 device_checkin_info_.secret = result->device_security_token; |
215 base::Time last_checkin_time = result->last_checkin_time; | |
215 InitializeMCSClient(result.Pass()); | 216 InitializeMCSClient(result.Pass()); |
216 if (!device_checkin_info_.IsValid()) { | 217 |
217 device_checkin_info_.Reset(); | 218 if (device_checkin_info_.IsValid()) { |
218 state_ = INITIAL_DEVICE_CHECKIN; | 219 SchedulePeriodicCheckin(last_checkin_time); |
219 StartCheckin(device_checkin_info_); | 220 OnReady(); |
220 return; | 221 return; |
221 } | 222 } |
222 | 223 |
223 OnReady(); | 224 state_ = INITIAL_DEVICE_CHECKIN; |
225 device_checkin_info_.Reset(); | |
226 StartCheckin(); | |
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)); |
231 connection_factory_ = internals_builder_->BuildConnectionFactory( | 234 connection_factory_ = internals_builder_->BuildConnectionFactory( |
232 endpoints, | 235 endpoints, |
233 kDefaultBackoffPolicy, | 236 kDefaultBackoffPolicy, |
(...skipping 40 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 DCHECK_EQ(device_checkin_info_.android_id, checkin_info.android_id); |
jianli
2014/04/02 23:02:51
Please comment on the fact that the checkin info i
| |
316 device_checkin_info_.secret != checkin_info.secret) { | 319 DCHECK_EQ(device_checkin_info_.secret, checkin_info.secret); |
317 ResetState(); | |
318 } else { | |
319 // TODO(fgorski): Reset the checkin timer. | |
320 } | |
321 } | 320 } |
321 | |
322 if (device_checkin_info_.IsValid()) { | |
323 base::Time last_checkin_time = clock_->Now(); | |
324 gcm_store_->SetLastCheckinTime( | |
325 last_checkin_time, | |
326 base::Bind(&GCMClientImpl::SetLastCheckinTimeCallback, | |
327 weak_ptr_factory_.GetWeakPtr())); | |
328 SchedulePeriodicCheckin(last_checkin_time); | |
329 } | |
330 } | |
331 | |
332 void GCMClientImpl::SchedulePeriodicCheckin( | |
333 const base::Time& last_checkin_time) { | |
334 base::TimeDelta time_to_next_checkin = last_checkin_time + | |
335 base::TimeDelta::FromSeconds(kDefaultCheckinInterval) - clock_->Now(); | |
336 if (time_to_next_checkin < base::TimeDelta::FromSeconds(0L)) | |
337 time_to_next_checkin = base::TimeDelta::FromSeconds(0L); | |
338 // TODO(fgorski): Make sure that once dynamic events (like accounts list | |
339 // change) trigger checkin we reset the timer. | |
340 base::MessageLoop::current()->PostDelayedTask( | |
341 FROM_HERE, | |
342 base::Bind(&GCMClientImpl::StartCheckin, | |
343 weak_ptr_factory_.GetWeakPtr()), | |
344 time_to_next_checkin); | |
345 } | |
346 | |
347 void GCMClientImpl::SetLastCheckinTimeCallback(bool success) { | |
348 // TODO(fgorski): This is one of the signals that store needs a rebuild. | |
349 DCHECK(success); | |
322 } | 350 } |
323 | 351 |
324 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { | 352 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { |
325 // TODO(fgorski): This is one of the signals that store needs a rebuild. | 353 // TODO(fgorski): This is one of the signals that store needs a rebuild. |
326 DCHECK(success); | 354 DCHECK(success); |
327 } | 355 } |
328 | 356 |
329 void GCMClientImpl::UpdateRegistrationCallback(bool success) { | 357 void GCMClientImpl::UpdateRegistrationCallback(bool success) { |
330 // TODO(fgorski): This is one of the signals that store needs a rebuild. | 358 // TODO(fgorski): This is one of the signals that store needs a rebuild. |
331 DCHECK(success); | 359 DCHECK(success); |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
658 if (iter != send_error_details.additional_data.end()) { | 686 if (iter != send_error_details.additional_data.end()) { |
659 send_error_details.message_id = iter->second; | 687 send_error_details.message_id = iter->second; |
660 send_error_details.additional_data.erase(iter); | 688 send_error_details.additional_data.erase(iter); |
661 } | 689 } |
662 | 690 |
663 delegate_->OnMessageSendError(data_message_stanza.category(), | 691 delegate_->OnMessageSendError(data_message_stanza.category(), |
664 send_error_details); | 692 send_error_details); |
665 } | 693 } |
666 | 694 |
667 } // namespace gcm | 695 } // namespace gcm |
OLD | NEW |