| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/system_monitor/system_monitor.h" | |
| 6 | |
| 7 #include "base/win/wrapped_window_proc.h" | |
| 8 | |
| 9 namespace base { | |
| 10 | |
| 11 namespace { | |
| 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; | |
| 57 switch (event_id) { | |
| 58 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. | |
| 59 power_event = SystemMonitor::POWER_STATE_EVENT; | |
| 60 break; | |
| 61 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. | |
| 62 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend. | |
| 63 // We don't notify for this latter event | |
| 64 // because if it occurs it is always sent as a | |
| 65 // second event after PBT_APMRESUMEAUTOMATIC. | |
| 66 power_event = SystemMonitor::RESUME_EVENT; | |
| 67 break; | |
| 68 case PBT_APMSUSPEND: // System has been suspended. | |
| 69 power_event = SystemMonitor::SUSPEND_EVENT; | |
| 70 break; | |
| 71 default: | |
| 72 return; | |
| 73 | |
| 74 // Other Power Events: | |
| 75 // PBT_APMBATTERYLOW - removed in Vista. | |
| 76 // PBT_APMOEMEVENT - removed in Vista. | |
| 77 // PBT_APMQUERYSUSPEND - removed in Vista. | |
| 78 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. | |
| 79 // PBT_APMRESUMECRITICAL - removed in Vista. | |
| 80 // PBT_POWERSETTINGCHANGE - user changed the power settings. | |
| 81 } | |
| 82 | |
| 83 SystemMonitor::Get()->ProcessPowerMessage(power_event); | |
| 84 } | |
| 85 | |
| 86 LRESULT CALLBACK SystemMonitor::PowerMessageWindow::WndProc( | |
| 87 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | |
| 88 switch (message) { | |
| 89 case WM_POWERBROADCAST: { | |
| 90 DWORD power_event = static_cast<DWORD>(message); | |
| 91 ProcessWmPowerBroadcastMessage(power_event); | |
| 92 return TRUE; | |
| 93 } | |
| 94 default: | |
| 95 break; | |
| 96 } | |
| 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); | |
| 109 } | |
| 110 | |
| 111 } // namespace base | |
| OLD | NEW |