summaryrefslogtreecommitdiffstats
path: root/hal/fallback/FallbackAlarm.cpp
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /hal/fallback/FallbackAlarm.cpp
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'hal/fallback/FallbackAlarm.cpp')
-rw-r--r--hal/fallback/FallbackAlarm.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/hal/fallback/FallbackAlarm.cpp b/hal/fallback/FallbackAlarm.cpp
new file mode 100644
index 000000000..9ec5c48e1
--- /dev/null
+++ b/hal/fallback/FallbackAlarm.cpp
@@ -0,0 +1,79 @@
+/* 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 "Hal.h"
+
+#include <algorithm>
+
+#include "mozilla/ClearOnShutdown.h"
+#include "mozilla/StaticPtr.h"
+#include "nsComponentManagerUtils.h"
+#include "nsITimer.h"
+#include "nsThreadUtils.h"
+
+namespace mozilla {
+namespace hal_impl {
+
+static void
+TimerCallbackFunc(nsITimer *aTimer, void *aClosure)
+{
+ hal::NotifyAlarmFired();
+}
+
+static StaticRefPtr<nsITimer> sTimer;
+
+bool
+EnableAlarm()
+{
+ static bool initialized = false;
+ if (!initialized) {
+ initialized = true;
+ ClearOnShutdown(&sTimer);
+ }
+
+ nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
+ sTimer = timer;
+ MOZ_ASSERT(sTimer);
+ return true;
+}
+
+void
+DisableAlarm()
+{
+ /*
+ * DisableAlarm() may be called after sTimer has been set to null by
+ * ClearOnShutdown().
+ */
+ if (sTimer) {
+ sTimer->Cancel();
+ }
+}
+
+bool
+SetAlarm(int32_t aSeconds, int32_t aNanoseconds)
+{
+ if (!sTimer) {
+ MOZ_ASSERT(false, "We should have enabled the alarm");
+ return false;
+ }
+
+ // Do the math to convert aSeconds and aNanoseconds into milliseconds since
+ // the epoch.
+ int64_t milliseconds = static_cast<int64_t>(aSeconds) * 1000 +
+ static_cast<int64_t>(aNanoseconds) / 1000000;
+
+ // nsITimer expects relative milliseconds.
+ int64_t relMilliseconds = milliseconds - PR_Now() / 1000;
+
+ // If the alarm time is in the past relative to PR_Now(),
+ // we choose to immediately fire the alarm. Passing 0 means nsITimer will
+ // queue a timeout event immediately.
+ sTimer->InitWithFuncCallback(TimerCallbackFunc, nullptr,
+ clamped<int64_t>(relMilliseconds, 0, INT32_MAX),
+ nsITimer::TYPE_ONE_SHOT);
+ return true;
+}
+
+} // namespace hal_impl
+} // namespace mozilla