|
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 "mozilla/ArrayUtils.h" |
|
9 |
|
10 #include "mozilla/dom/ContentParent.h" |
|
11 |
|
12 #include "nsAutoPtr.h" |
|
13 #include "nsCxPusher.h" |
|
14 |
|
15 using namespace mozilla; |
|
16 using mozilla::ipc::TestShellParent; |
|
17 using mozilla::ipc::TestShellCommandParent; |
|
18 using mozilla::ipc::PTestShellCommandParent; |
|
19 using mozilla::dom::ContentParent; |
|
20 |
|
21 PTestShellCommandParent* |
|
22 TestShellParent::AllocPTestShellCommandParent(const nsString& aCommand) |
|
23 { |
|
24 return new TestShellCommandParent(); |
|
25 } |
|
26 |
|
27 bool |
|
28 TestShellParent::DeallocPTestShellCommandParent(PTestShellCommandParent* aActor) |
|
29 { |
|
30 delete aActor; |
|
31 return true; |
|
32 } |
|
33 |
|
34 bool |
|
35 TestShellParent::CommandDone(TestShellCommandParent* command, |
|
36 const nsString& aResponse) |
|
37 { |
|
38 // XXX what should happen if the callback fails? |
|
39 /*bool ok = */command->RunCallback(aResponse); |
|
40 command->ReleaseCallback(); |
|
41 |
|
42 return true; |
|
43 } |
|
44 |
|
45 bool |
|
46 TestShellCommandParent::SetCallback(JSContext* aCx, |
|
47 JS::Value aCallback) |
|
48 { |
|
49 if (!mCallback.Hold(aCx)) { |
|
50 return false; |
|
51 } |
|
52 |
|
53 mCallback = aCallback; |
|
54 mCx = aCx; |
|
55 |
|
56 return true; |
|
57 } |
|
58 |
|
59 bool |
|
60 TestShellCommandParent::RunCallback(const nsString& aResponse) |
|
61 { |
|
62 NS_ENSURE_TRUE(!mCallback.get().isNull() && mCx, false); |
|
63 |
|
64 // We're pulling a cx off the heap, so make sure it's stack-top. |
|
65 AutoCxPusher pusher(mCx); |
|
66 NS_ENSURE_TRUE(mCallback.ToJSObject(), false); |
|
67 JSAutoCompartment ac(mCx, mCallback.ToJSObject()); |
|
68 JS::Rooted<JSObject*> global(mCx, JS::CurrentGlobalOrNull(mCx)); |
|
69 |
|
70 JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length()); |
|
71 NS_ENSURE_TRUE(str, false); |
|
72 |
|
73 JS::Rooted<JS::Value> strVal(mCx, JS::StringValue(str)); |
|
74 |
|
75 JS::Rooted<JS::Value> rval(mCx); |
|
76 JS::Rooted<JS::Value> callback(mCx, mCallback); |
|
77 bool ok = JS_CallFunctionValue(mCx, global, callback, strVal, &rval); |
|
78 NS_ENSURE_TRUE(ok, false); |
|
79 |
|
80 return true; |
|
81 } |
|
82 |
|
83 void |
|
84 TestShellCommandParent::ReleaseCallback() |
|
85 { |
|
86 mCallback.Release(); |
|
87 } |
|
88 |
|
89 bool |
|
90 TestShellCommandParent::ExecuteCallback(const nsString& aResponse) |
|
91 { |
|
92 return static_cast<TestShellParent*>(Manager())->CommandDone( |
|
93 this, aResponse); |
|
94 } |
|
95 |
|
96 void |
|
97 TestShellCommandParent::ActorDestroy(ActorDestroyReason why) |
|
98 { |
|
99 if (why == AbnormalShutdown) { |
|
100 ExecuteCallback(EmptyString()); |
|
101 } |
|
102 } |