2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-07-10 23:33:10 -07:00
|
|
|
|
|
|
|
#include "TestShellParent.h"
|
2011-10-10 22:50:08 -07:00
|
|
|
|
|
|
|
/* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2009-11-06 12:43:39 -08:00
|
|
|
#include "mozilla/jsipc/ContextWrapperParent.h"
|
2009-07-10 23:33:10 -07:00
|
|
|
|
2009-08-25 16:07:22 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
2009-07-10 23:33:10 -07:00
|
|
|
using mozilla::ipc::TestShellParent;
|
2009-08-25 16:07:22 -07:00
|
|
|
using mozilla::ipc::TestShellCommandParent;
|
2009-09-09 15:59:06 -07:00
|
|
|
using mozilla::ipc::PTestShellCommandParent;
|
2010-07-19 11:33:33 -07:00
|
|
|
using mozilla::dom::ContentParent;
|
2009-11-06 12:43:39 -08:00
|
|
|
using mozilla::jsipc::PContextWrapperParent;
|
|
|
|
using mozilla::jsipc::ContextWrapperParent;
|
2009-07-10 23:33:10 -07:00
|
|
|
|
2009-09-09 15:59:06 -07:00
|
|
|
PTestShellCommandParent*
|
2009-09-22 10:31:11 -07:00
|
|
|
TestShellParent::AllocPTestShellCommand(const nsString& aCommand)
|
2009-07-10 23:33:10 -07:00
|
|
|
{
|
2009-08-25 16:07:22 -07:00
|
|
|
return new TestShellCommandParent();
|
|
|
|
}
|
2009-07-10 23:33:10 -07:00
|
|
|
|
2009-09-17 16:09:20 -07:00
|
|
|
bool
|
2009-12-03 00:16:14 -08:00
|
|
|
TestShellParent::DeallocPTestShellCommand(PTestShellCommandParent* aActor)
|
2009-08-25 16:07:22 -07:00
|
|
|
{
|
|
|
|
delete aActor;
|
2009-09-17 16:09:20 -07:00
|
|
|
return true;
|
2009-07-10 23:33:10 -07:00
|
|
|
}
|
|
|
|
|
2009-09-17 16:09:20 -07:00
|
|
|
bool
|
2009-12-03 00:16:14 -08:00
|
|
|
TestShellParent::CommandDone(TestShellCommandParent* command,
|
|
|
|
const nsString& aResponse)
|
2009-07-10 23:33:10 -07:00
|
|
|
{
|
2009-12-03 00:16:14 -08:00
|
|
|
// XXX what should happen if the callback fails?
|
|
|
|
/*JSBool ok = */command->RunCallback(aResponse);
|
2009-08-25 16:07:22 -07:00
|
|
|
command->ReleaseCallback();
|
|
|
|
|
2009-09-17 16:09:20 -07:00
|
|
|
return true;
|
2009-08-25 16:07:22 -07:00
|
|
|
}
|
|
|
|
|
2009-11-06 12:43:39 -08:00
|
|
|
PContextWrapperParent*
|
|
|
|
TestShellParent::AllocPContextWrapper()
|
|
|
|
{
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentParent* cpp = static_cast<ContentParent*>(Manager());
|
2010-01-20 18:24:14 -08:00
|
|
|
return new ContextWrapperParent(cpp);
|
2009-11-06 12:43:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TestShellParent::DeallocPContextWrapper(PContextWrapperParent* actor)
|
|
|
|
{
|
|
|
|
delete actor;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-30 21:41:44 -07:00
|
|
|
JSBool
|
2009-11-06 12:43:39 -08:00
|
|
|
TestShellParent::GetGlobalJSObject(JSContext* cx, JSObject** globalp)
|
|
|
|
{
|
|
|
|
// TODO Unify this code with TabParent::GetGlobalJSObject.
|
2010-11-08 18:49:00 -08:00
|
|
|
InfallibleTArray<PContextWrapperParent*> cwps(1);
|
2009-11-06 12:43:39 -08:00
|
|
|
ManagedPContextWrapperParent(cwps);
|
|
|
|
if (cwps.Length() < 1)
|
2010-03-30 21:41:44 -07:00
|
|
|
return JS_FALSE;
|
2009-11-06 12:43:39 -08:00
|
|
|
NS_ASSERTION(cwps.Length() == 1, "More than one PContextWrapper?");
|
|
|
|
ContextWrapperParent* cwp = static_cast<ContextWrapperParent*>(cwps[0]);
|
2010-03-30 21:41:44 -07:00
|
|
|
return cwp->GetGlobalJSObject(cx, globalp);
|
2009-11-06 12:43:39 -08:00
|
|
|
}
|
|
|
|
|
2009-08-25 16:07:22 -07:00
|
|
|
JSBool
|
|
|
|
TestShellCommandParent::SetCallback(JSContext* aCx,
|
|
|
|
jsval aCallback)
|
|
|
|
{
|
|
|
|
if (!mCallback.Hold(aCx)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
|
|
|
mCx = aCx;
|
2009-07-10 23:33:10 -07:00
|
|
|
|
2009-08-25 16:07:22 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
TestShellCommandParent::RunCallback(const nsString& aResponse)
|
|
|
|
{
|
2011-09-19 09:34:49 -07:00
|
|
|
NS_ENSURE_TRUE(*mCallback.ToJSValPtr() != JSVAL_NULL && mCx, JS_FALSE);
|
2009-08-25 16:07:22 -07:00
|
|
|
|
|
|
|
JSAutoRequest ar(mCx);
|
|
|
|
|
|
|
|
JSObject* global = JS_GetGlobalObject(mCx);
|
|
|
|
NS_ENSURE_TRUE(global, JS_FALSE);
|
|
|
|
|
2010-09-30 00:14:37 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
2010-09-30 00:47:44 -07:00
|
|
|
if (!ac.enter(mCx, global)) {
|
2010-09-30 00:14:37 -07:00
|
|
|
NS_ERROR("Failed to enter compartment!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-25 16:07:22 -07:00
|
|
|
JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length());
|
|
|
|
NS_ENSURE_TRUE(str, JS_FALSE);
|
|
|
|
|
|
|
|
jsval argv[] = { STRING_TO_JSVAL(str) };
|
2012-02-28 15:11:11 -08:00
|
|
|
unsigned argc = ArrayLength(argv);
|
2009-08-25 16:07:22 -07:00
|
|
|
|
|
|
|
jsval rval;
|
|
|
|
JSBool ok = JS_CallFunctionValue(mCx, global, mCallback, argc, argv, &rval);
|
|
|
|
NS_ENSURE_TRUE(ok, JS_FALSE);
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TestShellCommandParent::ReleaseCallback()
|
|
|
|
{
|
|
|
|
mCallback.Release();
|
2009-07-10 23:33:10 -07:00
|
|
|
}
|
2011-06-23 16:31:58 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
TestShellCommandParent::ExecuteCallback(const nsString& aResponse)
|
|
|
|
{
|
|
|
|
return static_cast<TestShellParent*>(Manager())->CommandDone(
|
|
|
|
this, aResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TestShellCommandParent::ActorDestroy(ActorDestroyReason why)
|
|
|
|
{
|
|
|
|
if (why == AbnormalShutdown) {
|
|
|
|
ExecuteCallback(EmptyString());
|
|
|
|
}
|
|
|
|
}
|