| 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" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "components/gcm_driver/gcm_app_handler.h" | 16 #include "components/gcm_driver/gcm_app_handler.h" |
| 17 #include "components/gcm_driver/gcm_channel_status_syncer.h" |
| 17 #include "components/gcm_driver/gcm_client_factory.h" | 18 #include "components/gcm_driver/gcm_client_factory.h" |
| 18 #include "components/gcm_driver/gcm_delayed_task_controller.h" | 19 #include "components/gcm_driver/gcm_delayed_task_controller.h" |
| 19 #include "components/gcm_driver/system_encryptor.h" | 20 #include "components/gcm_driver/system_encryptor.h" |
| 20 #include "google_apis/gcm/engine/account_mapping.h" | 21 #include "google_apis/gcm/engine/account_mapping.h" |
| 21 #include "net/base/ip_endpoint.h" | 22 #include "net/base/ip_endpoint.h" |
| 22 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
| 23 | 24 |
| 24 namespace gcm { | 25 namespace gcm { |
| 25 | 26 |
| 26 class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { | 27 class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 const std::string& account_id) { | 323 const std::string& account_id) { |
| 323 DCHECK(io_thread_->RunsTasksOnCurrentThread()); | 324 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| 324 | 325 |
| 325 if (gcm_client_.get()) | 326 if (gcm_client_.get()) |
| 326 gcm_client_->RemoveAccountMapping(account_id); | 327 gcm_client_->RemoveAccountMapping(account_id); |
| 327 } | 328 } |
| 328 | 329 |
| 329 GCMDriverDesktop::GCMDriverDesktop( | 330 GCMDriverDesktop::GCMDriverDesktop( |
| 330 scoped_ptr<GCMClientFactory> gcm_client_factory, | 331 scoped_ptr<GCMClientFactory> gcm_client_factory, |
| 331 const GCMClient::ChromeBuildInfo& chrome_build_info, | 332 const GCMClient::ChromeBuildInfo& chrome_build_info, |
| 333 PrefService* prefs, |
| 332 const base::FilePath& store_path, | 334 const base::FilePath& store_path, |
| 333 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 335 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 334 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, | 336 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 335 const scoped_refptr<base::SequencedTaskRunner>& io_thread, | 337 const scoped_refptr<base::SequencedTaskRunner>& io_thread, |
| 336 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) | 338 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) |
| 337 : signed_in_(false), | 339 : gcm_channel_status_syncer_( |
| 340 new GCMChannelStatusSyncer(this, prefs, request_context)), |
| 341 signed_in_(false), |
| 338 gcm_started_(false), | 342 gcm_started_(false), |
| 339 gcm_enabled_(true), | 343 gcm_enabled_(true), |
| 340 connected_(false), | 344 connected_(false), |
| 341 ui_thread_(ui_thread), | 345 ui_thread_(ui_thread), |
| 342 io_thread_(io_thread), | 346 io_thread_(io_thread), |
| 343 weak_ptr_factory_(this) { | 347 weak_ptr_factory_(this) { |
| 348 gcm_enabled_ = gcm_channel_status_syncer_->gcm_enabled(); |
| 349 |
| 344 // Create and initialize the GCMClient. Note that this does not initiate the | 350 // Create and initialize the GCMClient. Note that this does not initiate the |
| 345 // GCM check-in. | 351 // GCM check-in. |
| 346 io_worker_.reset(new IOWorker(ui_thread, io_thread)); | 352 io_worker_.reset(new IOWorker(ui_thread, io_thread)); |
| 347 io_thread_->PostTask( | 353 io_thread_->PostTask( |
| 348 FROM_HERE, | 354 FROM_HERE, |
| 349 base::Bind(&GCMDriverDesktop::IOWorker::Initialize, | 355 base::Bind(&GCMDriverDesktop::IOWorker::Initialize, |
| 350 base::Unretained(io_worker_.get()), | 356 base::Unretained(io_worker_.get()), |
| 351 base::Passed(&gcm_client_factory), | 357 base::Passed(&gcm_client_factory), |
| 352 chrome_build_info, | 358 chrome_build_info, |
| 353 store_path, | 359 store_path, |
| 354 request_context, | 360 request_context, |
| 355 blocking_task_runner)); | 361 blocking_task_runner)); |
| 356 } | 362 } |
| 357 | 363 |
| 358 GCMDriverDesktop::~GCMDriverDesktop() { | 364 GCMDriverDesktop::~GCMDriverDesktop() { |
| 359 } | 365 } |
| 360 | 366 |
| 361 void GCMDriverDesktop::Shutdown() { | 367 void GCMDriverDesktop::Shutdown() { |
| 362 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 368 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 363 GCMDriver::Shutdown(); | 369 GCMDriver::Shutdown(); |
| 370 |
| 371 // Dispose the syncer in order to release the reference to |
| 372 // URLRequestContextGetter that needs to be done before IOThread gets |
| 373 // deleted. |
| 374 gcm_channel_status_syncer_.reset(); |
| 375 |
| 364 io_thread_->DeleteSoon(FROM_HERE, io_worker_.release()); | 376 io_thread_->DeleteSoon(FROM_HERE, io_worker_.release()); |
| 365 } | 377 } |
| 366 | 378 |
| 367 void GCMDriverDesktop::OnSignedIn() { | 379 void GCMDriverDesktop::OnSignedIn() { |
| 368 signed_in_ = true; | 380 signed_in_ = true; |
| 369 EnsureStarted(); | 381 EnsureStarted(); |
| 370 } | 382 } |
| 371 | 383 |
| 372 void GCMDriverDesktop::OnSignedOut() { | 384 void GCMDriverDesktop::OnSignedOut() { |
| 373 signed_in_ = false; | 385 signed_in_ = false; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 Stop(); | 447 Stop(); |
| 436 } | 448 } |
| 437 | 449 |
| 438 void GCMDriverDesktop::Stop() { | 450 void GCMDriverDesktop::Stop() { |
| 439 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); | 451 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| 440 | 452 |
| 441 // No need to stop GCM service if not started yet. | 453 // No need to stop GCM service if not started yet. |
| 442 if (!gcm_started_) | 454 if (!gcm_started_) |
| 443 return; | 455 return; |
| 444 | 456 |
| 457 gcm_channel_status_syncer_->Stop(); |
| 458 |
| 445 RemoveCachedData(); | 459 RemoveCachedData(); |
| 446 | 460 |
| 447 io_thread_->PostTask( | 461 io_thread_->PostTask( |
| 448 FROM_HERE, | 462 FROM_HERE, |
| 449 base::Bind(&GCMDriverDesktop::IOWorker::Stop, | 463 base::Bind(&GCMDriverDesktop::IOWorker::Stop, |
| 450 base::Unretained(io_worker_.get()))); | 464 base::Unretained(io_worker_.get()))); |
| 451 } | 465 } |
| 452 | 466 |
| 453 void GCMDriverDesktop::RegisterImpl( | 467 void GCMDriverDesktop::RegisterImpl( |
| 454 const std::string& app_id, | 468 const std::string& app_id, |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 // Have any app requested the service? | 634 // Have any app requested the service? |
| 621 if (app_handlers().empty()) | 635 if (app_handlers().empty()) |
| 622 return GCMClient::UNKNOWN_ERROR; | 636 return GCMClient::UNKNOWN_ERROR; |
| 623 | 637 |
| 624 if (!signed_in_ && !GCMDriver::IsAllowedForAllUsers()) | 638 if (!signed_in_ && !GCMDriver::IsAllowedForAllUsers()) |
| 625 return GCMClient::NOT_SIGNED_IN; | 639 return GCMClient::NOT_SIGNED_IN; |
| 626 | 640 |
| 627 DCHECK(!delayed_task_controller_); | 641 DCHECK(!delayed_task_controller_); |
| 628 delayed_task_controller_.reset(new GCMDelayedTaskController); | 642 delayed_task_controller_.reset(new GCMDelayedTaskController); |
| 629 | 643 |
| 644 // Polling for channel status is only needed when GCM is supported for all |
| 645 // users. |
| 646 if (GCMDriver::IsAllowedForAllUsers()) |
| 647 gcm_channel_status_syncer_->EnsureStarted(); |
| 648 |
| 630 // Note that we need to pass weak pointer again since the existing weak | 649 // Note that we need to pass weak pointer again since the existing weak |
| 631 // pointer in IOWorker might have been invalidated when check-out occurs. | 650 // pointer in IOWorker might have been invalidated when check-out occurs. |
| 632 io_thread_->PostTask( | 651 io_thread_->PostTask( |
| 633 FROM_HERE, | 652 FROM_HERE, |
| 634 base::Bind(&GCMDriverDesktop::IOWorker::Start, | 653 base::Bind(&GCMDriverDesktop::IOWorker::Start, |
| 635 base::Unretained(io_worker_.get()), | 654 base::Unretained(io_worker_.get()), |
| 636 weak_ptr_factory_.GetWeakPtr())); | 655 weak_ptr_factory_.GetWeakPtr())); |
| 637 | 656 |
| 638 gcm_started_ = true; | 657 gcm_started_ = true; |
| 639 return GCMClient::SUCCESS; | 658 return GCMClient::SUCCESS; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 | 753 |
| 735 // Normally request_gcm_statistics_callback_ would not be null. | 754 // Normally request_gcm_statistics_callback_ would not be null. |
| 736 if (!request_gcm_statistics_callback_.is_null()) | 755 if (!request_gcm_statistics_callback_.is_null()) |
| 737 request_gcm_statistics_callback_.Run(stats); | 756 request_gcm_statistics_callback_.Run(stats); |
| 738 else | 757 else |
| 739 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 758 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
| 740 } | 759 } |
| 741 | 760 |
| 742 } // namespace gcm | 761 } // namespace gcm |
| 743 | 762 |
| OLD | NEW |