Fix for bug 461563 (Allow WrapNative to return a jsval without the wrapper). r/sr=jst.

This commit is contained in:
Peter Van der Beken 2008-11-27 18:47:58 +01:00
Родитель 7707e54e37
Коммит 030aea3127
8 изменённых файлов: 160 добавлений и 164 удалений

Просмотреть файл

@ -1640,9 +1640,9 @@ nsDOMClassInfo::WrapNative(JSContext *cx, JSObject *scope, nsISupports *native,
const nsIID& aIID, jsval *vp,
nsIXPConnectJSObjectHolder **aHolder)
{
*aHolder = nsnull;
if (!native) {
NS_ASSERTION(!aHolder || !*aHolder, "*aHolder should be null!");
*vp = JSVAL_NULL;
return NS_OK;
@ -1650,19 +1650,8 @@ nsDOMClassInfo::WrapNative(JSContext *cx, JSObject *scope, nsISupports *native,
NS_ENSURE_TRUE(sXPConnect, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
nsresult rv = sXPConnect->WrapNative(cx, ::JS_GetGlobalForObject(cx, scope),
native, aIID, getter_AddRefs(holder));
NS_ENSURE_SUCCESS(rv, rv);
JSObject* obj = nsnull;
rv = holder->GetJSObject(&obj);
NS_ENSURE_SUCCESS(rv, rv);
*vp = OBJECT_TO_JSVAL(obj);
holder.swap(*aHolder);
return rv;
return sXPConnect->WrapNativeToJSVal(cx, ::JS_GetGlobalForObject(cx, scope),
native, aIID, vp, aHolder);
}
// static
@ -7730,9 +7719,7 @@ nsArraySH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
NS_ENSURE_SUCCESS(rv, rv);
if (array_item) {
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = WrapNative(cx, obj, array_item, NS_GET_IID(nsISupports), vp,
getter_AddRefs(holder));
rv = WrapNative(cx, obj, array_item, NS_GET_IID(nsISupports), vp);
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_SUCCESS_I_DID_SOMETHING;
@ -7867,9 +7854,7 @@ nsNamedArraySH::GetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx,
NS_ENSURE_SUCCESS(rv, rv);
if (item) {
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = WrapNative(cx, obj, item, NS_GET_IID(nsISupports), vp,
getter_AddRefs(holder));
rv = WrapNative(cx, obj, item, NS_GET_IID(nsISupports), vp);
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_SUCCESS_I_DID_SOMETHING;
@ -8514,9 +8499,7 @@ nsHTMLDocumentSH::DocumentAllGetProperty(JSContext *cx, JSObject *obj,
}
if (result) {
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = nsDOMClassInfo::WrapNative(cx, obj, result, NS_GET_IID(nsISupports),
vp, getter_AddRefs(holder));
rv = WrapNative(cx, obj, result, NS_GET_IID(nsISupports), vp);
if (NS_FAILED(rv)) {
nsDOMClassInfo::ThrowJSException(cx, rv);
@ -8948,9 +8931,7 @@ nsHTMLDocumentSH::GetProperty(nsIXPConnectWrappedNative *wrapper,
NS_ENSURE_SUCCESS(rv, rv);
if (result) {
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = WrapNative(cx, obj, result, NS_GET_IID(nsISupports), vp,
getter_AddRefs(holder));
rv = WrapNative(cx, obj, result, NS_GET_IID(nsISupports), vp);
if (NS_SUCCEEDED(rv)) {
rv = NS_SUCCESS_I_DID_SOMETHING;
}
@ -9090,9 +9071,7 @@ nsHTMLFormElementSH::GetProperty(nsIXPConnectWrappedNative *wrapper,
if (result) {
// Wrap result, result can be either an element or a list of
// elements
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
nsresult rv = WrapNative(cx, obj, result, NS_GET_IID(nsISupports), vp,
getter_AddRefs(holder));
nsresult rv = WrapNative(cx, obj, result, NS_GET_IID(nsISupports), vp);
return NS_FAILED(rv) ? rv : NS_SUCCESS_I_DID_SOMETHING;
}
}
@ -9104,9 +9083,7 @@ nsHTMLFormElementSH::GetProperty(nsIXPConnectWrappedNative *wrapper,
form->GetElementAt(n, getter_AddRefs(control));
if (control) {
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
nsresult rv = WrapNative(cx, obj, control, NS_GET_IID(nsISupports), vp,
getter_AddRefs(holder));
nsresult rv = WrapNative(cx, obj, control, NS_GET_IID(nsISupports), vp);
return NS_FAILED(rv) ? rv : NS_SUCCESS_I_DID_SOMETHING;
}
}
@ -9213,9 +9190,7 @@ nsHTMLSelectElementSH::GetProperty(nsIXPConnectWrappedNative *wrapper,
options->Item(n, getter_AddRefs(node));
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = WrapNative(cx, obj, node, NS_GET_IID(nsIDOMNode), vp,
getter_AddRefs(holder));
rv = WrapNative(cx, obj, node, NS_GET_IID(nsIDOMNode), vp);
if (NS_SUCCEEDED(rv)) {
rv = NS_SUCCESS_I_DID_SOMETHING;
}

Просмотреть файл

@ -132,9 +132,9 @@ public:
static nsresult WrapNative(JSContext *cx, JSObject *scope,
nsISupports *native, const nsIID& aIID,
jsval *vp,
// aHolder keeps the jsval alive while
// there's a ref to it
nsIXPConnectJSObjectHolder** aHolder);
// If non-null aHolder will keep the jsval alive
// while there's a ref to it
nsIXPConnectJSObjectHolder** aHolder = nsnull);
static nsresult ThrowJSException(JSContext *cx, nsresult aResult);
static nsresult InitDOMJSClass(JSContext *cx, JSObject *obj);

Просмотреть файл

@ -405,7 +405,7 @@ interface nsIXPCFunctionThisTranslator : nsISupports
{ 0xbd, 0xd6, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }
%}
[uuid(d4c6bc06-2a4f-4315-90ec-d12904aca046)]
[uuid(f8bf005e-3700-411c-ba0c-e018075f22a4)]
interface nsIXPConnect : nsISupports
{
%{ C++
@ -467,6 +467,19 @@ interface nsIXPConnect : nsISupports
in nsISupports aCOMObj,
in nsIIDRef aIID);
/**
* Same as wrapNative, but also returns the JSObject in aVal. C++ callers
* can pass in null for the aHolder argument, but in that case they must
* ensure that aVal is rooted.
*/
void
wrapNativeToJSVal(in JSContextPtr aJSContext,
in JSObjectPtr aScope,
in nsISupports aCOMObj,
in nsIIDRef aIID,
out JSVal aVal,
out nsIXPConnectJSObjectHolder aHolder);
/**
* wrapJS will yield a new or previously existing xpcom interface pointer
* to represent the JSObject passed in.

Просмотреть файл

@ -1101,6 +1101,7 @@ nsXPConnect::InitClassesWithNewWrappedGlobal(JSContext * aJSContext,
!JS_SetPrototype(aJSContext, tempGlobal, nsnull))
return UnexpectedFailure(NS_ERROR_FAILURE);
jsval v;
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
{
// Scope for our auto-marker; it just needs to keep tempGlobal alive
@ -1111,7 +1112,8 @@ nsXPConnect::InitClassesWithNewWrappedGlobal(JSContext * aJSContext,
return UnexpectedFailure(NS_ERROR_FAILURE);
nsresult rv;
if(!XPCConvert::NativeInterface2JSObject(ccx, getter_AddRefs(holder),
if(!XPCConvert::NativeInterface2JSObject(ccx, &v,
getter_AddRefs(holder),
aCOMObj, &aIID, tempGlobal,
PR_FALSE, OBJ_IS_GLOBAL, &rv))
return UnexpectedFailure(rv);
@ -1119,8 +1121,8 @@ nsXPConnect::InitClassesWithNewWrappedGlobal(JSContext * aJSContext,
NS_ASSERTION(NS_SUCCEEDED(rv) && holder, "Didn't wrap properly");
}
JSObject* globalJSObj;
if(NS_FAILED(holder->GetJSObject(&globalJSObj)) || !globalJSObj)
JSObject* globalJSObj = JSVAL_TO_OBJECT(v);
if(!globalJSObj)
return UnexpectedFailure(NS_ERROR_FAILURE);
if(aFlags & nsIXPConnect::FLAG_SYSTEM_GLOBAL_OBJECT)
@ -1180,29 +1182,42 @@ nsXPConnect::WrapNative(JSContext * aJSContext,
JSObject * aScope,
nsISupports *aCOMObj,
const nsIID & aIID,
nsIXPConnectJSObjectHolder **_retval)
nsIXPConnectJSObjectHolder **aHolder)
{
NS_ASSERTION(aHolder, "bad param");
jsval v;
return WrapNativeToJSVal(aJSContext, aScope, aCOMObj, aIID, &v, aHolder);
}
/* void wrapNativeToJSVal (in JSContextPtr aJSContext, in JSObjectPtr aScope, in nsISupports aCOMObj, in nsIIDRef aIID, out JSVal aVal, out nsIXPConnectJSObjectHolder aHolder); */
NS_IMETHODIMP
nsXPConnect::WrapNativeToJSVal(JSContext * aJSContext,
JSObject * aScope,
nsISupports *aCOMObj,
const nsIID & aIID,
jsval *aVal,
nsIXPConnectJSObjectHolder **aHolder)
{
NS_ASSERTION(aJSContext, "bad param");
NS_ASSERTION(aScope, "bad param");
NS_ASSERTION(aCOMObj, "bad param");
NS_ASSERTION(_retval, "bad param");
*_retval = nsnull;
if(aHolder)
*aHolder = nsnull;
XPCCallContext ccx(NATIVE_CALLER, aJSContext);
if(!ccx.IsValid())
return UnexpectedFailure(NS_ERROR_FAILURE);
nsresult rv;
if(!XPCConvert::NativeInterface2JSObject(ccx, _retval, aCOMObj, &aIID,
if(!XPCConvert::NativeInterface2JSObject(ccx, aVal, aHolder, aCOMObj, &aIID,
aScope, PR_FALSE,
OBJ_IS_NOT_GLOBAL, &rv))
return rv;
#ifdef DEBUG
JSObject* returnObj;
(*_retval)->GetJSObject(&returnObj);
NS_ASSERTION(!XPCNativeWrapper::IsNativeWrapper(returnObj),
NS_ASSERTION(!XPCNativeWrapper::IsNativeWrapper(JSVAL_TO_OBJECT(*aVal)),
"Shouldn't be returning a native wrapper here");
#endif

Просмотреть файл

@ -467,24 +467,19 @@ XPCConvert::NativeData2JS(XPCCallContext& ccx, jsval* d, const void* s,
// global object will not have been collected, and
// therefore this NativeInterface2JSObject will not end up
// creating a new XPCNativeScriptableShared.
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
if(!NativeInterface2JSObject(ccx, getter_AddRefs(holder),
iface, iid, scope, PR_TRUE,
OBJ_IS_NOT_GLOBAL, pErr))
JSObject* jsobj;
if(!NativeInterface2JSObject(ccx, &jsobj, iface, iid, scope,
PR_TRUE, OBJ_IS_NOT_GLOBAL,
pErr))
return JS_FALSE;
if(holder)
{
JSObject* jsobj;
if(NS_FAILED(holder->GetJSObject(&jsobj)))
return JS_FALSE;
*d = OBJECT_TO_JSVAL(jsobj);
#ifdef DEBUG
if(!STOBJ_GET_PARENT(jsobj))
NS_ASSERTION(STOBJ_GET_CLASS(jsobj)->flags & JSCLASS_IS_GLOBAL,
"Why did we recreate this wrapper?");
if(jsobj && !STOBJ_GET_PARENT(jsobj))
NS_ASSERTION(STOBJ_GET_CLASS(jsobj)->flags & JSCLASS_IS_GLOBAL,
"Why did we recreate this wrapper?");
#endif
*d = OBJECT_TO_JSVAL(jsobj);
}
}
break;
}
@ -1033,10 +1028,29 @@ XPCConvert::JSData2Native(XPCCallContext& ccx, void* d, jsval s,
return JS_TRUE;
}
JSBool
CreateHolderIfNeeded(XPCCallContext& ccx, JSObject* obj, jsval* d,
nsIXPConnectJSObjectHolder** dest)
{
if(dest)
{
XPCJSObjectHolder* objHolder = XPCJSObjectHolder::newHolder(ccx, obj);
if(!objHolder)
return JS_FALSE;
NS_ADDREF(*dest = objHolder);
}
*d = OBJECT_TO_JSVAL(obj);
return JS_TRUE;
}
/***************************************************************************/
// static
JSBool
XPCConvert::NativeInterface2JSObject(XPCCallContext& ccx,
jsval* d,
nsIXPConnectJSObjectHolder** dest,
nsISupports* src,
const nsID* iid,
@ -1045,13 +1059,16 @@ XPCConvert::NativeInterface2JSObject(XPCCallContext& ccx,
PRBool isGlobal,
nsresult* pErr)
{
NS_ASSERTION(dest, "bad param");
NS_ASSERTION(iid, "bad param");
NS_ASSERTION(scope, "bad param");
*dest = nsnull;
*d = JSVAL_NULL;
if(dest)
*dest = nsnull;
if(!src)
{
return JS_TRUE;
}
if(pErr)
*pErr = NS_ERROR_XPC_BAD_CONVERT_NATIVE;
@ -1073,11 +1090,17 @@ XPCConvert::NativeInterface2JSObject(XPCCallContext& ccx,
// verify that this wrapper is for the right interface
nsCOMPtr<nsISupports> wrapper;
if(NS_FAILED(src->QueryInterface(*iid,(void**)getter_AddRefs(wrapper))))
src->QueryInterface(*iid, (void**)getter_AddRefs(wrapper));
nsCOMPtr<nsIXPConnectJSObjectHolder> holder =
do_QueryInterface(wrapper);
JSObject* flat;
if(!holder || !(flat = holder->GetFlatJSObject()))
return JS_FALSE;
return NS_SUCCEEDED(wrapper->QueryInterface(
NS_GET_IID(nsIXPConnectJSObjectHolder),
(void**) dest));
*d = OBJECT_TO_JSVAL(flat);
if(dest)
holder.swap(*dest);
return JS_TRUE;
}
else
#endif /* XPC_DO_DOUBLE_WRAP */
@ -1094,20 +1117,26 @@ XPCConvert::NativeInterface2JSObject(XPCCallContext& ccx,
nsresult rv;
XPCWrappedNative* wrapper;
nsRefPtr<XPCWrappedNative> strongWrapper;
nsWrapperCache* cache = nsnull;
CallQueryInterface(src, &cache);
if(cache &&
(wrapper = static_cast<XPCWrappedNative*>(cache->GetWrapper())))
{
NS_ADDREF(wrapper);
// If asked to return the wrapper we'll return a strong reference,
// otherwise we'll just return its JSObject in rval (which should be
// rooted in that case).
if(dest)
strongWrapper = wrapper;
wrapper->FindTearOff(ccx, iface, JS_FALSE, &rv);
if(NS_FAILED(rv))
NS_RELEASE(wrapper);
}
else
{
rv = XPCWrappedNative::GetNewOrUsed(ccx, src, xpcscope, iface,
isGlobal, &wrapper);
isGlobal,
getter_AddRefs(strongWrapper));
wrapper = strongWrapper;
}
if(pErr)
@ -1116,6 +1145,7 @@ XPCConvert::NativeInterface2JSObject(XPCCallContext& ccx,
{
uint32 flags = 0;
JSObject *flat = wrapper->GetFlatJSObject();
jsval v = OBJECT_TO_JSVAL(flat);
if (allowNativeWrapper && wrapper->GetScope() != xpcscope)
{
// Cross scope access detected. Check if chrome code
@ -1176,6 +1206,12 @@ XPCConvert::NativeInterface2JSObject(XPCCallContext& ccx,
if(!JS_IsSystemObject(ccx, flat))
{
// From here on we might create new JSObjects, so we need to
// make sure that wrapper stays alive.
if(!strongWrapper)
strongWrapper = wrapper;
JSObject *destObj = nsnull;
if(flags & JSFILENAME_PROTECTED)
{
#ifdef DEBUG_XPCNativeWrapper
@ -1211,72 +1247,30 @@ XPCConvert::NativeInterface2JSObject(XPCCallContext& ccx,
}
}
JSObject *nativeWrapper =
destObj =
XPCNativeWrapper::GetNewOrUsed(ccx, wrapper,
objPrincipal);
if(nativeWrapper)
{
XPCJSObjectHolder *objHolder =
XPCJSObjectHolder::newHolder(ccx, nativeWrapper);
if (objHolder)
{
NS_ADDREF(objHolder);
NS_RELEASE(wrapper);
*dest = objHolder;
return JS_TRUE;
}
}
// Out of memory or other failure that already
// threw a JS exception.
NS_RELEASE(wrapper);
return JS_FALSE;
}
if (flags & JSFILENAME_SYSTEM)
else if (flags & JSFILENAME_SYSTEM)
{
#ifdef DEBUG_mrbkap
printf("Content accessed from chrome, wrapping in an "
"XPCSafeJSObjectWrapper\n");
#endif
jsval v = OBJECT_TO_JSVAL(wrapper->GetFlatJSObject());
XPCJSObjectHolder *objHolder;
if(!XPC_SJOW_Construct(ccx, nsnull, 1, &v, &v) ||
!(objHolder = XPCJSObjectHolder::newHolder(ccx,
JSVAL_TO_OBJECT(v))))
{
NS_RELEASE(wrapper);
return JS_FALSE;
}
NS_ADDREF(objHolder);
NS_RELEASE(wrapper);
*dest = objHolder;
return JS_TRUE;
if(XPC_SJOW_Construct(ccx, nsnull, 1, &v, &v))
destObj = JSVAL_TO_OBJECT(v);
}
// Reaching across scopes from content code. Wrap
// the new object in a XOW.
jsval v = OBJECT_TO_JSVAL(flat);
XPCJSObjectHolder *objHolder = nsnull;
if (!XPC_XOW_WrapObject(ccx, scope, &v) ||
!(objHolder =
XPCJSObjectHolder::newHolder(ccx,
JSVAL_TO_OBJECT(v))))
else
{
NS_RELEASE(wrapper);
return JS_FALSE;
// Reaching across scopes from content code. Wrap
// the new object in a XOW.
if (XPC_XOW_WrapObject(ccx, scope, &v))
destObj = JSVAL_TO_OBJECT(v);
}
NS_ADDREF(objHolder);
NS_RELEASE(wrapper);
*dest = objHolder;
return JS_TRUE;
return destObj &&
CreateHolderIfNeeded(ccx, destObj, d, dest);
}
}
@ -1286,22 +1280,18 @@ XPCConvert::NativeInterface2JSObject(XPCCallContext& ccx,
!JS_IsSystemObject(ccx, flat) &&
XPC_XOW_ClassNeedsXOW(name))
{
jsval v = OBJECT_TO_JSVAL(flat);
XPCJSObjectHolder *objHolder = nsnull;
if (!XPC_XOW_WrapObject(ccx, scope, &v) ||
!(objHolder = XPCJSObjectHolder::newHolder(ccx, JSVAL_TO_OBJECT(v))))
{
NS_RELEASE(wrapper);
return JS_FALSE;
}
// From here on we might create new JSObjects, so we need to
// make sure that wrapper stays alive.
if(!strongWrapper)
strongWrapper = wrapper;
NS_ADDREF(objHolder);
NS_RELEASE(wrapper);
*dest = objHolder;
return JS_TRUE;
return XPC_XOW_WrapObject(ccx, scope, &v) &&
CreateHolderIfNeeded(ccx, JSVAL_TO_OBJECT(v), d, dest);
}
*dest = static_cast<nsIXPConnectJSObjectHolder*>(wrapper);
*d = v;
if(dest)
*dest = strongWrapper.forget().get();
return JS_TRUE;
}
}

Просмотреть файл

@ -2761,6 +2761,7 @@ public:
* @param pErr [out] relevant error code, if any.
*/
static JSBool NativeInterface2JSObject(XPCCallContext& ccx,
jsval* d,
nsIXPConnectJSObjectHolder** dest,
nsISupports* src,
const nsID* iid,
@ -2768,6 +2769,22 @@ public:
PRBool allowNativeWrapper,
PRBool isGlobal,
nsresult* pErr);
static JSBool NativeInterface2JSObject(XPCCallContext& ccx,
JSObject** dest,
nsISupports* src,
const nsID* iid,
JSObject* scope,
PRBool allowNativeWrapper,
PRBool isGlobal,
nsresult* pErr)
{
jsval v;
JSBool ok = NativeInterface2JSObject(ccx, &v, nsnull, src, iid, scope,
allowNativeWrapper, isGlobal,
pErr);
*dest = JSVAL_TO_OBJECT(v);
return ok;
}
static JSBool GetNativeInterfaceFromJSObject(XPCCallContext& ccx,
void** dest, JSObject* src,

Просмотреть файл

@ -756,11 +756,9 @@ xpc_qsXPCOMObjectToJsval(XPCCallContext &ccx, nsISupports *p,
// global object will not have been collected, and
// therefore this NativeInterface2JSObject will not end up
// creating a new XPCNativeScriptableShared.
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
nsresult rv;
if(!XPCConvert::NativeInterface2JSObject(ccx, getter_AddRefs(holder),
p, &iid, scope, PR_TRUE,
OBJ_IS_NOT_GLOBAL, &rv))
if(!XPCConvert::NativeInterface2JSObject(ccx, rval, nsnull, p, &iid, scope,
PR_TRUE, OBJ_IS_NOT_GLOBAL, &rv))
{
// I can't tell if NativeInterface2JSObject throws JS exceptions
// or not. This is a sloppy stab at the right semantics; the
@ -770,22 +768,13 @@ xpc_qsXPCOMObjectToJsval(XPCCallContext &ccx, nsISupports *p,
return JS_FALSE;
}
if(holder)
{
JSObject* jsobj;
if(NS_FAILED(holder->GetJSObject(&jsobj)))
return JS_FALSE;
#ifdef DEBUG
if(!STOBJ_GET_PARENT(jsobj))
NS_ASSERTION(STOBJ_GET_CLASS(jsobj)->flags & JSCLASS_IS_GLOBAL,
"Why did we recreate this wrapper?");
JSObject* jsobj = JSVAL_TO_OBJECT(*rval);
if(jsobj && !STOBJ_GET_PARENT(jsobj))
NS_ASSERTION(STOBJ_GET_CLASS(jsobj)->flags & JSCLASS_IS_GLOBAL,
"Why did we recreate this wrapper?");
#endif
*rval = OBJECT_TO_JSVAL(jsobj);
}
else
{
*rval = JSVAL_NULL;
}
return JS_TRUE;
}

Просмотреть файл

@ -1328,16 +1328,13 @@ nsXPCWrappedJSClass::CallMethod(nsXPCWrappedJS* wrapper, uint16 methodIndex,
newWrapperIID =
const_cast<nsIID*>
(&NS_GET_IID(nsISupports));
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
JSBool ok =
XPCConvert::NativeInterface2JSObject(ccx,
getter_AddRefs(holder), newThis,
newWrapperIID, obj, PR_FALSE, PR_FALSE,
nsnull);
&thisObj, newThis, newWrapperIID, obj,
PR_FALSE, PR_FALSE, nsnull);
if(newWrapperIID != &NS_GET_IID(nsISupports))
nsMemory::Free(newWrapperIID);
if(!ok ||
NS_FAILED(holder->GetJSObject(&thisObj)))
if(!ok)
{
goto pre_call_clean_up;
}