Bug 939166 - Stop going through nsIScriptGlobalObject in CallSetup. r=bz

This commit is contained in:
Bobby Holley 2013-11-20 08:48:00 -08:00
parent 688bba41bf
commit 86629121be

View File

@ -18,6 +18,7 @@
#include "nsIScriptSecurityManager.h"
#include "xpcprivate.h"
#include "WorkerPrivate.h"
#include "nsGlobalWindow.h"
namespace mozilla {
namespace dom {
@ -66,38 +67,26 @@ CallbackObject::CallSetup::CallSetup(JS::Handle<JSObject*> aCallback,
JSContext* cx = nullptr;
if (mIsMainThread) {
// Now get the nsIScriptGlobalObject for this callback.
nsIScriptContext* ctx = nullptr;
nsIScriptGlobalObject* sgo = nsJSUtils::GetStaticScriptGlobal(realCallback);
if (sgo) {
// Now get the global and JSContext for this callback.
nsGlobalWindow* win = xpc::WindowGlobalOrNull(realCallback);
if (win) {
// Make sure that if this is a window it's the current inner, since the
// nsIScriptContext and hence JSContext are associated with the outer
// window. Which means that if someone holds on to a function from a
// now-unloaded document we'd have the new document as the script entry
// point...
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(sgo);
if (win) {
MOZ_ASSERT(win->IsInnerWindow());
nsPIDOMWindow* outer = win->GetOuterWindow();
if (!outer || win != outer->GetCurrentInnerWindow()) {
// Just bail out from here
return;
}
MOZ_ASSERT(win->IsInnerWindow());
nsPIDOMWindow* outer = win->GetOuterWindow();
if (!outer || win != outer->GetCurrentInnerWindow()) {
// Just bail out from here
return;
}
// if not a window at all, just press on
ctx = sgo->GetContext();
if (ctx) {
// We don't check whether scripts are enabled on ctx, because
// CheckFunctionAccess will do that anyway... and because we ignore them
// being disabled if the callee is system.
cx = ctx->GetNativeContext();
}
}
if (!cx) {
// We didn't manage to hunt down a script global to work with. Just fall
// back on using the safe context.
cx = win->GetContext() ? win->GetContext()->GetNativeContext()
// This happens - Removing it causes
// test_bug293235.xul to go orange.
: nsContentUtils::GetSafeJSContext();
} else {
// No DOM Window. Use the SafeJSContext.
cx = nsContentUtils::GetSafeJSContext();
}