Bug 889714 - Use the SafeJSContext in XPCShellEnvironment. r=mrbkap

This commit is contained in:
Bobby Holley 2013-07-16 20:38:46 -07:00
parent c2a9310a01
commit 30df80f1ac
2 changed files with 28 additions and 61 deletions

View File

@ -417,8 +417,6 @@ XPCShellEnvironment::ProcessFile(JSContext *cx,
JSBool forceTTY) JSBool forceTTY)
{ {
XPCShellEnvironment* env = this; XPCShellEnvironment* env = this;
nsCxPusher pusher;
pusher.Push(env->GetContext());
JSScript *script; JSScript *script;
JS::Rooted<JS::Value> result(cx); JS::Rooted<JS::Value> result(cx);
@ -569,28 +567,25 @@ XPCShellEnvironment::CreateEnvironment()
} }
XPCShellEnvironment::XPCShellEnvironment() XPCShellEnvironment::XPCShellEnvironment()
: mCx(NULL), : mQuitting(JS_FALSE)
mQuitting(JS_FALSE)
{ {
} }
XPCShellEnvironment::~XPCShellEnvironment() XPCShellEnvironment::~XPCShellEnvironment()
{ {
if (mCx) {
JS_BeginRequest(mCx);
JSObject* global = GetGlobalObject(); AutoSafeJSContext cx;
if (global) { JSObject* global = GetGlobalObject();
JS_SetAllNonReservedSlotsToUndefined(mCx, global); if (global) {
{
JSAutoCompartment ac(cx, global);
JS_SetAllNonReservedSlotsToUndefined(cx, global);
} }
mGlobalHolder.Release(); mGlobalHolder.Release();
JSRuntime *rt = JS_GetRuntime(mCx); JSRuntime *rt = JS_GetRuntime(cx);
JS_GC(rt); JS_GC(rt);
JS_EndRequest(mCx);
JS_DestroyContext(mCx);
if (gOldContextCallback) { if (gOldContextCallback) {
NS_ASSERTION(rt, "Should never be null!"); NS_ASSERTION(rt, "Should never be null!");
JS_SetContextCallback(rt, gOldContextCallback); JS_SetContextCallback(rt, gOldContextCallback);
@ -629,17 +624,7 @@ XPCShellEnvironment::Init()
} }
gOldContextCallback = JS_SetContextCallback(rt, ContextCallback); gOldContextCallback = JS_SetContextCallback(rt, ContextCallback);
AutoSafeJSContext cx;
JSContext *cx = JS_NewContext(rt, 8192);
if (!cx) {
NS_ERROR("JS_NewContext failed!");
JS_SetContextCallback(rt, gOldContextCallback);
gOldContextCallback = NULL;
return false;
}
mCx = cx;
JS_SetContextPrivate(cx, this); JS_SetContextPrivate(cx, this);
@ -662,9 +647,6 @@ XPCShellEnvironment::Init()
fprintf(stderr, "+++ Failed to get ScriptSecurityManager service, running without principals"); fprintf(stderr, "+++ Failed to get ScriptSecurityManager service, running without principals");
} }
nsCxPusher pusher;
pusher.Push(mCx);
nsRefPtr<BackstagePass> backstagePass; nsRefPtr<BackstagePass> backstagePass;
rv = NS_NewBackstagePass(getter_AddRefs(backstagePass)); rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
@ -691,22 +673,19 @@ XPCShellEnvironment::Init()
NS_ERROR("Failed to get global JSObject!"); NS_ERROR("Failed to get global JSObject!");
return false; return false;
} }
JSAutoCompartment ac(cx, globalObj);
backstagePass->SetGlobalObject(globalObj); backstagePass->SetGlobalObject(globalObj);
if (!JS_DefineProperty(cx, globalObj, "__XPCShellEnvironment",
PRIVATE_TO_JSVAL(this), JS_PropertyStub,
JS_StrictPropertyStub,
JSPROP_READONLY | JSPROP_PERMANENT) ||
!JS_DefineFunctions(cx, globalObj, gGlobalFunctions) ||
!JS_DefineProfilingFunctions(cx, globalObj))
{ {
JSAutoRequest ar(cx); NS_ERROR("JS_DefineFunctions failed!");
JSAutoCompartment ac(cx, globalObj); return false;
if (!JS_DefineProperty(cx, globalObj, "__XPCShellEnvironment",
PRIVATE_TO_JSVAL(this), JS_PropertyStub,
JS_StrictPropertyStub,
JSPROP_READONLY | JSPROP_PERMANENT) ||
!JS_DefineFunctions(cx, globalObj, gGlobalFunctions) ||
!JS_DefineProfilingFunctions(cx, globalObj)) {
NS_ERROR("JS_DefineFunctions failed!");
return false;
}
} }
mGlobalHolder = globalObj; mGlobalHolder = globalObj;
@ -726,19 +705,12 @@ bool
XPCShellEnvironment::EvaluateString(const nsString& aString, XPCShellEnvironment::EvaluateString(const nsString& aString,
nsString* aResult) nsString* aResult)
{ {
XPCShellEnvironment* env = this; AutoSafeJSContext cx;
nsCxPusher pusher; JS::Rooted<JSObject*> global(cx, GetGlobalObject());
pusher.Push(env->GetContext()); JSAutoCompartment ac(cx, global);
JSAutoRequest ar(mCx);
JS_ClearPendingException(mCx);
JS::Rooted<JSObject*> global(mCx, GetGlobalObject());
JSAutoCompartment ac(mCx, global);
JSScript* script = JSScript* script =
JS_CompileUCScriptForPrincipals(mCx, global, GetPrincipal(), JS_CompileUCScriptForPrincipals(cx, global, GetPrincipal(),
aString.get(), aString.Length(), aString.get(), aString.Length(),
"typein", 0); "typein", 0);
if (!script) { if (!script) {
@ -749,15 +721,15 @@ XPCShellEnvironment::EvaluateString(const nsString& aString,
aResult->Truncate(); aResult->Truncate();
} }
JS::Rooted<JS::Value> result(mCx); JS::Rooted<JS::Value> result(cx);
JSBool ok = JS_ExecuteScript(mCx, global, script, result.address()); JSBool ok = JS_ExecuteScript(cx, global, script, result.address());
if (ok && result != JSVAL_VOID) { if (ok && result != JSVAL_VOID) {
JSErrorReporter old = JS_SetErrorReporter(mCx, NULL); JSErrorReporter old = JS_SetErrorReporter(cx, NULL);
JSString* str = JS_ValueToString(mCx, result); JSString* str = JS_ValueToString(cx, result);
nsDependentJSString depStr; nsDependentJSString depStr;
if (str) if (str)
depStr.init(mCx, str); depStr.init(cx, str);
JS_SetErrorReporter(mCx, old); JS_SetErrorReporter(cx, old);
if (!depStr.IsEmpty() && aResult) { if (!depStr.IsEmpty() && aResult) {
aResult->Assign(depStr); aResult->Assign(depStr);

View File

@ -43,10 +43,6 @@ public:
return mGlobalHolder.ToJSObject(); return mGlobalHolder.ToJSObject();
} }
JSContext* GetContext() {
return mCx;
}
void SetIsQuitting() { void SetIsQuitting() {
mQuitting = JS_TRUE; mQuitting = JS_TRUE;
} }
@ -59,7 +55,6 @@ protected:
bool Init(); bool Init();
private: private:
JSContext* mCx;
nsAutoJSValHolder mGlobalHolder; nsAutoJSValHolder mGlobalHolder;
JSBool mQuitting; JSBool mQuitting;