Chromium Code Reviews| Index: components/gcm_driver/gcm_driver_desktop.cc |
| diff --git a/components/gcm_driver/gcm_driver_desktop.cc b/components/gcm_driver/gcm_driver_desktop.cc |
| index b2ab93ae355226a065eeadfd477a0eef433ab262..13d1bfd94cee456710b75993ce20fad4ea15526a 100644 |
| --- a/components/gcm_driver/gcm_driver_desktop.cc |
| +++ b/components/gcm_driver/gcm_driver_desktop.cc |
| @@ -24,6 +24,12 @@ |
| #include "net/base/ip_endpoint.h" |
| #include "net/url_request/url_request_context_getter.h" |
| +#if defined(OS_CHROMEOS) |
| +#include "components/timers/alarm_timer.h" |
| +#include "google_apis/gcm/engine/heartbeat_manager.h" |
| +#include "google_apis/gcm/engine/mcs_client.h" |
| +#endif |
| + |
| namespace gcm { |
| class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { |
| @@ -81,6 +87,7 @@ class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { |
| void UpdateAccountMapping(const AccountMapping& account_mapping); |
| void RemoveAccountMapping(const std::string& account_id); |
| void SetLastTokenFetchTime(const base::Time& time); |
| + void WakeFromSuspendForHeartbeat(bool wake); |
| // For testing purpose. Can be called from UI thread. Use with care. |
| GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } |
| @@ -341,6 +348,22 @@ void GCMDriverDesktop::IOWorker::SetLastTokenFetchTime(const base::Time& time) { |
| gcm_client_->SetLastTokenFetchTime(time); |
| } |
| +void GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat(bool wake) { |
| +#if defined(OS_CHROMEOS) |
| + DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
| + DCHECK(gcm_client_->GetMCSClient()); |
| + |
| + scoped_ptr<base::Timer> timer; |
| + if (wake) |
| + timer.reset(new timers::AlarmTimer(true, false)); |
| + else |
| + timer.reset(new base::Timer(true, false)); |
| + |
| + gcm_client_->GetMCSClient()->heartbeat_manager()->UpdateHeartbeatTimer( |
| + timer.Pass()); |
| +#endif |
| +} |
| + |
| GCMDriverDesktop::GCMDriverDesktop( |
| scoped_ptr<GCMClientFactory> gcm_client_factory, |
| const GCMClient::ChromeBuildInfo& chrome_build_info, |
| @@ -659,6 +682,38 @@ void GCMDriverDesktop::SetLastTokenFetchTime(const base::Time& time) { |
| time)); |
| } |
| +void GCMDriverDesktop::WakeFromSuspendForHeartbeat(bool wake) { |
| + DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| + |
| + if (!IsStarted()) { |
| + // The GCM service has not been started yet, either because the user has not |
| + // signed in or because no apps/extensions are using GCM. Queue this task |
| + // to run whenever GCM does start up. |
| + gcm_started_callback_ = |
|
Nicolas Zea
2014/12/05 00:53:05
I don't think we need a generic callback here unti
Chirantan Ekbote
2014/12/05 21:49:05
Done.
|
| + base::Bind(&GCMDriverDesktop::WakeFromSuspendForHeartbeat, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + wake); |
| + return; |
| + } |
| + |
| + if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { |
| + // The GCM service has started but the client is not ready yet. |
| + delayed_task_controller_->AddTask( |
| + base::Bind(&GCMDriverDesktop::WakeFromSuspendForHeartbeat, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + wake)); |
| + return; |
| + } |
| + |
| + // The GCMClient is ready so we can go ahead and post this task to the |
| + // IOWorker. |
| + io_thread_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&GCMDriverDesktop::IOWorker::WakeFromSuspendForHeartbeat, |
| + base::Unretained(io_worker_.get()), |
| + wake)); |
| +} |
| + |
| void GCMDriverDesktop::SetAccountTokens( |
| const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
| DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
| @@ -714,6 +769,11 @@ GCMClient::Result GCMDriverDesktop::EnsureStarted() { |
| weak_ptr_factory_.GetWeakPtr())); |
| gcm_started_ = true; |
| + if (!gcm_started_callback_.is_null()) { |
| + gcm_started_callback_.Run(); |
| + gcm_started_callback_.Reset(); |
| + } |
| + |
| return GCMClient::SUCCESS; |
| } |
| @@ -825,4 +885,3 @@ void GCMDriverDesktop::GetGCMStatisticsFinished( |
| } |
| } // namespace gcm |
| - |