xpfe/test/winopen.js

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 // target for window.open()
     2 const KID_URL       = "child-window.html";
     4 // formats final results
     5 const SERVER_URL    = "http://jrgm.mcom.com/cgi-bin/window-open-2.0/openreport.pl";
     7 // let system settle between each window.open
     8 const OPENER_DELAY  = 1000;   
    10 // three phases: single open/close; overlapped open/close; open-all/close-all
    11 var PHASE_ONE     = 10; 
    12 var PHASE_TWO     = 0; 
    13 var PHASE_THREE   = 0; 
    15 // keep this many windows concurrently open during overlapped phase
    16 var OVERLAP_COUNT = 3;
    18 // repeat three phases CYCLES times    
    19 var CYCLES        = 1;  
    21 // autoclose flag
    22 var AUTOCLOSE = 1;
    24 // Chrome url for child windows.
    25 var KID_CHROME   = null;
    26 var SAVED_CHROME = null;
    28 // URL options and correspnding vars.
    29 const options = [ [ "phase1", "PHASE_ONE", false ],
    30                   [ "phase2", "PHASE_TWO", false ],
    31                   [ "phase3", "PHASE_THREE", false ],
    32                   [ "overlap", "OVERLAP_COUNT", false ],
    33                   [ "cycles", "CYCLES", false ],
    34                   [ "chrome", "KID_CHROME", true ],
    35                   [ "close", "AUTOCLOSE", false ] ];
    37 // Note: You can attach search options to the url for this file to control
    38 // any of the options in the array above.  E.g., specifying
    39 // mozilla -chrome "file:///D|/mozilla/xpfe/test/winopen.xul?phase1=16&close=0"
    40 // will run this script with PHASE_ONE=16 and AUTOCLOSE=0.
    41 //
    42 // On Win32, you must enclose the -chrome option in quotes in order pass funny Win32 shell
    43 // characters such as '&' or '|'!
    45 var opts = window.location.search.substring(1).split( '&' );
    46 for ( opt in opts ) {
    47     for ( var i in options ) {
    48         if ( opts[opt].indexOf( options[i][0]+"=" ) == 0 ) {
    49             var newVal = opts[opt].split( '=' )[ 1 ];
    50             // wrap with quotes, if required.
    51             if ( options[i][2] ) {
    52                 newVal = '"' + newVal + '"';
    53             }
    54             eval( options[i][1] + "=" + newVal + ";" );
    55         }
    56     }
    57 }
    59 var prefs = null;
    61 if ( KID_CHROME ) {
    62     // Reset browser.chromeURL so it points to KID_CHROME.
    63     // This will cause window.open in openWindow to open that chrome.
    64     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    65     prefs = Components.classes["@mozilla.org/preferences-service;1"]
    66               .getService( Components.interfaces.nsIPrefBranch );
    67     SAVED_CHROME = prefs.getCharPref( "browser.chromeURL" );
    68     prefs.setCharPref( "browser.chromeURL", KID_CHROME );
    69 }
    71 const CYCLE_SIZE    = PHASE_ONE + PHASE_TWO + PHASE_THREE;
    72 const MAX_INDEX     = CYCLE_SIZE * CYCLES;  // total number of windows to open
    74 var windowList      = [];   // handles to opened windows
    75 var startingTimes   = [];   // time that window.open is called
    76 var openingTimes    = [];   // time that child window took to fire onload
    77 var closingTimes    = [];   // collect stats for case of closing >1 windows
    78 var currentIndex    = 0;       
    81 function childIsOpen(aTime) {
    82     openingTimes[currentIndex] = aTime - startingTimes[currentIndex];
    83     updateDisplay(currentIndex, openingTimes[currentIndex]);
    84     reapWindows(currentIndex);
    85     currentIndex++;
    86     if (currentIndex < MAX_INDEX)
    87         scheduleNextWindow();
    88     else
    89         window.setTimeout(reportResults, OPENER_DELAY);
    90 }
    93 function updateDisplay(index, time) {
    94     var formIndex = document.getElementById("formIndex");
    95     if (formIndex) 
    96         formIndex.setAttribute("value", index+1);
    97     var formTime  = document.getElementById("formTime");
    98     if (formTime) 
    99         formTime.setAttribute("value", time);
   100 }
   103 function scheduleNextWindow() {
   104     window.setTimeout(openWindow, OPENER_DELAY);
   105 }
   108 function closeOneWindow(aIndex) {
   109     var win = windowList[aIndex];
   110     // no-op if window is already closed
   111     if (win && !win.closed) {
   112 	win.close();
   113 	windowList[aIndex] = null;
   114     }
   115 }    
   118 function closeAllWindows(aRecordTimeToClose) {
   119     var timeToClose = (new Date()).getTime();
   120     var count = 0;
   121     for (var i = 0; i < windowList.length; i++) {
   122 	if (windowList[i])
   123 	    count++;
   124 	closeOneWindow(i);
   125     }
   126     if (aRecordTimeToClose && count > 0) {
   127         timeToClose = (new Date()).getTime() - timeToClose;
   128         closingTimes.push(parseInt(timeToClose/count));
   129     }
   130 }    
   133 // close some, none, or all open windows in the list
   134 function reapWindows() {
   135     var modIndex = currentIndex % CYCLE_SIZE;
   136     if (modIndex < PHASE_ONE-1) {
   137 	// first phase in each "cycle", are single open/close sequences
   138 	closeOneWindow(currentIndex);
   139     } 
   140     else if (PHASE_ONE-1 <= modIndex && modIndex < PHASE_ONE+PHASE_TWO-1) {
   141 	// next phase in each "cycle", keep N windows concurrently open
   142 	closeOneWindow(currentIndex - OVERLAP_COUNT);
   143     }
   144     else if (modIndex == PHASE_ONE+PHASE_TWO-1) {
   145 	// end overlapping windows cycle; close all windows
   146 	closeAllWindows(false);
   147     }
   148     else if (PHASE_ONE+PHASE_TWO <= modIndex && modIndex < CYCLE_SIZE-1) {
   149 	// do nothing; keep adding windows
   150     }
   151     else if (modIndex == CYCLE_SIZE-1) {
   152 	// end open-all/close-all phase; close windows, recording time to close
   153 	closeAllWindows(true);
   154     }
   155 }
   157 function calcMedian( numbers ) {
   158     if ( numbers.length == 0 ) {
   159         return 0;
   160     } else if ( numbers.length == 1 ) {
   161         return numbers[0];
   162     } else if ( numbers.length == 2 ) {
   163         return ( numbers[0] + numbers[1] ) / 2;
   164     } else {
   165         numbers.sort( function (a,b){ return a-b; } );
   166         var n = Math.floor( numbers.length / 2 );
   167         return numbers.length % 2 ? numbers[n] : ( numbers[n-1] + numbers[n] ) / 2;
   168     }
   169 }
   171 function reportResults() {
   172     //XXX need to create a client-side method to do this?
   173     var opening = openingTimes.join(':'); // times for each window open
   174     var closing = closingTimes.join(':'); // these are for >1 url, as a group
   175     //var ua = escape(navigator.userAgent).replace(/\+/g, "%2B"); // + == ' ', on servers
   176     //var reportURL = SERVER_URL + 
   177     //    "?opening="    + opening + 
   178     //    "&closing="    + closing + 
   179     //    "&maxIndex="   + MAX_INDEX + 
   180     //    "&cycleSize="  + CYCLE_SIZE + 
   181 	//"&ua="         + ua;
   182     //window.open(reportURL, "test-results");
   183     var avgOpenTime = 0;
   184     var minOpenTime = 99999;
   185     var maxOpenTime = 0;
   186     var medOpenTime = calcMedian( openingTimes.slice(1) );
   187     // ignore first open
   188     for (i = 1; i < MAX_INDEX; i++) {
   189         avgOpenTime += openingTimes[i];
   190         if ( minOpenTime > openingTimes[i] ) {
   191             minOpenTime = openingTimes[i];
   192         }
   193         if ( maxOpenTime < openingTimes[i] ) {
   194             maxOpenTime = openingTimes[i];
   195         }
   196     }
   197     avgOpenTime = Math.round(avgOpenTime / (MAX_INDEX - 1));
   198     dump("openingTimes="+openingTimes.slice(1)+"\n");
   199     dump("avgOpenTime:" + avgOpenTime + "\n" );
   200     dump("minOpenTime:" + minOpenTime + "\n" );
   201     dump("maxOpenTime:" + maxOpenTime + "\n" );
   202     dump("medOpenTime:" + medOpenTime + "\n" );
   203     dump("__xulWinOpenTime:" + medOpenTime + "\n");
   204     // Close the root window, if required.
   205     if ( AUTOCLOSE ) {
   206         window.close();
   207     } else {
   208         document.getElementById("formTimes").value = openingTimes.slice(1);
   209         document.getElementById("formAvg").value   = avgOpenTime;
   210         document.getElementById("formMin").value   = minOpenTime;
   211         document.getElementById("formMax").value   = maxOpenTime;
   212         document.getElementById("formMed").value   = medOpenTime;
   213         document.getElementById("formAgain").setAttribute( "disabled", "false" );
   214     }
   215 }
   217 function tryAgain() {
   218     document.getElementById("formAgain").setAttribute( "disabled", "true" );
   219     windowList      = [];
   220     startingTimes   = [];
   221     openingTimes    = [];
   222     closingTimes    = [];
   223     currentIndex    = 0;       
   224     openWindow();
   225 }
   227 function restoreChromeURL() {
   228     // Restore browser.chromeURL pref.
   229     if ( KID_CHROME && SAVED_CHROME.length ) {
   230         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
   231         prefs.setCharPref( "browser.chromeURL", SAVED_CHROME );
   232     }
   233 }
   235 function openWindow() {
   236     startingTimes[currentIndex] = (new Date()).getTime();
   237     var path   = window.location.pathname.substring( 0, window.location.pathname.lastIndexOf('/') );
   238     var url    = window.location.protocol + "//" + 
   239                  window.location.hostname + path + "/" +
   240                  KID_URL;
   241     windowList[currentIndex] = window.open(url, currentIndex);
   242 }

mercurial