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/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "content/common/power_monitor_messages.h"
11 #include "ipc/message_filter.h"
15 class PowerMessageFilter
: public IPC::MessageFilter
{
17 PowerMessageFilter(PowerMonitorBroadcastSource
* source
,
18 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
19 : source_(source
), task_runner_(task_runner
) {}
21 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 ~PowerMessageFilter() override
{};
41 void OnPowerStateChange(bool on_battery_power
) {
42 task_runner_
->PostTask(
43 FROM_HERE
, base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange
,
44 this, on_battery_power
));
47 task_runner_
->PostTask(
48 FROM_HERE
, base::Bind(&PowerMessageFilter::NotifySourceSuspend
, this));
51 task_runner_
->PostTask(
52 FROM_HERE
, 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::SingleThreadTaskRunner
> task_runner_
;
73 DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter
);
76 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
77 : last_reported_battery_power_state_(false) {
79 new PowerMessageFilter(this, base::ThreadTaskRunnerHandle::Get());
82 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
83 message_filter_
->RemoveSource();
86 IPC::MessageFilter
* PowerMonitorBroadcastSource::GetMessageFilter() {
87 return message_filter_
.get();
90 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() {
91 return last_reported_battery_power_state_
;
94 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power
) {
95 last_reported_battery_power_state_
= on_battery_power
;
96 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT
);
99 void PowerMonitorBroadcastSource::OnSuspend() {
100 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT
);
103 void PowerMonitorBroadcastSource::OnResume() {
104 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT
);
107 } // namespace content