Bug 860438 - Remove custom AutoPusher in ipc XPCShellEnvironment. r=gabor
authorBobby Holley <bobbyholley@gmail.com>
Thu, 18 Apr 2013 11:36:05 -0400
changeset 135990 dd382d615402a519130ac52d50cfebed5cec8041
parent 135989 c14d7d33f3e7871120be9e1f6f5a2d3ac3f2fe06
child 135991 4be115c79b5074791fa76d6faab66f90a9c9849a
push idunknown
push userunknown
push dateunknown
reviewersgabor
bugs860438
milestone23.0a1
Bug 860438 - Remove custom AutoPusher in ipc XPCShellEnvironment. r=gabor Some of this existing code is little wacky in that it calls Environment(mCx) in a non-static method, which I would think would be equivalent to |this|. But I don't know this code well enough to be sure of that, so I'm just going to do the careful thing.
ipc/testshell/XPCShellEnvironment.cpp
ipc/testshell/XPCShellEnvironment.h
--- a/ipc/testshell/XPCShellEnvironment.cpp
+++ b/ipc/testshell/XPCShellEnvironment.cpp
@@ -21,24 +21,24 @@
 
 #include "XPCShellEnvironment.h"
 
 #include "mozilla/XPCOM.h"
 
 #include "nsIChannel.h"
 #include "nsIClassInfo.h"
 #include "nsIDirectoryService.h"
-#include "nsIJSContextStack.h"
 #include "nsIJSRuntimeService.h"
 #include "nsIPrincipal.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsIURI.h"
 #include "nsIXPConnect.h"
 #include "nsIXPCScriptable.h"
 
+#include "nsContentUtils.h"
 #include "nsJSUtils.h"
 #include "nsJSPrincipals.h"
 #include "nsThreadUtils.h"
 #include "nsXULAppAPI.h"
 
 #include "BackstagePass.h"
 
 #include "TestShellChild.h"
@@ -524,17 +524,18 @@ typedef enum JSShellErrNum
 static void
 ProcessFile(JSContext *cx,
             JSObject *obj,
             const char *filename,
             FILE *file,
             JSBool forceTTY)
 {
     XPCShellEnvironment* env = Environment(cx);
-    XPCShellEnvironment::AutoContextPusher pusher(env);
+    nsCxPusher pusher;
+    pusher.Push(env->GetContext());
 
     JSScript *script;
     JS::Value result;
     int lineno, startline;
     JSBool ok, hitEOF;
     char *bufp, buffer[4096];
     JSString *str;
 
@@ -877,36 +878,16 @@ XPCShellDirProvider::GetFile(const char 
         *persistent = true;
         NS_ADDREF(*result = mGREDir);
         return NS_OK;
     }
 
     return NS_ERROR_FAILURE;
 }
 
-XPCShellEnvironment::
-AutoContextPusher::AutoContextPusher(XPCShellEnvironment* aEnv)
-{
-    NS_ASSERTION(aEnv->mCx, "Null context?!");
-
-    if (NS_SUCCEEDED(aEnv->mCxStack->Push(aEnv->mCx))) {
-        mEnv = aEnv;
-    }
-}
-
-XPCShellEnvironment::
-AutoContextPusher::~AutoContextPusher()
-{
-    if (mEnv) {
-        JSContext* cx;
-        mEnv->mCxStack->Pop(&cx);
-        NS_ASSERTION(cx == mEnv->mCx, "Wrong context on the stack!");
-    }
-}
-
 // static
 XPCShellEnvironment*
 XPCShellEnvironment::CreateEnvironment()
 {
     XPCShellEnvironment* env = new XPCShellEnvironment();
     if (env && !env->Init()) {
         delete env;
         env = nullptr;
@@ -933,18 +914,16 @@ XPCShellEnvironment::~XPCShellEnvironmen
         if (global) {
             JS_SetAllNonReservedSlotsToUndefined(mCx, global);
         }
         mGlobalHolder.Release();
 
         JSRuntime *rt = JS_GetRuntime(mCx);
         JS_GC(rt);
 
-        mCxStack = nullptr;
-
         if (mJSPrincipals) {
             JS_DropPrincipals(rt, mJSPrincipals);
         }
 
         JS_EndRequest(mCx);
         JS_DestroyContext(mCx);
 
         if (gOldContextCallback) {
@@ -1022,25 +1001,18 @@ XPCShellEnvironment::Init()
             mJSPrincipals = nsJSPrincipals::get(principal);
             JS_HoldPrincipals(mJSPrincipals);
             secman->SetSystemPrincipal(principal);
         }
     } else {
         fprintf(stderr, "+++ Failed to get ScriptSecurityManager service, running without principals");
     }
 
-    nsCOMPtr<nsIJSContextStack> cxStack =
-        do_GetService("@mozilla.org/js/xpc/ContextStack;1");
-    if (!cxStack) {
-        NS_ERROR("failed to get the nsThreadJSContextStack service!");
-        return false;
-    }
-    mCxStack = cxStack;
-
-    AutoContextPusher pusher(this);
+    nsCxPusher pusher;
+    pusher.Push(mCx);
 
     nsRefPtr<BackstagePass> backstagePass;
     rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
     if (NS_FAILED(rv)) {
         NS_ERROR("Failed to create backstage pass!");
         return false;
     }
 
@@ -1088,17 +1060,18 @@ XPCShellEnvironment::Init()
     return true;
 }
 
 bool
 XPCShellEnvironment::EvaluateString(const nsString& aString,
                                     nsString* aResult)
 {
   XPCShellEnvironment* env = Environment(mCx);
-  XPCShellEnvironment::AutoContextPusher pusher(env);
+  nsCxPusher pusher;
+  pusher.Push(env->GetContext());
 
   JSAutoRequest ar(mCx);
 
   JS_ClearPendingException(mCx);
 
   JSObject* global = GetGlobalObject();
   JSAutoCompartment ac(mCx, global);
 
--- a/ipc/testshell/XPCShellEnvironment.h
+++ b/ipc/testshell/XPCShellEnvironment.h
@@ -68,33 +68,23 @@ public:
 
     void SetShouldCompoleOnly(JSBool aCompileOnly) {
         mCompileOnly = aCompileOnly;
     }
     JSBool ShouldCompileOnly() {
         return mCompileOnly;
     }
 
-    class AutoContextPusher
-    {
-    public:
-        AutoContextPusher(XPCShellEnvironment* aEnv);
-        ~AutoContextPusher();
-    private:
-        XPCShellEnvironment* mEnv;
-    };
-
 protected:
     XPCShellEnvironment();
     bool Init();
 
 private:
     JSContext* mCx;
     nsAutoJSValHolder mGlobalHolder;
-    nsCOMPtr<nsIJSContextStack> mCxStack;
     JSPrincipals* mJSPrincipals;
 
     int mExitCode;
     JSBool mQuitting;
     JSBool mReportWarnings;
     JSBool mCompileOnly;
 };