Bug 1866777 - Disable test_race_cache_with_network.js on windows opt for frequent...
[gecko.git] / hal / windows / WindowsBattery.cpp
blobfba786c8a2bc3ba67b609e85b4cf4b3f028531be
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "Hal.h"
7 #include "HalImpl.h"
8 #include "mozilla/Preferences.h"
9 #include "mozilla/dom/battery/Constants.h"
11 #include <windows.h>
13 using namespace mozilla::dom::battery;
15 namespace mozilla {
16 namespace hal_impl {
18 static HPOWERNOTIFY sPowerHandle = nullptr;
19 static HPOWERNOTIFY sCapacityHandle = nullptr;
20 static HWND sHWnd = nullptr;
22 static LRESULT CALLBACK BatteryWindowProc(HWND hwnd, UINT msg, WPARAM wParam,
23 LPARAM lParam) {
24 if (msg != WM_POWERBROADCAST || wParam != PBT_POWERSETTINGCHANGE) {
25 return DefWindowProc(hwnd, msg, wParam, lParam);
28 hal::BatteryInformation currentInfo;
30 // Since we need update remainingTime, we cannot use LPARAM.
31 hal_impl::GetCurrentBatteryInformation(&currentInfo);
33 hal::NotifyBatteryChange(currentInfo);
34 return TRUE;
37 void EnableBatteryNotifications() {
38 // Create custom window to watch battery event
39 // If we can get Gecko's window handle, this is unnecessary.
41 if (sHWnd == nullptr) {
42 WNDCLASSW wc;
43 HMODULE hSelf = GetModuleHandle(nullptr);
45 if (!GetClassInfoW(hSelf, L"MozillaBatteryClass", &wc)) {
46 ZeroMemory(&wc, sizeof(WNDCLASSW));
47 wc.hInstance = hSelf;
48 wc.lpfnWndProc = BatteryWindowProc;
49 wc.lpszClassName = L"MozillaBatteryClass";
50 RegisterClassW(&wc);
53 sHWnd = CreateWindowW(L"MozillaBatteryClass", L"Battery Watcher", 0, 0, 0,
54 0, 0, nullptr, nullptr, hSelf, nullptr);
57 if (sHWnd == nullptr) {
58 return;
61 sPowerHandle = RegisterPowerSettingNotification(
62 sHWnd, &GUID_ACDC_POWER_SOURCE, DEVICE_NOTIFY_WINDOW_HANDLE);
63 sCapacityHandle = RegisterPowerSettingNotification(
64 sHWnd, &GUID_BATTERY_PERCENTAGE_REMAINING, DEVICE_NOTIFY_WINDOW_HANDLE);
67 void DisableBatteryNotifications() {
68 if (sPowerHandle) {
69 UnregisterPowerSettingNotification(sPowerHandle);
70 sPowerHandle = nullptr;
73 if (sCapacityHandle) {
74 UnregisterPowerSettingNotification(sCapacityHandle);
75 sCapacityHandle = nullptr;
78 if (sHWnd) {
79 DestroyWindow(sHWnd);
80 sHWnd = nullptr;
84 void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo) {
85 SYSTEM_POWER_STATUS status;
86 if (!GetSystemPowerStatus(&status)) {
87 aBatteryInfo->level() = kDefaultLevel;
88 aBatteryInfo->charging() = kDefaultCharging;
89 aBatteryInfo->remainingTime() = kDefaultRemainingTime;
90 return;
93 aBatteryInfo->level() = status.BatteryLifePercent == 255
94 ? kDefaultLevel
95 : ((double)status.BatteryLifePercent) / 100.0;
96 aBatteryInfo->charging() = (status.ACLineStatus != 0);
97 if (status.ACLineStatus != 0) {
98 if (aBatteryInfo->level() == 1.0) {
99 // GetSystemPowerStatus API may returns -1 for BatteryFullLifeTime.
100 // So, if battery is 100%, set kDefaultRemainingTime at force.
101 aBatteryInfo->remainingTime() = kDefaultRemainingTime;
102 } else {
103 aBatteryInfo->remainingTime() = status.BatteryFullLifeTime == (DWORD)-1
104 ? kUnknownRemainingTime
105 : status.BatteryFullLifeTime;
107 } else {
108 aBatteryInfo->remainingTime() = status.BatteryLifeTime == (DWORD)-1
109 ? kUnknownRemainingTime
110 : status.BatteryLifeTime;
114 } // namespace hal_impl
115 } // namespace mozilla