diff options
author | scottmg@chromium.org <scottmg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-15 01:59:49 +0000 |
---|---|---|
committer | scottmg@chromium.org <scottmg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-15 01:59:49 +0000 |
commit | cd1cd4c0ad92e4c290aa24a81bb83bcf65d035c4 (patch) | |
tree | 16e5774afb6b57028244d868491d711bbec695d5 /base/system_monitor | |
parent | fd6583a2e3735e64abbe8e3743bc90caed73397e (diff) | |
download | chromium_src-cd1cd4c0ad92e4c290aa24a81bb83bcf65d035c4.zip chromium_src-cd1cd4c0ad92e4c290aa24a81bb83bcf65d035c4.tar.gz chromium_src-cd1cd4c0ad92e4c290aa24a81bb83bcf65d035c4.tar.bz2 |
Send WM_DEVICECHANGE message through SystemMonitor
WM_DEVICECHANGE is sent when there's been a change to devices or the computer;
specifically when a USB device is connected or disconnected. This is intended
for use in support of Gamepads for more performant polling and
connect/disconnect testing. Currently only on Windows, though seems reasonable
to add for other platforms in the future.
BUG=79050
Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=109960
Review URL: http://codereview.chromium.org/8523021
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110014 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/system_monitor')
-rw-r--r-- | base/system_monitor/system_monitor.cc | 37 | ||||
-rw-r--r-- | base/system_monitor/system_monitor.h | 23 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_unittest.cc | 47 |
3 files changed, 95 insertions, 12 deletions
diff --git a/base/system_monitor/system_monitor.cc b/base/system_monitor/system_monitor.cc index 5131fbf..28fc70b 100644 --- a/base/system_monitor/system_monitor.cc +++ b/base/system_monitor/system_monitor.cc @@ -19,7 +19,9 @@ static int kDelayedBatteryCheckMs = 10 * 1000; #endif // defined(ENABLE_BATTERY_MONITORING) SystemMonitor::SystemMonitor() - : observer_list_(new ObserverListThreadSafe<PowerObserver>()), + : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), + devices_changed_observer_list_( + new ObserverListThreadSafe<DevicesChangedObserver>()), battery_in_use_(false), suspended_(false) { DCHECK(!g_system_monitor); @@ -77,28 +79,47 @@ void SystemMonitor::ProcessPowerMessage(PowerEvent event_id) { } } -void SystemMonitor::AddObserver(PowerObserver* obs) { - observer_list_->AddObserver(obs); +void SystemMonitor::ProcessDevicesChanged() { + NotifyDevicesChanged(); } -void SystemMonitor::RemoveObserver(PowerObserver* obs) { - observer_list_->RemoveObserver(obs); +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 SystemMonitor::RemoveDevicesChangedObserver(DevicesChangedObserver* obs) { + devices_changed_observer_list_->RemoveObserver(obs); +} + +void SystemMonitor::NotifyDevicesChanged() { + DVLOG(1) << "DevicesChanged"; + devices_changed_observer_list_->Notify( + &DevicesChangedObserver::OnDevicesChanged); } void SystemMonitor::NotifyPowerStateChange() { DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") << " battery"; - observer_list_->Notify(&PowerObserver::OnPowerStateChange, BatteryPower()); + power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, + BatteryPower()); } void SystemMonitor::NotifySuspend() { DVLOG(1) << "Power Suspending"; - observer_list_->Notify(&PowerObserver::OnSuspend); + power_observer_list_->Notify(&PowerObserver::OnSuspend); } void SystemMonitor::NotifyResume() { DVLOG(1) << "Power Resuming"; - observer_list_->Notify(&PowerObserver::OnResume); + power_observer_list_->Notify(&PowerObserver::OnResume); } void SystemMonitor::BatteryCheck() { diff --git a/base/system_monitor/system_monitor.h b/base/system_monitor/system_monitor.h index c779994..7684523 100644 --- a/base/system_monitor/system_monitor.h +++ b/base/system_monitor/system_monitor.h @@ -90,15 +90,26 @@ class BASE_EXPORT SystemMonitor { virtual ~PowerObserver() {} }; + class BASE_EXPORT DevicesChangedObserver { + public: + // Notification that the devices connected to the system have changed. + virtual void OnDevicesChanged() {} + + protected: + virtual ~DevicesChangedObserver() {} + }; + // Add a new observer. // Can be called from any thread. // Must not be called from within a notification callback. - void AddObserver(PowerObserver* obs); + void AddPowerObserver(PowerObserver* obs); + void AddDevicesChangedObserver(DevicesChangedObserver* obs); // Remove an existing observer. // Can be called from any thread. // Must not be called from within a notification callback. - void RemoveObserver(PowerObserver* obs); + void RemovePowerObserver(PowerObserver* obs); + void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); #if defined(OS_WIN) // Windows-specific handling of a WM_POWERBROADCAST message. @@ -110,6 +121,9 @@ class BASE_EXPORT SystemMonitor { // Cross-platform handling of a power event. void ProcessPowerMessage(PowerEvent event_id); + // Cross-platform handling of a device change event. + void ProcessDevicesChanged(); + private: #if defined(OS_MACOSX) void PlatformInit(); @@ -126,11 +140,14 @@ class BASE_EXPORT SystemMonitor { void BatteryCheck(); // Functions to trigger notifications. + void NotifyDevicesChanged(); void NotifyPowerStateChange(); void NotifySuspend(); void NotifyResume(); - scoped_refptr<ObserverListThreadSafe<PowerObserver> > observer_list_; + scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; + scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > + devices_changed_observer_list_; bool battery_in_use_; bool suspended_; diff --git a/base/system_monitor/system_monitor_unittest.cc b/base/system_monitor/system_monitor_unittest.cc index f4a4e73..1d5d6af 100644 --- a/base/system_monitor/system_monitor_unittest.cc +++ b/base/system_monitor/system_monitor_unittest.cc @@ -55,7 +55,7 @@ TEST(SystemMonitor, PowerNotifications) { SystemMonitor system_monitor; PowerTest test[kObservers]; for (int index = 0; index < kObservers; ++index) - system_monitor.AddObserver(&test[index]); + system_monitor.AddPowerObserver(&test[index]); // Send a bunch of power changes. Since the battery power hasn't // actually changed, we shouldn't get notifications. @@ -90,4 +90,49 @@ TEST(SystemMonitor, PowerNotifications) { EXPECT_EQ(test[0].resumes(), 1); } +class DevicesChangedTest : public SystemMonitor::DevicesChangedObserver { + public: + DevicesChangedTest() + : changes_(0) { + } + + // DevicesChangedObserver callbacks. + virtual void OnDevicesChanged() OVERRIDE { + changes_++; + } + + // Test status counts. + int changes() const { return changes_; } + + private: + int changes_; // Count of OnDevicesChanged notifications. + + DISALLOW_COPY_AND_ASSIGN(DevicesChangedTest); +}; + +TEST(SystemMonitor, DeviceChangeNotifications) { + const int kObservers = 5; + + // Initialize a message loop for this to run on. + MessageLoop loop; + +#if defined(OS_MACOSX) + SystemMonitor::AllocateSystemIOPorts(); +#endif + + SystemMonitor system_monitor; + DevicesChangedTest test[kObservers]; + for (int index = 0; index < kObservers; ++index) + system_monitor.AddDevicesChangedObserver(&test[index]); + + system_monitor.ProcessDevicesChanged(); + loop.RunAllPending(); + EXPECT_EQ(1, test[0].changes()); + + system_monitor.ProcessDevicesChanged(); + system_monitor.ProcessDevicesChanged(); + loop.RunAllPending(); + EXPECT_EQ(3, test[0].changes()); +} + } // namespace base |