From 64a72a1f7bd0d4d76d35c49d631806a10c4fbfbc Mon Sep 17 00:00:00 2001 From: Emilio Cobos Alvarez Date: Thu, 10 Sep 2020 17:25:21 +0000 Subject: [PATCH] Bug 1659470 - Handle printing with an empty file name in nsDeviceContextSpecWin. r=jwatt Ideally print preview and co. shouldn't need a PrintTarget at all, I'd think... Though that's a bigger refactoring. Differential Revision: https://phabricator.services.mozilla.com/D89452 --- widget/windows/nsDeviceContextSpecWin.cpp | 19 ++++++++++++++++--- widget/windows/nsDeviceContextSpecWin.h | 5 +++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/widget/windows/nsDeviceContextSpecWin.cpp b/widget/windows/nsDeviceContextSpecWin.cpp index fa020b66b8df..7e25eab02878 100644 --- a/widget/windows/nsDeviceContextSpecWin.cpp +++ b/widget/windows/nsDeviceContextSpecWin.cpp @@ -12,6 +12,7 @@ #include "mozilla/Preferences.h" #include "mozilla/RefPtr.h" #include "mozilla/Telemetry.h" +#include "nsAnonymousTemporaryFile.h" #include #include @@ -74,6 +75,10 @@ NS_IMPL_ISUPPORTS(nsDeviceContextSpecWin, nsIDeviceContextSpec) nsDeviceContextSpecWin::~nsDeviceContextSpecWin() { SetDevMode(nullptr); + if (mTempFile) { + mTempFile->Remove(/* recursive = */ false); + } + if (nsCOMPtr ps = do_QueryInterface(mPrintSettings)) { ps->SetDeviceName(EmptyString()); ps->SetDriverName(EmptyString()); @@ -287,9 +292,17 @@ already_AddRefed nsDeviceContextSpecWin::MakePrintTarget() { width /= TWIPS_PER_POINT_FLOAT; height /= TWIPS_PER_POINT_FLOAT; - nsCOMPtr file = do_CreateInstance("@mozilla.org/file/local;1"); - nsresult rv = file->InitWithPath(filename); - if (NS_FAILED(rv)) { + nsCOMPtr file; + nsresult rv; + if (!filename.IsEmpty()) { + file = do_CreateInstance("@mozilla.org/file/local;1"); + rv = file->InitWithPath(filename); + } else { + rv = NS_OpenAnonymousTemporaryNsIFile(getter_AddRefs(mTempFile)); + file = mTempFile; + } + + if (NS_WARN_IF(NS_FAILED(rv))) { return nullptr; } diff --git a/widget/windows/nsDeviceContextSpecWin.h b/widget/windows/nsDeviceContextSpecWin.h index 686ef73ccfc7..8363e52cbe09 100644 --- a/widget/windows/nsDeviceContextSpecWin.h +++ b/widget/windows/nsDeviceContextSpecWin.h @@ -14,6 +14,7 @@ #include "mozilla/Attributes.h" #include "mozilla/RefPtr.h" +class nsIFile; class nsIWidget; class nsDeviceContextSpecWin : public nsIDeviceContextSpec { @@ -71,6 +72,10 @@ class nsDeviceContextSpecWin : public nsIDeviceContextSpec { nsCOMPtr mPrintSettings; int16_t mOutputFormat = nsIPrintSettings::kOutputFormatNative; + // A temporary file to create an "anonymous" print target. See bug 1664253, + // this should ideally not be needed. + nsCOMPtr mTempFile; + #ifdef MOZ_ENABLE_SKIA_PDF // This variable is independant of nsIPrintSettings::kOutputFormatPDF.