if (mPrt && mPrt->mPreparingForPrint) { mPrt->mDocWasToBeDestroyed = PR_TRUE; return NS_OK;
if (mPrintEngine) { if (mPrintEngine->CheckBeforeDestroy()) { return NS_OK; }
DocumentViewerImpl::Destroy(){  // Here is where we check to see if the docment was still being prepared   // for printing when it was asked to be destroy from someone externally  // This usually happens if the document is unloaded while the user is in the Print Dialog  //  // So we flip the bool to remember that the document is going away  // and we can clean up and abort later after returning from the Print Dialog  if (mPrt && mPrt->mPreparingForPrint) {    mPrt->mDocWasToBeDestroyed = PR_TRUE;    return NS_OK;  }  // Don't let the document get unloaded while we are printing  // this could happen if we hit the back button during printing  if (mDestroyRefCount != 0) {    --mDestroyRefCount;    return NS_OK;  }  // All callers are supposed to call destroy to break circular  // references.  If we do this stuff in the destructor, the  // destructor might never be called (especially if we're being  // used from JS.  if (mPrt) {    delete mPrt;    mPrt = nsnull;  }#ifdef NS_PRINT_PREVIEW  if (mPrtPreview) {    delete mPrtPreview;    mPrtPreview = nsnull;  }  // This is insruance  if (mOldPrtPreview) {    delete mOldPrtPreview;    mOldPrtPreview = nsnull;  }#endif  // Avoid leaking the old viewer.  if (mPreviousViewer) {    mPreviousViewer->Destroy();    mPreviousViewer = nsnull;  }  if (mDeviceContext) {    mDeviceContext->FlushFontCache();    mDeviceContext = nsnull;  }  if (mPresShell) {    // Break circular reference (or something)    mPresShell->EndObservingDocument();    nsCOMPtr<nsISelection> selection;    GetDocumentSelection(getter_AddRefs(selection));    nsCOMPtr<nsISelectionPrivate> selPrivate(do_QueryInterface(selection));    if (selPrivate && mSelectionListener)      selPrivate->RemoveSelectionListener(mSelectionListener);    mPresShell->Destroy();    mPresShell = nsnull;  }  return NS_OK;}