Bug 1329216 - Pass default printer name to child. r=mconley draft
authorJulian Hector <julian.r.hector@gmail.com>
Mon, 13 Feb 2017 13:07:44 +0100
changeset 485859 76b7e89dd3e555f0c38f87c62bf112c5835ba6d3
parent 485858 2188dacfc757e568275f2b4f3dd4c97816a9eadc
child 485860 47e5c01413df0ee5e5bf1dcb6910dbed83e2d1ac
push id45870
push userbmo:kechen@mozilla.com
push dateFri, 17 Feb 2017 09:36:54 +0000
reviewersmconley
bugs1329216
milestone54.0a1
Bug 1329216 - Pass default printer name to child. r=mconley
toolkit/components/printing/content/printUtils.js
toolkit/content/browser-content.js
--- a/toolkit/components/printing/content/printUtils.js
+++ b/toolkit/components/printing/content/printUtils.js
@@ -95,29 +95,44 @@ var PrintUtils = {
       }
     } catch (e) {
       dump("showPageSetup " + e + "\n");
       return false;
     }
     return true;
   },
 
+  getDefaultPrinterName() {
+    try {
+      let PSSVC = Cc["@mozilla.org/gfx/printsettings-service;1"]
+                    .getService(Ci.nsIPrintSettingsService);
+
+      return PSSVC.defaultPrinterName;
+    } catch (e) {
+      Components.utils.reportError(e);
+    }
+
+    return null;
+  },
+
   /**
    * Starts the process of printing the contents of a window.
    *
    * @param aWindowID
    *        The outer window ID of the nsIDOMWindow to print.
    * @param aBrowser
    *        The <xul:browser> that the nsIDOMWindow for aWindowID belongs to.
    */
   printWindow(aWindowID, aBrowser) {
     let mm = aBrowser.messageManager;
+    let defaultPrinterName = this.getDefaultPrinterName();
     mm.sendAsyncMessage("Printing:Print", {
       windowID: aWindowID,
       simplifiedMode: this._shouldSimplify,
+      defaultPrinterName,
     });
   },
 
   /**
    * Deprecated.
    *
    * Starts the process of printing the contents of window.content.
    *
@@ -486,21 +501,23 @@ var PrintUtils = {
   enterPrintPreview() {
     // Send a message to the print preview browser to initialize
     // print preview. If we happen to have gotten a print preview
     // progress listener from nsIPrintingPromptService.showProgress
     // in printPreview, we add listeners to feed that progress
     // listener.
     let ppBrowser = this._listener.getPrintPreviewBrowser();
     let mm = ppBrowser.messageManager;
+    let defaultPrinterName = this.getDefaultPrinterName();
 
     let sendEnterPreviewMessage = function(browser, simplified) {
       mm.sendAsyncMessage("Printing:Preview:Enter", {
         windowID: browser.outerWindowID,
         simplifiedMode: simplified,
+        defaultPrinterName,
       });
     };
 
     // If we happen to have gotten simplify page checked, we will lazily
     // instantiate a new tab that parses the original page using ReaderMode
     // primitives. When it's ready, and in order to enter on preview, we send
     // over a message to print preview browser passing up the simplified tab as
     // reference. If not, we pass the original tab instead as content source.
--- a/toolkit/content/browser-content.js
+++ b/toolkit/content/browser-content.js
@@ -438,17 +438,17 @@ var Printing = {
       });
     }
   },
 
   receiveMessage(message) {
     let data = message.data;
     switch (message.name) {
       case "Printing:Preview:Enter": {
-        this.enterPrintPreview(Services.wm.getOuterWindowWithId(data.windowID), data.simplifiedMode);
+        this.enterPrintPreview(Services.wm.getOuterWindowWithId(data.windowID), data.simplifiedMode, data.defaultPrinterName);
         break;
       }
 
       case "Printing:Preview:Exit": {
         this.exitPrintPreview();
         break;
       }
 
@@ -463,30 +463,30 @@ var Printing = {
       }
 
       case "Printing:Preview:UpdatePageCount": {
         this.updatePageCount();
         break;
       }
 
       case "Printing:Print": {
-        this.print(Services.wm.getOuterWindowWithId(data.windowID), data.simplifiedMode);
+        this.print(Services.wm.getOuterWindowWithId(data.windowID), data.simplifiedMode, data.defaultPrinterName);
         break;
       }
     }
   },
 
-  getPrintSettings() {
+  getPrintSettings(defaultPrinterName) {
     try {
       let PSSVC = Cc["@mozilla.org/gfx/printsettings-service;1"]
                     .getService(Ci.nsIPrintSettingsService);
 
       let printSettings = PSSVC.globalPrintSettings;
       if (!printSettings.printerName) {
-        printSettings.printerName = PSSVC.defaultPrinterName;
+        printSettings.printerName = defaultPrinterName;
       }
       // First get any defaults from the printer
       PSSVC.initPrintSettingsFromPrinter(printSettings.printerName,
                                          printSettings);
       // now augment them with any values from last time
       PSSVC.initPrintSettingsFromPrefs(printSettings, true,
                                        printSettings.kInitSaveAll);
 
@@ -615,17 +615,17 @@ var Printing = {
 
       readerContent.appendChild(contentFragment);
 
       // Display reader content element
       readerContent.style.display = "block";
     });
   },
 
-  enterPrintPreview(contentWindow, simplifiedMode) {
+  enterPrintPreview(contentWindow, simplifiedMode, defaultPrinterName) {
     // We'll call this whenever we've finished reflowing the document, or if
     // we errored out while attempting to print preview (in which case, we'll
     // notify the parent that we've failed).
     let notifyEntered = (error) => {
       removeEventListener("printPreviewUpdate", onPrintPreviewReady);
       sendAsyncMessage("Printing:Preview:Entered", {
         failed: !!error,
       });
@@ -638,17 +638,17 @@ var Printing = {
     // We have to wait for the print engine to finish reflowing all of the
     // documents and subdocuments before we can tell the parent to flip to
     // the print preview UI - otherwise, the print preview UI might ask for
     // information (like the number of pages in the document) before we have
     // our PresShells set up.
     addEventListener("printPreviewUpdate", onPrintPreviewReady);
 
     try {
-      let printSettings = this.getPrintSettings();
+      let printSettings = this.getPrintSettings(defaultPrinterName);
 
       // If we happen to be on simplified mode, we need to set docURL in order
       // to generate header/footer content correctly, since simplified tab has
       // "about:blank" as its URI.
       if (printSettings && simplifiedMode)
         printSettings.docURL = contentWindow.document.baseURI;
 
       docShell.printPreview.printPreview(printSettings, contentWindow, this);
@@ -659,18 +659,18 @@ var Printing = {
       notifyEntered(error);
     }
   },
 
   exitPrintPreview() {
     docShell.printPreview.exitPrintPreview();
   },
 
-  print(contentWindow, simplifiedMode) {
-    let printSettings = this.getPrintSettings();
+  print(contentWindow, simplifiedMode, defaultPrinterName) {
+    let printSettings = this.getPrintSettings(defaultPrinterName);
 
     // If we happen to be on simplified mode, we need to set docURL in order
     // to generate header/footer content correctly, since simplified tab has
     // "about:blank" as its URI.
     if (printSettings && simplifiedMode) {
       printSettings.docURL = contentWindow.document.baseURI;
     }