• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/message_loop/message_loop.h"
8 #include "content/common/power_monitor_messages.h"
9 
10 namespace content {
11 
12 class PowerMessageFilter : public IPC::ChannelProxy::MessageFilter {
13  public:
PowerMessageFilter(PowerMonitorBroadcastSource * source,scoped_refptr<base::MessageLoopProxy> message_loop)14   PowerMessageFilter(
15       PowerMonitorBroadcastSource* source,
16       scoped_refptr<base::MessageLoopProxy> message_loop)
17       : source_(source),
18         message_loop_(message_loop) {
19   }
20 
OnMessageReceived(const IPC::Message & message)21   virtual 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;
30   }
31 
RemoveSource()32   void RemoveSource() {
33     source_ = NULL;
34   }
35 
36  private:
37   friend class base::RefCounted<PowerMessageFilter>;
38 
~PowerMessageFilter()39   virtual ~PowerMessageFilter() {};
40 
OnPowerStateChange(bool on_battery_power)41   void OnPowerStateChange(bool on_battery_power) {
42     message_loop_->PostTask(FROM_HERE,
43         base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange, this,
44             on_battery_power));
45   }
OnSuspend()46   void OnSuspend() {
47     message_loop_->PostTask(FROM_HERE,
48         base::Bind(&PowerMessageFilter::NotifySourceSuspend, this));
49   }
OnResume()50   void OnResume() {
51     message_loop_->PostTask(FROM_HERE,
52         base::Bind(&PowerMessageFilter::NotifySourceResume, this));
53   }
54 
NotifySourcePowerStateChange(bool on_battery_power)55   void NotifySourcePowerStateChange(bool on_battery_power) {
56     if (source_)
57       source_->OnPowerStateChange(on_battery_power);
58   }
NotifySourceSuspend()59   void NotifySourceSuspend() {
60     if (source_)
61       source_->OnSuspend();
62   }
NotifySourceResume()63   void NotifySourceResume() {
64      if (source_)
65       source_->OnResume();
66   }
67 
68   // source_ should only be accessed on the thread associated with
69   // message_loop_.
70   PowerMonitorBroadcastSource* source_;
71   scoped_refptr<base::MessageLoopProxy> message_loop_;
72 
73   DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter);
74 };
75 
PowerMonitorBroadcastSource()76 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
77     : last_reported_battery_power_state_(false) {
78   message_filter_ = new PowerMessageFilter(this,
79       base::MessageLoopProxy::current());
80 }
81 
~PowerMonitorBroadcastSource()82 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
83   message_filter_->RemoveSource();
84 }
85 
86 IPC::ChannelProxy::MessageFilter*
GetMessageFilter()87 PowerMonitorBroadcastSource::GetMessageFilter() {
88   return message_filter_.get();
89 }
90 
IsOnBatteryPowerImpl()91 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() {
92   return last_reported_battery_power_state_;
93 }
94 
OnPowerStateChange(bool on_battery_power)95 void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) {
96   last_reported_battery_power_state_ = on_battery_power;
97   ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT);
98 }
99 
OnSuspend()100 void PowerMonitorBroadcastSource::OnSuspend() {
101   ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT);
102 }
103 
OnResume()104 void PowerMonitorBroadcastSource::OnResume() {
105   ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT);
106 }
107 
108 }  // namespace content
109