Bug 1401069 - Part 1. Check the value of mPDFDoc and early return if it's invalid in both RenderPageToDC and DrawPageToFile. draft
authorcku <cku@mozilla.com>
Mon, 18 Sep 2017 23:59:29 +0800
changeset 667471 fa8b36fa1ebbc136c4cf490d80d297c12cf152c2
parent 667303 a0eb21bf55e1c1ae0ba311e6f2273da05c712799
child 667472 2d8ccf84a002cb0d74ac1a9465adb75d7cc5db18
push id80721
push usercku@mozilla.com
push dateWed, 20 Sep 2017 04:22:29 +0000
bugs1401069
milestone57.0a1
Bug 1401069 - Part 1. Check the value of mPDFDoc and early return if it's invalid in both RenderPageToDC and DrawPageToFile. MozReview-Commit-ID: LQ9LTL8ZRKJ
widget/windows/PDFViaEMFPrintHelper.cpp
--- a/widget/windows/PDFViaEMFPrintHelper.cpp
+++ b/widget/windows/PDFViaEMFPrintHelper.cpp
@@ -110,27 +110,35 @@ PDFViaEMFPrintHelper::RenderPageToDC(HDC
 
   return true;
 }
 
 bool
 PDFViaEMFPrintHelper::DrawPage(HDC aPrinterDC, unsigned int aPageIndex,
                                int aPageWidth, int aPageHeight)
 {
+  MOZ_ASSERT(aPrinterDC);
+
+  // OpenDocument might fail.
+  if (!mPDFDoc) {
+    MOZ_ASSERT_UNREACHABLE("Make sure OpenDocument return true before"
+                           "using DrawPage.");
+    return false;
+  }
+
   // There is a comment in Chromium.
   // https://cs.chromium.org/chromium/src/pdf/pdfium/pdfium_engine.cc?rcl=9ad9f6860b4d6a4ec7f7f975b2c99672e02d5d49&l=4008
   // Some PDFs seems to render very slowly if RenderPageToDC is directly used
   // on a printer DC.
   // The way Chromium works around the issue at the code linked above is to
   // print to a bitmap and send that to a printer.  Instead of doing that we
   // render to an EMF file and replay that on the printer DC.  It is unclear
   // whether our approach will avoid the performance issues though.  Bug
   // 1359298 covers investigating that.
 
-  MOZ_ASSERT(aPrinterDC);
   WindowsEMF emf;
   bool result = emf.InitForDrawing();
   NS_ENSURE_TRUE(result, false);
 
   result = RenderPageToDC(emf.GetDC(), aPageIndex, aPageWidth, aPageHeight);
   NS_ENSURE_TRUE(result, false);
 
   RECT printRect = {0, 0, aPageWidth, aPageHeight};
@@ -138,16 +146,23 @@ PDFViaEMFPrintHelper::DrawPage(HDC aPrin
   return result;
 }
 
 bool
 PDFViaEMFPrintHelper::DrawPageToFile(const wchar_t* aFilePath,
                                      unsigned int aPageIndex,
                                      int aPageWidth, int aPageHeight)
 {
+  // OpenDocument might fail.
+  if (!mPDFDoc) {
+    MOZ_ASSERT_UNREACHABLE("Make sure OpenDocument return true before"
+                           "using DrawPageToFile.");
+    return false;
+  }
+
   WindowsEMF emf;
   bool result = emf.InitForDrawing(aFilePath);
   NS_ENSURE_TRUE(result, false);
 
   result = RenderPageToDC(emf.GetDC(), aPageIndex, aPageWidth, aPageHeight);
   NS_ENSURE_TRUE(result, false);
   return emf.SaveToFile();
 }