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