2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-04-03 15:58:00 +04:00
|
|
|
/* 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"
|
2001-03-14 05:41:55 +03:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-03-13 17:45:02 +03:00
|
|
|
#include "jsfriendapi.h"
|
2013-11-05 19:35:41 +04:00
|
|
|
#include "xpcprivate.h" // For xpc::OptionsBase
|
2018-08-25 06:51:49 +03:00
|
|
|
#include "js/CompilationAndEvaluation.h"
|
2018-11-09 05:42:48 +03:00
|
|
|
#include "js/SourceText.h"
|
2018-02-21 19:30:19 +03:00
|
|
|
#include "js/Wrapper.h"
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2018-07-17 22:38:48 +03:00
|
|
|
#include "mozilla/ContentPrincipal.h"
|
2015-05-21 08:14:49 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2017-05-08 09:24:22 +03:00
|
|
|
#include "mozilla/dom/ScriptLoader.h"
|
2015-05-21 08:14:49 +03:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2017-05-04 08:06:33 +03:00
|
|
|
#include "mozilla/ScriptPreloader.h"
|
2018-07-17 22:38:19 +03:00
|
|
|
#include "mozilla/SystemPrincipal.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"
|
2017-12-07 03:52:51 +03:00
|
|
|
#include "nsString.h"
|
2015-05-21 08:14:49 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2017-06-15 01:58:25 +03:00
|
|
|
#include "GeckoProfiler.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),
|
|
|
|
ignoreCache(false),
|
2015-05-21 08:14:49 +03:00
|
|
|
async(false),
|
2017-04-15 19:32:34 +03:00
|
|
|
wantReturnValue(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual bool Parse() override {
|
2018-12-09 02:14:27 +03:00
|
|
|
return ParseObject("target", &target) &&
|
2015-05-21 08:14:49 +03:00
|
|
|
ParseBoolean("ignoreCache", &ignoreCache) &&
|
2017-04-15 19:32:34 +03:00
|
|
|
ParseBoolean("async", &async) &&
|
|
|
|
ParseBoolean("wantReturnValue", &wantReturnValue);
|
2013-11-05 19:35:41 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-05 19:35:41 +04:00
|
|
|
RootedObject target;
|
|
|
|
bool ignoreCache;
|
2015-05-21 08:14:49 +03:00
|
|
|
bool async;
|
2017-04-15 19:32:34 +03:00
|
|
|
bool wantReturnValue;
|
2013-11-05 19:35:41 +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"
|
2008-03-18 06:46:53 +03:00
|
|
|
#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
|
|
|
|
2017-04-15 00:13:19 +03:00
|
|
|
mozJSSubScriptLoader::mozJSSubScriptLoader() {}
|
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
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(mozJSSubScriptLoader, mozIJSSubScriptLoader)
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2017-09-13 01:05:08 +03:00
|
|
|
#define JSSUB_CACHE_PREFIX(aType) "jssubloader/" aType
|
|
|
|
|
|
|
|
static void SubscriptCachePath(JSContext* cx, nsIURI* uri,
|
|
|
|
JS::HandleObject targetObj,
|
|
|
|
nsACString& cachePath) {
|
2017-11-17 14:13:15 +03:00
|
|
|
// StartupCache must distinguish between non-syntactic vs global when
|
|
|
|
// computing the cache key.
|
2018-09-08 01:12:01 +03:00
|
|
|
if (!JS_IsGlobalObject(targetObj)) {
|
|
|
|
cachePath.AssignLiteral(JSSUB_CACHE_PREFIX("non-syntactic"));
|
|
|
|
} else {
|
|
|
|
cachePath.AssignLiteral(JSSUB_CACHE_PREFIX("global"));
|
|
|
|
}
|
2017-09-13 01:05:08 +03:00
|
|
|
PathifyURI(uri, cachePath);
|
|
|
|
}
|
|
|
|
|
2017-03-18 02:53:04 +03:00
|
|
|
static void ReportError(JSContext* cx, const nsACString& msg) {
|
|
|
|
NS_ConvertUTF8toUTF16 ucMsg(msg);
|
|
|
|
|
|
|
|
RootedValue exn(cx);
|
|
|
|
if (xpc::NonVoidStringToJsval(cx, ucMsg, &exn)) {
|
|
|
|
JS_SetPendingException(cx, exn);
|
|
|
|
}
|
2011-07-10 07:21:16 +04:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static void ReportError(JSContext* cx, const char* origMsg, nsIURI* uri) {
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!uri) {
|
2017-03-18 02:53:04 +03:00
|
|
|
ReportError(cx, nsDependentCString(origMsg));
|
2016-12-03 04:36:42 +03:00
|
|
|
return;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-11-09 04:08:09 +03:00
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv = uri->GetSpec(spec);
|
2018-09-12 21:14:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-09-04 08:12:56 +03:00
|
|
|
spec.AssignLiteral("(unknown)");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-11-09 04:08:09 +03:00
|
|
|
nsAutoCString msg(origMsg);
|
2017-09-04 08:14:11 +03:00
|
|
|
msg.AppendLiteral(": ");
|
2014-11-09 04:08:09 +03:00
|
|
|
msg.Append(spec);
|
2017-03-18 02:53:04 +03:00
|
|
|
ReportError(cx, msg);
|
2016-12-03 04:36:42 +03:00
|
|
|
}
|
2014-11-09 04:08:09 +03:00
|
|
|
|
2019-04-09 20:43:52 +03:00
|
|
|
static bool PrepareScript(nsIURI* uri, JSContext* cx, bool wantGlobalScript,
|
|
|
|
const char* uriStr, const char* buf, int64_t len,
|
|
|
|
bool wantReturnValue, MutableHandleScript script) {
|
2014-11-09 04:08:09 +03:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setFileAndLine(uriStr, 1).setNoScriptRval(!wantReturnValue);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-12-26 23:37:42 +03:00
|
|
|
// This presumes that no one else might be compiling a script for this
|
|
|
|
// (URL, syntactic-or-not) key *not* using UTF-8. Seeing as JS source can
|
|
|
|
// only be compiled as UTF-8 or UTF-16 now -- there isn't a JSAPI function to
|
|
|
|
// compile Latin-1 now -- this presumption seems relatively safe.
|
|
|
|
//
|
|
|
|
// This also presumes that lazy parsing is disabled, for the sake of the
|
|
|
|
// startup cache. If lazy parsing is ever enabled for pertinent scripts that
|
|
|
|
// pass through here, we may need to disable lazy source for them.
|
|
|
|
options.setSourceIsLazy(true);
|
|
|
|
|
2018-07-04 04:26:04 +03:00
|
|
|
if (wantGlobalScript) {
|
2019-04-09 20:43:52 +03:00
|
|
|
return JS::CompileUtf8(cx, options, buf, len, script);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
2019-04-09 20:43:52 +03:00
|
|
|
return JS::CompileUtf8ForNonSyntacticScope(cx, options, buf, len, script);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool EvalScript(JSContext* cx, HandleObject targetObj,
|
|
|
|
HandleObject loadScope, MutableHandleValue retval,
|
2017-05-04 08:06:33 +03:00
|
|
|
nsIURI* uri, bool startupCache, bool preloadCache,
|
2017-05-17 19:21:54 +03:00
|
|
|
MutableHandleScript script) {
|
2018-07-06 19:16:24 +03:00
|
|
|
MOZ_ASSERT(!js::IsWrapper(targetObj));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-17 19:21:54 +03:00
|
|
|
if (JS_IsGlobalObject(targetObj)) {
|
|
|
|
if (!JS::CloneAndExecuteScript(cx, script, retval)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-10 21:23:32 +03:00
|
|
|
} else if (js::IsJSMEnvironment(targetObj)) {
|
|
|
|
if (!ExecuteInJSMEnvironment(cx, script, targetObj)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
retval.setUndefined();
|
2015-05-21 08:14:49 +03:00
|
|
|
} else {
|
2019-03-26 17:00:51 +03:00
|
|
|
JS::RootedObjectVector envChain(cx);
|
2017-05-17 19:21:54 +03:00
|
|
|
if (!envChain.append(targetObj)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-10 21:23:32 +03:00
|
|
|
if (!loadScope) {
|
2018-12-08 19:35:06 +03:00
|
|
|
// A null loadScope means we are cross-realm. In this case, we should
|
|
|
|
// check the target isn't in the JSM loader shared-global or we will
|
|
|
|
// contaminate all JSMs in the realm.
|
2017-09-10 21:23:32 +03:00
|
|
|
//
|
|
|
|
// NOTE: If loadScope is already a shared-global JSM, we can't
|
|
|
|
// determine which JSM the target belongs to and have to assume it
|
|
|
|
// is in our JSM.
|
2017-09-14 13:35:00 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2018-07-06 19:16:24 +03:00
|
|
|
JSObject* targetGlobal = JS::GetNonCCWObjectGlobal(targetObj);
|
2017-09-10 21:23:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(
|
|
|
|
!mozJSComponentLoader::Get()->IsLoaderGlobal(targetGlobal),
|
|
|
|
"Don't load subscript into target in a shared-global JSM");
|
2017-09-14 13:35:00 +03:00
|
|
|
#endif
|
2017-09-10 21:23:32 +03:00
|
|
|
if (!JS::CloneAndExecuteScript(cx, envChain, script, retval)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-09-10 21:23:32 +03:00
|
|
|
} else if (JS_IsGlobalObject(loadScope)) {
|
|
|
|
if (!JS::CloneAndExecuteScript(cx, envChain, script, retval)) {
|
2017-09-10 21:23:32 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-09-10 21:23:32 +03:00
|
|
|
MOZ_ASSERT(js::IsJSMEnvironment(loadScope));
|
|
|
|
if (!js::ExecuteInJSMEnvironment(cx, script, loadScope, envChain)) {
|
2017-09-10 21:23:32 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-09-10 21:23:32 +03:00
|
|
|
retval.setUndefined();
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2017-09-13 01:05:08 +03:00
|
|
|
JSAutoRealm rar(cx, targetObj);
|
2017-05-04 08:06:33 +03:00
|
|
|
if (!JS_WrapValue(cx, retval)) {
|
2017-06-15 01:38:59 +03:00
|
|
|
return false;
|
2017-05-04 08:06:33 +03:00
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2017-05-16 01:04:45 +03:00
|
|
|
if (script && (startupCache || preloadCache)) {
|
2017-07-19 00:47:27 +03:00
|
|
|
nsAutoCString cachePath;
|
2015-05-21 08:14:49 +03:00
|
|
|
SubscriptCachePath(cx, uri, targetObj, cachePath);
|
|
|
|
|
2017-05-04 08:06:33 +03:00
|
|
|
nsCString uriStr;
|
|
|
|
if (preloadCache && NS_SUCCEEDED(uri->GetSpec(uriStr))) {
|
2017-06-15 01:38:59 +03:00
|
|
|
// Note that, when called during startup, this will keep the
|
2015-05-21 08:14:49 +03:00
|
|
|
// original JSScript object alive for an indefinite amount of time.
|
2017-06-15 01:38:59 +03:00
|
|
|
// This has the side-effect of keeping the global that the script
|
|
|
|
// was compiled for alive, too.
|
2018-11-30 13:46:48 +03:00
|
|
|
//
|
2017-06-15 01:38:59 +03:00
|
|
|
// For most startups, the global in question will be the
|
2015-05-21 08:14:49 +03:00
|
|
|
// CompilationScope, since we pre-compile any scripts that were
|
|
|
|
// needed during the last startup in that scope. But for startups
|
2017-06-15 01:38:59 +03:00
|
|
|
// when a non-cached script is used (e.g., after add-on
|
|
|
|
// installation), this may be a Sandbox global, which may be
|
2018-07-04 04:26:04 +03:00
|
|
|
// nuked but held alive by the JSScript. We can avoid this problem
|
|
|
|
// by using a different scope when compiling the script. See
|
2015-05-21 08:14:49 +03:00
|
|
|
// useCompilationScope in ReadScript().
|
2018-11-30 13:46:48 +03:00
|
|
|
//
|
2015-05-21 08:14:49 +03:00
|
|
|
// In general, this isn't a problem, since add-on Sandboxes which
|
|
|
|
// use the script preloader are not destroyed until add-on shutdown,
|
2017-06-15 01:38:59 +03:00
|
|
|
// and when add-ons are uninstalled or upgraded, the preloader cache
|
|
|
|
// is immediately flushed after shutdown. But it's possible to
|
|
|
|
// disable and reenable an add-on without uninstalling it, leading
|
|
|
|
// to cached scripts being held alive, and tied to nuked Sandbox
|
2015-05-21 08:14:49 +03:00
|
|
|
// globals. Given the unusual circumstances required to trigger
|
|
|
|
// this, it's not a major concern. But it should be kept in mind.
|
|
|
|
ScriptPreloader::GetSingleton().NoteScript(uriStr, cachePath, script);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-05-04 08:06:33 +03:00
|
|
|
if (startupCache) {
|
2015-05-21 08:14:49 +03:00
|
|
|
JSAutoRealm ar(cx, script);
|
|
|
|
WriteCachedScript(StartupCache::GetSingleton(), cachePath, cx, script);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2015-11-30 17:54:11 +03:00
|
|
|
class AsyncScriptLoader : public nsIIncrementalStreamLoaderObserver {
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2015-05-21 08:14:49 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2015-11-30 17:54:11 +03:00
|
|
|
NS_DECL_NSIINCREMENTALSTREAMLOADEROBSERVER
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AsyncScriptLoader)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-16 01:04:45 +03:00
|
|
|
AsyncScriptLoader(nsIChannel* aChannel, bool aWantReturnValue,
|
2018-12-15 01:25:56 +03:00
|
|
|
JSObject* aTargetObj, JSObject* aLoadScope, bool aCache,
|
|
|
|
Promise* aPromise)
|
2015-05-21 08:14:49 +03:00
|
|
|
: mChannel(aChannel),
|
|
|
|
mTargetObj(aTargetObj),
|
2017-07-19 00:47:27 +03:00
|
|
|
mLoadScope(aLoadScope),
|
2015-05-21 08:14:49 +03:00
|
|
|
mPromise(aPromise),
|
2017-04-15 19:32:34 +03:00
|
|
|
mWantReturnValue(aWantReturnValue),
|
2015-05-21 08:14:49 +03:00
|
|
|
mCache(aCache) {
|
|
|
|
// Needed for the cycle collector to manage mTargetObj.
|
|
|
|
mozilla::HoldJSObjects(this);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-05-21 08:14:49 +03:00
|
|
|
virtual ~AsyncScriptLoader() { mozilla::DropJSObjects(this); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-15 00:13:19 +03:00
|
|
|
RefPtr<nsIChannel> mChannel;
|
|
|
|
Heap<JSObject*> mTargetObj;
|
2017-07-19 00:47:27 +03:00
|
|
|
Heap<JSObject*> mLoadScope;
|
2017-04-15 00:13:19 +03:00
|
|
|
RefPtr<Promise> mPromise;
|
2017-04-15 19:32:34 +03:00
|
|
|
bool mWantReturnValue;
|
2017-04-15 00:13:19 +03:00
|
|
|
bool mCache;
|
2015-05-21 08:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2017-07-19 00:47:27 +03:00
|
|
|
tmp->mLoadScope = nullptr;
|
2015-05-21 08:14:49 +03:00
|
|
|
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)
|
2017-07-19 00:47:27 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mLoadScope)
|
2015-05-21 08:14:49 +03:00
|
|
|
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, Promise* aPromise,
|
2015-05-21 08:14:49 +03:00
|
|
|
nsIGlobalObject* aGlobalObject)
|
2016-12-03 04:36:42 +03:00
|
|
|
: mAutoEntryScript(aAutoEntryScript),
|
|
|
|
mPromise(aPromise),
|
|
|
|
mGlobalObject(aGlobalObject) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-12-03 04:36:42 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
private:
|
2017-04-15 00:13:19 +03:00
|
|
|
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));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
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();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
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);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
if (aLength == 0) {
|
2016-12-03 04:36:42 +03:00
|
|
|
ReportError(cx, LOAD_ERROR_NOCONTENT, uri);
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
RootedScript script(cx);
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv = uri->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2017-04-15 00:10:40 +03:00
|
|
|
RootedObject targetObj(cx, mTargetObj);
|
2015-05-21 08:14:49 +03:00
|
|
|
RootedObject loadScope(cx, mLoadScope);
|
|
|
|
|
2019-04-09 20:43:52 +03:00
|
|
|
if (!PrepareScript(uri, cx, JS_IsGlobalObject(targetObj), spec.get(),
|
|
|
|
reinterpret_cast<const char*>(aBuf), aLength,
|
|
|
|
mWantReturnValue, &script)) {
|
2017-07-19 00:47:27 +03:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2018-07-04 04:26:04 +03:00
|
|
|
JS::Rooted<JS::Value> retval(cx);
|
|
|
|
if (EvalScript(cx, targetObj, loadScope, &retval, uri, mCache,
|
2017-05-17 19:21:54 +03:00
|
|
|
mCache && !mWantReturnValue, &script)) {
|
2016-12-03 04:36:42 +03:00
|
|
|
autoPromise.ResolvePromise(retval);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2017-05-04 08:06:33 +03:00
|
|
|
return NS_OK;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2017-04-15 16:44:35 +03:00
|
|
|
nsresult mozJSSubScriptLoader::ReadScriptAsync(nsIURI* uri,
|
2017-07-19 00:47:27 +03:00
|
|
|
HandleObject targetObj,
|
|
|
|
HandleObject loadScope,
|
2018-12-15 01:25:56 +03:00
|
|
|
nsIIOService* serv,
|
|
|
|
bool wantReturnValue, bool cache,
|
|
|
|
MutableHandleValue retval) {
|
2017-04-15 00:10:40 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = xpc::NativeGlobal(targetObj);
|
2015-05-21 08:14:49 +03:00
|
|
|
ErrorResult result;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(globalObject))) {
|
2016-12-03 04:36:42 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
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()) {
|
2017-03-09 20:24:28 +03:00
|
|
|
return result.StealNSResult();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
DebugOnly<bool> asJS = ToJSValue(jsapi.cx(), promise, retval);
|
|
|
|
MOZ_ASSERT(asJS, "Should not fail to convert the promise to a JS value");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
// We create a channel and call SetContentType, to avoid expensive MIME type
|
2016-12-03 04:36:42 +03:00
|
|
|
// lookups (bug 632490).
|
2015-05-21 08:14:49 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), uri,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
2016-12-03 04:36:42 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2015-05-21 08:14:49 +03:00
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
2019-03-08 12:04:11 +03:00
|
|
|
nullptr, // nsICookieSettings
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // aPerformanceStorage
|
2015-05-21 08:14:49 +03:00
|
|
|
nullptr, // aLoadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
nsIRequest::LOAD_NORMAL, serv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
if (!NS_SUCCEEDED(rv)) {
|
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("application/javascript"));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncScriptLoader> loadObserver = new AsyncScriptLoader(
|
2018-12-15 01:25:56 +03:00
|
|
|
channel, wantReturnValue, targetObj, loadScope, cache, promise);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-30 17:54:11 +03:00
|
|
|
nsCOMPtr<nsIIncrementalStreamLoader> loader;
|
|
|
|
rv = NS_NewIncrementalStreamLoader(getter_AddRefs(loader), loadObserver);
|
2016-08-30 07:22:04 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
nsCOMPtr<nsIStreamListener> listener = loader.get();
|
2019-02-12 19:08:25 +03:00
|
|
|
return channel->AsyncOpen(listener);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 20:43:52 +03:00
|
|
|
bool mozJSSubScriptLoader::ReadScript(nsIURI* uri, JSContext* cx,
|
|
|
|
HandleObject targetObj,
|
|
|
|
const char* uriStr, nsIIOService* serv,
|
|
|
|
bool wantReturnValue,
|
|
|
|
bool useCompilationScope,
|
|
|
|
MutableHandleScript script) {
|
|
|
|
script.set(nullptr);
|
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
// We create a channel and call SetContentType, to avoid expensive MIME type
|
|
|
|
// lookups (bug 632490).
|
|
|
|
nsCOMPtr<nsIChannel> chan;
|
2014-01-22 21:50:32 +04:00
|
|
|
nsCOMPtr<nsIInputStream> instream;
|
2015-05-21 08:14:49 +03: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,
|
2015-05-21 08:14:49 +03:00
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
2019-03-08 12:04:11 +03:00
|
|
|
nullptr, // nsICookieSettings
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // PerformanceStorage
|
2015-05-21 08:14:49 +03:00
|
|
|
nullptr, // aLoadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
nsIRequest::LOAD_NORMAL, serv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
chan->SetContentType(NS_LITERAL_CSTRING("application/javascript"));
|
2019-02-12 19:08:25 +03:00
|
|
|
rv = chan->Open(getter_AddRefs(instream));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 08:14:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ReportError(cx, LOAD_ERROR_NOSTREAM, uri);
|
2019-04-09 20:43:52 +03:00
|
|
|
return false;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-15 02:35:34 +03:00
|
|
|
int64_t len = -1;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-12 07:26:40 +03:00
|
|
|
rv = chan->GetContentLength(&len);
|
2014-09-21 20:45:16 +04:00
|
|
|
if (NS_FAILED(rv) || len == -1) {
|
|
|
|
ReportError(cx, LOAD_ERROR_NOCONTENT, uri);
|
2019-04-09 20:43:52 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
if (len > INT32_MAX) {
|
|
|
|
ReportError(cx, LOAD_ERROR_CONTENTTOOBIG, uri);
|
2019-04-09 20:43:52 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
nsCString buf;
|
2015-09-20 06:26:09 +03:00
|
|
|
rv = NS_ReadInputStreamToString(instream, buf, len);
|
2019-04-09 20:43:52 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
Maybe<JSAutoRealm> ar;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
// Note that when using the ScriptPreloader cache with loadSubScript, there
|
2011-07-10 07:21:16 +04:00
|
|
|
// will be a side-effect of keeping the global that the script was compiled
|
2018-07-04 04:26:04 +03:00
|
|
|
// for alive. See note above in EvalScript().
|
|
|
|
//
|
|
|
|
// This will compile the script in XPConnect compilation scope. When the
|
|
|
|
// script is evaluated, it will be cloned into the target scope to be
|
|
|
|
// executed, avoiding leaks on the first session when we don't have a
|
|
|
|
// startup cache.
|
|
|
|
if (useCompilationScope) {
|
|
|
|
ar.emplace(cx, xpc::CompilationScope());
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-12-15 01:25:56 +03:00
|
|
|
return PrepareScript(uri, cx, JS_IsGlobalObject(targetObj), uriStr, buf.get(),
|
2019-04-09 20:43:52 +03:00
|
|
|
len, wantReturnValue, script);
|
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, HandleValue target,
|
2018-12-20 00:46:20 +03:00
|
|
|
JSContext* cx, MutableHandleValue retval) {
|
2001-03-14 05:41:55 +03:00
|
|
|
/*
|
2018-12-20 00:46:20 +03:00
|
|
|
* Loads a local url, referring to UTF-8-encoded data, and evals it into the
|
|
|
|
* current cx. Synchronous (an async version would be cool too.)
|
2001-03-14 05:41:55 +03:00
|
|
|
* url: The url to load. Must be local so that it can be loaded
|
|
|
|
* synchronously.
|
2017-04-15 00:10:40 +03:00
|
|
|
* targetObj: Optional object to eval the script onto (defaults to context
|
|
|
|
* global)
|
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);
|
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) {
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!optionsVal.isObject()) {
|
2013-11-05 19:35:41 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2018-12-15 01:25:56 +03:00
|
|
|
|
2013-11-05 19:35:41 +04:00
|
|
|
LoadSubScriptOptions options(cx, &optionsVal.toObject());
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!options.Parse()) {
|
2013-11-05 19:35:41 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2018-12-09 02:14:27 +03:00
|
|
|
|
2013-11-05 19:35:41 +04:00
|
|
|
return DoLoadSubScriptWithOptions(url, options, cx, retval);
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
nsresult mozJSSubScriptLoader::DoLoadSubScriptWithOptions(
|
|
|
|
const nsAString& url, LoadSubScriptOptions& options, JSContext* cx,
|
2014-01-09 21:39:36 +04:00
|
|
|
MutableHandleValue retval) {
|
2011-12-18 14:09:16 +04:00
|
|
|
nsresult rv = NS_OK;
|
2013-04-26 21:50:18 +04:00
|
|
|
RootedObject targetObj(cx);
|
2017-07-19 00:47:27 +03:00
|
|
|
RootedObject loadScope(cx);
|
|
|
|
mozJSComponentLoader* loader = mozJSComponentLoader::Get();
|
|
|
|
loader->FindTargetObject(cx, &loadScope);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-12 21:14:49 +03:00
|
|
|
if (options.target) {
|
2013-11-05 19:35:41 +04:00
|
|
|
targetObj = options.target;
|
2018-09-12 21:14:49 +03:00
|
|
|
} else {
|
|
|
|
targetObj = loadScope;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-06 19:16:24 +03:00
|
|
|
targetObj = JS_FindCompilationScope(cx, targetObj);
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!targetObj || !loadScope) {
|
2008-03-21 08:07:25 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-08-02 09:49:10 +03:00
|
|
|
MOZ_ASSERT(!js::IsWrapper(targetObj), "JS_FindCompilationScope must unwrap");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-12-08 19:35:06 +03:00
|
|
|
if (js::GetNonCCWObjectRealm(loadScope) !=
|
|
|
|
js::GetNonCCWObjectRealm(targetObj)) {
|
2017-10-13 08:12:57 +03:00
|
|
|
loadScope = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2008-03-18 06:46:53 +03:00
|
|
|
/* load up the url. From here on, failures are reflected as ``custom''
|
|
|
|
* js exceptions */
|
2017-06-15 01:58:25 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2017-03-18 02:53:04 +03:00
|
|
|
nsAutoCString uriStr;
|
|
|
|
nsAutoCString scheme;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-07 18:00:24 +03:00
|
|
|
// Figure out who's calling us
|
2018-07-04 04:26:04 +03:00
|
|
|
JS::AutoFilename filename;
|
2018-07-04 04:24:52 +03:00
|
|
|
if (!JS::DescribeScriptedCaller(cx, &filename)) {
|
|
|
|
// No scripted frame means we don't know who's calling, bail.
|
2011-07-21 03:11:32 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-07-04 04:24:52 +03:00
|
|
|
JSAutoRealm ar(cx, targetObj);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-07-04 04:26:04 +03:00
|
|
|
nsCOMPtr<nsIIOService> serv = do_GetService(NS_IOSERVICE_CONTRACTID);
|
|
|
|
if (!serv) {
|
2018-07-04 04:24:52 +03:00
|
|
|
ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOSERVICE));
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-07-04 04:24:52 +03:00
|
|
|
|
2017-06-07 18:00:24 +03:00
|
|
|
NS_LossyConvertUTF16toASCII asciiUrl(url);
|
2019-02-11 20:53:22 +03:00
|
|
|
AUTO_PROFILER_TEXT_MARKER_CAUSE("SubScript", asciiUrl, JS,
|
|
|
|
profiler_get_backtrace());
|
2017-06-07 18:00:24 +03:00
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(
|
|
|
|
"mozJSSubScriptLoader::DoLoadSubScriptWithOptions", OTHER, asciiUrl);
|
|
|
|
|
2017-09-13 01:05:08 +03:00
|
|
|
// Make sure to explicitly create the URI, since we'll need the
|
|
|
|
// canonicalized spec.
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), asciiUrl.get(), nullptr, serv);
|
2008-03-18 06:46:53 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-09-13 01:05:08 +03:00
|
|
|
ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOURI));
|
2016-12-03 04:36:42 +03:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-07-10 07:21:16 +04:00
|
|
|
|
2017-05-04 08:06:33 +03:00
|
|
|
rv = uri->GetSpec(uriStr);
|
|
|
|
if (NS_FAILED(rv)) {
|
2016-03-22 10:48:38 +03:00
|
|
|
ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOSPEC));
|
2016-12-03 04:36:42 +03:00
|
|
|
return NS_OK;
|
2016-03-22 10:48:38 +03:00
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
rv = uri->GetScheme(scheme);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ReportError(cx, LOAD_ERROR_NOSCHEME, uri);
|
2017-05-16 01:04:45 +03:00
|
|
|
return NS_OK;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 01:04:45 +03:00
|
|
|
// Suppress caching if we're compiling as content or if we're loading a
|
|
|
|
// blob: URI.
|
2018-07-04 04:26:04 +03:00
|
|
|
bool useCompilationScope = false;
|
|
|
|
auto* principal = BasePrincipal::Cast(GetObjectPrincipal(targetObj));
|
2018-07-04 04:24:52 +03:00
|
|
|
bool isSystem = principal->Is<SystemPrincipal>();
|
2018-07-04 04:26:04 +03:00
|
|
|
if (!isSystem && principal->Is<ContentPrincipal>()) {
|
2017-05-16 01:04:45 +03:00
|
|
|
auto* content = principal->As<ContentPrincipal>();
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString scheme;
|
2018-07-04 04:24:52 +03:00
|
|
|
content->mCodebase->GetScheme(scheme);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
// We want to enable caching for scripts with Activity Stream's
|
|
|
|
// codebase URLs.
|
2018-07-04 04:24:52 +03:00
|
|
|
if (scheme.EqualsLiteral("about")) {
|
|
|
|
nsAutoCString filePath;
|
|
|
|
content->mCodebase->GetFilePath(filePath);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
useCompilationScope = filePath.EqualsLiteral("home") ||
|
2018-07-04 04:26:04 +03:00
|
|
|
filePath.EqualsLiteral("newtab") ||
|
|
|
|
filePath.EqualsLiteral("welcome");
|
|
|
|
isSystem = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-04 04:24:52 +03:00
|
|
|
bool ignoreCache =
|
2017-07-19 00:47:27 +03:00
|
|
|
options.ignoreCache || !isSystem || scheme.EqualsLiteral("blob");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
StartupCache* cache = ignoreCache ? nullptr : StartupCache::GetSingleton();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString cachePath;
|
2017-09-13 01:05:08 +03:00
|
|
|
SubscriptCachePath(cx, uri, targetObj, cachePath);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-02-25 19:43:14 +04:00
|
|
|
RootedScript script(cx);
|
2017-05-01 07:53:49 +03:00
|
|
|
if (!options.ignoreCache) {
|
2017-07-19 00:47:27 +03:00
|
|
|
if (!options.wantReturnValue) {
|
2017-05-04 08:06:33 +03:00
|
|
|
script = ScriptPreloader::GetSingleton().GetCachedScript(cx, cachePath);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!script && cache) {
|
2017-07-19 00:47:27 +03:00
|
|
|
rv = ReadCachedScript(cache, cachePath, cx, &script);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-07-10 07:21:16 +04:00
|
|
|
if (NS_FAILED(rv) || !script) {
|
2016-03-22 10:48:38 +03:00
|
|
|
// ReadCachedScript may have set a pending exception.
|
|
|
|
JS_ClearPendingException(cx);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
// If we are doing an async load, trigger it and bail out.
|
2015-05-21 08:14:49 +03:00
|
|
|
if (!script && options.async) {
|
2018-12-15 01:25:56 +03:00
|
|
|
return ReadScriptAsync(uri, targetObj, loadScope, serv,
|
2017-05-04 08:06:33 +03:00
|
|
|
options.wantReturnValue, !!cache, retval);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 01:04:45 +03:00
|
|
|
if (script) {
|
2017-05-04 08:06:33 +03:00
|
|
|
// |script| came from the cache, so don't bother writing it
|
2017-05-16 01:04:45 +03:00
|
|
|
// |back there.
|
2017-05-17 19:21:54 +03:00
|
|
|
cache = nullptr;
|
2019-04-09 20:43:52 +03:00
|
|
|
} else if (!ReadScript(
|
|
|
|
uri, cx, targetObj, static_cast<const char*>(uriStr.get()),
|
|
|
|
serv, options.wantReturnValue, useCompilationScope, &script)) {
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
Unused << EvalScript(cx, targetObj, loadScope, retval, uri, !!cache,
|
2017-05-16 01:04:45 +03:00
|
|
|
!ignoreCache && !options.wantReturnValue, &script);
|
2016-12-03 04:36:42 +03:00
|
|
|
return NS_OK;
|
2001-03-14 05:41:55 +03:00
|
|
|
}
|