| 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 "components/gcm_driver/gcm_driver_desktop.h" | 5 #include "components/gcm_driver/gcm_driver_desktop.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 : gcm_channel_status_syncer_( | 346 : gcm_channel_status_syncer_( |
| 347 new GCMChannelStatusSyncer(this, | 347 new GCMChannelStatusSyncer(this, |
| 348 prefs, | 348 prefs, |
| 349 channel_status_request_url, | 349 channel_status_request_url, |
| 350 user_agent, | 350 user_agent, |
| 351 request_context)), | 351 request_context)), |
| 352 signed_in_(false), | 352 signed_in_(false), |
| 353 gcm_started_(false), | 353 gcm_started_(false), |
| 354 gcm_enabled_(true), | 354 gcm_enabled_(true), |
| 355 connected_(false), | 355 connected_(false), |
| 356 account_mapper_(new GCMAccountMapper(this)), |
| 356 ui_thread_(ui_thread), | 357 ui_thread_(ui_thread), |
| 357 io_thread_(io_thread), | 358 io_thread_(io_thread), |
| 358 weak_ptr_factory_(this) { | 359 weak_ptr_factory_(this) { |
| 359 gcm_enabled_ = gcm_channel_status_syncer_->gcm_enabled(); | 360 gcm_enabled_ = gcm_channel_status_syncer_->gcm_enabled(); |
| 360 | 361 |
| 361 // Create and initialize the GCMClient. Note that this does not initiate the | 362 // Create and initialize the GCMClient. Note that this does not initiate the |
| 362 // GCM check-in. | 363 // GCM check-in. |
| 363 io_worker_.reset(new IOWorker(ui_thread, io_thread)); | 364 io_worker_.reset(new IOWorker(ui_thread, io_thread)); |
| 364 io_thread_->PostTask( | 365 io_thread_->PostTask( |
| 365 FROM_HERE, | 366 FROM_HERE, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 392 void GCMDriverDesktop::OnSignedIn() { | 393 void GCMDriverDesktop::OnSignedIn() { |
| 393 signed_in_ = true; | 394 signed_in_ = true; |
| 394 EnsureStarted(); | 395 EnsureStarted(); |
| 395 } | 396 } |
| 396 | 397 |
| 397 void GCMDriverDesktop::OnSignedOut() { | 398 void GCMDriverDesktop::OnSignedOut() { |
| 398 signed_in_ = false; | 399 signed_in_ = false; |
| 399 | 400 |
| 400 // When sign-in enforcement is not dropped, we will stop the GCM connection | 401 // When sign-in enforcement is not dropped, we will stop the GCM connection |
| 401 // when the user signs out. | 402 // when the user signs out. |
| 402 if (!GCMDriver::IsAllowedForAllUsers()) | 403 if (!GCMDriver::IsAllowedForAllUsers()) { |
| 403 Stop(); | 404 Stop(); |
| 405 } |
| 404 } | 406 } |
| 405 | 407 |
| 406 void GCMDriverDesktop::Purge() { | 408 void GCMDriverDesktop::Purge() { |
| 407 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 409 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 408 | 410 |
| 409 RemoveCachedData(); | 411 RemoveCachedData(); |
| 410 | 412 |
| 411 io_thread_->PostTask(FROM_HERE, | 413 io_thread_->PostTask(FROM_HERE, |
| 412 base::Bind(&GCMDriverDesktop::IOWorker::CheckOut, | 414 base::Bind(&GCMDriverDesktop::IOWorker::CheckOut, |
| 413 base::Unretained(io_worker_.get()))); | 415 base::Unretained(io_worker_.get()))); |
| 414 } | 416 } |
| 415 | 417 |
| 416 void GCMDriverDesktop::AddAppHandler(const std::string& app_id, | 418 void GCMDriverDesktop::AddAppHandler(const std::string& app_id, |
| 417 GCMAppHandler* handler) { | 419 GCMAppHandler* handler) { |
| 418 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 420 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 419 GCMDriver::AddAppHandler(app_id, handler); | 421 GCMDriver::AddAppHandler(app_id, handler); |
| 420 | 422 |
| 421 // Ensures that the GCM service is started when there is an interest. | 423 // Ensures that the GCM service is started when there is an interest. |
| 422 EnsureStarted(); | 424 EnsureStarted(); |
| 423 } | 425 } |
| 424 | 426 |
| 425 void GCMDriverDesktop::RemoveAppHandler(const std::string& app_id) { | 427 void GCMDriverDesktop::RemoveAppHandler(const std::string& app_id) { |
| 426 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 428 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 427 GCMDriver::RemoveAppHandler(app_id); | 429 GCMDriver::RemoveAppHandler(app_id); |
| 428 | 430 |
| 429 // Stops the GCM service when no app intends to consume it. | 431 // Stops the GCM service when no app intends to consume it. Stop function will |
| 430 if (app_handlers().empty()) | 432 // remove the last app handler - account mapper. |
| 433 if (app_handlers().size() == 1) { |
| 431 Stop(); | 434 Stop(); |
| 435 } |
| 432 } | 436 } |
| 433 | 437 |
| 434 void GCMDriverDesktop::AddConnectionObserver(GCMConnectionObserver* observer) { | 438 void GCMDriverDesktop::AddConnectionObserver(GCMConnectionObserver* observer) { |
| 435 connection_observer_list_.AddObserver(observer); | 439 connection_observer_list_.AddObserver(observer); |
| 436 } | 440 } |
| 437 | 441 |
| 438 void GCMDriverDesktop::RemoveConnectionObserver( | 442 void GCMDriverDesktop::RemoveConnectionObserver( |
| 439 GCMConnectionObserver* observer) { | 443 GCMConnectionObserver* observer) { |
| 440 connection_observer_list_.RemoveObserver(observer); | 444 connection_observer_list_.RemoveObserver(observer); |
| 441 } | 445 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 462 | 466 |
| 463 void GCMDriverDesktop::Stop() { | 467 void GCMDriverDesktop::Stop() { |
| 464 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 468 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 465 | 469 |
| 466 // No need to stop GCM service if not started yet. | 470 // No need to stop GCM service if not started yet. |
| 467 if (!gcm_started_) | 471 if (!gcm_started_) |
| 468 return; | 472 return; |
| 469 | 473 |
| 470 gcm_channel_status_syncer_->Stop(); | 474 gcm_channel_status_syncer_->Stop(); |
| 471 | 475 |
| 476 account_mapper_->ShutdownHandler(); |
| 477 GCMDriver::RemoveAppHandler(kGCMAccountMapperAppId); |
| 478 |
| 472 RemoveCachedData(); | 479 RemoveCachedData(); |
| 473 | 480 |
| 474 io_thread_->PostTask( | 481 io_thread_->PostTask( |
| 475 FROM_HERE, | 482 FROM_HERE, |
| 476 base::Bind(&GCMDriverDesktop::IOWorker::Stop, | 483 base::Bind(&GCMDriverDesktop::IOWorker::Stop, |
| 477 base::Unretained(io_worker_.get()))); | 484 base::Unretained(io_worker_.get()))); |
| 478 } | 485 } |
| 479 | 486 |
| 480 void GCMDriverDesktop::RegisterImpl( | 487 void GCMDriverDesktop::RegisterImpl( |
| 481 const std::string& app_id, | 488 const std::string& app_id, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 FROM_HERE, | 628 FROM_HERE, |
| 622 base::Bind(&GCMDriverDesktop::IOWorker::RemoveAccountMapping, | 629 base::Bind(&GCMDriverDesktop::IOWorker::RemoveAccountMapping, |
| 623 base::Unretained(io_worker_.get()), | 630 base::Unretained(io_worker_.get()), |
| 624 account_id)); | 631 account_id)); |
| 625 } | 632 } |
| 626 | 633 |
| 627 void GCMDriverDesktop::SetAccountTokens( | 634 void GCMDriverDesktop::SetAccountTokens( |
| 628 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { | 635 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
| 629 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 636 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 630 | 637 |
| 638 account_mapper_->SetAccountTokens(account_tokens); |
| 639 |
| 631 io_thread_->PostTask( | 640 io_thread_->PostTask( |
| 632 FROM_HERE, | 641 FROM_HERE, |
| 633 base::Bind(&GCMDriverDesktop::IOWorker::SetAccountTokens, | 642 base::Bind(&GCMDriverDesktop::IOWorker::SetAccountTokens, |
| 634 base::Unretained(io_worker_.get()), | 643 base::Unretained(io_worker_.get()), |
| 635 account_tokens)); | 644 account_tokens)); |
| 636 } | 645 } |
| 637 | 646 |
| 638 GCMClient::Result GCMDriverDesktop::EnsureStarted() { | 647 GCMClient::Result GCMDriverDesktop::EnsureStarted() { |
| 639 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 648 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 640 | 649 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 if (!gcm_started_) | 742 if (!gcm_started_) |
| 734 return; | 743 return; |
| 735 | 744 |
| 736 GetAppHandler(app_id)->OnSendAcknowledged(app_id, message_id); | 745 GetAppHandler(app_id)->OnSendAcknowledged(app_id, message_id); |
| 737 } | 746 } |
| 738 | 747 |
| 739 void GCMDriverDesktop::GCMClientReady( | 748 void GCMDriverDesktop::GCMClientReady( |
| 740 const std::vector<AccountMapping>& account_mappings) { | 749 const std::vector<AccountMapping>& account_mappings) { |
| 741 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 750 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 742 | 751 |
| 752 GCMDriver::AddAppHandler(kGCMAccountMapperAppId, account_mapper_.get()); |
| 753 account_mapper_->Initialize(account_mappings); |
| 754 |
| 743 delayed_task_controller_->SetReady(); | 755 delayed_task_controller_->SetReady(); |
| 744 } | 756 } |
| 745 | 757 |
| 746 void GCMDriverDesktop::OnConnected(const net::IPEndPoint& ip_endpoint) { | 758 void GCMDriverDesktop::OnConnected(const net::IPEndPoint& ip_endpoint) { |
| 747 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 759 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 748 | 760 |
| 749 connected_ = true; | 761 connected_ = true; |
| 750 | 762 |
| 751 // Drop the event if the service has been stopped. | 763 // Drop the event if the service has been stopped. |
| 752 if (!gcm_started_) | 764 if (!gcm_started_) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 776 | 788 |
| 777 // Normally request_gcm_statistics_callback_ would not be null. | 789 // Normally request_gcm_statistics_callback_ would not be null. |
| 778 if (!request_gcm_statistics_callback_.is_null()) | 790 if (!request_gcm_statistics_callback_.is_null()) |
| 779 request_gcm_statistics_callback_.Run(stats); | 791 request_gcm_statistics_callback_.Run(stats); |
| 780 else | 792 else |
| 781 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 793 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
| 782 } | 794 } |
| 783 | 795 |
| 784 } // namespace gcm | 796 } // namespace gcm |
| 785 | 797 |
| OLD | NEW |