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

Side by Side Diff: base/system_monitor/system_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: Use PowerMonitor instead of SystemMonitor in XXXMain function 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 (c) 2012 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/system_monitor/system_monitor.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/time.h" 11 #include "base/time.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 static SystemMonitor* g_system_monitor = NULL; 15 static SystemMonitor* g_system_monitor = NULL;
16 16
17 #if defined(ENABLE_BATTERY_MONITORING)
18 // The amount of time (in ms) to wait before running the initial
19 // battery check.
20 static int kDelayedBatteryCheckMs = 10 * 1000;
21 #endif // defined(ENABLE_BATTERY_MONITORING)
22
23 SystemMonitor::SystemMonitor() 17 SystemMonitor::SystemMonitor()
24 : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), 18 : devices_changed_observer_list_(
25 devices_changed_observer_list_( 19 new ObserverListThreadSafe<DevicesChangedObserver>()) {
26 new ObserverListThreadSafe<DevicesChangedObserver>()),
27 battery_in_use_(false),
28 suspended_(false) {
29 DCHECK(!g_system_monitor); 20 DCHECK(!g_system_monitor);
30 g_system_monitor = this; 21 g_system_monitor = this;
31
32 DCHECK(MessageLoop::current());
33 #if defined(ENABLE_BATTERY_MONITORING)
34 delayed_battery_check_.Start(FROM_HERE,
35 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this,
36 &SystemMonitor::BatteryCheck);
37 #endif // defined(ENABLE_BATTERY_MONITORING)
38 #if defined(OS_MACOSX)
39 PlatformInit();
40 #endif
41 } 22 }
42 23
43 SystemMonitor::~SystemMonitor() { 24 SystemMonitor::~SystemMonitor() {
44 #if defined(OS_MACOSX)
45 PlatformDestroy();
46 #endif
47 DCHECK_EQ(this, g_system_monitor); 25 DCHECK_EQ(this, g_system_monitor);
48 g_system_monitor = NULL; 26 g_system_monitor = NULL;
49 } 27 }
50 28
51 // static 29 // static
52 SystemMonitor* SystemMonitor::Get() { 30 SystemMonitor* SystemMonitor::Get() {
53 return g_system_monitor; 31 return g_system_monitor;
54 } 32 }
55 33
56 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
57 // Suppress duplicate notifications. Some platforms may
58 // send multiple notifications of the same event.
59 switch (event_id) {
60 case POWER_STATE_EVENT:
61 {
62 bool on_battery = IsBatteryPower();
63 if (on_battery != battery_in_use_) {
64 battery_in_use_ = on_battery;
65 NotifyPowerStateChange();
66 }
67 }
68 break;
69 case RESUME_EVENT:
70 if (suspended_) {
71 suspended_ = false;
72 NotifyResume();
73 }
74 break;
75 case SUSPEND_EVENT:
76 if (!suspended_) {
77 suspended_ = true;
78 NotifySuspend();
79 }
80 break;
81 }
82 }
83
84 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { 34 void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) {
85 NotifyDevicesChanged(device_type); 35 NotifyDevicesChanged(device_type);
86 } 36 }
87 37
88 void SystemMonitor::AddPowerObserver(PowerObserver* obs) {
89 power_observer_list_->AddObserver(obs);
90 }
91
92 void SystemMonitor::RemovePowerObserver(PowerObserver* obs) {
93 power_observer_list_->RemoveObserver(obs);
94 }
95
96 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) { 38 void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) {
97 devices_changed_observer_list_->AddObserver(obs); 39 devices_changed_observer_list_->AddObserver(obs);
98 } 40 }
99 41
100 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { 42 void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) {
101 devices_changed_observer_list_->RemoveObserver(obs); 43 devices_changed_observer_list_->RemoveObserver(obs);
102 } 44 }
103 45
104 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { 46 void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) {
105 DVLOG(1) << "DevicesChanged with device type " << device_type; 47 DVLOG(1) << "DevicesChanged with device type " << device_type;
106 devices_changed_observer_list_->Notify( 48 devices_changed_observer_list_->Notify(
107 &DevicesChangedObserver::OnDevicesChanged, device_type); 49 &DevicesChangedObserver::OnDevicesChanged, device_type);
108 } 50 }
109 51
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";
124 power_observer_list_->Notify(&PowerObserver::OnResume);
125 }
126
127 void SystemMonitor::BatteryCheck() {
128 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
129 }
130
131 } // namespace base 52 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698