2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2008-01-29 18:11:48 -08:00
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is not a generated file. It contains common utility functions
|
|
|
|
* invoked from the JavaScript code generated from IDL interfaces.
|
|
|
|
* The goal of the utility functions is to cut down on the size of
|
|
|
|
* the generated code itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2008-02-12 19:52:43 -08:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2011-07-20 12:18:54 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
|
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
|
|
|
nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
|
2011-04-07 14:25:32 -07:00
|
|
|
PRUint32* aLineno)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
JSScript* script = nullptr;
|
2012-04-16 12:30:00 -07:00
|
|
|
unsigned lineno = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-16 12:30:00 -07:00
|
|
|
if (!JS_DescribeScriptedCaller(aContext, &script, &lineno)) {
|
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-04-16 12:30:00 -07:00
|
|
|
*aFilename = ::JS_GetScriptFilename(aContext, script);
|
|
|
|
*aLineno = lineno;
|
|
|
|
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptGlobalObject *
|
|
|
|
nsJSUtils::GetStaticScriptGlobal(JSContext* aContext, JSObject* aObj)
|
|
|
|
{
|
|
|
|
JSClass* clazz;
|
|
|
|
JSObject* glob = aObj; // starting point for search
|
|
|
|
|
|
|
|
if (!glob)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 17:57:27 -07:00
|
|
|
glob = JS_GetGlobalForObject(aContext, glob);
|
|
|
|
NS_ABORT_IF_FALSE(glob, "Infallible returns null");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-03 16:54:57 -08:00
|
|
|
clazz = JS_GetClass(glob);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
// 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) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
!(clazz->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) ||
|
2012-02-05 12:07:23 -08:00
|
|
|
!(supports = (nsISupports*)::JS_GetPrivate(glob))) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-12-21 17:41:23 -08:00
|
|
|
// 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));
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(wrapper, nullptr);
|
2010-12-21 17:41:23 -08:00
|
|
|
sgo = do_QueryWrappedNative(wrapper);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We're returning a pointer to something that's about to be
|
|
|
|
// released, but that's ok here.
|
|
|
|
return sgo;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptContext *
|
|
|
|
nsJSUtils::GetStaticScriptContext(JSContext* aContext, JSObject* aObj)
|
|
|
|
{
|
|
|
|
nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aContext, aObj);
|
|
|
|
if (!nativeGlobal)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-23 10:13:29 -07:00
|
|
|
return nativeGlobal->GetScriptContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptGlobalObject *
|
|
|
|
nsJSUtils::GetDynamicScriptGlobal(JSContext* aContext)
|
|
|
|
{
|
|
|
|
nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext);
|
|
|
|
if (!scriptCX)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return scriptCX->GetGlobalObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptContext *
|
|
|
|
nsJSUtils::GetDynamicScriptContext(JSContext *aContext)
|
|
|
|
{
|
|
|
|
return GetScriptContextFromJSContext(aContext);
|
|
|
|
}
|
2010-12-20 08:21:58 -08:00
|
|
|
|
|
|
|
PRUint64
|
2011-08-24 13:44:35 -07:00
|
|
|
nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext)
|
2010-12-20 08:21:58 -08:00
|
|
|
{
|
|
|
|
if (!aContext)
|
|
|
|
return 0;
|
|
|
|
|
2011-08-24 13:44:35 -07:00
|
|
|
PRUint64 innerWindowID = 0;
|
2011-02-19 20:46:44 -08:00
|
|
|
|
|
|
|
JSObject *jsGlobal = JS_GetGlobalForScopeChain(aContext);
|
|
|
|
if (jsGlobal) {
|
|
|
|
nsIScriptGlobalObject *scriptGlobal = GetStaticScriptGlobal(aContext,
|
|
|
|
jsGlobal);
|
|
|
|
if (scriptGlobal) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(scriptGlobal);
|
|
|
|
if (win)
|
2011-08-24 13:44:35 -07:00
|
|
|
innerWindowID = win->WindowID();
|
2011-02-19 20:46:44 -08:00
|
|
|
}
|
|
|
|
}
|
2010-12-20 08:21:58 -08:00
|
|
|
|
2011-08-24 13:44:35 -07:00
|
|
|
return innerWindowID;
|
2010-12-20 08:21:58 -08:00
|
|
|
}
|
|
|
|
|