Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: base/system_monitor/system_monitor.h

Issue 7042003: Revert 85732 (broke build) - Move SystemMonitor to base/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gypi ('k') | base/system_monitor/system_monitor.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #ifndef BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_
6 #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_
7 #pragma once
8
9 #include "base/basictypes.h"
10 #include "build/build_config.h"
11
12 // Windows HiRes timers drain the battery faster so we need to know the battery
13 // status. This isn't true for other platforms.
14 #if defined(OS_WIN)
15 #define ENABLE_BATTERY_MONITORING 1
16 #else
17 #undef ENABLE_BATTERY_MONITORING
18 #endif // !OS_WIN
19
20 #include "base/observer_list_threadsafe.h"
21 #if defined(ENABLE_BATTERY_MONITORING)
22 #include "base/timer.h"
23 #endif // defined(ENABLE_BATTERY_MONITORING)
24
25 #if defined(OS_MACOSX)
26 #include <IOKit/pwr_mgt/IOPMLib.h>
27 #include <IOKit/IOMessage.h>
28 #endif // OS_MACOSX
29
30 namespace base {
31
32 // Class for monitoring various system-related subsystems
33 // such as power management, network status, etc.
34 // TODO(mbelshe): Add support beyond just power management.
35 class SystemMonitor {
36 public:
37 // Normalized list of power events.
38 enum PowerEvent {
39 POWER_STATE_EVENT, // The Power status of the system has changed.
40 SUSPEND_EVENT, // The system is being suspended.
41 RESUME_EVENT // The system is being resumed.
42 };
43
44 // Create SystemMonitor. Only one SystemMonitor instance per application
45 // is allowed.
46 SystemMonitor();
47 ~SystemMonitor();
48
49 // Get the application-wide SystemMonitor (if not present, returns NULL).
50 static SystemMonitor* Get();
51
52 //
53 // Power-related APIs
54 //
55
56 // Is the computer currently on battery power.
57 // Can be called on any thread.
58 bool BatteryPower() const {
59 // Using a lock here is not necessary for just a bool.
60 return battery_in_use_;
61 }
62
63 // Callbacks will be called on the thread which creates the SystemMonitor.
64 // During the callback, Add/RemoveObserver will block until the callbacks
65 // are finished. Observers should implement quick callback functions; if
66 // lengthy operations are needed, the observer should take care to invoke
67 // the operation on an appropriate thread.
68 class PowerObserver {
69 public:
70 // Notification of a change in power status of the computer, such
71 // as from switching between battery and A/C power.
72 virtual void OnPowerStateChange(bool on_battery_power) {}
73
74 // Notification that the system is suspending.
75 virtual void OnSuspend() {}
76
77 // Notification that the system is resuming.
78 virtual void OnResume() {}
79
80 protected:
81 virtual ~PowerObserver() {}
82 };
83
84 // Add a new observer.
85 // Can be called from any thread.
86 // Must not be called from within a notification callback.
87 void AddObserver(PowerObserver* obs);
88
89 // Remove an existing observer.
90 // Can be called from any thread.
91 // Must not be called from within a notification callback.
92 void RemoveObserver(PowerObserver* obs);
93
94 #if defined(OS_WIN)
95 // Windows-specific handling of a WM_POWERBROADCAST message.
96 // Embedders of this API should hook their top-level window
97 // message loop and forward WM_POWERBROADCAST through this call.
98 void ProcessWmPowerBroadcastMessage(int event_id);
99 #endif
100
101 // Cross-platform handling of a power event.
102 void ProcessPowerMessage(PowerEvent event_id);
103
104 private:
105 #if defined(OS_MACOSX)
106 void PlatformInit();
107 void PlatformDestroy();
108 #endif
109
110 // Platform-specific method to check whether the system is currently
111 // running on battery power. Returns true if running on batteries,
112 // false otherwise.
113 bool IsBatteryPower();
114
115 // Checks the battery status and notifies observers if the battery
116 // status has changed.
117 void BatteryCheck();
118
119 // Functions to trigger notifications.
120 void NotifyPowerStateChange();
121 void NotifySuspend();
122 void NotifyResume();
123
124 scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_;
125 bool battery_in_use_;
126 bool suspended_;
127
128 #if defined(ENABLE_BATTERY_MONITORING)
129 base::OneShotTimer<SystemMonitor> delayed_battery_check_;
130 #endif
131
132 #if defined(OS_MACOSX)
133 IONotificationPortRef notification_port_ref_;
134 io_object_t notifier_object_;
135 #endif
136
137 DISALLOW_COPY_AND_ASSIGN(SystemMonitor);
138 };
139
140 } // namespace base
141
142 #endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/system_monitor/system_monitor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698