diff options
author | hongbo.min@intel.com <hongbo.min@intel.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-03 19:06:51 +0000 |
---|---|---|
committer | hongbo.min@intel.com <hongbo.min@intel.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-03 19:06:51 +0000 |
commit | 6c5905b763d18bf2d81e2d1f4aac7f16ae32f6ea (patch) | |
tree | 86297e816ccbbe8dd10ab6fec01b350be86f63ea /base/system_monitor | |
parent | 617b65edba554089efd872e9f59f01ae964690ab (diff) | |
download | chromium_src-6c5905b763d18bf2d81e2d1f4aac7f16ae32f6ea.zip chromium_src-6c5905b763d18bf2d81e2d1f4aac7f16ae32f6ea.tar.gz chromium_src-6c5905b763d18bf2d81e2d1f4aac7f16ae32f6ea.tar.bz2 |
Split the power monitoring feature from SystemMonitor
The SystemMonitor is a mixed monitor which not only monitors
the power state changes but also the devices changes. This
patch is to separate the power monitor from SystemMonitor
as a new class PowerMonitor which is dedicated to monitor
power state.
The next step is to seek a opportunity to refactor SystemMonitor
as something like DeviceMonitor.
BUG=149059
TEST=base_unittests --gtest_filter=PowerMonitorTest.*
Review URL: https://chromiumcodereview.appspot.com/10959020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@192114 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/system_monitor')
-rw-r--r-- | base/system_monitor/system_monitor.cc | 83 | ||||
-rw-r--r-- | base/system_monitor/system_monitor.h | 140 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_android.cc | 36 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_android.h | 17 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_ios.mm | 40 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_mac.mm | 98 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_posix.cc | 14 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_unittest.cc | 76 | ||||
-rw-r--r-- | base/system_monitor/system_monitor_win.cc | 111 |
9 files changed, 4 insertions, 611 deletions
diff --git a/base/system_monitor/system_monitor.cc b/base/system_monitor/system_monitor.cc index 7c960cc..b934139 100644 --- a/base/system_monitor/system_monitor.cc +++ b/base/system_monitor/system_monitor.cc @@ -14,36 +14,14 @@ namespace base { static SystemMonitor* g_system_monitor = 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) - SystemMonitor::SystemMonitor() - : power_observer_list_(new ObserverListThreadSafe<PowerObserver>()), - devices_changed_observer_list_( - new ObserverListThreadSafe<DevicesChangedObserver>()), - battery_in_use_(false), - suspended_(false) { + : devices_changed_observer_list_( + new ObserverListThreadSafe<DevicesChangedObserver>()) { DCHECK(!g_system_monitor); g_system_monitor = this; - - DCHECK(MessageLoop::current()); -#if defined(ENABLE_BATTERY_MONITORING) - delayed_battery_check_.Start(FROM_HERE, - base::TimeDelta::FromMilliseconds(kDelayedBatteryCheckMs), this, - &SystemMonitor::BatteryCheck); -#endif // defined(ENABLE_BATTERY_MONITORING) -#if defined(OS_MACOSX) - PlatformInit(); -#endif } SystemMonitor::~SystemMonitor() { -#if defined(OS_MACOSX) - PlatformDestroy(); -#endif DCHECK_EQ(this, g_system_monitor); g_system_monitor = NULL; } @@ -53,46 +31,10 @@ SystemMonitor* SystemMonitor::Get() { return g_system_monitor; } -void SystemMonitor::ProcessPowerMessage(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 SystemMonitor::ProcessDevicesChanged(DeviceType device_type) { NotifyDevicesChanged(device_type); } -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); } @@ -107,25 +49,4 @@ void SystemMonitor::NotifyDevicesChanged(DeviceType device_type) { &DevicesChangedObserver::OnDevicesChanged, device_type); } -void SystemMonitor::NotifyPowerStateChange() { - DVLOG(1) << "PowerStateChange: " << (BatteryPower() ? "On" : "Off") - << " battery"; - power_observer_list_->Notify(&PowerObserver::OnPowerStateChange, - BatteryPower()); -} - -void SystemMonitor::NotifySuspend() { - DVLOG(1) << "Power Suspending"; - power_observer_list_->Notify(&PowerObserver::OnSuspend); -} - -void SystemMonitor::NotifyResume() { - DVLOG(1) << "Power Resuming"; - power_observer_list_->Notify(&PowerObserver::OnResume); -} - -void SystemMonitor::BatteryCheck() { - ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); -} - } // namespace base diff --git a/base/system_monitor/system_monitor.h b/base/system_monitor/system_monitor.h index 10f279f..5dd849f 100644 --- a/base/system_monitor/system_monitor.h +++ b/base/system_monitor/system_monitor.h @@ -5,35 +5,11 @@ #ifndef BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ #define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_H_ -#include <map> -#include <string> -#include <vector> - #include "base/base_export.h" #include "base/basictypes.h" -#include "build/build_config.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/memory/ref_counted.h" #include "base/observer_list_threadsafe.h" -#if defined(ENABLE_BATTERY_MONITORING) -#include "base/timer.h" -#endif // defined(ENABLE_BATTERY_MONITORING) - -#if defined(OS_MACOSX) && !defined(OS_IOS) -#include <IOKit/pwr_mgt/IOPMLib.h> -#include <IOKit/IOMessage.h> -#endif // OS_MACOSX && !OS_IOS - -#if defined(OS_IOS) -#include <objc/runtime.h> -#endif // OS_IOS +#include "build/build_config.h" namespace base { @@ -42,13 +18,6 @@ namespace base { // TODO(mbelshe): Add support beyond just power management. class BASE_EXPORT SystemMonitor { 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. - }; - // Type of devices whose change need to be monitored, such as add/remove. enum DeviceType { DEVTYPE_AUDIO_CAPTURE, // Audio capture device, e.g., microphone. @@ -64,50 +33,6 @@ class BASE_EXPORT SystemMonitor { // Get the application-wide SystemMonitor (if not present, returns NULL). static SystemMonitor* Get(); -#if defined(OS_MACOSX) - // Allocate system resources needed by the SystemMonitor 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 - - // - // Power-related APIs - // - - // 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_; - } - - // Callbacks will be called on the thread which creates the SystemMonitor. - // 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 { - 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() {} - }; - class BASE_EXPORT DevicesChangedObserver { public: // Notification that the devices connected to the system have changed. @@ -121,87 +46,26 @@ class BASE_EXPORT SystemMonitor { // Add a new observer. // Can be called from any thread. // Must not be called from within a notification callback. - 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 RemovePowerObserver(PowerObserver* obs); void RemoveDevicesChangedObserver(DevicesChangedObserver* obs); // The ProcessFoo() style methods are a broken pattern and should not // be copied. Any significant addition to this class is blocked on // refactoring to improve the state of affairs. See http://crbug.com/149059 - // Cross-platform handling of a power event. - void ProcessPowerMessage(PowerEvent event_id); - // Cross-platform handling of a device change event. void ProcessDevicesChanged(DeviceType device_type); private: -#if defined(OS_WIN) - // Represents a message-only window for power message handling on Windows. - // Only allow SystemMonitor to create it. - class PowerMessageWindow { - public: - PowerMessageWindow(); - ~PowerMessageWindow(); - - private: - void ProcessWmPowerBroadcastMessage(int event_id); - LRESULT CALLBACK WndProc(HWND hwnd, UINT message, - WPARAM wparam, LPARAM lparam); - static LRESULT CALLBACK WndProcThunk(HWND hwnd, - UINT message, - WPARAM wparam, - LPARAM lparam); - // Instance of the module containing the window procedure. - HMODULE instance_; - // A hidden message-only window. - HWND message_hwnd_; - }; -#endif - -#if defined(OS_MACOSX) - void PlatformInit(); - void PlatformDestroy(); -#endif - - // 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(); - // Functions to trigger notifications. void NotifyDevicesChanged(DeviceType device_type); - void NotifyPowerStateChange(); - void NotifySuspend(); - void NotifyResume(); - scoped_refptr<ObserverListThreadSafe<PowerObserver> > power_observer_list_; scoped_refptr<ObserverListThreadSafe<DevicesChangedObserver> > devices_changed_observer_list_; - bool battery_in_use_; - bool suspended_; - -#if defined(ENABLE_BATTERY_MONITORING) - base::OneShotTimer<SystemMonitor> delayed_battery_check_; -#endif - -#if defined(OS_IOS) - // Holds pointers to system event notification observers. - std::vector<id> notification_observers_; -#endif - -#if defined(OS_WIN) - PowerMessageWindow power_message_window_; -#endif DISALLOW_COPY_AND_ASSIGN(SystemMonitor); }; diff --git a/base/system_monitor/system_monitor_android.cc b/base/system_monitor/system_monitor_android.cc deleted file mode 100644 index f29b7b0..0000000 --- a/base/system_monitor/system_monitor_android.cc +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2011 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/system_monitor/system_monitor.h" -#include "jni/SystemMonitor_jni.h" - -namespace base { - -namespace android { - -// Native implementation of SystemMonitor.java. -void OnBatteryChargingChanged(JNIEnv* env, jclass clazz) { - SystemMonitor::Get()->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); -} - -void OnMainActivityResumed(JNIEnv* env, jclass clazz) { - SystemMonitor::Get()->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); -} - -void OnMainActivitySuspended(JNIEnv* env, jclass clazz) { - SystemMonitor::Get()->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); -} - -} // namespace android - -bool SystemMonitor::IsBatteryPower() { - JNIEnv* env = base::android::AttachCurrentThread(); - return base::android::Java_SystemMonitor_isBatteryPower(env); -} - -bool RegisterSystemMonitor(JNIEnv* env) { - return base::android::RegisterNativesImpl(env); -} - -} // namespace base diff --git a/base/system_monitor/system_monitor_android.h b/base/system_monitor/system_monitor_android.h deleted file mode 100644 index 9f60560..0000000 --- a/base/system_monitor/system_monitor_android.h +++ /dev/null @@ -1,17 +0,0 @@ -// 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. - -#ifndef BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_ -#define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_ - -#include <jni.h> - -namespace base { - -// Registers the JNI bindings for SystemMonitor. -bool RegisterSystemMonitor(JNIEnv* env); - -} // namespace base - -#endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_ diff --git a/base/system_monitor/system_monitor_ios.mm b/base/system_monitor/system_monitor_ios.mm deleted file mode 100644 index f3251b6..0000000 --- a/base/system_monitor/system_monitor_ios.mm +++ /dev/null @@ -1,40 +0,0 @@ -// 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/system_monitor/system_monitor.h" - -#import <UIKit/UIKit.h> - -namespace base { - -void SystemMonitor::PlatformInit() { - NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; - id foreground = - [nc addObserverForName:UIApplicationWillEnterForegroundNotification - object:nil - queue:nil - usingBlock:^(NSNotification* notification) { - ProcessPowerMessage(RESUME_EVENT); - }]; - id background = - [nc addObserverForName:UIApplicationDidEnterBackgroundNotification - object:nil - queue:nil - usingBlock:^(NSNotification* notification) { - ProcessPowerMessage(SUSPEND_EVENT); - }]; - notification_observers_.push_back(foreground); - notification_observers_.push_back(background); -} - -void SystemMonitor::PlatformDestroy() { - NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; - for (std::vector<id>::iterator it = notification_observers_.begin(); - it != notification_observers_.end(); ++it) { - [nc removeObserver:*it]; - } - notification_observers_.clear(); -} - -} // namespace base diff --git a/base/system_monitor/system_monitor_mac.mm b/base/system_monitor/system_monitor_mac.mm deleted file mode 100644 index d0dbaab..0000000 --- a/base/system_monitor/system_monitor_mac.mm +++ /dev/null @@ -1,98 +0,0 @@ -// 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. - -// Implementation based on sample code from -// http://developer.apple.com/library/mac/#qa/qa1340/_index.html. - -#include "base/system_monitor/system_monitor.h" - -#include <IOKit/pwr_mgt/IOPMLib.h> -#include <IOKit/IOMessage.h> - -namespace base { - -namespace { - -io_connect_t g_system_power_io_port = 0; -IONotificationPortRef g_notification_port_ref = 0; -io_object_t g_notifier_object = 0; - -void SystemPowerEventCallback(void*, - io_service_t service, - natural_t message_type, - void* message_argument) { - SystemMonitor* sys_monitor = SystemMonitor::Get(); - DCHECK(sys_monitor); - switch (message_type) { - case kIOMessageSystemWillSleep: - sys_monitor->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); - IOAllowPowerChange(g_system_power_io_port, - reinterpret_cast<intptr_t>(message_argument)); - break; - - case kIOMessageSystemWillPowerOn: - sys_monitor->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - break; - } -} - -} // namespace - -// The reason we can't include this code in the constructor is because -// PlatformInit() requires an active runloop and the IO port needs to be -// allocated at sandbox initialization time, before there's a runloop. -// See crbug.com/83783 . - -// static -void SystemMonitor::AllocateSystemIOPorts() { - DCHECK_EQ(g_system_power_io_port, 0u); - - // Notification port allocated by IORegisterForSystemPower. - - g_system_power_io_port = IORegisterForSystemPower( - NULL, &g_notification_port_ref, SystemPowerEventCallback, - &g_notifier_object); - - DCHECK_NE(g_system_power_io_port, 0u); -} - -void SystemMonitor::PlatformInit() { - // Need to call AllocateSystemIOPorts() before constructing a SystemMonitor - // object. - DCHECK_NE(g_system_power_io_port, 0u); - if (g_system_power_io_port == 0) - return; - - // Add the notification port to the application runloop - CFRunLoopAddSource( - CFRunLoopGetCurrent(), - IONotificationPortGetRunLoopSource(g_notification_port_ref), - kCFRunLoopCommonModes); -} - -void SystemMonitor::PlatformDestroy() { - DCHECK_NE(g_system_power_io_port, 0u); - if (g_system_power_io_port == 0) - return; - - // Remove the sleep notification port from the application runloop - CFRunLoopRemoveSource( - CFRunLoopGetCurrent(), - IONotificationPortGetRunLoopSource(g_notification_port_ref), - kCFRunLoopCommonModes); - - // Deregister for system sleep notifications - IODeregisterForSystemPower(&g_notifier_object); - - // IORegisterForSystemPower implicitly opens the Root Power Domain IOService, - // so we close it here. - IOServiceClose(g_system_power_io_port); - - g_system_power_io_port = 0; - - // Destroy the notification port allocated by IORegisterForSystemPower. - IONotificationPortDestroy(g_notification_port_ref); -} - -} // namespace base diff --git a/base/system_monitor/system_monitor_posix.cc b/base/system_monitor/system_monitor_posix.cc deleted file mode 100644 index 6cf01bf..0000000 --- a/base/system_monitor/system_monitor_posix.cc +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2011 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/system_monitor/system_monitor.h" - -namespace base { - -bool SystemMonitor::IsBatteryPower() { - NOTIMPLEMENTED(); - return false; -} - -} // namespace base diff --git a/base/system_monitor/system_monitor_unittest.cc b/base/system_monitor/system_monitor_unittest.cc index 43f1dd6..b46e52f 100644 --- a/base/system_monitor/system_monitor_unittest.cc +++ b/base/system_monitor/system_monitor_unittest.cc @@ -14,45 +14,9 @@ namespace base { namespace { -class PowerTest : public SystemMonitor::PowerObserver { - public: - PowerTest() - : power_state_changes_(0), - suspends_(0), - resumes_(0) { - } - - // PowerObserver callbacks. - virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE { - power_state_changes_++; - } - - virtual void OnSuspend() OVERRIDE { - suspends_++; - } - - virtual void OnResume() OVERRIDE { - resumes_++; - } - - // Test status counts. - int power_state_changes() { return power_state_changes_; } - int suspends() { return suspends_; } - int resumes() { return resumes_; } - - private: - int power_state_changes_; // Count of OnPowerStateChange notifications. - int suspends_; // Count of OnSuspend notifications. - int resumes_; // Count of OnResume notifications. -}; - class SystemMonitorTest : public testing::Test { protected: SystemMonitorTest() { -#if defined(OS_MACOSX) - // This needs to happen before SystemMonitor's ctor. - SystemMonitor::AllocateSystemIOPorts(); -#endif system_monitor_.reset(new SystemMonitor); } virtual ~SystemMonitorTest() {} @@ -63,46 +27,6 @@ class SystemMonitorTest : public testing::Test { DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); }; -TEST_F(SystemMonitorTest, PowerNotifications) { - const int kObservers = 5; - - PowerTest test[kObservers]; - for (int index = 0; index < kObservers; ++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. - for (int index = 0; index < 5; index++) { - system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); - EXPECT_EQ(test[0].power_state_changes(), 0); - } - - // Sending resume when not suspended should have no effect. - system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - RunLoop().RunUntilIdle(); - EXPECT_EQ(test[0].resumes(), 0); - - // Pretend we suspended. - system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); - RunLoop().RunUntilIdle(); - EXPECT_EQ(test[0].suspends(), 1); - - // Send a second suspend notification. This should be suppressed. - system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); - RunLoop().RunUntilIdle(); - EXPECT_EQ(test[0].suspends(), 1); - - // Pretend we were awakened. - system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - RunLoop().RunUntilIdle(); - EXPECT_EQ(test[0].resumes(), 1); - - // Send a duplicate resume notification. This should be suppressed. - system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); - RunLoop().RunUntilIdle(); - EXPECT_EQ(test[0].resumes(), 1); -} - TEST_F(SystemMonitorTest, DeviceChangeNotifications) { const int kObservers = 5; diff --git a/base/system_monitor/system_monitor_win.cc b/base/system_monitor/system_monitor_win.cc deleted file mode 100644 index 562b69b..0000000 --- a/base/system_monitor/system_monitor_win.cc +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) 2011 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/system_monitor/system_monitor.h" - -#include "base/win/wrapped_window_proc.h" - -namespace base { - -namespace { - -const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow"; - -} // namespace - -// Function to query the system to see if it is currently running on -// battery power. Returns true if running on battery. -bool SystemMonitor::IsBatteryPower() { - SYSTEM_POWER_STATUS status; - if (!GetSystemPowerStatus(&status)) { - DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed"; - return false; - } - return (status.ACLineStatus == 0); -} - -SystemMonitor::PowerMessageWindow::PowerMessageWindow() - : instance_(NULL), message_hwnd_(NULL) { - WNDCLASSEX window_class; - base::win::InitializeWindowClass( - kWindowClassName, - &base::win::WrappedWindowProc< - SystemMonitor::PowerMessageWindow::WndProcThunk>, - 0, 0, 0, NULL, NULL, NULL, NULL, NULL, - &window_class); - instance_ = window_class.hInstance; - ATOM clazz = RegisterClassEx(&window_class); - DCHECK(clazz); - - message_hwnd_ = CreateWindowEx(WS_EX_NOACTIVATE, kWindowClassName, - NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, instance_, NULL); - SetWindowLongPtr(message_hwnd_, GWLP_USERDATA, - reinterpret_cast<LONG_PTR>(this)); -} - -SystemMonitor::PowerMessageWindow::~PowerMessageWindow() { - if (message_hwnd_) { - DestroyWindow(message_hwnd_); - UnregisterClass(kWindowClassName, instance_); - } -} - -void SystemMonitor::PowerMessageWindow::ProcessWmPowerBroadcastMessage( - int event_id) { - SystemMonitor::PowerEvent power_event; - switch (event_id) { - case PBT_APMPOWERSTATUSCHANGE: // The power status changed. - power_event = SystemMonitor::POWER_STATE_EVENT; - break; - case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. - //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. - // We don't notify for this latter event - // because if it occurs it is always sent as a - // second event after PBT_APMRESUMEAUTOMATIC. - power_event = SystemMonitor::RESUME_EVENT; - break; - case PBT_APMSUSPEND: // System has been suspended. - power_event = SystemMonitor::SUSPEND_EVENT; - break; - default: - return; - - // Other Power Events: - // PBT_APMBATTERYLOW - removed in Vista. - // PBT_APMOEMEVENT - removed in Vista. - // PBT_APMQUERYSUSPEND - removed in Vista. - // PBT_APMQUERYSUSPENDFAILED - removed in Vista. - // PBT_APMRESUMECRITICAL - removed in Vista. - // PBT_POWERSETTINGCHANGE - user changed the power settings. - } - - SystemMonitor::Get()->ProcessPowerMessage(power_event); -} - -LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProc( - HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { - switch (message) { - case WM_POWERBROADCAST: { - DWORD power_event = static_cast<DWORD>(message); - ProcessWmPowerBroadcastMessage(power_event); - return TRUE; - } - default: - break; - } - return ::DefWindowProc(hwnd, message, wparam, lparam); -} - -// static -LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProcThunk( - HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { - SystemMonitor::PowerMessageWindow* message_hwnd = - reinterpret_cast<SystemMonitor::PowerMessageWindow*>( - GetWindowLongPtr(hwnd, GWLP_USERDATA)); - if (message_hwnd) - return message_hwnd->WndProc(hwnd, message, wparam, lparam); - return ::DefWindowProc(hwnd, message, wparam, lparam); -} - -} // namespace base |