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