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

Unified Diff: base/power_monitor/power_monitor.cc

Issue 226263008: Revert of Attempting to resolve a race condition with PowerMonitor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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
« no previous file with comments | « base/power_monitor/power_monitor.h ('k') | base/power_monitor/power_monitor_source.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/power_monitor/power_monitor.cc
diff --git a/base/power_monitor/power_monitor.cc b/base/power_monitor/power_monitor.cc
index 9a53ba1936003a8161f09974e505b7ba92eeece1..14dc4b517832785c07153abfed2bcc4a3a738940 100644
--- a/base/power_monitor/power_monitor.cc
+++ b/base/power_monitor/power_monitor.cc
@@ -3,108 +3,59 @@
// found in the LICENSE file.
#include "base/power_monitor/power_monitor.h"
-
-#include "base/lazy_instance.h"
#include "base/power_monitor/power_monitor_source.h"
-#include "base/synchronization/lock.h"
namespace base {
-class PowerMonitorImpl;
+static PowerMonitor* g_power_monitor = NULL;
-LazyInstance<Lock>::Leaky g_power_monitor_lock = LAZY_INSTANCE_INITIALIZER;
-static PowerMonitorImpl* g_power_monitor = NULL;
-
-// A class used to monitor the power state change and notify the observers about
-// the change event.
-class BASE_EXPORT PowerMonitorImpl {
- public:
- explicit PowerMonitorImpl(scoped_ptr<PowerMonitorSource> source)
- : observers_(new ObserverListThreadSafe<PowerObserver>()),
- source_(source.Pass()) { }
-
- ~PowerMonitorImpl() { }
-
- scoped_refptr<ObserverListThreadSafe<PowerObserver> > observers_;
- scoped_ptr<PowerMonitorSource> source_;
-
- DISALLOW_COPY_AND_ASSIGN(PowerMonitorImpl);
-};
-
-void PowerMonitor::Initialize(scoped_ptr<PowerMonitorSource> source) {
- AutoLock(g_power_monitor_lock.Get());
+PowerMonitor::PowerMonitor(scoped_ptr<PowerMonitorSource> source)
+ : observers_(new ObserverListThreadSafe<PowerObserver>()),
+ source_(source.Pass()) {
DCHECK(!g_power_monitor);
- g_power_monitor = new PowerMonitorImpl(source.Pass());
+ g_power_monitor = this;
}
-void PowerMonitor::ShutdownForTesting() {
- AutoLock(g_power_monitor_lock.Get());
- if (g_power_monitor) {
- delete g_power_monitor;
- g_power_monitor = NULL;
- }
+PowerMonitor::~PowerMonitor() {
+ DCHECK_EQ(this, g_power_monitor);
+ g_power_monitor = NULL;
}
-bool PowerMonitor::IsInitialized() {
- AutoLock(g_power_monitor_lock.Get());
- return g_power_monitor != NULL;
+// static
+PowerMonitor* PowerMonitor::Get() {
+ return g_power_monitor;
}
-bool PowerMonitor::AddObserver(PowerObserver* observer) {
- AutoLock(g_power_monitor_lock.Get());
- if (!g_power_monitor)
- return false;
- g_power_monitor->observers_->AddObserver(observer);
- return true;
+void PowerMonitor::AddObserver(PowerObserver* obs) {
+ observers_->AddObserver(obs);
}
-bool PowerMonitor::RemoveObserver(PowerObserver* observer) {
- AutoLock(g_power_monitor_lock.Get());
- if (!g_power_monitor)
- return false;
- g_power_monitor->observers_->RemoveObserver(observer);
- return true;
+void PowerMonitor::RemoveObserver(PowerObserver* obs) {
+ observers_->RemoveObserver(obs);
+}
+
+PowerMonitorSource* PowerMonitor::Source() {
+ return source_.get();
}
bool PowerMonitor::IsOnBatteryPower() {
- g_power_monitor_lock.Get().AssertAcquired();
- if (!g_power_monitor)
- return false;
- return g_power_monitor->source_->IsOnBatteryPower();
-}
-
-Lock* PowerMonitor::GetLock() {
- return &g_power_monitor_lock.Get();
-}
-
-bool PowerMonitor::IsInitializedLocked() {
- g_power_monitor_lock.Get().AssertAcquired();
- return g_power_monitor != NULL;
-}
-
-PowerMonitorSource* PowerMonitor::GetSource() {
- g_power_monitor_lock.Get().AssertAcquired();
- return g_power_monitor->source_.get();
+ return source_->IsOnBatteryPower();
}
void PowerMonitor::NotifyPowerStateChange(bool battery_in_use) {
DVLOG(1) << "PowerStateChange: " << (battery_in_use ? "On" : "Off")
<< " battery";
- g_power_monitor_lock.Get().AssertAcquired();
- g_power_monitor->observers_->Notify(&PowerObserver::OnPowerStateChange,
- battery_in_use);
+ observers_->Notify(&PowerObserver::OnPowerStateChange, battery_in_use);
}
void PowerMonitor::NotifySuspend() {
DVLOG(1) << "Power Suspending";
- g_power_monitor_lock.Get().AssertAcquired();
- g_power_monitor->observers_->Notify(&PowerObserver::OnSuspend);
+ observers_->Notify(&PowerObserver::OnSuspend);
}
void PowerMonitor::NotifyResume() {
DVLOG(1) << "Power Resuming";
- g_power_monitor_lock.Get().AssertAcquired();
- g_power_monitor->observers_->Notify(&PowerObserver::OnResume);
+ observers_->Notify(&PowerObserver::OnResume);
}
} // namespace base
« no previous file with comments | « base/power_monitor/power_monitor.h ('k') | base/power_monitor/power_monitor_source.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698