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"
|
2016-08-23 07:09:32 +03: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
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
static void
|
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
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
static void
|
2015-03-29 01:22:11 +03:00
|
|
|
ReportError(JSContext* cx, const char* origMsg, nsIURI* uri)
|
2014-11-09 04:08:09 +03:00
|
|
|
{
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!uri) {
|
|
|
|
ReportError(cx, origMsg);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-09 04:08:09 +03:00
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv = uri->GetSpec(spec);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
spec.Assign("(unknown)");
|
|
|
|
|
|
|
|
nsAutoCString msg(origMsg);
|
|
|
|
msg.Append(": ");
|
|
|
|
msg.Append(spec);
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, msg.get());
|
2014-11-09 04:08:09 +03:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
bool
|
2015-05-21 08:14:49 +03:00
|
|
|
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);
|
2016-10-15 05:47:00 +03:00
|
|
|
// Use line 0 to make the function body starts from line 1 when
|
|
|
|
// |reuseGlobal == true|.
|
|
|
|
options.setFileAndLine(uriStr, reuseGlobal ? 0 : 1)
|
2015-07-02 02:26:08 +03:00
|
|
|
.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)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_BADCHARSET, uri);
|
|
|
|
return false;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!reuseGlobal) {
|
2016-12-03 04:36:42 +03:00
|
|
|
if (JS_IsGlobalObject(targetObj)) {
|
|
|
|
return JS::Compile(cx, options, srcBuf, script);
|
|
|
|
}
|
|
|
|
return JS::CompileForNonSyntacticScope(cx, options, srcBuf, script);
|
2015-05-21 08:14:49 +03:00
|
|
|
} else {
|
2016-08-25 11:28:47 +03:00
|
|
|
AutoObjectVector envChain(cx);
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!JS_IsGlobalObject(targetObj) && !envChain.append(targetObj)) {
|
|
|
|
return false;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
2016-12-03 04:36:42 +03:00
|
|
|
return JS::CompileFunction(cx, envChain, options, nullptr, 0, nullptr,
|
|
|
|
srcBuf, function);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
} 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);
|
2016-12-03 04:36:42 +03:00
|
|
|
if (JS_IsGlobalObject(targetObj)) {
|
|
|
|
return JS::Compile(cx, options, buf, len, script);
|
|
|
|
}
|
|
|
|
return JS::CompileForNonSyntacticScope(cx, options, buf, len, script);
|
2015-05-21 08:14:49 +03:00
|
|
|
} else {
|
2016-08-25 11:28:47 +03:00
|
|
|
AutoObjectVector envChain(cx);
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!JS_IsGlobalObject(targetObj) && !envChain.append(targetObj)) {
|
|
|
|
return false;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
2016-12-03 04:36:42 +03:00
|
|
|
return JS::CompileFunction(cx, envChain, options, nullptr, 0, nullptr,
|
|
|
|
buf, len, function);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
bool
|
2015-05-21 08:14:49 +03:00
|
|
|
EvalScript(JSContext* cx,
|
|
|
|
RootedObject& target_obj,
|
|
|
|
MutableHandleValue retval,
|
|
|
|
nsIURI* uri,
|
|
|
|
bool cache,
|
|
|
|
RootedScript& script,
|
|
|
|
RootedFunction& function)
|
|
|
|
{
|
|
|
|
if (function) {
|
|
|
|
script = JS_GetFunctionScript(cx, function);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (function) {
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!JS_CallFunction(cx, target_obj, function, JS::HandleValueArray::empty(), retval)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
} else {
|
|
|
|
if (JS_IsGlobalObject(target_obj)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!JS_ExecuteScript(cx, script, retval)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
} else {
|
2016-08-25 11:28:47 +03:00
|
|
|
JS::AutoObjectVector envChain(cx);
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!envChain.append(target_obj)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!JS_ExecuteScript(cx, envChain, script, retval)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
JSAutoCompartment rac(cx, target_obj);
|
|
|
|
if (!JS_WrapValue(cx, retval)) {
|
|
|
|
return false;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
if (cache && !!script) {
|
|
|
|
nsAutoCString cachePath;
|
|
|
|
JSVersion version = JS_GetVersion(cx);
|
|
|
|
cachePath.AppendPrintf("jssubloader/%d", version);
|
|
|
|
PathifyURI(uri, cachePath);
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secman =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
|
|
|
|
if (!secman) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsresult rv = secman->GetSystemPrincipal(getter_AddRefs(principal));
|
|
|
|
if (NS_FAILED(rv) || !principal) {
|
|
|
|
ReportError(cx, LOAD_ERROR_NOPRINCIPALS, uri);
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
|
|
|
|
WriteCachedScript(StartupCache::GetSingleton(),
|
|
|
|
cachePath, cx, principal, script);
|
|
|
|
}
|
2016-12-03 04:36:42 +03:00
|
|
|
|
|
|
|
return true;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2015-11-30 17:54:11 +03:00
|
|
|
class AsyncScriptLoader : public nsIIncrementalStreamLoaderObserver
|
2015-05-21 08:14:49 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2015-11-30 17:54:11 +03:00
|
|
|
NS_DECL_NSIINCREMENTALSTREAMLOADEROBSERVER
|
2015-05-21 08:14:49 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIChannel> mChannel;
|
2015-05-21 08:14:49 +03:00
|
|
|
Heap<JSObject*> mTargetObj;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> mPromise;
|
2015-05-21 08:14:49 +03:00
|
|
|
nsString mCharset;
|
|
|
|
bool mReuseGlobal;
|
|
|
|
bool mCache;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AsyncScriptLoader)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AsyncScriptLoader)
|
2015-11-30 17:54:11 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIIncrementalStreamLoaderObserver)
|
2015-05-21 08:14:49 +03:00
|
|
|
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_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
|
|
|
|
{
|
2016-12-03 04:36:42 +03:00
|
|
|
public:
|
|
|
|
AutoRejectPromise(AutoEntryScript& aAutoEntryScript,
|
2015-05-21 08:14:49 +03:00
|
|
|
Promise* aPromise,
|
|
|
|
nsIGlobalObject* aGlobalObject)
|
2016-12-03 04:36:42 +03:00
|
|
|
: mAutoEntryScript(aAutoEntryScript)
|
|
|
|
, mPromise(aPromise)
|
|
|
|
, mGlobalObject(aGlobalObject) {}
|
2015-05-21 08:14:49 +03:00
|
|
|
|
|
|
|
~AutoRejectPromise() {
|
2016-12-03 04:36:42 +03:00
|
|
|
if (mPromise) {
|
|
|
|
JSContext* cx = mAutoEntryScript.cx();
|
|
|
|
RootedValue rejectionValue(cx, JS::UndefinedValue());
|
|
|
|
if (mAutoEntryScript.HasException()) {
|
|
|
|
Unused << mAutoEntryScript.PeekException(&rejectionValue);
|
|
|
|
}
|
|
|
|
mPromise->MaybeReject(cx, rejectionValue);
|
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResolvePromise(HandleValue aResolveValue) {
|
2016-12-03 04:36:42 +03:00
|
|
|
mPromise->MaybeResolve(aResolveValue);
|
|
|
|
mPromise = nullptr;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
private:
|
|
|
|
AutoEntryScript& mAutoEntryScript;
|
|
|
|
RefPtr<Promise> mPromise;
|
2015-05-21 08:14:49 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobalObject;
|
|
|
|
};
|
|
|
|
|
2015-12-01 17:00:58 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncScriptLoader::OnIncrementalData(nsIIncrementalStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
uint32_t aDataLength,
|
|
|
|
const uint8_t* aData,
|
|
|
|
uint32_t *aConsumedData)
|
|
|
|
{
|
2016-12-03 04:36:42 +03:00
|
|
|
return NS_OK;
|
2015-12-01 17:00:58 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
NS_IMETHODIMP
|
2015-11-30 17:54:11 +03:00
|
|
|
AsyncScriptLoader::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
|
2015-05-21 08:14:49 +03:00
|
|
|
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");
|
2016-12-03 04:36:42 +03:00
|
|
|
AutoRejectPromise autoPromise(aes, mPromise, globalObject);
|
2015-05-21 08:14:49 +03:00
|
|
|
JSContext* cx = aes.cx();
|
|
|
|
|
|
|
|
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) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_NOCONTENT, uri);
|
|
|
|
return NS_OK;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aLength > INT32_MAX) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_CONTENTTOOBIG, uri);
|
|
|
|
return NS_OK;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RootedFunction function(cx);
|
|
|
|
RootedScript script(cx);
|
|
|
|
nsAutoCString spec;
|
2016-08-30 07:22:04 +03:00
|
|
|
nsresult rv = uri->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-21 08:14:49 +03:00
|
|
|
|
|
|
|
RootedObject target_obj(cx, mTargetObj);
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!PrepareScript(uri, cx, target_obj, spec.get(), mCharset,
|
2016-08-30 07:22:04 +03:00
|
|
|
reinterpret_cast<const char*>(aBuf), aLength,
|
2016-12-03 04:36:42 +03:00
|
|
|
mReuseGlobal, &script, &function))
|
|
|
|
{
|
|
|
|
return NS_OK;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> retval(cx);
|
2016-12-03 04:36:42 +03:00
|
|
|
if (EvalScript(cx, target_obj, &retval, uri, mCache, script, function)) {
|
2015-05-21 08:14:49 +03:00
|
|
|
autoPromise.ResolvePromise(retval);
|
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
return NS_OK;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
mozJSSubScriptLoader::ReadScriptAsync(nsIURI* uri, JSObject* targetObjArg,
|
|
|
|
const nsAString& charset,
|
|
|
|
nsIIOService* serv, bool reuseGlobal,
|
|
|
|
bool cache, MutableHandleValue retval)
|
|
|
|
{
|
2016-08-11 15:39:23 +03:00
|
|
|
RootedObject target_obj(RootingCx(), targetObjArg);
|
2015-05-21 08:14:49 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = xpc::NativeGlobal(target_obj);
|
|
|
|
ErrorResult result;
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(globalObject))) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(globalObject, result);
|
2015-05-21 08:14:49 +03:00
|
|
|
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"));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncScriptLoader> loadObserver =
|
2015-05-21 08:14:49 +03:00
|
|
|
new AsyncScriptLoader(channel,
|
|
|
|
reuseGlobal,
|
|
|
|
target_obj,
|
|
|
|
charset,
|
|
|
|
cache,
|
|
|
|
promise);
|
|
|
|
|
2015-11-30 17:54:11 +03:00
|
|
|
nsCOMPtr<nsIIncrementalStreamLoader> loader;
|
|
|
|
rv = NS_NewIncrementalStreamLoader(getter_AddRefs(loader), loadObserver);
|
2015-05-21 08:14:49 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
bool
|
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)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_NOSTREAM, uri);
|
|
|
|
return false;
|
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) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_NOCONTENT, uri);
|
|
|
|
return false;
|
2011-07-10 07:21:16 +04:00
|
|
|
}
|
|
|
|
|
2012-10-22 21:51:07 +04:00
|
|
|
if (len > INT32_MAX) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_CONTENTTOOBIG, uri);
|
|
|
|
return false;
|
2012-10-22 21:51:07 +04:00
|
|
|
}
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
nsCString buf;
|
|
|
|
rv = NS_ReadInputStreamToString(instream, buf, len);
|
2016-12-03 04:36:42 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2011-07-10 07:21:16 +04:00
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
return PrepareScript(uri, cx, target_obj, uriStr, charset,
|
|
|
|
buf.get(), len,
|
|
|
|
reuseGlobal,
|
|
|
|
script, function);
|
2011-07-10 07:21:16 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
2016-03-09 13:20:11 +03:00
|
|
|
JS::AutoFilename filename;
|
2014-02-25 19:43:14 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-27 03:36:09 +03:00
|
|
|
JSAutoCompartment ac(cx, targetObj);
|
|
|
|
|
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) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_NOSERVICE);
|
|
|
|
return NS_OK;
|
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)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_NOURI);
|
|
|
|
return NS_OK;
|
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)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_NOSPEC);
|
|
|
|
return NS_OK;
|
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)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_NOSCHEME, uri);
|
|
|
|
return NS_OK;
|
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) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_URI_NOT_LOCAL, uri);
|
|
|
|
return NS_OK;
|
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);
|
2016-03-22 10:48:38 +03:00
|
|
|
if (cache && !options.ignoreCache) {
|
2013-09-20 13:22:59 +04:00
|
|
|
rv = ReadCachedScript(cache, cachePath, cx, mSystemPrincipal, &script);
|
2016-03-22 10:48:38 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// ReadCachedScript may have set a pending exception.
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!ReadScript(uri, cx, targetObj, options.charset,
|
2011-12-18 14:09:16 +04:00
|
|
|
static_cast<const char*>(uriStr.get()), serv,
|
2016-12-03 04:36:42 +03:00
|
|
|
principal, reusingGlobal, &script, &function))
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
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
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
Unused << EvalScript(cx, targetObj, retval, uri, !!cache, script, function);
|
|
|
|
return NS_OK;
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-11-30 17:54:11 +03:00
|
|
|
class ScriptPrecompiler : public nsIIncrementalStreamLoaderObserver
|
2014-04-18 09:03:03 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2015-11-30 17:54:11 +03:00
|
|
|
NS_DECL_NSIINCREMENTALSTREAMLOADEROBSERVER
|
2014-04-18 09:03:03 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIObserver> mObserver;
|
|
|
|
RefPtr<nsIPrincipal> mPrincipal;
|
|
|
|
RefPtr<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
|
|
|
};
|
|
|
|
|
2015-11-30 17:54:11 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ScriptPrecompiler, nsIIncrementalStreamLoaderObserver);
|
2014-04-18 09:03:03 +04:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class NotifyPrecompilationCompleteRunnable : public Runnable
|
2014-04-18 09:03:03 +04:00
|
|
|
{
|
|
|
|
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:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScriptPrecompiler> mPrecompiler;
|
2014-04-18 09:03:03 +04:00
|
|
|
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) {
|
2016-09-14 16:48:17 +03:00
|
|
|
JSContext* cx = XPCJSContext::Get()->Context();
|
2016-07-19 10:19:54 +03:00
|
|
|
NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE);
|
|
|
|
JS::CancelOffThreadScript(cx, mToken);
|
2014-04-18 09:03:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-01 17:00:58 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ScriptPrecompiler::OnIncrementalData(nsIIncrementalStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
|
|
|
uint32_t aDataLength,
|
|
|
|
const uint8_t* aData,
|
|
|
|
uint32_t *aConsumedData)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-18 09:03:03 +04:00
|
|
|
NS_IMETHODIMP
|
2015-11-30 17:54:11 +03:00
|
|
|
ScriptPrecompiler::OnStreamComplete(nsIIncrementalStreamLoader* aLoader,
|
2014-04-18 09:03:03 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NotifyPrecompilationCompleteRunnable> runnable =
|
2014-04-18 09:03:03 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << runnable.forget();
|
2014-04-18 09:03:03 +04:00
|
|
|
notifier.Disarm();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
ScriptPrecompiler::OffThreadCallback(void* aToken, void* aData)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NotifyPrecompilationCompleteRunnable> runnable =
|
2014-04-18 09:03:03 +04:00
|
|
|
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);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ScriptPrecompiler> loadObserver =
|
2014-04-18 09:03:03 +04:00
|
|
|
new ScriptPrecompiler(aObserver, aPrincipal, channel);
|
|
|
|
|
2015-11-30 17:54:11 +03:00
|
|
|
nsCOMPtr<nsIIncrementalStreamLoader> loader;
|
|
|
|
rv = NS_NewIncrementalStreamLoader(getter_AddRefs(loader), loadObserver);
|
2014-04-18 09:03:03 +04:00
|
|
|
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;
|
|
|
|
}
|