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

Side by Side Diff: app/system_monitor.cc

Issue 431008: Make SystemMonitor not a Singleton and move it out of base (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: fix ChromeFrame build Created 11 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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.h" 5 #include "app/system_monitor.h"
6
6 #include "base/logging.h" 7 #include "base/logging.h"
7 #include "base/message_loop.h" 8 #include "base/message_loop.h"
8 #include "base/singleton.h" 9 #include "base/time.h"
9 10
10 namespace base { 11 static SystemMonitor* g_system_monitor = NULL;
11 12
12 #if defined(ENABLE_BATTERY_MONITORING) 13 #if defined(ENABLE_BATTERY_MONITORING)
13 // 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
14 // battery check. 15 // battery check.
15 static int kDelayedBatteryCheckMs = 10 * 1000; 16 static int kDelayedBatteryCheckMs = 10 * 1000;
16 #endif // defined(ENABLE_BATTERY_MONITORING) 17 #endif // defined(ENABLE_BATTERY_MONITORING)
17 18
18 SystemMonitor::SystemMonitor() 19 SystemMonitor::SystemMonitor()
19 : battery_in_use_(false), 20 : observer_list_(new ObserverListThreadSafe<PowerObserver>()),
21 battery_in_use_(false),
20 suspended_(false) { 22 suspended_(false) {
21 observer_list_ = new ObserverListThreadSafe<PowerObserver>(); 23 DCHECK(!g_system_monitor);
24 g_system_monitor = this;
25
26 DCHECK(MessageLoop::current());
27 #if defined(ENABLE_BATTERY_MONITORING)
28 delayed_battery_check_.Start(
29 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this,
30 &SystemMonitor::BatteryCheck);
31 #endif // defined(ENABLE_BATTERY_MONITORING)
32 }
33
34 SystemMonitor::~SystemMonitor() {
35 DCHECK_EQ(this, g_system_monitor);
36 g_system_monitor = NULL;
37 }
38
39 // static
40 SystemMonitor* SystemMonitor::Get() {
41 return g_system_monitor;
22 } 42 }
23 43
24 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { 44 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
25 // Suppress duplicate notifications. Some platforms may 45 // Suppress duplicate notifications. Some platforms may
26 // send multiple notifications of the same event. 46 // send multiple notifications of the same event.
27 switch (event_id) { 47 switch (event_id) {
28 case POWER_STATE_EVENT: 48 case POWER_STATE_EVENT:
29 { 49 {
30 bool on_battery = IsBatteryPower(); 50 bool on_battery = IsBatteryPower();
31 if (on_battery != battery_in_use_) { 51 if (on_battery != battery_in_use_) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 void SystemMonitor::NotifySuspend() { 86 void SystemMonitor::NotifySuspend() {
67 LOG(INFO) << L"Power Suspending"; 87 LOG(INFO) << L"Power Suspending";
68 observer_list_->Notify(&PowerObserver::OnSuspend); 88 observer_list_->Notify(&PowerObserver::OnSuspend);
69 } 89 }
70 90
71 void SystemMonitor::NotifyResume() { 91 void SystemMonitor::NotifyResume() {
72 LOG(INFO) << L"Power Resuming"; 92 LOG(INFO) << L"Power Resuming";
73 observer_list_->Notify(&PowerObserver::OnResume); 93 observer_list_->Notify(&PowerObserver::OnResume);
74 } 94 }
75 95
76 // static
77 SystemMonitor* SystemMonitor::Get() {
78 // Uses the LeakySingletonTrait because cleanup is optional.
79 return
80 Singleton<SystemMonitor, LeakySingletonTraits<SystemMonitor> >::get();
81 }
82
83 // static
84 void SystemMonitor::Start() {
85 #if defined(ENABLE_BATTERY_MONITORING)
86 DCHECK(MessageLoop::current()); // Can't call start too early.
87 SystemMonitor* monitor = Get();
88 monitor->delayed_battery_check_.Start(
89 TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor,
90 &SystemMonitor::BatteryCheck);
91 #endif // defined(ENABLE_BATTERY_MONITORING)
92 }
93
94 void SystemMonitor::BatteryCheck() { 96 void SystemMonitor::BatteryCheck() {
95 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); 97 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
96 } 98 }
97
98 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698