OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/power_monitor_broadcast_source.h" | 5 #include "content/child/power_monitor_broadcast_source.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/location.h" |
| 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/thread_task_runner_handle.h" |
8 #include "content/common/power_monitor_messages.h" | 10 #include "content/common/power_monitor_messages.h" |
9 #include "ipc/message_filter.h" | 11 #include "ipc/message_filter.h" |
10 | 12 |
11 namespace content { | 13 namespace content { |
12 | 14 |
13 class PowerMessageFilter : public IPC::MessageFilter { | 15 class PowerMessageFilter : public IPC::MessageFilter { |
14 public: | 16 public: |
15 PowerMessageFilter( | 17 PowerMessageFilter(PowerMonitorBroadcastSource* source, |
16 PowerMonitorBroadcastSource* source, | 18 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
17 scoped_refptr<base::MessageLoopProxy> message_loop) | 19 : source_(source), task_runner_(task_runner) {} |
18 : source_(source), | |
19 message_loop_(message_loop) { | |
20 } | |
21 | 20 |
22 bool OnMessageReceived(const IPC::Message& message) override { | 21 bool OnMessageReceived(const IPC::Message& message) override { |
23 bool handled = true; | 22 bool handled = true; |
24 IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter, message) | 23 IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter, message) |
25 IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange, OnPowerStateChange) | 24 IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange, OnPowerStateChange) |
26 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend, OnSuspend) | 25 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend, OnSuspend) |
27 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume, OnResume) | 26 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume, OnResume) |
28 IPC_MESSAGE_UNHANDLED(handled = false) | 27 IPC_MESSAGE_UNHANDLED(handled = false) |
29 IPC_END_MESSAGE_MAP() | 28 IPC_END_MESSAGE_MAP() |
30 return handled; | 29 return handled; |
31 } | 30 } |
32 | 31 |
33 void RemoveSource() { | 32 void RemoveSource() { |
34 source_ = NULL; | 33 source_ = NULL; |
35 } | 34 } |
36 | 35 |
37 private: | 36 private: |
38 friend class base::RefCounted<PowerMessageFilter>; | 37 friend class base::RefCounted<PowerMessageFilter>; |
39 | 38 |
40 ~PowerMessageFilter() override{}; | 39 ~PowerMessageFilter() override{}; |
41 | 40 |
42 void OnPowerStateChange(bool on_battery_power) { | 41 void OnPowerStateChange(bool on_battery_power) { |
43 message_loop_->PostTask(FROM_HERE, | 42 task_runner_->PostTask( |
44 base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange, this, | 43 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange, |
45 on_battery_power)); | 44 this, on_battery_power)); |
46 } | 45 } |
47 void OnSuspend() { | 46 void OnSuspend() { |
48 message_loop_->PostTask(FROM_HERE, | 47 task_runner_->PostTask( |
49 base::Bind(&PowerMessageFilter::NotifySourceSuspend, this)); | 48 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceSuspend, this)); |
50 } | 49 } |
51 void OnResume() { | 50 void OnResume() { |
52 message_loop_->PostTask(FROM_HERE, | 51 task_runner_->PostTask( |
53 base::Bind(&PowerMessageFilter::NotifySourceResume, this)); | 52 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceResume, this)); |
54 } | 53 } |
55 | 54 |
56 void NotifySourcePowerStateChange(bool on_battery_power) { | 55 void NotifySourcePowerStateChange(bool on_battery_power) { |
57 if (source_) | 56 if (source_) |
58 source_->OnPowerStateChange(on_battery_power); | 57 source_->OnPowerStateChange(on_battery_power); |
59 } | 58 } |
60 void NotifySourceSuspend() { | 59 void NotifySourceSuspend() { |
61 if (source_) | 60 if (source_) |
62 source_->OnSuspend(); | 61 source_->OnSuspend(); |
63 } | 62 } |
64 void NotifySourceResume() { | 63 void NotifySourceResume() { |
65 if (source_) | 64 if (source_) |
66 source_->OnResume(); | 65 source_->OnResume(); |
67 } | 66 } |
68 | 67 |
69 // source_ should only be accessed on the thread associated with | 68 // source_ should only be accessed on the thread associated with |
70 // message_loop_. | 69 // message_loop_. |
71 PowerMonitorBroadcastSource* source_; | 70 PowerMonitorBroadcastSource* source_; |
72 scoped_refptr<base::MessageLoopProxy> message_loop_; | 71 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
73 | 72 |
74 DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter); | 73 DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter); |
75 }; | 74 }; |
76 | 75 |
77 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource() | 76 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource() |
78 : last_reported_battery_power_state_(false) { | 77 : last_reported_battery_power_state_(false) { |
79 message_filter_ = new PowerMessageFilter(this, | 78 message_filter_ = |
80 base::MessageLoopProxy::current()); | 79 new PowerMessageFilter(this, base::ThreadTaskRunnerHandle::Get()); |
81 } | 80 } |
82 | 81 |
83 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { | 82 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { |
84 message_filter_->RemoveSource(); | 83 message_filter_->RemoveSource(); |
85 } | 84 } |
86 | 85 |
87 IPC::MessageFilter* PowerMonitorBroadcastSource::GetMessageFilter() { | 86 IPC::MessageFilter* PowerMonitorBroadcastSource::GetMessageFilter() { |
88 return message_filter_.get(); | 87 return message_filter_.get(); |
89 } | 88 } |
90 | 89 |
91 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() { | 90 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() { |
92 return last_reported_battery_power_state_; | 91 return last_reported_battery_power_state_; |
93 } | 92 } |
94 | 93 |
95 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) { | 94 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) { |
96 last_reported_battery_power_state_ = on_battery_power; | 95 last_reported_battery_power_state_ = on_battery_power; |
97 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); | 96 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); |
98 } | 97 } |
99 | 98 |
100 void PowerMonitorBroadcastSource::OnSuspend() { | 99 void PowerMonitorBroadcastSource::OnSuspend() { |
101 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); | 100 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); |
102 } | 101 } |
103 | 102 |
104 void PowerMonitorBroadcastSource::OnResume() { | 103 void PowerMonitorBroadcastSource::OnResume() { |
105 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); | 104 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); |
106 } | 105 } |
107 | 106 |
108 } // namespace content | 107 } // namespace content |
OLD | NEW |