extern "C" int
StartXPCOM(nsIServiceManager** srvmgr)
{
    nsresult res = NS_InitXPCOM2(srvmgr, 0, 0);
    if (NS_SUCCEEDED(res)) {
        extern nsIInterfaceInfoManager *infomgr;
        infomgr = XPTI_GetInterfaceInfoManager();
    }
    return res;
}
/*
 * Class:     org_mozilla_xpcom_Utilities
 * Method:    getInterfaceMethodNames
 * Signature: (Ljava/lang/String;)[Ljava/lang/String;
 */
JNIEXPORT jobjectArray JNICALL Java_org_mozilla_xpcom_Utilities_getInterfaceMethodNames
    (JNIEnv *env, jclass clazz, jstring jiid) {
    if (!jiid) {
        return NULL;
    }
    nsIID iid;
    const char * str = NULL;
    str = env->GetStringUTFChars(jiid, NULL);
    iid.Parse(str);
    env->ReleaseStringUTFChars(jiid,str);

    nsIInterfaceInfo *interfaceInfo;
    nsIInterfaceInfoManager* iimgr;
    if((iimgr = XPTI_GetInterfaceInfoManager()) != NULL) {
        if (NS_FAILED(iimgr->GetInfoForIID(&iid, &interfaceInfo))) {
            return NULL;  //nb exception handling
        }
        NS_RELEASE(iimgr);
    } else {
        return NULL;
    }
    PRUint16 num;
    interfaceInfo->GetMethodCount(&num);
    jobjectArray names;
    jclass stringClass = env->GetObjectClass(jiid);
    names = env->NewObjectArray(num, stringClass, NULL);    
    nsXPTMethodInfo* info;
    char buf[256];
    for (int i = 0; i < num; i++) {
        interfaceInfo->GetMethodInfo(i, (const nsXPTMethodInfo **)&info);
        const char* name = info->GetName();

        if (info->IsGetter()) {
            sprintf(buf, "get%c%s", toupper(*name), name+1);
        } else if (info->IsSetter()) {
            sprintf(buf, "set%c%s", toupper(*name), name+1);
        } else {
            // first letter of the method name is in lowercase in java
            sprintf(buf, "%c%s", tolower(*name), name + 1);
        }
        env->SetObjectArrayElement(names, i, env->NewStringUTF(buf));
    }
    return names;
}
JNIEXPORT jobject JNICALL Java_org_mozilla_xpcom_Utilities_callMethodByIndex
    (JNIEnv *env, jclass clazz, jlong _oid, jint mid, jstring jiid, jlong _orb, jobjectArray args) {
    bcIORB * orb = (bcIORB*) _orb;
    bcOID oid = (bcOID)_oid;
    nsIID iid;
    PRLogModuleInfo *log = bcJavaGlobal::GetLog();
    PR_LOG(log, PR_LOG_DEBUG, ("--[c++] jni Java_org_mozilla_xpcom_Utilities_callMethodByIndex %d\n",(int)mid));
    const char * str = NULL;
    str = env->GetStringUTFChars(jiid,NULL);
    iid.Parse(str);
    env->ReleaseStringUTFChars(jiid,str);
    if (mid == 2) {
        INVOKE_RELEASE(&oid,&iid,orb);
        return NULL;
    } else if (mid == 1) {
        INVOKE_ADDREF(&oid,&iid,orb);
        return NULL;
    }
    bcICall *call = orb->CreateCall(&iid, &oid, mid);
    
    /*****/
    nsIInterfaceInfo *interfaceInfo;
    nsIInterfaceInfoManager* iimgr;
    if( (iimgr = XPTI_GetInterfaceInfoManager()) != NULL) {
        if (NS_FAILED(iimgr->GetInfoForIID(&iid, &interfaceInfo))) {
            return NULL;  //nb exception handling
        }
        NS_RELEASE(iimgr);
    } else {
        return NULL;
    }
    /*****/
    bcIMarshaler * m = call->GetMarshaler();
    bcJavaMarshalToolkit * mt = new bcJavaMarshalToolkit((unsigned)mid, interfaceInfo, args, env, 0, orb);
    mt->Marshal(m);
    orb->SendReceive(call);
    bcIUnMarshaler * um = call->GetUnMarshaler();
    nsresult result;
    jobject retval;
    um->ReadSimple(&result, bc_T_U32);
    if (NS_SUCCEEDED(result)) {
        mt->UnMarshal(um, &retval);
    } else {
        if (XPCOMExceptionClass == NULL) {
            XPCOMExceptionClass = (jclass) env->FindClass("org/mozilla/xpcom/XPCOMException");
            if (!env->ExceptionOccurred()) { // if there is an exception it will be catched in java
                XPCOMExceptionClass = (jclass)env->NewGlobalRef(XPCOMExceptionClass);
                if (!env->ExceptionOccurred()) {
                    XPCOMExceptionInitMID = env->GetMethodID(XPCOMExceptionClass,"<init>","(I)V");
                    if (env->ExceptionOccurred()) {
                        XPCOMExceptionClass = NULL;
                    }
                }
            }
        }
        if (!env->ExceptionOccurred()) {
            jthrowable exception = (jthrowable) env->NewObject(XPCOMExceptionClass, XPCOMExceptionInitMID,(jint)result);
            env->Throw(exception);
        }
    }
    delete call; delete m; delete um; delete mt;
    
    return retval;
}
示例#4
0
PyG_Base::PyG_Base(PyObject *instance, const nsIID &iid)
{
	// Note that "instance" is the _policy_ instance!!
	PR_AtomicIncrement(&cGateways);
	m_pBaseObject = GetDefaultGateway(instance);
	// m_pWeakRef is an nsCOMPtr and needs no init.

	NS_ABORT_IF_FALSE(!(iid.Equals(NS_GET_IID(nsISupportsWeakReference)) || iid.Equals(NS_GET_IID(nsIWeakReference))),"Should not be creating gateways with weak-ref interfaces");
	m_iid = iid;
	m_pPyObject = instance;
	NS_PRECONDITION(instance, "NULL PyObject for PyXPCOM_XPTStub!");

#ifdef NS_BUILD_REFCNT_LOGGING
	// If XPCOM reference count logging is enabled, then allow us to give the Python class.
	PyObject *realInstance = PyObject_GetAttrString(instance, "_obj_");
	PyObject *r = PyObject_Repr(realInstance);
	const char *szRepr;
	if (r==NULL) {
		PyXPCOM_LogError("Getting the __repr__ of the object failed");
		PyErr_Clear();
		szRepr = "(repr failed!)";
	}
	else
		szRepr = PyString_AsString(r);
	if (szRepr==NULL) szRepr = "";
	int reprOffset = *szRepr=='<' ? 1 : 0;
	static const char *reprPrefix = "component:";
	if (strncmp(reprPrefix, szRepr+reprOffset, strlen(reprPrefix)) == 0)
		reprOffset += strlen(reprPrefix);
	strncpy(refcntLogRepr, szRepr + reprOffset, sizeof(refcntLogRepr)-1);
	refcntLogRepr[sizeof(refcntLogRepr)-1] = '\0';
	// See if we should get rid of the " at 0x12345" portion.
	char *lastPos = strstr(refcntLogRepr, " at ");
	if (lastPos) *lastPos = '\0';
	Py_XDECREF(realInstance);
	Py_XDECREF(r);
#endif // NS_BUILD_REFCNT_LOGGING

#ifdef DEBUG_LIFETIMES
	{
		char *iid_repr;
		nsCOMPtr<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
		if (iim!=nsnull)
			iim->GetNameForIID(&iid, &iid_repr);
		PyObject *real_instance = PyObject_GetAttrString(instance, "_obj_");
		PyObject *real_repr = PyObject_Repr(real_instance);

		PYXPCOM_LOG_DEBUG("PyG_Base created at %p\n  instance_repr=%s\n  IID=%s\n", this, PyString_AsString(real_repr), iid_repr);
		nsMemory::Free(iid_repr);
		Py_XDECREF(real_instance);
		Py_XDECREF(real_repr);
	}
#endif // DEBUG_LIFETIMES
	Py_XINCREF(instance); // instance should never be NULL - but whats an X between friends!

	PyXPCOM_DLLAddRef();

#ifdef DEBUG_FULL
	LogF("PyGatewayBase: created %s", m_pPyObject ? m_pPyObject->ob_type->tp_name : "<NULL>");
#endif
}