This source file includes following definitions.
- ProcessPowerEventHelper
- ProcessWmPowerBroadcastMessage
- IsOnBatteryPowerImpl
- message_hwnd_
- WndProcThunk
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h"
#include "base/power_monitor/power_monitor_source.h"
#include "base/win/wrapped_window_proc.h"
namespace base {
void ProcessPowerEventHelper(PowerMonitorSource::PowerEvent event) {
PowerMonitorSource::ProcessPowerEvent(event);
}
namespace {
const wchar_t kWindowClassName[] = L"Base_PowerMessageWindow";
void ProcessWmPowerBroadcastMessage(WPARAM event_id) {
PowerMonitorSource::PowerEvent power_event;
switch (event_id) {
case PBT_APMPOWERSTATUSCHANGE:
power_event = PowerMonitorSource::POWER_STATE_EVENT;
break;
case PBT_APMRESUMEAUTOMATIC:
power_event = PowerMonitorSource::RESUME_EVENT;
break;
case PBT_APMSUSPEND:
power_event = PowerMonitorSource::SUSPEND_EVENT;
break;
default:
return;
}
ProcessPowerEventHelper(power_event);
}
}
bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() {
SYSTEM_POWER_STATUS status;
if (!GetSystemPowerStatus(&status)) {
DLOG_GETLASTERROR(ERROR) << "GetSystemPowerStatus failed";
return false;
}
return (status.ACLineStatus == 0);
}
PowerMonitorDeviceSource::PowerMessageWindow::PowerMessageWindow()
: instance_(NULL), message_hwnd_(NULL) {
if (!MessageLoopForUI::IsCurrent()) {
DLOG(ERROR)
<< "Cannot create windows on non-UI thread, power monitor disabled!";
return;
}
WNDCLASSEX window_class;
base::win::InitializeWindowClass(
kWindowClassName,
&base::win::WrappedWindowProc<
PowerMonitorDeviceSource::PowerMessageWindow::WndProcThunk>,
0, 0, 0, NULL, NULL, NULL, NULL, NULL,
&window_class);
instance_ = window_class.hInstance;
ATOM clazz = RegisterClassEx(&window_class);
DCHECK(clazz);
message_hwnd_ = CreateWindowEx(WS_EX_NOACTIVATE, kWindowClassName,
NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, instance_, NULL);
}
PowerMonitorDeviceSource::PowerMessageWindow::~PowerMessageWindow() {
if (message_hwnd_) {
DestroyWindow(message_hwnd_);
UnregisterClass(kWindowClassName, instance_);
}
}
LRESULT CALLBACK PowerMonitorDeviceSource::PowerMessageWindow::WndProcThunk(
HWND hwnd,
UINT message,
WPARAM wparam,
LPARAM lparam) {
switch (message) {
case WM_POWERBROADCAST:
ProcessWmPowerBroadcastMessage(wparam);
return TRUE;
default:
return ::DefWindowProc(hwnd, message, wparam, lparam);
}
}
}