summaryrefslogtreecommitdiffstats
path: root/dom/battery
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /dom/battery
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/battery')
-rw-r--r--dom/battery/BatteryManager.cpp183
-rw-r--r--dom/battery/BatteryManager.h77
-rw-r--r--dom/battery/Constants.h23
-rw-r--r--dom/battery/moz.build23
-rw-r--r--dom/battery/test/chrome.toml7
-rw-r--r--dom/battery/test/mochitest.toml3
-rw-r--r--dom/battery/test/test_battery_basics.html39
-rw-r--r--dom/battery/test/test_battery_charging.html35
-rw-r--r--dom/battery/test/test_battery_discharging.html35
-rw-r--r--dom/battery/test/test_battery_unprivileged.html25
10 files changed, 450 insertions, 0 deletions
diff --git a/dom/battery/BatteryManager.cpp b/dom/battery/BatteryManager.cpp
new file mode 100644
index 0000000000..0a56f12af1
--- /dev/null
+++ b/dom/battery/BatteryManager.cpp
@@ -0,0 +1,183 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include <cmath>
+#include <limits>
+#include "BatteryManager.h"
+#include "Constants.h"
+#include "mozilla/DOMEventTargetHelper.h"
+#include "mozilla/Hal.h"
+#include "mozilla/dom/BatteryManagerBinding.h"
+#include "mozilla/Preferences.h"
+#include "nsContentUtils.h"
+#include "mozilla/dom/Document.h"
+
+/**
+ * We have to use macros here because our leak analysis tool things we are
+ * leaking strings when we have |static const nsString|. Sad :(
+ */
+#define LEVELCHANGE_EVENT_NAME u"levelchange"_ns
+#define CHARGINGCHANGE_EVENT_NAME u"chargingchange"_ns
+#define DISCHARGINGTIMECHANGE_EVENT_NAME u"dischargingtimechange"_ns
+#define CHARGINGTIMECHANGE_EVENT_NAME u"chargingtimechange"_ns
+
+namespace mozilla::dom::battery {
+
+BatteryManager::BatteryManager(nsPIDOMWindowInner* aWindow)
+ : DOMEventTargetHelper(aWindow),
+ mLevel(kDefaultLevel),
+ mCharging(kDefaultCharging),
+ mRemainingTime(kDefaultRemainingTime) {}
+
+void BatteryManager::Init() {
+ hal::RegisterBatteryObserver(this);
+
+ hal::BatteryInformation batteryInfo;
+ hal::GetCurrentBatteryInformation(&batteryInfo);
+
+ UpdateFromBatteryInfo(batteryInfo);
+}
+
+void BatteryManager::Shutdown() { hal::UnregisterBatteryObserver(this); }
+
+JSObject* BatteryManager::WrapObject(JSContext* aCx,
+ JS::Handle<JSObject*> aGivenProto) {
+ return BatteryManager_Binding::Wrap(aCx, this, aGivenProto);
+}
+
+bool BatteryManager::Charging() const {
+ MOZ_ASSERT(NS_IsMainThread());
+ // For testing, unable to report the battery status information
+ if (Preferences::GetBool("dom.battery.test.default", false)) {
+ return true;
+ }
+ if (Preferences::GetBool("dom.battery.test.charging", false)) {
+ return true;
+ }
+ if (Preferences::GetBool("dom.battery.test.discharging", false)) {
+ return false;
+ }
+
+ return mCharging;
+}
+
+double BatteryManager::DischargingTime() const {
+ MOZ_ASSERT(NS_IsMainThread());
+ // For testing, unable to report the battery status information
+ if (Preferences::GetBool("dom.battery.test.default", false)) {
+ return std::numeric_limits<double>::infinity();
+ }
+ if (Preferences::GetBool("dom.battery.test.discharging", false)) {
+ return 42.0;
+ }
+
+ if (Charging() || mRemainingTime == kUnknownRemainingTime) {
+ return std::numeric_limits<double>::infinity();
+ }
+
+ return mRemainingTime;
+}
+
+double BatteryManager::ChargingTime() const {
+ MOZ_ASSERT(NS_IsMainThread());
+ // For testing, unable to report the battery status information
+ if (Preferences::GetBool("dom.battery.test.default", false)) {
+ return 0.0;
+ }
+ if (Preferences::GetBool("dom.battery.test.charging", false)) {
+ return 42.0;
+ }
+
+ if (!Charging() || mRemainingTime == kUnknownRemainingTime) {
+ return std::numeric_limits<double>::infinity();
+ }
+
+ return mRemainingTime;
+}
+
+double BatteryManager::Level() const {
+ MOZ_ASSERT(NS_IsMainThread());
+ // For testing, unable to report the battery status information
+ if (Preferences::GetBool("dom.battery.test.default")) {
+ return 1.0;
+ }
+
+ return mLevel;
+}
+
+void BatteryManager::UpdateFromBatteryInfo(
+ const hal::BatteryInformation& aBatteryInfo) {
+ mLevel = aBatteryInfo.level();
+
+ // Round to the nearest ten percent for non-chrome.
+ Document* doc = GetOwner() ? GetOwner()->GetDoc() : nullptr;
+
+ mCharging = aBatteryInfo.charging();
+ mRemainingTime = aBatteryInfo.remainingTime();
+
+ if (!nsContentUtils::IsChromeDoc(doc)) {
+ mLevel = lround(mLevel * 10.0) / 10.0;
+ if (mLevel == 1.0) {
+ mRemainingTime =
+ mCharging ? kDefaultRemainingTime : kUnknownRemainingTime;
+ } else if (mRemainingTime != kUnknownRemainingTime) {
+ // Round the remaining time to a multiple of 15 minutes and never zero
+ const double MINUTES_15 = 15.0 * 60.0;
+ mRemainingTime =
+ fmax(lround(mRemainingTime / MINUTES_15) * MINUTES_15, MINUTES_15);
+ }
+ }
+
+ // Add some guards to make sure the values are coherent.
+ if (mLevel == 1.0 && mCharging == true &&
+ mRemainingTime != kDefaultRemainingTime) {
+ mRemainingTime = kDefaultRemainingTime;
+ NS_ERROR(
+ "Battery API: When charging and level at 1.0, remaining time "
+ "should be 0. Please fix your backend!");
+ }
+}
+
+void BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo) {
+ double previousLevel = mLevel;
+ bool previousCharging = mCharging;
+ double previousRemainingTime = mRemainingTime;
+
+ UpdateFromBatteryInfo(aBatteryInfo);
+
+ if (previousCharging != mCharging) {
+ DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME);
+ }
+
+ if (previousLevel != mLevel) {
+ DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME);
+ }
+
+ /*
+ * There are a few situations that could happen here:
+ * 1. Charging state changed:
+ * a. Previous remaining time wasn't unkwonw, we have to fire an event for
+ * the change.
+ * b. New remaining time isn't unkwonw, we have to fire an event for it.
+ * 2. Charging state didn't change but remainingTime did, we have to fire
+ * the event that correspond to the current charging state.
+ */
+ if (mCharging != previousCharging) {
+ if (previousRemainingTime != kUnknownRemainingTime) {
+ DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME
+ : DISCHARGINGTIMECHANGE_EVENT_NAME);
+ }
+ if (mRemainingTime != kUnknownRemainingTime) {
+ DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
+ : DISCHARGINGTIMECHANGE_EVENT_NAME);
+ }
+ } else if (previousRemainingTime != mRemainingTime) {
+ DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME
+ : DISCHARGINGTIMECHANGE_EVENT_NAME);
+ }
+}
+
+} // namespace mozilla::dom::battery
diff --git a/dom/battery/BatteryManager.h b/dom/battery/BatteryManager.h
new file mode 100644
index 0000000000..c92df7e662
--- /dev/null
+++ b/dom/battery/BatteryManager.h
@@ -0,0 +1,77 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_dom_battery_BatteryManager_h
+#define mozilla_dom_battery_BatteryManager_h
+
+#include "mozilla/DOMEventTargetHelper.h"
+#include "mozilla/HalBatteryInformation.h"
+
+namespace mozilla {
+
+namespace hal {
+class BatteryInformation;
+} // namespace hal
+
+namespace dom::battery {
+
+class BatteryManager : public DOMEventTargetHelper,
+ public hal::BatteryObserver {
+ public:
+ explicit BatteryManager(nsPIDOMWindowInner* aWindow);
+
+ void Init();
+ void Shutdown();
+
+ // For IObserver.
+ void Notify(const hal::BatteryInformation& aBatteryInfo) override;
+
+ /**
+ * WebIDL Interface
+ */
+
+ nsPIDOMWindowInner* GetParentObject() const { return GetOwner(); }
+
+ virtual JSObject* WrapObject(JSContext* aCx,
+ JS::Handle<JSObject*> aGivenProto) override;
+
+ bool Charging() const;
+
+ double ChargingTime() const;
+
+ double DischargingTime() const;
+
+ double Level() const;
+
+ IMPL_EVENT_HANDLER(chargingchange)
+ IMPL_EVENT_HANDLER(chargingtimechange)
+ IMPL_EVENT_HANDLER(dischargingtimechange)
+ IMPL_EVENT_HANDLER(levelchange)
+
+ private:
+ /**
+ * Update the battery information stored in the battery manager object using
+ * a battery information object.
+ */
+ void UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo);
+
+ /**
+ * Represents the battery level, ranging from 0.0 (dead or removed?)
+ * to 1.0 (fully charged)
+ */
+ double mLevel;
+ bool mCharging;
+ /**
+ * Represents the discharging time or the charging time, depending on the
+ * current battery status (charging or not).
+ */
+ double mRemainingTime;
+};
+
+} // namespace dom::battery
+} // namespace mozilla
+
+#endif // mozilla_dom_battery_BatteryManager_h
diff --git a/dom/battery/Constants.h b/dom/battery/Constants.h
new file mode 100644
index 0000000000..3e0b852647
--- /dev/null
+++ b/dom/battery/Constants.h
@@ -0,0 +1,23 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_dom_battery_Constants_h__
+#define mozilla_dom_battery_Constants_h__
+
+/**
+ * A set of constants that might need to be used by battery backends.
+ * It's not part of BatteryManager.h to prevent those backends to include it.
+ */
+namespace mozilla::dom::battery {
+
+static const double kDefaultLevel = 1.0;
+static const bool kDefaultCharging = true;
+static const double kDefaultRemainingTime = 0;
+static const double kUnknownRemainingTime = -1;
+
+} // namespace mozilla::dom::battery
+
+#endif // mozilla_dom_battery_Constants_h__
diff --git a/dom/battery/moz.build b/dom/battery/moz.build
new file mode 100644
index 0000000000..3a90c93c01
--- /dev/null
+++ b/dom/battery/moz.build
@@ -0,0 +1,23 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+with Files("**"):
+ BUG_COMPONENT = ("Core", "DOM: Device Interfaces")
+
+EXPORTS.mozilla.dom.battery += [
+ "Constants.h",
+]
+
+SOURCES += [
+ "BatteryManager.cpp",
+]
+
+include("/ipc/chromium/chromium-config.mozbuild")
+
+FINAL_LIBRARY = "xul"
+
+MOCHITEST_CHROME_MANIFESTS += ["test/chrome.toml"]
+MOCHITEST_MANIFESTS += ["test/mochitest.toml"]
diff --git a/dom/battery/test/chrome.toml b/dom/battery/test/chrome.toml
new file mode 100644
index 0000000000..2fd3810b26
--- /dev/null
+++ b/dom/battery/test/chrome.toml
@@ -0,0 +1,7 @@
+[DEFAULT]
+
+["test_battery_basics.html"]
+
+["test_battery_charging.html"]
+
+["test_battery_discharging.html"]
diff --git a/dom/battery/test/mochitest.toml b/dom/battery/test/mochitest.toml
new file mode 100644
index 0000000000..86f6921b30
--- /dev/null
+++ b/dom/battery/test/mochitest.toml
@@ -0,0 +1,3 @@
+[DEFAULT]
+
+["test_battery_unprivileged.html"]
diff --git a/dom/battery/test/test_battery_basics.html b/dom/battery/test/test_battery_basics.html
new file mode 100644
index 0000000000..3cc16ad1e8
--- /dev/null
+++ b/dom/battery/test/test_battery_basics.html
@@ -0,0 +1,39 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for Battery API</title>
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+/** Test for Battery API **/
+ok("getBattery" in navigator, "navigator.getBattery should exist");
+ok(!("battery" in navigator), "navigator.battery should not exist");
+
+navigator.getBattery().then(function(battery) {
+ ok(battery.level >= 0.0 && battery.level <= 1.0, "Battery level " + battery.level + " should be in the range [0.0, 1.0]");
+
+ SpecialPowers.pushPrefEnv({"set": [["dom.battery.test.default", true]]}, function() {
+ ok(battery.charging, "Battery should be charging by default");
+ is(battery.chargingTime, 0, "Battery chargingTime " + battery.chargingTime + " should be zero by default");
+ is(battery.dischargingTime, Infinity, "Battery dischargingTime should be Infinity by default");
+ is(battery.level, 1.0, "Battery level " + battery.level + " should be 1.0 by default");
+
+ SimpleTest.finish();
+ });
+});
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/battery/test/test_battery_charging.html b/dom/battery/test/test_battery_charging.html
new file mode 100644
index 0000000000..b284253aef
--- /dev/null
+++ b/dom/battery/test/test_battery_charging.html
@@ -0,0 +1,35 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for Battery API</title>
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+/** Test for Battery API **/
+navigator.getBattery().then(function(battery) {
+ ok(battery.level >= 0.0 && battery.level <= 1.0, "Battery level " + battery.level + " should be in the range [0.0, 1.0]");
+
+ SpecialPowers.pushPrefEnv({"set": [["dom.battery.test.charging", true]]}, function() {
+ is(battery.charging, true, "Battery should be charging");
+ ok(battery.chargingTime >= 0, "Battery chargingTime " + battery.chargingTime + " should be nonnegative when charging");
+ is(battery.dischargingTime, Infinity, "Battery dischargingTime should be Infinity when charging");
+
+ SimpleTest.finish();
+ });
+});
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/battery/test/test_battery_discharging.html b/dom/battery/test/test_battery_discharging.html
new file mode 100644
index 0000000000..916e3725e7
--- /dev/null
+++ b/dom/battery/test/test_battery_discharging.html
@@ -0,0 +1,35 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for Battery API</title>
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+/** Test for Battery API **/
+navigator.getBattery().then(function(battery) {
+ ok(battery.level >= 0.0 && battery.level <= 1.0, "Battery level " + battery.level + " should be in the range [0.0, 1.0]");
+
+ SpecialPowers.pushPrefEnv({"set": [["dom.battery.test.discharging", true]]}, function() {
+ is(battery.charging, false, "Battery should be discharging");
+ is(battery.chargingTime, Infinity, "Battery chargingTime should be Infinity when discharging");
+ ok(battery.dischargingTime > 0, "Battery dischargingTime " + battery.dischargingTime + " should be positive when discharging");
+
+ SimpleTest.finish();
+ });
+});
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/battery/test/test_battery_unprivileged.html b/dom/battery/test/test_battery_unprivileged.html
new file mode 100644
index 0000000000..2129aaf6a4
--- /dev/null
+++ b/dom/battery/test/test_battery_unprivileged.html
@@ -0,0 +1,25 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for Battery API</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+"use strict";
+
+/** Test for Battery API **/
+ok(!("getBattery" in navigator), "navigator.getBattery should not exist for unprivileged web content");
+ok(!("battery" in navigator), "navigator.battery should not exist");
+ok(!("BatteryManager" in window), "BatteryManager should not exist");
+
+</script>
+</pre>
+</body>
+</html>