| 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 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 340 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 341 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, | 341 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 342 const scoped_refptr<base::SequencedTaskRunner>& io_thread, | 342 const scoped_refptr<base::SequencedTaskRunner>& io_thread, |
| 343 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) | 343 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) |
| 344 : gcm_channel_status_syncer_( | 344 : gcm_channel_status_syncer_( |
| 345 new GCMChannelStatusSyncer(this, prefs, request_context)), | 345 new GCMChannelStatusSyncer(this, prefs, request_context)), |
| 346 signed_in_(false), | 346 signed_in_(false), |
| 347 gcm_started_(false), | 347 gcm_started_(false), |
| 348 gcm_enabled_(true), | 348 gcm_enabled_(true), |
| 349 connected_(false), | 349 connected_(false), |
| 350 account_mapper_(new GCMAccountMapper(this)), |
| 350 ui_thread_(ui_thread), | 351 ui_thread_(ui_thread), |
| 351 io_thread_(io_thread), | 352 io_thread_(io_thread), |
| 352 weak_ptr_factory_(this) { | 353 weak_ptr_factory_(this) { |
| 353 gcm_enabled_ = gcm_channel_status_syncer_->gcm_enabled(); | 354 gcm_enabled_ = gcm_channel_status_syncer_->gcm_enabled(); |
| 354 | 355 |
| 355 // Create and initialize the GCMClient. Note that this does not initiate the | 356 // Create and initialize the GCMClient. Note that this does not initiate the |
| 356 // GCM check-in. | 357 // GCM check-in. |
| 357 io_worker_.reset(new IOWorker(ui_thread, io_thread)); | 358 io_worker_.reset(new IOWorker(ui_thread, io_thread)); |
| 358 io_thread_->PostTask( | 359 io_thread_->PostTask( |
| 359 FROM_HERE, | 360 FROM_HERE, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 384 void GCMDriverDesktop::OnSignedIn() { | 385 void GCMDriverDesktop::OnSignedIn() { |
| 385 signed_in_ = true; | 386 signed_in_ = true; |
| 386 EnsureStarted(); | 387 EnsureStarted(); |
| 387 } | 388 } |
| 388 | 389 |
| 389 void GCMDriverDesktop::OnSignedOut() { | 390 void GCMDriverDesktop::OnSignedOut() { |
| 390 signed_in_ = false; | 391 signed_in_ = false; |
| 391 | 392 |
| 392 // When sign-in enforcement is not dropped, we will stop the GCM connection | 393 // When sign-in enforcement is not dropped, we will stop the GCM connection |
| 393 // when the user signs out. | 394 // when the user signs out. |
| 394 if (!GCMDriver::IsAllowedForAllUsers()) | 395 if (!GCMDriver::IsAllowedForAllUsers()) { |
| 395 Stop(); | 396 Stop(); |
| 397 } |
| 396 } | 398 } |
| 397 | 399 |
| 398 void GCMDriverDesktop::Purge() { | 400 void GCMDriverDesktop::Purge() { |
| 399 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 401 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 400 | 402 |
| 401 RemoveCachedData(); | 403 RemoveCachedData(); |
| 402 | 404 |
| 403 io_thread_->PostTask(FROM_HERE, | 405 io_thread_->PostTask(FROM_HERE, |
| 404 base::Bind(&GCMDriverDesktop::IOWorker::CheckOut, | 406 base::Bind(&GCMDriverDesktop::IOWorker::CheckOut, |
| 405 base::Unretained(io_worker_.get()))); | 407 base::Unretained(io_worker_.get()))); |
| 406 } | 408 } |
| 407 | 409 |
| 408 void GCMDriverDesktop::AddAppHandler(const std::string& app_id, | 410 void GCMDriverDesktop::AddAppHandler(const std::string& app_id, |
| 409 GCMAppHandler* handler) { | 411 GCMAppHandler* handler) { |
| 410 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 412 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 411 GCMDriver::AddAppHandler(app_id, handler); | 413 GCMDriver::AddAppHandler(app_id, handler); |
| 412 | 414 |
| 413 // Ensures that the GCM service is started when there is an interest. | 415 // Ensures that the GCM service is started when there is an interest. |
| 414 EnsureStarted(); | 416 EnsureStarted(); |
| 415 } | 417 } |
| 416 | 418 |
| 417 void GCMDriverDesktop::RemoveAppHandler(const std::string& app_id) { | 419 void GCMDriverDesktop::RemoveAppHandler(const std::string& app_id) { |
| 418 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 420 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 419 GCMDriver::RemoveAppHandler(app_id); | 421 GCMDriver::RemoveAppHandler(app_id); |
| 420 | 422 |
| 421 // Stops the GCM service when no app intends to consume it. | 423 // Stops the GCM service when no app intends to consume it. Stop function will |
| 422 if (app_handlers().empty()) | 424 // remove the last app handler - account mapper. |
| 425 if (app_handlers().size() == 1) { |
| 423 Stop(); | 426 Stop(); |
| 427 } |
| 424 } | 428 } |
| 425 | 429 |
| 426 void GCMDriverDesktop::AddConnectionObserver(GCMConnectionObserver* observer) { | 430 void GCMDriverDesktop::AddConnectionObserver(GCMConnectionObserver* observer) { |
| 427 connection_observer_list_.AddObserver(observer); | 431 connection_observer_list_.AddObserver(observer); |
| 428 } | 432 } |
| 429 | 433 |
| 430 void GCMDriverDesktop::RemoveConnectionObserver( | 434 void GCMDriverDesktop::RemoveConnectionObserver( |
| 431 GCMConnectionObserver* observer) { | 435 GCMConnectionObserver* observer) { |
| 432 connection_observer_list_.RemoveObserver(observer); | 436 connection_observer_list_.RemoveObserver(observer); |
| 433 } | 437 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 454 | 458 |
| 455 void GCMDriverDesktop::Stop() { | 459 void GCMDriverDesktop::Stop() { |
| 456 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 460 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 457 | 461 |
| 458 // No need to stop GCM service if not started yet. | 462 // No need to stop GCM service if not started yet. |
| 459 if (!gcm_started_) | 463 if (!gcm_started_) |
| 460 return; | 464 return; |
| 461 | 465 |
| 462 gcm_channel_status_syncer_->Stop(); | 466 gcm_channel_status_syncer_->Stop(); |
| 463 | 467 |
| 468 account_mapper_->ShutdownHandler(); |
| 469 GCMDriver::RemoveAppHandler(kGCMAccountMapperAppId); |
| 470 |
| 464 RemoveCachedData(); | 471 RemoveCachedData(); |
| 465 | 472 |
| 466 io_thread_->PostTask( | 473 io_thread_->PostTask( |
| 467 FROM_HERE, | 474 FROM_HERE, |
| 468 base::Bind(&GCMDriverDesktop::IOWorker::Stop, | 475 base::Bind(&GCMDriverDesktop::IOWorker::Stop, |
| 469 base::Unretained(io_worker_.get()))); | 476 base::Unretained(io_worker_.get()))); |
| 470 } | 477 } |
| 471 | 478 |
| 472 void GCMDriverDesktop::RegisterImpl( | 479 void GCMDriverDesktop::RegisterImpl( |
| 473 const std::string& app_id, | 480 const std::string& app_id, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 FROM_HERE, | 620 FROM_HERE, |
| 614 base::Bind(&GCMDriverDesktop::IOWorker::RemoveAccountMapping, | 621 base::Bind(&GCMDriverDesktop::IOWorker::RemoveAccountMapping, |
| 615 base::Unretained(io_worker_.get()), | 622 base::Unretained(io_worker_.get()), |
| 616 account_id)); | 623 account_id)); |
| 617 } | 624 } |
| 618 | 625 |
| 619 void GCMDriverDesktop::SetAccountTokens( | 626 void GCMDriverDesktop::SetAccountTokens( |
| 620 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { | 627 const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
| 621 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 628 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 622 | 629 |
| 630 account_mapper_->SetAccountTokens(account_tokens); |
| 631 |
| 623 io_thread_->PostTask( | 632 io_thread_->PostTask( |
| 624 FROM_HERE, | 633 FROM_HERE, |
| 625 base::Bind(&GCMDriverDesktop::IOWorker::SetAccountTokens, | 634 base::Bind(&GCMDriverDesktop::IOWorker::SetAccountTokens, |
| 626 base::Unretained(io_worker_.get()), | 635 base::Unretained(io_worker_.get()), |
| 627 account_tokens)); | 636 account_tokens)); |
| 628 } | 637 } |
| 629 | 638 |
| 630 GCMClient::Result GCMDriverDesktop::EnsureStarted() { | 639 GCMClient::Result GCMDriverDesktop::EnsureStarted() { |
| 631 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 640 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 632 | 641 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 if (!gcm_started_) | 728 if (!gcm_started_) |
| 720 return; | 729 return; |
| 721 | 730 |
| 722 GetAppHandler(app_id)->OnSendAcknowledged(app_id, message_id); | 731 GetAppHandler(app_id)->OnSendAcknowledged(app_id, message_id); |
| 723 } | 732 } |
| 724 | 733 |
| 725 void GCMDriverDesktop::GCMClientReady( | 734 void GCMDriverDesktop::GCMClientReady( |
| 726 const std::vector<AccountMapping>& account_mappings) { | 735 const std::vector<AccountMapping>& account_mappings) { |
| 727 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 736 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 728 | 737 |
| 738 GCMDriver::AddAppHandler(kGCMAccountMapperAppId, account_mapper_.get()); |
| 739 account_mapper_->Initialize(account_mappings); |
| 740 |
| 729 delayed_task_controller_->SetReady(); | 741 delayed_task_controller_->SetReady(); |
| 730 } | 742 } |
| 731 | 743 |
| 732 void GCMDriverDesktop::OnConnected(const net::IPEndPoint& ip_endpoint) { | 744 void GCMDriverDesktop::OnConnected(const net::IPEndPoint& ip_endpoint) { |
| 733 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 745 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 734 | 746 |
| 735 connected_ = true; | 747 connected_ = true; |
| 736 | 748 |
| 737 // Drop the event if the service has been stopped. | 749 // Drop the event if the service has been stopped. |
| 738 if (!gcm_started_) | 750 if (!gcm_started_) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 762 | 774 |
| 763 // Normally request_gcm_statistics_callback_ would not be null. | 775 // Normally request_gcm_statistics_callback_ would not be null. |
| 764 if (!request_gcm_statistics_callback_.is_null()) | 776 if (!request_gcm_statistics_callback_.is_null()) |
| 765 request_gcm_statistics_callback_.Run(stats); | 777 request_gcm_statistics_callback_.Run(stats); |
| 766 else | 778 else |
| 767 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 779 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
| 768 } | 780 } |
| 769 | 781 |
| 770 } // namespace gcm | 782 } // namespace gcm |
| 771 | 783 |
| OLD | NEW |