Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1784)

Side by Side Diff: base/power_monitor/power_monitor.cc

Issue 10959020: SystemMonitor refactoring: move power state monitor into a separate class called PowerMonitor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: modify per 1st round review Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/system_monitor/system_monitor.h" 5 #include "base/power_monitor/power_monitor.h"
6 6
7 #include <utility>
8
9 #include "base/logging.h"
10 #include "base/message_loop.h"
11 #include "base/time.h" 7 #include "base/time.h"
12 8
13 namespace base { 9 namespace base {
14 10
15 static SystemMonitor* g_system_monitor = NULL; 11 static PowerMonitor* g_power_monitor = NULL;
16 12
17 #if defined(ENABLE_BATTERY_MONITORING) 13 #if defined(ENABLE_BATTERY_MONITORING)
18 // The amount of time (in ms) to wait before running the initial 14 // The amount of time (in ms) to wait before running the initial
19 // battery check. 15 // battery check.
20 static int kDelayedBatteryCheckMs = 10 * 1000; 16 static int kDelayedBatteryCheckMs = 10 * 1000;
21 #endif // defined(ENABLE_BATTERY_MONITORING) 17 #endif // defined(ENABLE_BATTERY_MONITORING)
22 18
23 SystemMonitor::SystemMonitor() 19 PowerMonitor::PowerMonitor()
24 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), 20 : observers_(new ObserverListThreadSafe<PowerObserver>()),
25 devices_changed_observer_list_(
26 new ObserverListThreadSafe<DevicesChangedObserver>()),
27 battery_in_use_(false), 21 battery_in_use_(false),
28 suspended_(false) { 22 suspended_(false) {
29 DCHECK(!g_system_monitor); 23 DCHECK(!g_power_monitor);
30 g_system_monitor = this; 24 g_power_monitor = this;
31 25
32 DCHECK(MessageLoop::current()); 26 DCHECK(MessageLoop::current());
33 #if defined(ENABLE_BATTERY_MONITORING) 27 #if defined(ENABLE_BATTERY_MONITORING)
34 delayed_battery_check_.Start(FROM_HERE, 28 delayed_battery_check_.Start(FROM_HERE,
35 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, 29 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this,
36 &SystemMonitor::BatteryCheck); 30 &PowerMonitor::BatteryCheck);
37 #endif // defined(ENABLE_BATTERY_MONITORING) 31 #endif // defined(ENABLE_BATTERY_MONITORING)
38 #if defined(OS_MACOSX) 32 #if defined(OS_MACOSX)
39 PlatformInit(); 33 PlatformInit();
40 #endif 34 #endif
41 } 35 }
42 36
43 SystemMonitor::~SystemMonitor() { 37 PowerMonitor::~PowerMonitor() {
44 #if defined(OS_MACOSX) 38 #if defined(OS_MACOSX)
45 PlatformDestroy(); 39 PlatformDestroy();
46 #endif 40 #endif
47 DCHECK_EQ(this, g_system_monitor); 41 DCHECK_EQ(this, g_power_monitor);
48 g_system_monitor = NULL; 42 g_power_monitor = NULL;
49 } 43 }
50 44
51 // static 45 // static
52 SystemMonitor* SystemMonitor::Get() { 46 PowerMonitor* PowerMonitor::Get() {
53 return g_system_monitor; 47 return g_power_monitor;
54 } 48 }
55 49
56 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { 50 void PowerMonitor::AddObserver(PowerObserver* obs) {
51 observers_->AddObserver(obs);
52 }
53
54 void PowerMonitor::RemoveObserver(PowerObserver* obs) {
55 observers_->RemoveObserver(obs);
56 }
57
58 void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) {
57 // Suppress duplicate notifications. Some platforms may 59 // Suppress duplicate notifications. Some platforms may
58 // send multiple notifications of the same event. 60 // send multiple notifications of the same event.
59 switch (event_id) { 61 switch (event_id) {
60 case POWER_STATE_EVENT: 62 case POWER_STATE_EVENT:
61 { 63 {
62 bool on_battery = IsBatteryPower(); 64 bool on_battery = IsBatteryPower();
63 if (on_battery != battery_in_use_) { 65 if (on_battery != battery_in_use_) {
64 battery_in_use_ = on_battery; 66 battery_in_use_ = on_battery;
65 NotifyPowerStateChange(); 67 NotifyPowerStateChange();
66 } 68 }
67 } 69 }
68 break; 70 break;
69 case RESUME_EVENT: 71 case RESUME_EVENT:
70 if (suspended_) { 72 if (suspended_) {
71 suspended_ = false; 73 suspended_ = false;
72 NotifyResume(); 74 NotifyResume();
73 } 75 }
74 break; 76 break;
75 case SUSPEND_EVENT: 77 case SUSPEND_EVENT:
76 if (!suspended_) { 78 if (!suspended_) {
77 suspended_ = true; 79 suspended_ = true;
78 NotifySuspend(); 80 NotifySuspend();
79 } 81 }
80 break; 82 break;
81 } 83 }
82 } 84 }
83 85
84 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { 86 void PowerMonitor::BatteryCheck() {
85 NotifyDevicesChanged(device_type); 87 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT);
86 } 88 }
87 89
88 void SystemMonitor::AddPowerObserver(PowerObserver* obs) { 90 void PowerMonitor::NotifyPowerStateChange() {
89 power_observer_list_->AddObserver(obs); 91 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off")
92 << " battery";
93 observers_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower());
90 } 94 }
91 95
92 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) { 96 void PowerMonitor::NotifySuspend() {
93 power_observer_list_->RemoveObserver(obs); 97 DVLOG(1) << "Power Suspending";
98 observers_->Notify(&PowerObserver::OnSuspend);
94 } 99 }
95 100
96 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { 101 void PowerMonitor::NotifyResume() {
97 devices_changed_observer_list_->AddObserver(obs);
98 }
99
100 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) {
101 devices_changed_observer_list_->RemoveObserver(obs);
102 }
103
104 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) {
105 DVLOG(1) << "DevicesChanged with device type " << device_type;
106 devices_changed_observer_list_->Notify(
107 &DevicesChangedObserver::OnDevicesChanged, device_type);
108 }
109
110 void SystemMonitor::NotifyPowerStateChange() {
111 DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off")
112 << " battery";
113 power_observer_list_->Notify(&PowerObserver::OnPowerStateChange,
114 BatteryPower());
115 }
116
117 void SystemMonitor::NotifySuspend() {
118 DVLOG(1) << "Power Suspending";
119 power_observer_list_->Notify(&PowerObserver::OnSuspend);
120 }
121
122 void SystemMonitor::NotifyResume() {
123 DVLOG(1) << "Power Resuming"; 102 DVLOG(1) << "Power Resuming";
124 power_observer_list_->Notify(&PowerObserver::OnResume); 103 observers_->Notify(&PowerObserver::OnResume);
125 }
126
127 void SystemMonitor::BatteryCheck() {
128 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
129 } 104 }
130 105
131 } // namespace base 106 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698