Index: content/child/power_monitor_broadcast_source.cc |
diff --git a/content/child/power_monitor_broadcast_source.cc b/content/child/power_monitor_broadcast_source.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8ef6b8979345bce576220fd1ae4244a9a68d867f |
--- /dev/null |
+++ b/content/child/power_monitor_broadcast_source.cc |
@@ -0,0 +1,89 @@ |
+// 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/child/power_monitor_broadcast_source.h" |
+ |
+#include "content/common/power_monitor_messages.h" |
+ |
+namespace content { |
+ |
+class PowerMessageFilter : public IPC::ChannelProxy::MessageFilter { |
+ public: |
+ PowerMessageFilter( |
+ base::WeakPtr<PowerMonitorBroadcastSource> source, |
+ scoped_refptr<base::MessageLoopProxy> message_loop) |
+ : source_(source), |
+ message_loop_(message_loop) { |
+ } |
+ |
+ virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter, message) |
+ 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() |
+ return handled; |
+ } |
+ |
+ private: |
+ friend class base::RefCounted<PowerMessageFilter>; |
+ |
+ virtual ~PowerMessageFilter() {}; |
+ |
+ void OnPowerStateChange(bool on_battery_power) { |
+ message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&PowerMonitorBroadcastSource::OnPowerStateChange, source_, |
+ on_battery_power)); |
jam
2013/07/31 17:27:17
I didn't know this works?
http://www.chromium.org
piman
2013/08/01 18:41:01
As long as you only dereference the weakptr on the
|
+ } |
+ void OnSuspend() { |
+ message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&PowerMonitorBroadcastSource::OnSuspend, source_)); |
+ } |
+ void OnResume() { |
+ message_loop_->PostTask(FROM_HERE, |
+ base::Bind(&PowerMonitorBroadcastSource::OnResume, source_)); |
+ } |
+ |
+ base::WeakPtr<PowerMonitorBroadcastSource> source_; |
+ scoped_refptr<base::MessageLoopProxy> message_loop_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter); |
+}; |
+ |
+PowerMonitorBroadcastSource::PowerMonitorBroadcastSource( |
+ scoped_refptr<base::MessageLoopProxy> message_loop) |
+ : last_reported_battery_power_state_(false), |
+ weak_factory_(this) { |
+ message_filter_ = new PowerMessageFilter(weak_factory_.GetWeakPtr(), |
+ message_loop); |
+} |
+ |
+PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { |
+} |
+ |
+IPC::ChannelProxy::MessageFilter* |
+PowerMonitorBroadcastSource::GetMessageFilter() { |
+ return message_filter_.get(); |
+} |
+ |
+bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() { |
+ return last_reported_battery_power_state_; |
+} |
+ |
+void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) { |
+ last_reported_battery_power_state_ = on_battery_power; |
+ ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); |
+} |
+ |
+void PowerMonitorBroadcastSource::OnSuspend() { |
+ ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); |
+} |
+ |
+void PowerMonitorBroadcastSource::OnResume() { |
+ ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); |
+} |
+ |
+} // namespace content |