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

Unified Diff: base/power_state_manager.cc

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.cc
diff --git a/base/power_state_manager.cc b/base/power_state_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4d119a0a68546c4fa35fab35a3ecc5d0698d7c64
--- /dev/null
+++ b/base/power_state_manager.cc
@@ -0,0 +1,102 @@
+// 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.
+
+#include "base/power_state_manager.h"
+
+namespace base {
+
+static PowerStateManager* g_power_state_manager = NULL;
+
+#if defined(ENABLE_BATTERY_MONITORING)
+// The amount of time (in ms) to wait before running the initial
+// battery check.
+static int kDelayedBatteryCheckMs = 10 * 1000;
+#endif // defined(ENABLE_BATTERY_MONITORING)
+
+PowerStateManager::PowerStateManager()
+ : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()),
+ battery_in_use_(false),
+ suspended_(false) {
+ DCHECK(!g_power_state_manager);
+ g_power_state_manager = this;
+
+ DCHECK(MessageLoop::current());
+#if defined(ENABLE_BATTERY_MONITORING)
+ delayed_battery_check_.Start(FROM_HERE,
+ base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this,
+ &PowerStateManager::BatteryCheck);
+#endif // defined(ENABLE_BATTERY_MONITORING)
+
+#if defined(OS_MACOSX)
+ PlatformInit();
+#endif
+}
+
+PowerStateManager::~PowerStateManager() {
+#if defined(OS_MACOSX)
+ PlaformDestory();
+#endif
+ DCHECK_EQ(this, g_power_state_manager);
+ g_power_state_manager = NULL;
+}
+
+void PowerStateManager::AddPowerObserver(PowerObserver* obs) {
+ power_observer_list_->AddObserver(obs);
+}
+
+void PowerStateManager::RemovePowerObserver(PowerObserver* obs) {
+ power_observer_list_->RemoveObserver(obs);
+}
+
+void PowerStateManager::HandlePowerEvent(PowerEvent event_id) {
+ // Suppress duplicate notifications. Some platforms may
+ // send multiple notifications of the same event.
+ switch (event_id) {
+ case POWER_STATE_EVENT:
+ {
+ bool on_battery = IsBatteryPower();
+ if (on_battery != battery_in_use_) {
+ battery_in_use_ = on_battery;
+ NotifyPowerStateChange();
+ }
+ }
+ break;
+ case RESUME_EVENT:
+ if (suspended_) {
+ suspended_ = false;
+ NotifyResume();
+ }
+ break;
+ case SUSPEND_EVENT:
+ if (!suspended_) {
+ suspended_ = true;
+ NotifySuspend();
+ }
+ break;
+ }
+}
+
+void PowerStateManager::NotifyPowerStateChange() {
+ DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off")
+ << " battery";
+ power_observer_list_->Notify(&PowerObserver::OnPowerStateChange,
+ BatteryPower());
+}
+
+void PowerStateManager::NotifySuspend() {
+ DVLOG(1) << "Power Suspending";
+ power_observer_list_->Notify(&PowerObserver::OnSuspend);
+}
+
+void PowerStateManager::NotifyResume() {
+ DVLOG(1) << "Power Resuming";
+ power_observer_list_->Notify(&PowerObserver::OnResume);
+}
+
+void PowerStateManager::BatteryCheck() {
+ HandlePowerEvent(PowerStateManager::POWER_STATE_EVENT);
+}
+
+} // namespace base
+

Powered by Google App Engine
This is Rietveld 408576698