jclass netscape_oji_ProxyClassLoaderFac = env->FindClass("netscape/oji/ProxyClassLoaderFactory"); if (!netscape_oji_ProxyClassLoaderFac) { env->ExceptionClear(); return NS_ERROR_FAILURE; } jmethodID staticMethodID = env->GetStaticMethodID(netscape_oji_ProxyClassLoaderFac, "createClassLoader", "(Ljava/lang/String;)Ljava/lang/ClassLoader;"); if (!staticMethodID) { env->ExceptionClear(); return NS_ERROR_FAILURE; }
static nsresult getScriptClassLoader(JNIEnv* env, jobject* classloader){    // get the current JSContext from the context stack service.    nsresult rv;    nsCOMPtr<nsIJSContextStack> contexts = do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv);    if (NS_FAILED(rv)) return rv;    JSContext* cx;    rv = contexts->Peek(&cx);    if (NS_FAILED(rv)) return rv;        // lookup "window.navigator.javaclasses", if it exists, this is the class loader bound    // to this page.    JSObject* window = JS_GetGlobalObject(cx);    if (!window) return NS_ERROR_FAILURE;    jsval navigator;    if (!JS_GetProperty(cx, window, "navigator", &navigator)) return NS_ERROR_FAILURE;        jsval javaclasses;    if (JS_GetProperty(cx, JSVAL_TO_OBJECT(navigator), "javaclasses", &javaclasses)) {        // unwrap this, the way LiveConnect does it.        if (JSJ_ConvertJSValueToJavaObject(cx, javaclasses, classloader))            return NS_OK;    }    nsCOMPtr<nsIURI> codebase;    rv = getScriptCodebase(cx, getter_AddRefs(codebase));    if (NS_FAILED(rv)) return rv;        // create a netscape.oji.ProxyClassLoader instance.    nsCAutoString spec;    rv = codebase->GetSpec(spec);    if (NS_FAILED(rv)) return rv;        jstring jspec = env->NewStringUTF(spec.get());    if (!jspec) {        env->ExceptionClear();        return NS_ERROR_FAILURE;    }    jclass netscape_oji_ProxyClassLoaderFac = env->FindClass("netscape/oji/ProxyClassLoaderFactory");    if (!netscape_oji_ProxyClassLoaderFac) {        env->ExceptionClear();        return NS_ERROR_FAILURE;    }    jmethodID staticMethodID = env->GetStaticMethodID(netscape_oji_ProxyClassLoaderFac, "createClassLoader", 						      "(Ljava/lang/String;)Ljava/lang/ClassLoader;");    if (!staticMethodID) {        env->ExceptionClear();        return NS_ERROR_FAILURE;    }    // In order to have permission to create classloader, we need to grant enough permission    nsISecurityContext* origContext = nsnull;    if (NS_FAILED(GetSecurityContext(env, &origContext))) {	return NS_ERROR_FAILURE;    }    nsCOMPtr<nsISecurityContext> nullContext(new nsCNullSecurityContext());    if (!nullContext) {	return NS_ERROR_OUT_OF_MEMORY;    }        SetSecurityContext(env, nullContext);    *classloader = env->CallStaticObjectMethod(netscape_oji_ProxyClassLoaderFac, staticMethodID, jspec);    SetSecurityContext(env, origContext);    if (!*classloader) {        env->ExceptionClear();        return NS_ERROR_FAILURE;    }    env->DeleteLocalRef(jspec);    env->DeleteLocalRef(netscape_oji_ProxyClassLoaderFac);        // now, cache the class loader in "window.navigator.javaclasses"    if (JSJ_ConvertJavaObjectToJSValue(cx, *classloader, &javaclasses)) {        JS_SetProperty(cx, JSVAL_TO_OBJECT(navigator), "javaclasses", &javaclasses);    }        return NS_OK;}