Another take on menu's. This uses the hosting menu scroll view container as a menuba...
[chromium-blink-merge.git] / app / system_monitor.cc
blobb0e715ab42149b6cc58c51c66e2cfb193c31c0b2
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
3 // found in the LICENSE file.
5 #include "app/system_monitor.h"
7 #include "base/logging.h"
8 #include "base/message_loop.h"
9 #include "base/time.h"
11 static SystemMonitor* g_system_monitor = NULL;
13 #if defined(ENABLE_BATTERY_MONITORING)
14 // The amount of time (in ms) to wait before running the initial
15 // battery check.
16 static int kDelayedBatteryCheckMs = 10 * 1000;
17 #endif // defined(ENABLE_BATTERY_MONITORING)
19 SystemMonitor::SystemMonitor()
20 : observer_list_(new ObserverListThreadSafe<PowerObserver>()),
21 battery_in_use_(false),
22 suspended_(false) {
23 DCHECK(!g_system_monitor);
24 g_system_monitor = this;
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)
34 SystemMonitor::~SystemMonitor() {
35 DCHECK_EQ(this, g_system_monitor);
36 g_system_monitor = NULL;
39 // static
40 SystemMonitor* SystemMonitor::Get() {
41 return g_system_monitor;
44 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
45 // Suppress duplicate notifications. Some platforms may
46 // send multiple notifications of the same event.
47 switch (event_id) {
48 case POWER_STATE_EVENT:
50 bool on_battery = IsBatteryPower();
51 if (on_battery != battery_in_use_) {
52 battery_in_use_ = on_battery;
53 NotifyPowerStateChange();
56 break;
57 case RESUME_EVENT:
58 if (suspended_) {
59 suspended_ = false;
60 NotifyResume();
62 break;
63 case SUSPEND_EVENT:
64 if (!suspended_) {
65 suspended_ = true;
66 NotifySuspend();
68 break;
72 void SystemMonitor::AddObserver(PowerObserver* obs) {
73 observer_list_->AddObserver(obs);
76 void SystemMonitor::RemoveObserver(PowerObserver* obs) {
77 observer_list_->RemoveObserver(obs);
80 void SystemMonitor::NotifyPowerStateChange() {
81 LOG(INFO) << L"PowerStateChange: "
82 << (BatteryPower() ? L"On" : L"Off") << L" battery";
83 observer_list_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower());
86 void SystemMonitor::NotifySuspend() {
87 LOG(INFO) << L"Power Suspending";
88 observer_list_->Notify(&PowerObserver::OnSuspend);
91 void SystemMonitor::NotifyResume() {
92 LOG(INFO) << L"Power Resuming";
93 observer_list_->Notify(&PowerObserver::OnResume);
96 void SystemMonitor::BatteryCheck() {
97 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);