Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | var gPrintSettingsAreGlobal = false; |
michael@0 | 8 | var gSavePrintSettings = false; |
michael@0 | 9 | var gFocusedElement = null; |
michael@0 | 10 | |
michael@0 | 11 | var PrintUtils = { |
michael@0 | 12 | |
michael@0 | 13 | showPageSetup: function () |
michael@0 | 14 | { |
michael@0 | 15 | try { |
michael@0 | 16 | var printSettings = this.getPrintSettings(); |
michael@0 | 17 | var PRINTPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"] |
michael@0 | 18 | .getService(Components.interfaces.nsIPrintingPromptService); |
michael@0 | 19 | PRINTPROMPTSVC.showPageSetup(window, printSettings, null); |
michael@0 | 20 | if (gSavePrintSettings) { |
michael@0 | 21 | // Page Setup data is a "native" setting on the Mac |
michael@0 | 22 | var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"] |
michael@0 | 23 | .getService(Components.interfaces.nsIPrintSettingsService); |
michael@0 | 24 | PSSVC.savePrintSettingsToPrefs(printSettings, true, printSettings.kInitSaveNativeData); |
michael@0 | 25 | } |
michael@0 | 26 | } catch (e) { |
michael@0 | 27 | dump("showPageSetup "+e+"\n"); |
michael@0 | 28 | return false; |
michael@0 | 29 | } |
michael@0 | 30 | return true; |
michael@0 | 31 | }, |
michael@0 | 32 | |
michael@0 | 33 | print: function (aWindow) |
michael@0 | 34 | { |
michael@0 | 35 | var webBrowserPrint = this.getWebBrowserPrint(aWindow); |
michael@0 | 36 | var printSettings = this.getPrintSettings(); |
michael@0 | 37 | try { |
michael@0 | 38 | webBrowserPrint.print(printSettings, null); |
michael@0 | 39 | if (gPrintSettingsAreGlobal && gSavePrintSettings) { |
michael@0 | 40 | var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"] |
michael@0 | 41 | .getService(Components.interfaces.nsIPrintSettingsService); |
michael@0 | 42 | PSSVC.savePrintSettingsToPrefs(printSettings, true, |
michael@0 | 43 | printSettings.kInitSaveAll); |
michael@0 | 44 | PSSVC.savePrintSettingsToPrefs(printSettings, false, |
michael@0 | 45 | printSettings.kInitSavePrinterName); |
michael@0 | 46 | } |
michael@0 | 47 | } catch (e) { |
michael@0 | 48 | // Pressing cancel is expressed as an NS_ERROR_ABORT return value, |
michael@0 | 49 | // causing an exception to be thrown which we catch here. |
michael@0 | 50 | // Unfortunately this will also consume helpful failures, so add a |
michael@0 | 51 | // dump("print: "+e+"\n"); // if you need to debug |
michael@0 | 52 | } |
michael@0 | 53 | }, |
michael@0 | 54 | |
michael@0 | 55 | // If aCallback is not null, it must be an object which has the following methods: |
michael@0 | 56 | // getPrintPreviewBrowser(), getSourceBrowser(), |
michael@0 | 57 | // getNavToolbox(), onEnter() and onExit(). |
michael@0 | 58 | // If aCallback is null, then printPreview must previously have been called with |
michael@0 | 59 | // non-null aCallback and that object will be reused. |
michael@0 | 60 | printPreview: function (aCallback) |
michael@0 | 61 | { |
michael@0 | 62 | // if we're already in PP mode, don't set the callback; chances |
michael@0 | 63 | // are it is null because someone is calling printPreview() to |
michael@0 | 64 | // get us to refresh the display. |
michael@0 | 65 | if (!document.getElementById("print-preview-toolbar")) { |
michael@0 | 66 | this._callback = aCallback; |
michael@0 | 67 | this._sourceBrowser = aCallback.getSourceBrowser(); |
michael@0 | 68 | this._originalTitle = this._sourceBrowser.contentDocument.title; |
michael@0 | 69 | this._originalURL = this._sourceBrowser.currentURI.spec; |
michael@0 | 70 | } else { |
michael@0 | 71 | // collapse the browser here -- it will be shown in |
michael@0 | 72 | // enterPrintPreview; this forces a reflow which fixes display |
michael@0 | 73 | // issues in bug 267422. |
michael@0 | 74 | this._sourceBrowser = this._callback.getPrintPreviewBrowser(); |
michael@0 | 75 | this._sourceBrowser.collapsed = true; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | this._webProgressPP = {}; |
michael@0 | 79 | var ppParams = {}; |
michael@0 | 80 | var notifyOnOpen = {}; |
michael@0 | 81 | var webBrowserPrint = this.getWebBrowserPrint(); |
michael@0 | 82 | var printSettings = this.getPrintSettings(); |
michael@0 | 83 | // Here we get the PrintingPromptService so we can display the PP Progress from script |
michael@0 | 84 | // For the browser implemented via XUL with the PP toolbar we cannot let it be |
michael@0 | 85 | // automatically opened from the print engine because the XUL scrollbars in the PP window |
michael@0 | 86 | // will layout before the content window and a crash will occur. |
michael@0 | 87 | // Doing it all from script, means it lays out before hand and we can let printing do its own thing |
michael@0 | 88 | var PPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"] |
michael@0 | 89 | .getService(Components.interfaces.nsIPrintingPromptService); |
michael@0 | 90 | // just in case we are already printing, |
michael@0 | 91 | // an error code could be returned if the Prgress Dialog is already displayed |
michael@0 | 92 | try { |
michael@0 | 93 | PPROMPTSVC.showProgress(window, webBrowserPrint, printSettings, this._obsPP, false, |
michael@0 | 94 | this._webProgressPP, ppParams, notifyOnOpen); |
michael@0 | 95 | if (ppParams.value) { |
michael@0 | 96 | ppParams.value.docTitle = this._originalTitle; |
michael@0 | 97 | ppParams.value.docURL = this._originalURL; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // this tells us whether we should continue on with PP or |
michael@0 | 101 | // wait for the callback via the observer |
michael@0 | 102 | if (!notifyOnOpen.value.valueOf() || this._webProgressPP.value == null) |
michael@0 | 103 | this.enterPrintPreview(); |
michael@0 | 104 | } catch (e) { |
michael@0 | 105 | this.enterPrintPreview(); |
michael@0 | 106 | } |
michael@0 | 107 | }, |
michael@0 | 108 | |
michael@0 | 109 | getWebBrowserPrint: function (aWindow) |
michael@0 | 110 | { |
michael@0 | 111 | var contentWindow = aWindow || window.content; |
michael@0 | 112 | return contentWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor) |
michael@0 | 113 | .getInterface(Components.interfaces.nsIWebBrowserPrint); |
michael@0 | 114 | }, |
michael@0 | 115 | |
michael@0 | 116 | getPrintPreview: function() { |
michael@0 | 117 | return this._callback.getPrintPreviewBrowser().docShell.printPreview; |
michael@0 | 118 | }, |
michael@0 | 119 | |
michael@0 | 120 | //////////////////////////////////////// |
michael@0 | 121 | // "private" methods. Don't use them. // |
michael@0 | 122 | //////////////////////////////////////// |
michael@0 | 123 | |
michael@0 | 124 | setPrinterDefaultsForSelectedPrinter: function (aPSSVC, aPrintSettings) |
michael@0 | 125 | { |
michael@0 | 126 | if (!aPrintSettings.printerName) |
michael@0 | 127 | aPrintSettings.printerName = aPSSVC.defaultPrinterName; |
michael@0 | 128 | |
michael@0 | 129 | // First get any defaults from the printer |
michael@0 | 130 | aPSSVC.initPrintSettingsFromPrinter(aPrintSettings.printerName, aPrintSettings); |
michael@0 | 131 | // now augment them with any values from last time |
michael@0 | 132 | aPSSVC.initPrintSettingsFromPrefs(aPrintSettings, true, aPrintSettings.kInitSaveAll); |
michael@0 | 133 | }, |
michael@0 | 134 | |
michael@0 | 135 | getPrintSettings: function () |
michael@0 | 136 | { |
michael@0 | 137 | var pref = Components.classes["@mozilla.org/preferences-service;1"] |
michael@0 | 138 | .getService(Components.interfaces.nsIPrefBranch); |
michael@0 | 139 | if (pref) { |
michael@0 | 140 | gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false); |
michael@0 | 141 | gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | var printSettings; |
michael@0 | 145 | try { |
michael@0 | 146 | var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"] |
michael@0 | 147 | .getService(Components.interfaces.nsIPrintSettingsService); |
michael@0 | 148 | if (gPrintSettingsAreGlobal) { |
michael@0 | 149 | printSettings = PSSVC.globalPrintSettings; |
michael@0 | 150 | this.setPrinterDefaultsForSelectedPrinter(PSSVC, printSettings); |
michael@0 | 151 | } else { |
michael@0 | 152 | printSettings = PSSVC.newPrintSettings; |
michael@0 | 153 | } |
michael@0 | 154 | } catch (e) { |
michael@0 | 155 | dump("getPrintSettings: "+e+"\n"); |
michael@0 | 156 | } |
michael@0 | 157 | return printSettings; |
michael@0 | 158 | }, |
michael@0 | 159 | |
michael@0 | 160 | _closeHandlerPP: null, |
michael@0 | 161 | _webProgressPP: null, |
michael@0 | 162 | _callback: null, |
michael@0 | 163 | _sourceBrowser: null, |
michael@0 | 164 | _originalTitle: "", |
michael@0 | 165 | _originalURL: "", |
michael@0 | 166 | |
michael@0 | 167 | // This observer is called once the progress dialog has been "opened" |
michael@0 | 168 | _obsPP: |
michael@0 | 169 | { |
michael@0 | 170 | observe: function(aSubject, aTopic, aData) |
michael@0 | 171 | { |
michael@0 | 172 | // delay the print preview to show the content of the progress dialog |
michael@0 | 173 | setTimeout(function () { PrintUtils.enterPrintPreview(); }, 0); |
michael@0 | 174 | }, |
michael@0 | 175 | |
michael@0 | 176 | QueryInterface : function(iid) |
michael@0 | 177 | { |
michael@0 | 178 | if (iid.equals(Components.interfaces.nsIObserver) || |
michael@0 | 179 | iid.equals(Components.interfaces.nsISupportsWeakReference) || |
michael@0 | 180 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 181 | return this; |
michael@0 | 182 | throw Components.results.NS_NOINTERFACE; |
michael@0 | 183 | } |
michael@0 | 184 | }, |
michael@0 | 185 | |
michael@0 | 186 | enterPrintPreview: function () |
michael@0 | 187 | { |
michael@0 | 188 | gFocusedElement = document.commandDispatcher.focusedElement; |
michael@0 | 189 | |
michael@0 | 190 | var webBrowserPrint; |
michael@0 | 191 | var printSettings = this.getPrintSettings(); |
michael@0 | 192 | var originalWindow = this._sourceBrowser.contentWindow; |
michael@0 | 193 | |
michael@0 | 194 | try { |
michael@0 | 195 | webBrowserPrint = this.getPrintPreview(); |
michael@0 | 196 | webBrowserPrint.printPreview(printSettings, originalWindow, |
michael@0 | 197 | this._webProgressPP.value); |
michael@0 | 198 | } catch (e) { |
michael@0 | 199 | // Pressing cancel is expressed as an NS_ERROR_ABORT return value, |
michael@0 | 200 | // causing an exception to be thrown which we catch here. |
michael@0 | 201 | // Unfortunately this will also consume helpful failures, so add a |
michael@0 | 202 | // dump(e); // if you need to debug |
michael@0 | 203 | |
michael@0 | 204 | // Need to call enter and exit so that UI gets back to normal. |
michael@0 | 205 | this._callback.onEnter(); |
michael@0 | 206 | this._callback.onExit(); |
michael@0 | 207 | return; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | var printPreviewTB = document.getElementById("print-preview-toolbar"); |
michael@0 | 211 | if (printPreviewTB) { |
michael@0 | 212 | printPreviewTB.updateToolbar(); |
michael@0 | 213 | var browser = this._callback.getPrintPreviewBrowser(); |
michael@0 | 214 | browser.collapsed = false; |
michael@0 | 215 | browser.contentWindow.focus(); |
michael@0 | 216 | return; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | // Set the original window as an active window so any mozPrintCallbacks can |
michael@0 | 220 | // run without delayed setTimeouts. |
michael@0 | 221 | var docShell = originalWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor) |
michael@0 | 222 | .getInterface(Components.interfaces.nsIWebNavigation) |
michael@0 | 223 | .QueryInterface(Components.interfaces.nsIDocShell); |
michael@0 | 224 | docShell.isActive = true; |
michael@0 | 225 | |
michael@0 | 226 | // show the toolbar after we go into print preview mode so |
michael@0 | 227 | // that we can initialize the toolbar with total num pages |
michael@0 | 228 | var XUL_NS = |
michael@0 | 229 | "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; |
michael@0 | 230 | printPreviewTB = document.createElementNS(XUL_NS, "toolbar"); |
michael@0 | 231 | printPreviewTB.setAttribute("printpreview", true); |
michael@0 | 232 | printPreviewTB.id = "print-preview-toolbar"; |
michael@0 | 233 | printPreviewTB.className = "toolbar-primary"; |
michael@0 | 234 | |
michael@0 | 235 | var navToolbox = this._callback.getNavToolbox(); |
michael@0 | 236 | navToolbox.parentNode.insertBefore(printPreviewTB, navToolbox); |
michael@0 | 237 | |
michael@0 | 238 | // copy the window close handler |
michael@0 | 239 | if (document.documentElement.hasAttribute("onclose")) |
michael@0 | 240 | this._closeHandlerPP = document.documentElement.getAttribute("onclose"); |
michael@0 | 241 | else |
michael@0 | 242 | this._closeHandlerPP = null; |
michael@0 | 243 | document.documentElement.setAttribute("onclose", "PrintUtils.exitPrintPreview(); return false;"); |
michael@0 | 244 | |
michael@0 | 245 | // disable chrome shortcuts... |
michael@0 | 246 | window.addEventListener("keydown", this.onKeyDownPP, true); |
michael@0 | 247 | window.addEventListener("keypress", this.onKeyPressPP, true); |
michael@0 | 248 | |
michael@0 | 249 | var browser = this._callback.getPrintPreviewBrowser(); |
michael@0 | 250 | browser.collapsed = false; |
michael@0 | 251 | browser.contentWindow.focus(); |
michael@0 | 252 | |
michael@0 | 253 | // on Enter PP Call back |
michael@0 | 254 | this._callback.onEnter(); |
michael@0 | 255 | }, |
michael@0 | 256 | |
michael@0 | 257 | exitPrintPreview: function () |
michael@0 | 258 | { |
michael@0 | 259 | window.removeEventListener("keydown", this.onKeyDownPP, true); |
michael@0 | 260 | window.removeEventListener("keypress", this.onKeyPressPP, true); |
michael@0 | 261 | |
michael@0 | 262 | // restore the old close handler |
michael@0 | 263 | document.documentElement.setAttribute("onclose", this._closeHandlerPP); |
michael@0 | 264 | this._closeHandlerPP = null; |
michael@0 | 265 | |
michael@0 | 266 | var webBrowserPrint = this.getPrintPreview(); |
michael@0 | 267 | webBrowserPrint.exitPrintPreview(); |
michael@0 | 268 | |
michael@0 | 269 | // remove the print preview toolbar |
michael@0 | 270 | var printPreviewTB = document.getElementById("print-preview-toolbar"); |
michael@0 | 271 | this._callback.getNavToolbox().parentNode.removeChild(printPreviewTB); |
michael@0 | 272 | |
michael@0 | 273 | var fm = Components.classes["@mozilla.org/focus-manager;1"] |
michael@0 | 274 | .getService(Components.interfaces.nsIFocusManager); |
michael@0 | 275 | if (gFocusedElement) |
michael@0 | 276 | fm.setFocus(gFocusedElement, fm.FLAG_NOSCROLL); |
michael@0 | 277 | else |
michael@0 | 278 | window.content.focus(); |
michael@0 | 279 | gFocusedElement = null; |
michael@0 | 280 | |
michael@0 | 281 | this._callback.onExit(); |
michael@0 | 282 | }, |
michael@0 | 283 | |
michael@0 | 284 | onKeyDownPP: function (aEvent) |
michael@0 | 285 | { |
michael@0 | 286 | // Esc exits the PP |
michael@0 | 287 | if (aEvent.keyCode == aEvent.DOM_VK_ESCAPE) { |
michael@0 | 288 | PrintUtils.exitPrintPreview(); |
michael@0 | 289 | } |
michael@0 | 290 | }, |
michael@0 | 291 | |
michael@0 | 292 | onKeyPressPP: function (aEvent) |
michael@0 | 293 | { |
michael@0 | 294 | var closeKey; |
michael@0 | 295 | try { |
michael@0 | 296 | closeKey = document.getElementById("key_close") |
michael@0 | 297 | .getAttribute("key"); |
michael@0 | 298 | closeKey = aEvent["DOM_VK_"+closeKey]; |
michael@0 | 299 | } catch (e) {} |
michael@0 | 300 | var isModif = aEvent.ctrlKey || aEvent.metaKey; |
michael@0 | 301 | // Ctrl-W exits the PP |
michael@0 | 302 | if (isModif && |
michael@0 | 303 | (aEvent.charCode == closeKey || aEvent.charCode == closeKey + 32)) { |
michael@0 | 304 | PrintUtils.exitPrintPreview(); |
michael@0 | 305 | } |
michael@0 | 306 | else if (isModif) { |
michael@0 | 307 | var printPreviewTB = document.getElementById("print-preview-toolbar"); |
michael@0 | 308 | var printKey = document.getElementById("printKb").getAttribute("key").toUpperCase(); |
michael@0 | 309 | var pressedKey = String.fromCharCode(aEvent.charCode).toUpperCase(); |
michael@0 | 310 | if (printKey == pressedKey) { |
michael@0 | 311 | PrintUtils.print(); |
michael@0 | 312 | } |
michael@0 | 313 | } |
michael@0 | 314 | // cancel shortkeys |
michael@0 | 315 | if (isModif) { |
michael@0 | 316 | aEvent.preventDefault(); |
michael@0 | 317 | aEvent.stopPropagation(); |
michael@0 | 318 | } |
michael@0 | 319 | } |
michael@0 | 320 | } |