Bug 272294: use backstage pass in xpcshell, for higher-fidelity reproduction

of the environment that privileged JS runs in when in the browser, etc.
r=dbradley.
This commit is contained in:
shaver%mozilla.org 2004-12-07 18:13:02 +00:00
Родитель 178b5e327a
Коммит 3c456cc17f
7 изменённых файлов: 132 добавлений и 102 удалений

Просмотреть файл

@ -41,9 +41,11 @@
#include "nsISupports.idl"
[ptr] native JSRuntime(JSRuntime);
interface nsIXPCScriptable;
[uuid(dcfbb49c-1dd1-11b2-9f39-935aef484817)]
[uuid(e7d09265-4c23-4028-b1b0-c99e02aa78f8)]
interface nsIJSRuntimeService : nsISupports
{
readonly attribute JSRuntime runtime;
readonly attribute nsIXPCScriptable backstagePass;
};

Просмотреть файл

@ -65,7 +65,6 @@
#include "nsString.h"
#ifndef XPCONNECT_STANDALONE
#include "nsIScriptSecurityManager.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsIURL.h"
#include "nsIStandardURL.h"
#include "nsNetUtil.h"
@ -327,76 +326,6 @@ static JSFunctionSpec gGlobalFun[] = {
{0}
};
#ifndef XPCONNECT_STANDALONE
class BackstagePass : public nsIScriptObjectPrincipal, public nsIXPCScriptable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSCRIPTABLE
virtual nsIPrincipal* GetPrincipal() {
return mPrincipal;
}
BackstagePass(nsIPrincipal *prin) :
mPrincipal(prin)
{
}
virtual ~BackstagePass() { }
private:
nsCOMPtr<nsIPrincipal> mPrincipal;
};
NS_IMPL_THREADSAFE_ISUPPORTS2(BackstagePass, nsIScriptObjectPrincipal, nsIXPCScriptable)
#else
class BackstagePass : public nsIXPCScriptable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSCRIPTABLE
BackstagePass()
{
}
virtual ~BackstagePass() { }
};
NS_IMPL_THREADSAFE_ISUPPORTS1(BackstagePass, nsIXPCScriptable)
#endif
// The nsIXPCScriptable map declaration that will generate stubs for us...
#define XPC_MAP_CLASSNAME BackstagePass
#define XPC_MAP_QUOTED_CLASSNAME "BackstagePass"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_FLAGS nsIXPCScriptable::USE_JSSTUB_FOR_ADDPROPERTY | \
nsIXPCScriptable::USE_JSSTUB_FOR_DELPROPERTY | \
nsIXPCScriptable::USE_JSSTUB_FOR_SETPROPERTY | \
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS | \
nsIXPCScriptable::DONT_ENUM_QUERY_INTERFACE | \
nsIXPCScriptable::DONT_REFLECT_INTERFACE_NAMES
#include "xpc_map_end.h" /* This will #undef the above */
/* PRBool newResolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in PRUint32 flags, out JSObjectPtr objp); */
NS_IMETHODIMP
BackstagePass::NewResolve(nsIXPConnectWrappedNative *wrapper,
JSContext * cx, JSObject * obj,
jsval id, PRUint32 flags,
JSObject * *objp, PRBool *_retval)
{
JSBool resolved;
*_retval = JS_ResolveStandardClass(cx, obj, id, &resolved);
if (*_retval && resolved)
*objp = obj;
return NS_OK;
}
mozJSComponentLoader::mozJSComponentLoader()
: mRuntime(nsnull),
mModules(nsnull),
@ -1020,17 +949,16 @@ mozJSComponentLoader::GlobalForLocation(const char *aLocation,
return nsnull;
#ifndef XPCONNECT_STANDALONE
nsCOMPtr<nsIScriptObjectPrincipal> backstagePass =
new BackstagePass(mSystemPrincipal);
rv = mSystemPrincipal->GetJSPrincipals(cx, &jsPrincipals);
if (NS_FAILED(rv) || !jsPrincipals)
return nsnull;
#else
nsCOMPtr<nsISupports> backstagePass = new BackstagePass();
#endif
nsCOMPtr<nsIXPCScriptable> backstagePass;
rv = mRuntimeService->GetBackstagePass(getter_AddRefs(backstagePass));
if (NS_FAILED(rv))
return nsnull;
JSCLAutoErrorReporterSetter aers(cx, Reporter);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;

Просмотреть файл

@ -74,5 +74,5 @@ DEFINES += -DJS_THREADSAFE
#
ifdef JS_READLINE
DEFINES += -DEDITLINE
LIBS += -lreadline
LIBS += -lreadline $(JS_READLINE_EXTRA_LIBS)
endif

Просмотреть файл

@ -1036,7 +1036,6 @@ main(int argc, char **argv, char **envp)
// eval loop)
{
nsCOMPtr<nsIPrincipal> princ;
nsresult rv = NS_OK;
nsCOMPtr<nsIScriptSecurityManager> securityManager =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
@ -1074,15 +1073,29 @@ main(int argc, char **argv, char **envp)
return 1;
}
glob = JS_NewObject(cx, &global_class, NULL, NULL);
if (!glob)
nsCOMPtr<nsIXPCScriptable> backstagePass;
nsresult rv = rtsvc->GetBackstagePass(getter_AddRefs(backstagePass));
if (NS_FAILED(rv)) {
fprintf(gErrFile, "+++ Failed to get backstage pass from rtsvc: %8x\n",
rv);
return 1;
if (!JS_InitStandardClasses(cx, glob))
}
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
rv = xpc->InitClassesWithNewWrappedGlobal(cx, backstagePass,
NS_GET_IID(nsISupports),
PR_FALSE,
getter_AddRefs(holder));
if (NS_FAILED(rv))
return 1;
rv = holder->GetJSObject(&glob);
if (NS_FAILED(rv)) {
NS_ASSERTION(glob == nsnull, "bad GetJSObject?");
return 1;
}
if (!JS_DefineFunctions(cx, glob, glob_functions))
return 1;
if (NS_FAILED(xpc->InitClasses(cx, glob)))
return 1;
envobj = JS_DefineObject(cx, glob, "environment", &env_class, NULL, 0);
if (!envobj || !JS_SetPrivate(cx, envobj, envp))
@ -1094,20 +1107,6 @@ main(int argc, char **argv, char **envp)
result = ProcessArgs(cx, glob, argv, argc);
#ifdef TEST_InitClassesWithNewWrappedGlobal
// quick hacky test...
JSContext* foo = JS_NewContext(rt, 8192);
nsCOMPtr<nsIXPCTestNoisy> bar(new TestGlobal());
nsCOMPtr<nsIXPConnectJSObjectHolder> baz;
xpc->InitClassesWithNewWrappedGlobal(foo, bar, NS_GET_IID(nsIXPCTestNoisy),
PR_TRUE, getter_AddRefs(baz));
bar = nsnull;
baz = nsnull;
JS_GC(foo);
JS_DestroyContext(foo);
#endif
//#define TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN 1
#ifdef TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN

Просмотреть файл

@ -62,6 +62,7 @@ REQUIRES = xpcom \
string \
js \
caps \
dom \
$(NULL)
CPPSRCS = \

Просмотреть файл

@ -2772,6 +2772,49 @@ private:
{0xb5e65b52, 0x1dd1, 0x11b2, \
{ 0xae, 0x8f, 0xf0, 0x92, 0x8e, 0xd8, 0x84, 0x82 }}
#ifndef XPCONNECT_STANDALONE
#include "nsIScriptSecurityManager.h"
#include "nsIPrincipal.h"
#include "nsIScriptObjectPrincipal.h"
class BackstagePass : public nsIScriptObjectPrincipal, public nsIXPCScriptable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSCRIPTABLE
virtual nsIPrincipal* GetPrincipal() {
return mPrincipal;
}
BackstagePass(nsIPrincipal *prin) :
mPrincipal(prin)
{
}
virtual ~BackstagePass() { }
private:
nsCOMPtr<nsIPrincipal> mPrincipal;
};
#else
class BackstagePass : public nsIXPCScriptable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCSCRIPTABLE
BackstagePass()
{
}
virtual ~BackstagePass() { }
};
#endif
class nsJSRuntimeServiceImpl : public nsIJSRuntimeService,
public nsSupportsWeakReference
{
@ -2793,6 +2836,7 @@ class nsJSRuntimeServiceImpl : public nsIJSRuntimeService,
protected:
JSRuntime *mRuntime;
static nsJSRuntimeServiceImpl* gJSRuntimeService;
nsCOMPtr<nsIXPCScriptable> mBackstagePass;
};
/***************************************************************************/

