Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/system_monitor/system_monitor.h" | 5 #include "base/power_monitor/power_monitor.h" |
| 6 | 6 |
| 7 #include "base/win/wrapped_window_proc.h" | 7 #include "base/win/wrapped_window_proc.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow"; | 13 const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow"; |
| 14 | 14 |
| 15 } // namespace | 15 } // namespace |
| 16 | 16 |
| 17 // Function to query the system to see if it is currently running on | 17 // Function to query the system to see if it is currently running on |
| 18 // battery power. Returns true if running on battery. | 18 // battery power. Returns true if running on battery. |
| 19 bool SystemMonitor::IsBatteryPower() { | 19 bool PowerMonitor::IsBatteryPower() { |
| 20 SYSTEM_POWER_STATUS status; | 20 SYSTEM_POWER_STATUS status; |
| 21 if (!GetSystemPowerStatus(&status)) { | 21 if (!GetSystemPowerStatus(&status)) { |
| 22 DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed"; | 22 DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed"; |
| 23 return false; | 23 return false; |
| 24 } | 24 } |
| 25 return (status.ACLineStatus == 0); | 25 return (status.ACLineStatus == 0); |
| 26 } | 26 } |
| 27 | 27 |
| 28 SystemMonitor::PowerMessageWindow::PowerMessageWindow() | 28 PowerMonitor::PowerMessageWindow::PowerMessageWindow() |
| 29 : instance_(NULL), message_hwnd_(NULL) { | 29 : instance_(NULL), message_hwnd_(NULL) { |
| 30 WNDCLASSEX window_class; | 30 WNDCLASSEX window_class; |
| 31 base::win::InitializeWindowClass( | 31 base::win::InitializeWindowClass( |
| 32 kWindowClassName, | 32 kWindowClassName, |
| 33 &base::win::WrappedWindowProc< | 33 &base::win::WrappedWindowProc< |
| 34 SystemMonitor::PowerMessageWindow::WndProcThunk>, | 34 PowerMonitor::PowerMessageWindow::WndProcThunk>, |
| 35 0, 0, 0, NULL, NULL, NULL, NULL, NULL, | 35 0, 0, 0, NULL, NULL, NULL, NULL, NULL, |
| 36 &window_class); | 36 &window_class); |
| 37 instance_ = window_class.hInstance; | 37 instance_ = window_class.hInstance; |
| 38 ATOM clazz = RegisterClassEx(&window_class); | 38 ATOM clazz = RegisterClassEx(&window_class); |
| 39 DCHECK(clazz); | 39 DCHECK(clazz); |
| 40 | 40 |
| 41 message_hwnd_ = CreateWindowEx(WS_EX_NOACTIVATE, kWindowClassName, | 41 message_hwnd_ = CreateWindowEx(WS_EX_NOACTIVATE, kWindowClassName, |
| 42 NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, instance_, NULL); | 42 NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, instance_, NULL); |
| 43 SetWindowLongPtr(message_hwnd_, GWLP_USERDATA, | 43 SetWindowLongPtr(message_hwnd_, GWLP_USERDATA, |
| 44 reinterpret_cast<LONG_PTR>(this)); | 44 reinterpret_cast<LONG_PTR>(this)); |
| 45 } | 45 } |
| 46 | 46 |
| 47 SystemMonitor::PowerMessageWindow::~PowerMessageWindow() { | 47 PowerMonitor::PowerMessageWindow::~PowerMessageWindow() { |
| 48 if (message_hwnd_) { | 48 if (message_hwnd_) { |
| 49 DestroyWindow(message_hwnd_); | 49 DestroyWindow(message_hwnd_); |
| 50 UnregisterClass(kWindowClassName, instance_); | 50 UnregisterClass(kWindowClassName, instance_); |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 void SystemMonitor::PowerMessageWindow::ProcessWmPowerBroadcastMessage( | 54 void PowerMonitor::PowerMessageWindow::ProcessWmPowerBroadcastMessage( |
| 55 int event_id) { | 55 int event_id) { |
| 56 SystemMonitor::PowerEvent power_event; | 56 PowerMonitor::PowerEvent power_event; |
| 57 switch (event_id) { | 57 switch (event_id) { |
| 58 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. | 58 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. |
| 59 power_event = SystemMonitor::POWER_STATE_EVENT; | 59 power_event = PowerMonitor::POWER_STATE_EVENT; |
| 60 break; | 60 break; |
| 61 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. | 61 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. |
| 62 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. | 62 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. |
| 63 // We don't notify for this latter event | 63 // We don't notify for this latter event |
| 64 // because if it occurs it is always sent as a | 64 // because if it occurs it is always sent as a |
| 65 // second event after PBT_APMRESUMEAUTOMATIC. | 65 // second event after PBT_APMRESUMEAUTOMATIC. |
| 66 power_event = SystemMonitor::RESUME_EVENT; | 66 power_event = PowerMonitor::RESUME_EVENT; |
| 67 break; | 67 break; |
| 68 case PBT_APMSUSPEND: // System has been suspended. | 68 case PBT_APMSUSPEND: // System has been suspended. |
| 69 power_event = SystemMonitor::SUSPEND_EVENT; | 69 power_event = PowerMonitor::SUSPEND_EVENT; |
| 70 break; | 70 break; |
| 71 default: | 71 default: |
| 72 return; | 72 return; |
| 73 | 73 |
| 74 // Other Power Events: | 74 // Other Power Events: |
| 75 // PBT_APMBATTERYLOW - removed in Vista. | 75 // PBT_APMBATTERYLOW - removed in Vista. |
| 76 // PBT_APMOEMEVENT - removed in Vista. | 76 // PBT_APMOEMEVENT - removed in Vista. |
| 77 // PBT_APMQUERYSUSPEND - removed in Vista. | 77 // PBT_APMQUERYSUSPEND - removed in Vista. |
| 78 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. | 78 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. |
| 79 // PBT_APMRESUMECRITICAL - removed in Vista. | 79 // PBT_APMRESUMECRITICAL - removed in Vista. |
| 80 // PBT_POWERSETTINGCHANGE - user changed the power settings. | 80 // PBT_POWERSETTINGCHANGE - user changed the power settings. |
| 81 } | 81 } |
| 82 | 82 |
| 83 SystemMonitor::Get()->ProcessPowerMessage(power_event); | 83 PowerMonitor::Get()->ProcessPowerEvent(power_event); |
| 84 } | 84 } |
| 85 | 85 |
| 86 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProc( | 86 LRESULT CALLBACK PowerMonitor::PowerMessageWindow::WndProc( |
| 87 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | 87 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
| 88 switch (message) { | 88 switch (message) { |
| 89 case WM_POWERBROADCAST: { | 89 case WM_POWERBROADCAST: { |
| 90 DWORD power_event = static_cast<DWORD>(message); | 90 DWORD power_event = static_cast<DWORD>(message); |
| 91 ProcessWmPowerBroadcastMessage(power_event); | 91 ProcessWmPowerBroadcastMessage(power_event); |
| 92 return TRUE; | 92 return TRUE; |
| 93 } | 93 } |
| 94 default: | 94 default: |
| 95 break; | 95 break; |
| 96 } | 96 } |
| 97 return ::DefWindowProc(hwnd, message, wparam, lparam); | 97 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // static | 100 // static |
| 101 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProcThunk( | 101 LRESULT CALLBACK PowerMonitor::PowerMessageWindow::WndProcThunk( |
| 102 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | 102 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
| 103 SystemMonitor::PowerMessageWindow* message_hwnd = | 103 PowerMonitor::PowerMessageWindow* message_hwnd = |
| 104 reinterpret_cast<SystemMonitor::PowerMessageWindow*>( | 104 reinterpret_cast<PowerMonitor::PowerMessageWindow*>( |
|
vandebo (ex-Chrome)
2013/03/19 23:36:04
This was the correct indenting, please undo.
| |
| 105 GetWindowLongPtr(hwnd, GWLP_USERDATA)); | 105 GetWindowLongPtr(hwnd, GWLP_USERDATA)); |
| 106 if (message_hwnd) | 106 if (message_hwnd) |
| 107 return message_hwnd->WndProc(hwnd, message, wparam, lparam); | 107 return message_hwnd->WndProc(hwnd, message, wparam, lparam); |
| 108 return ::DefWindowProc(hwnd, message, wparam, lparam); | 108 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 109 } | 109 } |
| 110 | 110 |
| 111 } // namespace base | 111 } // namespace base |
| OLD | NEW |