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