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. */
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2010-07-19 18:33:33 +00:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2009-07-11 06:33:10 +00:00
|
|
|
|
2009-08-25 23:07:22 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2014-01-02 03:23:55 +00:00
|
|
|
#include "nsCxPusher.h"
|
2009-08-25 23:07:22 +00:00
|
|
|
|
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-07-11 06:33:10 +00:00
|
|
|
|
2009-09-09 22:59:06 +00:00
|
|
|
PTestShellCommandParent*
|
2013-07-08 15:48:39 +00:00
|
|
|
TestShellParent::AllocPTestShellCommandParent(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
|
2013-07-08 15:48:39 +00:00
|
|
|
TestShellParent::DeallocPTestShellCommandParent(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?
|
2013-08-08 22:53:04 +00:00
|
|
|
/*bool 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
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
bool
|
2009-08-25 23:07:22 +00:00
|
|
|
TestShellCommandParent::SetCallback(JSContext* aCx,
|
2013-03-16 05:22:01 +00:00
|
|
|
JS::Value aCallback)
|
2009-08-25 23:07:22 +00:00
|
|
|
{
|
|
|
|
if (!mCallback.Hold(aCx)) {
|
2013-08-07 06:59:54 +00:00
|
|
|
return false;
|
2009-08-25 23:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
|
|
|
mCx = aCx;
|
2009-07-11 06:33:10 +00:00
|
|
|
|
2013-08-07 06:59:54 +00:00
|
|
|
return true;
|
2009-08-25 23:07:22 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
bool
|
2009-08-25 23:07:22 +00:00
|
|
|
TestShellCommandParent::RunCallback(const nsString& aResponse)
|
|
|
|
{
|
2014-04-16 08:47:57 +00:00
|
|
|
NS_ENSURE_TRUE(!mCallback.get().isNull() && mCx, false);
|
2009-08-25 23:07:22 +00:00
|
|
|
|
2014-01-02 03:23:55 +00:00
|
|
|
// We're pulling a cx off the heap, so make sure it's stack-top.
|
|
|
|
AutoCxPusher pusher(mCx);
|
2013-08-07 06:59:54 +00:00
|
|
|
NS_ENSURE_TRUE(mCallback.ToJSObject(), false);
|
2013-07-17 18:53:53 +00:00
|
|
|
JSAutoCompartment ac(mCx, mCallback.ToJSObject());
|
2013-07-29 23:45:27 +00:00
|
|
|
JS::Rooted<JSObject*> global(mCx, JS::CurrentGlobalOrNull(mCx));
|
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());
|
2013-08-07 06:59:54 +00:00
|
|
|
NS_ENSURE_TRUE(str, false);
|
2009-08-25 23:07:22 +00:00
|
|
|
|
2013-05-30 21:46:48 +00:00
|
|
|
JS::Rooted<JS::Value> strVal(mCx, JS::StringValue(str));
|
2009-08-25 23:07:22 +00:00
|
|
|
|
2013-05-30 21:46:48 +00:00
|
|
|
JS::Rooted<JS::Value> rval(mCx);
|
2014-02-13 15:33:04 +00:00
|
|
|
JS::Rooted<JS::Value> callback(mCx, mCallback);
|
|
|
|
bool ok = JS_CallFunctionValue(mCx, global, callback, strVal, &rval);
|
2013-08-07 06:59:54 +00:00
|
|
|
NS_ENSURE_TRUE(ok, false);
|
2009-08-25 23:07:22 +00:00
|
|
|
|
2013-08-07 06:59:54 +00:00
|
|
|
return true;
|
2009-08-25 23:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|