ported from mozilla: Bug 1385907 - Combine exclusive fields of nsHtml5SpeculativeLoad. r=hsivonen (72e90a73a9)

This commit is contained in:
roytam1 2024-10-01 11:28:49 +08:00
commit ac5f375e8f
2 changed files with 84 additions and 82 deletions

View file

@ -28,16 +28,18 @@ nsHtml5SpeculativeLoad::Perform(nsHtml5TreeOpExecutor* aExecutor)
{ {
switch (mOpCode) { switch (mOpCode) {
case eSpeculativeLoadBase: case eSpeculativeLoadBase:
aExecutor->SetSpeculationBase(mUrl); aExecutor->SetSpeculationBase(mUrlOrSizes);
break; break;
case eSpeculativeLoadCSP: case eSpeculativeLoadCSP:
aExecutor->AddSpeculationCSP(mMetaCSP); aExecutor->AddSpeculationCSP(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity);
break; break;
case eSpeculativeLoadMetaReferrer: case eSpeculativeLoadMetaReferrer:
aExecutor->SetSpeculationReferrerPolicy(mReferrerPolicy); aExecutor->SetSpeculationReferrerPolicy(mReferrerPolicyOrIntegrity);
break; break;
case eSpeculativeLoadImage: case eSpeculativeLoadImage:
aExecutor->PreloadImage(mUrl, mCrossOrigin, mSrcset, mSizes, mReferrerPolicy); aExecutor->PreloadImage(mUrlOrSizes, mCrossOriginOrMedia, mCharsetOrSrcset,
mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
mReferrerPolicyOrIntegrity);
break; break;
case eSpeculativeLoadOpenPicture: case eSpeculativeLoadOpenPicture:
aExecutor->PreloadOpenPicture(); aExecutor->PreloadOpenPicture();
@ -46,56 +48,61 @@ nsHtml5SpeculativeLoad::Perform(nsHtml5TreeOpExecutor* aExecutor)
aExecutor->PreloadEndPicture(); aExecutor->PreloadEndPicture();
break; break;
case eSpeculativeLoadPictureSource: case eSpeculativeLoadPictureSource:
aExecutor->PreloadPictureSource(mSrcset, mSizes, mTypeOrCharsetSourceOrDocumentMode, aExecutor->PreloadPictureSource(mCharsetOrSrcset, mUrlOrSizes,
mMedia); mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
mCrossOriginOrMedia);
break; break;
case eSpeculativeLoadScript: case eSpeculativeLoadScript:
aExecutor->PreloadScript(mUrl, mCharset, mTypeOrCharsetSourceOrDocumentMode, aExecutor->PreloadScript(mUrlOrSizes, mCharsetOrSrcset,
mCrossOrigin, mIntegrity, false, mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
mCrossOriginOrMedia, mReferrerPolicyOrIntegrity, false,
mIsAsync, mIsDefer, false); mIsAsync, mIsDefer, false);
break; break;
case eSpeculativeLoadScriptFromHead: case eSpeculativeLoadScriptFromHead:
aExecutor->PreloadScript(mUrl, mCharset, mTypeOrCharsetSourceOrDocumentMode, aExecutor->PreloadScript(mUrlOrSizes, mCharsetOrSrcset,
mCrossOrigin, mIntegrity, true, mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
mCrossOriginOrMedia, mReferrerPolicyOrIntegrity, true,
mIsAsync, mIsDefer, false); mIsAsync, mIsDefer, false);
break; break;
case eSpeculativeLoadNoModuleScript: case eSpeculativeLoadNoModuleScript:
aExecutor->PreloadScript(mUrl, mCharset, mTypeOrCharsetSourceOrDocumentMode, aExecutor->PreloadScript(mUrlOrSizes, mCharsetOrSrcset,
mCrossOrigin, mIntegrity, false, mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
mCrossOriginOrMedia, mReferrerPolicyOrIntegrity, false,
mIsAsync, mIsDefer, true); mIsAsync, mIsDefer, true);
break; break;
case eSpeculativeLoadNoModuleScriptFromHead: case eSpeculativeLoadNoModuleScriptFromHead:
aExecutor->PreloadScript(mUrl, mCharset, mTypeOrCharsetSourceOrDocumentMode, aExecutor->PreloadScript(mUrlOrSizes, mCharsetOrSrcset,
mCrossOrigin, mIntegrity, true, mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
mCrossOriginOrMedia, mReferrerPolicyOrIntegrity, true,
mIsAsync, mIsDefer, true); mIsAsync, mIsDefer, true);
break; break;
case eSpeculativeLoadStyle: case eSpeculativeLoadStyle:
aExecutor->PreloadStyle(mUrl, mCharset, mCrossOrigin, mReferrerPolicy, aExecutor->PreloadStyle(mUrlOrSizes, mCharsetOrSrcset, mCrossOriginOrMedia, mReferrerPolicyOrIntegrity,
mIntegrity); mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity);
break; break;
case eSpeculativeLoadManifest: case eSpeculativeLoadManifest:
aExecutor->ProcessOfflineManifest(mUrl); aExecutor->ProcessOfflineManifest(mUrlOrSizes);
break; break;
case eSpeculativeLoadSetDocumentCharset: { case eSpeculativeLoadSetDocumentCharset: {
nsAutoCString narrowName; nsAutoCString narrowName;
CopyUTF16toUTF8(mCharset, narrowName); CopyUTF16toUTF8(mCharsetOrSrcset, narrowName);
NS_ASSERTION(mTypeOrCharsetSourceOrDocumentMode.Length() == 1, NS_ASSERTION(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity.Length() == 1,
"Unexpected charset source string"); "Unexpected charset source string");
int32_t intSource = (int32_t)mTypeOrCharsetSourceOrDocumentMode.First(); int32_t intSource = (int32_t)mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity.First();
aExecutor->SetDocumentCharsetAndSource(narrowName, aExecutor->SetDocumentCharsetAndSource(narrowName,
intSource); intSource);
} }
break; break;
case eSpeculativeLoadSetDocumentMode: { case eSpeculativeLoadSetDocumentMode: {
NS_ASSERTION(mTypeOrCharsetSourceOrDocumentMode.Length() == 1, NS_ASSERTION(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity.Length() == 1,
"Unexpected document mode string"); "Unexpected document mode string");
nsHtml5DocumentMode mode = nsHtml5DocumentMode mode =
(nsHtml5DocumentMode)mTypeOrCharsetSourceOrDocumentMode.First(); (nsHtml5DocumentMode)mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity.First();
aExecutor->SetDocumentMode(mode); aExecutor->SetDocumentMode(mode);
} }
break; break;
case eSpeculativeLoadPreconnect: case eSpeculativeLoadPreconnect:
aExecutor->Preconnect(mUrl, mCrossOrigin); aExecutor->Preconnect(mUrlOrSizes, mCrossOriginOrMedia);
break; break;
default: default:
NS_NOTREACHED("Bogus speculative load."); NS_NOTREACHED("Bogus speculative load.");

View file

@ -43,7 +43,7 @@ class nsHtml5SpeculativeLoad {
NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized, NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized,
"Trying to reinitialize a speculative load!"); "Trying to reinitialize a speculative load!");
mOpCode = eSpeculativeLoadBase; mOpCode = eSpeculativeLoadBase;
aUrl.ToString(mUrl); aUrl.ToString(mUrlOrSizes);
} }
inline void InitMetaCSP(nsHtml5String aCSP) inline void InitMetaCSP(nsHtml5String aCSP)
@ -53,7 +53,7 @@ class nsHtml5SpeculativeLoad {
mOpCode = eSpeculativeLoadCSP; mOpCode = eSpeculativeLoadCSP;
nsString csp; // Not Auto, because using it to hold nsStringBuffer* nsString csp; // Not Auto, because using it to hold nsStringBuffer*
aCSP.ToString(csp); aCSP.ToString(csp);
mMetaCSP.Assign( mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity.Assign(
nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(csp)); nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(csp));
} }
@ -65,7 +65,7 @@ class nsHtml5SpeculativeLoad {
nsString nsString
referrerPolicy; // Not Auto, because using it to hold nsStringBuffer* referrerPolicy; // Not Auto, because using it to hold nsStringBuffer*
aReferrerPolicy.ToString(referrerPolicy); aReferrerPolicy.ToString(referrerPolicy);
mReferrerPolicy.Assign( mReferrerPolicyOrIntegrity.Assign(
nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>( nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(
referrerPolicy)); referrerPolicy));
} }
@ -79,16 +79,16 @@ class nsHtml5SpeculativeLoad {
NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized, NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized,
"Trying to reinitialize a speculative load!"); "Trying to reinitialize a speculative load!");
mOpCode = eSpeculativeLoadImage; mOpCode = eSpeculativeLoadImage;
aUrl.ToString(mUrl); aUrl.ToString(mUrlOrSizes);
aCrossOrigin.ToString(mCrossOrigin); aCrossOrigin.ToString(mCrossOriginOrMedia);
nsString nsString
referrerPolicy; // Not Auto, because using it to hold nsStringBuffer* referrerPolicy; // Not Auto, because using it to hold nsStringBuffer*
aReferrerPolicy.ToString(referrerPolicy); aReferrerPolicy.ToString(referrerPolicy);
mReferrerPolicy.Assign( mReferrerPolicyOrIntegrity.Assign(
nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>( nsContentUtils::TrimWhitespace<nsContentUtils::IsHTMLWhitespace>(
referrerPolicy)); referrerPolicy));
aSrcset.ToString(mSrcset); aSrcset.ToString(mCharsetOrSrcset);
aSizes.ToString(mSizes); aSizes.ToString(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity);
} }
// <picture> elements have multiple <source> nodes followed by an <img>, // <picture> elements have multiple <source> nodes followed by an <img>,
@ -120,10 +120,10 @@ class nsHtml5SpeculativeLoad {
NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized, NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized,
"Trying to reinitialize a speculative load!"); "Trying to reinitialize a speculative load!");
mOpCode = eSpeculativeLoadPictureSource; mOpCode = eSpeculativeLoadPictureSource;
aSrcset.ToString(mSrcset); aSrcset.ToString(mCharsetOrSrcset);
aSizes.ToString(mSizes); aSizes.ToString(mUrlOrSizes);
aType.ToString(mTypeOrCharsetSourceOrDocumentMode); aType.ToString(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity);
aMedia.ToString(mMedia); aMedia.ToString(mCrossOriginOrMedia);
} }
inline void InitScript(nsHtml5String aUrl, inline void InitScript(nsHtml5String aUrl,
@ -145,11 +145,11 @@ class nsHtml5SpeculativeLoad {
mOpCode = aParserInHead ? eSpeculativeLoadScriptFromHead mOpCode = aParserInHead ? eSpeculativeLoadScriptFromHead
: eSpeculativeLoadScript; : eSpeculativeLoadScript;
} }
aUrl.ToString(mUrl); aUrl.ToString(mUrlOrSizes);
aCharset.ToString(mCharset); aCharset.ToString(mCharsetOrSrcset);
aType.ToString(mTypeOrCharsetSourceOrDocumentMode); aType.ToString(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity);
aCrossOrigin.ToString(mCrossOrigin); aCrossOrigin.ToString(mCrossOriginOrMedia);
aIntegrity.ToString(mIntegrity); aIntegrity.ToString(mReferrerPolicyOrIntegrity);
mIsAsync = aAsync; mIsAsync = aAsync;
mIsDefer = aDefer; mIsDefer = aDefer;
} }
@ -163,11 +163,11 @@ class nsHtml5SpeculativeLoad {
NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized, NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized,
"Trying to reinitialize a speculative load!"); "Trying to reinitialize a speculative load!");
mOpCode = eSpeculativeLoadStyle; mOpCode = eSpeculativeLoadStyle;
aUrl.ToString(mUrl); aUrl.ToString(mUrlOrSizes);
aCharset.ToString(mCharset); aCharset.ToString(mCharsetOrSrcset);
aCrossOrigin.ToString(mCrossOrigin); aCrossOrigin.ToString(mCrossOriginOrMedia);
aReferrerPolicy.ToString(mReferrerPolicy); aReferrerPolicy.ToString(mReferrerPolicyOrIntegrity);
aIntegrity.ToString(mIntegrity); aIntegrity.ToString(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity);
} }
/** /**
@ -186,7 +186,7 @@ class nsHtml5SpeculativeLoad {
NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized, NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized,
"Trying to reinitialize a speculative load!"); "Trying to reinitialize a speculative load!");
mOpCode = eSpeculativeLoadManifest; mOpCode = eSpeculativeLoadManifest;
aUrl.ToString(mUrl); aUrl.ToString(mUrlOrSizes);
} }
/** /**
@ -205,8 +205,8 @@ class nsHtml5SpeculativeLoad {
NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized, NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized,
"Trying to reinitialize a speculative load!"); "Trying to reinitialize a speculative load!");
mOpCode = eSpeculativeLoadSetDocumentCharset; mOpCode = eSpeculativeLoadSetDocumentCharset;
CopyUTF8toUTF16(aCharset, mCharset); CopyUTF8toUTF16(aCharset, mCharsetOrSrcset);
mTypeOrCharsetSourceOrDocumentMode.Assign((char16_t)aCharsetSource); mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity.Assign((char16_t)aCharsetSource);
} }
/** /**
@ -220,7 +220,7 @@ class nsHtml5SpeculativeLoad {
NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized, NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized,
"Trying to reinitialize a speculative load!"); "Trying to reinitialize a speculative load!");
mOpCode = eSpeculativeLoadSetDocumentMode; mOpCode = eSpeculativeLoadSetDocumentMode;
mTypeOrCharsetSourceOrDocumentMode.Assign((char16_t)aMode); mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity.Assign((char16_t)aMode);
} }
inline void InitPreconnect(nsHtml5String aUrl, nsHtml5String aCrossOrigin) inline void InitPreconnect(nsHtml5String aUrl, nsHtml5String aCrossOrigin)
@ -228,8 +228,8 @@ class nsHtml5SpeculativeLoad {
NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized, NS_PRECONDITION(mOpCode == eSpeculativeLoadUninitialized,
"Trying to reinitialize a speculative load!"); "Trying to reinitialize a speculative load!");
mOpCode = eSpeculativeLoadPreconnect; mOpCode = eSpeculativeLoadPreconnect;
aUrl.ToString(mUrl); aUrl.ToString(mUrlOrSizes);
aCrossOrigin.ToString(mCrossOrigin); aCrossOrigin.ToString(mCrossOriginOrMedia);
} }
void Perform(nsHtml5TreeOpExecutor* aExecutor); void Perform(nsHtml5TreeOpExecutor* aExecutor);
@ -243,55 +243,50 @@ class nsHtml5SpeculativeLoad {
bool mIsAsync; bool mIsAsync;
bool mIsDefer; bool mIsDefer;
nsString mUrl; /* If mOpCode is eSpeculativeLoadPictureSource, this is the value of the
nsString mReferrerPolicy; * "sizes" attribute. If the attribute is not set, this will be a void
nsString mMetaCSP; * string. Otherwise it empty or the value of the url.
*/
nsString mUrlOrSizes;
/**
* If mOpCode is eSpeculativeLoadScript[FromHead], this is the value of the
* "integrity" attribute. If the attribute is not set, this will be a void
* string. Otherwise it is empty or the value of the referrer policy.
*/
nsString mReferrerPolicyOrIntegrity;
/** /**
* If mOpCode is eSpeculativeLoadStyle or eSpeculativeLoadScript[FromHead] * If mOpCode is eSpeculativeLoadStyle or eSpeculativeLoadScript[FromHead]
* then this is the value of the "charset" attribute. For * then this is the value of the "charset" attribute. For
* eSpeculativeLoadSetDocumentCharset it is the charset that the * eSpeculativeLoadSetDocumentCharset it is the charset that the
* document's charset is being set to. Otherwise it's empty. * document's charset is being set to. If mOpCode is eSpeculativeLoadImage
* or eSpeculativeLoadPictureSource, this is the value of the "srcset" attribute.
* If the attribute is not set, this will be a void string. Otherwise it's empty.
*/ */
nsString mCharset; nsString mCharsetOrSrcset;
/** /**
* If mOpCode is eSpeculativeLoadSetDocumentCharset, this is a * If mOpCode is eSpeculativeLoadSetDocumentCharset, this is a
* one-character string whose single character's code point is to be * one-character string whose single character's code point is to be
* interpreted as a charset source integer. If mOpCode is * interpreted as a charset source integer. If mOpCode is
* eSpeculativeLoadSetDocumentMode, this is a one-character string whose * eSpeculativeLoadSetDocumentMode, this is a one-character string whose
* single character's code point is to be interpreted as an * single character's code point is to be interpreted as an
* nsHtml5DocumentMode. Otherwise, it is empty or the value of the type * nsHtml5DocumentMode. If mOpCode is eSpeculativeLoadCSP, this is a meta
* attribute. * element's CSP value. If mOpCode is eSpeculativeLoadImage, this is the
* value of the "sizes" attribute. If the attribute is not set, this will
* be a void string. If mOpCode is eSpeculativeLoadStyle, this
* is the value of the "integrity" attribute. If the attribute is not set,
* this will be a void string. Otherwise it is empty or the value of the
* referrer policy. Otherwise, it is empty or the value of the type attribute.
*/ */
nsString mTypeOrCharsetSourceOrDocumentMode; nsString mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity;
/** /**
* If mOpCode is eSpeculativeLoadImage or eSpeculativeLoadScript[FromHead] * If mOpCode is eSpeculativeLoadImage or eSpeculativeLoadScript[FromHead]
* or eSpeculativeLoadPreconnect this is the value of the "crossorigin" * or eSpeculativeLoadPreconnect this is the value of the "crossorigin"
* attribute. If the attribute is not set, this will be a void string. * attribute. If the attribute is not set, this will be a void string.
* If mOpCode is eSpeculativeLoadPictureSource, this is the value of the
* "media" attribute. If the attribute is not set, this will be a void string.
*/ */
nsString mCrossOrigin; nsString mCrossOriginOrMedia;
/**
* If mOpCode is eSpeculativeLoadImage or eSpeculativeLoadPictureSource,
* this is the value of "srcset" attribute. If the attribute is not set,
* this will be a void string.
*/
nsString mSrcset;
/**
* If mOpCode is eSpeculativeLoadPictureSource, this is the value of "sizes"
* attribute. If the attribute is not set, this will be a void string.
*/
nsString mSizes;
/**
* If mOpCode is eSpeculativeLoadPictureSource, this is the value of "media"
* attribute. If the attribute is not set, this will be a void string.
*/
nsString mMedia;
/**
* If mOpCode is eSpeculativeLoadScript[FromHead], this is the value of the
* "integrity" attribute. If the attribute is not set, this will be a void
* string.
*/
nsString mIntegrity;
}; };
#endif // nsHtml5SpeculativeLoad_h #endif // nsHtml5SpeculativeLoad_h