TestShellParent.cpp (2806B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "TestShellParent.h" 6 7 /* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */ 8 #include "jsfriendapi.h" 9 #include "js/CallAndConstruct.h" // JS_CallFunctionValue 10 11 #include "mozilla/dom/AutoEntryScript.h" 12 13 using namespace mozilla; 14 using mozilla::ipc::PTestShellCommandParent; 15 using mozilla::ipc::TestShellCommandParent; 16 using mozilla::ipc::TestShellParent; 17 18 void TestShellParent::ActorDestroy(ActorDestroyReason aWhy) { 19 // Implement me! Bug 1005177 20 } 21 22 PTestShellCommandParent* TestShellParent::AllocPTestShellCommandParent( 23 const nsAString& aCommand) { 24 return new TestShellCommandParent(); 25 } 26 27 bool TestShellParent::DeallocPTestShellCommandParent( 28 PTestShellCommandParent* aActor) { 29 delete aActor; 30 return true; 31 } 32 33 bool TestShellParent::CommandDone(TestShellCommandParent* command, 34 const nsAString& aResponse) { 35 // XXX what should happen if the callback fails? 36 /*bool ok = */ command->RunCallback(aResponse); 37 command->ReleaseCallback(); 38 39 return true; 40 } 41 42 bool TestShellCommandParent::SetCallback(JSContext* aCx, 43 const JS::Value& aCallback) { 44 if (!mCallback.initialized()) { 45 mCallback.init(aCx, aCallback); 46 return true; 47 } 48 49 mCallback = aCallback; 50 51 return true; 52 } 53 54 bool TestShellCommandParent::RunCallback(const nsAString& aResponse) { 55 NS_ENSURE_TRUE(mCallback.isObject(), false); 56 57 MOZ_RELEASE_ASSERT(js::IsFunctionObject(&mCallback.toObject())); 58 59 // We're about to run script via JS_CallFunctionValue, so we need an 60 // AutoEntryScript. This is just for testing and not in any spec. 61 dom::AutoEntryScript aes(&mCallback.toObject(), "TestShellCommand"); 62 JSContext* cx = aes.cx(); 63 JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx)); 64 65 JSString* str = 66 JS_NewUCStringCopyN(cx, aResponse.BeginReading(), aResponse.Length()); 67 NS_ENSURE_TRUE(str, false); 68 69 JS::Rooted<JS::Value> strVal(cx, JS::StringValue(str)); 70 71 JS::Rooted<JS::Value> rval(cx); 72 JS::Rooted<JS::Value> callback(cx, mCallback); 73 bool ok = JS_CallFunctionValue(cx, global, callback, 74 JS::HandleValueArray(strVal), &rval); 75 NS_ENSURE_TRUE(ok, false); 76 77 return true; 78 } 79 80 void TestShellCommandParent::ReleaseCallback() { mCallback.reset(); } 81 82 bool TestShellCommandParent::ExecuteCallback(const nsAString& aResponse) { 83 return static_cast<TestShellParent*>(Manager())->CommandDone(this, aResponse); 84 } 85 86 void TestShellCommandParent::ActorDestroy(ActorDestroyReason why) { 87 if (why == AbnormalShutdown) { 88 ExecuteCallback(u""_ns); 89 } 90 }