• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 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/browser/power_monitor_message_broadcaster.h"
6 
7 #include "base/power_monitor/power_monitor.h"
8 #include "content/common/power_monitor_messages.h"
9 #include "ipc/ipc_sender.h"
10 
11 namespace content {
12 
PowerMonitorMessageBroadcaster(IPC::Sender * sender)13 PowerMonitorMessageBroadcaster::PowerMonitorMessageBroadcaster(
14     IPC::Sender* sender)
15     : sender_(sender) {
16   base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
17   if (power_monitor)
18     power_monitor->AddObserver(this);
19 }
20 
~PowerMonitorMessageBroadcaster()21 PowerMonitorMessageBroadcaster::~PowerMonitorMessageBroadcaster() {
22   base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
23   if (power_monitor)
24     power_monitor->RemoveObserver(this);
25 }
26 
Init()27 void PowerMonitorMessageBroadcaster::Init() {
28   base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
29   // Unit tests does not initialize the PowerMonitor.
30   if (power_monitor)
31     OnPowerStateChange(power_monitor->IsOnBatteryPower());
32 }
33 
OnPowerStateChange(bool on_battery_power)34 void PowerMonitorMessageBroadcaster::OnPowerStateChange(bool on_battery_power) {
35   sender_->Send(new PowerMonitorMsg_PowerStateChange(on_battery_power));
36 }
37 
OnSuspend()38 void PowerMonitorMessageBroadcaster::OnSuspend() {
39   sender_->Send(new PowerMonitorMsg_Suspend());
40 }
41 
OnResume()42 void PowerMonitorMessageBroadcaster::OnResume() {
43   sender_->Send(new PowerMonitorMsg_Resume());
44 }
45 
46 }  // namespace content
47