Bug 1399787 - Part 4. Take out the code we landed in bug 1370488. draft
authorcku <cku@mozilla.com>
Sun, 05 Nov 2017 00:53:05 +0800
changeset 710600 9432131ddf243b2644ab07fb076776dcee4486f8
parent 710599 0f4cbff19ae5cd34f8ab9a0fa8e1fa8b115a3f73
child 710601 f57b0d7977317c8338cdd874deb6539cec433f0e
push id92855
push usercku@mozilla.com
push dateMon, 11 Dec 2017 02:44:26 +0000
bugs1399787, 1370488
milestone59.0a1
Bug 1399787 - Part 4. Take out the code we landed in bug 1370488. 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
widget/windows/nsDeviceContextSpecWin.cpp
widget/windows/nsDeviceContextSpecWin.h
--- a/widget/windows/nsDeviceContextSpecWin.cpp
+++ b/widget/windows/nsDeviceContextSpecWin.cpp
@@ -32,17 +32,16 @@
 #include "mozilla/Services.h"
 #include "nsWindowsHelpers.h"
 
 #include "mozilla/gfx/Logging.h"
 
 #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"
 #endif
 
 static mozilla::LazyLogModule kWidgetPrintingLogMod("printing-widget");
 #define PR_PL(_p1)  MOZ_LOG(kWidgetPrintingLogMod, mozilla::LogLevel::Debug, _p1)
 
@@ -93,23 +92,16 @@ struct AutoFreeGlobalPrinters
     GlobalPrinters::GetInstance()->FreeGlobalPrinters();
   }
 };
 
 //----------------------------------------------------------------------------------
 nsDeviceContextSpecWin::nsDeviceContextSpecWin()
 {
   mDevMode       = nullptr;
-#ifdef MOZ_ENABLE_SKIA_PDF
-  mPrintViaSkPDF          = false;
-  mDC                     = NULL;
-  mPDFPageCount           = 0;
-  mPDFCurrentPageNum      = 0;
-  mPrintViaPDFInProgress  = false;
-#endif
 }
 
 
 //----------------------------------------------------------------------------------
 
 NS_IMPL_ISUPPORTS(nsDeviceContextSpecWin, nsIDeviceContextSpec)
 
 nsDeviceContextSpecWin::~nsDeviceContextSpecWin()
@@ -118,21 +110,16 @@ nsDeviceContextSpecWin::~nsDeviceContext
 
   nsCOMPtr<nsIPrintSettingsWin> psWin(do_QueryInterface(mPrintSettings));
   if (psWin) {
     psWin->SetDeviceName(EmptyString());
     psWin->SetDriverName(EmptyString());
     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();
 }
 
 //----------------------------------------------------------------------------------
 NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget,
                                            nsIPrintSettings* aPrintSettings,
                                            bool aIsPrintPreview)
@@ -235,47 +222,17 @@ already_AddRefed<PrintTarget> nsDeviceCo
       mPrintSettings->GetToFileName(filename);
 
       nsAutoCString printFile(NS_ConvertUTF16toUTF8(filename).get());
       auto skStream = MakeUnique<SkFILEWStream>(printFile.get());
       return PrintTargetSkPDF::CreateOrNull(Move(skStream), size);
     }
 
     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
 
   if (mOutputFormat == nsIPrintSettings::kOutputFormatPDF) {
     nsString filename;
     mPrintSettings->GetToFileName(filename);
 
@@ -348,155 +305,16 @@ nsDeviceContextSpecWin::GetPrintingScale
   }
 
   // The print settings will have the resolution stored from the real device.
   int32_t resolution;
   mPrintSettings->GetResolution(&resolution);
   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)
 {
   mDeviceName = aDeviceName;
 }
 
 //----------------------------------------------------------------------------------
 void nsDeviceContextSpecWin::SetDriverName(const nsAString& aDriverName)
--- a/widget/windows/nsDeviceContextSpecWin.h
+++ b/widget/windows/nsDeviceContextSpecWin.h
@@ -12,41 +12,29 @@
 #include "nsIPrintSettings.h"
 #include "nsISupportsPrimitives.h"
 #include <windows.h>
 #include "mozilla/Attributes.h"
 #include "mozilla/RefPtr.h"
 
 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();
 
   NS_DECL_ISUPPORTS
 
   virtual already_AddRefed<PrintTarget> MakePrintTarget() final;
   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; }
 
   NS_IMETHOD Init(nsIWidget* aWidget, nsIPrintSettings* aPS, bool aIsPrintPreview) override;
 
   float GetDPI() final;
 
   float GetPrintingScale() final;
@@ -75,29 +63,21 @@ protected:
   nsString mDriverName;
   nsString mDeviceName;
   LPDEVMODEW mDevMode;
 
   nsCOMPtr<nsIPrintSettings> mPrintSettings;
   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
 };
 
 
 //-------------------------------------------------------------------------
 // Printer Enumerator
 //-------------------------------------------------------------------------
 class nsPrinterEnumeratorWin final : public nsIPrinterEnumerator