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"
|
2021-07-13 14:52:43 +03:00
|
|
|
#include "js/CallAndConstruct.h" // JS_CallFunctionValue
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2021-03-05 18:29:49 +03:00
|
|
|
#include "mozilla/dom/AutoEntryScript.h"
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
2009-09-10 02:59:06 +04:00
|
|
|
using mozilla::ipc::PTestShellCommandParent;
|
2009-08-26 03:07:22 +04:00
|
|
|
using mozilla::ipc::TestShellCommandParent;
|
2009-07-11 10:33:10 +04:00
|
|
|
using mozilla::ipc::TestShellParent;
|
|
|
|
|
2014-05-02 22:44:13 +04:00
|
|
|
void TestShellParent::ActorDestroy(ActorDestroyReason aWhy) {
|
|
|
|
// Implement me! Bug 1005177
|
|
|
|
}
|
|
|
|
|
2013-07-08 19:48:39 +04:00
|
|
|
PTestShellCommandParent* TestShellParent::AllocPTestShellCommandParent(
|
2022-07-25 23:19:48 +03:00
|
|
|
const nsAString& aCommand) {
|
2009-08-26 03:07:22 +04:00
|
|
|
return new TestShellCommandParent();
|
|
|
|
}
|
2009-07-11 10:33:10 +04:00
|
|
|
|
2013-07-08 19:48:39 +04:00
|
|
|
bool 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-12-03 11:16:14 +03:00
|
|
|
bool TestShellParent::CommandDone(TestShellCommandParent* command,
|
2022-07-25 23:19:48 +03:00
|
|
|
const nsAString& aResponse) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool TestShellCommandParent::SetCallback(JSContext* aCx,
|
2016-09-11 12:15:24 +03:00
|
|
|
const JS::Value& aCallback) {
|
2015-01-23 13:23:57 +03:00
|
|
|
if (!mCallback.initialized()) {
|
|
|
|
mCallback.init(aCx, aCallback);
|
|
|
|
return true;
|
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
|
|
|
}
|
|
|
|
|
2022-07-25 23:19:48 +03:00
|
|
|
bool TestShellCommandParent::RunCallback(const nsAString& aResponse) {
|
2015-01-23 13:23:57 +03:00
|
|
|
NS_ENSURE_TRUE(mCallback.isObject(), false);
|
2010-09-30 11:14:37 +04:00
|
|
|
|
2018-07-16 15:02:16 +03:00
|
|
|
MOZ_RELEASE_ASSERT(js::IsFunctionObject(&mCallback.toObject()));
|
|
|
|
|
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.
|
2016-03-09 23:28:26 +03:00
|
|
|
dom::AutoEntryScript aes(&mCallback.toObject(), "TestShellCommand");
|
2014-08-08 17:01:25 +04:00
|
|
|
JSContext* cx = aes.cx();
|
|
|
|
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
|
2022-07-25 23:19:48 +03:00
|
|
|
JSString* str =
|
|
|
|
JS_NewUCStringCopyN(cx, aResponse.BeginReading(), 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.reset(); }
|
2011-06-24 03:31:58 +04:00
|
|
|
|
2022-07-25 23:19:48 +03:00
|
|
|
bool TestShellCommandParent::ExecuteCallback(const nsAString& aResponse) {
|
2011-06-24 03:31:58 +04:00
|
|
|
return static_cast<TestShellParent*>(Manager())->CommandDone(this, aResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestShellCommandParent::ActorDestroy(ActorDestroyReason why) {
|
|
|
|
if (why == AbnormalShutdown) {
|
2020-09-23 18:17:15 +03:00
|
|
|
ExecuteCallback(u""_ns);
|
2011-06-24 03:31:58 +04:00
|
|
|
}
|
|
|
|
}
|