1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/child/power_monitor_broadcast_source.h"
7 #include "base/message_loop/message_loop.h"
8 #include "content/common/power_monitor_messages.h"
12 class PowerMessageFilter
: public IPC::ChannelProxy::MessageFilter
{
15 PowerMonitorBroadcastSource
* source
,
16 scoped_refptr
<base::MessageLoopProxy
> message_loop
)
18 message_loop_(message_loop
) {
21 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
{
23 IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter
, message
)
24 IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange
, OnPowerStateChange
)
25 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend
, OnSuspend
)
26 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume
, OnResume
)
27 IPC_MESSAGE_UNHANDLED(handled
= false)
37 friend class base::RefCounted
<PowerMessageFilter
>;
39 virtual ~PowerMessageFilter() {};
41 void OnPowerStateChange(bool on_battery_power
) {
42 message_loop_
->PostTask(FROM_HERE
,
43 base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange
, this,
47 message_loop_
->PostTask(FROM_HERE
,
48 base::Bind(&PowerMessageFilter::NotifySourceSuspend
, this));
51 message_loop_
->PostTask(FROM_HERE
,
52 base::Bind(&PowerMessageFilter::NotifySourceResume
, this));
55 void NotifySourcePowerStateChange(bool on_battery_power
) {
57 source_
->OnPowerStateChange(on_battery_power
);
59 void NotifySourceSuspend() {
63 void NotifySourceResume() {
68 // source_ should only be accessed on the thread associated with
70 PowerMonitorBroadcastSource
* source_
;
71 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
73 DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter
);
76 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
77 : last_reported_battery_power_state_(false) {
78 message_filter_
= new PowerMessageFilter(this,
79 base::MessageLoopProxy::current());
82 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
83 message_filter_
->RemoveSource();
86 IPC::ChannelProxy::MessageFilter
*
87 PowerMonitorBroadcastSource::GetMessageFilter() {
88 return message_filter_
.get();
91 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() {
92 return last_reported_battery_power_state_
;
95 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power
) {
96 last_reported_battery_power_state_
= on_battery_power
;
97 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT
);
100 void PowerMonitorBroadcastSource::OnSuspend() {
101 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT
);
104 void PowerMonitorBroadcastSource::OnResume() {
105 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT
);
108 } // namespace content