Bug 1047509 - Part 13: Replace AutoCxPusher in TestShellCommandParent::RunCallback. r=bholley

This commit is contained in:
Bob Owen 2014-08-08 14:01:25 +01:00
parent b9a88b5b76
commit 6f898cd915
2 changed files with 15 additions and 15 deletions

View File

@ -5,12 +5,14 @@
#include "TestShellParent.h" #include "TestShellParent.h"
/* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */ /* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */
#include "jsfriendapi.h"
#include "mozilla/ArrayUtils.h" #include "mozilla/ArrayUtils.h"
#include "mozilla/dom/ContentParent.h" #include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsAutoPtr.h" #include "nsAutoPtr.h"
#include "nsCxPusher.h" #include "xpcpublic.h"
using namespace mozilla; using namespace mozilla;
using mozilla::ipc::TestShellParent; using mozilla::ipc::TestShellParent;
@ -57,7 +59,6 @@ TestShellCommandParent::SetCallback(JSContext* aCx,
} }
mCallback = aCallback; mCallback = aCallback;
mCx = aCx;
return true; return true;
} }
@ -65,22 +66,22 @@ TestShellCommandParent::SetCallback(JSContext* aCx,
bool bool
TestShellCommandParent::RunCallback(const nsString& aResponse) TestShellCommandParent::RunCallback(const nsString& aResponse)
{ {
NS_ENSURE_TRUE(!mCallback.get().isNull() && mCx, false);
// We're pulling a cx off the heap, so make sure it's stack-top.
AutoCxPusher pusher(mCx);
NS_ENSURE_TRUE(mCallback.ToJSObject(), false); NS_ENSURE_TRUE(mCallback.ToJSObject(), false);
JSAutoCompartment ac(mCx, mCallback.ToJSObject());
JS::Rooted<JSObject*> global(mCx, JS::CurrentGlobalOrNull(mCx));
JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length()); // We're about to run script via JS_CallFunctionValue, so we need an
// AutoEntryScript. This is just for testing and not in any spec.
dom::AutoEntryScript aes(xpc::GetNativeForGlobal(js::GetGlobalForObjectCrossCompartment(mCallback.ToJSObject())));
JSContext* cx = aes.cx();
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
JSString* str = JS_NewUCStringCopyN(cx, aResponse.get(), aResponse.Length());
NS_ENSURE_TRUE(str, false); NS_ENSURE_TRUE(str, false);
JS::Rooted<JS::Value> strVal(mCx, JS::StringValue(str)); JS::Rooted<JS::Value> strVal(cx, JS::StringValue(str));
JS::Rooted<JS::Value> rval(mCx); JS::Rooted<JS::Value> rval(cx);
JS::Rooted<JS::Value> callback(mCx, mCallback); JS::Rooted<JS::Value> callback(cx, mCallback);
bool ok = JS_CallFunctionValue(mCx, global, callback, JS::HandleValueArray(strVal), &rval); bool ok = JS_CallFunctionValue(cx, global, callback, JS::HandleValueArray(strVal), &rval);
NS_ENSURE_TRUE(ok, false); NS_ENSURE_TRUE(ok, false);
return true; return true;

View File

@ -40,7 +40,7 @@ public:
class TestShellCommandParent : public PTestShellCommandParent class TestShellCommandParent : public PTestShellCommandParent
{ {
public: public:
TestShellCommandParent() : mCx(nullptr) { } TestShellCommandParent() {}
bool SetCallback(JSContext* aCx, JS::Value aCallback); bool SetCallback(JSContext* aCx, JS::Value aCallback);
@ -58,7 +58,6 @@ protected:
} }
private: private:
JSContext* mCx;
nsAutoJSValHolder mCallback; nsAutoJSValHolder mCallback;
}; };