addon-sdk/source/lib/sdk/timers.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict';
michael@0 5
michael@0 6 module.metadata = {
michael@0 7 "stability": "stable"
michael@0 8 };
michael@0 9
michael@0 10 const { CC, Cc, Ci } = require("chrome");
michael@0 11 const { when: unload } = require("./system/unload");
michael@0 12
michael@0 13 const { TYPE_ONE_SHOT, TYPE_REPEATING_SLACK } = Ci.nsITimer;
michael@0 14 const Timer = CC("@mozilla.org/timer;1", "nsITimer");
michael@0 15 const timers = Object.create(null);
michael@0 16 const threadManager = Cc["@mozilla.org/thread-manager;1"].
michael@0 17 getService(Ci.nsIThreadManager);
michael@0 18 const prefBranch = Cc["@mozilla.org/preferences-service;1"].
michael@0 19 getService(Ci.nsIPrefService).
michael@0 20 QueryInterface(Ci.nsIPrefBranch);
michael@0 21
michael@0 22 let MIN_DELAY = 4;
michael@0 23 // Try to get min timeout delay used by browser.
michael@0 24 try { MIN_DELAY = prefBranch.getIntPref("dom.min_timeout_value"); } finally {}
michael@0 25
michael@0 26
michael@0 27 // Last timer id.
michael@0 28 let lastID = 0;
michael@0 29
michael@0 30 // Sets typer either by timeout or by interval
michael@0 31 // depending on a given type.
michael@0 32 function setTimer(type, callback, delay, ...args) {
michael@0 33 let id = ++ lastID;
michael@0 34 let timer = timers[id] = Timer();
michael@0 35 timer.initWithCallback({
michael@0 36 notify: function notify() {
michael@0 37 try {
michael@0 38 if (type === TYPE_ONE_SHOT)
michael@0 39 delete timers[id];
michael@0 40 callback.apply(null, args);
michael@0 41 }
michael@0 42 catch(error) {
michael@0 43 console.exception(error);
michael@0 44 }
michael@0 45 }
michael@0 46 }, Math.max(delay || MIN_DELAY), type);
michael@0 47 return id;
michael@0 48 }
michael@0 49
michael@0 50 function unsetTimer(id) {
michael@0 51 let timer = timers[id];
michael@0 52 delete timers[id];
michael@0 53 if (timer) timer.cancel();
michael@0 54 }
michael@0 55
michael@0 56 let immediates = new Map();
michael@0 57
michael@0 58 let dispatcher = _ => {
michael@0 59 // Allow scheduling of a new dispatch loop.
michael@0 60 dispatcher.scheduled = false;
michael@0 61 // Take a snapshot of timer `id`'s that have being present before
michael@0 62 // starting a dispatch loop, in order to ignore timers registered
michael@0 63 // in side effect to dispatch while also skipping immediates that
michael@0 64 // were removed in side effect.
michael@0 65 let ids = [id for ([id] of immediates)];
michael@0 66 for (let id of ids) {
michael@0 67 let immediate = immediates.get(id);
michael@0 68 if (immediate) {
michael@0 69 immediates.delete(id);
michael@0 70 try { immediate(); }
michael@0 71 catch (error) { console.exception(error); }
michael@0 72 }
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 function setImmediate(callback, ...params) {
michael@0 77 let id = ++ lastID;
michael@0 78 // register new immediate timer with curried params.
michael@0 79 immediates.set(id, _ => callback.apply(callback, params));
michael@0 80 // if dispatch loop is not scheduled schedule one. Own scheduler
michael@0 81 if (!dispatcher.scheduled) {
michael@0 82 dispatcher.scheduled = true;
michael@0 83 threadManager.currentThread.dispatch(dispatcher,
michael@0 84 Ci.nsIThread.DISPATCH_NORMAL);
michael@0 85 }
michael@0 86 return id;
michael@0 87 }
michael@0 88
michael@0 89 function clearImmediate(id) {
michael@0 90 immediates.delete(id);
michael@0 91 }
michael@0 92
michael@0 93 // Bind timers so that toString-ing them looks same as on native timers.
michael@0 94 exports.setImmediate = setImmediate.bind(null);
michael@0 95 exports.clearImmediate = clearImmediate.bind(null);
michael@0 96 exports.setTimeout = setTimer.bind(null, TYPE_ONE_SHOT);
michael@0 97 exports.setInterval = setTimer.bind(null, TYPE_REPEATING_SLACK);
michael@0 98 exports.clearTimeout = unsetTimer.bind(null);
michael@0 99 exports.clearInterval = unsetTimer.bind(null);
michael@0 100
michael@0 101 // all timers are cleared out on unload.
michael@0 102 unload(function() {
michael@0 103 immediates.clear();
michael@0 104 Object.keys(timers).forEach(unsetTimer)
michael@0 105 });

mercurial