From 1562e95bd844cec70a4a78c7ddaf5ad0c2e4566b Mon Sep 17 00:00:00 2001 From: "peterv%propagandism.org" Date: Mon, 12 Nov 2007 14:47:04 +0000 Subject: [PATCH] Fix for bug 390446 (Javascript is still disabled after leaving a page that had designMode on). r/sr=jst. git-svn-id: svn://10.0.0.236/trunk@239207 18797224-902f-48f8-a5cc-f745e15eee43 --- .../html/document/src/nsHTMLDocument.cpp | 35 +----- .../html/document/src/nsHTMLDocument.h | 2 - .../docshell/base/nsDocShellEditorData.cpp | 2 +- .../composer/public/nsIEditingSession.idl | 14 ++- .../editor/composer/src/nsEditingSession.cpp | 100 +++++++++++------- .../editor/composer/src/nsEditingSession.h | 2 + .../embedding/qa/testembed/nsIEditSession.cpp | 2 +- 7 files changed, 83 insertions(+), 74 deletions(-) diff --git a/mozilla/content/html/document/src/nsHTMLDocument.cpp b/mozilla/content/html/document/src/nsHTMLDocument.cpp index fc4bcf57535..d973bf1a66d 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/content/html/document/src/nsHTMLDocument.cpp @@ -3922,7 +3922,7 @@ nsHTMLDocument::TurnEditingOff() NS_ENSURE_SUCCESS(rv, rv); // turn editing off - rv = editSession->TearDownEditorOnWindow(window, PR_TRUE); + rv = editSession->TearDownEditorOnWindow(window); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr editor; @@ -3934,14 +3934,6 @@ nsHTMLDocument::TurnEditingOff() editorss->RemoveOverrideStyleSheet(NS_LITERAL_STRING("resource://gre/res/designmode.css")); } - if (mEditingState == eDesignMode) { - rv = docshell->SetAllowJavascript(mScriptsEnabled); - NS_ENSURE_SUCCESS(rv, rv); - - rv = docshell->SetAllowPlugins(mPluginsEnabled); - NS_ENSURE_SUCCESS(rv, rv); - } - mEditingState = eOff; return NS_OK; @@ -4038,22 +4030,8 @@ nsHTMLDocument::EditingStateChanged() // designMode is being turned on (overrides contentEditable). editorss->AddOverrideStyleSheet(NS_LITERAL_STRING("resource://gre/res/designmode.css")); - // Store scripting and plugins state. - PRBool tmp; - rv = docshell->GetAllowJavascript(&tmp); - NS_ENSURE_SUCCESS(rv, rv); - - mScriptsEnabled = tmp; - - rv = docshell->SetAllowJavascript(PR_FALSE); - NS_ENSURE_SUCCESS(rv, rv); - - rv = docshell->GetAllowPlugins(&tmp); - NS_ENSURE_SUCCESS(rv, rv); - - mPluginsEnabled = tmp; - - rv = docshell->SetAllowPlugins(PR_FALSE); + // Disable scripting and plugins. + rv = editSession->DisableJSAndPlugins(window); NS_ENSURE_SUCCESS(rv, rv); updateState = PR_TRUE; @@ -4063,10 +4041,7 @@ nsHTMLDocument::EditingStateChanged() // designMode is being turned off (contentEditable is still on). editorss->RemoveOverrideStyleSheet(NS_LITERAL_STRING("resource://gre/res/designmode.css")); - rv = docshell->SetAllowJavascript(mScriptsEnabled); - NS_ENSURE_SUCCESS(rv, rv); - - rv = docshell->SetAllowPlugins(mPluginsEnabled); + rv = editSession->RestoreJSAndPlugins(window); NS_ENSURE_SUCCESS(rv, rv); updateState = PR_TRUE; @@ -4089,7 +4064,7 @@ nsHTMLDocument::EditingStateChanged() if (NS_FAILED(rv)) { // Editor setup failed. Editing is not on after all. // XXX Should we reset the editable flag on nodes? - editSession->TearDownEditorOnWindow(window, PR_TRUE); + editSession->TearDownEditorOnWindow(window); mEditingState = eOff; return rv; diff --git a/mozilla/content/html/document/src/nsHTMLDocument.h b/mozilla/content/html/document/src/nsHTMLDocument.h index ff80c06fa3f..70f5dfb0545 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.h +++ b/mozilla/content/html/document/src/nsHTMLDocument.h @@ -378,8 +378,6 @@ protected: PRUint32 mContentEditableCount; EditingState mEditingState; - PRPackedBool mScriptsEnabled; - PRPackedBool mPluginsEnabled; nsresult DoClipboardSecurityCheck(PRBool aPaste); static jsval sCutCopyInternal_id; diff --git a/mozilla/docshell/base/nsDocShellEditorData.cpp b/mozilla/docshell/base/nsDocShellEditorData.cpp index 90c8b410369..a26d6752cad 100644 --- a/mozilla/docshell/base/nsDocShellEditorData.cpp +++ b/mozilla/docshell/base/nsDocShellEditorData.cpp @@ -73,7 +73,7 @@ nsDocShellEditorData::~nsDocShellEditorData() nsCOMPtr domWindow = do_GetInterface(mDocShell); // This will eventually call nsDocShellEditorData::SetEditor(nsnull) // which will call mEditorPreDestroy() and delete the editor - mEditingSession->TearDownEditorOnWindow(domWindow, PR_TRUE); + mEditingSession->TearDownEditorOnWindow(domWindow); } else if (mEditor) // Should never have this w/o nsEditingSession! { diff --git a/mozilla/editor/composer/public/nsIEditingSession.idl b/mozilla/editor/composer/public/nsIEditingSession.idl index 5714bbf0ccf..dd1a3181d31 100644 --- a/mozilla/editor/composer/public/nsIEditingSession.idl +++ b/mozilla/editor/composer/public/nsIEditingSession.idl @@ -43,7 +43,7 @@ interface nsIEditor; -[scriptable, uuid(aee80d50-2065-4411-834d-0cadfb649a19)] +[scriptable, uuid(274cd32e-3675-47e1-9d8a-fc6504ded9ce)] interface nsIEditingSession : nsISupports { @@ -101,10 +101,20 @@ interface nsIEditingSession : nsISupports /** * Destroy editor and related support objects */ - void tearDownEditorOnWindow(in nsIDOMWindow window, in boolean aStopEditing); + void tearDownEditorOnWindow(in nsIDOMWindow window); void setEditorOnControllers(in nsIDOMWindow aWindow, in nsIEditor aEditor); + /** + * Disable scripts and plugins in aWindow. + */ + void disableJSAndPlugins(in nsIDOMWindow aWindow); + + /** + * Restore JS and plugins (enable/disable them) according to the state they + * were before the last call to disableJSAndPlugins. + */ + void restoreJSAndPlugins(in nsIDOMWindow aWindow); }; diff --git a/mozilla/editor/composer/src/nsEditingSession.cpp b/mozilla/editor/composer/src/nsEditingSession.cpp index c0a78234a13..cb1e2b42d53 100644 --- a/mozilla/editor/composer/src/nsEditingSession.cpp +++ b/mozilla/editor/composer/src/nsEditingSession.cpp @@ -97,6 +97,7 @@ nsEditingSession::nsEditingSession() , mCanCreateEditor(PR_FALSE) , mInteractive(PR_FALSE) , mMakeWholeDocumentEditable(PR_TRUE) +, mDisabledJSAndPlugins(PR_FALSE) , mScriptsEnabled(PR_TRUE) , mPluginsEnabled(PR_TRUE) , mProgressListenerRegistered(PR_FALSE) @@ -156,28 +157,12 @@ nsEditingSession::MakeWindowEditable(nsIDOMWindow *aWindow, nsresult rv; if (!mInteractive) { - // Disable JavaScript in this document: - PRBool tmp; - rv = docShell->GetAllowJavascript(&tmp); - NS_ENSURE_SUCCESS(rv, rv); - - mScriptsEnabled = tmp; - - rv = docShell->SetAllowJavascript(PR_FALSE); - NS_ENSURE_SUCCESS(rv, rv); - - // Disable plugins in this document: - rv = docShell->GetAllowPlugins(&tmp); - NS_ENSURE_SUCCESS(rv, rv); - - mPluginsEnabled = tmp; - - rv = docShell->SetAllowPlugins(PR_FALSE); + rv = DisableJSAndPlugins(aWindow); NS_ENSURE_SUCCESS(rv, rv); } // Always remove existing editor - TearDownEditorOnWindow(aWindow, PR_FALSE); + TearDownEditorOnWindow(aWindow); // Tells embedder that startup is in progress mEditorStatus = eEditorCreationInProgress; @@ -226,11 +211,55 @@ nsEditingSession::MakeWindowEditable(nsIDOMWindow *aWindow, // Since this is used only when editing an existing page, // it IS ok to destroy current editor if (NS_FAILED(rv)) - TearDownEditorOnWindow(aWindow, PR_FALSE); + TearDownEditorOnWindow(aWindow); } return rv; } +NS_IMETHODIMP +nsEditingSession::DisableJSAndPlugins(nsIDOMWindow *aWindow) +{ + nsIDocShell *docShell = GetDocShellFromWindow(aWindow); + if (!docShell) return NS_ERROR_FAILURE; + + PRBool tmp; + nsresult rv = docShell->GetAllowJavascript(&tmp); + NS_ENSURE_SUCCESS(rv, rv); + + mScriptsEnabled = tmp; + + rv = docShell->SetAllowJavascript(PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); + + // Disable plugins in this document: + rv = docShell->GetAllowPlugins(&tmp); + NS_ENSURE_SUCCESS(rv, rv); + + mPluginsEnabled = tmp; + + rv = docShell->SetAllowPlugins(PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); + + mDisabledJSAndPlugins = PR_TRUE; + + return NS_OK; +} + +NS_IMETHODIMP +nsEditingSession::RestoreJSAndPlugins(nsIDOMWindow *aWindow) +{ + mDisabledJSAndPlugins = PR_FALSE; + + nsIDocShell *docShell = GetDocShellFromWindow(aWindow); + if (!docShell) return NS_ERROR_FAILURE; + + nsresult rv = docShell->SetAllowJavascript(mScriptsEnabled); + NS_ENSURE_SUCCESS(rv, rv); + + // Disable plugins in this document: + return docShell->SetAllowPlugins(mPluginsEnabled); +} + /*--------------------------------------------------------------------------- WindowIsEditable @@ -500,12 +529,10 @@ nsEditingSession::SetupEditorOnWindow(nsIDOMWindow *aWindow) TearDownEditorOnWindow - void tearDownEditorOnWindow (in nsIDOMWindow aWindow, - in boolean aStopEditing); + void tearDownEditorOnWindow (in nsIDOMWindow aWindow); ----------------------------------------------------------------------------*/ NS_IMETHODIMP -nsEditingSession::TearDownEditorOnWindow(nsIDOMWindow *aWindow, - PRBool aStopEditing) +nsEditingSession::TearDownEditorOnWindow(nsIDOMWindow *aWindow) { if (!mDoneSetup) return NS_OK; @@ -523,7 +550,12 @@ nsEditingSession::TearDownEditorOnWindow(nsIDOMWindow *aWindow, mDoneSetup = PR_FALSE; - if (aStopEditing) { + // Check if we're turning off editing (from contentEditable or designMode). + nsCOMPtr domDoc; + aWindow->GetDocument(getter_AddRefs(domDoc)); + nsCOMPtr htmlDoc = do_QueryInterface(domDoc); + PRBool stopEditing = htmlDoc && htmlDoc->IsEditingOn(); + if (stopEditing) { nsCOMPtr webProgress = do_GetInterface(docShell); if (webProgress) { webProgress->RemoveProgressListener(this); @@ -628,27 +660,19 @@ nsEditingSession::TearDownEditorOnWindow(nsIDOMWindow *aWindow, mHTMLCommandControllerId = 0; } - if (aStopEditing) { - if (!mInteractive) { + if (stopEditing) { + if (mDisabledJSAndPlugins) { // Make things the way they were before we started editing. - if (mScriptsEnabled) { - docShell->SetAllowJavascript(PR_TRUE); - } - - if (mPluginsEnabled) { - docShell->SetAllowPlugins(PR_TRUE); - } + RestoreJSAndPlugins(aWindow); + } + if (!mInteractive) { nsCOMPtr utils(do_GetInterface(aWindow)); if (utils) utils->SetImageAnimationMode(mImageAnimationMode); } if (mMakeWholeDocumentEditable) { - nsCOMPtr domDoc; - rv = aWindow->GetDocument(getter_AddRefs(domDoc)); - NS_ENSURE_SUCCESS(rv, rv); - nsCOMPtr doc = do_QueryInterface(domDoc, &rv); NS_ENSURE_SUCCESS(rv, rv); @@ -991,7 +1015,7 @@ nsEditingSession::StartDocumentLoad(nsIWebProgress *aWebProgress, aWebProgress->GetDOMWindow(getter_AddRefs(domWindow)); if (domWindow) { - TearDownEditorOnWindow(domWindow, PR_FALSE); + TearDownEditorOnWindow(domWindow); } if (aIsToBeMadeEditable) diff --git a/mozilla/editor/composer/src/nsEditingSession.h b/mozilla/editor/composer/src/nsEditingSession.h index 534ef145612..fa4e48c2a8c 100644 --- a/mozilla/editor/composer/src/nsEditingSession.h +++ b/mozilla/editor/composer/src/nsEditingSession.h @@ -132,6 +132,8 @@ protected: PRPackedBool mInteractive; PRPackedBool mMakeWholeDocumentEditable; + PRPackedBool mDisabledJSAndPlugins; + // True if scripts were enabled before the editor turned scripts // off, otherwise false. PRPackedBool mScriptsEnabled; diff --git a/mozilla/embedding/qa/testembed/nsIEditSession.cpp b/mozilla/embedding/qa/testembed/nsIEditSession.cpp index 0c4fdbfb548..a6745449585 100644 --- a/mozilla/embedding/qa/testembed/nsIEditSession.cpp +++ b/mozilla/embedding/qa/testembed/nsIEditSession.cpp @@ -172,7 +172,7 @@ void CnsIEditSession::TearEditorWinTest(PRInt16 displayMode) editingSession = GetEditSessionObject(); domWindow = GetTheDOMWindow(qaWebBrowser); if (editingSession) { - rv = editingSession->TearDownEditorOnWindow(domWindow, PR_FALSE); + rv = editingSession->TearDownEditorOnWindow(domWindow); RvTestResult(rv, "TearDownEditorOnWindow() test", displayMode); if (displayMode == 1) RvTestResultDlg(rv, "TearDownEditorOnWindow() test");