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

Side by Side Diff: components/gcm_driver/gcm_client_impl.cc

Issue 378643002: [GCM] Check-in with signed in accounts associates device to user (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixing compilation issue on android Created 6 years, 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/gcm_driver/gcm_client_impl.h" 5 #include "components/gcm_driver/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 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 net::NetLog* net_log, 240 net::NetLog* net_log,
241 GCMStatsRecorder* recorder) { 241 GCMStatsRecorder* recorder) {
242 return make_scoped_ptr<ConnectionFactory>( 242 return make_scoped_ptr<ConnectionFactory>(
243 new ConnectionFactoryImpl(endpoints, 243 new ConnectionFactoryImpl(endpoints,
244 backoff_policy, 244 backoff_policy,
245 network_session, 245 network_session,
246 net_log, 246 net_log,
247 recorder)); 247 recorder));
248 } 248 }
249 249
250 GCMClientImpl::CheckinInfo::CheckinInfo()
251 : android_id(0),
252 secret(0),
253 last_checkin_accounts_count(0),
254 accounts_set(false) {
255 }
256
257 GCMClientImpl::CheckinInfo::~CheckinInfo() {
258 }
259
260 void GCMClientImpl::CheckinInfo::Reset() {
261 android_id = 0;
262 secret = 0;
263 last_checkin_accounts_count = 0;
264 accounts_set = false;
265 account_tokens.clear();
266 }
267
250 GCMClientImpl::GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder) 268 GCMClientImpl::GCMClientImpl(scoped_ptr<GCMInternalsBuilder> internals_builder)
251 : internals_builder_(internals_builder.Pass()), 269 : internals_builder_(internals_builder.Pass()),
252 state_(UNINITIALIZED), 270 state_(UNINITIALIZED),
253 delegate_(NULL), 271 delegate_(NULL),
254 clock_(internals_builder_->BuildClock()), 272 clock_(internals_builder_->BuildClock()),
255 url_request_context_getter_(NULL), 273 url_request_context_getter_(NULL),
256 pending_registration_requests_deleter_(&pending_registration_requests_), 274 pending_registration_requests_deleter_(&pending_registration_requests_),
257 pending_unregistration_requests_deleter_( 275 pending_unregistration_requests_deleter_(
258 &pending_unregistration_requests_), 276 &pending_unregistration_requests_),
259 periodic_checkin_ptr_factory_(this), 277 periodic_checkin_ptr_factory_(this),
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 DCHECK_EQ(LOADING, state_); 325 DCHECK_EQ(LOADING, state_);
308 326
309 if (!result->success) { 327 if (!result->success) {
310 ResetState(); 328 ResetState();
311 return; 329 return;
312 } 330 }
313 331
314 registrations_ = result->registrations; 332 registrations_ = result->registrations;
315 device_checkin_info_.android_id = result->device_android_id; 333 device_checkin_info_.android_id = result->device_android_id;
316 device_checkin_info_.secret = result->device_security_token; 334 device_checkin_info_.secret = result->device_security_token;
335 device_checkin_info_.last_checkin_accounts_count = result->accounts_count;
336 // A case where there were previously no accounts reported with checkin is
337 // considered to be the same as when the list of accounts is empty. It enables
338 // scheduling a periodc checkin for devices with no signed in users
Nicolas Zea 2014/07/10 20:57:32 nit: periodic
fgorski 2014/07/11 22:51:20 Done.
339 // immediately after restart, while keeping |accounts_set == false| delays the
340 // checkin until the list of accounts is set explicitly.
341 if (result->accounts_count == 0)
342 device_checkin_info_.accounts_set = true;
317 last_checkin_time_ = result->last_checkin_time; 343 last_checkin_time_ = result->last_checkin_time;
318 gservices_settings_.UpdateFromLoadResult(*result); 344 gservices_settings_.UpdateFromLoadResult(*result);
319 InitializeMCSClient(result.Pass()); 345 InitializeMCSClient(result.Pass());
320 346
321 if (device_checkin_info_.IsValid()) { 347 if (device_checkin_info_.IsValid()) {
322 SchedulePeriodicCheckin(); 348 SchedulePeriodicCheckin();
323 OnReady(); 349 OnReady();
324 return; 350 return;
325 } 351 }
326 352
(...skipping 29 matching lines...) Expand all
356 weak_ptr_factory_.GetWeakPtr()), 382 weak_ptr_factory_.GetWeakPtr()),
357 result.Pass()); 383 result.Pass());
358 } 384 }
359 385
360 void GCMClientImpl::OnFirstTimeDeviceCheckinCompleted( 386 void GCMClientImpl::OnFirstTimeDeviceCheckinCompleted(
361 const CheckinInfo& checkin_info) { 387 const CheckinInfo& checkin_info) {
362 DCHECK(!device_checkin_info_.IsValid()); 388 DCHECK(!device_checkin_info_.IsValid());
363 389
364 device_checkin_info_.android_id = checkin_info.android_id; 390 device_checkin_info_.android_id = checkin_info.android_id;
365 device_checkin_info_.secret = checkin_info.secret; 391 device_checkin_info_.secret = checkin_info.secret;
392 // If accounts were not set by now, we can consider them set (to empty list)
393 // to make sure periodic checkins get scheduled after initial checkin.
394 device_checkin_info_.accounts_set = true;
366 gcm_store_->SetDeviceCredentials( 395 gcm_store_->SetDeviceCredentials(
367 checkin_info.android_id, checkin_info.secret, 396 checkin_info.android_id, checkin_info.secret,
368 base::Bind(&GCMClientImpl::SetDeviceCredentialsCallback, 397 base::Bind(&GCMClientImpl::SetDeviceCredentialsCallback,
369 weak_ptr_factory_.GetWeakPtr())); 398 weak_ptr_factory_.GetWeakPtr()));
370 399
371 OnReady(); 400 OnReady();
372 } 401 }
373 402
374 void GCMClientImpl::OnReady() { 403 void GCMClientImpl::OnReady() {
375 state_ = READY; 404 state_ = READY;
376 StartMCSLogin(); 405 StartMCSLogin();
377 406
378 delegate_->OnGCMReady(); 407 delegate_->OnGCMReady();
379 } 408 }
380 409
381 void GCMClientImpl::StartMCSLogin() { 410 void GCMClientImpl::StartMCSLogin() {
382 DCHECK_EQ(READY, state_); 411 DCHECK_EQ(READY, state_);
383 DCHECK(device_checkin_info_.IsValid()); 412 DCHECK(device_checkin_info_.IsValid());
384 mcs_client_->Login(device_checkin_info_.android_id, 413 mcs_client_->Login(device_checkin_info_.android_id,
385 device_checkin_info_.secret); 414 device_checkin_info_.secret);
386 } 415 }
387 416
388 void GCMClientImpl::ResetState() { 417 void GCMClientImpl::ResetState() {
389 state_ = UNINITIALIZED; 418 state_ = UNINITIALIZED;
390 // TODO(fgorski): reset all of the necessart objects and start over. 419 // TODO(fgorski): reset all of the necessart objects and start over.
391 } 420 }
392 421
422 void GCMClientImpl::SetAccountsForCheckin(
423 const std::map<std::string, std::string>& account_tokens,
424 bool account_removed) {
425 bool accounts_set_before = device_checkin_info_.accounts_set;
426 device_checkin_info_.account_tokens = account_tokens;
427 device_checkin_info_.accounts_set = true;
428
429 DVLOG(1) << "Set account called with: " << account_tokens.size()
430 << " accounts.";
431
432 if (state_ != READY && state_ != INITIAL_DEVICE_CHECKIN)
433 return;
434
435 // Checkin will be forced when any of the accounts was removed during the
436 // current Chrome session or if there has been an account removed between the
437 // restarts of Chrome. If there is a checkin in progress, it will be canceled.
438 if (account_removed ||
Nicolas Zea 2014/07/10 20:57:32 Do we need the account_removed boolean if we can c
fgorski 2014/07/11 22:51:20 If the same account is first removed and then adde
439 device_checkin_info_.last_checkin_accounts_count >
Nicolas Zea 2014/07/10 20:57:32 I also wonder if there's risk of issues if an acc
fgorski 2014/07/11 22:51:21 This approach can run into issues where the count
440 account_tokens.size()) {
441 DVLOG(1) << "Detecting that account has been removed, forcing checkin.";
442 checkin_request_.reset();
443 StartCheckin();
444 } else if (!accounts_set_before) {
445 SchedulePeriodicCheckin();
446 DVLOG(1) << "Scheduled periodic checkin.";
447 }
448 }
449
393 void GCMClientImpl::StartCheckin() { 450 void GCMClientImpl::StartCheckin() {
394 // Make sure no checkin is in progress. 451 // Make sure no checkin is in progress.
395 if (checkin_request_.get()) 452 if (checkin_request_.get())
396 return; 453 return;
397 454
398 checkin_proto::ChromeBuildProto chrome_build_proto; 455 checkin_proto::ChromeBuildProto chrome_build_proto;
399 ToCheckinProtoVersion(chrome_build_info_, &chrome_build_proto); 456 ToCheckinProtoVersion(chrome_build_info_, &chrome_build_proto);
400 CheckinRequest::RequestInfo request_info(device_checkin_info_.android_id, 457 CheckinRequest::RequestInfo request_info(device_checkin_info_.android_id,
401 device_checkin_info_.secret, 458 device_checkin_info_.secret,
459 device_checkin_info_.account_tokens,
402 gservices_settings_.digest(), 460 gservices_settings_.digest(),
403 chrome_build_proto); 461 chrome_build_proto);
404 checkin_request_.reset( 462 checkin_request_.reset(
405 new CheckinRequest(gservices_settings_.GetCheckinURL(), 463 new CheckinRequest(gservices_settings_.GetCheckinURL(),
406 request_info, 464 request_info,
407 kDefaultBackoffPolicy, 465 kDefaultBackoffPolicy,
408 base::Bind(&GCMClientImpl::OnCheckinCompleted, 466 base::Bind(&GCMClientImpl::OnCheckinCompleted,
409 weak_ptr_factory_.GetWeakPtr()), 467 weak_ptr_factory_.GetWeakPtr()),
410 url_request_context_getter_, 468 url_request_context_getter_,
411 &recorder_)); 469 &recorder_));
470 // Taking a snapshot of the accounts count here, as there might be an asynch
471 // update of the account tokens while checkin is in progress.
472 device_checkin_info_.last_checkin_accounts_count =
473 device_checkin_info_.account_tokens.size();
412 checkin_request_->Start(); 474 checkin_request_->Start();
413 } 475 }
414 476
415 void GCMClientImpl::OnCheckinCompleted( 477 void GCMClientImpl::OnCheckinCompleted(
416 const checkin_proto::AndroidCheckinResponse& checkin_response) { 478 const checkin_proto::AndroidCheckinResponse& checkin_response) {
417 checkin_request_.reset(); 479 checkin_request_.reset();
418 480
419 if (!checkin_response.has_android_id() || 481 if (!checkin_response.has_android_id() ||
420 !checkin_response.has_security_token()) { 482 !checkin_response.has_security_token()) {
421 // TODO(fgorski): I don't think a retry here will help, we should probably 483 // TODO(fgorski): I don't think a retry here will help, we should probably
(...skipping 19 matching lines...) Expand all
441 // First update G-services settings, as something might have changed. 503 // First update G-services settings, as something might have changed.
442 if (gservices_settings_.UpdateFromCheckinResponse(checkin_response)) { 504 if (gservices_settings_.UpdateFromCheckinResponse(checkin_response)) {
443 gcm_store_->SetGServicesSettings( 505 gcm_store_->SetGServicesSettings(
444 gservices_settings_.settings_map(), 506 gservices_settings_.settings_map(),
445 gservices_settings_.digest(), 507 gservices_settings_.digest(),
446 base::Bind(&GCMClientImpl::SetGServicesSettingsCallback, 508 base::Bind(&GCMClientImpl::SetGServicesSettingsCallback,
447 weak_ptr_factory_.GetWeakPtr())); 509 weak_ptr_factory_.GetWeakPtr()));
448 } 510 }
449 511
450 last_checkin_time_ = clock_->Now(); 512 last_checkin_time_ = clock_->Now();
451 gcm_store_->SetLastCheckinTime( 513 gcm_store_->SetLastCheckinInfo(
452 last_checkin_time_, 514 last_checkin_time_,
453 base::Bind(&GCMClientImpl::SetLastCheckinTimeCallback, 515 device_checkin_info_.last_checkin_accounts_count,
516 base::Bind(&GCMClientImpl::SetLastCheckinInfoCallback,
454 weak_ptr_factory_.GetWeakPtr())); 517 weak_ptr_factory_.GetWeakPtr()));
455 SchedulePeriodicCheckin(); 518 SchedulePeriodicCheckin();
456 } 519 }
457 } 520 }
458 521
459 void GCMClientImpl::SetGServicesSettingsCallback(bool success) { 522 void GCMClientImpl::SetGServicesSettingsCallback(bool success) {
460 DCHECK(success); 523 DCHECK(success);
461 } 524 }
462 525
463 void GCMClientImpl::SchedulePeriodicCheckin() { 526 void GCMClientImpl::SchedulePeriodicCheckin() {
464 // Make sure no checkin is in progress. 527 // Make sure no checkin is in progress.
465 if (checkin_request_.get()) 528 if (checkin_request_.get() || !device_checkin_info_.accounts_set)
466 return; 529 return;
467 530
468 // There should be only one periodic checkin pending at a time. Removing 531 // There should be only one periodic checkin pending at a time. Removing
469 // pending periodic checkin to schedule a new one. 532 // pending periodic checkin to schedule a new one.
470 periodic_checkin_ptr_factory_.InvalidateWeakPtrs(); 533 periodic_checkin_ptr_factory_.InvalidateWeakPtrs();
471 534
472 base::TimeDelta time_to_next_checkin = GetTimeToNextCheckin(); 535 base::TimeDelta time_to_next_checkin = GetTimeToNextCheckin();
473 if (time_to_next_checkin < base::TimeDelta()) 536 if (time_to_next_checkin < base::TimeDelta())
474 time_to_next_checkin = base::TimeDelta(); 537 time_to_next_checkin = base::TimeDelta();
475 538
476 base::MessageLoop::current()->PostDelayedTask( 539 base::MessageLoop::current()->PostDelayedTask(
477 FROM_HERE, 540 FROM_HERE,
478 base::Bind(&GCMClientImpl::StartCheckin, 541 base::Bind(&GCMClientImpl::StartCheckin,
479 periodic_checkin_ptr_factory_.GetWeakPtr()), 542 periodic_checkin_ptr_factory_.GetWeakPtr()),
480 time_to_next_checkin); 543 time_to_next_checkin);
481 } 544 }
482 545
483 base::TimeDelta GCMClientImpl::GetTimeToNextCheckin() const { 546 base::TimeDelta GCMClientImpl::GetTimeToNextCheckin() const {
484 return last_checkin_time_ + gservices_settings_.GetCheckinInterval() - 547 return last_checkin_time_ + gservices_settings_.GetCheckinInterval() -
485 clock_->Now(); 548 clock_->Now();
486 } 549 }
487 550
488 void GCMClientImpl::SetLastCheckinTimeCallback(bool success) { 551 void GCMClientImpl::SetLastCheckinInfoCallback(bool success) {
489 // TODO(fgorski): This is one of the signals that store needs a rebuild. 552 // TODO(fgorski): This is one of the signals that store needs a rebuild.
490 DCHECK(success); 553 DCHECK(success);
491 } 554 }
492 555
493 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) { 556 void GCMClientImpl::SetDeviceCredentialsCallback(bool success) {
494 // TODO(fgorski): This is one of the signals that store needs a rebuild. 557 // TODO(fgorski): This is one of the signals that store needs a rebuild.
495 DCHECK(success); 558 DCHECK(success);
496 } 559 }
497 560
498 void GCMClientImpl::UpdateRegistrationCallback(bool success) { 561 void GCMClientImpl::UpdateRegistrationCallback(bool success) {
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after
882 945
883 recorder_.RecordIncomingSendError( 946 recorder_.RecordIncomingSendError(
884 data_message_stanza.category(), 947 data_message_stanza.category(),
885 data_message_stanza.to(), 948 data_message_stanza.to(),
886 data_message_stanza.id()); 949 data_message_stanza.id());
887 delegate_->OnMessageSendError(data_message_stanza.category(), 950 delegate_->OnMessageSendError(data_message_stanza.category(),
888 send_error_details); 951 send_error_details);
889 } 952 }
890 953
891 } // namespace gcm 954 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698