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"
|
2012-03-09 13:48:50 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
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
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
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"
|
2014-09-21 20:45:16 +04:00
|
|
|
#include "nsContentUtils.h"
|
2014-11-09 04:08:09 +03:00
|
|
|
#include "nsStringGlue.h"
|
2015-05-21 08:14:49 +03:00
|
|
|
#include "nsCycleCollectionParticipant.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;
|
2015-05-21 08:14:49 +03:00
|
|
|
using namespace mozilla::dom;
|
2013-11-05 19:35:41 +04:00
|
|
|
|
|
|
|
class MOZ_STACK_CLASS LoadSubScriptOptions : public OptionsBase {
|
|
|
|
public:
|
2015-03-29 01:22:11 +03:00
|
|
|
explicit LoadSubScriptOptions(JSContext* cx = xpc_GetSafeJSContext(),
|
|
|
|
JSObject* options = nullptr)
|
2013-11-05 19:35:41 +04:00
|
|
|
: OptionsBase(cx, options)
|
|
|
|
, target(cx)
|
|
|
|
, charset(NullString())
|
|
|
|
, ignoreCache(false)
|
2015-05-21 08:14:49 +03:00
|
|
|
, async(false)
|
2013-11-05 19:35:41 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool Parse() {
|
|
|
|
return ParseObject("target", &target) &&
|
|
|
|
ParseString("charset", charset) &&
|
2015-05-21 08:14:49 +03:00
|
|
|
ParseBoolean("ignoreCache", &ignoreCache) &&
|
|
|
|
ParseBoolean("async", &async);
|
2013-11-05 19:35:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RootedObject target;
|
|
|
|
nsString charset;
|
|
|
|
bool ignoreCache;
|
2015-05-21 08:14:49 +03:00
|
|
|
bool async;
|
2013-11-05 19:35:41 +04:00
|
|
|
};
|
|
|
|
|
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
|
2015-03-29 01:22:11 +03:00
|
|
|
ReportError(JSContext* cx, const char* msg)
|
2011-07-10 07:21:16 +04:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-09 04:08:09 +03:00
|
|
|
static nsresult
|
2015-03-29 01:22:11 +03:00
|
|
|
ReportError(JSContext* cx, const char* origMsg, nsIURI* uri)
|
2014-11-09 04:08:09 +03:00
|
|
|
{
|
|
|
|
if (!uri)
|
|
|
|
return ReportError(cx, origMsg);
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv = uri->GetSpec(spec);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
spec.Assign("(unknown)");
|
|
|
|
|
|
|
|
nsAutoCString msg(origMsg);
|
|
|
|
msg.Append(": ");
|
|
|
|
msg.Append(spec);
|
|
|
|
return ReportError(cx, msg.get());
|
|
|
|
}
|
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
nsresult
|
|
|
|
PrepareScript(nsIURI* uri,
|
|
|
|
JSContext* cx,
|
|
|
|
RootedObject& targetObj,
|
|
|
|
const char* uriStr,
|
|
|
|
const nsAString& charset,
|
|
|
|
const char* buf,
|
|
|
|
int64_t len,
|
|
|
|
bool reuseGlobal,
|
|
|
|
MutableHandleScript script,
|
|
|
|
MutableHandleFunction function)
|
|
|
|
{
|
|
|
|
JS::CompileOptions options(cx);
|
2015-07-02 02:26:08 +03:00
|
|
|
options.setFileAndLine(uriStr, 1)
|
|
|
|
.setVersion(JSVERSION_LATEST);
|
2015-05-21 08:14:49 +03:00
|
|
|
if (!charset.IsVoid()) {
|
|
|
|
char16_t* scriptBuf = nullptr;
|
|
|
|
size_t scriptLength = 0;
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
nsScriptLoader::ConvertToUTF16(nullptr, reinterpret_cast<const uint8_t*>(buf), len,
|
|
|
|
charset, nullptr, scriptBuf, scriptLength);
|
|
|
|
|
|
|
|
JS::SourceBufferHolder srcBuf(scriptBuf, scriptLength,
|
|
|
|
JS::SourceBufferHolder::GiveOwnership);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_BADCHARSET, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reuseGlobal) {
|
2015-06-21 21:49:57 +03:00
|
|
|
if (JS_IsGlobalObject(targetObj))
|
|
|
|
JS::Compile(cx, options, srcBuf, script);
|
|
|
|
else
|
|
|
|
JS::CompileForNonSyntacticScope(cx, options, srcBuf, script);
|
2015-05-21 08:14:49 +03:00
|
|
|
} else {
|
|
|
|
AutoObjectVector scopeChain(cx);
|
|
|
|
if (!JS_IsGlobalObject(targetObj) &&
|
|
|
|
!scopeChain.append(targetObj)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
// XXXbz do we really not care if the compile fails???
|
|
|
|
JS::CompileFunction(cx, scopeChain, options, nullptr, 0, nullptr,
|
|
|
|
srcBuf, function);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We only use lazy source when no special encoding is specified because
|
|
|
|
// the lazy source loader doesn't know the encoding.
|
|
|
|
if (!reuseGlobal) {
|
2015-06-21 21:49:57 +03:00
|
|
|
options.setSourceIsLazy(true);
|
|
|
|
if (JS_IsGlobalObject(targetObj))
|
|
|
|
JS::Compile(cx, options, buf, len, script);
|
|
|
|
else
|
|
|
|
JS::CompileForNonSyntacticScope(cx, options, buf, len, script);
|
2015-05-21 08:14:49 +03:00
|
|
|
} else {
|
|
|
|
AutoObjectVector scopeChain(cx);
|
|
|
|
if (!JS_IsGlobalObject(targetObj) &&
|
|
|
|
!scopeChain.append(targetObj)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
// XXXbz do we really not care if the compile fails???
|
|
|
|
JS::CompileFunction(cx, scopeChain, options, nullptr, 0, nullptr,
|
|
|
|
buf, len, function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
EvalScript(JSContext* cx,
|
|
|
|
RootedObject& target_obj,
|
|
|
|
MutableHandleValue retval,
|
|
|
|
nsIURI* uri,
|
|
|
|
bool cache,
|
|
|
|
RootedScript& script,
|
|
|
|
RootedFunction& function)
|
|
|
|
{
|
|
|
|
if (function) {
|
|
|
|
script = JS_GetFunctionScript(cx, function);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
if (function) {
|
|
|
|
ok = JS_CallFunction(cx, target_obj, function, JS::HandleValueArray::empty(),
|
|
|
|
retval);
|
|
|
|
} else {
|
|
|
|
if (JS_IsGlobalObject(target_obj)) {
|
|
|
|
ok = JS_ExecuteScript(cx, script, retval);
|
|
|
|
} else {
|
|
|
|
JS::AutoObjectVector scopeChain(cx);
|
|
|
|
ok = scopeChain.append(target_obj) &&
|
|
|
|
JS_ExecuteScript(cx, scopeChain, script, retval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
JSAutoCompartment rac(cx, target_obj);
|
|
|
|
if (!JS_WrapValue(cx, retval))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString cachePath;
|
|
|
|
JSVersion version = JS_GetVersion(cx);
|
|
|
|
cachePath.AppendPrintf("jssubloader/%d", version);
|
|
|
|
PathifyURI(uri, cachePath);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secman =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
|
|
|
|
if (!secman)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsresult rv = secman->GetSystemPrincipal(getter_AddRefs(principal));
|
|
|
|
if (NS_FAILED(rv) || !principal)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (cache && ok && !!script) {
|
|
|
|
WriteCachedScript(StartupCache::GetSingleton(),
|
|
|
|
cachePath, cx, principal, script);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AsyncScriptLoader : public nsIStreamLoaderObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AsyncScriptLoader)
|
|
|
|
|
|
|
|
AsyncScriptLoader(nsIChannel* aChannel, bool aReuseGlobal,
|
|
|
|
JSObject* aTargetObj, const nsAString& aCharset,
|
|
|
|
bool aCache, Promise* aPromise)
|
|
|
|
: mChannel(aChannel)
|
|
|
|
, mTargetObj(aTargetObj)
|
|
|
|
, mPromise(aPromise)
|
|
|
|
, mCharset(aCharset)
|
|
|
|
, mReuseGlobal(aReuseGlobal)
|
|
|
|
, mCache(aCache)
|
|
|
|
{
|
|
|
|
// Needed for the cycle collector to manage mTargetObj.
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~AsyncScriptLoader() {
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsIChannel> mChannel;
|
|
|
|
Heap<JSObject*> mTargetObj;
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
nsString mCharset;
|
|
|
|
bool mReuseGlobal;
|
|
|
|
bool mCache;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AsyncScriptLoader)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AsyncScriptLoader)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamLoaderObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AsyncScriptLoader)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromise)
|
|
|
|
tmp->mTargetObj = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AsyncScriptLoader)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(AsyncScriptLoader)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mTargetObj)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(AsyncScriptLoader)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(AsyncScriptLoader)
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS AutoRejectPromise
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoRejectPromise(JSContext* cx,
|
|
|
|
Promise* aPromise,
|
|
|
|
nsIGlobalObject* aGlobalObject)
|
|
|
|
: mCx(cx)
|
|
|
|
, mPromise(aPromise)
|
|
|
|
, mGlobalObject(aGlobalObject) {}
|
|
|
|
|
|
|
|
~AutoRejectPromise() {
|
|
|
|
if (mPromise) {
|
|
|
|
JS::Rooted<JS::Value> undefined(mCx, JS::UndefinedValue());
|
|
|
|
mPromise->MaybeReject(mCx, undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResolvePromise(HandleValue aResolveValue) {
|
|
|
|
mPromise->MaybeResolve(aResolveValue);
|
|
|
|
mPromise = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSContext* mCx;
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobalObject;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncScriptLoader::OnStreamComplete(nsIStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
uint32_t aLength,
|
|
|
|
const uint8_t* aBuf)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = xpc::NativeGlobal(mTargetObj);
|
|
|
|
AutoEntryScript aes(globalObject, "async loadSubScript");
|
|
|
|
JSContext* cx = aes.cx();
|
|
|
|
AutoRejectPromise autoPromise(cx, mPromise, globalObject);
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
ReportError(cx, "Unable to load script.", uri);
|
|
|
|
}
|
|
|
|
// Just notify that we are done with this load.
|
|
|
|
NS_ENSURE_SUCCESS(aStatus, NS_OK);
|
|
|
|
|
|
|
|
if (aLength == 0) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_NOCONTENT, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLength > INT32_MAX) {
|
|
|
|
return ReportError(cx, LOAD_ERROR_CONTENTTOOBIG, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
RootedFunction function(cx);
|
|
|
|
RootedScript script(cx);
|
|
|
|
nsAutoCString spec;
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
|
|
|
|
RootedObject target_obj(cx, mTargetObj);
|
|
|
|
|
|
|
|
nsresult rv = PrepareScript(uri, cx, target_obj, spec.get(),
|
|
|
|
mCharset,
|
|
|
|
reinterpret_cast<const char*>(aBuf), aLength,
|
|
|
|
mReuseGlobal, &script, &function);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> retval(cx);
|
|
|
|
rv = EvalScript(cx, target_obj, &retval, uri, mCache, script, function);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
autoPromise.ResolvePromise(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
mozJSSubScriptLoader::ReadScriptAsync(nsIURI* uri, JSObject* targetObjArg,
|
|
|
|
const nsAString& charset,
|
|
|
|
nsIIOService* serv, bool reuseGlobal,
|
|
|
|
bool cache, MutableHandleValue retval)
|
|
|
|
{
|
|
|
|
RootedObject target_obj(nsContentUtils::RootingCx(), targetObjArg);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = xpc::NativeGlobal(target_obj);
|
|
|
|
ErrorResult result;
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(globalObject))) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<Promise> promise = Promise::Create(globalObject, result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
promise = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugOnly<bool> asJS = ToJSValue(jsapi.cx(), promise, retval);
|
|
|
|
MOZ_ASSERT(asJS, "Should not fail to convert the promise to a JS value");
|
|
|
|
|
|
|
|
// We create a channel and call SetContentType, to avoid expensive MIME type
|
|
|
|
// lookups (bug 632490).
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
2015-09-20 06:26:09 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2015-05-21 08:14:49 +03:00
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
nullptr, // aLoadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
nsIRequest::LOAD_NORMAL,
|
|
|
|
serv);
|
|
|
|
|
|
|
|
if (!NS_SUCCEEDED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("application/javascript"));
|
|
|
|
|
|
|
|
nsRefPtr<AsyncScriptLoader> loadObserver =
|
|
|
|
new AsyncScriptLoader(channel,
|
|
|
|
reuseGlobal,
|
|
|
|
target_obj,
|
|
|
|
charset,
|
|
|
|
cache,
|
|
|
|
promise);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamLoader> loader;
|
|
|
|
rv = NS_NewStreamLoader(getter_AddRefs(loader), loadObserver);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> listener = loader.get();
|
2015-09-20 06:26:09 +03:00
|
|
|
return channel->AsyncOpen2(listener);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
nsresult
|
2015-03-29 01:22:11 +03:00
|
|
|
mozJSSubScriptLoader::ReadScript(nsIURI* uri, JSContext* cx, JSObject* targetObjArg,
|
|
|
|
const nsAString& charset, const char* uriStr,
|
|
|
|
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
|
|
|
{
|
2014-06-12 04:38:22 +04:00
|
|
|
script.set(nullptr);
|
|
|
|
function.set(nullptr);
|
2012-11-27 02:41:55 +04:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
RootedObject target_obj(cx, targetObjArg);
|
|
|
|
|
2015-01-12 07:26:40 +03:00
|
|
|
// We create a channel 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;
|
2014-09-21 20:45:16 +04:00
|
|
|
nsresult rv;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(chan),
|
|
|
|
uri,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
2015-09-20 06:26:09 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2014-09-21 20:45:16 +04:00
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
nullptr, // aLoadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
nsIRequest::LOAD_NORMAL,
|
|
|
|
serv);
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
chan->SetContentType(NS_LITERAL_CSTRING("application/javascript"));
|
2015-09-20 06:26:09 +03:00
|
|
|
rv = chan->Open2(getter_AddRefs(instream));
|
2011-07-10 07:21:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-11-09 04:08:09 +03:00
|
|
|
return ReportError(cx, LOAD_ERROR_NOSTREAM, uri);
|
2011-07-10 07:21:16 +04:00
|
|
|
}
|
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) {
|
2014-11-09 04:08:09 +03:00
|
|
|
return ReportError(cx, LOAD_ERROR_NOCONTENT, uri);
|
2011-07-10 07:21:16 +04:00
|
|
|
}
|
|
|
|
|
2012-10-22 21:51:07 +04:00
|
|
|
if (len > INT32_MAX) {
|
2014-11-09 04:08:09 +03:00
|
|
|
return ReportError(cx, LOAD_ERROR_CONTENTTOOBIG, uri);
|
2012-10-22 21:51:07 +04:00
|
|
|
}
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
nsCString buf;
|
|
|
|
rv = NS_ReadInputStreamToString(instream, buf, len);
|
2015-05-21 08:14:49 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-10 07:21:16 +04:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
rv = PrepareScript(uri, cx, target_obj, uriStr, charset,
|
|
|
|
buf.get(), len,
|
|
|
|
reuseGlobal,
|
|
|
|
script, function);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-10 07:21:16 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-18 14:09:16 +04:00
|
|
|
NS_IMETHODIMP
|
2015-03-29 01:22:11 +03:00
|
|
|
mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
|
2014-01-09 21:39:36 +04:00
|
|
|
HandleValue target,
|
2015-03-29 01:22:11 +03:00
|
|
|
const nsAString& charset,
|
|
|
|
JSContext* cx,
|
2014-01-09 21:39:36 +04:00
|
|
|
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
|
2015-03-29 01:22:11 +03:00
|
|
|
mozJSSubScriptLoader::LoadSubScriptWithOptions(const nsAString& url,
|
2014-01-09 21:39:36 +04:00
|
|
|
HandleValue optionsVal,
|
2015-03-29 01:22:11 +03:00
|
|
|
JSContext* cx,
|
2014-01-09 21:39:36 +04:00
|
|
|
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
|
2015-03-29 01:22:11 +03:00
|
|
|
mozJSSubScriptLoader::DoLoadSubScriptWithOptions(const nsAString& url,
|
|
|
|
LoadSubScriptOptions& options,
|
|
|
|
JSContext* cx,
|
2014-01-09 21:39:36 +04:00
|
|
|
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);
|
2015-03-29 01:22:11 +03: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)) {
|
2014-11-09 04:08:09 +03:00
|
|
|
return ReportError(cx, LOAD_ERROR_NOSCHEME, uri);
|
2008-03-18 06:46:53 +03:00
|
|
|
}
|
2008-03-21 08:07:25 +03:00
|
|
|
|
2014-12-17 02:32:28 +03:00
|
|
|
if (!scheme.EqualsLiteral("chrome") && !scheme.EqualsLiteral("app")) {
|
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) {
|
2014-11-09 04:08:09 +03:00
|
|
|
return ReportError(cx, LOAD_ERROR_URI_NOT_LOCAL, uri);
|
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-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);
|
2015-05-21 08:14:49 +03:00
|
|
|
|
|
|
|
// If we are doing an async load, trigger it and bail out.
|
|
|
|
if (!script && options.async) {
|
|
|
|
return ReadScriptAsync(uri, targetObj, options.charset, serv,
|
|
|
|
reusingGlobal, !!cache, retval);
|
|
|
|
}
|
|
|
|
|
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);
|
2015-05-21 08:14:49 +03:00
|
|
|
} else {
|
|
|
|
cache = nullptr;
|
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
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
return EvalScript(cx, targetObj, retval, uri, !!cache, script, function);
|
2001-03-14 05:41:55 +03:00
|
|
|
}
|
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
|
|
|
{}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static void OffThreadCallback(void* aToken, void* aData);
|
2014-06-23 22:49:08 +04:00
|
|
|
|
|
|
|
/* 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-07-22 08:43:21 +04:00
|
|
|
char16_t* mScriptBuf;
|
2014-04-25 18:11:57 +04:00
|
|
|
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
|
|
|
|
|
2014-09-01 05:06:35 +04:00
|
|
|
explicit NotifyPrecompilationCompleteRunnable(ScriptPrecompiler* aPrecompiler)
|
2014-04-18 09:03:03 +04:00
|
|
|
: 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:
|
2014-09-01 05:06:35 +04:00
|
|
|
explicit AutoSendObserverNotification(ScriptPrecompiler* aPrecompiler)
|
2014-04-18 09:03:03 +04:00
|
|
|
: 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) {
|
2015-03-29 01:22:11 +03:00
|
|
|
JSRuntime* rt = XPCJSRuntime::Get()->Runtime();
|
2014-04-18 09:03:03 +04:00
|
|
|
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);
|
|
|
|
|
2014-07-22 08:43:21 +04:00
|
|
|
// Convert data to char16_t* and prepare to call CompileOffThread.
|
2014-04-18 09:03:03 +04:00
|
|
|
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.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,
|
2015-03-29 01:22:11 +03:00
|
|
|
nsIObserver* aObserver)
|
2014-04-18 09:03:03 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(channel),
|
2014-09-21 20:45:16 +04:00
|
|
|
aURI,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
2015-09-20 06:26:09 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2014-09-21 20:45:16 +04:00
|
|
|
nsIContentPolicy::TYPE_OTHER);
|
|
|
|
|
2014-04-18 09:03:03 +04:00
|
|
|
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();
|
2015-09-20 06:26:09 +03:00
|
|
|
rv = channel->AsyncOpen2(listener);
|
2014-04-18 09:03:03 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|