ipc/chromium/src/base/system_monitor.h

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 #ifndef BASE_SYSTEM_MONITOR_H_
michael@0 6 #define BASE_SYSTEM_MONITOR_H_
michael@0 7
michael@0 8 #include "base/observer_list_threadsafe.h"
michael@0 9 #include "base/singleton.h"
michael@0 10
michael@0 11 // Windows HiRes timers drain the battery faster so we need to know the battery
michael@0 12 // status. This isn't true for other platforms.
michael@0 13 #if defined(OS_WIN)
michael@0 14 #define ENABLE_BATTERY_MONITORING 1
michael@0 15 #else
michael@0 16 #undef ENABLE_BATTERY_MONITORING
michael@0 17 #endif // !OS_WIN
michael@0 18
michael@0 19 namespace base {
michael@0 20
michael@0 21 // Class for monitoring various system-related subsystems
michael@0 22 // such as power management, network status, etc.
michael@0 23 // TODO(mbelshe): Add support beyond just power management.
michael@0 24 class SystemMonitor {
michael@0 25 public:
michael@0 26 // Access to the Singleton
michael@0 27 static SystemMonitor* Get() {
michael@0 28 // Uses the LeakySingletonTrait because cleanup is optional.
michael@0 29 return
michael@0 30 Singleton<SystemMonitor, LeakySingletonTraits<SystemMonitor> >::get();
michael@0 31 }
michael@0 32
michael@0 33 // Start the System Monitor within a process. This method
michael@0 34 // is provided so that the battery check can be deferred.
michael@0 35 // The MessageLoop must be started before calling this
michael@0 36 // method.
michael@0 37 // This is a no-op on platforms for which ENABLE_BATTERY_MONITORING is
michael@0 38 // disabled.
michael@0 39 static void Start();
michael@0 40
michael@0 41 //
michael@0 42 // Power-related APIs
michael@0 43 //
michael@0 44
michael@0 45 // Is the computer currently on battery power.
michael@0 46 // Can be called on any thread.
michael@0 47 bool BatteryPower() {
michael@0 48 // Using a lock here is not necessary for just a bool.
michael@0 49 return battery_in_use_;
michael@0 50 }
michael@0 51
michael@0 52 // Normalized list of power events.
michael@0 53 enum PowerEvent {
michael@0 54 POWER_STATE_EVENT, // The Power status of the system has changed.
michael@0 55 SUSPEND_EVENT, // The system is being suspended.
michael@0 56 RESUME_EVENT // The system is being resumed.
michael@0 57 };
michael@0 58
michael@0 59 // Callbacks will be called on the thread which creates the SystemMonitor.
michael@0 60 // During the callback, Add/RemoveObserver will block until the callbacks
michael@0 61 // are finished. Observers should implement quick callback functions; if
michael@0 62 // lengthy operations are needed, the observer should take care to invoke
michael@0 63 // the operation on an appropriate thread.
michael@0 64 class PowerObserver {
michael@0 65 public:
michael@0 66 // Notification of a change in power status of the computer, such
michael@0 67 // as from switching between battery and A/C power.
michael@0 68 virtual void OnPowerStateChange(SystemMonitor*) = 0;
michael@0 69
michael@0 70 // Notification that the system is suspending.
michael@0 71 virtual void OnSuspend(SystemMonitor*) = 0;
michael@0 72
michael@0 73 // Notification that the system is resuming.
michael@0 74 virtual void OnResume(SystemMonitor*) = 0;
michael@0 75 };
michael@0 76
michael@0 77 // Add a new observer.
michael@0 78 // Can be called from any thread.
michael@0 79 // Must not be called from within a notification callback.
michael@0 80 void AddObserver(PowerObserver* obs);
michael@0 81
michael@0 82 // Remove an existing observer.
michael@0 83 // Can be called from any thread.
michael@0 84 // Must not be called from within a notification callback.
michael@0 85 void RemoveObserver(PowerObserver* obs);
michael@0 86
michael@0 87 #if defined(OS_WIN)
michael@0 88 // Windows-specific handling of a WM_POWERBROADCAST message.
michael@0 89 // Embedders of this API should hook their top-level window
michael@0 90 // message loop and forward WM_POWERBROADCAST through this call.
michael@0 91 void ProcessWmPowerBroadcastMessage(int event_id);
michael@0 92 #endif
michael@0 93
michael@0 94 // Cross-platform handling of a power event.
michael@0 95 void ProcessPowerMessage(PowerEvent event_id);
michael@0 96
michael@0 97 // Constructor.
michael@0 98 // Don't use this; access SystemMonitor via the Singleton.
michael@0 99 SystemMonitor();
michael@0 100
michael@0 101 private:
michael@0 102 // Platform-specific method to check whether the system is currently
michael@0 103 // running on battery power. Returns true if running on batteries,
michael@0 104 // false otherwise.
michael@0 105 bool IsBatteryPower();
michael@0 106
michael@0 107 // Checks the battery status and notifies observers if the battery
michael@0 108 // status has changed.
michael@0 109 void BatteryCheck();
michael@0 110
michael@0 111 // Functions to trigger notifications.
michael@0 112 void NotifyPowerStateChange();
michael@0 113 void NotifySuspend();
michael@0 114 void NotifyResume();
michael@0 115
michael@0 116 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_;
michael@0 117 bool battery_in_use_;
michael@0 118 bool suspended_;
michael@0 119
michael@0 120 #if defined(ENABLE_BATTERY_MONITORING)
michael@0 121 base::OneShotTimer<SystemMonitor> delayed_battery_check_;
michael@0 122 #endif
michael@0 123
michael@0 124 DISALLOW_COPY_AND_ASSIGN(SystemMonitor);
michael@0 125 };
michael@0 126
michael@0 127 }
michael@0 128
michael@0 129 #endif // BASE_SYSTEM_MONITOR_H_

mercurial