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. */
|
2014-08-08 17:01:25 +04:00
|
|
|
#include "jsfriendapi.h"
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2010-07-19 22:33:33 +04:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2014-08-08 17:01:25 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2009-08-26 03:07:22 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2014-08-08 17:01:25 +04:00
|
|
|
#include "xpcpublic.h"
|
2009-08-26 03:07:22 +04:00
|
|
|
|
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
|
|
|
|
2014-05-02 22:44:13 +04:00
|
|
|
void
|
|
|
|
TestShellParent::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
// Implement me! Bug 1005177
|
|
|
|
}
|
|
|
|
|
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?
|
2013-08-09 02:53:04 +04:00
|
|
|
/*bool 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
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2009-08-26 03:07:22 +04:00
|
|
|
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)) {
|
2013-08-07 10:59:54 +04:00
|
|
|
return false;
|
2009-08-26 03:07:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-08-07 10:59:54 +04:00
|
|
|
return true;
|
2009-08-26 03:07:22 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
bool
|
2009-08-26 03:07:22 +04:00
|
|
|
TestShellCommandParent::RunCallback(const nsString& aResponse)
|
|
|
|
{
|
2013-08-07 10:59:54 +04:00
|
|
|
NS_ENSURE_TRUE(mCallback.ToJSObject(), false);
|
2010-09-30 11:14:37 +04:00
|
|
|
|
2014-08-08 17:01:25 +04: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.
|
2014-09-21 23:31:53 +04:00
|
|
|
dom::AutoEntryScript aes(xpc::NativeGlobal(js::GetGlobalForObjectCrossCompartment(mCallback.ToJSObject())));
|
2014-08-08 17:01:25 +04:00
|
|
|
JSContext* cx = aes.cx();
|
|
|
|
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
|
|
|
|
JSString* str = JS_NewUCStringCopyN(cx, aResponse.get(), aResponse.Length());
|
2013-08-07 10:59:54 +04:00
|
|
|
NS_ENSURE_TRUE(str, false);
|
2009-08-26 03:07:22 +04:00
|
|
|
|
2014-08-08 17:01:25 +04:00
|
|
|
JS::Rooted<JS::Value> strVal(cx, JS::StringValue(str));
|
2009-08-26 03:07:22 +04:00
|
|
|
|
2014-08-08 17:01:25 +04: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-07 10:59:54 +04:00
|
|
|
NS_ENSURE_TRUE(ok, false);
|
2009-08-26 03:07:22 +04:00
|
|
|
|
2013-08-07 10:59:54 +04:00
|
|
|
return true;
|
2009-08-26 03:07:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|