Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/services/gcm/gcm_driver.h" | 5 #include "chrome/browser/services/gcm/gcm_driver.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 stats = gcm_client_->GetStatistics(); | 375 stats = gcm_client_->GetStatistics(); |
| 376 stats.gcm_client_created = true; | 376 stats.gcm_client_created = true; |
| 377 } | 377 } |
| 378 | 378 |
| 379 content::BrowserThread::PostTask( | 379 content::BrowserThread::PostTask( |
| 380 content::BrowserThread::UI, | 380 content::BrowserThread::UI, |
| 381 FROM_HERE, | 381 FROM_HERE, |
| 382 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); | 382 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); |
| 383 } | 383 } |
| 384 | 384 |
| 385 GCMDriver::GCMDriver(scoped_ptr<IdentityProvider> identity_provider) | 385 GCMDriver::GCMDriver( |
| 386 scoped_ptr<GCMClientFactory> gcm_client_factory, | |
| 387 scoped_ptr<IdentityProvider> identity_provider, | |
| 388 const base::FilePath& store_path, | |
| 389 const scoped_refptr<net::URLRequestContextGetter>& request_context) | |
| 386 : identity_provider_(identity_provider.Pass()), | 390 : identity_provider_(identity_provider.Pass()), |
| 391 gcm_enabled_(true), | |
| 387 gcm_client_ready_(false), | 392 gcm_client_ready_(false), |
| 388 weak_ptr_factory_(this) { | 393 weak_ptr_factory_(this) { |
| 389 } | |
| 390 | |
| 391 GCMDriver::~GCMDriver() { | |
| 392 } | |
| 393 | |
| 394 void GCMDriver::Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory) { | |
| 395 // Get the list of available accounts. | 394 // Get the list of available accounts. |
| 396 std::vector<std::string> account_ids; | 395 std::vector<std::string> account_ids; |
| 397 #if !defined(OS_ANDROID) | 396 #if !defined(OS_ANDROID) |
| 398 account_ids = identity_provider_->GetTokenService()->GetAccounts(); | 397 account_ids = identity_provider_->GetTokenService()->GetAccounts(); |
| 399 #endif | 398 #endif |
| 400 | 399 |
| 401 // Create and initialize the GCMClient. Note that this does not initiate the | 400 // Create and initialize the GCMClient. Note that this does not initiate the |
| 402 // GCM check-in. | 401 // GCM check-in. |
| 403 DCHECK(!io_worker_); | 402 DCHECK(!io_worker_); |
| 404 io_worker_.reset(new IOWorker()); | 403 io_worker_.reset(new IOWorker()); |
| 405 content::BrowserThread::PostTask( | 404 content::BrowserThread::PostTask( |
| 406 content::BrowserThread::IO, | 405 content::BrowserThread::IO, |
| 407 FROM_HERE, | 406 FROM_HERE, |
| 408 base::Bind(&GCMDriver::IOWorker::Initialize, | 407 base::Bind(&GCMDriver::IOWorker::Initialize, |
| 409 base::Unretained(io_worker_.get()), | 408 base::Unretained(io_worker_.get()), |
| 410 base::Passed(&gcm_client_factory), | 409 base::Passed(&gcm_client_factory), |
| 411 GetStorePath(), | 410 store_path, |
| 412 account_ids, | 411 account_ids, |
| 413 GetURLRequestContextGetter())); | 412 request_context)); |
| 414 | |
| 415 // Start the GCM service if the rollout signal indicates yes. | |
| 416 if (ShouldStartAutomatically()) | |
| 417 EnsureStarted(); | |
| 418 | 413 |
| 419 identity_provider_->AddObserver(this); | 414 identity_provider_->AddObserver(this); |
| 420 } | 415 } |
| 421 | 416 |
| 422 void GCMDriver::Start() { | 417 GCMDriver::GCMDriver() |
| 418 : gcm_enabled_(true), | |
| 419 gcm_client_ready_(false), | |
| 420 weak_ptr_factory_(this) { | |
| 421 } | |
| 422 | |
| 423 GCMDriver::~GCMDriver() { | |
| 424 } | |
| 425 | |
| 426 void GCMDriver::Enable() { | |
| 423 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 427 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 424 | 428 |
| 429 if (gcm_enabled_) | |
| 430 return; | |
| 431 gcm_enabled_ = true; | |
| 432 | |
| 425 EnsureStarted(); | 433 EnsureStarted(); |
| 426 } | 434 } |
| 427 | 435 |
| 436 void GCMDriver::Disable() { | |
| 437 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 438 | |
| 439 if (!gcm_enabled_) | |
| 440 return; | |
| 441 gcm_enabled_ = false; | |
| 442 | |
| 443 Stop(); | |
| 444 } | |
| 445 | |
| 428 void GCMDriver::Stop() { | 446 void GCMDriver::Stop() { |
| 429 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 447 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 430 | 448 |
| 431 // No need to stop GCM service if not started yet. | 449 // No need to stop GCM service if not started yet. |
| 432 if (account_id_.empty()) | 450 if (account_id_.empty()) |
| 433 return; | 451 return; |
| 434 | 452 |
| 435 RemoveCachedData(); | 453 RemoveCachedData(); |
| 436 | 454 |
| 437 content::BrowserThread::PostTask( | 455 content::BrowserThread::PostTask( |
| 438 content::BrowserThread::IO, | 456 content::BrowserThread::IO, |
| 439 FROM_HERE, | 457 FROM_HERE, |
| 440 base::Bind(&GCMDriver::IOWorker::Stop, | 458 base::Bind(&GCMDriver::IOWorker::Stop, |
| 441 base::Unretained(io_worker_.get()))); | 459 base::Unretained(io_worker_.get()))); |
| 442 } | 460 } |
| 443 | 461 |
| 444 void GCMDriver::ShutdownService() { | 462 void GCMDriver::Shutdown() { |
| 445 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 463 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 446 identity_provider_->RemoveObserver(this); | 464 identity_provider_->RemoveObserver(this); |
| 447 for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); | 465 for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); |
| 448 iter != app_handlers_.end(); ++iter) { | 466 iter != app_handlers_.end(); ++iter) { |
| 449 iter->second->ShutdownHandler(); | 467 iter->second->ShutdownHandler(); |
| 450 } | 468 } |
| 451 app_handlers_.clear(); | 469 app_handlers_.clear(); |
| 452 content::BrowserThread::DeleteSoon(content::BrowserThread::IO, | 470 content::BrowserThread::DeleteSoon(content::BrowserThread::IO, |
| 453 FROM_HERE, | 471 FROM_HERE, |
| 454 io_worker_.release()); | 472 io_worker_.release()); |
| 455 } | 473 } |
| 456 | 474 |
| 457 void GCMDriver::AddAppHandler(const std::string& app_id, | 475 void GCMDriver::AddAppHandler(const std::string& app_id, |
| 458 GCMAppHandler* handler) { | 476 GCMAppHandler* handler) { |
| 459 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 477 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 460 DCHECK(!app_id.empty()); | 478 DCHECK(!app_id.empty()); |
| 461 DCHECK(handler); | 479 DCHECK(handler); |
| 462 DCHECK(app_handlers_.find(app_id) == app_handlers_.end()); | 480 DCHECK(app_handlers_.find(app_id) == app_handlers_.end()); |
| 463 | 481 |
| 464 app_handlers_[app_id] = handler; | 482 app_handlers_[app_id] = handler; |
| 483 | |
| 484 // Ensures that the GCM service is started when there is an interest. | |
| 485 EnsureStarted(); | |
| 465 } | 486 } |
| 466 | 487 |
| 467 void GCMDriver::RemoveAppHandler(const std::string& app_id) { | 488 void GCMDriver::RemoveAppHandler(const std::string& app_id) { |
| 468 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 489 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 469 DCHECK(!app_id.empty()); | 490 DCHECK(!app_id.empty()); |
| 470 | 491 |
| 471 app_handlers_.erase(app_id); | 492 app_handlers_.erase(app_id); |
| 472 } | 493 } |
| 473 | 494 |
| 474 void GCMDriver::Register(const std::string& app_id, | 495 void GCMDriver::Register(const std::string& app_id, |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 660 request_gcm_statistics_callback_ = callback; | 681 request_gcm_statistics_callback_ = callback; |
| 661 content::BrowserThread::PostTask( | 682 content::BrowserThread::PostTask( |
| 662 content::BrowserThread::IO, | 683 content::BrowserThread::IO, |
| 663 FROM_HERE, | 684 FROM_HERE, |
| 664 base::Bind(&GCMDriver::IOWorker::SetGCMRecording, | 685 base::Bind(&GCMDriver::IOWorker::SetGCMRecording, |
| 665 base::Unretained(io_worker_.get()), | 686 base::Unretained(io_worker_.get()), |
| 666 recording)); | 687 recording)); |
| 667 } | 688 } |
| 668 | 689 |
| 669 void GCMDriver::OnActiveAccountLogin() { | 690 void GCMDriver::OnActiveAccountLogin() { |
| 670 if (ShouldStartAutomatically()) | 691 if (gcm_enabled_) |
| 671 EnsureStarted(); | 692 EnsureStarted(); |
| 672 } | 693 } |
| 673 | 694 |
| 674 void GCMDriver::OnActiveAccountLogout() { | 695 void GCMDriver::OnActiveAccountLogout() { |
| 675 CheckOut(); | 696 CheckOut(); |
| 676 } | 697 } |
| 677 | 698 |
| 678 void GCMDriver::EnsureStarted() { | 699 void GCMDriver::EnsureStarted() { |
|
fgorski
2014/05/19 17:33:32
please make sure to check on gcm_enabled_
jianli
2014/05/19 18:37:20
Done.
| |
| 679 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 700 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 680 const std::string account_id = identity_provider_->GetActiveAccountId(); | 701 const std::string account_id = identity_provider_->GetActiveAccountId(); |
| 681 if (account_id.empty()) | 702 if (account_id.empty()) |
| 682 return; | 703 return; |
| 683 | 704 |
| 684 // CheckIn could be called more than once when: | 705 // CheckIn could be called more than once when: |
| 685 // 1) The password changes. | 706 // 1) The password changes. |
| 686 // 2) Register/send function calls it to ensure CheckIn is done. | 707 // 2) Register/send function calls it to ensure CheckIn is done. |
| 687 if (account_id_ == account_id) | 708 if (account_id_ == account_id) |
| 688 return; | 709 return; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 852 void GCMDriver::GetGCMStatisticsFinished(GCMClient::GCMStatistics stats) { | 873 void GCMDriver::GetGCMStatisticsFinished(GCMClient::GCMStatistics stats) { |
| 853 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 874 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 854 | 875 |
| 855 // Normally request_gcm_statistics_callback_ would not be null. | 876 // Normally request_gcm_statistics_callback_ would not be null. |
| 856 if (!request_gcm_statistics_callback_.is_null()) | 877 if (!request_gcm_statistics_callback_.is_null()) |
| 857 request_gcm_statistics_callback_.Run(stats); | 878 request_gcm_statistics_callback_.Run(stats); |
| 858 else | 879 else |
| 859 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 880 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
| 860 } | 881 } |
| 861 | 882 |
| 883 std::string GCMDriver::SignedInUserName() const { | |
| 884 if (IsStarted()) | |
| 885 return identity_provider_->GetActiveUsername(); | |
| 886 return std::string(); | |
| 887 } | |
| 888 | |
| 862 } // namespace gcm | 889 } // namespace gcm |
| OLD | NEW |