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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_ScriptLoadRequest_h
|
|
|
|
#define mozilla_dom_ScriptLoadRequest_h
|
|
|
|
|
|
|
|
#include "mozilla/CORSMode.h"
|
|
|
|
#include "mozilla/dom/SRIMetadata.h"
|
|
|
|
#include "mozilla/LinkedList.h"
|
2018-05-16 17:58:13 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2017-05-08 09:24:44 +03:00
|
|
|
#include "mozilla/net/ReferrerPolicy.h"
|
2018-05-16 17:58:13 +03:00
|
|
|
#include "mozilla/Variant.h"
|
2017-05-08 09:24:44 +03:00
|
|
|
#include "mozilla/Vector.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsIScriptElement.h"
|
|
|
|
|
|
|
|
class nsICacheInfoChannel;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class ModuleLoadRequest;
|
|
|
|
class ScriptLoadRequestList;
|
|
|
|
|
|
|
|
enum class ScriptKind {
|
2018-01-08 18:17:33 +03:00
|
|
|
eClassic,
|
|
|
|
eModule
|
2017-05-08 09:24:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A class that handles loading and evaluation of <script> elements.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ScriptLoadRequest : public nsISupports,
|
|
|
|
private mozilla::LinkedListElement<ScriptLoadRequest>
|
|
|
|
{
|
|
|
|
typedef LinkedListElement<ScriptLoadRequest> super;
|
|
|
|
|
|
|
|
// Allow LinkedListElement<ScriptLoadRequest> to cast us to itself as needed.
|
|
|
|
friend class mozilla::LinkedListElement<ScriptLoadRequest>;
|
|
|
|
friend class ScriptLoadRequestList;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~ScriptLoadRequest();
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScriptLoadRequest(ScriptKind aKind,
|
2017-12-15 00:13:57 +03:00
|
|
|
nsIURI* aURI,
|
2017-05-08 09:24:44 +03:00
|
|
|
nsIScriptElement* aElement,
|
|
|
|
mozilla::CORSMode aCORSMode,
|
2017-12-21 13:58:53 +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
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ScriptLoadRequest)
|
|
|
|
|
|
|
|
bool IsModuleRequest() const
|
|
|
|
{
|
2018-01-08 18:17:33 +03:00
|
|
|
return mKind == ScriptKind::eModule;
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ModuleLoadRequest* AsModuleRequest();
|
|
|
|
|
|
|
|
void FireScriptAvailable(nsresult aResult)
|
|
|
|
{
|
2017-12-01 16:12:48 +03:00
|
|
|
bool isInlineClassicScript = mIsInline && !IsModuleRequest();
|
|
|
|
mElement->ScriptAvailable(aResult, mElement, isInlineClassicScript, mURI,
|
|
|
|
mLineNo);
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
|
|
|
void FireScriptEvaluated(nsresult aResult)
|
|
|
|
{
|
|
|
|
mElement->ScriptEvaluated(aResult, mElement, mIsInline);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPreload()
|
|
|
|
{
|
|
|
|
return mElement == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Cancel();
|
|
|
|
|
|
|
|
bool IsCanceled() const
|
|
|
|
{
|
|
|
|
return mIsCanceled;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetReady();
|
|
|
|
|
2018-05-10 16:51:14 +03:00
|
|
|
JS::OffThreadToken** OffThreadTokenPtr()
|
2017-05-08 09:24:44 +03:00
|
|
|
{
|
|
|
|
return mOffThreadToken ? &mOffThreadToken : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsTracking() const
|
|
|
|
{
|
|
|
|
return mIsTracking;
|
|
|
|
}
|
|
|
|
void SetIsTracking()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsTracking);
|
|
|
|
mIsTracking = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class Progress : uint8_t {
|
2018-01-08 18:17:33 +03:00
|
|
|
eLoading, // Request either source or bytecode
|
|
|
|
eLoading_Source, // Explicitly Request source stream
|
|
|
|
eCompiling,
|
|
|
|
eFetchingImports,
|
|
|
|
eReady
|
2017-05-08 09:24:44 +03:00
|
|
|
};
|
|
|
|
|
2018-01-08 18:17:33 +03:00
|
|
|
bool IsReadyToRun() const
|
|
|
|
{
|
|
|
|
return mProgress == Progress::eReady;
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
2018-01-08 18:17:33 +03:00
|
|
|
bool IsLoading() const
|
|
|
|
{
|
|
|
|
return mProgress == Progress::eLoading ||
|
|
|
|
mProgress == Progress::eLoading_Source;
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
2018-01-08 18:17:33 +03:00
|
|
|
bool IsLoadingSource() const
|
|
|
|
{
|
|
|
|
return mProgress == Progress::eLoading_Source;
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
2018-01-08 18:17:33 +03:00
|
|
|
bool InCompilingStage() const
|
|
|
|
{
|
|
|
|
return mProgress == Progress::eCompiling ||
|
2017-05-08 09:24:44 +03:00
|
|
|
(IsReadyToRun() && mWasCompiledOMT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type of data provided by the nsChannel.
|
|
|
|
enum class DataType : uint8_t {
|
2018-01-08 18:17:33 +03:00
|
|
|
eUnknown,
|
2018-05-16 17:58:13 +03:00
|
|
|
eTextSource,
|
|
|
|
eBinASTSource,
|
2018-01-08 18:17:33 +03:00
|
|
|
eBytecode
|
2017-05-08 09:24:44 +03:00
|
|
|
};
|
|
|
|
|
2018-01-08 18:17:33 +03:00
|
|
|
bool IsUnknownDataType() const
|
|
|
|
{
|
|
|
|
return mDataType == DataType::eUnknown;
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
2018-05-16 17:58:13 +03:00
|
|
|
bool IsTextSource() const
|
|
|
|
{
|
|
|
|
return mDataType == DataType::eTextSource;
|
|
|
|
}
|
|
|
|
bool IsBinASTSource() const
|
|
|
|
{
|
|
|
|
#ifdef JS_BUILD_BINAST
|
|
|
|
return mDataType == DataType::eBinASTSource;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2018-01-08 18:17:33 +03:00
|
|
|
bool IsSource() const
|
|
|
|
{
|
2018-05-16 17:58:13 +03:00
|
|
|
return IsTextSource() || IsBinASTSource();
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
2018-01-08 18:17:33 +03:00
|
|
|
bool IsBytecode() const
|
|
|
|
{
|
|
|
|
return mDataType == DataType::eBytecode;
|
2017-05-08 09:24:44 +03:00
|
|
|
}
|
|
|
|
|
2018-05-16 17:58:13 +03:00
|
|
|
void SetUnknownDataType();
|
|
|
|
void SetTextSource();
|
|
|
|
void SetBinASTSource();
|
|
|
|
void SetBytecode();
|
|
|
|
|
2018-07-30 16:49:02 +03:00
|
|
|
const Vector<char16_t>& ScriptText() const {
|
2018-05-16 17:58:13 +03:00
|
|
|
MOZ_ASSERT(IsTextSource());
|
2018-07-30 16:49:02 +03:00
|
|
|
return mScriptData->as<Vector<char16_t>>();
|
2018-05-16 17:58:13 +03:00
|
|
|
}
|
2018-07-30 16:49:02 +03:00
|
|
|
Vector<char16_t>& ScriptText() {
|
2018-05-16 17:58:13 +03:00
|
|
|
MOZ_ASSERT(IsTextSource());
|
2018-07-30 16:49:02 +03:00
|
|
|
return mScriptData->as<Vector<char16_t>>();
|
2018-05-16 17:58:13 +03:00
|
|
|
}
|
2018-07-30 16:49:02 +03:00
|
|
|
const Vector<uint8_t>& ScriptBinASTData() const {
|
2018-05-16 17:58:13 +03:00
|
|
|
MOZ_ASSERT(IsBinASTSource());
|
2018-07-30 16:49:02 +03:00
|
|
|
return mScriptData->as<Vector<uint8_t>>();
|
2018-05-16 17:58:13 +03:00
|
|
|
}
|
2018-07-30 16:49:02 +03:00
|
|
|
Vector<uint8_t>& ScriptBinASTData() {
|
2018-05-16 17:58:13 +03:00
|
|
|
MOZ_ASSERT(IsBinASTSource());
|
2018-07-30 16:49:02 +03:00
|
|
|
return mScriptData->as<Vector<uint8_t>>();
|
2018-05-16 17:58:13 +03:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:17:33 +03:00
|
|
|
enum class ScriptMode : uint8_t {
|
|
|
|
eBlocking,
|
|
|
|
eDeferred,
|
|
|
|
eAsync
|
|
|
|
};
|
|
|
|
|
|
|
|
void SetScriptMode(bool aDeferAttr, bool aAsyncAttr);
|
|
|
|
|
|
|
|
bool IsBlockingScript() const
|
|
|
|
{
|
|
|
|
return mScriptMode == ScriptMode::eBlocking;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDeferredScript() const
|
|
|
|
{
|
|
|
|
return mScriptMode == ScriptMode::eDeferred;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAsyncScript() const
|
|
|
|
{
|
|
|
|
return mScriptMode == ScriptMode::eAsync;
|
|
|
|
}
|
|
|
|
|
2018-02-15 16:18:59 +03:00
|
|
|
virtual bool IsTopLevel() const
|
|
|
|
{
|
|
|
|
// Classic scripts are always top level.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-16 17:58:13 +03:00
|
|
|
bool ShouldAcceptBinASTEncoding() const;
|
|
|
|
|
|
|
|
void ClearScriptSource();
|
2018-02-15 16:18:59 +03:00
|
|
|
|
2017-05-08 09:24:44 +03:00
|
|
|
void MaybeCancelOffThreadScript();
|
|
|
|
void DropBytecodeCacheReferences();
|
|
|
|
|
|
|
|
using super::getNext;
|
|
|
|
using super::isInList;
|
|
|
|
|
|
|
|
const ScriptKind mKind;
|
|
|
|
nsCOMPtr<nsIScriptElement> mElement;
|
|
|
|
bool mScriptFromHead; // Synchronous head script block loading of other non js/css content.
|
|
|
|
Progress mProgress; // Are we still waiting for a load to complete?
|
|
|
|
DataType mDataType; // Does this contain Source or Bytecode?
|
2018-01-08 18:17:33 +03:00
|
|
|
ScriptMode mScriptMode; // Whether this is a blocking, defer or async script.
|
2017-05-08 09:24:44 +03:00
|
|
|
bool mIsInline; // Is the script inline or loaded?
|
|
|
|
bool mHasSourceMapURL; // Does the HTTP header have a source map url?
|
2018-01-08 18:17:33 +03:00
|
|
|
bool mInDeferList; // True if we live in mDeferRequests.
|
|
|
|
bool mInAsyncList; // True if we live in mLoadingAsyncRequests or mLoadedAsyncRequests.
|
2017-05-08 09:24:44 +03:00
|
|
|
bool mIsNonAsyncScriptInserted; // True if we live in mNonAsyncExternalScriptInsertedRequests
|
|
|
|
bool mIsXSLT; // True if we live in mXSLTRequests.
|
|
|
|
bool mIsCanceled; // True if we have been explicitly canceled.
|
|
|
|
bool mWasCompiledOMT; // True if the script has been compiled off main thread.
|
|
|
|
bool mIsTracking; // True if the script comes from a source on our tracking protection list.
|
2018-05-10 16:51:14 +03:00
|
|
|
JS::OffThreadToken* mOffThreadToken; // Off-thread parsing token.
|
2017-05-08 09:24:44 +03:00
|
|
|
nsString mSourceMapURL; // Holds source map url for loaded scripts
|
|
|
|
|
|
|
|
// Holds the top-level JSScript that corresponds to the current source, once
|
|
|
|
// it is parsed, and planned to be saved in the bytecode cache.
|
|
|
|
JS::Heap<JSScript*> mScript;
|
|
|
|
|
2018-05-16 17:58:13 +03:00
|
|
|
// Holds script source data for non-inline scripts. Don't use nsString so we
|
|
|
|
// can give ownership to jsapi. Holds either char16_t source text characters
|
|
|
|
// or BinAST encoded bytes depending on mSourceEncoding.
|
2018-07-30 16:49:02 +03:00
|
|
|
Maybe<Variant<Vector<char16_t>, Vector<uint8_t>>> mScriptData;
|
2017-05-08 09:24:44 +03:00
|
|
|
|
|
|
|
// Holds the SRI serialized hash and the script bytecode for non-inline
|
|
|
|
// scripts.
|
|
|
|
mozilla::Vector<uint8_t> mScriptBytecode;
|
|
|
|
uint32_t mBytecodeOffset; // Offset of the bytecode in mScriptBytecode
|
|
|
|
|
2017-12-15 00:13:57 +03:00
|
|
|
const nsCOMPtr<nsIURI> mURI;
|
2017-10-05 08:16:32 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
|
2017-05-08 09:24:44 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> mOriginPrincipal;
|
|
|
|
nsAutoCString mURL; // Keep the URI's filename alive during off thread parsing.
|
|
|
|
int32_t mLineNo;
|
|
|
|
const mozilla::CORSMode mCORSMode;
|
2017-12-21 13:58:53 +03:00
|
|
|
const SRIMetadata mIntegrity;
|
2017-12-19 18:30:49 +03:00
|
|
|
const nsCOMPtr<nsIURI> mReferrer;
|
|
|
|
const mozilla::net::ReferrerPolicy mReferrerPolicy;
|
2017-05-08 09:24:44 +03:00
|
|
|
|
|
|
|
// Holds the Cache information, which is used to register the bytecode
|
|
|
|
// on the cache entry, such that we can load it the next time.
|
|
|
|
nsCOMPtr<nsICacheInfoChannel> mCacheInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScriptLoadRequestList : private mozilla::LinkedList<ScriptLoadRequest>
|
|
|
|
{
|
|
|
|
typedef mozilla::LinkedList<ScriptLoadRequest> super;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~ScriptLoadRequestList();
|
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool Contains(ScriptLoadRequest* aElem) const;
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
using super::getFirst;
|
|
|
|
using super::isEmpty;
|
|
|
|
|
|
|
|
void AppendElement(ScriptLoadRequest* aElem)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aElem->isInList());
|
|
|
|
NS_ADDREF(aElem);
|
|
|
|
insertBack(aElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_MUST_USE
|
|
|
|
already_AddRefed<ScriptLoadRequest> Steal(ScriptLoadRequest* aElem)
|
|
|
|
{
|
|
|
|
aElem->removeFrom(*this);
|
|
|
|
return dont_AddRef(aElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_MUST_USE
|
|
|
|
already_AddRefed<ScriptLoadRequest> StealFirst()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!isEmpty());
|
|
|
|
return Steal(getFirst());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Remove(ScriptLoadRequest* aElem)
|
|
|
|
{
|
|
|
|
aElem->removeFrom(*this);
|
|
|
|
NS_RELEASE(aElem);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ImplCycleCollectionUnlink(ScriptLoadRequestList& aField);
|
|
|
|
|
|
|
|
void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
ScriptLoadRequestList& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags);
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_ScriptLoadRequest_h
|