Index: content/common/power_monitor_broadcast_source.cc |
diff --git a/content/common/power_monitor_broadcast_source.cc b/content/common/power_monitor_broadcast_source.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0055af55f933ea0fa2fa36f139425bccfc406573 |
--- /dev/null |
+++ b/content/common/power_monitor_broadcast_source.cc |
@@ -0,0 +1,90 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/common/power_monitor_broadcast_source.h" |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "content/common/power_monitor_messages.h" |
+ |
+namespace content { |
+ |
+PowerMonitorBroadcastSource::PowerMonitorBroadcastSource( |
+ scoped_refptr<base::MessageLoopProxy> message_loop) |
+ : on_battery_power_(false), |
+ message_filter_(new PowerMessageFilter(this)), |
+ message_loop_(message_loop), |
+ weak_factory_(this) { |
+} |
+ |
+PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { |
+} |
+ |
+// MessageFilters are owned by the Channel they are added to, so this class does |
+// not need to retain a reference. |
+IPC::ChannelProxy::MessageFilter* |
+PowerMonitorBroadcastSource::MessageFilter() { |
+ return message_filter_.get(); |
+} |
+ |
+bool PowerMonitorBroadcastSource::IsBatteryPower() { |
+ return on_battery_power_; |
+} |
+ |
+void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) { |
+ on_battery_power_ = on_battery_power; |
Ken Russell (switch to Gerrit)
2013/07/03 09:49:43
Is synchronization needed around this variable? It
|
+ message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&PowerMonitorBroadcastSource::ProcessPowerEvent, |
+ weak_factory_.GetWeakPtr(), PowerMonitorSource::POWER_STATE_EVENT)); |
+ //ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); |
Ken Russell (switch to Gerrit)
2013/07/03 09:49:43
Here and below: remove commented out code.
|
+} |
+ |
+void PowerMonitorBroadcastSource::OnSuspend() { |
+ message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&PowerMonitorBroadcastSource::ProcessPowerEvent, |
+ weak_factory_.GetWeakPtr(), PowerMonitorSource::SUSPEND_EVENT)); |
+ //ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); |
+} |
+ |
+void PowerMonitorBroadcastSource::OnResume() { |
+ message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&PowerMonitorBroadcastSource::ProcessPowerEvent, |
+ weak_factory_.GetWeakPtr(), PowerMonitorSource::RESUME_EVENT)); |
+ //ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); |
+} |
+ |
+PowerMonitorBroadcastSource::PowerMessageFilter::PowerMessageFilter( |
+ PowerMonitorBroadcastSource* source) : source_(source) { |
+} |
+ |
+PowerMonitorBroadcastSource::PowerMessageFilter::~PowerMessageFilter() { |
+} |
+ |
+bool PowerMonitorBroadcastSource::PowerMessageFilter::OnMessageReceived( |
+ const IPC::Message& message) { |
+ bool msg_is_ok = true; |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP_EX(PowerMessageFilter, message, msg_is_ok) |
+ IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange, OnPowerStateChange) |
+ IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend, OnSuspend) |
+ IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume, OnResume) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP_EX() |
+ return handled; |
+} |
+ |
+void PowerMonitorBroadcastSource::PowerMessageFilter::OnPowerStateChange( |
+ bool on_battery_power) { |
+ source_->OnPowerStateChange(on_battery_power); |
+} |
+ |
+void PowerMonitorBroadcastSource::PowerMessageFilter::OnSuspend() { |
+ source_->OnSuspend(); |
+} |
+ |
+void PowerMonitorBroadcastSource::PowerMessageFilter::OnResume() { |
+ source_->OnResume(); |
+} |
+ |
+} // namespace content |