Bug 939166 - Be more direct in GetStaticScriptGlobal. r=bz

This can all collapse because of the following facts:
* Ever since we introduced SandboxPrivate, we never actually use a Window
  as an SOP for a sandbox.
* nsGlobalWindow is actually the only thing that implements nsIScriptGlobalObject.
This commit is contained in:
Bobby Holley 2013-11-20 08:48:00 -08:00
parent cc8b137bde
commit 8b0fb2235d
3 changed files with 33 additions and 39 deletions

View File

@ -26,6 +26,7 @@
#include "nsJSPrincipals.h"
#include "xpcpublic.h"
#include "nsContentUtils.h"
#include "nsGlobalWindow.h"
bool
nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
@ -47,46 +48,9 @@ nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
nsIScriptGlobalObject *
nsJSUtils::GetStaticScriptGlobal(JSObject* aObj)
{
const JSClass* clazz;
JSObject* glob = aObj; // starting point for search
if (!glob)
if (!aObj)
return nullptr;
glob = js::GetGlobalForObjectCrossCompartment(glob);
NS_ABORT_IF_FALSE(glob, "Infallible returns null");
clazz = JS_GetClass(glob);
// Whenever we end up with globals that are JSCLASS_IS_DOMJSCLASS
// and have an nsISupports DOM object, we will need to modify this
// check here.
MOZ_ASSERT(!(clazz->flags & JSCLASS_IS_DOMJSCLASS));
nsISupports* supports;
if (!(clazz->flags & JSCLASS_HAS_PRIVATE) ||
!(clazz->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) ||
!(supports = (nsISupports*)::JS_GetPrivate(glob))) {
return nullptr;
}
// We might either have a window directly (e.g. if the global is a
// sandbox whose script object principal pointer is a window), or an
// XPCWrappedNative for a window. We could also have other
// sandbox-related script object principals, but we can't do much
// about those short of trying to walk the proto chain of |glob|
// looking for a window or something.
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(supports));
if (!sgo) {
nsCOMPtr<nsIXPConnectWrappedNative> wrapper(do_QueryInterface(supports));
if (!wrapper) {
return nullptr;
}
sgo = do_QueryWrappedNative(wrapper);
}
// We're returning a pointer to something that's about to be
// released, but that's ok here.
return sgo;
return xpc::WindowGlobalOrNull(aObj);
}
nsIScriptContext *

View File

@ -37,6 +37,7 @@
#include "mozilla/dom/GeneratedAtomList.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/WindowBinding.h"
#include "mozilla/Attributes.h"
#include "AccessCheck.h"
#include "nsGlobalWindow.h"
@ -566,6 +567,28 @@ GetJunkScopeGlobal()
return GetNativeForGlobal(junkScope);
}
nsGlobalWindow*
WindowGlobalOrNull(JSObject *aObj)
{
MOZ_ASSERT(aObj);
JSObject *glob = js::GetGlobalForObjectCrossCompartment(aObj);
MOZ_ASSERT(glob);
// This will always return null until we have Window on WebIDL bindings,
// at which point it will do the right thing.
if (!IS_WN_CLASS(js::GetObjectClass(glob))) {
nsGlobalWindow* win = nullptr;
UNWRAP_OBJECT(Window, nullptr, glob, win);
return win;
}
nsISupports* supports = XPCWrappedNative::Get(glob)->GetIdentityObject();
nsCOMPtr<nsPIDOMWindow> piWin = do_QueryInterface(supports);
if (!piWin)
return nullptr;
return static_cast<nsGlobalWindow*>(piWin.get());
}
}
static void

View File

@ -404,6 +404,13 @@ GetJunkScope();
nsIGlobalObject *
GetJunkScopeGlobal();
/**
* If |aObj| has a window for a global, returns the associated nsGlobalWindow.
* Otherwise, returns null.
*/
nsGlobalWindow*
WindowGlobalOrNull(JSObject *aObj);
// Error reporter used when there is no associated DOM window on to which to
// report errors and warnings.
void