Просмотреть файл

@ -41,6 +41,39 @@
#include "xpcprivate.h"
#ifndef XPCONNECT_STANDALONE
NS_IMPL_THREADSAFE_ISUPPORTS2(BackstagePass, nsIScriptObjectPrincipal, nsIXPCScriptable)
#else
NS_IMPL_THREADSAFE_ISUPPORTS1(BackstagePass, nsIXPCScriptable)
#endif
// The nsIXPCScriptable map declaration that will generate stubs for us...
#define XPC_MAP_CLASSNAME BackstagePass
#define XPC_MAP_QUOTED_CLASSNAME "BackstagePass"
#define XPC_MAP_WANT_NEWRESOLVE
#define XPC_MAP_FLAGS nsIXPCScriptable::USE_JSSTUB_FOR_ADDPROPERTY | \
nsIXPCScriptable::USE_JSSTUB_FOR_DELPROPERTY | \
nsIXPCScriptable::USE_JSSTUB_FOR_SETPROPERTY | \
nsIXPCScriptable::DONT_ENUM_STATIC_PROPS | \
nsIXPCScriptable::DONT_ENUM_QUERY_INTERFACE | \
nsIXPCScriptable::DONT_REFLECT_INTERFACE_NAMES
#include "xpc_map_end.h" /* This will #undef the above */
/* PRBool newResolve (in nsIXPConnectWrappedNative wrapper, in JSContextPtr cx, in JSObjectPtr obj, in JSVal id, in PRUint32 flags, out JSObjectPtr objp); */
NS_IMETHODIMP
BackstagePass::NewResolve(nsIXPConnectWrappedNative *wrapper,
JSContext * cx, JSObject * obj,
jsval id, PRUint32 flags,
JSObject * *objp, PRBool *_retval)
{
JSBool resolved;
*_retval = JS_ResolveStandardClass(cx, obj, id, &resolved);
if(*_retval && resolved)
*objp = obj;
return NS_OK;
}
/*
* This object holds state that we don't want to lose!
*
@ -80,6 +113,7 @@ nsJSRuntimeServiceImpl::GetSingleton()
gJSRuntimeService = new nsJSRuntimeServiceImpl();
// hold an extra reference to lock it down
NS_IF_ADDREF(gJSRuntimeService);
}
NS_IF_ADDREF(gJSRuntimeService);
@ -108,8 +142,30 @@ nsJSRuntimeServiceImpl::GetRuntime(JSRuntime **runtime)
return NS_ERROR_OUT_OF_MEMORY;
}
*runtime = mRuntime;
#ifdef DEBUG_shaver_off
fprintf(stderr, "nJRSI: returning %p\n", (void *)mRuntime);
#endif
return NS_OK;
}
/* attribute nsIXPCScriptable backstagePass; */
NS_IMETHODIMP
nsJSRuntimeServiceImpl::GetBackstagePass(nsIXPCScriptable **bsp)
{
if(!mBackstagePass) {
#ifndef XPCONNECT_STANDALONE
nsCOMPtr<nsIPrincipal> sysprin;
nsCOMPtr<nsIScriptSecurityManager> secman =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
if(!secman)
return nsnull;
if(NS_FAILED(secman->GetSystemPrincipal(getter_AddRefs(sysprin))))
return nsnull;
mBackstagePass = new BackstagePass(sysprin);
#else
mBackstagePass = new BackstagePass();
#endif
if(!mBackstagePass)
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(*bsp = mBackstagePass);
return NS_OK;
}