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"
|
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)
|
2017-09-22 07:35:46 +03:00
|
|
|
, charset(VoidString())
|
2013-11-05 19:35:41 +04:00
|
|
|
, ignoreCache(false)
|
2015-05-21 08:14:49 +03:00
|
|
|
, async(false)
|
2017-04-15 19:32:34 +03:00
|
|
|
, wantReturnValue(false)
|
2013-11-05 19:35:41 +04:00
|
|
|
{ }
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual bool Parse() override {
|
2013-11-05 19:35:41 +04:00
|
|
|
return ParseObject("target", &target) &&
|
|
|
|
ParseString("charset", charset) &&
|
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
|
|
|
}
|
|
|
|
|
|
|
|
RootedObject target;
|
|
|
|
nsString charset;
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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"
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-03 04:36:42 +03:00
|
|
|
static void
|
2017-03-18 02:53:04 +03:00
|
|
|
ReportError(JSContext* cx, const nsACString& msg)
|
2011-07-10 07:21:16 +04:00
|
|
|
{
|
2017-03-18 02:53:04 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2017-03-18 02:53:04 +03:00
|
|
|
ReportError(cx, nsDependentCString(origMsg));
|
2016-12-03 04:36:42 +03:00
|
|
|
return;
|
|
|
|
}
|
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-09-12 21:14:49 +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);
|
2014-11-09 04:08:09 +03:00
|
|
|
}
|
|
|
|
|
2017-04-15 00:33:17 +03:00
|
|
|
static bool
|
2015-05-21 08:14:49 +03:00
|
|
|
PrepareScript(nsIURI* uri,
|
|
|
|
JSContext* cx,
|
2018-07-04 04:26:04 +03:00
|
|
|
bool wantGlobalScript,
|
2015-05-21 08:14:49 +03:00
|
|
|
const char* uriStr,
|
|
|
|
const nsAString& charset,
|
|
|
|
const char* buf,
|
|
|
|
int64_t len,
|
2017-04-15 19:32:34 +03:00
|
|
|
bool wantReturnValue,
|
2017-05-17 19:21:54 +03:00
|
|
|
MutableHandleScript script)
|
2015-05-21 08:14:49 +03:00
|
|
|
{
|
|
|
|
JS::CompileOptions options(cx);
|
2017-05-16 01:04:45 +03:00
|
|
|
options.setFileAndLine(uriStr, 1)
|
2017-04-15 19:32:34 +03:00
|
|
|
.setNoScriptRval(!wantReturnValue);
|
2015-05-21 08:14:49 +03:00
|
|
|
if (!charset.IsVoid()) {
|
|
|
|
char16_t* scriptBuf = nullptr;
|
|
|
|
size_t scriptLength = 0;
|
|
|
|
|
|
|
|
nsresult rv =
|
2017-05-08 09:24:22 +03:00
|
|
|
ScriptLoader::ConvertToUTF16(nullptr, reinterpret_cast<const uint8_t*>(buf), len,
|
|
|
|
charset, nullptr, scriptBuf, scriptLength);
|
2015-05-21 08:14:49 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-09 05:42:48 +03:00
|
|
|
JS::SourceText<char16_t> srcBuf;
|
2018-10-23 22:27:16 +03:00
|
|
|
if (!srcBuf.init(cx, JS::UniqueTwoByteChars(scriptBuf), scriptLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-04 04:26:04 +03:00
|
|
|
if (wantGlobalScript) {
|
2017-05-16 01:04:45 +03:00
|
|
|
return JS::Compile(cx, options, srcBuf, script);
|
2017-02-14 17:50:56 +03:00
|
|
|
}
|
2017-05-16 01:04:45 +03:00
|
|
|
return JS::CompileForNonSyntacticScope(cx, options, srcBuf, script);
|
2017-02-14 17:50:56 +03:00
|
|
|
}
|
|
|
|
// We only use lazy source when no special encoding is specified because
|
|
|
|
// the lazy source loader doesn't know the encoding.
|
2017-05-16 01:04:45 +03:00
|
|
|
options.setSourceIsLazy(true);
|
2018-07-04 04:26:04 +03:00
|
|
|
if (wantGlobalScript) {
|
2018-09-13 06:48:44 +03:00
|
|
|
return JS::CompileLatin1(cx, options, buf, len, script);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
2018-09-13 06:48:44 +03:00
|
|
|
return JS::CompileLatin1ForNonSyntacticScope(cx, options, buf, len, script);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2017-04-15 00:33:17 +03:00
|
|
|
static bool
|
2015-05-21 08:14:49 +03:00
|
|
|
EvalScript(JSContext* cx,
|
2017-04-15 00:38:51 +03:00
|
|
|
HandleObject targetObj,
|
2017-07-19 00:47:27 +03:00
|
|
|
HandleObject loadScope,
|
2015-05-21 08:14:49 +03:00
|
|
|
MutableHandleValue retval,
|
|
|
|
nsIURI* uri,
|
2017-05-04 08:06:33 +03:00
|
|
|
bool startupCache,
|
|
|
|
bool preloadCache,
|
2017-05-17 19:21:54 +03:00
|
|
|
MutableHandleScript script)
|
2015-05-21 08:14:49 +03:00
|
|
|
{
|
2018-07-06 19:16:24 +03:00
|
|
|
MOZ_ASSERT(!js::IsWrapper(targetObj));
|
|
|
|
|
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 {
|
2017-05-17 19:21:54 +03:00
|
|
|
JS::AutoObjectVector envChain(cx);
|
|
|
|
if (!envChain.append(targetObj)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-10 21:23:32 +03:00
|
|
|
if (!loadScope) {
|
|
|
|
// A null loadScope means we are cross-compartment. In this case, we
|
|
|
|
// should check the target isn't in the JSM loader shared-global or
|
|
|
|
// we will contaiminate all JSMs in the compartment.
|
|
|
|
//
|
|
|
|
// 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)) {
|
2017-09-10 21:23:32 +03:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-10 21:23:32 +03:00
|
|
|
} else if (JS_IsGlobalObject(loadScope)) {
|
|
|
|
if (!JS::CloneAndExecuteScript(cx, envChain, script, retval)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(js::IsJSMEnvironment(loadScope));
|
|
|
|
if (!js::ExecuteInJSMEnvironment(cx, script, loadScope, envChain)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
retval.setUndefined();
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:49:10 +03:00
|
|
|
JSAutoRealm rar(cx, targetObj);
|
2016-12-03 04:36:42 +03:00
|
|
|
if (!JS_WrapValue(cx, retval)) {
|
|
|
|
return false;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2017-05-04 08:06:33 +03:00
|
|
|
if (script && (startupCache || preloadCache)) {
|
2017-09-13 06:11:49 +03:00
|
|
|
nsAutoCString cachePath;
|
2017-09-13 01:05:08 +03:00
|
|
|
SubscriptCachePath(cx, uri, targetObj, cachePath);
|
2015-05-21 08:14:49 +03:00
|
|
|
|
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
|
|
|
|
// original JSScript object alive for an indefinite amount of time.
|
|
|
|
// This has the side-effect of keeping the global that the script
|
|
|
|
// was compiled for alive, too.
|
|
|
|
//
|
|
|
|
// For most startups, the global in question will be the
|
|
|
|
// CompilationScope, since we pre-compile any scripts that were
|
|
|
|
// needed during the last startup in that scope. But for startups
|
|
|
|
// 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
|
|
|
|
// useCompilationScope in ReadScript().
|
2017-06-15 01:38:59 +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,
|
|
|
|
// 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
|
|
|
|
// globals. Given the unusual circumstances required to trigger
|
|
|
|
// this, it's not a major concern. But it should be kept in mind.
|
2017-05-04 08:06:33 +03:00
|
|
|
ScriptPreloader::GetSingleton().NoteScript(uriStr, cachePath, script);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startupCache) {
|
2018-08-02 09:49:10 +03:00
|
|
|
JSAutoRealm ar(cx, script);
|
2017-07-07 21:27:53 +03:00
|
|
|
WriteCachedScript(StartupCache::GetSingleton(), cachePath, cx, script);
|
2017-05-04 08:06:33 +03:00
|
|
|
}
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
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)
|
|
|
|
|
2017-05-16 01:04:45 +03:00
|
|
|
AsyncScriptLoader(nsIChannel* aChannel, bool aWantReturnValue,
|
2017-07-19 00:47:27 +03:00
|
|
|
JSObject* aTargetObj, JSObject* aLoadScope,
|
|
|
|
const nsAString& aCharset, 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)
|
|
|
|
, mCharset(aCharset)
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~AsyncScriptLoader() {
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
nsString mCharset;
|
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,
|
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:
|
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));
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-15 00:10:40 +03:00
|
|
|
RootedObject targetObj(cx, mTargetObj);
|
2017-07-19 00:47:27 +03:00
|
|
|
RootedObject loadScope(cx, mLoadScope);
|
2015-05-21 08:14:49 +03:00
|
|
|
|
2018-07-04 04:26:04 +03:00
|
|
|
if (!PrepareScript(uri, cx, JS_IsGlobalObject(targetObj), spec.get(),
|
|
|
|
mCharset, reinterpret_cast<const char*>(aBuf), aLength,
|
2017-05-17 19:21:54 +03:00
|
|
|
mWantReturnValue, &script))
|
2016-12-03 04:36:42 +03:00
|
|
|
{
|
|
|
|
return NS_OK;
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> retval(cx);
|
2017-07-19 00:47:27 +03:00
|
|
|
if (EvalScript(cx, targetObj, loadScope, &retval, uri, mCache,
|
2017-05-04 08:06:33 +03:00
|
|
|
mCache && !mWantReturnValue,
|
2017-05-17 19:21:54 +03:00
|
|
|
&script)) {
|
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
|
2017-04-15 16:44:35 +03:00
|
|
|
mozJSSubScriptLoader::ReadScriptAsync(nsIURI* uri,
|
2017-04-15 02:35:34 +03:00
|
|
|
HandleObject targetObj,
|
2017-07-19 00:47:27 +03:00
|
|
|
HandleObject loadScope,
|
2015-05-21 08:14:49 +03:00
|
|
|
const nsAString& charset,
|
2017-04-15 16:44:35 +03:00
|
|
|
nsIIOService* serv,
|
2017-04-15 19:32:34 +03:00
|
|
|
bool wantReturnValue,
|
2017-04-15 16:44:35 +03:00
|
|
|
bool cache,
|
|
|
|
MutableHandleValue retval)
|
2015-05-21 08:14:49 +03:00
|
|
|
{
|
2017-04-15 00:10:40 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = xpc::NativeGlobal(targetObj);
|
2015-05-21 08:14:49 +03:00
|
|
|
ErrorResult result;
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(globalObject))) {
|
2017-03-09 20:24:28 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2015-05-21 08:14:49 +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();
|
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");
|
|
|
|
|
|
|
|
// 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,
|
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);
|
|
|
|
|
|
|
|
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,
|
2017-04-15 19:32:34 +03:00
|
|
|
wantReturnValue,
|
2017-04-15 00:10:40 +03:00
|
|
|
targetObj,
|
2017-07-19 00:47:27 +03:00
|
|
|
loadScope,
|
2015-05-21 08:14:49 +03:00
|
|
|
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
|
2017-04-15 16:44:35 +03:00
|
|
|
mozJSSubScriptLoader::ReadScript(nsIURI* uri,
|
|
|
|
JSContext* cx,
|
2017-04-15 02:35:34 +03:00
|
|
|
HandleObject targetObj,
|
2017-04-15 16:44:35 +03:00
|
|
|
const nsAString& charset,
|
|
|
|
const char* uriStr,
|
|
|
|
nsIIOService* serv,
|
2017-04-15 19:32:34 +03:00
|
|
|
bool wantReturnValue,
|
2018-07-04 04:26:04 +03:00
|
|
|
bool useCompilationScope,
|
2017-05-17 19:21:54 +03:00
|
|
|
MutableHandleScript script)
|
2011-07-10 07:21:16 +04:00
|
|
|
{
|
2014-06-12 04:38:22 +04:00
|
|
|
script.set(nullptr);
|
2012-11-27 02:41:55 +04:00
|
|
|
|
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,
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // PerformanceStorage
|
2014-09-21 20:45:16 +04:00
|
|
|
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
|
|
|
|
2018-08-02 09:49:10 +03:00
|
|
|
Maybe<JSAutoRealm> ar;
|
2018-07-04 04:26:04 +03:00
|
|
|
|
|
|
|
// Note that when using the ScriptPreloader cache with loadSubScript, there
|
|
|
|
// will be a side-effect of keeping the global that the script was compiled
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrepareScript(uri, cx, JS_IsGlobalObject(targetObj),
|
|
|
|
uriStr, charset, buf.get(), len, wantReturnValue,
|
2017-05-17 19:21:54 +03:00
|
|
|
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,
|
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.
|
2017-04-15 00:10:40 +03:00
|
|
|
* targetObj: 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
|
|
|
{
|
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
|
|
|
}
|
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
|
|
|
}
|
2013-11-05 19:35:41 +04:00
|
|
|
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;
|
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-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 {
|
2017-07-19 00:47:27 +03:00
|
|
|
targetObj = loadScope;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2005-12-09 21:58:23 +03:00
|
|
|
|
2011-12-18 14:09:16 +04:00
|
|
|
targetObj = JS_FindCompilationScope(cx, targetObj);
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!targetObj || !loadScope) {
|
2011-07-21 03:11:32 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2011-07-21 03:11:32 +04:00
|
|
|
|
2018-07-06 19:16:24 +03:00
|
|
|
MOZ_ASSERT(!js::IsWrapper(targetObj),
|
|
|
|
"JS_FindCompilationScope must unwrap");
|
|
|
|
|
2018-09-12 21:14:49 +03:00
|
|
|
if (js::GetObjectCompartment(loadScope) != js::GetObjectCompartment(targetObj)) {
|
2017-07-19 00:47:27 +03:00
|
|
|
loadScope = nullptr;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2017-07-19 00:47:27 +03: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;
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:49:10 +03:00
|
|
|
JSAutoRealm ar(cx, targetObj);
|
2016-05-27 03:36:09 +03:00
|
|
|
|
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) {
|
2017-03-18 02:53:04 +03:00
|
|
|
ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOSERVICE));
|
2016-12-03 04:36:42 +03:00
|
|
|
return NS_OK;
|
2001-03-14 05:41:55 +03:00
|
|
|
}
|
|
|
|
|
2017-10-13 08:12:57 +03:00
|
|
|
NS_LossyConvertUTF16toASCII asciiUrl(url);
|
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(
|
|
|
|
"mozJSSubScriptLoader::DoLoadSubScriptWithOptions", OTHER, asciiUrl);
|
2017-06-15 01:58:25 +03:00
|
|
|
|
2008-03-18 06:46:53 +03:00
|
|
|
// Make sure to explicitly create the URI, since we'll need the
|
|
|
|
// canonicalized spec.
|
2017-06-15 01:58:25 +03:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), asciiUrl.get(), nullptr, serv);
|
2008-03-18 06:46:53 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-03-18 02:53:04 +03:00
|
|
|
ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOURI));
|
2016-12-03 04:36:42 +03:00
|
|
|
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)) {
|
2017-03-18 02:53:04 +03:00
|
|
|
ReportError(cx, NS_LITERAL_CSTRING(LOAD_ERROR_NOSPEC));
|
2016-12-03 04:36:42 +03:00
|
|
|
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
|
|
|
|
2017-06-07 18:00:24 +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;
|
2018-07-04 04:24:52 +03:00
|
|
|
auto* principal = BasePrincipal::Cast(GetObjectPrincipal(targetObj));
|
|
|
|
bool isSystem = principal->Is<SystemPrincipal>();
|
|
|
|
if (!isSystem && principal->Is<ContentPrincipal>()) {
|
|
|
|
auto* content = principal->As<ContentPrincipal>();
|
|
|
|
|
|
|
|
nsAutoCString scheme;
|
|
|
|
content->mCodebase->GetScheme(scheme);
|
|
|
|
|
|
|
|
// We want to enable caching for scripts with Activity Stream's
|
|
|
|
// codebase URLs.
|
|
|
|
if (scheme.EqualsLiteral("about")) {
|
|
|
|
nsAutoCString filePath;
|
|
|
|
content->mCodebase->GetFilePath(filePath);
|
|
|
|
|
2018-07-04 04:26:04 +03:00
|
|
|
useCompilationScope = filePath.EqualsLiteral("home") ||
|
|
|
|
filePath.EqualsLiteral("newtab") ||
|
|
|
|
filePath.EqualsLiteral("welcome");
|
|
|
|
isSystem = true;
|
2018-07-04 04:24:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool ignoreCache = options.ignoreCache || !isSystem || scheme.EqualsLiteral("blob");
|
|
|
|
|
2017-06-07 18:00:24 +03:00
|
|
|
StartupCache* cache = ignoreCache ? nullptr : StartupCache::GetSingleton();
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString cachePath;
|
2017-09-13 01:05:08 +03:00
|
|
|
SubscriptCachePath(cx, uri, targetObj, cachePath);
|
2011-07-10 07:21:16 +04:00
|
|
|
|
2014-02-25 19:43:14 +04:00
|
|
|
RootedScript script(cx);
|
2017-05-01 07:53:49 +03:00
|
|
|
if (!options.ignoreCache) {
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!options.wantReturnValue) {
|
2017-05-04 08:06:33 +03:00
|
|
|
script = ScriptPreloader::GetSingleton().GetCachedScript(cx, cachePath);
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
|
|
|
if (!script && cache) {
|
2017-07-07 21:14:04 +03:00
|
|
|
rv = ReadCachedScript(cache, cachePath, cx, &script);
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2017-05-04 08:06:33 +03:00
|
|
|
if (NS_FAILED(rv) || !script) {
|
2016-03-22 10:48:38 +03:00
|
|
|
// 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) {
|
2017-07-19 00:47:27 +03:00
|
|
|
return ReadScriptAsync(uri, targetObj, loadScope, options.charset, serv,
|
2017-05-16 01:04:45 +03:00
|
|
|
options.wantReturnValue, !!cache, retval);
|
2015-05-21 08:14:49 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 01:04:45 +03:00
|
|
|
if (script) {
|
|
|
|
// |script| came from the cache, so don't bother writing it
|
|
|
|
// |back there.
|
2015-05-21 08:14:49 +03:00
|
|
|
cache = nullptr;
|
2017-05-16 01:04:45 +03:00
|
|
|
} else if (!ReadScript(uri, cx, targetObj, options.charset,
|
2017-05-17 19:21:54 +03:00
|
|
|
static_cast<const char*>(uriStr.get()), serv,
|
2018-07-04 04:26:04 +03:00
|
|
|
options.wantReturnValue, useCompilationScope, &script)) {
|
2017-05-16 01:04:45 +03:00
|
|
|
return NS_OK;
|
2003-03-23 10:22:18 +03:00
|
|
|
}
|
2001-03-14 05:41:55 +03:00
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
Unused << EvalScript(cx, targetObj, loadScope, retval, uri, !!cache,
|
2017-05-04 08:06:33 +03:00
|
|
|
!ignoreCache && !options.wantReturnValue,
|
2017-05-17 19:21:54 +03:00
|
|
|
&script);
|
2016-12-03 04:36:42 +03:00
|
|
|
return NS_OK;
|
2001-03-14 05:41:55 +03:00
|
|
|
}
|