2017-05-08 09:24:44 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "ModuleLoadRequest.h"
|
|
|
|
#include "ModuleScript.h"
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-05-02 20:01:51 +03:00
|
|
|
#undef LOG
|
|
|
|
#define LOG(args) \
|
|
|
|
MOZ_LOG(ScriptLoader::gScriptLoaderLog, mozilla::LogLevel::Debug, args)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ModuleLoadRequest)
|
2017-05-08 09:24:44 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(ScriptLoadRequest)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ModuleLoadRequest, ScriptLoadRequest,
|
|
|
|
mBaseURL,
|
|
|
|
mLoader,
|
|
|
|
mModuleScript,
|
|
|
|
mImports)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ModuleLoadRequest, ScriptLoadRequest)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ModuleLoadRequest, ScriptLoadRequest)
|
|
|
|
|
2017-12-15 00:13:57 +03:00
|
|
|
ModuleLoadRequest::ModuleLoadRequest(nsIURI* aURI,
|
|
|
|
nsIScriptElement* aElement,
|
2017-05-08 09:24:44 +03:00
|
|
|
CORSMode aCORSMode,
|
2017-05-08 09:25:03 +03:00
|
|
|
const SRIMetadata& aIntegrity,
|
2017-12-19 18:30:49 +03:00
|
|
|
nsIURI* aReferrer,
|
|
|
|
mozilla::net::ReferrerPolicy aReferrerPolicy,
|
2017-05-08 09:24:44 +03:00
|
|
|
ScriptLoader* aLoader)
|
2018-01-08 18:17:33 +03:00
|
|
|
: ScriptLoadRequest(ScriptKind::eModule,
|
2017-12-15 00:13:57 +03:00
|
|
|
aURI,
|
2017-05-08 09:24:44 +03:00
|
|
|
aElement,
|
|
|
|
aCORSMode,
|
2017-12-19 18:30:49 +03:00
|
|
|
aIntegrity,
|
|
|
|
aReferrer,
|
|
|
|
aReferrerPolicy),
|
2017-05-08 09:24:44 +03:00
|
|
|
mIsTopLevel(true),
|
2017-12-15 00:13:57 +03:00
|
|
|
mLoader(aLoader),
|
|
|
|
mVisitedSet(new VisitedURLSet())
|
|
|
|
{
|
|
|
|
mVisitedSet->PutEntry(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleLoadRequest::ModuleLoadRequest(nsIURI* aURI,
|
|
|
|
ModuleLoadRequest* aParent)
|
2018-01-08 18:17:33 +03:00
|
|
|
: ScriptLoadRequest(ScriptKind::eModule,
|
2017-12-15 00:13:57 +03:00
|
|
|
aURI,
|
|
|
|
aParent->mElement,
|
|
|
|
aParent->mCORSMode,
|
2017-12-21 13:58:53 +03:00
|
|
|
SRIMetadata(),
|
2017-12-19 18:30:49 +03:00
|
|
|
aParent->mURI,
|
|
|
|
aParent->mReferrerPolicy),
|
2017-12-15 00:13:57 +03:00
|
|
|
mIsTopLevel(false),
|
|
|
|
mLoader(aParent->mLoader),
|
|
|
|
mVisitedSet(aParent->mVisitedSet)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mVisitedSet->Contains(aURI));
|
|
|
|
|
|
|
|
mTriggeringPrincipal = aParent->mTriggeringPrincipal;
|
|
|
|
mIsInline = false;
|
2018-01-08 18:17:33 +03:00
|
|
|
mScriptMode = aParent->mScriptMode;
|
2017-12-15 00:13:57 +03:00
|
|
|
}
|
2017-05-08 09:24:44 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
ModuleLoadRequest::Cancel()
|
|
|
|
{
|
|
|
|
ScriptLoadRequest::Cancel();
|
|
|
|
mModuleScript = nullptr;
|
2018-01-08 18:17:33 +03:00
|
|
|
mProgress = ScriptLoadRequest::Progress::eReady;
|
2017-08-22 12:34:14 +03:00
|
|
|
CancelImports();
|
|
|
|
mReady.RejectIfExists(NS_ERROR_DOM_ABORT_ERR, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModuleLoadRequest::CancelImports()
|
|
|
|
{
|
2017-05-08 09:24:44 +03:00
|
|
|
for (size_t i = 0; i < mImports.Length(); i++) {
|
|
|
|
mImports[i]->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModuleLoadRequest::SetReady()
|
|
|
|
{
|
2017-05-02 20:01:51 +03:00
|
|
|
// Mark a module as ready to execute. This means that this module and all it
|
|
|
|
// dependencies have had their source loaded, parsed as a module and the
|
|
|
|
// modules instantiated.
|
|
|
|
//
|
|
|
|
// The mReady promise is used to ensure that when all dependencies of a module
|
|
|
|
// have become ready, DependenciesLoaded is called on that module
|
|
|
|
// request. This is set up in StartFetchingModuleDependencies.
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
for (size_t i = 0; i < mImports.Length(); i++) {
|
|
|
|
MOZ_ASSERT(mImports[i]->IsReadyToRun());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ScriptLoadRequest::SetReady();
|
|
|
|
mReady.ResolveIfExists(true, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModuleLoadRequest::ModuleLoaded()
|
|
|
|
{
|
|
|
|
// A module that was found to be marked as fetching in the module map has now
|
|
|
|
// been loaded.
|
|
|
|
|
2017-05-02 20:01:51 +03:00
|
|
|
LOG(("ScriptLoadRequest (%p): Module loaded", this));
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
mModuleScript = mLoader->GetFetchedModule(mURI);
|
2017-12-06 17:54:58 +03:00
|
|
|
if (!mModuleScript || mModuleScript->HasParseError()) {
|
2017-08-22 12:34:14 +03:00
|
|
|
ModuleErrored();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
mLoader->StartFetchingModuleDependencies(this);
|
|
|
|
}
|
|
|
|
|
2017-08-22 12:34:14 +03:00
|
|
|
void
|
|
|
|
ModuleLoadRequest::ModuleErrored()
|
|
|
|
{
|
|
|
|
LOG(("ScriptLoadRequest (%p): Module errored", this));
|
|
|
|
|
|
|
|
mLoader->CheckModuleDependenciesLoaded(this);
|
2017-12-06 17:54:58 +03:00
|
|
|
MOZ_ASSERT(!mModuleScript || mModuleScript->HasParseError());
|
2017-08-22 12:34:14 +03:00
|
|
|
|
|
|
|
CancelImports();
|
|
|
|
SetReady();
|
|
|
|
LoadFinished();
|
|
|
|
}
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
void
|
|
|
|
ModuleLoadRequest::DependenciesLoaded()
|
|
|
|
{
|
|
|
|
// The module and all of its dependencies have been successfully fetched and
|
|
|
|
// compiled.
|
|
|
|
|
2017-05-02 20:01:51 +03:00
|
|
|
LOG(("ScriptLoadRequest (%p): Module dependencies loaded", this));
|
|
|
|
|
2017-08-22 12:34:14 +03:00
|
|
|
MOZ_ASSERT(mModuleScript);
|
|
|
|
|
|
|
|
mLoader->CheckModuleDependenciesLoaded(this);
|
2017-05-08 09:24:44 +03:00
|
|
|
SetReady();
|
2017-08-22 12:34:14 +03:00
|
|
|
LoadFinished();
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModuleLoadRequest::LoadFailed()
|
|
|
|
{
|
2017-08-22 12:34:14 +03:00
|
|
|
// We failed to load the source text or an error occurred unrelated to the
|
|
|
|
// content of the module (e.g. OOM).
|
|
|
|
|
2017-05-02 20:01:51 +03:00
|
|
|
LOG(("ScriptLoadRequest (%p): Module load failed", this));
|
|
|
|
|
2017-08-22 12:34:14 +03:00
|
|
|
MOZ_ASSERT(!mModuleScript);
|
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
Cancel();
|
2017-08-22 12:34:14 +03:00
|
|
|
LoadFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ModuleLoadRequest::LoadFinished()
|
|
|
|
{
|
2017-05-08 09:24:44 +03:00
|
|
|
mLoader->ProcessLoadedModuleTree(this);
|
2017-08-22 12:34:14 +03:00
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
mLoader = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|