summaryrefslogtreecommitdiff
path: root/data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/panic.js
diff options
context:
space:
mode:
authorRuben Rodriguez <ruben@gnu.org>2015-11-28 15:24:36 -0600
committerRuben Rodriguez <ruben@gnu.org>2015-11-28 16:27:06 -0600
commite4a3586a14996bbece3b26c9e3b7704ea6af8615 (patch)
tree499bdd16b3a90c30b01e4b47a5882d13b4800f50 /data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/panic.js
parent4dbc2fae927bb02ef243c87938e638af9afee8fa (diff)
LibreJS upgraded to 6.0.10
Diffstat (limited to 'data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/panic.js')
-rw-r--r--data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/panic.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/panic.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/panic.js
new file mode 100644
index 0000000..a478077
--- /dev/null
+++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/node_modules/pathfinder/lib/panic.js
@@ -0,0 +1,92 @@
+/* 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/. */
+'use strict';
+
+const { setTimeout } = require('sdk/timers');
+const { get, set } = require('sdk/preferences/service');
+const { add, notify } = require('sdk/deprecated/observer-service');
+const { on, once, emit, off } = require('sdk/event/core');
+const { loadReason } = require('sdk/self');
+
+const { unload } = require('./addon/unload');
+
+const PREF_NAME = 'security.addon.panic';
+function inPanic() get(PREF_NAME, false);
+function setPanic(value) set(PREF_NAME, value);
+
+const PREF_END_NAME = 'security.addon.panic_end';
+function getEndTime() get(PREF_END_NAME, 0) * 1;
+function setEndTime(timestamp) set(PREF_END_NAME, timestamp + "");
+
+Object.defineProperty(exports, "inPanic", {
+ get: function() inPanic()
+});
+
+const panic = function (ms) {
+ ms = ms || 0;
+ let endTime = Date.now() + ms;
+
+ // check that the current end timestamp is not greater
+ if (getEndTime() >= endTime)
+ return;
+
+ // set the end timestamp (to handle the reboot situation)
+ setEndTime(endTime);
+
+ // notify system of a panic
+ notify('panic-start');
+
+ // end the panic
+ setTimeout(function() {
+ // check that the end timestamp was not extended by another panic
+ // NOTE: another instance of panic module could have caused the old panic
+ if (getEndTime() != endTime) return;
+
+ notify('panic-end');
+ }, ms);
+};
+exports.panic = panic;
+
+// internal object used to emit on, instead of `exports`, so that outside code
+// cannot emit on this object.
+const panicEmitter = {};
+
+// create and expose event listener related methods for this module
+exports.on = on.bind(null, panicEmitter);
+exports.once = once.bind(null, panicEmitter);
+exports.off = off.bind(null, panicEmitter);
+
+// listen to 'panic-start' events in the observer-service since they may come
+// from other instances of this module
+add('panic-start', function () {
+ setPanic(true);
+ emit(panicEmitter, 'start');
+});
+// listen to 'panic-end' events for the same reason as for 'panic-start'
+add('panic-end', function () {
+ setPanic(false);
+ emit(panicEmitter, 'end');
+});
+
+// cleanup prefs on startup, since the add-on could be installed before or
+// during startup
+if (loadReason == 'startup') {
+ // check the end timestamp (for the reboot situation)
+ if (getEndTime() <= Date.now()) {
+ setEndTime(0);
+
+ if (inPanic()) {
+ setPanic(false);
+ }
+ }
+}
+
+// clean up prefs on shutdown, don't do cleanup on other reasons because there
+// may be other instances of the module running
+unload(function(reason) {
+ if (reason == 'shutdown') {
+ setPanic(false);
+ setEndTime(0);
+ }
+});