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

Unified Diff: base/power_monitor/power_monitor.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: make HWNDMessageHandler as a friend class of PowerMonitor Created 8 years, 2 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_monitor/power_monitor.h
diff --git a/base/power_monitor/power_monitor.h b/base/power_monitor/power_monitor.h
new file mode 100644
index 0000000000000000000000000000000000000000..b8e98d9f4efe2e7880408fc7272e653b27b6edc9
--- /dev/null
+++ b/base/power_monitor/power_monitor.h
@@ -0,0 +1,129 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
vandebo (ex-Chrome) 2012/10/11 00:58:43 It'd be nice to have this file tracked as a move.
Hongbo Min 2012/10/11 07:57:53 Done. git cl upload have an option "--similarity"
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_POWER_MONITOR_POWER_MONITOR_H_
+#define BASE_POWER_MONITOR_POWER_MONITOR_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"
vandebo (ex-Chrome) 2012/10/11 00:58:43 Move this to line 10
Hongbo Min 2012/10/11 07:57:53 Done.
+#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 PowerMonitor {
+ 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.
+ };
+
+ PowerMonitor();
+ ~PowerMonitor();
+
+ // Callbacks will be called on the thread which creates the SystemMonitor.
vandebo (ex-Chrome) 2012/10/11 00:58:43 SystemMonitor -> PowerMonitor
Hongbo Min 2012/10/11 07:57:53 Done.
+ // 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 {
vandebo (ex-Chrome) 2012/10/11 00:58:43 per willchan's comment, move this to the top of th
Hongbo Min 2012/10/11 07:57:53 Done.
+ 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() {}
+ };
+
+ void AddPowerObserver(PowerObserver* observer);
vandebo (ex-Chrome) 2012/10/11 00:58:43 Add a copy of the comment from system_monitor.h:15
Hongbo Min 2012/10/11 07:57:53 Done.
+ void RemovePowerObserver(PowerObserver* observer);
+
+ // 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 PowerMonitor* Get();
vandebo (ex-Chrome) 2012/10/11 00:58:43 We should use Singleton instead of the custom Get
Hongbo Min 2012/10/11 07:57:53 Done.
+
+#if defined(OS_MACOSX)
vandebo (ex-Chrome) 2012/10/11 00:58:43 Move these init type methods to just below the con
Hongbo Min 2012/10/11 07:57:53 Done.
+ // Allocate system resources needed by the PowerMonitor 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:
+ friend class HWNDMessageHandler;
vandebo (ex-Chrome) 2012/10/11 00:58:43 Looks like the Mac implementation needs a friend f
Hongbo Min 2012/10/11 07:57:53 If making a friend function for Mac, it will intro
vandebo (ex-Chrome) 2012/10/11 20:49:57 Part of the point of refactoring is to remove/make
vandebo (ex-Chrome) 2012/10/12 00:15:51 If need be, you can create another function that m
+ friend class PowerMonitorTest_PowerNotifications_Test;
+
+#if defined(OS_MACOSX)
+ void PlatformInit();
+ void PlatformDestroy();
+#endif
+
+ // Handle the power event and notify its observers with the specific state.
+ void HandlePowerEvent(PowerEvent event_id);
+
+ // 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<PowerMonitor> delayed_battery_check_;
+#endif
+ scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_;
vandebo (ex-Chrome) 2012/10/11 00:58:43 power_observer_list_ -> observers_
Hongbo Min 2012/10/11 07:57:53 Done.
+ bool battery_in_use_;
+ bool suspended_;
+
+ DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
+};
+
+} // namespace base
+
+#endif // BASE_POWER_MONITOR_POWER_MONITOR_H_

Powered by Google App Engine
This is Rietveld 408576698