Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
xpccomponents.cpp File Reference
#include "xpcprivate.h"
#include "nsReadableUtils.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsIDOMWindow.h"
#include "xpc_map_end.h"
#include "nsIScriptSecurityManager.h"
#include "nsIURL.h"
#include "nsIStandardURL.h"
#include "nsNetUtil.h"

Go to the source code of this file.

Classes

class  nsXPCComponents_InterfacesByID
class  nsXPCComponents_Classes
class  nsXPCComponents_ClassesByID
class  nsXPCComponents_Results
class  nsXPCComponents_ID
class  nsXPCComponents_Exception
class  nsXPCConstructor
class  nsXPCComponents_Constructor
class  nsXPCComponents_utils_Sandbox
class  nsXPCComponents_Utils
class  PrincipalHolder
class  ContextHolder

Defines

#define XPC_MAP_CLASSNAME   nsXPCComponents_Interfaces
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Interfaces"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_WANT_NEWENUMERATE
#define XPC_MAP_FLAGS
#define XPC_MAP_CLASSNAME   nsXPCComponents_InterfacesByID
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_InterfacesByID"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_WANT_NEWENUMERATE
#define XPC_MAP_FLAGS
#define XPC_MAP_CLASSNAME   nsXPCComponents_Classes
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Classes"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_WANT_NEWENUMERATE
#define XPC_MAP_FLAGS
#define XPC_MAP_CLASSNAME   nsXPCComponents_ClassesByID
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_ClassesByID"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_WANT_NEWENUMERATE
#define XPC_MAP_FLAGS
#define XPC_MAP_CLASSNAME   nsXPCComponents_Results
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Results"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_WANT_NEWENUMERATE
#define XPC_MAP_FLAGS
#define XPC_MAP_CLASSNAME   nsXPCComponents_ID
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_ID"
#define XPC_MAP_WANT_CALL
#define XPC_MAP_WANT_CONSTRUCT
#define XPC_MAP_WANT_HASINSTANCE
#define XPC_MAP_FLAGS   0
#define XPC_MAP_CLASSNAME   nsXPCComponents_Exception
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Exception"
#define XPC_MAP_WANT_CALL
#define XPC_MAP_WANT_CONSTRUCT
#define XPC_MAP_WANT_HASINSTANCE
#define XPC_MAP_FLAGS   0
#define NS_XPCCONSTRUCTOR_CID
#define XPC_MAP_CLASSNAME   nsXPCConstructor
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCConstructor"
#define XPC_MAP_WANT_CALL
#define XPC_MAP_WANT_CONSTRUCT
#define XPC_MAP_FLAGS   0
#define XPC_MAP_CLASSNAME   nsXPCComponents_Constructor
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Constructor"
#define XPC_MAP_WANT_CALL
#define XPC_MAP_WANT_CONSTRUCT
#define XPC_MAP_WANT_HASINSTANCE
#define XPC_MAP_FLAGS   0
#define XPC_MAP_CLASSNAME   nsXPCComponents_Utils
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Utils"
#define XPC_MAP_FLAGS   nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
#define PRINCIPALHOLDER_IID   {0xbf109f49, 0xf94a, 0x43d8, {0x93, 0xdb, 0xe4, 0x66, 0x49, 0xc5, 0xd9, 0x7d}}
#define XPC_MAP_CLASSNAME   nsXPCComponents_utils_Sandbox
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_utils_Sandbox"
#define XPC_MAP_WANT_CALL
#define XPC_MAP_WANT_CONSTRUCT
#define XPC_MAP_FLAGS   0
#define XPC_IMPL_GET_OBJ_METHOD(_b, _n)
#define XPC_MAP_CLASSNAME   nsXPCComponents
#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_WANT_GETPROPERTY
#define XPC_MAP_WANT_SETPROPERTY
#define XPC_MAP_FLAGS   nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE

Functions

