Bug 1399787 - Part 12. Delay dispatching FinalizePrint message until the last page was processed. draft
authorcku <cku@mozilla.com>
Mon, 06 Nov 2017 16:28:51 +0800
changeset 710611 85e65df8ddb48b66b9dff482c34aebdf4c51ea25
parent 710610 abb8a6394ed9e4abe140e07ae7893bc146c94e79
child 710612 77613816563a39f6a3f9b1ba6e4ee41d0d90172d
push id92855
push usercku@mozilla.com
push dateMon, 11 Dec 2017 02:44:26 +0000
bugs1399787
milestone59.0a1
Bug 1399787 - Part 12. Delay dispatching FinalizePrint message until the last page was processed. For the last page, here is the final three messages sent between the content process, RemotePrintJobChild, and the chrome process, RemotePrintJobParent, for printing: 1. The content process sends *ProcessPage* to the chrome process via SendProcessPrint to request the chrome process print the last page. 2. The content process sends *FinalizePrint* to the chrome process via SendFinalizePrint to notify the chrome that there are no more outstanding print requests, and that the chrome process can release interal resource now. 3. The content process receive PageProcessed message from the chrome process. This calling sequence is fine for sync style PrintTarget (even though the FinalizePrint message is sent out a bit ealy). Since a sync PrintTarget completes its print task right after receiving *ProcessPage* message in #1, sending FinalizePrint before getting PageProcessed response is harmless. But this message dispatching sequence does cause a problem for async style PrintTargetEMF. After getting a message sent in #2, PrintTargetEMF release all resources before getting a EMF conversion response from the PDFium process. So the last page can not be printed correctly. This patch reorder the #2 and #3 message, that is to send FinalizePrint after the content process received PageProcessed message of the last page. MozReview-Commit-ID: 9ZVSrFnuHBU
layout/printing/nsPagePrintTimer.cpp
--- a/layout/printing/nsPagePrintTimer.cpp
+++ b/layout/printing/nsPagePrintTimer.cpp
@@ -76,18 +76,21 @@ nsPagePrintTimer::Run()
   // inRange will be true if a page is actually printed
   bool inRange;
   bool donePrinting;
 
   // donePrinting will be true if it completed successfully or
   // if the printing was cancelled
   donePrinting = !mPrintEngine || mPrintEngine->PrintPage(mPrintObj, inRange);
   if (donePrinting) {
-    // now clean up print or print the next webshell
-    if (!mPrintEngine || mPrintEngine->DonePrintingPages(mPrintObj, NS_OK)) {
+
+    if (mWaitingForRemotePrint ||
+        // If we are not waiting for the remote printing, it is the time to
+        // end printing task by calling DonePrintingPages.
+        (!mPrintEngine || mPrintEngine->DonePrintingPages(mPrintObj, NS_OK))) {
       initNewTimer = false;
       mDone = true;
     }
   }
 
   // Note that the Stop() destroys this after the print job finishes
   // (The PrintEngine stops holding a reference when DonePrintingPages
   // returns true.)
@@ -176,16 +179,21 @@ nsPagePrintTimer::WaitForRemotePrint()
 
 void
 nsPagePrintTimer::RemotePrintFinished()
 {
   if (!mWaitingForRemotePrint) {
     return;
   }
 
+  // now clean up print or print the next webshell
+  if (mDone && mPrintEngine) {
+    mDone = mPrintEngine->DonePrintingPages(mPrintObj, NS_OK);
+  }
+
   mWaitingForRemotePrint->SetTarget(
     mDocument->EventTargetFor(mozilla::TaskCategory::Other));
   mozilla::Unused <<
     mWaitingForRemotePrint->InitWithCallback(this, 0, nsITimer::TYPE_ONE_SHOT);
 }
 
 nsresult
 nsPagePrintTimer::Start(nsPrintObject* aPO)