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-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-07-10 23:33:10 -07:00
|
|
|
|
2009-09-09 15:59:06 -07:00
|
|
|
PTestShellCommandParent*
|
2013-07-08 08:48:39 -07:00
|
|
|
TestShellParent::AllocPTestShellCommandParent(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
|
2013-07-08 08:48:39 -07:00
|
|
|
TestShellParent::DeallocPTestShellCommandParent(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?
|
2013-08-08 15:53:04 -07:00
|
|
|
/*bool 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
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2009-08-25 16:07:22 -07:00
|
|
|
TestShellCommandParent::SetCallback(JSContext* aCx,
|
2013-03-15 22:22:01 -07:00
|
|
|
JS::Value aCallback)
|
2009-08-25 16:07:22 -07:00
|
|
|
{
|
|
|
|
if (!mCallback.Hold(aCx)) {
|
2013-08-06 23:59:54 -07:00
|
|
|
return false;
|
2009-08-25 16:07:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
|
|
|
mCx = aCx;
|
2009-07-10 23:33:10 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2009-08-25 16:07:22 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2009-08-25 16:07:22 -07:00
|
|
|
TestShellCommandParent::RunCallback(const nsString& aResponse)
|
|
|
|
{
|
2013-08-06 23:59:54 -07:00
|
|
|
NS_ENSURE_TRUE(*mCallback.ToJSValPtr() != JSVAL_NULL && mCx, false);
|
2009-08-25 16:07:22 -07:00
|
|
|
|
|
|
|
JSAutoRequest ar(mCx);
|
2013-08-06 23:59:54 -07:00
|
|
|
NS_ENSURE_TRUE(mCallback.ToJSObject(), false);
|
2013-07-17 11:53:53 -07:00
|
|
|
JSAutoCompartment ac(mCx, mCallback.ToJSObject());
|
2013-07-29 16:45:27 -07:00
|
|
|
JS::Rooted<JSObject*> global(mCx, JS::CurrentGlobalOrNull(mCx));
|
2010-09-30 00:14:37 -07:00
|
|
|
|
2009-08-25 16:07:22 -07:00
|
|
|
JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length());
|
2013-08-06 23:59:54 -07:00
|
|
|
NS_ENSURE_TRUE(str, false);
|
2009-08-25 16:07:22 -07:00
|
|
|
|
2013-05-30 14:46:48 -07:00
|
|
|
JS::Rooted<JS::Value> strVal(mCx, JS::StringValue(str));
|
2009-08-25 16:07:22 -07:00
|
|
|
|
2013-05-30 14:46:48 -07:00
|
|
|
JS::Rooted<JS::Value> rval(mCx);
|
2013-08-08 15:53:04 -07:00
|
|
|
bool ok = JS_CallFunctionValue(mCx, global, mCallback, 1, strVal.address(),
|
2013-07-08 08:48:39 -07:00
|
|
|
rval.address());
|
2013-08-06 23:59:54 -07:00
|
|
|
NS_ENSURE_TRUE(ok, false);
|
2009-08-25 16:07:22 -07:00
|
|
|
|
2013-08-06 23:59:54 -07:00
|
|
|
return true;
|
2009-08-25 16:07:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|