1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/testshell/TestShellParent.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "TestShellParent.h" 1.9 + 1.10 +/* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */ 1.11 +#include "mozilla/ArrayUtils.h" 1.12 + 1.13 +#include "mozilla/dom/ContentParent.h" 1.14 + 1.15 +#include "nsAutoPtr.h" 1.16 +#include "nsCxPusher.h" 1.17 + 1.18 +using namespace mozilla; 1.19 +using mozilla::ipc::TestShellParent; 1.20 +using mozilla::ipc::TestShellCommandParent; 1.21 +using mozilla::ipc::PTestShellCommandParent; 1.22 +using mozilla::dom::ContentParent; 1.23 + 1.24 +PTestShellCommandParent* 1.25 +TestShellParent::AllocPTestShellCommandParent(const nsString& aCommand) 1.26 +{ 1.27 + return new TestShellCommandParent(); 1.28 +} 1.29 + 1.30 +bool 1.31 +TestShellParent::DeallocPTestShellCommandParent(PTestShellCommandParent* aActor) 1.32 +{ 1.33 + delete aActor; 1.34 + return true; 1.35 +} 1.36 + 1.37 +bool 1.38 +TestShellParent::CommandDone(TestShellCommandParent* command, 1.39 + const nsString& aResponse) 1.40 +{ 1.41 + // XXX what should happen if the callback fails? 1.42 + /*bool ok = */command->RunCallback(aResponse); 1.43 + command->ReleaseCallback(); 1.44 + 1.45 + return true; 1.46 +} 1.47 + 1.48 +bool 1.49 +TestShellCommandParent::SetCallback(JSContext* aCx, 1.50 + JS::Value aCallback) 1.51 +{ 1.52 + if (!mCallback.Hold(aCx)) { 1.53 + return false; 1.54 + } 1.55 + 1.56 + mCallback = aCallback; 1.57 + mCx = aCx; 1.58 + 1.59 + return true; 1.60 +} 1.61 + 1.62 +bool 1.63 +TestShellCommandParent::RunCallback(const nsString& aResponse) 1.64 +{ 1.65 + NS_ENSURE_TRUE(!mCallback.get().isNull() && mCx, false); 1.66 + 1.67 + // We're pulling a cx off the heap, so make sure it's stack-top. 1.68 + AutoCxPusher pusher(mCx); 1.69 + NS_ENSURE_TRUE(mCallback.ToJSObject(), false); 1.70 + JSAutoCompartment ac(mCx, mCallback.ToJSObject()); 1.71 + JS::Rooted<JSObject*> global(mCx, JS::CurrentGlobalOrNull(mCx)); 1.72 + 1.73 + JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length()); 1.74 + NS_ENSURE_TRUE(str, false); 1.75 + 1.76 + JS::Rooted<JS::Value> strVal(mCx, JS::StringValue(str)); 1.77 + 1.78 + JS::Rooted<JS::Value> rval(mCx); 1.79 + JS::Rooted<JS::Value> callback(mCx, mCallback); 1.80 + bool ok = JS_CallFunctionValue(mCx, global, callback, strVal, &rval); 1.81 + NS_ENSURE_TRUE(ok, false); 1.82 + 1.83 + return true; 1.84 +} 1.85 + 1.86 +void 1.87 +TestShellCommandParent::ReleaseCallback() 1.88 +{ 1.89 + mCallback.Release(); 1.90 +} 1.91 + 1.92 +bool 1.93 +TestShellCommandParent::ExecuteCallback(const nsString& aResponse) 1.94 +{ 1.95 + return static_cast<TestShellParent*>(Manager())->CommandDone( 1.96 + this, aResponse); 1.97 +} 1.98 + 1.99 +void 1.100 +TestShellCommandParent::ActorDestroy(ActorDestroyReason why) 1.101 +{ 1.102 + if (why == AbnormalShutdown) { 1.103 + ExecuteCallback(EmptyString()); 1.104 + } 1.105 +}