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
This commit is contained in:
Emilio Cobos Alvarez 2020-09-10 17:25:21 +00:00
parent 76563e652c
commit 22c7b2a254
2 changed files with 21 additions and 3 deletions

View File

@ -12,6 +12,7 @@
#include "mozilla/Preferences.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Telemetry.h"
#include "nsAnonymousTemporaryFile.h"
#include <wchar.h>
#include <windef.h>
@ -74,6 +75,10 @@ NS_IMPL_ISUPPORTS(nsDeviceContextSpecWin, nsIDeviceContextSpec)
nsDeviceContextSpecWin::~nsDeviceContextSpecWin() {
SetDevMode(nullptr);
if (mTempFile) {
mTempFile->Remove(/* recursive = */ false);
}
if (nsCOMPtr<nsIPrintSettingsWin> ps = do_QueryInterface(mPrintSettings)) {
ps->SetDeviceName(EmptyString());
ps->SetDriverName(EmptyString());
@ -287,9 +292,17 @@ already_AddRefed<PrintTarget> nsDeviceContextSpecWin::MakePrintTarget() {
width /= TWIPS_PER_POINT_FLOAT;
height /= TWIPS_PER_POINT_FLOAT;
nsCOMPtr<nsIFile> file = do_CreateInstance("@mozilla.org/file/local;1");
nsresult rv = file->InitWithPath(filename);
if (NS_FAILED(rv)) {
nsCOMPtr<nsIFile> 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;
}

View File

@ -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<nsIPrintSettings> 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<nsIFile> mTempFile;
#ifdef MOZ_ENABLE_SKIA_PDF
// This variable is independant of nsIPrintSettings::kOutputFormatPDF.