toolkit/components/printing/content/printProgress.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.

     1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 // dialog is just an array we'll use to store various properties from the dialog document...
     8 var dialog;
    10 // the printProgress is a nsIPrintProgress object
    11 var printProgress = null; 
    13 // random global variables...
    14 var targetFile;
    16 var docTitle = "";
    17 var docURL   = "";
    18 var progressParams = null;
    19 var switchUI = true;
    21 function ellipseString(aStr, doFront)
    22 {
    23   if (aStr.length > 3 && (aStr.substr(0, 3) == "..." || aStr.substr(aStr.length-4, 3) == "...")) {
    24     return aStr;
    25   }
    27   var fixedLen = 64;
    28   if (aStr.length > fixedLen) {
    29     if (doFront) {
    30       var endStr = aStr.substr(aStr.length-fixedLen, fixedLen);
    31       var str = "..." + endStr;
    32       return str;
    33     } else {
    34       var frontStr = aStr.substr(0, fixedLen);
    35       var str = frontStr + "...";
    36       return str;
    37     }
    38   }
    39   return aStr;
    40 }
    42 // all progress notifications are done through the nsIWebProgressListener implementation...
    43 var progressListener = {
    44     onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus)
    45     {
    46       if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_START)
    47       {
    48         // Put progress meter in undetermined mode.
    49         // dialog.progress.setAttribute( "value", 0 );
    50         dialog.progress.setAttribute( "mode", "undetermined" );
    51       }
    53       if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
    54       {
    55         // we are done printing 
    56         // Indicate completion in title area.
    57         var msg = getString( "printComplete" );
    58         dialog.title.setAttribute("value", msg);
    60         // Put progress meter at 100%.
    61         dialog.progress.setAttribute( "value", 100 );
    62         dialog.progress.setAttribute( "mode", "normal" );
    63         var percentPrint = getString( "progressText" );
    64         percentPrint = replaceInsert( percentPrint, 1, 100 );
    65         dialog.progressText.setAttribute("value", percentPrint);
    67         var fm = Components.classes["@mozilla.org/focus-manager;1"]
    68                      .getService(Components.interfaces.nsIFocusManager);
    69         if (fm && fm.activeWindow == window) {
    70           // This progress dialog is the currently active window. In
    71           // this case we need to make sure that some other window
    72           // gets focus before we close this dialog to work around the
    73           // buggy Windows XP Fax dialog, which ends up parenting
    74           // itself to the currently focused window and is unable to
    75           // survive that window going away. What happens without this
    76           // opener.focus() call on Windows XP is that the fax dialog
    77           // is opened only to go away when this dialog actually
    78           // closes (which can happen asynchronously, so the fax
    79           // dialog just flashes once and then goes away), so w/o this
    80           // fix, it's impossible to fax on Windows XP w/o manually
    81           // switching focus to another window (or holding on to the
    82           // progress dialog with the mouse long enough).
    83           opener.focus();
    84         }
    86         window.close();
    87       }
    88     },
    90     onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
    91     {
    92       if (switchUI) 
    93       {
    94         dialog.tempLabel.setAttribute("hidden", "true");
    95         dialog.progress.setAttribute("hidden", "false");
    96         dialog.cancel.setAttribute("disabled", "false");
    98         var progressLabel = getString("progress");
    99         if (progressLabel == "") {
   100           progressLabel = "Progress:"; // better than nothing
   101         }
   102         switchUI = false;
   103       }
   105       if (progressParams)
   106       {
   107         var docTitleStr = ellipseString(progressParams.docTitle, false);
   108         if (docTitleStr != docTitle) {
   109           docTitle = docTitleStr;
   110           dialog.title.value = docTitle;
   111         }
   112         var docURLStr = progressParams.docURL;
   113         if (docURLStr != docURL && dialog.title != null) {
   114           docURL = docURLStr;
   115           if (docTitle == "") {
   116             dialog.title.value = ellipseString(docURLStr, true);
   117           }
   118         }
   119       }
   121       // Calculate percentage.
   122       var percent;
   123       if ( aMaxTotalProgress > 0 ) 
   124       {
   125         percent = Math.round( (aCurTotalProgress*100)/aMaxTotalProgress );
   126         if ( percent > 100 )
   127           percent = 100;
   129         dialog.progress.removeAttribute( "mode");
   131         // Advance progress meter.
   132         dialog.progress.setAttribute( "value", percent );
   134         // Update percentage label on progress meter.
   135         var percentPrint = getString( "progressText" );
   136         percentPrint = replaceInsert( percentPrint, 1, percent );
   137         dialog.progressText.setAttribute("value", percentPrint);
   138       } 
   139       else 
   140       {
   141         // Progress meter should be barber-pole in this case.
   142         dialog.progress.setAttribute( "mode", "undetermined" );
   143         // Update percentage label on progress meter.
   144         dialog.progressText.setAttribute("value", "");
   145       }
   146     },
   148 	  onLocationChange: function(aWebProgress, aRequest, aLocation, aFlags)
   149     {
   150       // we can ignore this notification
   151     },
   153     onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage)
   154     {
   155       if (aMessage != "")
   156         dialog.title.setAttribute("value", aMessage);
   157     },
   159     onSecurityChange: function(aWebProgress, aRequest, state)
   160     {
   161       // we can ignore this notification
   162     },
   164     QueryInterface : function(iid)
   165     {
   166      if (iid.equals(Components.interfaces.nsIWebProgressListener) || iid.equals(Components.interfaces.nsISupportsWeakReference))
   167       return this;
   169      throw Components.results.NS_NOINTERFACE;
   170     }
   171 };
   173 function getString( stringId ) {
   174    // Check if we've fetched this string already.
   175    if (!(stringId in dialog.strings)) {
   176       // Try to get it.
   177       var elem = document.getElementById( "dialog.strings."+stringId );
   178       try {
   179         if ( elem
   180            &&
   181            elem.childNodes
   182            &&
   183            elem.childNodes[0]
   184            &&
   185            elem.childNodes[0].nodeValue ) {
   186          dialog.strings[ stringId ] = elem.childNodes[0].nodeValue;
   187         } else {
   188           // If unable to fetch string, use an empty string.
   189           dialog.strings[ stringId ] = "";
   190         }
   191       } catch (e) { dialog.strings[ stringId ] = ""; }
   192    }
   193    return dialog.strings[ stringId ];
   194 }
   196 function loadDialog() 
   197 {
   198 }
   200 function replaceInsert( text, index, value ) {
   201    var result = text;
   202    var regExp = new RegExp( "#"+index );
   203    result = result.replace( regExp, value );
   204    return result;
   205 }
   207 function onLoad() {
   209     // Set global variables.
   210     printProgress = window.arguments[0];
   211     if (window.arguments[1])
   212     {
   213       progressParams = window.arguments[1].QueryInterface(Components.interfaces.nsIPrintProgressParams)
   214       if (progressParams)
   215       {
   216         docTitle = ellipseString(progressParams.docTitle, false);
   217         docURL   = ellipseString(progressParams.docURL, true);
   218       }
   219     }
   221     if ( !printProgress ) {
   222         dump( "Invalid argument to printProgress.xul\n" );
   223         window.close()
   224         return;
   225     }
   227     dialog = new Object;
   228     dialog.strings = new Array;
   229     dialog.title        = document.getElementById("dialog.title");
   230     dialog.titleLabel   = document.getElementById("dialog.titleLabel");
   231     dialog.progress     = document.getElementById("dialog.progress");
   232     dialog.progressText = document.getElementById("dialog.progressText");
   233     dialog.progressLabel = document.getElementById("dialog.progressLabel");
   234     dialog.tempLabel    = document.getElementById("dialog.tempLabel");
   235     dialog.cancel       = document.getElementById("cancel");
   237     dialog.progress.setAttribute("hidden", "true");
   238     dialog.cancel.setAttribute("disabled", "true");
   240     var progressLabel = getString("preparing");
   241     if (progressLabel == "") {
   242       progressLabel = "Preparing..."; // better than nothing
   243     }
   244     dialog.tempLabel.value = progressLabel;
   246     dialog.title.value = docTitle;
   248     // Set up dialog button callbacks.
   249     var object = this;
   250     doSetOKCancel("", function () { return object.onCancel();});
   252     // Fill dialog.
   253     loadDialog();
   255     // set our web progress listener on the helper app launcher
   256     printProgress.registerListener(progressListener);
   257     moveToAlertPosition();
   258     //We need to delay the set title else dom will overwrite it
   259     window.setTimeout(doneIniting, 500);
   260 }
   262 function onUnload() 
   263 {
   264   if (printProgress)
   265   {
   266    try 
   267    {
   268      printProgress.unregisterListener(progressListener);
   269      printProgress = null;
   270    }
   272    catch( exception ) {}
   273   }
   274 }
   276 // If the user presses cancel, tell the app launcher and close the dialog...
   277 function onCancel () 
   278 {
   279   // Cancel app launcher.
   280    try 
   281    {
   282      printProgress.processCanceledByUser = true;
   283    }
   284    catch( exception ) {return true;}
   286   // don't Close up dialog by returning false, the backend will close the dialog when everything will be aborted.
   287   return false;
   288 }
   290 function doneIniting() 
   291 {
   292   printProgress.doneIniting();
   293 }

mercurial