JSBool XPCDispObject::Invoke(XPCCallContext & ccx, CallMode mode) { nsresult rv = ccx.CanCallNow(); if(NS_FAILED(rv)) { // If the security manager is complaining then this is not really an // internal error in xpconnect. So, no reason to botch the assertion. NS_ASSERTION(rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO, "hmm? CanCallNow failed in XPCDispObject::Invoke. " "We are finding out about this late!"); XPCThrower::Throw(rv, ccx); return JS_FALSE; } // TODO: Remove type cast and change GetIDispatchMember to use the correct type XPCDispInterface::Member* member = reinterpret_cast<XPCDispInterface::Member*>(ccx.GetIDispatchMember()); XPCJSRuntime* rt = ccx.GetRuntime(); XPCContext* xpcc = ccx.GetXPCContext(); XPCPerThreadData* tls = ccx.GetThreadData(); jsval* argv = ccx.GetArgv(); uintN argc = ccx.GetArgc(); tls->SetException(nsnull); xpcc->SetLastResult(NS_ERROR_UNEXPECTED); // set up the method index and do the security check if needed PRUint32 secFlag; PRUint32 secAction; switch(mode) { case CALL_METHOD: secFlag = nsIXPCSecurityManager::HOOK_CALL_METHOD; secAction = nsIXPCSecurityManager::ACCESS_CALL_METHOD; break; case CALL_GETTER: secFlag = nsIXPCSecurityManager::HOOK_GET_PROPERTY; secAction = nsIXPCSecurityManager::ACCESS_GET_PROPERTY; break; case CALL_SETTER: secFlag = nsIXPCSecurityManager::HOOK_SET_PROPERTY; secAction = nsIXPCSecurityManager::ACCESS_SET_PROPERTY; break; default: NS_ASSERTION(0,"bad value"); return JS_FALSE; } jsval name = member->GetName(); nsIXPCSecurityManager* sm = xpcc->GetAppropriateSecurityManager(secFlag); XPCWrappedNative* wrapper = ccx.GetWrapper(); if(sm && NS_FAILED(sm->CanAccess(secAction, &ccx, ccx, ccx.GetFlattenedJSObject(), wrapper->GetIdentityObject(), wrapper->GetClassInfo(), name, wrapper->GetSecurityInfoAddr()))) { // the security manager vetoed. It should have set an exception. return JS_FALSE; } IDispatch * pObj = reinterpret_cast<IDispatch*> (ccx.GetTearOff()->GetNative()); PRUint32 args = member->GetParamCount(); uintN err; // Make sure setter has one argument if(mode == CALL_SETTER) args = 1; // Allow for optional parameters. We'll let COM handle the error if there // are not enough parameters if(argc < args) args = argc; XPCDispParams * params = new XPCDispParams(args); jsval val; // If this is a setter, we just need to convert the first parameter if(mode == CALL_SETTER) { params->SetNamedPropID(); if(!XPCDispConvert::JSToCOM(ccx, argv[0], params->GetParamRef(0), err)) { delete params; return ThrowBadParam(err, 0, ccx); } } else if(mode != CALL_GETTER) // This is a function { // Convert the arguments to the function for(PRUint32 index = 0; index < args; ++index) { const XPCDispInterface::Member::ParamInfo & paramInfo = member->GetParamInfo(index); if(paramInfo.IsIn()) { val = argv[index]; if(paramInfo.IsOut()) { if(JSVAL_IS_PRIMITIVE(val) || !OBJ_GET_PROPERTY(ccx, JSVAL_TO_OBJECT(val), rt->GetStringID(XPCJSRuntime::IDX_VALUE), &val)) { delete params; return ThrowBadParam(NS_ERROR_XPC_NEED_OUT_OBJECT, index, ccx); } paramInfo.InitializeOutputParam(params->GetOutputBuffer(index), params->GetParamRef(index)); } if(!XPCDispConvert::JSToCOM(ccx, val, params->GetParamRef(index), err, paramInfo.IsOut())) { delete params; return ThrowBadParam(err, index, ccx); } } else { paramInfo.InitializeOutputParam(params->GetOutputBuffer(index), params->GetParamRef(index)); } } } // If this is a parameterized property if(member->IsParameterizedProperty()) { // We need to get a parameterized property object to return to JS // NewInstance takes ownership of params if(XPCDispParamPropJSClass::NewInstance(ccx, wrapper, member->GetDispID(), params, &val)) { ccx.SetRetVal(val); if(!JS_IdToValue(ccx, 1, &val)) { // This shouldn't fail NS_ERROR("JS_IdToValue failed in XPCDispParamPropJSClass::NewInstance"); return JS_FALSE; } JS_SetCallReturnValue2(ccx, val); return JS_TRUE; } // NewInstance would only fail if there was an out of memory problem JS_ReportOutOfMemory(ccx); delete params; return JS_FALSE; } JSBool retval = Dispatch(ccx, pObj, member->GetDispID(), mode, params, &val, member, rt); if(retval && mode == CALL_SETTER) { ccx.SetRetVal(argv[0]); } else { ccx.SetRetVal(val); } delete params; return retval; }
// static JSBool XPCDispObject::Dispatch(XPCCallContext& ccx, IDispatch * disp, DISPID dispID, CallMode mode, XPCDispParams * params, jsval* retval, XPCDispInterface::Member * member, XPCJSRuntime* rt) { _variant_t dispResult; jsval val; uintN err; uintN argc = params->GetParamCount(); // Figure out what we're doing (getter/setter/method) WORD dispFlags; if(mode == CALL_SETTER) { dispFlags = DISPATCH_PROPERTYPUT; } else if(mode == CALL_GETTER) { dispFlags = DISPATCH_PROPERTYGET; } else { dispFlags = DISPATCH_METHOD; } HRESULT invokeResult; EXCEPINFO exception; // Scope the lock { // avoid deadlock in case the native method blocks somehow AutoJSSuspendRequest req(ccx); // scoped suspend of request // call IDispatch's invoke invokeResult= disp->Invoke( dispID, // IDispatch ID IID_NULL, // Reserved must be IID_NULL LOCALE_SYSTEM_DEFAULT, // The locale context, use the system's dispFlags, // Type of Invoke call params->GetDispParams(), // Parameters &dispResult, // Where the result is stored &exception, // Exception information 0); // Index of an argument error } if(SUCCEEDED(invokeResult)) { *retval = JSVAL_VOID; if(mode == CALL_METHOD) { NS_ASSERTION(member, "member must not be null if this is a method"); for(PRUint32 index = 0; index < argc; ++index) { const XPCDispInterface::Member::ParamInfo & paramInfo = member->GetParamInfo(index); if(paramInfo.IsOut()) { if(!XPCDispConvert::COMToJS(ccx, params->GetParamRef(index), val, err)) return ThrowBadParam(err, index, ccx); if(paramInfo.IsRetVal()) { *retval = val; } else { jsval * argv = ccx.GetArgv(); // Out, in/out parameters must be objects if(!JSVAL_IS_OBJECT(argv[index]) || !OBJ_SET_PROPERTY(ccx, JSVAL_TO_OBJECT(argv[index]), rt->GetStringID(XPCJSRuntime::IDX_VALUE), &val)) return ThrowBadParam(NS_ERROR_XPC_CANT_SET_OUT_VAL, index, ccx); } } } } if(dispResult.vt != VT_EMPTY) { if(!XPCDispConvert::COMToJS(ccx, dispResult, val, err)) { ThrowBadParam(err, 0, ccx); } *retval = val; } } // Set the result and throw the error if one occured ccx.GetXPCContext()->SetLastResult(invokeResult); if(NS_FAILED(invokeResult)) { XPCThrower::ThrowCOMError(ccx, invokeResult, NS_ERROR_XPC_COM_ERROR, invokeResult == DISP_E_EXCEPTION ? &exception : nsnull); return JS_FALSE; } return JS_TRUE; }