nsCOMPtr<nsIURL> iURL; nsCOMPtr<nsIStandardURL> stdUrl = do_CreateInstance(kStandardURLContractID, &rv); if(!stdUrl || NS_FAILED((rv = stdUrl->Init(nsIStandardURL::URLTYPE_STANDARD, 80, codebase, nsnull, nsnull))) || !(iURL = do_QueryInterface(stdUrl, &rv))) { JS_ReportError(cx, "Can't create URL for evalInSandbox"); return rv; }
jsval *argv; rv = cc->GetArgvPtr(&argv); if (NS_FAILED(rv)) return rv; if (JSVAL_IS_PRIMITIVE(argv[1])) return NS_ERROR_INVALID_ARG; JSObject *sandbox = JSVAL_TO_OBJECT(argv[1]); if (JS_GetClass(cx, sandbox) != &SandboxClass) return NS_ERROR_INVALID_ARG;
nsXPCComponents_Utils::EvalInSandbox(const nsAString &source,                                    const nsAUTF8String &codebase){#ifdef XPCONNECT_STANDALONE    return NS_ERROR_NOT_AVAILABLE;#else    nsresult rv;    nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));    if(NS_FAILED(rv))        return rv;    // get the xpconnect native call context    nsCOMPtr<nsIXPCNativeCallContext> cc;    xpc->GetCurrentNativeCallContext(getter_AddRefs(cc));    if(!cc)        return NS_ERROR_FAILURE;    // Get JSContext of current call    JSContext* cx;    rv = cc->GetJSContext(&cx);    if(NS_FAILED(rv) || !cx)        return NS_ERROR_FAILURE;    // get place for return value    jsval *rval = nsnull;    rv = cc->GetRetValPtr(&rval);    if(NS_FAILED(rv) || !rval)        return NS_ERROR_FAILURE;    // get argc and argv and verify arg count    PRUint32 argc;    rv = cc->GetArgc(&argc);    if(NS_FAILED(rv))        return rv;    JSObject *sandbox = nsnull;    // Optional 3rd arg to reuse an existing sandbox.    if (argc > 2) {        jsval *argv;        rv = cc->GetArgvPtr(&argv);        if (NS_FAILED(rv))            return rv;        if (JSVAL_IS_PRIMITIVE(argv[2]))            return NS_ERROR_INVALID_ARG;        sandbox = JSVAL_TO_OBJECT(argv[2]);        if (JS_GetClass(cx, sandbox) != &SandboxClass)            return NS_ERROR_INVALID_ARG;    }    nsCOMPtr<nsIURL> iURL;    nsCOMPtr<nsIStandardURL> stdUrl =        do_CreateInstance(kStandardURLContractID, &rv);    if(!stdUrl ||       NS_FAILED((rv = stdUrl->Init(nsIStandardURL::URLTYPE_STANDARD, 80,                                    codebase, nsnull, nsnull))) ||       !(iURL = do_QueryInterface(stdUrl, &rv))) {       JS_ReportError(cx, "Can't create URL for evalInSandbox");       return rv;    }    JSPrincipals *jsPrincipals;    nsCOMPtr<nsIPrincipal> principal;    nsCOMPtr<nsIScriptSecurityManager> secman =        do_GetService(kScriptSecurityManagerContractID);    if(!secman ||       NS_FAILED(secman->GetCodebasePrincipal(iURL,                                              getter_AddRefs(principal))) ||       !principal ||       NS_FAILED(principal->GetJSPrincipals(cx, &jsPrincipals)) ||       !jsPrincipals) {       JS_ReportError(cx, "Can't get principals for evalInSandbox");       return NS_ERROR_FAILURE;    }    JSContext *sandcx = JS_NewContext(JS_GetRuntime(cx), 1024);    if(!sandcx) {        JS_ReportError(cx, "Can't prepare context for evalInSandbox");        JSPRINCIPALS_DROP(cx, jsPrincipals);        return NS_ERROR_OUT_OF_MEMORY;    }    if(!sandbox) {        sandbox = JS_NewObject(sandcx, &SandboxClass, nsnull, nsnull);        if(!sandbox) {            JS_DestroyContext(sandcx);            JSPRINCIPALS_DROP(cx, jsPrincipals);            return NS_ERROR_OUT_OF_MEMORY;        }        rv = xpc->InitClasses(sandcx, sandbox);        if (NS_SUCCEEDED(rv) &&            !JS_DefineFunctions(sandcx, sandbox, SandboxFunctions)) {            rv = NS_ERROR_FAILURE;        }        if (NS_FAILED(rv)) {            JS_DestroyContext(sandcx);            JSPRINCIPALS_DROP(cx, jsPrincipals);            return rv;        }    }    JS_SetGlobalObject(sandcx, sandbox);    // Capture uncaught exceptions reported as errors on sandcx and    // re-throw them on cx.    JS_SetContextPrivate(sandcx, cx);    JS_SetErrorReporter(sandcx, SandboxErrorReporter);    if (!JS_EvaluateUCScriptForPrincipals(sandcx, sandbox, jsPrincipals,                                          NS_REINTERPRET_CAST(const jschar *,                                              PromiseFlatString(source).get()),                                          source.Length(),                                          PromiseFlatUTF8String(codebase).get(),                                          1, rval)) {        rv = NS_ERROR_FAILURE;    }    if (NS_SUCCEEDED(rv)) {        *rval = OBJECT_TO_JSVAL(sandbox);        cc->SetReturnValueWasSet(PR_TRUE);    }    JS_DestroyContextNoGC(sandcx);    JSPRINCIPALS_DROP(cx, jsPrincipals);    return rv;#endif /* !XPCONNECT_STANDALONE */}