From 8621b9339d9c04bc155ddb0f68c40da5baf520a3 Mon Sep 17 00:00:00 2001 From: "bryner%brianryner.com" Date: Fri, 26 Sep 2003 21:45:15 +0000 Subject: [PATCH] If we're in the middle of printing when window.close() is called, then defer closing the window until printing completes. This avoids tearing down the presentation while the print engine is still using it. Bug 172921, r=jkeiser, sr=dbaron. git-svn-id: svn://10.0.0.236/trunk@147348 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/src/nsDocumentViewer.cpp | 24 ++++++++++++++++++- mozilla/docshell/base/nsIContentViewer.idl | 8 +++++++ mozilla/dom/src/base/nsGlobalWindow.cpp | 14 +++++++++++ mozilla/layout/base/nsDocumentViewer.cpp | 24 ++++++++++++++++++- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/mozilla/content/base/src/nsDocumentViewer.cpp b/mozilla/content/base/src/nsDocumentViewer.cpp index fb33ddba053..1568996078a 100644 --- a/mozilla/content/base/src/nsDocumentViewer.cpp +++ b/mozilla/content/base/src/nsDocumentViewer.cpp @@ -422,6 +422,7 @@ protected: PRPackedBool mEnableRendering; PRPackedBool mStopped; PRPackedBool mLoaded; + PRPackedBool mDeferredWindowClose; PRInt16 mNumURLStarts; PRInt16 mDestroyRefCount; // a second "refcount" for the document viewer's "destroy" @@ -486,6 +487,7 @@ void DocumentViewerImpl::PrepareToStartLoad() mEnableRendering = PR_TRUE; mStopped = PR_FALSE; mLoaded = PR_FALSE; + mDeferredWindowClose = PR_FALSE; #ifdef NS_PRINTING mPrintIsPending = PR_FALSE; @@ -1585,6 +1587,21 @@ DocumentViewerImpl::GetEnableRendering(PRBool* aResult) return NS_OK; } +NS_IMETHODIMP +DocumentViewerImpl::RequestWindowClose(PRBool* aCanClose) +{ +#ifdef NS_PRINTING + if (mPrintIsPending || (mPrintEngine && mPrintEngine->GetIsPrinting())) { + *aCanClose = PR_FALSE; + mDeferredWindowClose = PR_TRUE; + } else +#endif + *aCanClose = PR_TRUE; + + return NS_OK; +} + + void DocumentViewerImpl::ForceRefresh() { @@ -3817,7 +3834,12 @@ DocumentViewerImpl::OnDonePrinting() } // We are done printing, now cleanup - if (mClosingWhilePrinting) { + if (mDeferredWindowClose) { + mDeferredWindowClose = PR_FALSE; + nsCOMPtr win = do_GetInterface(mContainer); + if (win) + win->Close(); + } else if (mClosingWhilePrinting) { if (mDocument) { mDocument->SetScriptGlobalObject(nsnull); mDocument = nsnull; diff --git a/mozilla/docshell/base/nsIContentViewer.idl b/mozilla/docshell/base/nsIContentViewer.idl index 6b2b00fcba3..6489b9cb8a8 100644 --- a/mozilla/docshell/base/nsIContentViewer.idl +++ b/mozilla/docshell/base/nsIContentViewer.idl @@ -59,4 +59,12 @@ interface nsIContentViewer : nsISupports attribute boolean enableRendering; attribute boolean sticky; + + /* + * This is called when the DOM window wants to be closed. Returns true + * if the window can close immediately. Otherwise, returns false and will + * close the DOM window as soon as practical. + */ + + boolean requestWindowClose(); }; diff --git a/mozilla/dom/src/base/nsGlobalWindow.cpp b/mozilla/dom/src/base/nsGlobalWindow.cpp index 88553505d8a..53bedde68e1 100644 --- a/mozilla/dom/src/base/nsGlobalWindow.cpp +++ b/mozilla/dom/src/base/nsGlobalWindow.cpp @@ -3344,6 +3344,20 @@ GlobalWindowImpl::Close() } } + // Ask the content viewer whether the toplevel window can close. + // If the content viewer returns false, it is responsible for calling + // Close() as soon as it is possible for the window to close. + // This allows us to not close the window while printing is happening. + + nsCOMPtr cv; + mDocShell->GetContentViewer(getter_AddRefs(cv)); + if (cv) { + PRBool canClose; + cv->RequestWindowClose(&canClose); + if (!canClose) + return NS_OK; + } + // Fire a DOM event notifying listeners that this window is about to // be closed. The tab UI code may choose to cancel the default // action for this event, if so, we won't actually close the window diff --git a/mozilla/layout/base/nsDocumentViewer.cpp b/mozilla/layout/base/nsDocumentViewer.cpp index fb33ddba053..1568996078a 100644 --- a/mozilla/layout/base/nsDocumentViewer.cpp +++ b/mozilla/layout/base/nsDocumentViewer.cpp @@ -422,6 +422,7 @@ protected: PRPackedBool mEnableRendering; PRPackedBool mStopped; PRPackedBool mLoaded; + PRPackedBool mDeferredWindowClose; PRInt16 mNumURLStarts; PRInt16 mDestroyRefCount; // a second "refcount" for the document viewer's "destroy" @@ -486,6 +487,7 @@ void DocumentViewerImpl::PrepareToStartLoad() mEnableRendering = PR_TRUE; mStopped = PR_FALSE; mLoaded = PR_FALSE; + mDeferredWindowClose = PR_FALSE; #ifdef NS_PRINTING mPrintIsPending = PR_FALSE; @@ -1585,6 +1587,21 @@ DocumentViewerImpl::GetEnableRendering(PRBool* aResult) return NS_OK; } +NS_IMETHODIMP +DocumentViewerImpl::RequestWindowClose(PRBool* aCanClose) +{ +#ifdef NS_PRINTING + if (mPrintIsPending || (mPrintEngine && mPrintEngine->GetIsPrinting())) { + *aCanClose = PR_FALSE; + mDeferredWindowClose = PR_TRUE; + } else +#endif + *aCanClose = PR_TRUE; + + return NS_OK; +} + + void DocumentViewerImpl::ForceRefresh() { @@ -3817,7 +3834,12 @@ DocumentViewerImpl::OnDonePrinting() } // We are done printing, now cleanup - if (mClosingWhilePrinting) { + if (mDeferredWindowClose) { + mDeferredWindowClose = PR_FALSE; + nsCOMPtr win = do_GetInterface(mContainer); + if (win) + win->Close(); + } else if (mClosingWhilePrinting) { if (mDocument) { mDocument->SetScriptGlobalObject(nsnull); mDocument = nsnull;