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

Unified Diff: base/power_monitor/power_monitor.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: update Created 8 years, 1 month 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.cc
diff --git a/base/system_monitor/system_monitor.cc b/base/power_monitor/power_monitor.cc
similarity index 28%
copy from base/system_monitor/system_monitor.cc
copy to base/power_monitor/power_monitor.cc
index 2616275dfbc64db88abaa6abb0fa85e8cf561a7d..0241dfe1e6503aae37e30222b512890f880949a0 100644
--- a/base/system_monitor/system_monitor.cc
+++ b/base/power_monitor/power_monitor.cc
@@ -2,19 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/system_monitor/system_monitor.h"
-
-#include <utility>
-
-#include "base/logging.h"
-#include "base/message_loop.h"
-#include "base/stl_util.h"
-#include "base/time.h"
-#include "base/utf_string_conversions.h"
+#include "base/power_monitor/power_monitor.h"
namespace base {
-static SystemMonitor* g_system_monitor = NULL;
+static PowerMonitor* g_power_monitor = NULL;
#if defined(ENABLE_BATTERY_MONITORING)
// The amount of time (in ms) to wait before running the initial
@@ -22,52 +14,58 @@ static SystemMonitor* g_system_monitor = NULL;
static int kDelayedBatteryCheckMs = 10 * 1000;
#endif // defined(ENABLE_BATTERY_MONITORING)
-SystemMonitor::RemovableStorageInfo::RemovableStorageInfo() {
-}
-
-SystemMonitor::RemovableStorageInfo::RemovableStorageInfo(
- const std::string& id,
- const string16& device_name,
- const FilePath::StringType& device_location)
- : device_id(id),
- name(device_name),
- location(device_location) {
-}
-
-SystemMonitor::SystemMonitor()
- : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()),
- devices_changed_observer_list_(
- new ObserverListThreadSafe<DevicesChangedObserver>()),
+PowerMonitor::PowerMonitor()
+ : observers_(new ObserverListThreadSafe<PowerObserver>()),
battery_in_use_(false),
- suspended_(false) {
- DCHECK(!g_system_monitor);
- g_system_monitor = this;
+ suspended_(false),
+ signaler_available_(true) {
+ DCHECK(!g_power_monitor);
+ g_power_monitor = this;
DCHECK(MessageLoop::current());
#if defined(ENABLE_BATTERY_MONITORING)
delayed_battery_check_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this,
- &SystemMonitor::BatteryCheck);
+ &PowerMonitor::BatteryCheck);
#endif // defined(ENABLE_BATTERY_MONITORING)
+
#if defined(OS_MACOSX)
PlatformInit();
#endif
}
-SystemMonitor::~SystemMonitor() {
+PowerMonitor::~PowerMonitor() {
#if defined(OS_MACOSX)
PlatformDestroy();
#endif
- DCHECK_EQ(this, g_system_monitor);
- g_system_monitor = NULL;
+ DCHECK_EQ(this, g_power_monitor);
+ g_power_monitor = NULL;
}
// static
-SystemMonitor* SystemMonitor::Get() {
- return g_system_monitor;
+PowerMonitor* PowerMonitor::Get() {
+ return g_power_monitor;
+}
+
+PowerMonitor::Signaler* PowerMonitor::GetSignalerOnce() {
+ if (signaler_available_) {
willchan no longer on Chromium 2012/11/02 21:35:27 indentation off
Hongbo Min 2012/11/05 09:36:42 Done.
+ signaler_available_ = false;
+ return new PowerMonitor::Signaler();
+ }
+
+ // Return NULL after the first time it get called.
+ return NULL;
}
-void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
+void PowerMonitor::AddObserver(PowerObserver* obs) {
+ observers_->AddObserver(obs);
+}
+
+void PowerMonitor::RemoveObserver(PowerObserver* obs) {
+ observers_->RemoveObserver(obs);
+}
+
+void PowerMonitor::ProcessPowerEvent(PowerEvent event_id) {
// Suppress duplicate notifications. Some platforms may
// send multiple notifications of the same event.
switch (event_id) {
@@ -95,108 +93,46 @@ void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) {
}
}
-void SystemMonitor::ProcessDevicesChanged(DeviceType device_type) {
- NotifyDevicesChanged(device_type);
-}
-
-void SystemMonitor::ProcessRemovableStorageAttached(
- const std::string& id,
- const string16& name,
- const FilePath::StringType& location) {
- {
- base::AutoLock lock(removable_storage_lock_);
- if (ContainsKey(removable_storage_map_, id)) {
- // This can happen if our unique id scheme fails. Ignore the incoming
- // non-unique attachment.
- return;
- }
- RemovableStorageInfo info(id, name, location);
- removable_storage_map_.insert(std::make_pair(id, info));
- }
- NotifyRemovableStorageAttached(id, name, location);
-}
-
-void SystemMonitor::ProcessRemovableStorageDetached(const std::string& id) {
- {
- base::AutoLock lock(removable_storage_lock_);
- RemovableStorageMap::iterator it = removable_storage_map_.find(id);
- if (it == removable_storage_map_.end())
- return;
- removable_storage_map_.erase(it);
- }
- NotifyRemovableStorageDetached(id);
-}
-
-std::vector<SystemMonitor::RemovableStorageInfo>
-SystemMonitor::GetAttachedRemovableStorage() const {
- std::vector<RemovableStorageInfo> results;
-
- base::AutoLock lock(removable_storage_lock_);
- for (RemovableStorageMap::const_iterator it = removable_storage_map_.begin();
- it != removable_storage_map_.end();
- ++it) {
- results.push_back(it->second);
- }
- return results;
-}
-
-void SystemMonitor::AddPowerObserver(PowerObserver* obs) {
- power_observer_list_->AddObserver(obs);
-}
-
-void SystemMonitor::RemovePowerObserver(PowerObserver* obs) {
- power_observer_list_->RemoveObserver(obs);
-}
-
-void SystemMonitor::AddDevicesChangedObserver(DevicesChangedObserver* obs) {
- devices_changed_observer_list_->AddObserver(obs);
+void PowerMonitor::NotifyPowerStateChange() {
+ DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off")
+ << " battery";
+ observers_->Notify(&PowerObserver::OnPowerStateChange,
+ BatteryPower());
}
-void SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) {
- devices_changed_observer_list_->RemoveObserver(obs);
+void PowerMonitor::NotifySuspend() {
+ DVLOG(1) << "Power Suspending";
+ observers_->Notify(&PowerObserver::OnSuspend);
}
-void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) {
- DVLOG(1) << "DevicesChanged with device type " << device_type;
- devices_changed_observer_list_->Notify(
- &DevicesChangedObserver::OnDevicesChanged, device_type);
+void PowerMonitor::NotifyResume() {
+ DVLOG(1) << "Power Resuming";
+ observers_->Notify(&PowerObserver::OnResume);
}
-void SystemMonitor::NotifyRemovableStorageAttached(
- const std::string& id,
- const string16& name,
- const FilePath::StringType& location) {
- DVLOG(1) << "RemovableStorageAttached with name " << UTF16ToUTF8(name)
- << " and id " << id;
- devices_changed_observer_list_->Notify(
- &DevicesChangedObserver::OnRemovableStorageAttached, id, name, location);
+void PowerMonitor::BatteryCheck() {
+ ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT);
}
-void SystemMonitor::NotifyRemovableStorageDetached(const std::string& id) {
- DVLOG(1) << "RemovableStorageDetached for id " << id;
- devices_changed_observer_list_->Notify(
- &DevicesChangedObserver::OnRemovableStorageDetached, id);
-}
+// PowerMonitor::Signaler
-void SystemMonitor::NotifyPowerStateChange() {
- DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off")
- << " battery";
- power_observer_list_->Notify(&PowerObserver::OnPowerStateChange,
- BatteryPower());
+PowerMonitor::Signaler::Signaler() {
}
-void SystemMonitor::NotifySuspend() {
- DVLOG(1) << "Power Suspending";
- power_observer_list_->Notify(&PowerObserver::OnSuspend);
+PowerMonitor::Signaler::~Signaler() {
}
-void SystemMonitor::NotifyResume() {
- DVLOG(1) << "Power Resuming";
- power_observer_list_->Notify(&PowerObserver::OnResume);
+#if defined(OS_WIN)
+void PowerMonitor::Signaler::ProcessWmPowerBroadcastMessage(int event) {
+ if (PowerMonitor::Get())
+ PowerMonitor::Get()->ProcessWmPowerBroadcastMessage(event);
}
+#endif
-void SystemMonitor::BatteryCheck() {
- ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
+void PowerMonitor::Signaler::ProcessPowerEvent(PowerEvent event) {
+ if (PowerMonitor::Get())
+ PowerMonitor::Get()->ProcessPowerEvent(event);
}
} // namespace base
+

Powered by Google App Engine
This is Rietveld 408576698