/src/mozilla-central/ipc/testshell/TestShellParent.cpp
Line | Count | Source (jump to first uncovered line) |
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 "mozilla/ArrayUtils.h" |
10 | | |
11 | | #include "mozilla/dom/ContentParent.h" |
12 | | #include "mozilla/dom/ScriptSettings.h" |
13 | | |
14 | | #include "xpcpublic.h" |
15 | | |
16 | | using namespace mozilla; |
17 | | using mozilla::ipc::TestShellParent; |
18 | | using mozilla::ipc::TestShellCommandParent; |
19 | | using mozilla::ipc::PTestShellCommandParent; |
20 | | |
21 | | void |
22 | | TestShellParent::ActorDestroy(ActorDestroyReason aWhy) |
23 | 0 | { |
24 | 0 | // Implement me! Bug 1005177 |
25 | 0 | } |
26 | | |
27 | | PTestShellCommandParent* |
28 | | TestShellParent::AllocPTestShellCommandParent(const nsString& aCommand) |
29 | 0 | { |
30 | 0 | return new TestShellCommandParent(); |
31 | 0 | } |
32 | | |
33 | | bool |
34 | | TestShellParent::DeallocPTestShellCommandParent(PTestShellCommandParent* aActor) |
35 | 0 | { |
36 | 0 | delete aActor; |
37 | 0 | return true; |
38 | 0 | } |
39 | | |
40 | | bool |
41 | | TestShellParent::CommandDone(TestShellCommandParent* command, |
42 | | const nsString& aResponse) |
43 | 0 | { |
44 | 0 | // XXX what should happen if the callback fails? |
45 | 0 | /*bool ok = */command->RunCallback(aResponse); |
46 | 0 | command->ReleaseCallback(); |
47 | 0 |
|
48 | 0 | return true; |
49 | 0 | } |
50 | | |
51 | | bool |
52 | | TestShellCommandParent::SetCallback(JSContext* aCx, |
53 | | const JS::Value& aCallback) |
54 | 0 | { |
55 | 0 | if (!mCallback.initialized()) { |
56 | 0 | mCallback.init(aCx, aCallback); |
57 | 0 | return true; |
58 | 0 | } |
59 | 0 | |
60 | 0 | mCallback = aCallback; |
61 | 0 |
|
62 | 0 | return true; |
63 | 0 | } |
64 | | |
65 | | bool |
66 | | TestShellCommandParent::RunCallback(const nsString& aResponse) |
67 | 0 | { |
68 | 0 | NS_ENSURE_TRUE(mCallback.isObject(), false); |
69 | 0 |
|
70 | 0 | MOZ_RELEASE_ASSERT(js::IsFunctionObject(&mCallback.toObject())); |
71 | 0 |
|
72 | 0 | // We're about to run script via JS_CallFunctionValue, so we need an |
73 | 0 | // AutoEntryScript. This is just for testing and not in any spec. |
74 | 0 | dom::AutoEntryScript aes(&mCallback.toObject(), "TestShellCommand"); |
75 | 0 | JSContext* cx = aes.cx(); |
76 | 0 | JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx)); |
77 | 0 |
|
78 | 0 | JSString* str = JS_NewUCStringCopyN(cx, aResponse.get(), aResponse.Length()); |
79 | 0 | NS_ENSURE_TRUE(str, false); |
80 | 0 |
|
81 | 0 | JS::Rooted<JS::Value> strVal(cx, JS::StringValue(str)); |
82 | 0 |
|
83 | 0 | JS::Rooted<JS::Value> rval(cx); |
84 | 0 | JS::Rooted<JS::Value> callback(cx, mCallback); |
85 | 0 | bool ok = JS_CallFunctionValue(cx, global, callback, JS::HandleValueArray(strVal), &rval); |
86 | 0 | NS_ENSURE_TRUE(ok, false); |
87 | 0 |
|
88 | 0 | return true; |
89 | 0 | } |
90 | | |
91 | | void |
92 | | TestShellCommandParent::ReleaseCallback() |
93 | 0 | { |
94 | 0 | mCallback.reset(); |
95 | 0 | } |
96 | | |
97 | | bool |
98 | | TestShellCommandParent::ExecuteCallback(const nsString& aResponse) |
99 | 0 | { |
100 | 0 | return static_cast<TestShellParent*>(Manager())->CommandDone( |
101 | 0 | this, aResponse); |
102 | 0 | } |
103 | | |
104 | | void |
105 | | TestShellCommandParent::ActorDestroy(ActorDestroyReason why) |
106 | 0 | { |
107 | 0 | if (why == AbnormalShutdown) { |
108 | 0 | ExecuteCallback(EmptyString()); |
109 | 0 | } |
110 | 0 | } |