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.

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

mercurial