2014-04-03 15:58:00 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-03-14 05:41:55 +03:00
|
|
|
|
|
|
|
#include "mozJSSubScriptLoader.h"
|
2012-11-13 23:13:27 +04:00
|
|
|
#include "mozJSComponentLoader.h"
|
2011-07-10 07:21:16 +04:00
|
|
|
#include "mozJSLoaderUtils.h"
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2001-03-14 06:12:07 +03:00
|
|
|
#include "nsIURI.h"
|
2001-03-14 05:41:55 +03:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIInputStream.h"
|
2001-04-10 10:01:08 +04:00
|
|
|
#include "nsNetCID.h"
|
2008-03-18 06:46:53 +03:00
|
|
|
#include "nsNetUtil.h"
|
2008-03-21 08:07:25 +03:00
|
|
|
#include "nsIFileURL.h"
|
2010-10-07 21:44:55 +04:00
|
|
|
#include "nsScriptLoader.h"
|
2013-09-10 01:14:10 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-04-18 09:03:03 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2001-03-14 05:41:55 +03:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-03-13 17:45:02 +03:00
|
|
|
#include "jsfriendapi.h"
|
2013-08-27 06:05:20 +04:00
|
|
|
#include "js/OldDebugAPI.h"
|
2012-03-09 13:48:50 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
2013-08-24 06:42:40 +04:00
|
|
|
#include "xpcpublic.h" // For xpc::SystemErrorReporter
|
2013-11-05 19:35:41 +04:00
|
|
|
#include "xpcprivate.h" // For xpc::OptionsBase
|
2014-04-18 09:03:03 +04:00
|
|
|
#include "jswrapper.h"
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
|
|
|
#include "mozilla/scache/StartupCacheUtils.h"
|
2014-04-18 09:03:03 +04:00
|
|
|
#include "mozilla/unused.h"
|
2011-07-10 07:21:16 +04:00
|
|
|
|
|
|
|
using namespace mozilla::scache;
|
2013-04-26 21:50:18 +04:00
|
|
|
using namespace JS;
|
2013-11-05 19:35:41 +04:00
|
|
|
using namespace xpc;
|
2014-04-18 09:03:03 +04:00
|
|
|
using namespace mozilla;
|
2013-11-05 19:35:41 +04:00
|
|
|
|
|
|
|
class MOZ_STACK_CLASS LoadSubScriptOptions : public OptionsBase {
|
|
|
|
public:
|
|
|
|
LoadSubScriptOptions(JSContext *cx = xpc_GetSafeJSContext(),
|
|
|
|
JSObject *options = nullptr)
|
|
|
|
: OptionsBase(cx, options)
|
|
|
|
, target(cx)
|
|
|
|
, charset(NullString())
|
|
|
|
, ignoreCache(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool Parse() {
|
|
|
|
return ParseObject("target", &target) &&
|
|
|
|
ParseString("charset", charset) &&
|
|
|
|
ParseBoolean("ignoreCache", &ignoreCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
RootedObject target;
|
|
|
|
nsString charset;
|
|
|
|
bool ignoreCache;
|
|
|
|
};
|
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
|
2001-03-14 05:41:55 +03:00
|
|
|
/* load() error msgs, XXX localize? */
|
|
|
|
#define LOAD_ERROR_NOSERVICE "Error creating IO Service."
|
2008-03-18 06:46:53 +03:00
|
|
|
#define LOAD_ERROR_NOURI "Error creating URI (invalid URL scheme?)"
|
|
|
|
#define LOAD_ERROR_NOSCHEME "Failed to get URI scheme. This is bad."
|
2008-03-21 08:07:25 +03:00
|
|
|
#define LOAD_ERROR_URI_NOT_LOCAL "Trying to load a non-local URI."
|
2001-03-14 05:41:55 +03:00
|
|
|
#define LOAD_ERROR_NOSTREAM "Error opening input stream (invalid filename?)"
|
|
|
|
#define LOAD_ERROR_NOCONTENT "ContentLength not available (not a local URL?)"
|
2010-10-07 21:44:55 +04:00
|
|
|
#define LOAD_ERROR_BADCHARSET "Error converting to specified charset"
|
2001-03-14 05:41:55 +03:00
|
|
|
#define LOAD_ERROR_BADREAD "File Read Error."
|
|
|
|
#define LOAD_ERROR_READUNDERFLOW "File Read Error (underflow.)"
|
2008-03-18 06:46:53 +03:00
|
|
|
#define LOAD_ERROR_NOPRINCIPALS "Failed to get principals."
|
|
|
|
#define LOAD_ERROR_NOSPEC "Failed to get URI spec. This is bad."
|
2012-10-22 21:51:07 +04:00
|
|
|
#define LOAD_ERROR_CONTENTTOOBIG "ContentLength is too large"
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mozJSSubScriptLoader::mozJSSubScriptLoader() : mSystemPrincipal(nullptr)
|
2001-03-14 05:41:55 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
mozJSSubScriptLoader::~mozJSSubScriptLoader()
|
2001-03-14 05:41:55 +03:00
|
|
|
{
|
|
|
|
/* empty */
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(mozJSSubScriptLoader, mozIJSSubScriptLoader)
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
static nsresult
|
|
|
|
ReportError(JSContext *cx, const char *msg)
|
|
|
|
{
|
2013-09-19 11:54:01 +04:00
|
|
|
RootedValue exn(cx, JS::StringValue(JS_NewStringCopyZ(cx, msg)));
|
|
|
|
JS_SetPendingException(cx, exn);
|
2011-07-10 07:21:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-04-26 21:50:18 +04:00
|
|
|
mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *targetObjArg,
|
2014-01-22 21:50:32 +04:00
|
|
|
const nsAString &charset, const char *uriStr,
|
2011-07-21 03:11:32 +04:00
|
|
|
nsIIOService *serv, nsIPrincipal *principal,
|
2014-06-12 04:38:22 +04:00
|
|
|
bool reuseGlobal, JS::MutableHandleScript script,
|
|
|
|
JS::MutableHandleFunction function)
|
2011-07-10 07:21:16 +04:00
|
|
|
{
|
2013-04-26 21:50:18 +04:00
|
|
|
RootedObject target_obj(cx, targetObjArg);
|
|
|
|
|
2014-06-12 04:38:22 +04:00
|
|
|
script.set(nullptr);
|
|
|
|
function.set(nullptr);
|
2012-11-27 02:41:55 +04:00
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
// Instead of calling NS_OpenURI, we create the channel ourselves and call
|
|
|
|
// SetContentType, to avoid expensive MIME type lookups (bug 632490).
|
2014-01-22 21:50:32 +04:00
|
|
|
nsCOMPtr<nsIChannel> chan;
|
|
|
|
nsCOMPtr<nsIInputStream> instream;
|
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(chan), uri, serv,
|
|
|
|
nullptr, nullptr, nsIRequest::LOAD_NORMAL);
|
2011-07-10 07:21:16 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
chan->SetContentType(NS_LITERAL_CSTRING("application/javascript"));
|
|
|
|
rv = chan->Open(getter_AddRefs(instream));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_NOSTREAM);
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2012-10-22 21:51:07 +04:00
|
|
|
int64_t len = -1;
|
2011-07-10 07:21:16 +04:00
|
|
|
|
|
|
|
rv = chan->GetContentLength(&len);
|
|
|
|
if (NS_FAILED(rv) || len == -1) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_NOCONTENT);
|
|
|
|
}
|
|
|
|
|
2012-10-22 21:51:07 +04:00
|
|
|
if (len > INT32_MAX) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_CONTENTTOOBIG);
|
|
|
|
}
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
nsCString buf;
|
|
|
|
rv = NS_ReadInputStreamToString(instream, buf, len);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
/* set our own error reporter so we can report any bad things as catchable
|
|
|
|
* exceptions, including the source/line number */
|
2014-01-22 21:50:32 +04:00
|
|
|
JSErrorReporter er = JS_SetErrorReporter(cx, xpc::SystemErrorReporter);
|
2011-07-10 07:21:16 +04:00
|
|
|
|
2012-08-07 21:39:47 +04:00
|
|
|
JS::CompileOptions options(cx);
|
2014-03-07 02:28:05 +04:00
|
|
|
options.setFileAndLine(uriStr, 1);
|
2011-12-18 14:09:16 +04:00
|
|
|
if (!charset.IsVoid()) {
|
2014-04-25 18:11:57 +04:00
|
|
|
jschar *scriptBuf = nullptr;
|
|
|
|
size_t scriptLength = 0;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
rv = nsScriptLoader::ConvertToUTF16(nullptr, reinterpret_cast<const uint8_t*>(buf.get()), len,
|
2014-04-25 18:11:57 +04:00
|
|
|
charset, nullptr, scriptBuf, scriptLength);
|
|
|
|
|
|
|
|
JS::SourceBufferHolder srcBuf(scriptBuf, scriptLength,
|
|
|
|
JS::SourceBufferHolder::GiveOwnership);
|
2011-07-10 07:21:16 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_BADCHARSET);
|
|
|
|
}
|
|
|
|
|
2012-11-27 02:41:55 +04:00
|
|
|
if (!reuseGlobal) {
|
2014-06-12 04:38:22 +04:00
|
|
|
JS::Compile(cx, target_obj, options, srcBuf, script);
|
2012-11-27 02:41:55 +04:00
|
|
|
} else {
|
2014-06-12 04:38:22 +04:00
|
|
|
JS::CompileFunction(cx, target_obj, options,
|
|
|
|
nullptr, 0, nullptr,
|
|
|
|
srcBuf,
|
|
|
|
function);
|
2012-11-27 02:41:55 +04:00
|
|
|
}
|
2011-07-10 07:21:16 +04:00
|
|
|
} else {
|
2014-04-23 01:08:27 +04:00
|
|
|
// We only use lazy source when no special encoding is specified because
|
2012-11-20 21:30:03 +04:00
|
|
|
// the lazy source loader doesn't know the encoding.
|
2012-11-27 02:41:55 +04:00
|
|
|
if (!reuseGlobal) {
|
2014-04-23 01:08:27 +04:00
|
|
|
options.setSourceIsLazy(true);
|
2014-06-26 02:35:35 +04:00
|
|
|
JS::Compile(cx, target_obj, options, buf.get(), len, script);
|
2012-11-27 02:41:55 +04:00
|
|
|
} else {
|
2014-06-26 02:35:35 +04:00
|
|
|
JS::CompileFunction(cx, target_obj, options,
|
|
|
|
nullptr, 0, nullptr, buf.get(),
|
|
|
|
len, function);
|
2012-11-27 02:41:55 +04:00
|
|
|
}
|
2011-07-10 07:21:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* repent for our evil deeds */
|
|
|
|
JS_SetErrorReporter(cx, er);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-18 14:09:16 +04:00
|
|
|
NS_IMETHODIMP
|
2014-01-22 21:50:32 +04:00
|
|
|
mozJSSubScriptLoader::LoadSubScript(const nsAString &url,
|
2014-01-09 21:39:36 +04:00
|
|
|
HandleValue target,
|
2014-01-22 21:50:32 +04:00
|
|
|
const nsAString &charset,
|
2014-01-09 21:39:36 +04:00
|
|
|
JSContext *cx,
|
|
|
|
MutableHandleValue retval)
|
2001-03-14 05:41:55 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Loads a local url and evals it into the current cx
|
|
|
|
* Synchronous (an async version would be cool too.)
|
|
|
|
* url: The url to load. Must be local so that it can be loaded
|
|
|
|
* synchronously.
|
|
|
|
* target_obj: Optional object to eval the script onto (defaults to context
|
|
|
|
* global)
|
2013-11-05 19:35:41 +04:00
|
|
|
* charset: Optional character set to use for reading
|
2001-03-14 05:41:55 +03:00
|
|
|
* returns: Whatever jsval the script pointed to by the url returns.
|
|
|
|
* Should ONLY (O N L Y !) be called from JavaScript code.
|
|
|
|
*/
|
2013-11-05 19:35:41 +04:00
|
|
|
LoadSubScriptOptions options(cx);
|
|
|
|
options.charset = charset;
|
2014-01-09 21:39:36 +04:00
|
|
|
options.target = target.isObject() ? &target.toObject() : nullptr;
|
2013-11-05 19:35:41 +04:00
|
|
|
return DoLoadSubScriptWithOptions(url, options, cx, retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-22 21:50:32 +04:00
|
|
|
mozJSSubScriptLoader::LoadSubScriptWithOptions(const nsAString &url,
|
2014-01-09 21:39:36 +04:00
|
|
|
HandleValue optionsVal,
|
|
|
|
JSContext *cx,
|
|
|
|
MutableHandleValue retval)
|
2013-11-05 19:35:41 +04:00
|
|
|
{
|
|
|
|
if (!optionsVal.isObject())
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
LoadSubScriptOptions options(cx, &optionsVal.toObject());
|
|
|
|
if (!options.Parse())
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
return DoLoadSubScriptWithOptions(url, options, cx, retval);
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2013-11-05 19:35:41 +04:00
|
|
|
nsresult
|
2014-01-22 21:50:32 +04:00
|
|
|
mozJSSubScriptLoader::DoLoadSubScriptWithOptions(const nsAString &url,
|
|
|
|
LoadSubScriptOptions &options,
|
2014-01-09 21:39:36 +04:00
|
|
|
JSContext *cx,
|
|
|
|
MutableHandleValue retval)
|
2013-11-05 19:35:41 +04:00
|
|
|
{
|
2011-12-18 14:09:16 +04:00
|
|
|
nsresult rv = NS_OK;
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2012-03-09 13:48:50 +04:00
|
|
|
/* set the system principal if it's not here already */
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!mSystemPrincipal) {
|
2001-03-14 05:41:55 +03:00
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secman =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
|
|
|
|
if (!secman)
|
2011-12-18 14:09:16 +04:00
|
|
|
return NS_OK;
|
2001-03-14 05:41:55 +03:00
|
|
|
|
|
|
|
rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
|
|
|
|
if (NS_FAILED(rv) || !mSystemPrincipal)
|
|
|
|
return rv;
|
|
|
|
}
|
2006-06-13 02:39:55 +04:00
|
|
|
|
2013-04-26 21:50:18 +04:00
|
|
|
RootedObject targetObj(cx);
|
2014-01-22 21:50:32 +04:00
|
|
|
mozJSComponentLoader *loader = mozJSComponentLoader::Get();
|
2012-11-27 02:41:55 +04:00
|
|
|
rv = loader->FindTargetObject(cx, &targetObj);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-03-21 08:07:25 +03:00
|
|
|
|
2012-11-27 02:41:55 +04:00
|
|
|
// We base reusingGlobal off of what the loader told us, but we may not
|
|
|
|
// actually be using that object.
|
2013-11-05 19:35:41 +04:00
|
|
|
bool reusingGlobal = !JS_IsGlobalObject(targetObj);
|
2012-11-27 02:41:55 +04:00
|
|
|
|
2013-11-05 19:35:41 +04:00
|
|
|
if (options.target)
|
|
|
|
targetObj = options.target;
|
2005-12-09 21:58:23 +03:00
|
|
|
|
2010-10-11 02:46:16 +04:00
|
|
|
// Remember an object out of the calling compartment so that we
|
|
|
|
// can properly wrap the result later.
|
2011-07-21 03:11:32 +04:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = mSystemPrincipal;
|
2013-04-26 21:50:18 +04:00
|
|
|
RootedObject result_obj(cx, targetObj);
|
2011-12-18 14:09:16 +04:00
|
|
|
targetObj = JS_FindCompilationScope(cx, targetObj);
|
|
|
|
if (!targetObj)
|
2011-07-21 03:11:32 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2014-01-09 18:28:46 +04:00
|
|
|
if (targetObj != result_obj)
|
|
|
|
principal = GetObjectPrincipal(targetObj);
|
2010-10-11 02:37:19 +04:00
|
|
|
|
2012-08-22 05:42:53 +04:00
|
|
|
JSAutoCompartment ac(cx, targetObj);
|
2001-03-14 05:41:55 +03:00
|
|
|
|
|
|
|
/* load up the url. From here on, failures are reflected as ``custom''
|
|
|
|
* js exceptions */
|
2008-03-18 06:46:53 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString uriStr;
|
|
|
|
nsAutoCString scheme;
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2008-03-21 08:07:25 +03:00
|
|
|
// Figure out who's calling us
|
2014-02-25 19:43:14 +04:00
|
|
|
JS::AutoFilename filename;
|
|
|
|
if (!JS::DescribeScriptedCaller(cx, &filename)) {
|
2012-04-16 23:25:28 +04:00
|
|
|
// No scripted frame means we don't know who's calling, bail.
|
2008-03-21 08:07:25 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-07-21 03:11:32 +04:00
|
|
|
// Suppress caching if we're compiling as content.
|
|
|
|
StartupCache* cache = (principal == mSystemPrincipal)
|
|
|
|
? StartupCache::GetSingleton()
|
2012-07-30 18:20:58 +04:00
|
|
|
: nullptr;
|
2006-06-19 01:18:22 +04:00
|
|
|
nsCOMPtr<nsIIOService> serv = do_GetService(NS_IOSERVICE_CONTRACTID);
|
2011-07-10 07:21:16 +04:00
|
|
|
if (!serv) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_NOSERVICE);
|
2001-03-14 05:41:55 +03:00
|
|
|
}
|
|
|
|
|
2008-03-18 06:46:53 +03:00
|
|
|
// Make sure to explicitly create the URI, since we'll need the
|
|
|
|
// canonicalized spec.
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), NS_LossyConvertUTF16toASCII(url).get(), nullptr, serv);
|
2008-03-18 06:46:53 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-07-10 07:21:16 +04:00
|
|
|
return ReportError(cx, LOAD_ERROR_NOURI);
|
2001-03-14 05:41:55 +03:00
|
|
|
}
|
|
|
|
|
2008-03-21 08:07:25 +03:00
|
|
|
rv = uri->GetSpec(uriStr);
|
2008-03-18 06:46:53 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-07-10 07:21:16 +04:00
|
|
|
return ReportError(cx, LOAD_ERROR_NOSPEC);
|
2011-10-14 21:52:47 +04:00
|
|
|
}
|
2008-03-21 08:07:25 +03:00
|
|
|
|
|
|
|
rv = uri->GetScheme(scheme);
|
2011-07-10 07:21:16 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_NOSCHEME);
|
2008-03-18 06:46:53 +03:00
|
|
|
}
|
2008-03-21 08:07:25 +03:00
|
|
|
|
2011-10-14 21:52:48 +04:00
|
|
|
if (!scheme.EqualsLiteral("chrome")) {
|
2008-03-21 08:07:25 +03:00
|
|
|
// This might be a URI to a local file, though!
|
2010-09-09 07:39:00 +04:00
|
|
|
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(uri);
|
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(innerURI);
|
2011-07-10 07:21:16 +04:00
|
|
|
if (!fileURL) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_URI_NOT_LOCAL);
|
2008-03-21 08:07:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For file URIs prepend the filename with the filename of the
|
|
|
|
// calling script, and " -> ". See bug 418356.
|
2014-02-25 19:43:14 +04:00
|
|
|
nsAutoCString tmp(filename.get());
|
2008-03-21 08:07:25 +03:00
|
|
|
tmp.AppendLiteral(" -> ");
|
|
|
|
tmp.Append(uriStr);
|
|
|
|
|
|
|
|
uriStr = tmp;
|
2011-02-08 23:16:06 +03:00
|
|
|
}
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
bool writeScript = false;
|
2011-10-04 18:06:54 +04:00
|
|
|
JSVersion version = JS_GetVersion(cx);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString cachePath;
|
2011-07-10 07:21:16 +04:00
|
|
|
cachePath.AppendPrintf("jssubloader/%d", version);
|
2011-07-20 11:39:09 +04:00
|
|
|
PathifyURI(uri, cachePath);
|
2011-07-10 07:21:16 +04:00
|
|
|
|
2013-04-26 21:50:18 +04:00
|
|
|
RootedFunction function(cx);
|
2014-02-25 19:43:14 +04:00
|
|
|
RootedScript script(cx);
|
2013-11-05 19:35:41 +04:00
|
|
|
if (cache && !options.ignoreCache)
|
2013-09-20 13:22:59 +04:00
|
|
|
rv = ReadCachedScript(cache, cachePath, cx, mSystemPrincipal, &script);
|
2011-09-02 10:46:00 +04:00
|
|
|
if (!script) {
|
2013-11-05 19:35:41 +04:00
|
|
|
rv = ReadScript(uri, cx, targetObj, options.charset,
|
2011-12-18 14:09:16 +04:00
|
|
|
static_cast<const char*>(uriStr.get()), serv,
|
2014-06-12 04:38:22 +04:00
|
|
|
principal, reusingGlobal, &script, &function);
|
2012-11-27 02:41:55 +04:00
|
|
|
writeScript = !!script;
|
2003-03-23 10:22:18 +03:00
|
|
|
}
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2012-11-27 02:41:55 +04:00
|
|
|
if (NS_FAILED(rv) || (!script && !function))
|
2011-07-10 07:21:16 +04:00
|
|
|
return rv;
|
2010-10-11 02:46:16 +04:00
|
|
|
|
2012-11-27 02:41:55 +04:00
|
|
|
if (function) {
|
|
|
|
script = JS_GetFunctionScript(cx, function);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
if (function) {
|
2014-02-13 11:38:36 +04:00
|
|
|
ok = JS_CallFunction(cx, targetObj, function, JS::HandleValueArray::empty(),
|
|
|
|
retval);
|
2012-11-27 02:41:55 +04:00
|
|
|
} else {
|
2014-04-01 14:34:39 +04:00
|
|
|
ok = JS_ExecuteScriptVersion(cx, targetObj, script, retval, version);
|
2012-11-27 02:41:55 +04:00
|
|
|
}
|
2011-02-18 01:20:00 +03:00
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
if (ok) {
|
2012-08-22 05:42:53 +04:00
|
|
|
JSAutoCompartment rac(cx, result_obj);
|
2014-01-09 21:39:36 +04:00
|
|
|
if (!JS_WrapValue(cx, retval))
|
2011-10-14 21:52:47 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2010-10-11 02:46:16 +04:00
|
|
|
}
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
if (cache && ok && writeScript) {
|
2012-02-13 17:10:04 +04:00
|
|
|
WriteCachedScript(cache, cachePath, cx, mSystemPrincipal, script);
|
2011-07-10 07:21:16 +04:00
|
|
|
}
|
2001-03-14 05:41:55 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-04-18 09:03:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Let us compile scripts from a URI off the main thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ScriptPrecompiler : public nsIStreamLoaderObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
|
|
|
|
|
|
|
ScriptPrecompiler(nsIObserver* aObserver,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
nsIChannel* aChannel)
|
|
|
|
: mObserver(aObserver)
|
|
|
|
, mPrincipal(aPrincipal)
|
|
|
|
, mChannel(aChannel)
|
2014-04-25 18:11:57 +04:00
|
|
|
, mScriptBuf(nullptr)
|
|
|
|
, mScriptLength(0)
|
2014-04-18 09:03:03 +04:00
|
|
|
{}
|
|
|
|
|
2014-06-23 22:49:08 +04:00
|
|
|
static void OffThreadCallback(void *aToken, void *aData);
|
|
|
|
|
|
|
|
/* Sends the "done" notification back. Main thread only. */
|
|
|
|
void SendObserverNotification();
|
|
|
|
|
|
|
|
private:
|
2014-04-18 09:03:03 +04:00
|
|
|
virtual ~ScriptPrecompiler()
|
2014-04-25 18:11:57 +04:00
|
|
|
{
|
|
|
|
if (mScriptBuf) {
|
|
|
|
js_free(mScriptBuf);
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 09:03:03 +04:00
|
|
|
|
|
|
|
nsRefPtr<nsIObserver> mObserver;
|
|
|
|
nsRefPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsRefPtr<nsIChannel> mChannel;
|
2014-04-25 18:11:57 +04:00
|
|
|
jschar* mScriptBuf;
|
|
|
|
size_t mScriptLength;
|
2014-04-18 09:03:03 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ScriptPrecompiler, nsIStreamLoaderObserver);
|
2014-04-18 09:03:03 +04:00
|
|
|
|
|
|
|
class NotifyPrecompilationCompleteRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
NotifyPrecompilationCompleteRunnable(ScriptPrecompiler* aPrecompiler)
|
|
|
|
: mPrecompiler(aPrecompiler)
|
|
|
|
, mToken(nullptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void SetToken(void* aToken) {
|
|
|
|
MOZ_ASSERT(aToken && !mToken);
|
|
|
|
mToken = aToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsRefPtr<ScriptPrecompiler> mPrecompiler;
|
|
|
|
void* mToken;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* RAII helper class to send observer notifications */
|
|
|
|
class AutoSendObserverNotification {
|
|
|
|
public:
|
|
|
|
AutoSendObserverNotification(ScriptPrecompiler* aPrecompiler)
|
|
|
|
: mPrecompiler(aPrecompiler)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~AutoSendObserverNotification() {
|
|
|
|
if (mPrecompiler) {
|
|
|
|
mPrecompiler->SendObserverNotification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disarm() {
|
|
|
|
mPrecompiler = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ScriptPrecompiler* mPrecompiler;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
NotifyPrecompilationCompleteRunnable::Run(void)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mPrecompiler);
|
|
|
|
|
|
|
|
AutoSendObserverNotification notifier(mPrecompiler);
|
|
|
|
|
|
|
|
if (mToken) {
|
|
|
|
JSRuntime *rt = XPCJSRuntime::Get()->Runtime();
|
|
|
|
NS_ENSURE_TRUE(rt, NS_ERROR_FAILURE);
|
|
|
|
JS::FinishOffThreadScript(nullptr, rt, mToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ScriptPrecompiler::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
uint32_t aLength,
|
|
|
|
const uint8_t* aString)
|
|
|
|
{
|
|
|
|
AutoSendObserverNotification notifier(this);
|
|
|
|
|
|
|
|
// Just notify that we are done with this load.
|
|
|
|
NS_ENSURE_SUCCESS(aStatus, NS_OK);
|
|
|
|
|
|
|
|
// Convert data to jschar* and prepare to call CompileOffThread.
|
|
|
|
nsAutoString hintCharset;
|
|
|
|
nsresult rv =
|
|
|
|
nsScriptLoader::ConvertToUTF16(mChannel, aString, aLength,
|
2014-04-25 18:11:57 +04:00
|
|
|
hintCharset, nullptr,
|
|
|
|
mScriptBuf, mScriptLength);
|
2014-04-18 09:03:03 +04:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
|
|
|
|
// Our goal is to cache persistently the compiled script and to avoid quota
|
|
|
|
// checks. Since the caching mechanism decide the persistence type based on
|
|
|
|
// the principal, we create a new global with the app's principal.
|
|
|
|
// We then enter its compartment to compile with its principal.
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
RootedValue v(cx);
|
|
|
|
SandboxOptions sandboxOptions;
|
|
|
|
sandboxOptions.sandboxName.AssignASCII("asm.js precompilation");
|
|
|
|
sandboxOptions.invisibleToDebugger = true;
|
2014-04-23 01:08:29 +04:00
|
|
|
sandboxOptions.discardSource = true;
|
2014-04-18 09:03:03 +04:00
|
|
|
rv = CreateSandboxObject(cx, &v, mPrincipal, sandboxOptions);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, js::UncheckedUnwrap(&v.toObject()));
|
|
|
|
|
|
|
|
JS::CompileOptions options(cx, JSVERSION_DEFAULT);
|
|
|
|
options.forceAsync = true;
|
|
|
|
options.compileAndGo = true;
|
|
|
|
options.installedFile = true;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
nsAutoCString spec;
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
options.setFile(spec.get());
|
|
|
|
|
2014-04-25 18:11:57 +04:00
|
|
|
if (!JS::CanCompileOffThread(cx, options, mScriptLength)) {
|
2014-04-18 09:03:03 +04:00
|
|
|
NS_WARNING("Can't compile script off thread!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<NotifyPrecompilationCompleteRunnable> runnable =
|
|
|
|
new NotifyPrecompilationCompleteRunnable(this);
|
|
|
|
|
|
|
|
if (!JS::CompileOffThread(cx, options,
|
2014-04-25 18:11:57 +04:00
|
|
|
mScriptBuf, mScriptLength,
|
2014-04-18 09:03:03 +04:00
|
|
|
OffThreadCallback,
|
|
|
|
static_cast<void*>(runnable))) {
|
|
|
|
NS_WARNING("Failed to compile script off thread!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
unused << runnable.forget();
|
|
|
|
notifier.Disarm();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
ScriptPrecompiler::OffThreadCallback(void* aToken, void* aData)
|
|
|
|
{
|
|
|
|
nsRefPtr<NotifyPrecompilationCompleteRunnable> runnable =
|
|
|
|
dont_AddRef(static_cast<NotifyPrecompilationCompleteRunnable*>(aData));
|
|
|
|
runnable->SetToken(aToken);
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScriptPrecompiler::SendObserverNotification()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mChannel && mObserver);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
mObserver->Observe(uri, "script-precompiled", nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
mozJSSubScriptLoader::PrecompileScript(nsIURI* aURI,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
nsIObserver *aObserver)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
aURI, nullptr, nullptr, nullptr,
|
|
|
|
nsIRequest::LOAD_NORMAL, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<ScriptPrecompiler> loadObserver =
|
|
|
|
new ScriptPrecompiler(aObserver, aPrincipal, channel);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), loadObserver);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> listener = loader.get();
|
|
|
|
rv = channel->AsyncOpen(listener, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|