| 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_client_factory.h" | 17 #include "components/gcm_driver/gcm_client_factory.h" |
| 18 #include "components/gcm_driver/gcm_delayed_task_controller.h" |
| 18 #include "components/gcm_driver/system_encryptor.h" | 19 #include "components/gcm_driver/system_encryptor.h" |
| 19 #include "google_apis/gcm/engine/account_mapping.h" | 20 #include "google_apis/gcm/engine/account_mapping.h" |
| 20 #include "net/base/ip_endpoint.h" | 21 #include "net/base/ip_endpoint.h" |
| 21 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
| 22 | 23 |
| 23 namespace gcm { | 24 namespace gcm { |
| 24 | 25 |
| 25 // Helper class to save tasks to run until we're ready to execute them. | |
| 26 class GCMDriverDesktop::DelayedTaskController { | |
| 27 public: | |
| 28 DelayedTaskController(); | |
| 29 ~DelayedTaskController(); | |
| 30 | |
| 31 // Adds a task that will be invoked once we're ready. | |
| 32 void AddTask(const base::Closure& task); | |
| 33 | |
| 34 // Sets ready status. It is ready only when check-in is completed and | |
| 35 // the GCMClient is fully initialized. | |
| 36 void SetReady(); | |
| 37 | |
| 38 // Returns true if it is ready to perform tasks. | |
| 39 bool CanRunTaskWithoutDelay() const; | |
| 40 | |
| 41 private: | |
| 42 void RunTasks(); | |
| 43 | |
| 44 // Flag that indicates that GCM is ready. | |
| 45 bool ready_; | |
| 46 | |
| 47 std::vector<base::Closure> delayed_tasks_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(DelayedTaskController); | |
| 50 }; | |
| 51 | |
| 52 GCMDriverDesktop::DelayedTaskController::DelayedTaskController() | |
| 53 : ready_(false) { | |
| 54 } | |
| 55 | |
| 56 GCMDriverDesktop::DelayedTaskController::~DelayedTaskController() { | |
| 57 } | |
| 58 | |
| 59 void GCMDriverDesktop::DelayedTaskController::AddTask( | |
| 60 const base::Closure& task) { | |
| 61 delayed_tasks_.push_back(task); | |
| 62 } | |
| 63 | |
| 64 void GCMDriverDesktop::DelayedTaskController::SetReady() { | |
| 65 ready_ = true; | |
| 66 RunTasks(); | |
| 67 } | |
| 68 | |
| 69 bool GCMDriverDesktop::DelayedTaskController::CanRunTaskWithoutDelay() const { | |
| 70 return ready_; | |
| 71 } | |
| 72 | |
| 73 void GCMDriverDesktop::DelayedTaskController::RunTasks() { | |
| 74 DCHECK(ready_); | |
| 75 | |
| 76 for (size_t i = 0; i < delayed_tasks_.size(); ++i) | |
| 77 delayed_tasks_[i].Run(); | |
| 78 delayed_tasks_.clear(); | |
| 79 } | |
| 80 | |
| 81 class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { | 26 class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { |
| 82 public: | 27 public: |
| 83 // Called on UI thread. | 28 // Called on UI thread. |
| 84 IOWorker(const scoped_refptr<base::SequencedTaskRunner>& ui_thread, | 29 IOWorker(const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 85 const scoped_refptr<base::SequencedTaskRunner>& io_thread); | 30 const scoped_refptr<base::SequencedTaskRunner>& io_thread); |
| 86 virtual ~IOWorker(); | 31 virtual ~IOWorker(); |
| 87 | 32 |
| 88 // Overridden from GCMClient::Delegate: | 33 // Overridden from GCMClient::Delegate: |
| 89 // Called on IO thread. | 34 // Called on IO thread. |
| 90 virtual void OnRegisterFinished(const std::string& app_id, | 35 virtual void OnRegisterFinished(const std::string& app_id, |
| (...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 | 614 |
| 670 // Have any app requested the service? | 615 // Have any app requested the service? |
| 671 if (app_handlers().empty()) | 616 if (app_handlers().empty()) |
| 672 return GCMClient::UNKNOWN_ERROR; | 617 return GCMClient::UNKNOWN_ERROR; |
| 673 | 618 |
| 674 // TODO(jianli): To be removed when sign-in enforcement is dropped. | 619 // TODO(jianli): To be removed when sign-in enforcement is dropped. |
| 675 if (!signed_in_ && !GCMDriver::IsAllowedForAllUsers()) | 620 if (!signed_in_ && !GCMDriver::IsAllowedForAllUsers()) |
| 676 return GCMClient::NOT_SIGNED_IN; | 621 return GCMClient::NOT_SIGNED_IN; |
| 677 | 622 |
| 678 DCHECK(!delayed_task_controller_); | 623 DCHECK(!delayed_task_controller_); |
| 679 delayed_task_controller_.reset(new DelayedTaskController); | 624 delayed_task_controller_.reset(new GCMDelayedTaskController); |
| 680 | 625 |
| 681 // Note that we need to pass weak pointer again since the existing weak | 626 // Note that we need to pass weak pointer again since the existing weak |
| 682 // pointer in IOWorker might have been invalidated when check-out occurs. | 627 // pointer in IOWorker might have been invalidated when check-out occurs. |
| 683 io_thread_->PostTask( | 628 io_thread_->PostTask( |
| 684 FROM_HERE, | 629 FROM_HERE, |
| 685 base::Bind(&GCMDriverDesktop::IOWorker::Start, | 630 base::Bind(&GCMDriverDesktop::IOWorker::Start, |
| 686 base::Unretained(io_worker_.get()), | 631 base::Unretained(io_worker_.get()), |
| 687 weak_ptr_factory_.GetWeakPtr())); | 632 weak_ptr_factory_.GetWeakPtr())); |
| 688 | 633 |
| 689 gcm_started_ = true; | 634 gcm_started_ = true; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 785 | 730 |
| 786 // Normally request_gcm_statistics_callback_ would not be null. | 731 // Normally request_gcm_statistics_callback_ would not be null. |
| 787 if (!request_gcm_statistics_callback_.is_null()) | 732 if (!request_gcm_statistics_callback_.is_null()) |
| 788 request_gcm_statistics_callback_.Run(stats); | 733 request_gcm_statistics_callback_.Run(stats); |
| 789 else | 734 else |
| 790 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 735 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
| 791 } | 736 } |
| 792 | 737 |
| 793 } // namespace gcm | 738 } // namespace gcm |
| 794 | 739 |
| OLD | NEW |