From ba008990ed87ac121f85eb6a523830f9d71356d9 Mon Sep 17 00:00:00 2001 From: Olli Pettay Date: Tue, 10 Mar 2009 21:30:50 +0200 Subject: [PATCH] Bug 424377, r+sr=roc --- content/base/src/nsGkAtomList.h | 2 +- layout/base/nsPresShell.cpp | 2 +- layout/printing/nsPrintEngine.cpp | 72 +++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/content/base/src/nsGkAtomList.h b/content/base/src/nsGkAtomList.h index 7c507049944..027c1e1a76d 100644 --- a/content/base/src/nsGkAtomList.h +++ b/content/base/src/nsGkAtomList.h @@ -798,7 +798,7 @@ GK_ATOM(screen, "screen") GK_ATOM(screenX, "screenX") GK_ATOM(screenY, "screenY") GK_ATOM(script, "script") -GK_ATOM(scriptEnabledBeforePrintPreview, "scriptEnabledBeforePrintPreview") +GK_ATOM(scriptEnabledBeforePrintOrPreview, "scriptEnabledBeforePrintOrPreview") GK_ATOM(scrollbar, "scrollbar") GK_ATOM(scrollbarbutton, "scrollbarbutton") GK_ATOM(scrollbox, "scrollbox") diff --git a/layout/base/nsPresShell.cpp b/layout/base/nsPresShell.cpp index 1676df17254..1ced56b1b32 100644 --- a/layout/base/nsPresShell.cpp +++ b/layout/base/nsPresShell.cpp @@ -2057,7 +2057,7 @@ PresShell::SetPrefNoScriptRule() ((mPresContext->Type() == nsPresContext::eContext_PrintPreview || mPresContext->Type() == nsPresContext::eContext_Print) && NS_PTR_TO_INT32(mDocument->GetProperty( - nsGkAtoms::scriptEnabledBeforePrintPreview))); + nsGkAtoms::scriptEnabledBeforePrintOrPreview))); if (scriptEnabled) { if (!mPrefStyleSheet) { diff --git a/layout/printing/nsPrintEngine.cpp b/layout/printing/nsPrintEngine.cpp index 227e96180aa..8e20627d5ba 100644 --- a/layout/printing/nsPrintEngine.cpp +++ b/layout/printing/nsPrintEngine.cpp @@ -214,6 +214,35 @@ static void DumpPrintObjectsTreeLayout(nsPrintObject * aPO,nsIDeviceContext * aD #define DUMP_DOC_TREELAYOUT #endif +class nsScriptSuppressor +{ +public: + nsScriptSuppressor(nsPrintEngine* aPrintEngine) + : mPrintEngine(aPrintEngine), mSuppressed(PR_FALSE) {} + + ~nsScriptSuppressor() { Unsuppress(); } + + void Suppress() + { + if (mPrintEngine) { + mSuppressed = PR_TRUE; + mPrintEngine->TurnScriptingOn(PR_FALSE); + } + } + + void Unsuppress() + { + if (mPrintEngine && mSuppressed) { + mPrintEngine->TurnScriptingOn(PR_TRUE); + } + mSuppressed = PR_FALSE; + } + + void Disconnect() { mPrintEngine = nsnull; } +protected: + nsRefPtr mPrintEngine; + PRBool mSuppressed; +}; // Class IDs static NS_DEFINE_CID(kViewManagerCID, NS_VIEW_MANAGER_CID); @@ -554,11 +583,13 @@ nsPrintEngine::DoCommonPrint(PRBool aIsPrintPreview, (do_CreateInstance("@mozilla.org/gfx/devicecontextspec;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); + nsScriptSuppressor scriptSuppressor(this); if (!aIsPrintPreview) { #ifdef NS_DEBUG mPrt->mDebugFilePtr = mDebugFile; #endif + scriptSuppressor.Suppress(); PRBool printSilently; mPrt->mPrintSettings->GetPrintSilent(&printSilently); @@ -710,6 +741,9 @@ nsPrintEngine::DoCommonPrint(PRBool aIsPrintPreview, } } + // We will enable scripting later after printing has finished. + scriptSuppressor.Disconnect(); + return NS_OK; } @@ -2742,6 +2776,7 @@ nsPrintEngine::DonePrintingPages(nsPrintObject* aPO, nsresult aResult) FirePrintCompletionEvent(); } + TurnScriptingOn(PR_TRUE); SetIsPrinting(PR_FALSE); // Release reference to mPagePrintTimer; the timer object destroys itself @@ -3022,6 +3057,13 @@ nsPrintEngine::FindSmallestSTF() void nsPrintEngine::TurnScriptingOn(PRBool aDoTurnOn) { + if (mIsDoingPrinting && aDoTurnOn && mDocViewerPrint && + mDocViewerPrint->GetIsPrintPreview()) { + // We don't want to turn scripting on if print preview is shown still after + // printing. + return; + } + nsPrintData* prt = mPrt; #ifdef NS_PRINT_PREVIEW if (!prt) { @@ -3040,30 +3082,44 @@ nsPrintEngine::TurnScriptingOn(PRBool aDoTurnOn) NS_ASSERTION(po, "nsPrintObject can't be null!"); nsIDocument* doc = po->mDocument; - + if (!doc) { + continue; + } + // get the script global object nsIScriptGlobalObject *scriptGlobalObj = doc->GetScriptGlobalObject(); if (scriptGlobalObj) { + nsCOMPtr window = do_QueryInterface(scriptGlobalObj); + NS_ASSERTION(window, "Can't get nsPIDOMWindow"); nsIScriptContext *scx = scriptGlobalObj->GetContext(); - NS_ASSERTION(scx, "Can't get nsIScriptContext"); + NS_WARN_IF_FALSE(scx, "Can't get nsIScriptContext"); + nsresult propThere = NS_PROPTABLE_PROP_NOT_THERE; + doc->GetProperty(nsGkAtoms::scriptEnabledBeforePrintOrPreview, + &propThere); if (aDoTurnOn) { - doc->DeleteProperty(nsGkAtoms::scriptEnabledBeforePrintPreview); + if (propThere != NS_PROPTABLE_PROP_NOT_THERE) { + doc->DeleteProperty(nsGkAtoms::scriptEnabledBeforePrintOrPreview); + if (scx) { + scx->SetScriptsEnabled(PR_TRUE, PR_FALSE); + } + window->ResumeTimeouts(PR_FALSE); + } } else { // Have to be careful, because people call us over and over again with // aDoTurnOn == PR_FALSE. So don't set the property if it's already // set, since in that case we'd set it to the wrong value. - nsresult propThere; - doc->GetProperty(nsGkAtoms::scriptEnabledBeforePrintPreview, - &propThere); if (propThere == NS_PROPTABLE_PROP_NOT_THERE) { // Stash the current value of IsScriptEnabled on the document, so // that layout code running in print preview doesn't get confused. - doc->SetProperty(nsGkAtoms::scriptEnabledBeforePrintPreview, + doc->SetProperty(nsGkAtoms::scriptEnabledBeforePrintOrPreview, NS_INT32_TO_PTR(doc->IsScriptEnabled())); + if (scx) { + scx->SetScriptsEnabled(PR_FALSE, PR_FALSE); + } + window->SuspendTimeouts(1, PR_FALSE); } } - scx->SetScriptsEnabled(aDoTurnOn, PR_TRUE); } } }