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"
|
2001-03-14 05:41:55 +03:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-03-13 17:45:02 +03:00
|
|
|
#include "jsfriendapi.h"
|
2019-04-10 20:48:50 +03:00
|
|
|
#include "xpcprivate.h" // xpc::OptionsBase
|
2020-03-31 04:30:48 +03:00
|
|
|
#include "js/CompilationAndEvaluation.h" // JS::Compile{,ForNonSyntacticScope}
|
2019-04-10 20:48:50 +03:00
|
|
|
#include "js/SourceText.h" // JS::Source{Ownership,Text}
|
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"
|
2017-05-08 09:24:22 +03:00
|
|
|
#include "mozilla/dom/ScriptLoader.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"
|
2019-04-10 03:03:32 +03:00
|
|
|
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
|
2014-09-21 20:45:16 +04:00
|
|
|
#include "nsContentUtils.h"
|
2017-12-07 03:52:51 +03:00
|
|
|
#include "nsString.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),
|
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("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;
|
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
|
|
|
|
2020-03-16 16:47:02 +03:00
|
|
|
mozJSSubScriptLoader::mozJSSubScriptLoader() = default;
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2020-03-16 16:47:02 +03:00
|
|
|
mozJSSubScriptLoader::~mozJSSubScriptLoader() = default;
|
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:58:52 +03:00
|
|
|
static JSScript* PrepareScript(nsIURI* uri, JSContext* cx,
|
|
|
|
bool wantGlobalScript, const char* uriStr,
|
|
|
|
const char* buf, int64_t len,
|
|
|
|
bool wantReturnValue) {
|
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);
|
|
|
|
|
2019-04-10 03:03:32 +03:00
|
|
|
JS::SourceText<Utf8Unit> srcBuf;
|
|
|
|
if (!srcBuf.init(cx, buf, len, JS::SourceOwnership::Borrowed)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-04 04:26:04 +03:00
|
|
|
if (wantGlobalScript) {
|
2020-03-31 04:30:05 +03:00
|
|
|
return JS::Compile(cx, options, srcBuf);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
2020-03-31 04:30:48 +03:00
|
|
|
return JS::CompileForNonSyntacticScope(cx, options, srcBuf);
|
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
|
|
|
|
2019-10-02 06:20:33 +03:00
|
|
|
bool mozJSSubScriptLoader::ReadScript(JS::MutableHandle<JSScript*> script,
|
2019-04-09 20:58:52 +03:00
|
|
|
nsIURI* uri, JSContext* cx,
|
|
|
|
HandleObject targetObj,
|
|
|
|
const char* uriStr, nsIIOService* serv,
|
|
|
|
bool wantReturnValue,
|
|
|
|
bool useCompilationScope) {
|
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,
|
2020-03-04 11:59:08 +03:00
|
|
|
nullptr, // nsICookieJarSettings
|
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)) {
|
2020-07-01 11:29:29 +03:00
|
|
|
chan->SetContentType("application/javascript"_ns);
|
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-10-02 06:20:33 +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);
|
2020-07-02 16:38:36 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-09-21 20:45:16 +04:00
|
|
|
ReportError(cx, LOAD_ERROR_NOCONTENT, uri);
|
2019-10-02 06:20:33 +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-10-02 06:20:33 +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-10-02 06:20:33 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-07-02 16:38:36 +03:00
|
|
|
if (len < 0) {
|
|
|
|
len = buf.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
int64_t currentLength = -1;
|
|
|
|
// if getting content length succeeded above, it should not fail now
|
|
|
|
MOZ_ASSERT(chan->GetContentLength(¤tLength) == NS_OK);
|
|
|
|
// if content length was not known when GetContentLength() was called before,
|
|
|
|
// 'len' would be set to -1 until NS_ReadInputStreamToString() set its correct
|
|
|
|
// value. Every subsequent call to GetContentLength() should return the same
|
|
|
|
// length as that value.
|
|
|
|
MOZ_ASSERT(currentLength == len);
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2019-10-02 06:20:33 +03:00
|
|
|
JSScript* ret = PrepareScript(uri, cx, JS_IsGlobalObject(targetObj), uriStr,
|
|
|
|
buf.get(), len, wantReturnValue);
|
|
|
|
if (!ret) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
script.set(ret);
|
|
|
|
return true;
|
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
|
2019-06-15 00:19:41 +03:00
|
|
|
* current cx. Synchronous. ChromeUtils.compileScript() should be used for
|
|
|
|
* async loads.
|
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) {
|
2020-07-01 11:29:29 +03:00
|
|
|
ReportError(cx, nsLiteralCString(LOAD_ERROR_NOSERVICE));
|
2018-07-04 04:24:52 +03:00
|
|
|
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);
|
2020-01-20 21:53:09 +03:00
|
|
|
AUTO_PROFILER_TEXT_MARKER_CAUSE("SubScript", asciiUrl, JS, Nothing(),
|
2019-02-11 20:53:22 +03:00
|
|
|
profiler_get_backtrace());
|
2019-11-11 23:27:44 +03:00
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING_NONSENSITIVE(
|
2017-06-07 18:00:24 +03:00
|
|
|
"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.
|
2019-07-15 16:39:51 +03:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), asciiUrl);
|
2008-03-18 06:46:53 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2020-07-01 11:29:29 +03:00
|
|
|
ReportError(cx, nsLiteralCString(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)) {
|
2020-07-01 11:29:29 +03:00
|
|
|
ReportError(cx, nsLiteralCString(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;
|
2019-07-08 19:37:45 +03:00
|
|
|
content->mURI->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;
|
2019-07-08 19:37:45 +03:00
|
|
|
content->mURI->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
|
|
|
|
2020-07-02 14:05:53 +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-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:58:52 +03:00
|
|
|
} else {
|
2019-10-02 06:20:33 +03:00
|
|
|
if (!ReadScript(&script, uri, cx, targetObj,
|
|
|
|
static_cast<const char*>(uriStr.get()), serv,
|
|
|
|
options.wantReturnValue, useCompilationScope)) {
|
2019-04-09 20:58:52 +03:00
|
|
|
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
|
|
|
}
|