1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=4 sw=4 et tw=80:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
17 * The Original Code is Mozilla Communicator client code, released
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.
26 * Robert Ginda <rginda@netscape.com>
27 * Kris Maglione <maglione.k@gmail.com>
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.
41 * ***** END LICENSE BLOCK ***** */
44 * What follows is the ordinary mozIJSSubScriptLoader modified only to strip the
45 * useless, irritating, and troublesome "foo -> " prefixes from filenames.
48 #include "dactylUtils.h"
49 #include "mozJSLoaderUtils.h"
51 #include "nsIServiceManager.h"
52 #include "nsIXPConnect.h"
55 #include "nsIIOService.h"
56 #include "nsIChannel.h"
57 #include "nsIConsoleService.h"
58 #include "nsIScriptError.h"
59 #include "nsIInputStream.h"
61 #include "nsAutoPtr.h"
62 #include "nsNetUtil.h"
63 #include "nsIProtocolHandler.h"
64 #include "nsIScriptSecurityManager.h"
65 #include "nsIFileURL.h"
68 #include "nsICharsetConverterManager.h"
69 #include "nsIUnicodeDecoder.h"
71 inline PRBool EnsureStringLength(T& aStr, PRUint32 aLen)
74 return (aStr.Length() == aLen);
80 #include "jsfriendapi.h"
83 static inline JSVersion
84 JS_GetVersion(JSContext *cx) {
85 return cx->findVersion();
89 #include "mozilla/FunctionTimer.h"
90 #include "mozilla/scache/StartupCache.h"
91 #include "mozilla/scache/StartupCacheUtils.h"
93 using namespace mozilla::scache;
94 class nsACString_internal : nsACString {};
98 nsresult PathifyURI(nsIURI *in, nsACString_internal &out);
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."
115 /* Internal linkage, bloody hell... */
117 ConvertToUTF16(nsIChannel* aChannel, const PRUint8* aData,
118 PRUint32 aLength, const nsString& aHintCharset,
126 nsCAutoString characterSet;
130 rv = aChannel->GetContentCharset(characterSet);
133 if (!aHintCharset.IsEmpty() && (NS_FAILED(rv) || characterSet.IsEmpty())) {
134 // charset name is always ASCII.
135 LossyCopyUTF16toASCII(aHintCharset, characterSet);
138 if (characterSet.IsEmpty()) {
139 // fall back to ISO-8859-1, see bug 118404
140 characterSet.AssignLiteral("ISO-8859-1");
143 nsCOMPtr<nsICharsetConverterManager> charsetConv =
144 do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
146 nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
148 if (NS_SUCCEEDED(rv) && charsetConv) {
149 rv = charsetConv->GetUnicodeDecoder(characterSet.get(),
150 getter_AddRefs(unicodeDecoder));
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));
158 // converts from the charset to unicode
159 if (NS_SUCCEEDED(rv)) {
160 PRInt32 unicodeLength = 0;
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;
168 PRUnichar *ustr = aString.BeginWriting();
170 PRInt32 consumedLength = 0;
171 PRInt32 originalLength = aLength;
172 PRInt32 convertedLength = 0;
173 PRInt32 bufferLength = unicodeLength;
175 rv = unicodeDecoder->Convert(reinterpret_cast<const char*>(aData),
176 (PRInt32 *) &aLength, ustr,
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;
184 unicodeDecoder->Reset();
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);
198 // We just use the same reporter as the component loader
200 mozJSLoaderErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep)
204 /* Use the console service to register the error. */
205 nsCOMPtr<nsIConsoleService> consoleService =
206 do_GetService(NS_CONSOLESERVICE_CONTRACTID);
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.
213 nsCOMPtr<nsIScriptError> errorObject =
214 do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
216 if (consoleService && errorObject) {
218 * Got an error object; prepare appropriate-width versions of
219 * various arguments to it.
221 nsAutoString fileUni;
222 fileUni.Assign(NS_ConvertUTF8toUTF16(rep->filename));
224 PRUint32 column = rep->uctokenptr - rep->uclinebuf;
226 rv = errorObject->Init(reinterpret_cast<const PRUnichar*>
229 reinterpret_cast<const PRUnichar*>
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
245 * If any of the above fails for some reason, fall back to
246 * printing to stderr.
251 Dump(JSContext *cx, uintN argc, jsval *vp)
257 str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
262 const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length);
266 fputs(NS_ConvertUTF16toUTF8(reinterpret_cast<const PRUnichar*>(chars)).get(), stderr);
271 ReportError(JSContext *cx, const char *msg)
273 JS_SetPendingException(cx, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, msg)));
278 ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj,
279 jschar *charset, const char *uriStr,
280 nsIIOService *serv, nsIPrincipal *principal,
281 JSScriptType **scriptObjp)
283 nsCOMPtr<nsIChannel> chan;
284 nsCOMPtr<nsIInputStream> instream;
285 JSPrincipals *jsPrincipals;
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));
299 return ReportError(cx, LOAD_ERROR_NOSTREAM);
304 rv = chan->GetContentLength(&len);
305 if (NS_FAILED(rv) || len == -1) {
306 return ReportError(cx, LOAD_ERROR_NOCONTENT);
310 rv = NS_ReadInputStreamToString(instream, buf, len);
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
317 rv = principal->GetJSPrincipals(cx, &jsPrincipals);
318 if (NS_FAILED(rv) || !jsPrincipals) {
319 return ReportError(cx, LOAD_ERROR_NOPRINCIPALS);
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);
329 nsnull, reinterpret_cast<const PRUint8*>(buf.get()), len,
330 nsDependentString(reinterpret_cast<PRUnichar*>(charset)), script);
333 JSPRINCIPALS_DROP(cx, jsPrincipals);
334 return ReportError(cx, LOAD_ERROR_BADCHARSET);
338 JS_CompileUCScriptForPrincipals(cx, target_obj, jsPrincipals,
339 reinterpret_cast<const jschar*>(script.get()),
340 script.Length(), uriStr, 1);
343 JS_CompileScriptForPrincipals(cx, target_obj, jsPrincipals, buf.get(),
347 JSPRINCIPALS_DROP(cx, jsPrincipals);
349 /* repent for our evil deeds */
350 JS_SetErrorReporter(cx, er);
355 NS_IMETHODIMP /* args and return value are delt with using XPConnect and JSAPI */
356 dactylUtils::LoadSubScript (const PRUnichar * aURL
357 /* [, JSObject *target_obj] */)
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
364 * target_obj: Optional object to eval the script onto (defaults to context
366 * returns: Whatever jsval the script pointed to by the url returns.
367 * Should ONLY (O N L Y !) be called from JavaScript code.
370 /* gotta define most of this stuff up here because of all the gotos,
371 * defined the rest up here to be consistent */
375 #ifdef NS_FUNCTION_TIMER
376 NS_TIME_FUNCTION_FMT("%s (line %d) (url: %s)", MOZ_FUNCTION_NAME,
377 __LINE__, NS_LossyConvertUTF16toASCII(aURL).get());
379 (void)aURL; // prevent compiler warning
382 /* get JS things from the CallContext */
383 nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
384 if (!xpc) return NS_ERROR_FAILURE;
386 nsAXPCNativeCallContext *cc = nsnull;
387 rv = xpc->GetCurrentNativeCallContext(&cc);
388 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
391 rv = cc->GetJSContext (&cx);
392 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
395 rv = cc->GetArgc (&argc);
396 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
399 rv = cc->GetArgvPtr (&argv);
400 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
403 rv = cc->GetRetValPtr (&rval);
404 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
406 /* set mJSPrincipals if it's not here already */
407 if (!mSystemPrincipal)
409 nsCOMPtr<nsIScriptSecurityManager> secman =
410 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
414 rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
415 if (NS_FAILED(rv) || !mSystemPrincipal)
419 JSAutoRequest ar(cx);
422 JSObject *target_obj = nsnull;
423 jschar *charset = nsnull;
424 ok = JS_ConvertArguments (cx, argc, argv, "S / o W", &url, &target_obj, &charset);
427 /* let the exception raised by JS_ConvertArguments show through */
431 JSAutoByteString urlbytes(cx, url);
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 */
442 nsCOMPtr<nsIXPConnectWrappedNative> wn;
443 rv = cc->GetCalleeWrapper (getter_AddRefs(wn));
444 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
446 rv = wn->GetJSObject (&target_obj);
447 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
449 JSObject *maybe_glob = JS_GetParent (cx, target_obj);
450 while (maybe_glob != nsnull)
452 target_obj = maybe_glob;
453 maybe_glob = JS_GetParent (cx, maybe_glob);
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);
463 return NS_ERROR_FAILURE;
465 if (target_obj != result_obj)
467 nsCOMPtr<nsIScriptSecurityManager> secman =
468 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
470 return NS_ERROR_FAILURE;
472 rv = secman->GetObjectPrincipal(cx, target_obj, getter_AddRefs(principal));
473 NS_ENSURE_SUCCESS(rv, rv);
476 JSAutoEnterCompartment ac;
477 if (!ac.enter(cx, target_obj))
478 return NS_ERROR_UNEXPECTED;
480 /* load up the url. From here on, failures are reflected as ``custom''
482 nsCOMPtr<nsIURI> uri;
483 nsCAutoString uriStr;
484 nsCAutoString scheme;
486 JSStackFrame* frame = nsnull;
487 JSScript* script = nsnull;
489 // Figure out who's calling us
492 frame = JS_FrameIterator(cx, &frame);
495 script = JS_GetFrameScript(cx, frame);
496 } while (frame && !script);
500 // No script means we don't know who's calling, bail.
502 return NS_ERROR_FAILURE;
505 nsCOMPtr<nsIIOService> serv = do_GetService(NS_IOSERVICE_CONTRACTID);
507 return ReportError(cx, LOAD_ERROR_NOSERVICE);
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);
514 return ReportError(cx, LOAD_ERROR_NOURI);
517 rv = uri->GetSpec(uriStr);
519 return ReportError(cx, LOAD_ERROR_NOSPEC);
522 rv = uri->GetScheme(scheme);
524 return ReportError(cx, LOAD_ERROR_NOSCHEME);
527 bool writeScript = false;
528 JSScriptType *scriptObj = nsnull;
529 JSVersion version = JS_GetVersion(cx);
531 nsCAutoString cachePath;
532 cachePath.Append("jssubloader/");
533 cachePath.AppendInt(version);
535 cachePath.Append("/");
536 cachePath.Append(NS_ConvertUTF16toUTF8(
537 nsDependentString(reinterpret_cast<PRUnichar*>(charset))));
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);
545 cachePath.Append("/");
546 cachePath.Append(uriStr);
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);
558 rv = ReadScript(uri, cx, target_obj, charset, (char *)uriStr.get(), serv,
559 principal, &scriptObj);
563 if (NS_FAILED(rv) || !scriptObj)
568 ok = JS_ExecuteScriptVersion(cx, target_obj, scriptObj, rval, version);
571 JSAutoEnterCompartment rac;
572 if (!rac.enter(cx, result_obj) || !JS_WrapValue(cx, rval))
573 return NS_ERROR_UNEXPECTED;
576 if (cache && ok && writeScript) {
577 WriteCachedScript(cache, cachePath, cx, scriptObj);
580 cc->SetReturnValueWasSet (ok);