1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is Mozilla IPCShell.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Ben Turner <bent.mozilla@gmail.com>.
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "TestShellParent.h"
38 :
39 : /* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */
40 : #include "mozilla/Util.h"
41 :
42 : #include "mozilla/dom/ContentParent.h"
43 : #include "mozilla/jsipc/ContextWrapperParent.h"
44 :
45 : #include "nsAutoPtr.h"
46 :
47 : using namespace mozilla;
48 : using mozilla::ipc::TestShellParent;
49 : using mozilla::ipc::TestShellCommandParent;
50 : using mozilla::ipc::PTestShellCommandParent;
51 : using mozilla::dom::ContentParent;
52 : using mozilla::jsipc::PContextWrapperParent;
53 : using mozilla::jsipc::ContextWrapperParent;
54 :
55 : PTestShellCommandParent*
56 0 : TestShellParent::AllocPTestShellCommand(const nsString& aCommand)
57 : {
58 0 : return new TestShellCommandParent();
59 : }
60 :
61 : bool
62 0 : TestShellParent::DeallocPTestShellCommand(PTestShellCommandParent* aActor)
63 : {
64 0 : delete aActor;
65 0 : return true;
66 : }
67 :
68 : bool
69 0 : TestShellParent::CommandDone(TestShellCommandParent* command,
70 : const nsString& aResponse)
71 : {
72 : // XXX what should happen if the callback fails?
73 0 : /*JSBool ok = */command->RunCallback(aResponse);
74 0 : command->ReleaseCallback();
75 :
76 0 : return true;
77 : }
78 :
79 : PContextWrapperParent*
80 0 : TestShellParent::AllocPContextWrapper()
81 : {
82 0 : ContentParent* cpp = static_cast<ContentParent*>(Manager());
83 0 : return new ContextWrapperParent(cpp);
84 : }
85 :
86 : bool
87 0 : TestShellParent::DeallocPContextWrapper(PContextWrapperParent* actor)
88 : {
89 0 : delete actor;
90 0 : return true;
91 : }
92 :
93 : JSBool
94 0 : TestShellParent::GetGlobalJSObject(JSContext* cx, JSObject** globalp)
95 : {
96 : // TODO Unify this code with TabParent::GetGlobalJSObject.
97 0 : InfallibleTArray<PContextWrapperParent*> cwps(1);
98 0 : ManagedPContextWrapperParent(cwps);
99 0 : if (cwps.Length() < 1)
100 0 : return JS_FALSE;
101 0 : NS_ASSERTION(cwps.Length() == 1, "More than one PContextWrapper?");
102 0 : ContextWrapperParent* cwp = static_cast<ContextWrapperParent*>(cwps[0]);
103 0 : return cwp->GetGlobalJSObject(cx, globalp);
104 : }
105 :
106 : JSBool
107 0 : TestShellCommandParent::SetCallback(JSContext* aCx,
108 : jsval aCallback)
109 : {
110 0 : if (!mCallback.Hold(aCx)) {
111 0 : return JS_FALSE;
112 : }
113 :
114 0 : mCallback = aCallback;
115 0 : mCx = aCx;
116 :
117 0 : return JS_TRUE;
118 : }
119 :
120 : JSBool
121 0 : TestShellCommandParent::RunCallback(const nsString& aResponse)
122 : {
123 0 : NS_ENSURE_TRUE(*mCallback.ToJSValPtr() != JSVAL_NULL && mCx, JS_FALSE);
124 :
125 0 : JSAutoRequest ar(mCx);
126 :
127 0 : JSObject* global = JS_GetGlobalObject(mCx);
128 0 : NS_ENSURE_TRUE(global, JS_FALSE);
129 :
130 0 : JSAutoEnterCompartment ac;
131 0 : if (!ac.enter(mCx, global)) {
132 0 : NS_ERROR("Failed to enter compartment!");
133 0 : return false;
134 : }
135 :
136 0 : JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length());
137 0 : NS_ENSURE_TRUE(str, JS_FALSE);
138 :
139 0 : jsval argv[] = { STRING_TO_JSVAL(str) };
140 0 : unsigned argc = ArrayLength(argv);
141 :
142 : jsval rval;
143 0 : JSBool ok = JS_CallFunctionValue(mCx, global, mCallback, argc, argv, &rval);
144 0 : NS_ENSURE_TRUE(ok, JS_FALSE);
145 :
146 0 : return JS_TRUE;
147 : }
148 :
149 : void
150 0 : TestShellCommandParent::ReleaseCallback()
151 : {
152 0 : mCallback.Release();
153 0 : }
154 :
155 : bool
156 0 : TestShellCommandParent::ExecuteCallback(const nsString& aResponse)
157 : {
158 0 : return static_cast<TestShellParent*>(Manager())->CommandDone(
159 0 : this, aResponse);
160 : }
161 :
162 : void
163 0 : TestShellCommandParent::ActorDestroy(ActorDestroyReason why)
164 : {
165 0 : if (why == AbnormalShutdown) {
166 0 : ExecuteCallback(EmptyString());
167 : }
168 0 : }
|