OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/common/power_monitor_broadcast_source.h" | |
6 | |
7 #include "content/common/power_monitor_messages.h" | |
8 | |
9 namespace content { | |
10 | |
11 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource() | |
12 : last_reported_battery_power_state_(false), | |
13 weak_factory_(this) { | |
14 message_filter_ = new PowerMessageFilter(weak_factory_.GetWeakPtr()); | |
15 } | |
16 | |
17 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { | |
18 } | |
19 | |
20 // MessageFilters are owned by the Channel they are added to, so this class does | |
21 // not need to retain a reference. | |
22 IPC::ChannelProxy::MessageFilter* | |
23 PowerMonitorBroadcastSource::MessageFilter() { | |
24 return message_filter_.get(); | |
25 } | |
26 | |
27 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() { | |
28 return last_reported_battery_power_state_; | |
29 } | |
30 | |
31 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) { | |
32 last_reported_battery_power_state_ = on_battery_power; | |
33 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); | |
34 } | |
35 | |
36 void PowerMonitorBroadcastSource::OnSuspend() { | |
37 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); | |
38 } | |
39 | |
40 void PowerMonitorBroadcastSource::OnResume() { | |
41 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); | |
42 } | |
43 | |
44 PowerMonitorBroadcastSource::PowerMessageFilter::PowerMessageFilter( | |
45 base::WeakPtr<PowerMonitorBroadcastSource> source) : source_(source) { | |
46 } | |
47 | |
48 PowerMonitorBroadcastSource::PowerMessageFilter::~PowerMessageFilter() { | |
49 } | |
50 | |
51 bool PowerMonitorBroadcastSource::PowerMessageFilter::OnMessageReceived( | |
52 const IPC::Message& message) { | |
53 bool msg_is_ok = false; | |
54 bool handled = true; | |
55 IPC_BEGIN_MESSAGE_MAP_EX(PowerMessageFilter, message, msg_is_ok) | |
56 IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange, OnPowerStateChange) | |
57 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend, OnSuspend) | |
58 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume, OnResume) | |
59 IPC_MESSAGE_UNHANDLED(handled = false) | |
60 IPC_END_MESSAGE_MAP_EX() | |
61 return handled && msg_is_ok; | |
62 } | |
63 | |
64 void PowerMonitorBroadcastSource::PowerMessageFilter::OnPowerStateChange( | |
65 bool on_battery_power) { | |
66 if (source_.get()) | |
piman
2013/07/12 21:09:58
The weak reference gets destructed on the main thr
| |
67 source_->OnPowerStateChange(on_battery_power); | |
68 } | |
69 | |
70 void PowerMonitorBroadcastSource::PowerMessageFilter::OnSuspend() { | |
71 if (source_.get()) | |
72 source_->OnSuspend(); | |
73 } | |
74 | |
75 void PowerMonitorBroadcastSource::PowerMessageFilter::OnResume() { | |
76 if (source_.get()) | |
77 source_->OnResume(); | |
78 } | |
79 | |
80 } // namespace content | |
OLD | NEW |