Fix link in German terms of service.
[chromium-blink-merge.git] / content / child / power_monitor_broadcast_source.cc
blob061890a060d671728f06271326d04f8339256bfc
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"
13 namespace content {
15 class PowerMessageFilter : public IPC::MessageFilter {
16 public:
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 {
22 bool handled = true;
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)
28 IPC_END_MESSAGE_MAP()
29 return handled;
32 void RemoveSource() {
33 source_ = NULL;
36 private:
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));
46 void OnSuspend() {
47 task_runner_->PostTask(
48 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceSuspend, this));
50 void OnResume() {
51 task_runner_->PostTask(
52 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceResume, this));
55 void NotifySourcePowerStateChange(bool on_battery_power) {
56 if (source_)
57 source_->OnPowerStateChange(on_battery_power);
59 void NotifySourceSuspend() {
60 if (source_)
61 source_->OnSuspend();
63 void NotifySourceResume() {
64 if (source_)
65 source_->OnResume();
68 // source_ should only be accessed on the thread associated with
69 // message_loop_.
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) {
78 message_filter_ =
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