Bug 1376693 - part2: Make nsPrintEngine::DoCommonPrint() stop initializing the instance when the owner stops using the instance r?dholbert draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 18 Jul 2017 19:03:36 +0900
changeset 611018 3898c2d70c71aeb33eecc79d52cf7c7f7199ddf6
parent 611017 3c90040c657babe02fd45c0b114400244b10e52e
child 611019 8fe2a527efd9e3c6176ad5d28544f30579cf5f81
push id69097
push usermasayuki@d-toybox.com
push dateWed, 19 Jul 2017 05:32:07 +0000
reviewersdholbert
bugs1376693
milestone56.0a1
Bug 1376693 - part2: Make nsPrintEngine::DoCommonPrint() stop initializing the instance when the owner stops using the instance r?dholbert nsPrintEngine::FinishPrintPreview() may be called when nsAutoScriptBlocker is destroyed in nsPrintEngine::DoCommonPrint(). That means that the owner stopped print preview with the instance. In this case, nsPrintEngine::DoCommonPrint() doesn't need to keep initializing the instance anymore. MozReview-Commit-ID: DRQfmyW9FEL
layout/printing/nsPrintEngine.cpp
--- a/layout/printing/nsPrintEngine.cpp
+++ b/layout/printing/nsPrintEngine.cpp
@@ -531,16 +531,24 @@ nsPrintEngine::DoCommonPrint(bool       
     printData->mPrintObject->mFrameType =
       printData->mIsParentAFrameSet ? eFrameSet : eDoc;
 
     // Build the "tree" of PrintObjects
     BuildDocTree(printData->mPrintObject->mDocShell, &printData->mPrintDocList,
                  printData->mPrintObject);
   }
 
+  // The nsAutoScriptBlocker above will now have been destroyed, which may
+  // cause our print/print-preview operation to finish. In this case, we
+  // should immediately return an error code so that the root caller knows
+  // it shouldn't continue to do anything with this instance.
+  if (mIsDestroying || (aIsPrintPreview && !GetIsCreatingPrintPreview())) {
+    return NS_ERROR_FAILURE;
+  }
+
   if (!aIsPrintPreview) {
     SetIsPrinting(true);
   }
 
   // XXX This isn't really correct...
   if (!printData->mPrintObject->mDocument ||
       !printData->mPrintObject->mDocument->GetRootElement())
     return NS_ERROR_GFX_PRINTER_STARTDOC;
@@ -3597,16 +3605,23 @@ nsPrintEngine::FinishPrintPreview()
 
   if (!mPrt) {
     /* we're already finished with print preview */
     return rv;
   }
 
   rv = DocumentReadyForPrinting();
 
+  // Note that this method may be called while the instance is being
+  // initialized.  Some methods which initialize the instance (e.g.,
+  // DoCommonPrint) may need to stop initializing and return error if
+  // this is called.  Therefore it's important to set IsCreatingPrintPreview
+  // state to false here.  If you need to remove this call of
+  // SetIsCreatingPrintPreview here, you need to keep them being able to
+  // check whether the owner stopped using this instance.
   SetIsCreatingPrintPreview(false);
 
   // mPrt may be cleared during a call of nsPrintData::OnEndPrinting()
   // because that method invokes some arbitrary listeners.
   RefPtr<nsPrintData> printData = mPrt;
   if (NS_FAILED(rv)) {
     /* cleanup done, let's fire-up an error dialog to notify the user
      * what went wrong...