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/location.h" | 7 #include "base/location.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "mojo/public/cpp/bindings/binding.h" |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "content/common/power_monitor_messages.h" | |
12 #include "ipc/message_filter.h" | |
13 | 10 |
14 namespace content { | 11 namespace content { |
15 | 12 |
16 class PowerMessageFilter : public IPC::MessageFilter { | 13 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource() |
blundell
2016/10/20 22:42:22
There's a threading change here I think: Before th
| |
17 public: | 14 : last_reported_battery_power_state_(false), binding_(this) {} |
18 PowerMessageFilter(PowerMonitorBroadcastSource* source, | |
19 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
20 : source_(source), task_runner_(task_runner) {} | |
21 | 15 |
22 bool OnMessageReceived(const IPC::Message& message) override { | 16 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { |
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; | |
31 } | |
32 | |
33 void RemoveSource() { | |
34 source_ = NULL; | |
35 } | |
36 | |
37 private: | |
38 friend class base::RefCounted<PowerMessageFilter>; | |
39 | |
40 ~PowerMessageFilter() override{}; | |
41 | |
42 void OnPowerStateChange(bool on_battery_power) { | |
43 task_runner_->PostTask( | |
44 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange, | |
45 this, on_battery_power)); | |
46 } | |
47 void OnSuspend() { | |
48 task_runner_->PostTask( | |
49 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceSuspend, this)); | |
50 } | |
51 void OnResume() { | |
52 task_runner_->PostTask( | |
53 FROM_HERE, base::Bind(&PowerMessageFilter::NotifySourceResume, this)); | |
54 } | |
55 | |
56 void NotifySourcePowerStateChange(bool on_battery_power) { | |
57 if (source_) | |
58 source_->OnPowerStateChange(on_battery_power); | |
59 } | |
60 void NotifySourceSuspend() { | |
61 if (source_) | |
62 source_->OnSuspend(); | |
63 } | |
64 void NotifySourceResume() { | |
65 if (source_) | |
66 source_->OnResume(); | |
67 } | |
68 | |
69 // source_ should only be accessed on the thread associated with | |
70 // message_loop_. | |
71 PowerMonitorBroadcastSource* source_; | |
72 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter); | |
75 }; | |
76 | |
77 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource() | |
78 : last_reported_battery_power_state_(false) { | |
79 message_filter_ = | |
80 new PowerMessageFilter(this, base::ThreadTaskRunnerHandle::Get()); | |
81 } | 17 } |
82 | 18 |
83 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { | 19 void PowerMonitorBroadcastSource::BindRequest( |
84 message_filter_->RemoveSource(); | 20 device::mojom::PowerMonitorClientRequest request) { |
85 } | 21 binding_.Bind(std::move(request)); |
86 | |
87 IPC::MessageFilter* PowerMonitorBroadcastSource::GetMessageFilter() { | |
88 return message_filter_.get(); | |
89 } | 22 } |
90 | 23 |
91 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() { | 24 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() { |
92 return last_reported_battery_power_state_; | 25 return last_reported_battery_power_state_; |
93 } | 26 } |
94 | 27 |
95 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) { | 28 void PowerMonitorBroadcastSource::PowerStateChange(bool on_battery_power) { |
96 last_reported_battery_power_state_ = on_battery_power; | 29 last_reported_battery_power_state_ = on_battery_power; |
97 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); | 30 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); |
98 } | 31 } |
99 | 32 |
100 void PowerMonitorBroadcastSource::OnSuspend() { | 33 void PowerMonitorBroadcastSource::Suspend() { |
101 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); | 34 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); |
102 } | 35 } |
103 | 36 |
104 void PowerMonitorBroadcastSource::OnResume() { | 37 void PowerMonitorBroadcastSource::Resume() { |
105 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); | 38 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); |
106 } | 39 } |
107 | 40 |
108 } // namespace content | 41 } // namespace content |
OLD | NEW |