Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: google_apis/gcm/gcm_client_impl.cc

Issue 222913004: [GCM] Minimum change for periodic checkin (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Applying a final comments. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « google_apis/gcm/gcm_client_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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 {
317 // checkin_info is not expected to change after a periodic checkin as it
318 // would invalidate the registratoin IDs.
314 DCHECK_EQ(READY, state_); 319 DCHECK_EQ(READY, state_);
315 if (device_checkin_info_.android_id != checkin_info.android_id || 320 DCHECK_EQ(device_checkin_info_.android_id, checkin_info.android_id);
316 device_checkin_info_.secret != checkin_info.secret) { 321 DCHECK_EQ(device_checkin_info_.secret, checkin_info.secret);
317 ResetState();
318 } else {
319 // TODO(fgorski): Reset the checkin timer.
320 }
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 }
332 }
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) - clock_->Now();
338 if (time_to_next_checkin < base::TimeDelta::FromSeconds(0L))
339 time_to_next_checkin = base::TimeDelta::FromSeconds(0L);
340 // TODO(fgorski): Make sure that once dynamic events (like accounts list
341 // change) trigger checkin we reset the timer.
342 base::MessageLoop::current()->PostDelayedTask(
343 FROM_HERE,
344 base::Bind(&GCMClientImpl::StartCheckin,
345 weak_ptr_factory_.GetWeakPtr()),
346 time_to_next_checkin);
347 }
348
349 void GCMClientImpl::SetLastCheckinTimeCallback(bool success) {
350 // TODO(fgorski): This is one of the signals that store needs a rebuild.
351 DCHECK(success);
322 } 352 }
323 353
324 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { 354 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) {
325 // TODO(fgorski): This is one of the signals that store needs a rebuild. 355 // TODO(fgorski): This is one of the signals that store needs a rebuild.
326 DCHECK(success); 356 DCHECK(success);
327 } 357 }
328 358
329 void GCMClientImpl::UpdateRegistrationCallback(bool success) { 359 void GCMClientImpl::UpdateRegistrationCallback(bool success) {
330 // TODO(fgorski): This is one of the signals that store needs a rebuild. 360 // TODO(fgorski): This is one of the signals that store needs a rebuild.
331 DCHECK(success); 361 DCHECK(success);
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 if (iter != send_error_details.additional_data.end()) { 688 if (iter != send_error_details.additional_data.end()) {
659 send_error_details.message_id = iter->second; 689 send_error_details.message_id = iter->second;
660 send_error_details.additional_data.erase(iter); 690 send_error_details.additional_data.erase(iter);
661 } 691 }
662 692
663 delegate_->OnMessageSendError(data_message_stanza.category(), 693 delegate_->OnMessageSendError(data_message_stanza.category(),
664 send_error_details); 694 send_error_details);
665 } 695 }
666 696
667 } // namespace gcm 697 } // namespace gcm
OLDNEW
« no previous file with comments | « google_apis/gcm/gcm_client_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698