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