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

Unified Diff: base/power_state_manager.h

Issue 10959020: SystemMonitor refactoring: move power state monitor into a separate class called PowerMonitor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: base/power_state_manager.h
diff --git a/base/power_state_manager.h b/base/power_state_manager.h
new file mode 100644
index 0000000000000000000000000000000000000000..bbf77e0c2150979e071ce4cfdc50a60faefec83c
--- /dev/null
+++ b/base/power_state_manager.h
@@ -0,0 +1,126 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_POWER_STATE_MANAGMER_H_
+#define BASE_POWER_STATE_MANAGMER_H_
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+
+// Windows HiRes timers drain the battery faster so we need to know the battery
+// status. This isn't true for other platforms.
+#if defined(OS_WIN)
+#define ENABLE_BATTERY_MONITORING 1
+#else
+#undef ENABLE_BATTERY_MONITORING
+#endif // !OS_WIN
+
+#include "base/observer_list_threadsafe.h"
+#if defined(ENABLE_BATTERY_MONITORING)
+#include "base/timer.h"
+#endif // defined(ENABLE_BATTERY_MONITORING)
+
+#if defined(OS_IOS)
+#include <objc/runtime.h>
+#endif // OS_IOS
+
+namespace base {
+
+class BASE_EXPORT PowerStateManager {
+ public:
+ // Normalized list of power events.
+ enum PowerEvent {
+ POWER_STATE_EVENT, // The Power status of the system has changed.
+ SUSPEND_EVENT, // The system is being suspended.
+ RESUME_EVENT // The system is being resumed.
+ };
+
+ // Callbacks will be called on the thread which creates the SystemMonitor.
+ // During the callback, Add/RemoveObserver will block until the callbacks
+ // are finished. Observers should implement quick callback functions; if
+ // lengthy operations are needed, the observer should take care to invoke
+ // the operation on an appropriate thread.
+ class BASE_EXPORT PowerObserver {
+ public:
+ // Notification of a change in power status of the computer, such
+ // as from switching between battery and A/C power.
+ virtual void OnPowerStateChange(bool on_battery_power) {}
+
+ // Notification that the system is suspending.
+ virtual void OnSuspend() {}
+
+ // Notification that the system is resuming.
+ virtual void OnResume() {}
+
+ protected:
+ virtual ~PowerObserver() {}
+ };
+
+ PowerStateManager();
+ ~PowerStateManager();
+
+ void AddPowerObserver(PowerObserver* observer);
+ void RemovePowerObserver(PowerObserver* observer);
+
+ // Handle the power event and notify its observers with the specific state.
+ void HandlePowerEvent(PowerEvent event_id);
+
+ // Is the computer currently on battery power. Can be called on any thread.
+ bool BatteryPower() const {
+ // Using a lock here is not necessary for just a bool.
+ return battery_in_use_;
+ }
+
+ // Return the single instance in the application wide.
+ static PowerStateManager* Get();
+
+#if defined(OS_MACOSX)
+ // Allocate system resources needed by the PowerStateManager class.
+ //
+ // This function must be called before instantiating an instance of the class
+ // and before the Sandbox is initialized.
+#if !defined(OS_IOS)
+ static void AllocateSystemIOPorts();
+#else
+ static void AllocateSystemIOPorts() {}
+#endif // OS_IOS
+#endif // OS_MACOSX
+
+ private:
+#if defined(OS_MACOSX)
+ void PlatformInit();
+ void PlatformDestroy();
+#endif
+
+ // Platform-specific method to check whether the system is currently
+ // running on battery power. Returns true if running on batteries,
+ // false otherwise.
+ bool IsBatteryPower();
+
+ // Checks the battery status and notifies observers if the battery
+ // status has changed.
+ void BatteryCheck();
+
+ void NotifyPowerStateChange();
+ void NotifySuspend();
+ void NotifyResume();
+
+#if defined(OS_IOS)
+ // Holds pointers to system event notification observers.
+ std::vector<id> notification_observers_;
+#endif
+
+#if defined(ENABLE_BATTERY_MONITORING)
+ base::OneShotTimer<SystemMonitor> delayed_battery_check_;
+#endif
+ scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_;
+ bool battery_in_use_;
+ bool suspended_;
+
+ DISALLOW_COPY_AND_ASSIGN(PowerStateManager);
+};
+
+} // namespace base
+
+#endif // BASE_POWER_STATE_MANAGMER_H_

Powered by Google App Engine
This is Rietveld 408576698