]> git.donarmstrong.com Git - dactyl.git/blob - binary/src/subscriptLoader.cpp
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / binary / src / subscriptLoader.cpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=4 sw=4 et tw=80:
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is Mozilla Communicator client code, released
18  * March 31, 1998.
19  *
20  * The Initial Developer of the Original Code is
21  * Netscape Communications Corporation.
22  * Portions created by the Initial Developer are Copyright (C) 1998
23  * the Initial Developer. All Rights Reserved.
24  *
25  * Contributor(s):
26  *   Robert Ginda <rginda@netscape.com>
27  *   Kris Maglione <maglione.k@gmail.com>
28  *
29  * Alternatively, the contents of this file may be used under the terms of
30  * either of the GNU General Public License Version 2 or later (the "GPL"),
31  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32  * in which case the provisions of the GPL or the LGPL are applicable instead
33  * of those above. If you wish to allow use of your version of this file only
34  * under the terms of either the GPL or the LGPL, and not to allow others to
35  * use your version of this file under the terms of the MPL, indicate your
36  * decision by deleting the provisions above and replace them with the notice
37  * and other provisions required by the GPL or the LGPL. If you do not delete
38  * the provisions above, a recipient may use your version of this file under
39  * the terms of any one of the MPL, the GPL or the LGPL.
40  *
41  * ***** END LICENSE BLOCK ***** */
42
43 /*
44  * What follows is the ordinary mozIJSSubScriptLoader modified only to strip the
45  * useless, irritating, and troublesome "foo -> " prefixes from filenames.
46  */
47
48 #include "dactylUtils.h"
49 #include "mozJSLoaderUtils.h"
50
51 #include "nsIServiceManager.h"
52 #include "nsIXPConnect.h"
53
54 #include "nsIURI.h"
55 #include "nsIIOService.h"
56 #include "nsIChannel.h"
57 #include "nsIConsoleService.h"
58 #include "nsIScriptError.h"
59 #include "nsIInputStream.h"
60 #include "nsNetCID.h"
61 #include "nsAutoPtr.h"
62 #include "nsNetUtil.h"
63 #include "nsIProtocolHandler.h"
64 #include "nsIScriptSecurityManager.h"
65 #include "nsIFileURL.h"
66
67 // ConvertToUTF16
68 #include "nsICharsetConverterManager.h"
69 #include "nsIUnicodeDecoder.h"
70 template<class T>
71 inline PRBool EnsureStringLength(T& aStr, PRUint32 aLen)
72 {
73     aStr.SetLength(aLen);
74     return (aStr.Length() == aLen);
75 }
76
77 #include "jsapi.h"
78 #include "jscntxt.h"
79 #include "jsdbgapi.h"
80 #include "jsfriendapi.h"
81
82 #if GECKO_MAJOR < 10
83     static inline JSVersion
84     JS_GetVersion(JSContext *cx) {
85         return cx->findVersion();
86     }
87 #endif
88
89 #include "mozilla/FunctionTimer.h"
90 #include "mozilla/scache/StartupCache.h"
91 #include "mozilla/scache/StartupCacheUtils.h"
92
93 using namespace mozilla::scache;
94 class nsACString_internal : nsACString {};
95
96 namespace mozilla {
97     namespace scache {
98         nsresult PathifyURI(nsIURI *in, nsACString_internal &out);
99     }
100 }
101
102 /* load() error msgs, XXX localize? */
103 #define LOAD_ERROR_NOSERVICE "Error creating IO Service."
104 #define LOAD_ERROR_NOURI "Error creating URI (invalid URL scheme?)"
105 #define LOAD_ERROR_NOSCHEME "Failed to get URI scheme.  This is bad."
106 #define LOAD_ERROR_URI_NOT_LOCAL "Trying to load a non-local URI."
107 #define LOAD_ERROR_NOSTREAM  "Error opening input stream (invalid filename?)"
108 #define LOAD_ERROR_NOCONTENT "ContentLength not available (not a local URL?)"
109 #define LOAD_ERROR_BADCHARSET "Error converting to specified charset"
110 #define LOAD_ERROR_BADREAD   "File Read Error."
111 #define LOAD_ERROR_READUNDERFLOW "File Read Error (underflow.)"
112 #define LOAD_ERROR_NOPRINCIPALS "Failed to get principals."
113 #define LOAD_ERROR_NOSPEC "Failed to get URI spec.  This is bad."
114
115 /* Internal linkage, bloody hell... */
116 static nsresult
117 ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData,
118                PRUint32 aLength, const nsString& aHintCharset,
119                nsString& aString)
120 {
121   if (!aLength) {
122     aString.Truncate();
123     return NS_OK;
124   }
125
126   nsCAutoString characterSet;
127
128   nsresult rv = NS_OK;
129   if (aChannel) {
130     rv = aChannel->GetContentCharset(characterSet);
131   }
132
133   if (!aHintCharset.IsEmpty() && (NS_FAILED(rv) || characterSet.IsEmpty())) {
134     // charset name is always ASCII.
135     LossyCopyUTF16toASCII(aHintCharset, characterSet);
136   }
137
138   if (characterSet.IsEmpty()) {
139     // fall back to ISO-8859-1, see bug 118404
140     characterSet.AssignLiteral("ISO-8859-1");
141   }
142
143   nsCOMPtr<nsICharsetConverterManager> charsetConv =
144     do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
145
146   nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
147
148   if (NS_SUCCEEDED(rv) && charsetConv) {
149     rv = charsetConv->GetUnicodeDecoder(characterSet.get(),
150                                         getter_AddRefs(unicodeDecoder));
151     if (NS_FAILED(rv)) {
152       // fall back to ISO-8859-1 if charset is not supported. (bug 230104)
153       rv = charsetConv->GetUnicodeDecoderRaw("ISO-8859-1",
154                                              getter_AddRefs(unicodeDecoder));
155     }
156   }
157
158   // converts from the charset to unicode
159   if (NS_SUCCEEDED(rv)) {
160     PRInt32 unicodeLength = 0;
161
162     rv = unicodeDecoder->GetMaxLength(reinterpret_cast<const char*>(aData),
163                                       aLength, &unicodeLength);
164     if (NS_SUCCEEDED(rv)) {
165       if (!EnsureStringLength(aString, unicodeLength))
166         return NS_ERROR_OUT_OF_MEMORY;
167
168       PRUnichar *ustr = aString.BeginWriting();
169
170       PRInt32 consumedLength = 0;
171       PRInt32 originalLength = aLength;
172       PRInt32 convertedLength = 0;
173       PRInt32 bufferLength = unicodeLength;
174       do {
175         rv = unicodeDecoder->Convert(reinterpret_cast<const char*>(aData),
176                                      (PRInt32 *) &aLength, ustr,
177                                      &unicodeLength);
178         if (NS_FAILED(rv)) {
179           // if we failed, we consume one byte, replace it with U+FFFD
180           // and try the conversion again.
181           ustr[unicodeLength++] = (PRUnichar)0xFFFD;
182           ustr += unicodeLength;
183
184           unicodeDecoder->Reset();
185         }
186         aData += ++aLength;
187         consumedLength += aLength;
188         aLength = originalLength - consumedLength;
189         convertedLength += unicodeLength;
190         unicodeLength = bufferLength - convertedLength;
191       } while (NS_FAILED(rv) && (originalLength > consumedLength) && (bufferLength > convertedLength));
192       aString.SetLength(convertedLength);
193     }
194   }
195   return rv;
196 }
197
198 // We just use the same reporter as the component loader
199 static void
200 mozJSLoaderErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep)
201 {
202     nsresult rv;
203
204     /* Use the console service to register the error. */
205     nsCOMPtr<nsIConsoleService> consoleService =
206         do_GetService(NS_CONSOLESERVICE_CONTRACTID);
207
208     /*
209      * Make an nsIScriptError, populate it with information from this
210      * error, then log it with the console service.  The UI can then
211      * poll the service to update the Error console.
212      */
213     nsCOMPtr<nsIScriptError> errorObject =
214         do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
215
216     if (consoleService && errorObject) {
217         /*
218          * Got an error object; prepare appropriate-width versions of
219          * various arguments to it.
220          */
221         nsAutoString fileUni;
222         fileUni.Assign(NS_ConvertUTF8toUTF16(rep->filename));
223
224         PRUint32 column = rep->uctokenptr - rep->uclinebuf;
225
226         rv = errorObject->Init(reinterpret_cast<const PRUnichar*>
227                                                (rep->ucmessage),
228                                fileUni.get(),
229                                reinterpret_cast<const PRUnichar*>
230                                                (rep->uclinebuf),
231                                rep->lineno, column, rep->flags,
232                                "component javascript");
233         if (NS_SUCCEEDED(rv)) {
234             rv = consoleService->LogMessage(errorObject);
235             if (NS_SUCCEEDED(rv)) {
236                 // We're done!  Skip return to fall thru to stderr
237                 // printout, for the benefit of those invoking the
238                 // browser with -console
239                 // return;
240             }
241         }
242     }
243
244     /*
245      * If any of the above fails for some reason, fall back to
246      * printing to stderr.
247      */
248 }
249
250 static JSBool
251 Dump(JSContext *cx, uintN argc, jsval *vp)
252 {
253     JSString *str;
254     if (!argc)
255         return JS_TRUE;
256
257     str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
258     if (!str)
259         return JS_FALSE;
260
261     size_t length;
262     const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length);
263     if (!chars)
264         return JS_FALSE;
265
266     fputs(NS_ConvertUTF16toUTF8(reinterpret_cast<const PRUnichar*>(chars)).get(), stderr);
267     return JS_TRUE;
268 }
269
270 static nsresult
271 ReportError(JSContext *cx, const char *msg)
272 {
273     JS_SetPendingException(cx, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, msg)));
274     return NS_OK;
275 }
276
277 static nsresult
278 ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj,
279            jschar *charset, const char *uriStr,
280            nsIIOService *serv, nsIPrincipal *principal,
281            JSScriptType **scriptObjp)
282 {
283     nsCOMPtr<nsIChannel>     chan;
284     nsCOMPtr<nsIInputStream> instream;
285     JSPrincipals    *jsPrincipals;
286     JSErrorReporter  er;
287
288     nsresult rv;
289     // Instead of calling NS_OpenURI, we create the channel ourselves and call
290     // SetContentType, to avoid expensive MIME type lookups (bug 632490).
291     rv = NS_NewChannel(getter_AddRefs(chan), uri, serv,
292                        nsnull, nsnull, nsIRequest::LOAD_NORMAL);
293     if (NS_SUCCEEDED(rv)) {
294         chan->SetContentType(NS_LITERAL_CSTRING("application/javascript"));
295         rv = chan->Open(getter_AddRefs(instream));
296     }
297
298     if (NS_FAILED(rv)) {
299         return ReportError(cx, LOAD_ERROR_NOSTREAM);
300     }
301
302     PRInt32 len = -1;
303
304     rv = chan->GetContentLength(&len);
305     if (NS_FAILED(rv) || len == -1) {
306         return ReportError(cx, LOAD_ERROR_NOCONTENT);
307     }
308
309     nsCString buf;
310     rv = NS_ReadInputStreamToString(instream, buf, len);
311     if (NS_FAILED(rv))
312         return rv;
313
314     /* we can't hold onto jsPrincipals as a module var because the
315      * JSPRINCIPALS_DROP macro takes a JSContext, which we won't have in the
316      * destructor */
317     rv = principal->GetJSPrincipals(cx, &jsPrincipals);
318     if (NS_FAILED(rv) || !jsPrincipals) {
319         return ReportError(cx, LOAD_ERROR_NOPRINCIPALS);
320     }
321
322     /* set our own error reporter so we can report any bad things as catchable
323      * exceptions, including the source/line number */
324     er = JS_SetErrorReporter(cx, mozJSLoaderErrorReporter);
325
326     if (charset) {
327         nsString script;
328         rv = ConvertToUTF16(
329                 nsnull, reinterpret_cast<const PRUint8*>(buf.get()), len,
330                 nsDependentString(reinterpret_cast<PRUnichar*>(charset)), script);
331
332         if (NS_FAILED(rv)) {
333             JSPRINCIPALS_DROP(cx, jsPrincipals);
334             return ReportError(cx, LOAD_ERROR_BADCHARSET);
335         }
336
337         *scriptObjp =
338             JS_CompileUCScriptForPrincipals(cx, target_obj, jsPrincipals,
339                                             reinterpret_cast<const jschar*>(script.get()),
340                                             script.Length(), uriStr, 1);
341     } else {
342         *scriptObjp =
343             JS_CompileScriptForPrincipals(cx, target_obj, jsPrincipals, buf.get(),
344                                           len, uriStr, 1);
345     }
346
347     JSPRINCIPALS_DROP(cx, jsPrincipals);
348
349     /* repent for our evil deeds */
350     JS_SetErrorReporter(cx, er);
351
352     return NS_OK;
353 }
354
355 NS_IMETHODIMP /* args and return value are delt with using XPConnect and JSAPI */
356 dactylUtils::LoadSubScript (const PRUnichar * aURL
357                             /* [, JSObject *target_obj] */)
358 {
359     /*
360      * Loads a local url and evals it into the current cx
361      * Synchronous (an async version would be cool too.)
362      *   url: The url to load.  Must be local so that it can be loaded
363      *        synchronously.
364      *   target_obj: Optional object to eval the script onto (defaults to context
365      *               global)
366      *   returns: Whatever jsval the script pointed to by the url returns.
367      * Should ONLY (O N L Y !) be called from JavaScript code.
368      */
369
370     /* gotta define most of this stuff up here because of all the gotos,
371      * defined the rest up here to be consistent */
372     nsresult  rv;
373     JSBool    ok;
374
375 #ifdef NS_FUNCTION_TIMER
376     NS_TIME_FUNCTION_FMT("%s (line %d) (url: %s)", MOZ_FUNCTION_NAME,
377                          __LINE__, NS_LossyConvertUTF16toASCII(aURL).get());
378 #else
379     (void)aURL; // prevent compiler warning
380 #endif
381
382     /* get JS things from the CallContext */
383     nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
384     if (!xpc) return NS_ERROR_FAILURE;
385
386     nsAXPCNativeCallContext *cc = nsnull;
387     rv = xpc->GetCurrentNativeCallContext(&cc);
388     if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
389
390     JSContext *cx;
391     rv = cc->GetJSContext (&cx);
392     if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
393
394     PRUint32 argc;
395     rv = cc->GetArgc (&argc);
396     if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
397
398     jsval *argv;
399     rv = cc->GetArgvPtr (&argv);
400     if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
401
402     jsval *rval;
403     rv = cc->GetRetValPtr (&rval);
404     if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
405
406     /* set mJSPrincipals if it's not here already */
407     if (!mSystemPrincipal)
408     {
409         nsCOMPtr<nsIScriptSecurityManager> secman =
410             do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
411         if (!secman)
412             return rv;
413
414         rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
415         if (NS_FAILED(rv) || !mSystemPrincipal)
416             return rv;
417     }
418
419     JSAutoRequest ar(cx);
420
421     JSString *url;
422     JSObject *target_obj = nsnull;
423     jschar   *charset = nsnull;
424     ok = JS_ConvertArguments (cx, argc, argv, "S / o W", &url, &target_obj, &charset);
425     if (!ok)
426     {
427         /* let the exception raised by JS_ConvertArguments show through */
428         return NS_OK;
429     }
430
431     JSAutoByteString urlbytes(cx, url);
432     if (!urlbytes)
433     {
434         return NS_OK;
435     }
436
437     if (!target_obj)
438     {
439         /* if the user didn't provide an object to eval onto, find the global
440          * object by walking the parent chain of the calling object */
441
442         nsCOMPtr<nsIXPConnectWrappedNative> wn;
443         rv = cc->GetCalleeWrapper (getter_AddRefs(wn));
444         if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
445
446         rv = wn->GetJSObject (&target_obj);
447         if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
448
449         JSObject *maybe_glob = JS_GetParent (cx, target_obj);
450         while (maybe_glob != nsnull)
451         {
452             target_obj = maybe_glob;
453             maybe_glob = JS_GetParent (cx, maybe_glob);
454         }
455     }
456
457     // Remember an object out of the calling compartment so that we
458     // can properly wrap the result later.
459     nsCOMPtr<nsIPrincipal> principal = mSystemPrincipal;
460     JSObject *result_obj = target_obj;
461     target_obj = JS_FindCompilationScope(cx, target_obj);
462     if (!target_obj)
463         return NS_ERROR_FAILURE;
464
465     if (target_obj != result_obj)
466     {
467         nsCOMPtr<nsIScriptSecurityManager> secman =
468             do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
469         if (!secman)
470             return NS_ERROR_FAILURE;
471
472         rv = secman->GetObjectPrincipal(cx, target_obj, getter_AddRefs(principal));
473         NS_ENSURE_SUCCESS(rv, rv);
474     }
475
476     JSAutoEnterCompartment ac;
477     if (!ac.enter(cx, target_obj))
478         return NS_ERROR_UNEXPECTED;
479
480     /* load up the url.  From here on, failures are reflected as ``custom''
481      * js exceptions */
482     nsCOMPtr<nsIURI> uri;
483     nsCAutoString uriStr;
484     nsCAutoString scheme;
485
486     JSStackFrame* frame = nsnull;
487     JSScript* script = nsnull;
488
489     // Figure out who's calling us
490     do
491     {
492         frame = JS_FrameIterator(cx, &frame);
493
494         if (frame)
495             script = JS_GetFrameScript(cx, frame);
496     } while (frame && !script);
497
498     if (!script)
499     {
500         // No script means we don't know who's calling, bail.
501
502         return NS_ERROR_FAILURE;
503     }
504
505     nsCOMPtr<nsIIOService> serv = do_GetService(NS_IOSERVICE_CONTRACTID);
506     if (!serv) {
507         return ReportError(cx, LOAD_ERROR_NOSERVICE);
508     }
509
510     // Make sure to explicitly create the URI, since we'll need the
511     // canonicalized spec.
512     rv = NS_NewURI(getter_AddRefs(uri), urlbytes.ptr(), nsnull, serv);
513     if (NS_FAILED(rv)) {
514         return ReportError(cx, LOAD_ERROR_NOURI);
515     }
516
517     rv = uri->GetSpec(uriStr);
518     if (NS_FAILED(rv)) {
519         return ReportError(cx, LOAD_ERROR_NOSPEC);
520     }
521
522     rv = uri->GetScheme(scheme);
523     if (NS_FAILED(rv)) {
524         return ReportError(cx, LOAD_ERROR_NOSCHEME);
525     }
526
527     bool writeScript = false;
528     JSScriptType *scriptObj = nsnull;
529     JSVersion version = JS_GetVersion(cx);
530
531     nsCAutoString cachePath;
532     cachePath.Append("jssubloader/");
533     cachePath.AppendInt(version);
534     if (charset) {
535         cachePath.Append("/");
536         cachePath.Append(NS_ConvertUTF16toUTF8(
537                     nsDependentString(reinterpret_cast<PRUnichar*>(charset))));
538     }
539
540     if (false)
541         // This is evil. Very evil. Unfortunately, the PathifyURI symbol is
542         // exported, but with an argument type that we don't have access to.
543         PathifyURI(uri, *(nsACString_internal*)&cachePath);
544     else {
545         cachePath.Append("/");
546         cachePath.Append(uriStr);
547     }
548
549     // Suppress caching if we're compiling as content.
550     nsCOMPtr<nsIStartupCache> cache;
551     if (mSystemPrincipal) {
552         cache = do_GetService("@mozilla.org/startupcache/cache;1", &rv);
553         NS_ENSURE_SUCCESS(rv, rv);
554         rv = ReadCachedScript(cache, cachePath, cx, &scriptObj);
555     }
556
557     if (!scriptObj) {
558         rv = ReadScript(uri, cx, target_obj, charset, (char *)uriStr.get(), serv,
559                         principal, &scriptObj);
560         writeScript = true;
561     }
562
563     if (NS_FAILED(rv) || !scriptObj)
564         return rv;
565
566     ok = false;
567     if (scriptObj)
568         ok = JS_ExecuteScriptVersion(cx, target_obj, scriptObj, rval, version);
569
570     if (ok) {
571         JSAutoEnterCompartment rac;
572         if (!rac.enter(cx, result_obj) || !JS_WrapValue(cx, rval))
573             return NS_ERROR_UNEXPECTED;
574     }
575
576     if (cache && ok && writeScript) {
577         WriteCachedScript(cache, cachePath, cx, scriptObj);
578     }
579
580     cc->SetReturnValueWasSet (ok);
581     return NS_OK;
582 }
583