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