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. */
|
2014-08-08 06:01:25 -07:00
|
|
|
#include "jsfriendapi.h"
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-10 22:50:08 -07:00
|
|
|
|
2010-07-19 11:33:33 -07:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2014-08-08 06:01:25 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2009-07-10 23:33:10 -07:00
|
|
|
|
2009-08-25 16:07:22 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2014-08-08 06:01:25 -07:00
|
|
|
#include "xpcpublic.h"
|
2009-08-25 16:07:22 -07:00
|
|
|
|
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
|
|
|
|
2014-05-02 11:44:13 -07:00
|
|
|
void
|
|
|
|
TestShellParent::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
// Implement me! Bug 1005177
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-01-23 02:23:57 -08:00
|
|
|
if (!mCallback.initialized()) {
|
|
|
|
mCallback.init(aCx, aCallback);
|
|
|
|
return true;
|
2009-08-25 16:07:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
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)
|
|
|
|
{
|
2015-01-23 02:23:57 -08:00
|
|
|
NS_ENSURE_TRUE(mCallback.isObject(), false);
|
2010-09-30 00:14:37 -07:00
|
|
|
|
2014-08-08 06:01:25 -07:00
|
|
|
// We're about to run script via JS_CallFunctionValue, so we need an
|
|
|
|
// AutoEntryScript. This is just for testing and not in any spec.
|
2015-01-23 02:23:57 -08:00
|
|
|
dom::AutoEntryScript aes(
|
2015-04-08 18:23:48 -07:00
|
|
|
xpc::NativeGlobal(js::GetGlobalForObjectCrossCompartment(&mCallback.toObject())),
|
|
|
|
"TestShellCommand");
|
2014-08-08 06:01:25 -07:00
|
|
|
JSContext* cx = aes.cx();
|
|
|
|
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
|
|
|
|
JSString* str = JS_NewUCStringCopyN(cx, 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
|
|
|
|
2014-08-08 06:01:25 -07:00
|
|
|
JS::Rooted<JS::Value> strVal(cx, JS::StringValue(str));
|
2009-08-25 16:07:22 -07:00
|
|
|
|
2014-08-08 06:01:25 -07:00
|
|
|
JS::Rooted<JS::Value> rval(cx);
|
|
|
|
JS::Rooted<JS::Value> callback(cx, mCallback);
|
|
|
|
bool ok = JS_CallFunctionValue(cx, global, callback, JS::HandleValueArray(strVal), &rval);
|
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()
|
|
|
|
{
|
2015-01-23 02:23:57 -08:00
|
|
|
mCallback.reset();
|
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());
|
|
|
|
}
|
|
|
|
}
|