2012-05-21 15:12:37 +04: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 10:33:10 +04:00
|
|
|
|
|
|
|
#include "TestShellParent.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
|
|
|
/* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2010-07-19 22:33:33 +04:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2009-08-26 03:07:22 +04:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
2009-07-11 10:33:10 +04:00
|
|
|
using mozilla::ipc::TestShellParent;
|
2009-08-26 03:07:22 +04:00
|
|
|
using mozilla::ipc::TestShellCommandParent;
|
2009-09-10 02:59:06 +04:00
|
|
|
using mozilla::ipc::PTestShellCommandParent;
|
2010-07-19 22:33:33 +04:00
|
|
|
using mozilla::dom::ContentParent;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2009-09-10 02:59:06 +04:00
|
|
|
PTestShellCommandParent*
|
2013-07-08 19:48:39 +04:00
|
|
|
TestShellParent::AllocPTestShellCommandParent(const nsString& aCommand)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2009-08-26 03:07:22 +04:00
|
|
|
return new TestShellCommandParent();
|
|
|
|
}
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2009-09-18 03:09:20 +04:00
|
|
|
bool
|
2013-07-08 19:48:39 +04:00
|
|
|
TestShellParent::DeallocPTestShellCommandParent(PTestShellCommandParent* aActor)
|
2009-08-26 03:07:22 +04:00
|
|
|
{
|
|
|
|
delete aActor;
|
2009-09-18 03:09:20 +04:00
|
|
|
return true;
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
|
|
|
|
2009-09-18 03:09:20 +04:00
|
|
|
bool
|
2009-12-03 11:16:14 +03:00
|
|
|
TestShellParent::CommandDone(TestShellCommandParent* command,
|
|
|
|
const nsString& aResponse)
|
2009-07-11 10:33:10 +04:00
|
|
|
{
|
2009-12-03 11:16:14 +03:00
|
|
|
// XXX what should happen if the callback fails?
|
|
|
|
/*JSBool ok = */command->RunCallback(aResponse);
|
2009-08-26 03:07:22 +04:00
|
|
|
command->ReleaseCallback();
|
|
|
|
|
2009-09-18 03:09:20 +04:00
|
|
|
return true;
|
2009-08-26 03:07:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
TestShellCommandParent::SetCallback(JSContext* aCx,
|
2013-03-16 09:22:01 +04:00
|
|
|
JS::Value aCallback)
|
2009-08-26 03:07:22 +04:00
|
|
|
{
|
|
|
|
if (!mCallback.Hold(aCx)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
|
|
|
mCx = aCx;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2009-08-26 03:07:22 +04:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
TestShellCommandParent::RunCallback(const nsString& aResponse)
|
|
|
|
{
|
2011-09-19 20:34:49 +04:00
|
|
|
NS_ENSURE_TRUE(*mCallback.ToJSValPtr() != JSVAL_NULL && mCx, JS_FALSE);
|
2009-08-26 03:07:22 +04:00
|
|
|
|
|
|
|
JSAutoRequest ar(mCx);
|
|
|
|
|
2013-05-23 01:42:44 +04:00
|
|
|
JS::Rooted<JSObject*> global(mCx, JS_GetGlobalForObject(mCx, mCallback.ToJSObject()));
|
2009-08-26 03:07:22 +04:00
|
|
|
NS_ENSURE_TRUE(global, JS_FALSE);
|
|
|
|
|
2012-08-22 05:42:53 +04:00
|
|
|
JSAutoCompartment ac(mCx, global);
|
2010-09-30 11:14:37 +04:00
|
|
|
|
2009-08-26 03:07:22 +04:00
|
|
|
JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length());
|
|
|
|
NS_ENSURE_TRUE(str, JS_FALSE);
|
|
|
|
|
2013-05-31 01:46:48 +04:00
|
|
|
JS::Rooted<JS::Value> strVal(mCx, JS::StringValue(str));
|
2009-08-26 03:07:22 +04:00
|
|
|
|
2013-05-31 01:46:48 +04:00
|
|
|
JS::Rooted<JS::Value> rval(mCx);
|
|
|
|
JSBool ok = JS_CallFunctionValue(mCx, global, mCallback, 1, strVal.address(),
|
2013-07-08 19:48:39 +04:00
|
|
|
rval.address());
|
2009-08-26 03:07:22 +04:00
|
|
|
NS_ENSURE_TRUE(ok, JS_FALSE);
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TestShellCommandParent::ReleaseCallback()
|
|
|
|
{
|
|
|
|
mCallback.Release();
|
2009-07-11 10:33:10 +04:00
|
|
|
}
|
2011-06-24 03:31:58 +04: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());
|
|
|
|
}
|
|
|
|
}
|