1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is Mozilla IPCShell.
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.
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.
35 * ***** END LICENSE BLOCK ***** */
37 #include "TestShellParent.h"
38 #include "mozilla/dom/ContentParent.h"
39 #include "mozilla/jsipc/ContextWrapperParent.h"
41 #include "nsAutoPtr.h"
43 using mozilla::ipc::TestShellParent
;
44 using mozilla::ipc::TestShellCommandParent
;
45 using mozilla::ipc::PTestShellCommandParent
;
46 using mozilla::dom::ContentParent
;
47 using mozilla::jsipc::PContextWrapperParent
;
48 using mozilla::jsipc::ContextWrapperParent
;
50 PTestShellCommandParent
*
51 TestShellParent::AllocPTestShellCommand(const nsString
& aCommand
)
53 return new TestShellCommandParent();
57 TestShellParent::DeallocPTestShellCommand(PTestShellCommandParent
* aActor
)
64 TestShellParent::CommandDone(TestShellCommandParent
* command
,
65 const nsString
& aResponse
)
67 // XXX what should happen if the callback fails?
68 /*JSBool ok = */command
->RunCallback(aResponse
);
69 command
->ReleaseCallback();
74 PContextWrapperParent
*
75 TestShellParent::AllocPContextWrapper()
77 ContentParent
* cpp
= static_cast<ContentParent
*>(Manager());
78 return new ContextWrapperParent(cpp
);
82 TestShellParent::DeallocPContextWrapper(PContextWrapperParent
* actor
)
89 TestShellParent::GetGlobalJSObject(JSContext
* cx
, JSObject
** globalp
)
91 // TODO Unify this code with TabParent::GetGlobalJSObject.
92 InfallibleTArray
<PContextWrapperParent
*> cwps(1);
93 ManagedPContextWrapperParent(cwps
);
94 if (cwps
.Length() < 1)
96 NS_ASSERTION(cwps
.Length() == 1, "More than one PContextWrapper?");
97 ContextWrapperParent
* cwp
= static_cast<ContextWrapperParent
*>(cwps
[0]);
98 return cwp
->GetGlobalJSObject(cx
, globalp
);
102 TestShellCommandParent::SetCallback(JSContext
* aCx
,
105 if (!mCallback
.Hold(aCx
)) {
109 mCallback
= aCallback
;
116 TestShellCommandParent::RunCallback(const nsString
& aResponse
)
118 NS_ENSURE_TRUE(mCallback
!= JSVAL_NULL
&& mCx
, JS_FALSE
);
120 JSAutoRequest
ar(mCx
);
122 JSObject
* global
= JS_GetGlobalObject(mCx
);
123 NS_ENSURE_TRUE(global
, JS_FALSE
);
125 JSAutoEnterCompartment ac
;
126 if (!ac
.enter(mCx
, global
)) {
127 NS_ERROR("Failed to enter compartment!");
131 JSString
* str
= JS_NewUCStringCopyN(mCx
, aResponse
.get(), aResponse
.Length());
132 NS_ENSURE_TRUE(str
, JS_FALSE
);
134 jsval argv
[] = { STRING_TO_JSVAL(str
) };
135 int argc
= NS_ARRAY_LENGTH(argv
);
138 JSBool ok
= JS_CallFunctionValue(mCx
, global
, mCallback
, argc
, argv
, &rval
);
139 NS_ENSURE_TRUE(ok
, JS_FALSE
);
145 TestShellCommandParent::ReleaseCallback()