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..99c8afab6e5a0a8671dcea74dc974dc0ea1c6fc8 100644 |
--- a/components/gcm_driver/gcm_driver_desktop.cc |
+++ b/components/gcm_driver/gcm_driver_desktop.cc |
@@ -24,6 +24,10 @@ |
#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" |
+#endif |
+ |
namespace gcm { |
class GCMDriverDesktop::IOWorker : public GCMClient::Delegate { |
@@ -81,6 +85,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 +346,20 @@ 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()); |
+ |
+ scoped_ptr<base::Timer> timer; |
+ if (wake) |
+ timer.reset(new timers::AlarmTimer(true, false)); |
+ else |
+ timer.reset(new base::Timer(true, false)); |
+ |
+ gcm_client_->UpdateHeartbeatTimer(timer.Pass()); |
+#endif |
+} |
+ |
GCMDriverDesktop::GCMDriverDesktop( |
scoped_ptr<GCMClientFactory> gcm_client_factory, |
const GCMClient::ChromeBuildInfo& chrome_build_info, |
@@ -369,6 +388,7 @@ GCMDriverDesktop::GCMDriverDesktop( |
last_token_fetch_time_(base::Time::Max()), |
ui_thread_(ui_thread), |
io_thread_(io_thread), |
+ wake_from_suspend_enabled_(false), |
weak_ptr_factory_(this) { |
gcm_enabled_ = gcm_channel_status_syncer_->gcm_enabled(); |
@@ -659,6 +679,31 @@ void GCMDriverDesktop::SetLastTokenFetchTime(const base::Time& time) { |
time)); |
} |
+void GCMDriverDesktop::WakeFromSuspendForHeartbeat(bool wake) { |
+ DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
+ |
+ wake_from_suspend_enabled_ = wake; |
+ if (!IsStarted()) |
+ 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_from_suspend_enabled_)); |
+ 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_from_suspend_enabled_)); |
+} |
+ |
void GCMDriverDesktop::SetAccountTokens( |
const std::vector<GCMClient::AccountTokenInfo>& account_tokens) { |
DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
@@ -714,6 +759,9 @@ GCMClient::Result GCMDriverDesktop::EnsureStarted() { |
weak_ptr_factory_.GetWeakPtr())); |
gcm_started_ = true; |
+ if (wake_from_suspend_enabled_) |
+ WakeFromSuspendForHeartbeat(wake_from_suspend_enabled_); |
+ |
return GCMClient::SUCCESS; |
} |
@@ -825,4 +873,3 @@ void GCMDriverDesktop::GetGCMStatisticsFinished( |
} |
} // namespace gcm |
- |