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