Chromium Code Reviews| 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/child/power_monitor_broadcast_source.h" | |
| 6 | |
| 7 #include "content/common/power_monitor_messages.h" | |
| 8 | |
| 9 namespace content { | |
| 10 | |
| 11 class PowerMessageFilter : public IPC::ChannelProxy::MessageFilter { | |
| 12 public: | |
| 13 PowerMessageFilter( | |
| 14 base::WeakPtr<PowerMonitorBroadcastSource> source, | |
| 15 scoped_refptr<base::MessageLoopProxy> message_loop) | |
| 16 : source_(source), | |
| 17 message_loop_(message_loop) { | |
| 18 } | |
| 19 | |
| 20 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | |
| 21 bool handled = true; | |
| 22 IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter, message) | |
| 23 IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange, OnPowerStateChange) | |
| 24 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend, OnSuspend) | |
| 25 IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume, OnResume) | |
| 26 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 27 IPC_END_MESSAGE_MAP() | |
| 28 return handled; | |
| 29 } | |
| 30 | |
| 31 private: | |
| 32 friend class base::RefCounted<PowerMessageFilter>; | |
| 33 | |
| 34 virtual ~PowerMessageFilter() {}; | |
| 35 | |
| 36 void OnPowerStateChange(bool on_battery_power) { | |
| 37 message_loop_->PostTask(FROM_HERE, | |
| 38 base::Bind(&PowerMonitorBroadcastSource::OnPowerStateChange, source_, | |
| 39 on_battery_power)); | |
|
jam
2013/07/31 17:27:17
I didn't know this works?
http://www.chromium.org
piman
2013/08/01 18:41:01
As long as you only dereference the weakptr on the
| |
| 40 } | |
| 41 void OnSuspend() { | |
| 42 message_loop_->PostTask(FROM_HERE, | |
| 43 base::Bind(&PowerMonitorBroadcastSource::OnSuspend, source_)); | |
| 44 } | |
| 45 void OnResume() { | |
| 46 message_loop_->PostTask(FROM_HERE, | |
| 47 base::Bind(&PowerMonitorBroadcastSource::OnResume, source_)); | |
| 48 } | |
| 49 | |
| 50 base::WeakPtr<PowerMonitorBroadcastSource> source_; | |
| 51 scoped_refptr<base::MessageLoopProxy> message_loop_; | |
| 52 | |
| 53 DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter); | |
| 54 }; | |
| 55 | |
| 56 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource( | |
| 57 scoped_refptr<base::MessageLoopProxy> message_loop) | |
| 58 : last_reported_battery_power_state_(false), | |
| 59 weak_factory_(this) { | |
| 60 message_filter_ = new PowerMessageFilter(weak_factory_.GetWeakPtr(), | |
| 61 message_loop); | |
| 62 } | |
| 63 | |
| 64 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() { | |
| 65 } | |
| 66 | |
| 67 IPC::ChannelProxy::MessageFilter* | |
| 68 PowerMonitorBroadcastSource::GetMessageFilter() { | |
| 69 return message_filter_.get(); | |
| 70 } | |
| 71 | |
| 72 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() { | |
| 73 return last_reported_battery_power_state_; | |
| 74 } | |
| 75 | |
| 76 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) { | |
| 77 last_reported_battery_power_state_ = on_battery_power; | |
| 78 ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT); | |
| 79 } | |
| 80 | |
| 81 void PowerMonitorBroadcastSource::OnSuspend() { | |
| 82 ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT); | |
| 83 } | |
| 84 | |
| 85 void PowerMonitorBroadcastSource::OnResume() { | |
| 86 ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT); | |
| 87 } | |
| 88 | |
| 89 } // namespace content | |
| OLD | NEW |