Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / child / power_monitor_broadcast_source.cc
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 #include "ipc/message_filter.h"
10
11 namespace content {
12
13 class PowerMessageFilter : public IPC::MessageFilter {
14  public:
15   PowerMessageFilter(
16       PowerMonitorBroadcastSource* source,
17       scoped_refptr<base::MessageLoopProxy> message_loop)
18       : source_(source),
19         message_loop_(message_loop) {
20   }
21
22   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
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   virtual ~PowerMessageFilter() {};
41
42   void OnPowerStateChange(bool on_battery_power) {
43     message_loop_->PostTask(FROM_HERE,
44         base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange, this,
45             on_battery_power));
46   }
47   void OnSuspend() {
48     message_loop_->PostTask(FROM_HERE,
49         base::Bind(&PowerMessageFilter::NotifySourceSuspend, this));
50   }
51   void OnResume() {
52     message_loop_->PostTask(FROM_HERE,
53         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::MessageLoopProxy> message_loop_;
73
74   DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter);
75 };
76
77 PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
78     : last_reported_battery_power_state_(false) {
79   message_filter_ = new PowerMessageFilter(this,
80       base::MessageLoopProxy::current());
81 }
82
83 PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
84   message_filter_->RemoveSource();
85 }
86
87 IPC::MessageFilter* PowerMonitorBroadcastSource::GetMessageFilter() {
88   return message_filter_.get();
89 }
90
91 bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() {
92   return last_reported_battery_power_state_;
93 }
94
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
100 void PowerMonitorBroadcastSource::OnSuspend() {
101   ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT);
102 }
103
104 void PowerMonitorBroadcastSource::OnResume() {
105   ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT);
106 }
107
108 }  // namespace content