From 17f8eb8decae5a21aed0b0b17e0ee32986e75fa6 Mon Sep 17 00:00:00 2001 From: Jonathan Watt Date: Sun, 30 Oct 2022 10:32:28 +0000 Subject: [PATCH] Bug 1755153 p1 - Centralize saving of last used printer name. r=emilio Differential Revision: https://phabricator.services.mozilla.com/D146943 --- toolkit/components/printing/content/print.js | 4 ++-- .../printing/content/printPageSetup.js | 2 +- .../components/printing/content/printUtils.js | 10 ++-------- widget/cocoa/nsPrintDialogX.mm | 5 ++--- widget/gtk/nsPrintDialogGTK.cpp | 7 +++---- widget/nsIPrintSettingsService.idl | 8 ++++++-- widget/nsPrintSettingsService.cpp | 20 +++++++++++++++---- 7 files changed, 32 insertions(+), 24 deletions(-) diff --git a/toolkit/components/printing/content/print.js b/toolkit/components/printing/content/print.js index 1ef15112afab..d6e5cb5ed5e4 100644 --- a/toolkit/components/printing/content/print.js +++ b/toolkit/components/printing/content/print.js @@ -398,7 +398,7 @@ var PrintEventHandler = { await window._initialized; // This seems like it should be handled automatically but it isn't. - Services.prefs.setStringPref("print_printer", settings.printerName); + PSSVC.saveLastUsedPrinterNameToPrefs(settings.printerName); try { // We'll provide our own progress indicator. @@ -795,7 +795,7 @@ var PrintEventHandler = { }, saveSettingsToPrefs(flags) { - PSSVC.savePrintSettingsToPrefs(this.settings, true, flags); + PSSVC.savePrintSettingsToPrefs(this.settings, flags); }, /** diff --git a/toolkit/components/printing/content/printPageSetup.js b/toolkit/components/printing/content/printPageSetup.js index 5f2e0ac86e9a..71ef7ef88df7 100644 --- a/toolkit/components/printing/content/printPageSetup.js +++ b/toolkit/components/printing/content/printPageSetup.js @@ -522,7 +522,7 @@ function onAccept() { // XXX Should we do this in nsPrintDialogServiceWin::ShowPageSetup (the code // that invokes us), since ShowPageSetup is where we do the saving for the // other platforms? - gPrintService.savePrintSettingsToPrefs(gPrintSettings, true, flags); + gPrintService.savePrintSettingsToPrefs(gPrintSettings, flags); } // --------------------------------------------------- diff --git a/toolkit/components/printing/content/printUtils.js b/toolkit/components/printing/content/printUtils.js index 40a566083cef..123d817a7c73 100644 --- a/toolkit/components/printing/content/printUtils.js +++ b/toolkit/components/printing/content/printUtils.js @@ -115,14 +115,9 @@ var PrintUtils = { ); if (!PSSVC.lastUsedPrinterName) { if (printSettings.printerName) { + PSSVC.saveLastUsedPrinterNameToPrefs(printSettings.printerName); PSSVC.savePrintSettingsToPrefs( printSettings, - false, - Ci.nsIPrintSettings.kInitSavePrinterName - ); - PSSVC.savePrintSettingsToPrefs( - printSettings, - true, Ci.nsIPrintSettings.kInitSaveAll ); } @@ -354,13 +349,12 @@ var PrintUtils = { } // Update the saved last used printer name and print settings: - Services.prefs.setStringPref("print_printer", settings.printerName); var PSSVC = Cc["@mozilla.org/gfx/printsettings-service;1"].getService( Ci.nsIPrintSettingsService ); + PSSVC.saveLastUsedPrinterNameToPrefs(settings.printerName); PSSVC.savePrintSettingsToPrefs( settings, - true, Ci.nsIPrintSettings.kInitSaveAll ); } diff --git a/widget/cocoa/nsPrintDialogX.mm b/widget/cocoa/nsPrintDialogX.mm index cfd9e03f9693..e00bf1ecd5f1 100644 --- a/widget/cocoa/nsPrintDialogX.mm +++ b/widget/cocoa/nsPrintDialogX.mm @@ -122,8 +122,7 @@ nsPrintDialogServiceX::ShowPrintDialog(mozIDOMWindowProxy* aParent, bool aHaveSe // Save settings unless saving is pref'd off if (Preferences::GetBool("print.save_print_settings", false)) { - printSettingsSvc->SavePrintSettingsToPrefs(settingsX, true, - nsIPrintSettings::kInitSaveNativeData); + printSettingsSvc->SavePrintSettingsToPrefs(settingsX, nsIPrintSettings::kInitSaveNativeData); } return NS_OK; @@ -167,7 +166,7 @@ nsPrintDialogServiceX::ShowPageSetupDialog(mozIDOMWindowProxy* aParent, uint32_t flags = nsIPrintSettings::kInitSaveNativeData | nsIPrintSettings::kInitSavePaperSize | nsIPrintSettings::kInitSaveOrientation | nsIPrintSettings::kInitSaveScaling; - printSettingsService->SavePrintSettingsToPrefs(aNSSettings, true, flags); + printSettingsService->SavePrintSettingsToPrefs(aNSSettings, flags); } return NS_OK; } diff --git a/widget/gtk/nsPrintDialogGTK.cpp b/widget/gtk/nsPrintDialogGTK.cpp index 695881c9a9b3..426019c06e8f 100644 --- a/widget/gtk/nsPrintDialogGTK.cpp +++ b/widget/gtk/nsPrintDialogGTK.cpp @@ -599,10 +599,9 @@ nsPrintDialogServiceGTK::ShowPageSetupDialog(mozIDOMWindowProxy* aParent, if (psService) psService->SavePrintSettingsToPrefs( - aNSSettings, true, - nsIPrintSettings::kInitSaveOrientation | - nsIPrintSettings::kInitSavePaperSize | - nsIPrintSettings::kInitSaveUnwriteableMargins); + aNSSettings, nsIPrintSettings::kInitSaveOrientation | + nsIPrintSettings::kInitSavePaperSize | + nsIPrintSettings::kInitSaveUnwriteableMargins); return NS_OK; } diff --git a/widget/nsIPrintSettingsService.idl b/widget/nsIPrintSettingsService.idl index 34b8065bd76b..602a4bc42c34 100644 --- a/widget/nsIPrintSettingsService.idl +++ b/widget/nsIPrintSettingsService.idl @@ -93,7 +93,6 @@ interface nsIPrintSettingsService : nsISupports * If a PrinterName is there, then it saves the items qualified for that Printer * * aPS - PrintSettings to have its settings saved - * aUsePrinterNamePrefix - indicates whether to use the printer name as a prefix * aFlags - indicates which prefs to save, see nsIPrintSettings.idl for the const values. * * Items not written: @@ -102,7 +101,12 @@ interface nsIPrintSettingsService : nsISupports * printSilent, shrinkToFit, numCopies * */ - void savePrintSettingsToPrefs(in nsIPrintSettings aPrintSettings, in boolean aUsePrinterNamePrefix, in unsigned long aFlags); + void savePrintSettingsToPrefs(in nsIPrintSettings aPrintSettings, in unsigned long aFlags); + + /** + * Saves the given printer name as the last used printer name. + */ + void saveLastUsedPrinterNameToPrefs(in AString aPrinterName); /** * Given some nsIPrintSettings, diff --git a/widget/nsPrintSettingsService.cpp b/widget/nsPrintSettingsService.cpp index 8b650366ee0b..26cab4a0f716 100644 --- a/widget/nsPrintSettingsService.cpp +++ b/widget/nsPrintSettingsService.cpp @@ -930,14 +930,16 @@ nsPrintSettingsService::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS, * Save all of the printer settings; if we can find a printer name, save * printer-specific preferences. Otherwise, save generic ones. */ -nsresult nsPrintSettingsService::SavePrintSettingsToPrefs( - nsIPrintSettings* aPS, bool aUsePrinterNamePrefix, uint32_t aFlags) { +nsresult nsPrintSettingsService::SavePrintSettingsToPrefs(nsIPrintSettings* aPS, + uint32_t aFlags) { NS_ENSURE_ARG_POINTER(aPS); MOZ_DIAGNOSTIC_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); + MOZ_ASSERT(!(aFlags & nsIPrintSettings::kInitSavePrinterName), + "Use SaveLastUsedPrintNameToPrefs"); // Get the printer name from the PrinterSettings for an optional prefix. nsAutoString prtName; - nsresult rv = GetAdjustedPrinterName(aPS, aUsePrinterNamePrefix, prtName); + nsresult rv = GetAdjustedPrinterName(aPS, true, prtName); NS_ENSURE_SUCCESS(rv, rv); #ifndef MOZ_WIDGET_ANDROID @@ -946,7 +948,7 @@ nsresult nsPrintSettingsService::SavePrintSettingsToPrefs( // without a good way for us to fix things for them (unprefixed prefs act as // defaults and can result in values being inappropriately propagated to // prefixed prefs). - if (prtName.IsEmpty() && aFlags != nsIPrintSettings::kInitSavePrinterName) { + if (prtName.IsEmpty()) { MOZ_DIAGNOSTIC_ASSERT(false, "Print settings must be saved with a prefix"); return NS_ERROR_FAILURE; } @@ -955,6 +957,16 @@ nsresult nsPrintSettingsService::SavePrintSettingsToPrefs( return WritePrefs(aPS, prtName, aFlags); } +nsresult nsPrintSettingsService::SaveLastUsedPrinterNameToPrefs( + const nsAString& aPrinterName) { + MOZ_DIAGNOSTIC_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); + + if (!aPrinterName.IsEmpty()) { + Preferences::SetString(kPrinterName, aPrinterName); + } + return NS_OK; +} + //----------------------------------------------------- //-- Protected Methods -------------------------------- //-----------------------------------------------------