Bug 1399787 - Part 4. Take out the code we landed in bug 1370488. r=jwatt

To move EMF conversion job to a dedicated process, I will implement a new
PrintTarget subclass, named PrintTargetEMF, to coordinate tasks among the
content process, chrome process and PDFium process. All the code that we
change in nsDeviceContextSpecWin is no longer needed.

MozReview-Commit-ID: GgKZoB92WYE

--HG--
extra : rebase_source : 1687d72a8fe88223f17c954c23022236dfe029a0
This commit is contained in:
cku 2017-11-05 00:53:05 +08:00
Родитель 31ba0da513
Коммит f605599984
2 изменённых файлов: 3 добавлений и 205 удалений

Просмотреть файл

@ -37,7 +37,6 @@
#ifdef MOZ_ENABLE_SKIA_PDF
#include "mozilla/gfx/PrintTargetSkPDF.h"
#include "nsIUUIDGenerator.h"
#include "mozilla/widget/PDFViaEMFPrintHelper.h"
#include "nsDirectoryServiceDefs.h"
#include "nsPrintfCString.h"
#include "nsThreadUtils.h"
@ -98,13 +97,6 @@ struct AutoFreeGlobalPrinters
nsDeviceContextSpecWin::nsDeviceContextSpecWin()
{
mDevMode = nullptr;
#ifdef MOZ_ENABLE_SKIA_PDF
mPrintViaSkPDF = false;
mDC = NULL;
mPDFPageCount = 0;
mPDFCurrentPageNum = 0;
mPrintViaPDFInProgress = false;
#endif
}
@ -123,11 +115,6 @@ nsDeviceContextSpecWin::~nsDeviceContextSpecWin()
psWin->SetDevMode(nullptr);
}
#ifdef MOZ_ENABLE_SKIA_PDF
if (mPrintViaSkPDF ) {
CleanupPrintViaPDF();
}
#endif
// Free them, we won't need them for a while
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
}
@ -240,37 +227,7 @@ already_AddRefed<PrintTarget> nsDeviceContextSpecWin::MakePrintTarget()
}
if (mDevMode) {
// When printing to a printer via Skia PDF we open a temporary file that
// we draw the print output into as PDF output, then once we reach
// EndDcoument we'll convert that PDF file to EMF page by page to print
// each page. Here we create the temporary file and wrap it in a
// PrintTargetSkPDF that we return.
nsresult rv =
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mPDFTempFile));
NS_ENSURE_SUCCESS(rv, nullptr);
nsCOMPtr<nsIUUIDGenerator> uuidGenerator =
do_GetService("@mozilla.org/uuid-generator;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
nsID uuid;
rv = uuidGenerator->GenerateUUIDInPlace(&uuid);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
char uuidChars[NSID_LENGTH];
uuid.ToProvidedString(uuidChars);
nsAutoCString printFile("tmp-printing");
printFile.Append(nsPrintfCString("%s.pdf", uuidChars));
rv = mPDFTempFile->AppendNative(printFile);
NS_ENSURE_SUCCESS(rv, nullptr);
nsAutoCString filePath;
mPDFTempFile->GetNativePath(filePath);
auto skStream = MakeUnique<SkFILEWStream>(filePath.get());
return PrintTargetSkPDF::CreateOrNull(Move(skStream), size);
// TBD: Implement a new PrintTarget to serve EMF OOP.
}
}
#endif
@ -353,145 +310,6 @@ nsDeviceContextSpecWin::GetPrintingScale()
return float(resolution) / GetDPI();
}
#ifdef MOZ_ENABLE_SKIA_PDF
void
nsDeviceContextSpecWin::CleanupPrintViaPDF()
{
if (mPDFPrintHelper) {
mPDFPrintHelper->CloseDocument();
mPDFPrintHelper = nullptr;
mPDFPageCount = 0;
}
if (mPDFTempFile) {
mPDFTempFile->Remove(/* aRecursive */ false);
mPDFTempFile = nullptr;
}
if (mDC != NULL) {
if (mPrintViaPDFInProgress) {
::EndDoc(mDC);
mPrintViaPDFInProgress = false;
}
::DeleteDC(mDC);
mDC = NULL;
}
}
void
nsDeviceContextSpecWin::FinishPrintViaPDF()
{
MOZ_ASSERT(mDC != NULL);
MOZ_ASSERT(mPDFPrintHelper);
MOZ_ASSERT(mPDFTempFile);
MOZ_ASSERT(mPrintViaPDFInProgress);
bool isPrinted = false;
bool endPageSuccess = false;
if (::StartPage(mDC) > 0) {
isPrinted = mPDFPrintHelper->DrawPage(mDC, mPDFCurrentPageNum++,
::GetDeviceCaps(mDC, HORZRES),
::GetDeviceCaps(mDC, VERTRES));
if (::EndPage(mDC) > 0) {
endPageSuccess = true;
}
}
if (mPDFCurrentPageNum < mPDFPageCount && isPrinted && endPageSuccess) {
nsresult rv = NS_DispatchToCurrentThread(NewRunnableMethod(
"nsDeviceContextSpecWin::PrintPDFOnThread",
this,
&nsDeviceContextSpecWin::FinishPrintViaPDF));
if (NS_SUCCEEDED(rv)) {
return;
}
}
CleanupPrintViaPDF();
}
#endif
nsresult
nsDeviceContextSpecWin::BeginDocument(const nsAString& aTitle,
const nsAString& aPrintToFileName,
int32_t aStartPage,
int32_t aEndPage)
{
#ifdef MOZ_ENABLE_SKIA_PDF
if (mPrintViaSkPDF && (mOutputFormat != nsIPrintSettings::kOutputFormatPDF)) {
// Here we create mDC which we'll draw each page from our temporary PDF file
// to once we reach EndDocument. The only reason we create it here rather
// than in EndDocument is so that we don't need to store aTitle and
// aPrintToFileName as member data.
NS_WARNING_ASSERTION(!mDriverName.IsEmpty(), "No driver!");
mDC = ::CreateDCW(mDriverName.get(), mDeviceName.get(), nullptr, mDevMode);
if (mDC == NULL) {
gfxCriticalError(gfxCriticalError::DefaultOptions(false))
<< "Failed to create device context in GetSurfaceForPrinter";
return NS_ERROR_FAILURE;
}
const uint32_t DOC_TITLE_LENGTH = MAX_PATH - 1;
nsString title(aTitle);
nsString printToFileName(aPrintToFileName);
if (title.Length() > DOC_TITLE_LENGTH) {
title.SetLength(DOC_TITLE_LENGTH - 3);
title.AppendLiteral("...");
}
DOCINFOW di;
di.cbSize = sizeof(di);
di.lpszDocName = title.Length() > 0 ? title.get() : L"Mozilla Document";
di.lpszOutput = printToFileName.Length() > 0 ?
printToFileName.get() : nullptr;
di.lpszDatatype = nullptr;
di.fwType = 0;
if (::StartDocW(mDC, &di) <= 0) {
// Defer calling CleanupPrintViaPDF() in destructor because PDF temp file
// is not ready yet.
return NS_ERROR_FAILURE;
}
mPrintViaPDFInProgress = true;
}
#endif
return NS_OK;
}
nsresult
nsDeviceContextSpecWin::EndDocument()
{
nsresult rv = NS_OK;
#ifdef MOZ_ENABLE_SKIA_PDF
if (mPrintViaSkPDF &&
mOutputFormat != nsIPrintSettings::kOutputFormatPDF &&
mPrintViaPDFInProgress) {
mPDFPrintHelper = MakeUnique<PDFViaEMFPrintHelper>();
rv = mPDFPrintHelper->OpenDocument(mPDFTempFile);
NS_ENSURE_SUCCESS(rv, rv);
mPDFPageCount = mPDFPrintHelper->GetPageCount();
if (mPDFPageCount <= 0) {
CleanupPrintViaPDF();
return NS_ERROR_FAILURE;
}
mPDFCurrentPageNum = 0;
rv = NS_DispatchToCurrentThread(NewRunnableMethod(
"nsDeviceContextSpecWin::PrintPDFOnThread",
this,
&nsDeviceContextSpecWin::FinishPrintViaPDF));
if (NS_FAILED(rv)) {
CleanupPrintViaPDF();
NS_WARNING("Failed to dispatch to the current thread!");
}
}
#endif
return rv;
}
//----------------------------------------------------------------------------------
void nsDeviceContextSpecWin::SetDeviceName(const nsAString& aDeviceName)
{

Просмотреть файл

@ -17,20 +17,8 @@
class nsIWidget;
#ifdef MOZ_ENABLE_SKIA_PDF
namespace mozilla {
namespace widget {
class PDFViaEMFPrintHelper;
}
}
#endif
class nsDeviceContextSpecWin : public nsIDeviceContextSpec
{
#ifdef MOZ_ENABLE_SKIA_PDF
typedef mozilla::widget::PDFViaEMFPrintHelper PDFViaEMFPrintHelper;
#endif
public:
nsDeviceContextSpecWin();
@ -40,8 +28,8 @@ public:
NS_IMETHOD BeginDocument(const nsAString& aTitle,
const nsAString& aPrintToFileName,
int32_t aStartPage,
int32_t aEndPage) override;
NS_IMETHOD EndDocument() override;
int32_t aEndPage) override { return NS_OK; }
NS_IMETHOD EndDocument() override { return NS_OK; }
NS_IMETHOD BeginPage() override { return NS_OK; }
NS_IMETHOD EndPage() override { return NS_OK; }
@ -80,19 +68,11 @@ protected:
int16_t mOutputFormat = nsIPrintSettings::kOutputFormatNative;
#ifdef MOZ_ENABLE_SKIA_PDF
void FinishPrintViaPDF();
void CleanupPrintViaPDF();
// This variable is independant of nsIPrintSettings::kOutputFormatPDF.
// It controls both whether normal printing is done via PDF using Skia and
// whether print-to-PDF uses Skia.
bool mPrintViaSkPDF;
nsCOMPtr<nsIFile> mPDFTempFile;
HDC mDC;
bool mPrintViaPDFInProgress;
mozilla::UniquePtr<PDFViaEMFPrintHelper> mPDFPrintHelper;
int mPDFPageCount;
int mPDFCurrentPageNum;
#endif
};