tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

inspector-delay-image-response.sjs (1264B)


      1 /**
      2  * Adapted from https://searchfox.org/mozilla-central/source/layout/reftests/backgrounds/delay-image-response.sjs
      3  */
      4 "use strict";
      5 
      6 // A 1x1 PNG image.
      7 // Source: https://commons.wikimedia.org/wiki/File:1x1.png (Public Domain)
      8 const IMAGE = atob(
      9   "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" +
     10     "ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII="
     11 );
     12 
     13 // To avoid GC.
     14 let timer = null;
     15 
     16 function handleRequest(request, response) {
     17   const query = {};
     18   request.queryString.split("&").forEach(function (val) {
     19     const [name, value] = val.split("=");
     20     query[name] = unescape(value);
     21   });
     22 
     23   response.setStatusLine(request.httpVersion, 200, "OK");
     24   response.setHeader("Content-Type", "image/png", false);
     25 
     26   // If there is no delay, we write the image and leave.
     27   if (!("delay" in query)) {
     28     response.write(IMAGE);
     29     return;
     30   }
     31 
     32   // If there is a delay, we create a timer which, when it fires, will write
     33   // image and leave.
     34   response.processAsync();
     35   const nsITimer = Ci.nsITimer;
     36 
     37   timer = Cc["@mozilla.org/timer;1"].createInstance(nsITimer);
     38   timer.initWithCallback(
     39     function () {
     40       response.write(IMAGE);
     41       response.finish();
     42     },
     43     query.delay,
     44     nsITimer.TYPE_ONE_SHOT
     45   );
     46 }