Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / child / power_monitor_broadcast_source.cc
blob0fa58897202751b1cedf13b5ed5b0f1371b0d1d5
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"
11 namespace content {
13 class PowerMessageFilter : public IPC::MessageFilter {
14 public:
15 PowerMessageFilter(
16 PowerMonitorBroadcastSource* source,
17 scoped_refptr<base::MessageLoopProxy> message_loop)
18 : source_(source),
19 message_loop_(message_loop) {
22 bool OnMessageReceived(const IPC::Message& message) override {
23 bool handled = true;
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)
29 IPC_END_MESSAGE_MAP()
30 return handled;
33 void RemoveSource() {
34 source_ = NULL;
37 private:
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,
45 on_battery_power));
47 void OnSuspend() {
48 message_loop_->PostTask(FROM_HERE,
49 base::Bind(&PowerMessageFilter::NotifySourceSuspend, this));
51 void OnResume() {
52 message_loop_->PostTask(FROM_HERE,
53 base::Bind(&PowerMessageFilter::NotifySourceResume, this));
56 void NotifySourcePowerStateChange(bool on_battery_power) {
57 if (source_)
58 source_->OnPowerStateChange(on_battery_power);
60 void NotifySourceSuspend() {
61 if (source_)
62 source_->OnSuspend();
64 void NotifySourceResume() {
65 if (source_)
66 source_->OnResume();
69 // source_ should only be accessed on the thread associated with
70 // message_loop_.
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