Bug 1136855 - Send a message from the content script when printing has finished so the parent can save print settings. r=Mossop, a=sledru.
authorMike Conley <mconley@mozilla.com>
Wed, 25 Feb 2015 15:45:22 -0500
changeset 250081 4a73e4bc3ee5
parent 250080 b23a690fa325
child 250082 c0698821db6c
push id4498
push usermconley@mozilla.com
push date2015-02-26 22:28 +0000
treeherdermozilla-beta@4a73e4bc3ee5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMossop, sledru
bugs1136855
milestone37.0
Bug 1136855 - Send a message from the content script when printing has finished so the parent can save print settings. r=Mossop, a=sledru.
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
@@ -164,16 +164,39 @@ var PrintUtils = {
     if (!aBrowser) {
       throw new Error("PrintUtils.print could not resolve content window " +
                       "to a browser.");
     }
 
     let printSettings = this.getPrintSettings();
 
     let mm = aBrowser.messageManager;
+
+    mm.addMessageListener("Printing:Print:Done", function onPrintingDone(msg) {
+      mm.removeMessageListener("Printing:Print:Done", onPrintingDone);
+      let rv = msg.data.rv;
+      let printSettings = msg.objects.printSettings;
+      if (rv != Components.results.NS_OK &&
+          rv != Components.results.NS_ERROR_ABORT) {
+        Cu.reportError(`In Printing:Print:Done handler, got unexpected rv
+                        ${rv}.`);
+      }
+
+      if (gPrintSettingsAreGlobal && gSavePrintSettings) {
+        let PSSVC =
+          Components.classes["@mozilla.org/gfx/printsettings-service;1"]
+                    .getService(Components.interfaces.nsIPrintSettingsService);
+
+        PSSVC.savePrintSettingsToPrefs(printSettings, true,
+                                       printSettings.kInitSaveAll);
+        PSSVC.savePrintSettingsToPrefs(printSettings, false,
+                                       printSettings.kInitSavePrinterName);
+      }
+    });
+
     mm.sendAsyncMessage("Printing:Print", null, {
       printSettings: printSettings,
       contentWindow: aWindow,
     });
   },
 
   /**
    * Initializes print preview.
--- a/toolkit/content/browser-content.js
+++ b/toolkit/content/browser-content.js
@@ -453,19 +453,27 @@ let Printing = {
 
   print(printSettings, contentWindow) {
     // Bug 1088070 - we should instantiate nsIPrintSettings here in the
     // content script instead of passing it down as a CPOW.
     if (Cu.isCrossProcessWrapper(printSettings)) {
       printSettings = null;
     }
 
-    let print = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
-                             .getInterface(Ci.nsIWebBrowserPrint);
-    print.print(printSettings, null);
+    let rv = Cr.NS_OK;
+    try {
+      let print = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+                               .getInterface(Ci.nsIWebBrowserPrint);
+      print.print(printSettings, null);
+    } catch(e) {
+      // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
+      // causing an exception to be thrown which we catch here.
+      rv = e.result;
+    }
+    sendAsyncMessage("Printing:Print:Done", { rv }, { printSettings });
   },
 
   updatePageCount() {
     let numPages = docShell.printPreview.printPreviewNumPages;
     sendAsyncMessage("Printing:Preview:UpdatePageCount", {
       numPages: numPages,
     });
   },