2012-05-21 11:12:37 +00: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-11 06:33:10 +00:00
|
|
|
|
|
|
|
#include "TestShellParent.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
|
|
|
/* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2010-07-19 18:33:33 +00:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2009-11-06 20:43:39 +00:00
|
|
|
#include "mozilla/jsipc/ContextWrapperParent.h"
|
2009-07-11 06:33:10 +00:00
|
|
|
|
2009-08-25 23:07:22 +00:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
using namespace mozilla;
|
2009-07-11 06:33:10 +00:00
|
|
|
using mozilla::ipc::TestShellParent;
|
2009-08-25 23:07:22 +00:00
|
|
|
using mozilla::ipc::TestShellCommandParent;
|
2009-09-09 22:59:06 +00:00
|
|
|
using mozilla::ipc::PTestShellCommandParent;
|
2010-07-19 18:33:33 +00:00
|
|
|
using mozilla::dom::ContentParent;
|
2009-11-06 20:43:39 +00:00
|
|
|
using mozilla::jsipc::PContextWrapperParent;
|
|
|
|
using mozilla::jsipc::ContextWrapperParent;
|
2009-07-11 06:33:10 +00:00
|
|
|
|
2009-09-09 22:59:06 +00:00
|
|
|
PTestShellCommandParent*
|
2009-09-22 17:31:11 +00:00
|
|
|
TestShellParent::AllocPTestShellCommand(const nsString& aCommand)
|
2009-07-11 06:33:10 +00:00
|
|
|
{
|
2009-08-25 23:07:22 +00:00
|
|
|
return new TestShellCommandParent();
|
|
|
|
}
|
2009-07-11 06:33:10 +00:00
|
|
|
|
2009-09-17 23:09:20 +00:00
|
|
|
bool
|
2009-12-03 08:16:14 +00:00
|
|
|
TestShellParent::DeallocPTestShellCommand(PTestShellCommandParent* aActor)
|
2009-08-25 23:07:22 +00:00
|
|
|
{
|
|
|
|
delete aActor;
|
2009-09-17 23:09:20 +00:00
|
|
|
return true;
|
2009-07-11 06:33:10 +00:00
|
|
|
}
|
|
|
|
|
2009-09-17 23:09:20 +00:00
|
|
|
bool
|
2009-12-03 08:16:14 +00:00
|
|
|
TestShellParent::CommandDone(TestShellCommandParent* command,
|
|
|
|
const nsString& aResponse)
|
2009-07-11 06:33:10 +00:00
|
|
|
{
|
2009-12-03 08:16:14 +00:00
|
|
|
// XXX what should happen if the callback fails?
|
|
|
|
/*JSBool ok = */command->RunCallback(aResponse);
|
2009-08-25 23:07:22 +00:00
|
|
|
command->ReleaseCallback();
|
|
|
|
|
2009-09-17 23:09:20 +00:00
|
|
|
return true;
|
2009-08-25 23:07:22 +00:00
|
|
|
}
|
|
|
|
|
2009-11-06 20:43:39 +00:00
|
|
|
PContextWrapperParent*
|
|
|
|
TestShellParent::AllocPContextWrapper()
|
|
|
|
{
|
2010-07-19 18:33:33 +00:00
|
|
|
ContentParent* cpp = static_cast<ContentParent*>(Manager());
|
2010-01-21 02:24:14 +00:00
|
|
|
return new ContextWrapperParent(cpp);
|
2009-11-06 20:43:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TestShellParent::DeallocPContextWrapper(PContextWrapperParent* actor)
|
|
|
|
{
|
|
|
|
delete actor;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-31 04:41:44 +00:00
|
|
|
JSBool
|
2009-11-06 20:43:39 +00:00
|
|
|
TestShellParent::GetGlobalJSObject(JSContext* cx, JSObject** globalp)
|
|
|
|
{
|
|
|
|
// TODO Unify this code with TabParent::GetGlobalJSObject.
|
2010-11-09 02:49:00 +00:00
|
|
|
InfallibleTArray<PContextWrapperParent*> cwps(1);
|
2009-11-06 20:43:39 +00:00
|
|
|
ManagedPContextWrapperParent(cwps);
|
|
|
|
if (cwps.Length() < 1)
|
2010-03-31 04:41:44 +00:00
|
|
|
return JS_FALSE;
|
2009-11-06 20:43:39 +00:00
|
|
|
NS_ASSERTION(cwps.Length() == 1, "More than one PContextWrapper?");
|
|
|
|
ContextWrapperParent* cwp = static_cast<ContextWrapperParent*>(cwps[0]);
|
2010-03-31 04:41:44 +00:00
|
|
|
return cwp->GetGlobalJSObject(cx, globalp);
|
2009-11-06 20:43:39 +00:00
|
|
|
}
|
|
|
|
|
2009-08-25 23:07:22 +00:00
|
|
|
JSBool
|
|
|
|
TestShellCommandParent::SetCallback(JSContext* aCx,
|
|
|
|
jsval aCallback)
|
|
|
|
{
|
|
|
|
if (!mCallback.Hold(aCx)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
|
|
|
mCx = aCx;
|
2009-07-11 06:33:10 +00:00
|
|
|
|
2009-08-25 23:07:22 +00:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
TestShellCommandParent::RunCallback(const nsString& aResponse)
|
|
|
|
{
|
2011-09-19 16:34:49 +00:00
|
|
|
NS_ENSURE_TRUE(*mCallback.ToJSValPtr() != JSVAL_NULL && mCx, JS_FALSE);
|
2009-08-25 23:07:22 +00:00
|
|
|
|
|
|
|
JSAutoRequest ar(mCx);
|
|
|
|
|
|
|
|
JSObject* global = JS_GetGlobalObject(mCx);
|
|
|
|
NS_ENSURE_TRUE(global, JS_FALSE);
|
|
|
|
|
2012-08-22 01:42:53 +00:00
|
|
|
JSAutoCompartment ac(mCx, global);
|
2010-09-30 07:14:37 +00:00
|
|
|
|
2009-08-25 23:07:22 +00: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 23:11:11 +00:00
|
|
|
unsigned argc = ArrayLength(argv);
|
2009-08-25 23:07:22 +00: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-11 06:33:10 +00:00
|
|
|
}
|
2011-06-23 23:31:58 +00: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());
|
|
|
|
}
|
|
|
|
}
|