Chromium Code Reviews| Index: base/system_monitor/system_monitor_win.cc |
| diff --git a/base/system_monitor/system_monitor_win.cc b/base/system_monitor/system_monitor_win.cc |
| index 69ab85e84f6852c34095a143342a3a0417e230bc..52c73ade43a9f6596766b91b873cf0fe65e0a57e 100644 |
| --- a/base/system_monitor/system_monitor_win.cc |
| +++ b/base/system_monitor/system_monitor_win.cc |
| @@ -4,23 +4,67 @@ |
| #include "base/system_monitor/system_monitor.h" |
| +#include "base/win/wrapped_window_proc.h" |
| + |
| namespace base { |
| -void SystemMonitor::ProcessWmPowerBroadcastMessage(int event_id) { |
| +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_ = CreateWindow(kWindowClassName, |
| + 0, 0, 0, 0, 0, 0, 0, 0, instance_, 0); |
|
cpu_(ooo_6.6-7.5)
2013/02/21 20:44:38
the createwindow call is unnecessarily terse, use
Hongbo Min
2013/02/22 06:55:43
I am a bit confused by your concerns about the tit
cpu_(ooo_6.6-7.5)
2013/02/23 18:00:55
Because the right flags signal your intention. And
Hongbo Min
2013/02/24 07:27:22
cpu@, I did an experiment to verify the concerns y
vandebo (ex-Chrome)
2013/02/25 17:46:54
I think Carlos would like you to do the clearer th
|
| + SetWindowLongPtr(message_hwnd_, GWLP_USERDATA, |
| + reinterpret_cast<LONG_PTR>(this)); |
| +} |
| + |
| +SystemMonitor::PowerMessageWindow::~PowerMessageWindow() { |
| + if (message_hwnd_) |
| + DestroyWindow(message_hwnd_); |
| +} |
| + |
| +void SystemMonitor::PowerMessageWindow::ProcessWmPowerBroadcastMessage( |
| + int event_id) { |
| PowerEvent power_event; |
| switch (event_id) { |
| case PBT_APMPOWERSTATUSCHANGE: // The power status changed. |
| - power_event = POWER_STATE_EVENT; |
| + 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 = RESUME_EVENT; |
| + power_event = SystemMonitor::RESUME_EVENT; |
| break; |
| case PBT_APMSUSPEND: // System has been suspended. |
| - power_event = SUSPEND_EVENT; |
| + power_event = SystemMonitor::SUSPEND_EVENT; |
| break; |
| default: |
| return; |
| @@ -33,18 +77,32 @@ void SystemMonitor::ProcessWmPowerBroadcastMessage(int event_id) { |
| // PBT_APMRESUMECRITICAL - removed in Vista. |
| // PBT_POWERSETTINGCHANGE - user changed the power settings. |
| } |
| - ProcessPowerMessage(power_event); |
| + SystemMonitor::Get()->ProcessPowerMessage(power_event); |
| } |
| -// 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; |
| +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 (status.ACLineStatus == 0); |
| + 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)); |
|
cpu_(ooo_6.6-7.5)
2013/02/21 20:44:38
hmmm danger danger
You can (and will) receive mes
vandebo (ex-Chrome)
2013/02/21 20:54:45
Will message_hwnd be Null in that case? If so, li
Hongbo Min
2013/02/22 06:55:43
I agree with Steve.
cpu_(ooo_6.6-7.5)
2013/02/23 18:00:55
I agree too. I must have misread it.
|
| + if (message_hwnd) |
| + return message_hwnd->WndProc(hwnd, message, wparam, lparam); |
| + return ::DefWindowProc(hwnd, message, wparam, lparam); |
| } |
| } // namespace base |