toolkit/modules/Timer.jsm

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 /**
     8  * JS module implementation of nsIDOMJSWindow.setTimeout and clearTimeout.
     9  */
    11 this.EXPORTED_SYMBOLS = ["setTimeout", "clearTimeout"];
    13 const Cc = Components.classes;
    14 const Ci = Components.interfaces;
    15 const Cu = Components.utils;
    17 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    19 // This gives us >=2^30 unique timer IDs, enough for 1 per ms for 12.4 days.
    20 let gNextTimeoutId = 1; // setTimeout must return a positive integer
    22 let gTimeoutTable = new Map(); // int -> nsITimer
    24 this.setTimeout = function setTimeout(aCallback, aMilliseconds) {
    25   let id = gNextTimeoutId++;
    26   let args = Array.slice(arguments, 2);
    27   let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    28   timer.initWithCallback(function setTimeout_timer() {
    29     gTimeoutTable.delete(id);
    30     aCallback.apply(null, args);
    31   }, aMilliseconds, timer.TYPE_ONE_SHOT);
    33   gTimeoutTable.set(id, timer);
    34   return id;
    35 }
    37 this.clearTimeout = function clearTimeout(aId) {
    38   if (gTimeoutTable.has(aId)) {
    39     gTimeoutTable.get(aId).cancel();
    40     gTimeoutTable.delete(aId);
    41   }
    42 }

mercurial