From c75678d212c78b64bba9da0b7dae95b7d7353964 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 20 May 2014 16:07:17 -0400 Subject: [PATCH] Bug 1000175. Make sure error events on window only fire if the active document has not changed from when the exception was thrown. r=smaug --- dom/base/nsJSEnvironment.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/dom/base/nsJSEnvironment.cpp b/dom/base/nsJSEnvironment.cpp index efb76cabc192..b993f2164ea8 100644 --- a/dom/base/nsJSEnvironment.cpp +++ b/dom/base/nsJSEnvironment.cpp @@ -386,10 +386,8 @@ AsyncErrorReporter::AsyncErrorReporter(JSRuntime* aRuntime, NS_LITERAL_CSTRING("content javascript"); mInnerWindowID = 0; - if (aWindow && aWindow->IsOuterWindow()) { - aWindow = aWindow->GetCurrentInnerWindow(); - } if (aWindow) { + MOZ_ASSERT(aWindow->IsInnerWindow()); mInnerWindowID = aWindow->WindowID(); } } @@ -427,8 +425,7 @@ AsyncErrorReporter::ReportError() class ScriptErrorEvent : public AsyncErrorReporter { public: - ScriptErrorEvent(nsIScriptGlobalObject* aScriptGlobal, - JSRuntime* aRuntime, + ScriptErrorEvent(JSRuntime* aRuntime, JSErrorReport* aErrorReport, const char* aFallbackMessage, nsIPrincipal* aScriptOriginPrincipal, @@ -440,20 +437,21 @@ public: : AsyncErrorReporter(aRuntime, aErrorReport, aFallbackMessage, nsContentUtils::IsSystemPrincipal(aGlobalPrincipal), aWindow) - , mScriptGlobal(aScriptGlobal) , mOriginPrincipal(aScriptOriginPrincipal) , mDispatchEvent(aDispatchEvent) , mError(aRuntime, aError) + , mWindow(aWindow) { + MOZ_ASSERT_IF(mWindow, mWindow->IsInnerWindow()); } NS_IMETHOD Run() { nsEventStatus status = nsEventStatus_eIgnore; - // First, notify the DOM that we have a script error. - if (mDispatchEvent) { - nsCOMPtr win(do_QueryInterface(mScriptGlobal)); - nsIDocShell* docShell = win ? win->GetDocShell() : nullptr; + // First, notify the DOM that we have a script error, but only if + // our window is still the current inner, if we're associated with a window. + if (mDispatchEvent && (!mWindow || mWindow->IsCurrentInnerWindow())) { + nsIDocShell* docShell = mWindow ? mWindow->GetDocShell() : nullptr; if (docShell && !JSREPORT_IS_WARNING(mFlags) && !sHandlingScriptError) { @@ -469,7 +467,7 @@ public: init.mFilename = mFileName; init.mBubbles = true; - nsCOMPtr sop(do_QueryInterface(win)); + nsCOMPtr sop(do_QueryInterface(mWindow)); NS_ENSURE_STATE(sop); nsIPrincipal* p = sop->GetPrincipal(); NS_ENSURE_STATE(p); @@ -495,11 +493,11 @@ public: } nsRefPtr event = - ErrorEvent::Constructor(static_cast(win.get()), + ErrorEvent::Constructor(static_cast(mWindow.get()), NS_LITERAL_STRING("error"), init); event->SetTrusted(true); - EventDispatcher::DispatchDOMEvent(win, nullptr, event, presContext, + EventDispatcher::DispatchDOMEvent(mWindow, nullptr, event, presContext, &status); } } @@ -512,10 +510,10 @@ public: } private: - nsCOMPtr mScriptGlobal; nsCOMPtr mOriginPrincipal; bool mDispatchEvent; JS::PersistentRootedValue mError; + nsCOMPtr mWindow; static bool sHandlingScriptError; }; @@ -573,13 +571,15 @@ NS_ScriptErrorReporter(JSContext *cx, if (globalObject) { nsCOMPtr win = do_QueryInterface(globalObject); + if (win) { + win = win->GetCurrentInnerWindow(); + } nsCOMPtr scriptPrincipal = do_QueryInterface(globalObject); NS_ASSERTION(scriptPrincipal, "Global objects must implement " "nsIScriptObjectPrincipal"); nsContentUtils::AddScriptRunner( - new ScriptErrorEvent(globalObject, - JS_GetRuntime(cx), + new ScriptErrorEvent(JS_GetRuntime(cx), report, message, nsJSPrincipals::get(report->originPrincipals),