2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * Contributors, possibly:
16 * Kris Maglione <maglione.k at Gmail>
17 * Jeff Walden <jwalden@mit.edu>
18 * Mike Shaver <shaver@zeroknowledge.com>
19 * John Bandhauer <jband@netscape.com>
20 * Robert Ginda <rginda@netscape.com>
21 * Pierre Phaneuf <pp@ludusdesign.com>
23 * Alternatively, the contents of this file may be used under the terms of
24 * either of the GNU General Public License Version 2 or later (the "GPL"),
25 * or 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 *****
38 #include "dactylUtils.h"
43 #include "nsStringAPI.h"
46 static inline JSObject*
47 JS_GetGlobalForScopeChain(JSContext *cx) {
48 JSObject *callingScope = JS_GetScopeChain(cx);
51 return JS_GetGlobalForObject(cx, callingScope);
56 * Evil. Evil, evil, evil.
58 #define MOZILLA_INTERNAL_API
59 # define nsAString_h___
60 # define nsString_h___
61 # define nsStringFwd_h___
62 # define nsStringGlue_h__
63 # define nsContentUtils_h___
65 typedef nsString nsSubstring;
66 # include "nsIScrollableFrame.h"
67 #undef MOZILLA_INTERNAL_API
68 #include "nsPresContext.h"
69 #include "nsQueryFrame.h"
71 #include "nsIContent.h"
72 #include "nsIDOMXULElement.h"
73 #include "nsIXULTemplateBuilder.h"
74 #include "nsIObserverService.h"
75 #include "nsIScriptSecurityManager.h"
76 #include "nsIXPCScriptable.h"
78 #include "nsComponentManagerUtils.h"
79 #include "nsServiceManagerUtils.h"
82 class autoDropPrincipals {
84 autoDropPrincipals(JSContext *context, JSPrincipals *principals) : mContext(context), mJSPrincipals(principals) {}
85 ~autoDropPrincipals() {
86 JSPRINCIPALS_DROP(mContext, mJSPrincipals);
91 JSPrincipals *mJSPrincipals;
95 Dump(JSContext *cx, uintN argc, jsval *vp)
101 str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
106 const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length);
110 fputs(NS_ConvertUTF16toUTF8(reinterpret_cast<const PRUnichar*>(chars)).get(), stderr);
114 static JSFunctionSpec gGlobalFun[] = {
119 static dactylUtils* gService = nsnull;
121 dactylUtils::dactylUtils()
124 NS_ASSERTION(gService == nsnull, "Service already exists");
127 dactylUtils::~dactylUtils()
129 mRuntimeService = nsnull;
137 NS_ENSURE_TRUE(!gService, NS_ERROR_UNEXPECTED);
139 mRuntimeService = do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv);
140 NS_ENSURE_SUCCESS(rv, rv);
142 rv = mRuntimeService->GetRuntime(&mRuntime);
143 NS_ENSURE_SUCCESS(rv, rv);
145 nsCOMPtr<nsIScriptSecurityManager> secman =
146 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
147 NS_ENSURE_TRUE(secman, NS_ERROR_FAILURE);
149 rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
150 NS_ENSURE_TRUE(rv, rv);
151 NS_ENSURE_TRUE(mSystemPrincipal, NS_ERROR_FAILURE);
156 NS_IMPL_ISUPPORTS1(dactylUtils,
160 dactylUtils::CreateGlobal(JSContext *cx, jsval *out)
164 // JS::AutoPreserveCompartment pc(cx);
166 nsCOMPtr<nsIXPCScriptable> backstagePass;
167 rv = mRuntimeService->GetBackstagePass(getter_AddRefs(backstagePass));
168 NS_ENSURE_SUCCESS(rv, rv);
170 nsCOMPtr<nsIXPConnect> xpc =
171 do_GetService("@mozilla.org/js/xpc/XPConnect;1", &rv);
172 NS_ENSURE_SUCCESS(rv, rv);
174 // Make sure InitClassesWithNewWrappedGlobal() installs the
175 // backstage pass as the global in our compilation context.
176 JS_SetGlobalObject(cx, nsnull);
178 nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
179 rv = xpc->InitClassesWithNewWrappedGlobal(cx, backstagePass,
180 NS_GET_IID(nsISupports),
184 FLAG_SYSTEM_GLOBAL_OBJECT,
185 getter_AddRefs(holder));
186 NS_ENSURE_SUCCESS(rv, rv);
189 rv = holder->GetJSObject(&global);
190 NS_ENSURE_SUCCESS(rv, rv);
192 JSAutoEnterCompartment ac;
193 NS_ENSURE_TRUE(ac.enter(cx, global), NS_ERROR_FAILURE);
195 NS_ENSURE_TRUE(JS_DefineFunctions(cx, global, gGlobalFun),
197 NS_ENSURE_TRUE(JS_DefineProfilingFunctions(cx, global),
200 *out = OBJECT_TO_JSVAL(global);
205 dactylUtils::EvalInContext(const nsAString &aSource,
206 const jsval &aTarget,
207 const nsACString &aFilename,
214 nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
215 NS_ENSURE_SUCCESS(rv, rv);
219 if (!aFilename.IsEmpty())
220 filename.Assign(aFilename);
222 nsCOMPtr<nsIStackFrame> frame;
223 xpc->GetCurrentJSStack(getter_AddRefs(frame));
224 NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
225 frame->GetFilename(getter_Copies(filename));
226 frame->GetLineNumber(&aLineNumber);
230 NS_ENSURE_FALSE(JSVAL_IS_PRIMITIVE(aTarget), NS_ERROR_UNEXPECTED);
231 target = JSVAL_TO_OBJECT(aTarget);
234 JSObject *result = target;
235 target = JS_FindCompilationScope(cx, target);
236 NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
239 nsCOMPtr<nsIScriptSecurityManager> secman =
240 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
241 NS_ENSURE_TRUE(secman, NS_ERROR_FAILURE);
243 nsCOMPtr<nsIPrincipal> principal;
244 rv = secman->GetObjectPrincipal(cx, target, getter_AddRefs(principal));
245 NS_ENSURE_SUCCESS(rv, rv);
248 JSPrincipals *jsPrincipals;
249 rv = principal->GetJSPrincipals(cx, &jsPrincipals);
250 NS_ENSURE_SUCCESS(rv, rv);
251 autoDropPrincipals adp(cx, jsPrincipals);
253 JSObject *callingScope;
255 JSAutoRequest req(cx);
257 callingScope = JS_GetGlobalForScopeChain(cx);
258 NS_ENSURE_TRUE(callingScope, NS_ERROR_FAILURE);
262 JSAutoRequest req(cx);
263 JSAutoEnterCompartment ac;
266 NS_ENSURE_TRUE(ac.enter(cx, target), NS_ERROR_FAILURE);
269 JS_EvaluateUCScriptForPrincipals(cx, target,
271 reinterpret_cast<const jschar*>
272 (PromiseFlatString(aSource).get()),
274 filename.get(), aLineNumber, &v);
278 if (!JS_GetPendingException(cx, &exn))
279 rv = NS_ERROR_FAILURE;
281 JS_ClearPendingException(cx);
283 if (JS_WrapValue(cx, &exn))
284 JS_SetPendingException(cx, exn);
288 // Convert the result into something safe for our caller.
289 JSAutoRequest req(cx);
290 JSAutoEnterCompartment ac;
292 if (!ac.enter(cx, callingScope) || !JS_WrapValue(cx, &v))
293 rv = NS_ERROR_FAILURE;
295 if (NS_SUCCEEDED(rv))
304 dactylUtils::CreateContents(nsIDOMElement *aElement)
306 nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
308 for (nsIContent *element = content;
310 element = element->GetParent()) {
312 nsCOMPtr<nsIDOMXULElement> xulelem = do_QueryInterface(element);
314 nsCOMPtr<nsIXULTemplateBuilder> builder;
315 xulelem->GetBuilder(getter_AddRefs(builder));
317 builder->CreateContents(content, PR_TRUE);
325 namespace XPCWrapper {
326 extern JSObject *UnsafeUnwrapSecurityWrapper(JSObject *obj);
330 dactylUtils::GetGlobalForObject(const jsval &aObject,
336 NS_ENSURE_FALSE(JSVAL_IS_PRIMITIVE(aObject),
337 NS_ERROR_XPC_BAD_CONVERT_JS);
339 JSObject *obj = XPCWrapper::UnsafeUnwrapSecurityWrapper(JSVAL_TO_OBJECT(aObject));
341 JSObject *global = JS_GetGlobalForObject(cx, obj);
342 *rval = OBJECT_TO_JSVAL(global);
345 // Outerize if necessary.
346 if (JSObjectOp outerize = global->getClass()->ext.outerObject)
347 *rval = OBJECT_TO_JSVAL(outerize(cx, global));
354 dactylUtils::GetScrollable(nsIDOMElement *aElement, PRUint32 *rval)
356 nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
358 nsIFrame *frame = content->GetPrimaryFrame();
359 nsIScrollableFrame *scrollFrame = do_QueryFrame(frame);
363 nsPresContext::ScrollbarStyles ss = scrollFrame->GetScrollbarStyles();
364 PRUint32 scrollbarVisibility = scrollFrame->GetScrollbarVisibility();
365 nsRect scrollRange = scrollFrame->GetScrollRange();
367 if (ss.mHorizontal != NS_STYLE_OVERFLOW_HIDDEN &&
368 ((scrollbarVisibility & nsIScrollableFrame::HORIZONTAL) ||
369 scrollRange.width > 0))
370 *rval |= dactylIUtils::DIRECTION_HORIZONTAL;
372 if (ss.mVertical != NS_STYLE_OVERFLOW_HIDDEN &&
373 ((scrollbarVisibility & nsIScrollableFrame::VERTICAL) ||
374 scrollRange.height > 0))
375 *rval |= dactylIUtils::DIRECTION_VERTICAL;
381 /* vim:se sts=4 sw=4 et cin ft=cpp: */