ipc/chromium/src/base/system_monitor.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/system_monitor.h"
michael@0 6 #include "base/logging.h"
michael@0 7 #include "base/message_loop.h"
michael@0 8
michael@0 9 namespace base {
michael@0 10
michael@0 11 #if defined(ENABLE_BATTERY_MONITORING)
michael@0 12 // The amount of time (in ms) to wait before running the initial
michael@0 13 // battery check.
michael@0 14 static int kDelayedBatteryCheckMs = 10 * 1000;
michael@0 15 #endif // defined(ENABLE_BATTERY_MONITORING)
michael@0 16
michael@0 17 SystemMonitor::SystemMonitor()
michael@0 18 : battery_in_use_(false),
michael@0 19 suspended_(false) {
michael@0 20 observer_list_ = new ObserverListThreadSafe<PowerObserver>();
michael@0 21 }
michael@0 22
michael@0 23 void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
michael@0 24 // Suppress duplicate notifications. Some platforms may
michael@0 25 // send multiple notifications of the same event.
michael@0 26 switch (event_id) {
michael@0 27 case POWER_STATE_EVENT:
michael@0 28 {
michael@0 29 bool on_battery = IsBatteryPower();
michael@0 30 if (on_battery != battery_in_use_) {
michael@0 31 battery_in_use_ = on_battery;
michael@0 32 NotifyPowerStateChange();
michael@0 33 }
michael@0 34 }
michael@0 35 break;
michael@0 36 case RESUME_EVENT:
michael@0 37 if (suspended_) {
michael@0 38 suspended_ = false;
michael@0 39 NotifyResume();
michael@0 40 }
michael@0 41 break;
michael@0 42 case SUSPEND_EVENT:
michael@0 43 if (!suspended_) {
michael@0 44 suspended_ = true;
michael@0 45 NotifySuspend();
michael@0 46 }
michael@0 47 break;
michael@0 48 }
michael@0 49 }
michael@0 50
michael@0 51 void SystemMonitor::AddObserver(PowerObserver* obs) {
michael@0 52 observer_list_->AddObserver(obs);
michael@0 53 }
michael@0 54
michael@0 55 void SystemMonitor::RemoveObserver(PowerObserver* obs) {
michael@0 56 observer_list_->RemoveObserver(obs);
michael@0 57 }
michael@0 58
michael@0 59 void SystemMonitor::NotifyPowerStateChange() {
michael@0 60 CHROMIUM_LOG(INFO) << "PowerStateChange: "
michael@0 61 << (BatteryPower() ? "On" : "Off") << " battery";
michael@0 62 observer_list_->Notify(&PowerObserver::OnPowerStateChange, this);
michael@0 63 }
michael@0 64
michael@0 65 void SystemMonitor::NotifySuspend() {
michael@0 66 CHROMIUM_LOG(INFO) << "Power Suspending";
michael@0 67 observer_list_->Notify(&PowerObserver::OnSuspend, this);
michael@0 68 }
michael@0 69
michael@0 70 void SystemMonitor::NotifyResume() {
michael@0 71 CHROMIUM_LOG(INFO) << "Power Resuming";
michael@0 72 observer_list_->Notify(&PowerObserver::OnResume, this);
michael@0 73 }
michael@0 74
michael@0 75 void SystemMonitor::Start() {
michael@0 76 #if defined(ENABLE_BATTERY_MONITORING)
michael@0 77 DCHECK(MessageLoop::current()); // Can't call start too early.
michael@0 78 SystemMonitor* monitor = Get();
michael@0 79 monitor->delayed_battery_check_.Start(
michael@0 80 TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), monitor,
michael@0 81 &SystemMonitor::BatteryCheck);
michael@0 82 #endif // defined(ENABLE_BATTERY_MONITORING)
michael@0 83 }
michael@0 84
michael@0 85 void SystemMonitor::BatteryCheck() {
michael@0 86 ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
michael@0 87 }
michael@0 88
michael@0 89 } // namespace base

mercurial