static nsresult ThrowAndFail (uintN errNum, JSContext *cx, JSBool *retval)
static JSBool JSValIsInterfaceOfType (JSContext *cx, jsval v, REFNSIID iid)
char * xpc_CloneAllAccess ()
char * xpc_CheckAccessList (const PRUnichar *wideName, const char *list[])
static PRBool IsRegisteredCLSID (const char *str)
 SandboxDump (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 SandboxDebug (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 SandboxFunForwarder (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 SandboxImport (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 sandbox_enumerate (JSContext *cx, JSObject *obj)
 sandbox_resolve (JSContext *cx, JSObject *obj, jsval id)
 sandbox_finalize (JSContext *cx, JSObject *obj)
nsresult xpc_CreateSandboxObject (JSContext *cx, jsval *vp, nsISupports *prinOrSop)
static void SandboxErrorReporter (JSContext *cx, const char *message, JSErrorReport *report)
nsresult xpc_EvalInSandbox (JSContext *cx, JSObject *sandbox, const nsAString &source, const char *filename, PRInt32 lineNo, PRBool returnStringOnly, jsval *rval)

Variables

const char kScriptSecurityManagerContractID [] = NS_SCRIPTSECURITYMANAGER_CONTRACTID
const char kStandardURLContractID [] = "@mozilla.org/network/standard-url;1"
static JSClass SandboxClass
static JSFunctionSpec SandboxFunctions []

Define Documentation

Value:
{ 0xb4a95150, 0xe25a, 0x11d3, \
    { 0x8f, 0x61, 0x0, 0x10, 0xa4, 0xe7, 0x3d, 0x9a } }

Definition at line 1334 of file xpccomponents.cpp.

#define PRINCIPALHOLDER_IID   {0xbf109f49, 0xf94a, 0x43d8, {0x93, 0xdb, 0xe4, 0x66, 0x49, 0xc5, 0xd9, 0x7d}}

Definition at line 2086 of file xpccomponents.cpp.

#define XPC_IMPL_GET_OBJ_METHOD (   _b,
  _n 
)
Value:
NS_IMETHODIMP nsXPCComponents::Get##_n(_b##_n * *a##_n) { \
    NS_ENSURE_ARG_POINTER(a##_n); \
    if(!m##_n) { \
        if(!(m##_n = new nsXPCComponents_##_n())) { \
            *a##_n = nsnull; \
            return NS_ERROR_OUT_OF_MEMORY; \
        } \
        NS_ADDREF(m##_n); \
    } \
    NS_ADDREF(m##_n); \
    *a##_n = m##_n; \
    return NS_OK; \
}

Definition at line 2856 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Definition at line 2912 of file xpccomponents.cpp.

Value:
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS |\
                                    nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE

Definition at line 2917 of file xpccomponents.cpp.

Value:
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS |\
                                    nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE

Definition at line 2917 of file xpccomponents.cpp.

Value:
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS |\
                                    nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE

Definition at line 2917 of file xpccomponents.cpp.

Value:
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS |\
                                    nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE

Definition at line 2917 of file xpccomponents.cpp.

Value:
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS |\
                                    nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE

Definition at line 2917 of file xpccomponents.cpp.

Definition at line 2917 of file xpccomponents.cpp.

Definition at line 2917 of file xpccomponents.cpp.

Definition at line 2917 of file xpccomponents.cpp.

Definition at line 2917 of file xpccomponents.cpp.

Definition at line 2917 of file xpccomponents.cpp.

Definition at line 2917 of file xpccomponents.cpp.

Definition at line 2917 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Interfaces"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_InterfacesByID"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Classes"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_ClassesByID"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Results"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_ID"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Exception"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCConstructor"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Constructor"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_Utils"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents_utils_Sandbox"

Definition at line 2913 of file xpccomponents.cpp.

#define XPC_MAP_QUOTED_CLASSNAME   "nsXPCComponents"

Definition at line 2913 of file xpccomponents.cpp.

Definition at line 2281 of file xpccomponents.cpp.

Definition at line 2281 of file xpccomponents.cpp.

Definition at line 2281 of file xpccomponents.cpp.

Definition at line 2281 of file xpccomponents.cpp.

Definition at line 2281 of file xpccomponents.cpp.

Definition at line 2282 of file xpccomponents.cpp.

Definition at line 2282 of file xpccomponents.cpp.

Definition at line 2282 of file xpccomponents.cpp.

Definition at line 2282 of file xpccomponents.cpp.

Definition at line 2282 of file xpccomponents.cpp.

Definition at line 2915 of file xpccomponents.cpp.

Definition at line 1556 of file xpccomponents.cpp.

Definition at line 1556 of file xpccomponents.cpp.

Definition at line 1556 of file xpccomponents.cpp.

Definition at line 930 of file xpccomponents.cpp.

Definition at line 930 of file xpccomponents.cpp.

Definition at line 930 of file xpccomponents.cpp.

Definition at line 930 of file xpccomponents.cpp.

Definition at line 930 of file xpccomponents.cpp.

Definition at line 2914 of file xpccomponents.cpp.

Definition at line 2914 of file xpccomponents.cpp.

Definition at line 2914 of file xpccomponents.cpp.

Definition at line 2914 of file xpccomponents.cpp.

Definition at line 2914 of file xpccomponents.cpp.

Definition at line 2914 of file xpccomponents.cpp.

Definition at line 2916 of file xpccomponents.cpp.


Function Documentation

static PRBool IsRegisteredCLSID ( const char *  str) [static]

Definition at line 823 of file xpccomponents.cpp.

{
    PRBool registered;
    nsID id;

    if(!id.Parse(str))
        return PR_FALSE;

    nsCOMPtr<nsIComponentRegistrar> compMgr;
    if(NS_FAILED(NS_GetComponentRegistrar(getter_AddRefs(compMgr))) || !compMgr ||
       NS_FAILED(compMgr->IsCIDRegistered(id, &registered)))
        return PR_FALSE;

    return registered;
}

Here is the call graph for this function:

static JSBool JSValIsInterfaceOfType ( JSContext cx,
jsval  v,
REFNSIID  iid 
) [static]

Definition at line 61 of file xpccomponents.cpp.

{
    nsCOMPtr<nsIXPConnect> xpc;
    nsCOMPtr<nsIXPConnectWrappedNative> wn;
    nsCOMPtr<nsISupports> sup;
    nsISupports* iface;
    if(!JSVAL_IS_PRIMITIVE(v) &&
       nsnull != (xpc = nsXPConnect::GetXPConnect()) &&
       NS_SUCCEEDED(xpc->GetWrappedNativeOfJSObject(cx, JSVAL_TO_OBJECT(v),
                            getter_AddRefs(wn))) && wn &&
       NS_SUCCEEDED(wn->Native()->QueryInterface(iid, (void**)&iface)) && iface)
    {
        NS_RELEASE(iface);
        return JS_TRUE;
    }
    return JS_FALSE;
}

Here is the call graph for this function:

sandbox_enumerate ( JSContext cx,
JSObject obj 
)

Definition at line 2224 of file xpccomponents.cpp.

{
    return JS_EnumerateStandardClasses(cx, obj);
}

Here is the call graph for this function:

sandbox_finalize ( JSContext cx,
JSObject obj 
)

Definition at line 2237 of file xpccomponents.cpp.

Here is the call graph for this function:

sandbox_resolve ( JSContext cx,
JSObject obj,
jsval  id 
)

Definition at line 2230 of file xpccomponents.cpp.

{
    JSBool resolved;
    return JS_ResolveStandardClass(cx, obj, id, &resolved);
}

Here is the call graph for this function:

SandboxDebug ( JSContext cx,
JSObject obj,
uintN  argc,
jsval argv,
jsval rval 
)

Definition at line 2136 of file xpccomponents.cpp.

{
#ifdef DEBUG
    return SandboxDump(cx, obj, argc, argv, rval);
#else
    return JS_TRUE;
#endif
}

Here is the call graph for this function:

SandboxDump ( JSContext cx,
JSObject obj,
uintN  argc,
jsval argv,
jsval rval 
)

Definition at line 2117 of file xpccomponents.cpp.

{
    JSString *str;
    if (!argc)
        return JS_TRUE;

    str = JS_ValueToString(cx, argv[0]);
    if (!str)
        return JS_FALSE;

    char *bytes = JS_GetStringBytes(str);
    if (!bytes)
        return JS_FALSE;

    fputs(bytes, stderr);
    return JS_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void SandboxErrorReporter ( JSContext cx,
const char *  message,
JSErrorReport report 
) [static]

Definition at line 2570 of file xpccomponents.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

SandboxFunForwarder ( JSContext cx,
JSObject obj,
uintN  argc,
jsval argv,
jsval rval 
)

Definition at line 2146 of file xpccomponents.cpp.

{
    jsval v;
    if (!JS_GetReservedSlot(cx, JSVAL_TO_OBJECT(argv[-2]), 0, &v) ||
        !JS_CallFunctionValue(cx, obj, v, argc, argv, rval)) {
        return JS_FALSE;
    }

    if (JSVAL_IS_PRIMITIVE(*rval))
        return JS_TRUE; // nothing more to do.
    
    XPCThrower::Throw(NS_ERROR_NOT_IMPLEMENTED, cx);
    return JS_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

SandboxImport ( JSContext cx,
JSObject obj,
uintN  argc,
jsval argv,
jsval rval 
)

Definition at line 2163 of file xpccomponents.cpp.

{
    JSFunction *fun = JS_ValueToFunction(cx, argv[0]);
    if (!fun) {
        XPCThrower::Throw(NS_ERROR_INVALID_ARG, cx);
        return JS_FALSE;
    }

    JSString *funname;
    if (argc > 1) {
        // Use the second parameter as the function name.
        funname = JS_ValueToString(cx, argv[1]);
        if (!funname)
            return JS_FALSE;
        argv[1] = STRING_TO_JSVAL(funname);
    } else {
        // Use the actual function name as the name.
        funname = JS_GetFunctionId(fun);
        if (!funname) {
            XPCThrower::Throw(NS_ERROR_INVALID_ARG, cx);
            return JS_FALSE;
        }
    }

    nsresult rv = NS_ERROR_FAILURE;
    JSObject *oldfunobj = JS_GetFunctionObject(fun);
    nsXPConnect *xpc = nsXPConnect::GetXPConnect();

    if (xpc && oldfunobj) {
        nsIXPCSecurityManager *secman = xpc->GetDefaultSecurityManager();
        if (secman) {
            rv = secman->CanAccess(nsIXPCSecurityManager::ACCESS_CALL_METHOD,
                                   nsnull, cx, oldfunobj, nsnull, nsnull,
                                   STRING_TO_JSVAL(funname), nsnull);
        }
    }

    if (NS_FAILED(rv)) {
        if (rv == NS_ERROR_FAILURE)
            XPCThrower::Throw(NS_ERROR_XPC_SECURITY_MANAGER_VETO, cx);
        return JS_FALSE;
    }

    JSFunction *newfun = JS_DefineUCFunction(cx, obj,
            JS_GetStringChars(funname), JS_GetStringLength(funname),
            SandboxFunForwarder, JS_GetFunctionArity(fun), 0);

    if (!newfun)
        return JS_FALSE;

    JSObject *newfunobj = JS_GetFunctionObject(newfun);
    if (!newfunobj)
        return JS_FALSE;

    // Functions come with two extra reserved slots on them. Use the 0-th slot
    // to communicate the wrapped function to our forwarder.
    return JS_SetReservedSlot(cx, newfunobj, 0, argv[0]);
}

Here is the call graph for this function:

static nsresult ThrowAndFail ( uintN  errNum,
JSContext cx,
JSBool retval 
) [static]

Definition at line 53 of file xpccomponents.cpp.

{
    XPCThrower::Throw(errNum, cx);
    *retval = JS_FALSE;
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* xpc_CheckAccessList ( const PRUnichar wideName,
const char *  list[] 
)

Definition at line 86 of file xpccomponents.cpp.

{
    nsCAutoString asciiName;
    CopyUTF16toUTF8(nsDependentString(wideName), asciiName);

    for(const char** p = list; *p; p++)
        if(!strcmp(*p, asciiName.get()))
            return xpc_CloneAllAccess();

    return nsnull;
}

Here is the call graph for this function:

char* xpc_CloneAllAccess ( )

Definition at line 80 of file xpccomponents.cpp.

{
    static const char allAccess[] = "AllAccess";
    return (char*)nsMemory::Clone(allAccess, sizeof(allAccess));
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult xpc_CreateSandboxObject ( JSContext cx,
jsval vp,
nsISupports *  prinOrSop 
)

Definition at line 2288 of file xpccomponents.cpp.

{
    // Create the sandbox global object
    nsresult rv;
    nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
    if(NS_FAILED(rv))
        return NS_ERROR_XPC_UNEXPECTED;

    XPCAutoJSContext tempcx(JS_NewContext(JS_GetRuntime(cx), 1024), PR_FALSE);
    if (!tempcx)
        return NS_ERROR_OUT_OF_MEMORY;

    AutoJSRequestWithNoCallContext req(tempcx);
    JSObject *sandbox = JS_NewObject(tempcx, &SandboxClass, nsnull, nsnull);
    if (!sandbox)
        return NS_ERROR_XPC_UNEXPECTED;

    JS_SetGlobalObject(tempcx, sandbox);

    nsCOMPtr<nsIScriptObjectPrincipal> sop(do_QueryInterface(prinOrSop));

    if (!sop) {
        nsCOMPtr<nsIPrincipal> principal(do_QueryInterface(prinOrSop));

        if (!principal) {
            // Create a mostly unique URI that has access to
            // nothing other itself.
            static PRBool doneSeed = PR_FALSE;

            if (!doneSeed) {
                srand((int)(PR_Now() & 0xffffffff));

                doneSeed = PR_TRUE;
            }

            char buf[128];
            sprintf(buf, "about:noaccess-%ul", rand());

            nsCOMPtr<nsIURI> uri;
            rv = NS_NewURI(getter_AddRefs(uri), buf);
            NS_ENSURE_SUCCESS(rv, rv);

            nsCOMPtr<nsIScriptSecurityManager> secman =
                do_GetService(kScriptSecurityManagerContractID);
            NS_ENSURE_TRUE(secman, NS_ERROR_UNEXPECTED);

            rv = secman->GetCodebasePrincipal(uri, getter_AddRefs(principal));

            if (!principal || NS_FAILED(rv)) {
                if (NS_SUCCEEDED(rv))
                    rv = NS_ERROR_FAILURE;
                
                return rv;
            }
        }

        sop = new PrincipalHolder(principal);
        if (!sop)
            return NS_ERROR_OUT_OF_MEMORY;
    }

    // Pass on ownership of sop to |sandbox|.

    {
        nsIScriptObjectPrincipal *tmp = sop;

        if (!JS_SetPrivate(cx, sandbox, tmp)) {
            return NS_ERROR_XPC_UNEXPECTED;
        }

        NS_ADDREF(tmp);
    }

    rv = xpc->InitClasses(cx, sandbox);
    if (NS_SUCCEEDED(rv) &&
        !JS_DefineFunctions(cx, sandbox, SandboxFunctions)) {
        rv = NS_ERROR_FAILURE;
    }
    if (NS_FAILED(rv))
        return NS_ERROR_XPC_UNEXPECTED;

    if (vp)
        *vp = OBJECT_TO_JSVAL(sandbox);

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult xpc_EvalInSandbox ( JSContext cx,
JSObject sandbox,
const nsAString &  source,
const char *  filename,
PRInt32  lineNo,
PRBool  returnStringOnly,
jsval rval 
)

Definition at line 2656 of file xpccomponents.cpp.

{
    if (JS_GetClass(cx, sandbox) != &SandboxClass)
        return NS_ERROR_INVALID_ARG;

    nsIScriptObjectPrincipal *sop =
        (nsIScriptObjectPrincipal*)JS_GetPrivate(cx, sandbox);
    NS_ASSERTION(sop, "Invalid sandbox passed");
    nsCOMPtr<nsIPrincipal> prin = sop->GetPrincipal();

    JSPrincipals *jsPrincipals;

    if (!prin ||
        NS_FAILED(prin->GetJSPrincipals(cx, &jsPrincipals)) ||
        !jsPrincipals) {
        return NS_ERROR_FAILURE;
    }

    nsRefPtr<ContextHolder> sandcx = new ContextHolder(cx, sandbox);
    if(!sandcx || !sandcx->GetJSContext()) {
        JS_ReportError(cx, "Can't prepare context for evalInSandbox");
        JSPRINCIPALS_DROP(cx, jsPrincipals);
        return NS_ERROR_OUT_OF_MEMORY;
    }

    XPCPerThreadData *data = XPCPerThreadData::GetData();
    XPCJSContextStack *stack = nsnull;
    if (data && (stack = data->GetJSContextStack())) {
        if (NS_FAILED(stack->Push(sandcx->GetJSContext()))) {
            JS_ReportError(cx,
                    "Unable to initialize XPConnect with the sandbox context");
            JSPRINCIPALS_DROP(cx, jsPrincipals);
            return NS_ERROR_FAILURE;
        }
    }


    // Push a fake frame onto sandcx so that we can properly propagate uncaught
    // exceptions.
    JSStackFrame frame;
    memset(&frame, 0, sizeof frame);

    sandcx->GetJSContext()->fp = &frame;

    if (!filename) {
        // Default the filename to the codebase.
        filename = jsPrincipals->codebase;
        lineNo = 1;
    }

    nsresult rv = NS_OK;

    JSString *str = nsnull;
    if (!JS_EvaluateUCScriptForPrincipals(sandcx->GetJSContext(), sandbox,
                                          jsPrincipals,
                                          NS_REINTERPRET_CAST(const jschar *,
                                              PromiseFlatString(source).get()),
                                          source.Length(), filename, lineNo,
                                          rval) ||
        (returnStringOnly &&
         !JSVAL_IS_VOID(*rval) &&
         !(str = JS_ValueToString(sandcx->GetJSContext(), *rval)))) {
        jsval exn;
        if (JS_GetPendingException(sandcx->GetJSContext(), &exn)) {
            // Stash the exception in |cx| so we can execute code on
            // sandcx without a pending exception.
            JS_SetPendingException(cx, exn);
            JS_ClearPendingException(sandcx->GetJSContext());
            if (returnStringOnly) {
                // The caller asked for strings only, convert the
                // exception into a string.
                str = JS_ValueToString(sandcx->GetJSContext(), exn);

                if (str) {
                    // We converted the exception to a string. Use that
                    // as the value exception.
                    JS_SetPendingException(cx, STRING_TO_JSVAL(str));
                } else {
                    JS_ClearPendingException(cx);
                    rv = NS_ERROR_FAILURE;
                }
            }

            // Clear str so we don't confuse callers.
            str = nsnull;
        } else {
            rv = NS_ERROR_OUT_OF_MEMORY;
        }
    }

    if (str) {
        *rval = STRING_TO_JSVAL(str);
    }

    if (stack) {
        stack->Pop(nsnull);
    }

    NS_ASSERTION(sandcx->GetJSContext()->fp == &frame, "Dangling frame");
    sandcx->GetJSContext()->fp = NULL;

    sandcx->DidEval();

    JSPRINCIPALS_DROP(cx, jsPrincipals);

    return rv;
}

Here is the call graph for this function:


Variable Documentation

Definition at line 2083 of file xpccomponents.cpp.

const char kStandardURLContractID[] = "@mozilla.org/network/standard-url;1"

Definition at line 2084 of file xpccomponents.cpp.

Initial value:
 {
    {"dump", SandboxDump, 1},
    {"debug", SandboxDebug, 1},
    {"importFunction", SandboxImport, 1},
    {0}
}

Definition at line 2251 of file xpccomponents.cpp.