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"
9 #include "ipc/message_filter.h"
13 class PowerMessageFilter
: public IPC::MessageFilter
{
16 PowerMonitorBroadcastSource
* source
,
17 scoped_refptr
<base::MessageLoopProxy
> message_loop
)
19 message_loop_(message_loop
) {
22 bool OnMessageReceived(const IPC::Message
& message
) override
{
24 IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter
, message
)
25 IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange
, OnPowerStateChange
)
26 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend
, OnSuspend
)
27 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume
, OnResume
)
28 IPC_MESSAGE_UNHANDLED(handled
= false)
38 friend class base::RefCounted
<PowerMessageFilter
>;
40 ~PowerMessageFilter() override
{};
42 void OnPowerStateChange(bool on_battery_power
) {
43 message_loop_
->PostTask(FROM_HERE
,
44 base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange
, this,
48 message_loop_
->PostTask(FROM_HERE
,
49 base::Bind(&PowerMessageFilter::NotifySourceSuspend
, this));
52 message_loop_
->PostTask(FROM_HERE
,
53 base::Bind(&PowerMessageFilter::NotifySourceResume
, this));
56 void NotifySourcePowerStateChange(bool on_battery_power
) {
58 source_
->OnPowerStateChange(on_battery_power
);
60 void NotifySourceSuspend() {
64 void NotifySourceResume() {
69 // source_ should only be accessed on the thread associated with
71 PowerMonitorBroadcastSource
* source_
;
72 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
74 DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter
);
77 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
78 : last_reported_battery_power_state_(false) {
79 message_filter_
= new PowerMessageFilter(this,
80 base::MessageLoopProxy::current());
83 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
84 message_filter_
->RemoveSource();
87 IPC::MessageFilter
* 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