52
52
#include "nsIInputStream.h"
53
53
#include "nsNetCID.h"
54
54
#include "nsDependentString.h"
55
#include "nsAutoPtr.h"
56
#include "nsNetUtil.h"
57
#include "nsIProtocolHandler.h"
58
#include "nsIFileURL.h"
58
63
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
60
65
/* load() error msgs, XXX localize? */
61
66
#define LOAD_ERROR_NOSERVICE "Error creating IO Service."
62
#define LOAD_ERROR_NOCHANNEL "Error creating channel (invalid URL scheme?)"
67
#define LOAD_ERROR_NOURI "Error creating URI (invalid URL scheme?)"
68
#define LOAD_ERROR_NOSCHEME "Failed to get URI scheme. This is bad."
69
#define LOAD_ERROR_URI_NOT_LOCAL "Trying to load a non-local URI."
63
70
#define LOAD_ERROR_NOSTREAM "Error opening input stream (invalid filename?)"
64
71
#define LOAD_ERROR_NOCONTENT "ContentLength not available (not a local URL?)"
65
72
#define LOAD_ERROR_BADREAD "File Read Error."
66
73
#define LOAD_ERROR_READUNDERFLOW "File Read Error (underflow.)"
74
#define LOAD_ERROR_NOPRINCIPALS "Failed to get principals."
75
#define LOAD_ERROR_NOSPEC "Failed to get URI spec. This is bad."
68
77
// We just use the same reporter as the component loader
69
78
extern void JS_DLL_CALLBACK
135
144
rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
136
145
if (NS_FAILED(rv) || !mSystemPrincipal)
142
150
JSObject *target_obj = nsnull;
143
151
ok = JS_ConvertArguments (cx, argc, argv, "s / o", &url, &target_obj);
215
223
nsCOMPtr<nsIChannel> chan;
216
224
nsCOMPtr<nsIInputStream> instream;
225
nsCOMPtr<nsIURI> uri;
226
nsCAutoString uriStr;
227
nsCAutoString scheme;
229
JSStackFrame* frame = nsnull;
230
JSScript* script = nsnull;
232
// Figure out who's calling us
235
frame = JS_FrameIterator(cx, &frame);
238
script = JS_GetFrameScript(cx, frame);
239
} while (frame && !script);
243
// No script means we don't know who's calling, bail.
245
return NS_ERROR_FAILURE;
218
248
nsCOMPtr<nsIIOService> serv = do_GetService(kIOServiceCID);
222
252
goto return_exception;
225
rv = serv->NewChannel(nsDependentCString(url), nsnull, NS_STATIC_CAST(nsIURI *, nsnull),
226
getter_AddRefs(chan));
255
// Make sure to explicitly create the URI, since we'll need the
256
// canonicalized spec.
257
rv = NS_NewURI(getter_AddRefs(uri), url, nsnull, serv);
259
errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOURI);
260
goto return_exception;
263
rv = uri->GetSpec(uriStr);
265
errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOSPEC);
266
goto return_exception;
269
rv = uri->GetScheme(scheme);
227
270
if (NS_FAILED(rv))
229
errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOCHANNEL);
272
errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOSCHEME);
230
273
goto return_exception;
233
rv = chan->Open (getter_AddRefs(instream));
276
if (!scheme.EqualsLiteral("chrome"))
278
// This might be a URI to a local file, though!
279
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(uri);
282
errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_URI_NOT_LOCAL);
283
goto return_exception;
286
// For file URIs prepend the filename with the filename of the
287
// calling script, and " -> ". See bug 418356.
288
nsCAutoString tmp(JS_GetScriptFilename(cx, script));
289
tmp.AppendLiteral(" -> ");
295
rv = NS_OpenURI(getter_AddRefs(instream), uri, serv,
296
nsnull, nsnull, nsIRequest::LOAD_NORMAL,
297
getter_AddRefs(chan));
234
298
if (NS_FAILED(rv))
236
300
errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOSTREAM);
267
331
rv = mSystemPrincipal->GetJSPrincipals(cx, &jsPrincipals);
268
332
if (NS_FAILED(rv) || !jsPrincipals) {
333
errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOPRINCIPALS);
334
goto return_exception;
273
337
/* set our own error reporter so we can report any bad things as catchable
275
339
er = JS_SetErrorReporter (cx, mozJSLoaderErrorReporter);
277
341
ok = JS_EvaluateScriptForPrincipals (cx, target_obj, jsPrincipals,
278
buf, len, url, 1, rval);
342
buf, len, uriStr.get(), 1, rval);
279
343
/* repent for our evil deeds */
280
344
JS_SetErrorReporter (cx, er);
282
346
cc->SetExceptionWasThrown (!ok);
283
347
cc->SetReturnValueWasSet (ok);
286
349
JSPRINCIPALS_DROP(cx, jsPrincipals);
289
352
return_exception:
293
353
JS_SetPendingException (cx, STRING_TO_JSVAL(errmsg));
294
354
cc->SetExceptionWasThrown (JS_TRUE);
299
358
#endif /* NO_SUBSCRIPT_LOADER */