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.
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/win/wrapped_window_proc.h"
12 void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) {
13 PowerMonitorSource::ProcessPowerEvent(event);
18 const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow";
22 // Function to query the system to see if it is currently running on
23 // battery power. Returns true if running on battery.
24 bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() {
25 SYSTEM_POWER_STATUS status;
26 if (!GetSystemPowerStatus(&status)) {
27 DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed";
30 return (status.ACLineStatus == 0);
33 PowerMonitorDeviceSource::PowerMessageWindow::PowerMessageWindow()
34 : instance_(NULL), message_hwnd_(NULL) {
35 if (MessageLoop::current()->type() != MessageLoop::TYPE_UI) {
36 // Creating this window in (e.g.) a renderer inhibits shutdown on Windows.
37 // See http://crbug.com/230122. TODO(vandebo): http://crbug.com/236031
39 << "Cannot create windows on non-UI thread, power monitor disabled!";
42 WNDCLASSEX window_class;
43 base::win::InitializeWindowClass(
45 &base::win::WrappedWindowProc<
46 PowerMonitorDeviceSource::PowerMessageWindow::WndProcThunk>,
47 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
49 instance_ = window_class.hInstance;
50 ATOM clazz = RegisterClassEx(&window_class);
53 message_hwnd_ = CreateWindowEx(WS_EX_NOACTIVATE, kWindowClassName,
54 NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, instance_, NULL);
55 SetWindowLongPtr(message_hwnd_, GWLP_USERDATA,
56 reinterpret_cast<LONG_PTR>(this));
59 PowerMonitorDeviceSource::PowerMessageWindow::~PowerMessageWindow() {
61 DestroyWindow(message_hwnd_);
62 UnregisterClass(kWindowClassName, instance_);
67 PowerMonitorDeviceSource::PowerMessageWindow::ProcessWmPowerBroadcastMessage(
69 PowerMonitorSource::PowerEvent power_event;
71 case PBT_APMPOWERSTATUSCHANGE: // The power status changed.
72 power_event = PowerMonitorSource::POWER_STATE_EVENT;
74 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend.
75 //case PBT_APMRESUMESUSPEND: // User-initiated resume from suspend.
76 // We don't notify for this latter event
77 // because if it occurs it is always sent as a
78 // second event after PBT_APMRESUMEAUTOMATIC.
79 power_event = PowerMonitorSource::RESUME_EVENT;
81 case PBT_APMSUSPEND: // System has been suspended.
82 power_event = PowerMonitorSource::SUSPEND_EVENT;
87 // Other Power Events:
88 // PBT_APMBATTERYLOW - removed in Vista.
89 // PBT_APMOEMEVENT - removed in Vista.
90 // PBT_APMQUERYSUSPEND - removed in Vista.
91 // PBT_APMQUERYSUSPENDFAILED - removed in Vista.
92 // PBT_APMRESUMECRITICAL - removed in Vista.
93 // PBT_POWERSETTINGCHANGE - user changed the power settings.
96 ProcessPowerEventHelper(power_event);
99 LRESULT CALLBACK PowerMonitorDeviceSource::PowerMessageWindow::WndProc(
105 case WM_POWERBROADCAST: {
106 DWORD power_event = static_cast<DWORD>(message);
107 ProcessWmPowerBroadcastMessage(power_event);
113 return ::DefWindowProc(hwnd, message, wparam, lparam);
117 LRESULT CALLBACK PowerMonitorDeviceSource::PowerMessageWindow::WndProcThunk(
122 PowerMonitorDeviceSource::PowerMessageWindow* message_hwnd =
123 reinterpret_cast<PowerMonitorDeviceSource::PowerMessageWindow*>(
124 GetWindowLongPtr(hwnd, GWLP_USERDATA));
126 return message_hwnd->WndProc(hwnd, message, wparam, lparam);
127 return ::DefWindowProc(hwnd, message, wparam, lparam);