| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "base/system_monitor/system_monitor.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/time.h" | |
| 10 | |
| 11 namespace base { | |
| 12 | |
| 13 static SystemMonitor* g_system_monitor = NULL; | |
| 14 | |
| 15 #if defined(ENABLE_BATTERY_MONITORING) | |
| 16 // The amount of time (in ms) to wait before running the initial | |
| 17 // battery check. | |
| 18 static int kDelayedBatteryCheckMs = 10 * 1000; | |
| 19 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 20 | |
| 21 SystemMonitor::SystemMonitor() | |
| 22 : observer_list_(new ObserverListThreadSafe<PowerObserver>()), | |
| 23 battery_in_use_(false), | |
| 24 suspended_(false) { | |
| 25 DCHECK(!g_system_monitor); | |
| 26 g_system_monitor = this; | |
| 27 | |
| 28 DCHECK(MessageLoop::current()); | |
| 29 #if defined(ENABLE_BATTERY_MONITORING) | |
| 30 delayed_battery_check_.Start( | |
| 31 base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, | |
| 32 &SystemMonitor::BatteryCheck); | |
| 33 #endif // defined(ENABLE_BATTERY_MONITORING) | |
| 34 #if defined(OS_MACOSX) | |
| 35 PlatformInit(); | |
| 36 #endif | |
| 37 } | |
| 38 | |
| 39 SystemMonitor::~SystemMonitor() { | |
| 40 #if defined(OS_MACOSX) | |
| 41 PlatformDestroy(); | |
| 42 #endif | |
| 43 DCHECK_EQ(this, g_system_monitor); | |
| 44 g_system_monitor = NULL; | |
| 45 } | |
| 46 | |
| 47 // static | |
| 48 SystemMonitor* SystemMonitor::Get() { | |
| 49 return g_system_monitor; | |
| 50 } | |
| 51 | |
| 52 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { | |
| 53 // Suppress duplicate notifications. Some platforms may | |
| 54 // send multiple notifications of the same event. | |
| 55 switch (event_id) { | |
| 56 case POWER_STATE_EVENT: | |
| 57 { | |
| 58 bool on_battery = IsBatteryPower(); | |
| 59 if (on_battery != battery_in_use_) { | |
| 60 battery_in_use_ = on_battery; | |
| 61 NotifyPowerStateChange(); | |
| 62 } | |
| 63 } | |
| 64 break; | |
| 65 case RESUME_EVENT: | |
| 66 if (suspended_) { | |
| 67 suspended_ = false; | |
| 68 NotifyResume(); | |
| 69 } | |
| 70 break; | |
| 71 case SUSPEND_EVENT: | |
| 72 if (!suspended_) { | |
| 73 suspended_ = true; | |
| 74 NotifySuspend(); | |
| 75 } | |
| 76 break; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 void SystemMonitor::AddObserver(PowerObserver* obs) { | |
| 81 observer_list_->AddObserver(obs); | |
| 82 } | |
| 83 | |
| 84 void SystemMonitor::RemoveObserver(PowerObserver* obs) { | |
| 85 observer_list_->RemoveObserver(obs); | |
| 86 } | |
| 87 | |
| 88 void SystemMonitor::NotifyPowerStateChange() { | |
| 89 VLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") | |
| 90 << " battery"; | |
| 91 observer_list_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); | |
| 92 } | |
| 93 | |
| 94 void SystemMonitor::NotifySuspend() { | |
| 95 VLOG(1) << "Power Suspending"; | |
| 96 observer_list_->Notify(&PowerObserver::OnSuspend); | |
| 97 } | |
| 98 | |
| 99 void SystemMonitor::NotifyResume() { | |
| 100 VLOG(1) << "Power Resuming"; | |
| 101 observer_list_->Notify(&PowerObserver::OnResume); | |
| 102 } | |
| 103 | |
| 104 void SystemMonitor::BatteryCheck() { | |
| 105 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | |
| 106 } | |
| 107 | |
| 108 } // namespace base | |
| OLD | NEW |