Bug 1652344 p2. Stop creating an nsIPrintSettings in nsGlobalWindowOuter::PrintOuter. r=bobowen

The fallback code in nsPrintJob::DoCommonPrint to create an nsIPrintSettings if
none is passed in is never used, since all callers pass a settings object.
However, to simplify future changes I'd like nsGlobalWindowOuter::PrintOuter to
stop creating and passing in its own default valued nsIPrintSettings object.

This patch makes the fallback code that DoCommonPrint calls do what
nsGlobalWindowOuter::PrintOuter does, and makes the latter stop passing in a
settings object.

This patch also removes nsIWebBrowserPrint.globalPrintSettings since
nsGlobalWindowOuter::PrintOuter was its only consumer.

Differential Revision: https://phabricator.services.mozilla.com/D83268
This commit is contained in:
Jonathan Watt 2020-07-12 19:33:49 +00:00
parent b850705cc7
commit 44ed5abb22
5 changed files with 51 additions and 60 deletions

View File

@ -5192,36 +5192,8 @@ void nsGlobalWindowOuter::PrintOuter(ErrorResult& aError) {
do_GetService("@mozilla.org/gfx/printsettings-service;1");
if (printSettingsService) {
nsCOMPtr<nsIPrintSettings> printSettings;
printSettingsService->GetGlobalPrintSettings(
getter_AddRefs(printSettings));
nsAutoString printerName;
printSettings->GetPrinterName(printerName);
bool shouldGetLastUsedPrinterName = printerName.IsEmpty();
# ifdef MOZ_X11
// In Linux, GTK backend does not support per printer settings.
// Calling GetLastUsedPrinterName causes a sandbox violation (see Bug
// 1329216). The printer name is not needed anywhere else on Linux
// before it gets to the parent. In the parent, we will then query the
// last-used printer name if no name is set. Unless we are in the parent,
// we will skip this part.
if (!XRE_IsParentProcess()) {
shouldGetLastUsedPrinterName = false;
}
# endif
if (shouldGetLastUsedPrinterName) {
printSettingsService->GetLastUsedPrinterName(printerName);
printSettings->SetPrinterName(printerName);
}
printSettingsService->InitPrintSettingsFromPrinter(printerName,
printSettings);
printSettingsService->InitPrintSettingsFromPrefs(
printSettings, true, nsIPrintSettings::kInitSaveAll);
EnterModalState();
webBrowserPrint->Print(printSettings, nullptr);
webBrowserPrint->Print(nullptr, nullptr);
LeaveModalState();
}
}

View File

@ -3330,12 +3330,6 @@ nsDocumentViewer::PrintPreviewScrollToPage(int16_t aType, int32_t aPageNum) {
return NS_OK;
}
NS_IMETHODIMP
nsDocumentViewer::GetGlobalPrintSettings(
nsIPrintSettings** aGlobalPrintSettings) {
return nsPrintJob::GetGlobalPrintSettings(aGlobalPrintSettings);
}
// XXX This always returns false for subdocuments
NS_IMETHODIMP
nsDocumentViewer::GetDoingPrint(bool* aDoingPrint) {

View File

@ -401,6 +401,51 @@ static nsresult EnsureSettingsHasPrinterNameSet(
#endif
}
static nsresult GetGlobalPrintSettings(
nsIPrintSettings** aGlobalPrintSettings) {
*aGlobalPrintSettings = nullptr;
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsIPrintSettingsService> printSettingsService =
do_GetService(sPrintSettingsServiceContractID, &rv);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIPrintSettings> settings;
rv = printSettingsService->GetGlobalPrintSettings(getter_AddRefs(settings));
if (NS_FAILED(rv)) {
return rv;
}
nsAutoString printerName;
settings->GetPrinterName(printerName);
bool shouldGetLastUsedPrinterName = printerName.IsEmpty();
#ifdef MOZ_X11
// In Linux, GTK backend does not support per printer settings.
// Calling GetLastUsedPrinterName causes a sandbox violation (see Bug
// 1329216). The printer name is not needed anywhere else on Linux
// before it gets to the parent. In the parent, we will then query the
// last-used printer name if no name is set. Unless we are in the parent,
// we will skip this part.
if (!XRE_IsParentProcess()) {
shouldGetLastUsedPrinterName = false;
}
#endif
if (shouldGetLastUsedPrinterName) {
printSettingsService->GetLastUsedPrinterName(printerName);
settings->SetPrinterName(printerName);
}
printSettingsService->InitPrintSettingsFromPrinter(printerName, settings);
printSettingsService->InitPrintSettingsFromPrefs(
settings, true, nsIPrintSettings::kInitSaveAll);
*aGlobalPrintSettings = settings.forget().take();
return NS_OK;
}
//-------------------------------------------------------
NS_IMPL_ISUPPORTS(nsPrintJob, nsIWebProgressListener, nsISupportsWeakReference,
@ -907,6 +952,11 @@ nsresult nsPrintJob::Print(Document* aSourceDoc,
nsresult rv = CommonPrint(false, aPrintSettings, aWebProgressListener, doc);
if (!aPrintSettings) {
// This is temporary until after bug 1602410 lands.
return rv;
}
// Save the print settings if the user picked them.
// We should probably do this immediately after the user confirms their
// selection (that is, move this to nsPrintingPromptService::ShowPrintDialog,
@ -990,20 +1040,6 @@ int32_t nsPrintJob::GetPrintPreviewNumPages() {
return numPages;
}
//----------------------------------------------------------------------------------
nsresult nsPrintJob::GetGlobalPrintSettings(
nsIPrintSettings** aGlobalPrintSettings) {
NS_ENSURE_ARG_POINTER(aGlobalPrintSettings);
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsIPrintSettingsService> printSettingsService =
do_GetService(sPrintSettingsServiceContractID, &rv);
if (NS_SUCCEEDED(rv)) {
rv = printSettingsService->GetGlobalPrintSettings(aGlobalPrintSettings);
}
return rv;
}
//----------------------------------------------------------------------------------
already_AddRefed<nsIPrintSettings> nsPrintJob::GetCurrentPrintSettings() {
if (mPrt) {

View File

@ -46,7 +46,6 @@ class nsPrintJob final : public nsIObserver,
public nsIWebProgressListener,
public nsSupportsWeakReference {
public:
static nsresult GetGlobalPrintSettings(nsIPrintSettings** aPrintSettings);
static void CloseProgressDialog(nsIWebProgressListener* aWebProgressListener);
nsPrintJob() = default;

View File

@ -26,16 +26,6 @@ interface nsIWebBrowserPrint : nsISupports
const short PRINTPREVIEW_HOME = 3;
const short PRINTPREVIEW_END = 4;
/**
* Returns a "global" PrintSettings object
* Creates a new the first time, if one doesn't exist.
*
* Then returns the same object each time after that.
*
* Initializes the globalPrintSettings from the default printer
*/
readonly attribute nsIPrintSettings globalPrintSettings;
/**
* Returns a pointer to the PrintSettings object that
* that was passed into either "print" or "print preview"