Bug 1669187. Make sure we have a last used printer saved in the common case. r=bobowen,remote-protocol-reviewers
authorJonathan Watt <jwatt@jwatt.org>
Thu, 08 Oct 2020 13:41:24 +0000
changeset 552205 9270d433458733f104989082c3be6275a2b06146
parent 552204 f9d9e05386aca86eb31690300ca0a6fd8dcf92e7
child 552206 10655b36d226857f983388a1a87a85cdae558689
push id37847
push usercsabou@mozilla.com
push dateFri, 09 Oct 2020 04:17:54 +0000
treeherdermozilla-central@600f47bbfeb2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbobowen, remote-protocol-reviewers
bugs1669187
milestone83.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1669187. Make sure we have a last used printer saved in the common case. r=bobowen,remote-protocol-reviewers Differential Revision: https://phabricator.services.mozilla.com/D92879
toolkit/components/printing/content/printUtils.js
--- a/toolkit/components/printing/content/printUtils.js
+++ b/toolkit/components/printing/content/printUtils.js
@@ -96,18 +96,40 @@ var PrintUtils = {
 
   /**
    * Shows the page setup dialog, and saves any settings changed in
    * that dialog if print.save_print_settings is set to true.
    *
    * @return true on success, false on failure
    */
   showPageSetup() {
+    let printSettings = this.getPrintSettings();
+    // If we come directly from the Page Setup menu, the hack in
+    // _enterPrintPreview will not have been invoked to set the last used
+    // printer name. For the reasons outlined at that hack, we want that set
+    // here too.
+    let lastUsedPrinterName = this._getLastUsedPrinterName();
+    if (!lastUsedPrinterName) {
+      if (printSettings.printerName) {
+        let PSSVC = Cc["@mozilla.org/gfx/printsettings-service;1"].getService(
+          Ci.nsIPrintSettingsService
+        );
+        PSSVC.savePrintSettingsToPrefs(
+          printSettings,
+          false,
+          Ci.nsIPrintSettings.kInitSavePrinterName
+        );
+        PSSVC.savePrintSettingsToPrefs(
+          printSettings,
+          true,
+          Ci.nsIPrintSettings.kInitSaveAll
+        );
+      }
+    }
     try {
-      var printSettings = this.getPrintSettings();
       var PRINTPROMPTSVC = Cc[
         "@mozilla.org/embedcomp/printingprompt-service;1"
       ].getService(Ci.nsIPrintingPromptService);
       PRINTPROMPTSVC.showPageSetupDialog(window, printSettings, null);
     } catch (e) {
       dump("showPageSetup " + e + "\n");
       return false;
     }
@@ -585,16 +607,25 @@ var PrintUtils = {
 
   _setPrinterDefaultsForSelectedPrinter(
     aPSSVC,
     aPrintSettings,
     defaultsOnly = false
   ) {
     if (!aPrintSettings.printerName) {
       aPrintSettings.printerName = aPSSVC.lastUsedPrinterName;
+      if (!aPrintSettings.printerName) {
+        // It is important to try to avoid passing settings over to the
+        // content process in the old print UI by saving to unprefixed prefs.
+        // To avoid that we try to get the name of a printer we can use.
+        let printerList = Cc["@mozilla.org/gfx/printerlist;1"].getService(
+          Ci.nsIPrinterList
+        );
+        aPrintSettings.printerName = printerList.systemDefaultPrinterName;
+      }
     }
 
     // First get any defaults from the printer. We want to skip this for Save to
     // PDF since it isn't a real printer and will throw.
     if (aPrintSettings.printerName != this.SAVE_TO_PDF_PRINTER) {
       aPSSVC.initPrintSettingsFromPrinter(
         aPrintSettings.printerName,
         aPrintSettings
@@ -694,16 +725,42 @@ var PrintUtils = {
     let changingPrintPreviewBrowsers = false;
     if (this._currentPPBrowser && ppBrowser != this._currentPPBrowser) {
       changingPrintPreviewBrowsers = true;
       oldPPBrowser = this._currentPPBrowser;
     }
     this._currentPPBrowser = ppBrowser;
     let mm = ppBrowser.messageManager;
     let lastUsedPrinterName = this._getLastUsedPrinterName();
+    if (!lastUsedPrinterName) {
+      // We "pass" print settings over to the content process by saving them to
+      // prefs (yuck!). It is important to try to avoid saving to prefs without
+      // prefixing them with a printer name though, so this hack tries to make
+      // sure that (in the common case) we have set the "last used" printer,
+      // which makes us save to prefs prefixed with its name, and makes sure
+      // the content process will pick settings up from those prefixed prefs
+      // too.
+      let settings = this.getPrintSettings();
+      if (settings.printerName) {
+        let PSSVC = Cc["@mozilla.org/gfx/printsettings-service;1"].getService(
+          Ci.nsIPrintSettingsService
+        );
+        PSSVC.savePrintSettingsToPrefs(
+          settings,
+          false,
+          Ci.nsIPrintSettings.kInitSavePrinterName
+        );
+        PSSVC.savePrintSettingsToPrefs(
+          settings,
+          true,
+          Ci.nsIPrintSettings.kInitSaveAll
+        );
+        lastUsedPrinterName = settings.printerName;
+      }
+    }
 
     let sendEnterPreviewMessage = function(browser, simplified) {
       mm.sendAsyncMessage("Printing:Preview:Enter", {
         browsingContextId: browser.browsingContext.id,
         simplifiedMode: simplified,
         changingBrowsers: changingPrintPreviewBrowsers,
         lastUsedPrinterName,
       });