1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsHtml5SpeculativeLoad.h"
6 #include "mozilla/Encoding.h"
7 #include "nsHtml5TreeOpExecutor.h"
9 using namespace mozilla
;
11 nsHtml5SpeculativeLoad::nsHtml5SpeculativeLoad()
12 : mOpCode(eSpeculativeLoadUninitialized
),
15 mIsLinkPreload(false),
18 MOZ_COUNT_CTOR(nsHtml5SpeculativeLoad
);
19 new (&mCharsetOrSrcset
) nsString
;
22 nsHtml5SpeculativeLoad::~nsHtml5SpeculativeLoad() {
23 MOZ_COUNT_DTOR(nsHtml5SpeculativeLoad
);
24 NS_ASSERTION(mOpCode
!= eSpeculativeLoadUninitialized
,
25 "Uninitialized speculative load.");
26 if (!(mOpCode
== eSpeculativeLoadSetDocumentCharset
||
27 mOpCode
== eSpeculativeLoadMaybeComplainAboutCharset
)) {
28 mCharsetOrSrcset
.~nsString();
32 void nsHtml5SpeculativeLoad::Perform(nsHtml5TreeOpExecutor
* aExecutor
) {
34 case eSpeculativeLoadBase
:
35 aExecutor
->SetSpeculationBase(mUrlOrSizes
);
37 case eSpeculativeLoadCSP
:
38 aExecutor
->AddSpeculationCSP(
39 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
);
41 case eSpeculativeLoadMetaReferrer
:
42 aExecutor
->UpdateReferrerInfoFromMeta(mReferrerPolicyOrIntegrity
);
44 case eSpeculativeLoadImage
:
45 aExecutor
->PreloadImage(
46 mUrlOrSizes
, mCrossOrigin
, mMedia
, mCharsetOrSrcset
,
47 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
48 mReferrerPolicyOrIntegrity
, mIsLinkPreload
, mFetchPriority
);
50 case eSpeculativeLoadOpenPicture
:
51 aExecutor
->PreloadOpenPicture();
53 case eSpeculativeLoadEndPicture
:
54 aExecutor
->PreloadEndPicture();
56 case eSpeculativeLoadPictureSource
:
57 aExecutor
->PreloadPictureSource(
58 mCharsetOrSrcset
, mUrlOrSizes
,
59 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
62 case eSpeculativeLoadScript
:
63 aExecutor
->PreloadScript(
64 mUrlOrSizes
, mCharsetOrSrcset
,
65 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
66 mCrossOrigin
, mMedia
, mNonce
, mFetchPriority
,
67 mReferrerPolicyOrIntegrity
, mScriptReferrerPolicy
, false, mIsAsync
,
68 mIsDefer
, mIsLinkPreload
);
70 case eSpeculativeLoadScriptFromHead
:
71 aExecutor
->PreloadScript(
72 mUrlOrSizes
, mCharsetOrSrcset
,
73 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
74 mCrossOrigin
, mMedia
, mNonce
, mFetchPriority
,
75 mReferrerPolicyOrIntegrity
, mScriptReferrerPolicy
, true, mIsAsync
,
76 mIsDefer
, mIsLinkPreload
);
78 case eSpeculativeLoadStyle
:
79 aExecutor
->PreloadStyle(
80 mUrlOrSizes
, mCharsetOrSrcset
, mCrossOrigin
, mMedia
,
81 mReferrerPolicyOrIntegrity
, mNonce
,
82 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
83 mIsLinkPreload
, mFetchPriority
);
85 case eSpeculativeLoadManifest
:
88 case eSpeculativeLoadSetDocumentCharset
: {
89 MOZ_ASSERT(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
91 "Unexpected charset source string");
92 nsCharsetSource enumSource
=
94 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
96 aExecutor
->SetDocumentCharsetAndSource(WrapNotNull(mEncoding
),
98 if (mCommitEncodingSpeculation
) {
99 aExecutor
->CommitToInternalEncoding();
102 case eSpeculativeLoadSetDocumentMode
: {
103 NS_ASSERTION(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
105 "Unexpected document mode string");
106 nsHtml5DocumentMode mode
=
107 (nsHtml5DocumentMode
)
108 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
110 aExecutor
->SetDocumentMode(mode
);
112 case eSpeculativeLoadPreconnect
:
113 aExecutor
->Preconnect(mUrlOrSizes
, mCrossOrigin
);
115 case eSpeculativeLoadFont
:
116 aExecutor
->PreloadFont(mUrlOrSizes
, mCrossOrigin
, mMedia
,
117 mReferrerPolicyOrIntegrity
, mFetchPriority
);
119 case eSpeculativeLoadFetch
:
120 aExecutor
->PreloadFetch(mUrlOrSizes
, mCrossOrigin
, mMedia
,
121 mReferrerPolicyOrIntegrity
, mFetchPriority
);
123 case eSpeculativeLoadMaybeComplainAboutCharset
: {
124 MOZ_ASSERT(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
126 "Unexpected line number string");
129 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
133 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
135 uint32_t line
= (high
<< 16) | low
;
136 aExecutor
->MaybeComplainAboutCharset(mMsgId
, mIsError
, (int32_t)line
);
139 MOZ_ASSERT_UNREACHABLE("Bogus speculative load.");