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