]> git.donarmstrong.com Git - dactyl.git/blob - binary/src/dactylUtils.cpp
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / binary / src / dactylUtils.cpp
1 /*
2  * ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
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>
22  *
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.
34  *
35  * ***** END LICENSE BLOCK *****
36  */
37
38 #include "dactylUtils.h"
39
40 #include "jsdbgapi.h"
41 // #include "jsobj.h"
42
43 #include "nsStringAPI.h"
44
45 #if GECKO_MAJOR < 10
46     static inline JSObject*
47     JS_GetGlobalForScopeChain(JSContext *cx) {
48         JSObject *callingScope = JS_GetScopeChain(cx);
49         if (!callingScope)
50             return nsnull;
51         return JS_GetGlobalForObject(cx, callingScope);
52     }
53 #endif
54
55 /*
56  * Evil. Evil, evil, evil.
57  */
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___
64 class nsAFlatCString;
65 typedef nsString nsSubstring;
66 #  include "nsIScrollableFrame.h"
67 #undef MOZILLA_INTERNAL_API
68 #include "nsPresContext.h"
69 #include "nsQueryFrame.h"
70
71 #include "nsIContent.h"
72 #include "nsIDOMXULElement.h"
73 #include "nsIXULTemplateBuilder.h"
74 #include "nsIObserverService.h"
75 #include "nsIScriptSecurityManager.h"
76 #include "nsIXPCScriptable.h"
77
78 #include "nsComponentManagerUtils.h"
79 #include "nsServiceManagerUtils.h"
80
81
82 class autoDropPrincipals {
83 public:
84     autoDropPrincipals(JSContext *context, JSPrincipals *principals) : mContext(context), mJSPrincipals(principals) {}
85     ~autoDropPrincipals() {
86         JSPRINCIPALS_DROP(mContext, mJSPrincipals);
87     }
88
89 private:
90     JSContext *mContext;
91     JSPrincipals *mJSPrincipals;
92 };
93
94 static JSBool
95 Dump(JSContext *cx, uintN argc, jsval *vp)
96 {
97     JSString *str;
98     if (!argc)
99         return JS_TRUE;
100
101     str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
102     if (!str)
103         return JS_FALSE;
104
105     size_t length;
106     const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length);
107     if (!chars)
108         return JS_FALSE;
109
110     fputs(NS_ConvertUTF16toUTF8(reinterpret_cast<const PRUnichar*>(chars)).get(), stderr);
111     return JS_TRUE;
112 }
113
114 static JSFunctionSpec gGlobalFun[] = {
115     {"dump",    Dump,   1,0},
116     {nsnull,nsnull,0,0}
117 };
118
119 static dactylUtils* gService = nsnull;
120
121 dactylUtils::dactylUtils()
122     : mRuntime(nsnull)
123 {
124     NS_ASSERTION(gService == nsnull, "Service already exists");
125 }
126
127 dactylUtils::~dactylUtils()
128 {
129     mRuntimeService = nsnull;
130     gService = nsnull;
131 }
132
133 nsresult
134 dactylUtils::Init()
135 {
136     nsresult rv;
137     NS_ENSURE_TRUE(!gService, NS_ERROR_UNEXPECTED);
138
139     mRuntimeService = do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv);
140     NS_ENSURE_SUCCESS(rv, rv);
141
142     rv = mRuntimeService->GetRuntime(&mRuntime);
143     NS_ENSURE_SUCCESS(rv, rv);
144
145     nsCOMPtr<nsIScriptSecurityManager> secman =
146         do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
147     NS_ENSURE_TRUE(secman, NS_ERROR_FAILURE);
148
149     rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
150     NS_ENSURE_TRUE(rv, rv);
151     NS_ENSURE_TRUE(mSystemPrincipal, NS_ERROR_FAILURE);
152
153     return NS_OK;
154 }
155
156 NS_IMPL_ISUPPORTS1(dactylUtils,
157                    dactylIUtils)
158
159 NS_IMETHODIMP
160 dactylUtils::CreateGlobal(JSContext *cx, jsval *out)
161 {
162     nsresult rv;
163
164     // JS::AutoPreserveCompartment pc(cx);
165
166     nsCOMPtr<nsIXPCScriptable> backstagePass;
167     rv = mRuntimeService->GetBackstagePass(getter_AddRefs(backstagePass));
168     NS_ENSURE_SUCCESS(rv, rv);
169
170     nsCOMPtr<nsIXPConnect> xpc =
171         do_GetService("@mozilla.org/js/xpc/XPConnect;1", &rv);
172     NS_ENSURE_SUCCESS(rv, rv);
173
174     // Make sure InitClassesWithNewWrappedGlobal() installs the
175     // backstage pass as the global in our compilation context.
176     JS_SetGlobalObject(cx, nsnull);
177
178     nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
179     rv = xpc->InitClassesWithNewWrappedGlobal(cx, backstagePass,
180                                               NS_GET_IID(nsISupports),
181                                               mSystemPrincipal,
182                                               nsnull,
183                                               nsIXPConnect::
184                                                   FLAG_SYSTEM_GLOBAL_OBJECT,
185                                               getter_AddRefs(holder));
186     NS_ENSURE_SUCCESS(rv, rv);
187
188     JSObject *global;
189     rv = holder->GetJSObject(&global);
190     NS_ENSURE_SUCCESS(rv, rv);
191
192     JSAutoEnterCompartment ac;
193     NS_ENSURE_TRUE(ac.enter(cx, global), NS_ERROR_FAILURE);
194
195     NS_ENSURE_TRUE(JS_DefineFunctions(cx, global, gGlobalFun),
196                    NS_ERROR_FAILURE);
197     NS_ENSURE_TRUE(JS_DefineProfilingFunctions(cx, global),
198                    NS_ERROR_FAILURE);
199
200     *out = OBJECT_TO_JSVAL(global);
201     return NS_OK;
202 }
203
204 NS_IMETHODIMP
205 dactylUtils::EvalInContext(const nsAString &aSource,
206                            const jsval &aTarget,
207                            const nsACString &aFilename,
208                            PRInt32 aLineNumber,
209                            JSContext *cx,
210                            jsval *rval)
211 {
212     nsresult rv;
213
214     nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
215     NS_ENSURE_SUCCESS(rv, rv);
216
217     nsCString filename;
218
219     if (!aFilename.IsEmpty())
220         filename.Assign(aFilename);
221     else {
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);
227     }
228
229     JSObject *target;
230     NS_ENSURE_FALSE(JSVAL_IS_PRIMITIVE(aTarget), NS_ERROR_UNEXPECTED);
231     target = JSVAL_TO_OBJECT(aTarget);
232
233
234     JSObject *result = target;
235     target = JS_FindCompilationScope(cx, target);
236     NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
237
238
239     nsCOMPtr<nsIScriptSecurityManager> secman =
240         do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
241     NS_ENSURE_TRUE(secman, NS_ERROR_FAILURE);
242
243     nsCOMPtr<nsIPrincipal> principal;
244     rv = secman->GetObjectPrincipal(cx, target, getter_AddRefs(principal));
245     NS_ENSURE_SUCCESS(rv, rv);
246
247
248     JSPrincipals *jsPrincipals;
249     rv = principal->GetJSPrincipals(cx, &jsPrincipals);
250     NS_ENSURE_SUCCESS(rv, rv);
251     autoDropPrincipals adp(cx, jsPrincipals);
252
253     JSObject *callingScope;
254     {
255         JSAutoRequest req(cx);
256
257         callingScope = JS_GetGlobalForScopeChain(cx);
258         NS_ENSURE_TRUE(callingScope, NS_ERROR_FAILURE);
259     }
260
261     {
262         JSAutoRequest req(cx);
263         JSAutoEnterCompartment ac;
264         jsval v;
265
266         NS_ENSURE_TRUE(ac.enter(cx, target), NS_ERROR_FAILURE);
267
268         JSBool ok =
269             JS_EvaluateUCScriptForPrincipals(cx, target,
270                                              jsPrincipals,
271                                              reinterpret_cast<const jschar*>
272                                                 (PromiseFlatString(aSource).get()),
273                                              aSource.Length(),
274                                              filename.get(), aLineNumber, &v);
275
276         if (!ok) {
277             jsval exn;
278             if (!JS_GetPendingException(cx, &exn))
279                 rv = NS_ERROR_FAILURE;
280             else {
281                 JS_ClearPendingException(cx);
282
283                 if (JS_WrapValue(cx, &exn))
284                     JS_SetPendingException(cx, exn);
285             }
286         }
287         else {
288             // Convert the result into something safe for our caller.
289             JSAutoRequest req(cx);
290             JSAutoEnterCompartment ac;
291
292             if (!ac.enter(cx, callingScope) || !JS_WrapValue(cx, &v))
293                 rv = NS_ERROR_FAILURE;
294
295             if (NS_SUCCEEDED(rv))
296                 *rval = v;
297         }
298     }
299
300     return rv;
301 }
302
303 NS_IMETHODIMP
304 dactylUtils::CreateContents(nsIDOMElement *aElement)
305 {
306     nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
307
308     for (nsIContent *element = content;
309          element;
310          element = element->GetParent()) {
311
312         nsCOMPtr<nsIDOMXULElement> xulelem = do_QueryInterface(element);
313         if (xulelem) {
314             nsCOMPtr<nsIXULTemplateBuilder> builder;
315             xulelem->GetBuilder(getter_AddRefs(builder));
316             if (builder) {
317                 builder->CreateContents(content, PR_TRUE);
318                 break;
319             }
320         }
321     }
322     return NS_OK;
323 }
324
325 namespace XPCWrapper {
326     extern JSObject *UnsafeUnwrapSecurityWrapper(JSObject *obj);
327 };
328
329 NS_IMETHODIMP
330 dactylUtils::GetGlobalForObject(const jsval &aObject,
331                                 JSContext *cx,
332                                 jsval *rval)
333 {
334     nsresult rv;
335
336     NS_ENSURE_FALSE(JSVAL_IS_PRIMITIVE(aObject),
337                     NS_ERROR_XPC_BAD_CONVERT_JS);
338
339     JSObject *obj = XPCWrapper::UnsafeUnwrapSecurityWrapper(JSVAL_TO_OBJECT(aObject));
340
341     JSObject *global = JS_GetGlobalForObject(cx, obj);
342     *rval = OBJECT_TO_JSVAL(global);
343
344     /*
345     // Outerize if necessary.
346     if (JSObjectOp outerize = global->getClass()->ext.outerObject)
347         *rval = OBJECT_TO_JSVAL(outerize(cx, global));
348     */
349
350     return NS_OK;
351 }
352
353 NS_IMETHODIMP
354 dactylUtils::GetScrollable(nsIDOMElement *aElement, PRUint32 *rval)
355 {
356     nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
357
358     nsIFrame *frame = content->GetPrimaryFrame();
359     nsIScrollableFrame *scrollFrame = do_QueryFrame(frame);
360
361     *rval = 0;
362     if (scrollFrame) {
363         nsPresContext::ScrollbarStyles ss = scrollFrame->GetScrollbarStyles();
364         PRUint32 scrollbarVisibility = scrollFrame->GetScrollbarVisibility();
365         nsRect scrollRange = scrollFrame->GetScrollRange();
366
367         if (ss.mHorizontal != NS_STYLE_OVERFLOW_HIDDEN &&
368              ((scrollbarVisibility & nsIScrollableFrame::HORIZONTAL) ||
369               scrollRange.width > 0))
370             *rval |= dactylIUtils::DIRECTION_HORIZONTAL;
371
372         if (ss.mVertical != NS_STYLE_OVERFLOW_HIDDEN &&
373              ((scrollbarVisibility & nsIScrollableFrame::VERTICAL) ||
374               scrollRange.height > 0))
375             *rval |= dactylIUtils::DIRECTION_VERTICAL;
376     }
377
378     return NS_OK;
379 }
380
381 /* vim:se sts=4 sw=4 et cin ft=cpp: */