Bug 820170 - GetNativeForGlobal. r=bholley

This commit is contained in:
Gabor Krizsanits 2013-04-04 11:27:42 +02:00
parent 5af0153b14
commit fd0f98c248
2 changed files with 35 additions and 0 deletions

View File

@ -25,11 +25,13 @@
#include "mozilla/dom/DOMJSClass.h"
#include "nsMathUtils.h"
#include "nsStringBuffer.h"
#include "nsIGlobalObject.h"
#include "mozilla/dom/BindingDeclarations.h"
class nsIPrincipal;
class nsIXPConnectWrappedJS;
class nsScriptNameSpaceManager;
class nsIGlobalObject;
#ifndef BAD_TLS_INDEX
#define BAD_TLS_INDEX ((uint32_t) -1)
@ -400,6 +402,11 @@ ReportJSRuntimeExplicitTreeStats(const JS::RuntimeStats &rtStats,
bool
Throw(JSContext *cx, nsresult rv);
/**
* Every global should hold a native that implements the nsIGlobalObject interface.
*/
nsIGlobalObject *
GetNativeForGlobal(JSObject *global);
} // namespace xpc
nsCycleCollectionParticipant *

View File

@ -686,4 +686,32 @@ TransplantObjectWithWrapper(JSContext *cx,
return newSameCompartmentWrapper;
}
nsIGlobalObject *
GetNativeForGlobal(JSObject *obj)
{
MOZ_ASSERT(JS_IsGlobalObject(obj));
if (!EnsureCompartmentPrivate(obj)->scope)
return nullptr;
// Every global needs to hold a native as its private.
MOZ_ASSERT(GetObjectClass(obj)->flags & (JSCLASS_PRIVATE_IS_NSISUPPORTS |
JSCLASS_HAS_PRIVATE));
nsISupports *native =
static_cast<nsISupports *>(js::GetObjectPrivate(obj));
MOZ_ASSERT(native);
// In some cases (like for windows) it is a wrapped native,
// in other cases (sandboxes, backstage passes) it's just
// a direct pointer to the native. If it's a wrapped native
// let's unwrap it first.
if (nsCOMPtr<nsIXPConnectWrappedNative> wn = do_QueryInterface(native)) {
native = wn->Native();
}
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(native);
MOZ_ASSERT(global, "Native held by global needs to implement nsIGlobalObject!");
return global;
}
}