diff --git a/js/src/xpconnect/idl/nsIJSRuntimeService.idl b/js/src/xpconnect/idl/nsIJSRuntimeService.idl index 15dac79f91b..1daeec474c5 100644 --- a/js/src/xpconnect/idl/nsIJSRuntimeService.idl +++ b/js/src/xpconnect/idl/nsIJSRuntimeService.idl @@ -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; }; diff --git a/js/src/xpconnect/loader/mozJSComponentLoader.cpp b/js/src/xpconnect/loader/mozJSComponentLoader.cpp index b6486117087..a742c5eebbc 100644 --- a/js/src/xpconnect/loader/mozJSComponentLoader.cpp +++ b/js/src/xpconnect/loader/mozJSComponentLoader.cpp @@ -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 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 backstagePass = - new BackstagePass(mSystemPrincipal); - rv = mSystemPrincipal->GetJSPrincipals(cx, &jsPrincipals); if (NS_FAILED(rv) || !jsPrincipals) return nsnull; - -#else - nsCOMPtr backstagePass = new BackstagePass(); #endif + nsCOMPtr backstagePass; + rv = mRuntimeService->GetBackstagePass(getter_AddRefs(backstagePass)); + if (NS_FAILED(rv)) + return nsnull; + JSCLAutoErrorReporterSetter aers(cx, Reporter); nsCOMPtr holder; diff --git a/js/src/xpconnect/shell/Makefile.in b/js/src/xpconnect/shell/Makefile.in index cd80dbd3783..68b8f62202d 100644 --- a/js/src/xpconnect/shell/Makefile.in +++ b/js/src/xpconnect/shell/Makefile.in @@ -74,5 +74,5 @@ DEFINES += -DJS_THREADSAFE # ifdef JS_READLINE DEFINES += -DEDITLINE -LIBS += -lreadline +LIBS += -lreadline $(JS_READLINE_EXTRA_LIBS) endif diff --git a/js/src/xpconnect/shell/xpcshell.cpp b/js/src/xpconnect/shell/xpcshell.cpp index efd5ecbd3cd..94993ee75ce 100644 --- a/js/src/xpconnect/shell/xpcshell.cpp +++ b/js/src/xpconnect/shell/xpcshell.cpp @@ -1036,7 +1036,6 @@ main(int argc, char **argv, char **envp) // eval loop) { nsCOMPtr princ; - nsresult rv = NS_OK; nsCOMPtr 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 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 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 bar(new TestGlobal()); - nsCOMPtr 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 diff --git a/js/src/xpconnect/src/Makefile.in b/js/src/xpconnect/src/Makefile.in index 52a74f4fb0f..65ba4c75e49 100644 --- a/js/src/xpconnect/src/Makefile.in +++ b/js/src/xpconnect/src/Makefile.in @@ -62,6 +62,7 @@ REQUIRES = xpcom \ string \ js \ caps \ + dom \ $(NULL) CPPSRCS = \ diff --git a/js/src/xpconnect/src/xpcprivate.h b/js/src/xpconnect/src/xpcprivate.h index 424d266eaa1..2b63da9f9f4 100644 --- a/js/src/xpconnect/src/xpcprivate.h +++ b/js/src/xpconnect/src/xpcprivate.h @@ -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 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 mBackstagePass; }; /***************************************************************************/ diff --git a/js/src/xpconnect/src/xpcruntimesvc.cpp b/js/src/xpconnect/src/xpcruntimesvc.cpp index 607b5d5dd92..53c43c7d160 100644 --- a/js/src/xpconnect/src/xpcruntimesvc.cpp +++ b/js/src/xpconnect/src/xpcruntimesvc.cpp @@ -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 sysprin; + nsCOMPtr 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; }