Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 // Copyright (c) 2008 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 "base/system_monitor.h"
6 #include "base/logging.h"
7 #include "base/message_loop.h"
9 namespace base {
11 #if defined(ENABLE_BATTERY_MONITORING)
12 // The amount of time (in ms) to wait before running the initial
13 // battery check.
14 static int kDelayedBatteryCheckMs = 10 * 1000;
15 #endif // defined(ENABLE_BATTERY_MONITORING)
17 SystemMonitor::SystemMonitor()
18 : battery_in_use_(false),
19 suspended_(false) {
20 observer_list_ = new ObserverListThreadSafe<PowerObserver>();
21 }
23 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
24 // Suppress duplicate notifications. Some platforms may
25 // send multiple notifications of the same event.
26 switch (event_id) {
27 case POWER_STATE_EVENT:
28 {
29 bool on_battery = IsBatteryPower();
30 if (on_battery != battery_in_use_) {
31 battery_in_use_ = on_battery;
32 NotifyPowerStateChange();
33 }
34 }
35 break;
36 case RESUME_EVENT:
37 if (suspended_) {
38 suspended_ = false;
39 NotifyResume();
40 }
41 break;
42 case SUSPEND_EVENT:
43 if (!suspended_) {
44 suspended_ = true;
45 NotifySuspend();
46 }
47 break;
48 }
49 }
51 void SystemMonitor::AddObserver(PowerObserver* obs) {
52 observer_list_->AddObserver(obs);
53 }
55 void SystemMonitor::RemoveObserver(PowerObserver* obs) {
56 observer_list_->RemoveObserver(obs);
57 }
59 void SystemMonitor::NotifyPowerStateChange() {
60 CHROMIUM_LOG(INFO) << "PowerStateChange: "
61 << (BatteryPower() ? "On" : "Off") << " battery";
62 observer_list_->Notify(&PowerObserver::OnPowerStateChange, this);
63 }
65 void SystemMonitor::NotifySuspend() {
66 CHROMIUM_LOG(INFO) << "Power Suspending";
67 observer_list_->Notify(&PowerObserver::OnSuspend, this);
68 }
70 void SystemMonitor::NotifyResume() {
71 CHROMIUM_LOG(INFO) << "Power Resuming";
72 observer_list_->Notify(&PowerObserver::OnResume, this);
73 }
75 void SystemMonitor::Start() {
76 #if defined(ENABLE_BATTERY_MONITORING)
77 DCHECK(MessageLoop::current()); // Can't call start too early.
78 SystemMonitor* monitor = Get();
79 monitor->delayed_battery_check_.Start(
80 TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor,
81 &SystemMonitor::BatteryCheck);
82 #endif // defined(ENABLE_BATTERY_MONITORING)
83 }
85 void SystemMonitor::BatteryCheck() {
86 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
87 }
89 } // namespace base