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),
17 MOZ_COUNT_CTOR(nsHtml5SpeculativeLoad
);
18 new (&mCharsetOrSrcset
) nsString
;
21 nsHtml5SpeculativeLoad::~nsHtml5SpeculativeLoad() {
22 MOZ_COUNT_DTOR(nsHtml5SpeculativeLoad
);
23 NS_ASSERTION(mOpCode
!= eSpeculativeLoadUninitialized
,
24 "Uninitialized speculative load.");
25 if (mOpCode
!= eSpeculativeLoadSetDocumentCharset
) {
26 mCharsetOrSrcset
.~nsString();
30 void nsHtml5SpeculativeLoad::Perform(nsHtml5TreeOpExecutor
* aExecutor
) {
32 case eSpeculativeLoadBase
:
33 aExecutor
->SetSpeculationBase(mUrlOrSizes
);
35 case eSpeculativeLoadCSP
:
36 aExecutor
->AddSpeculationCSP(
37 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
);
39 case eSpeculativeLoadMetaReferrer
:
40 aExecutor
->UpdateReferrerInfoFromMeta(mReferrerPolicyOrIntegrity
);
42 case eSpeculativeLoadImage
:
43 aExecutor
->PreloadImage(
44 mUrlOrSizes
, mCrossOrigin
, mMedia
, mCharsetOrSrcset
,
45 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
46 mReferrerPolicyOrIntegrity
, mIsLinkPreload
);
48 case eSpeculativeLoadOpenPicture
:
49 aExecutor
->PreloadOpenPicture();
51 case eSpeculativeLoadEndPicture
:
52 aExecutor
->PreloadEndPicture();
54 case eSpeculativeLoadPictureSource
:
55 aExecutor
->PreloadPictureSource(
56 mCharsetOrSrcset
, mUrlOrSizes
,
57 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
60 case eSpeculativeLoadScript
:
61 aExecutor
->PreloadScript(
62 mUrlOrSizes
, mCharsetOrSrcset
,
63 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
64 mCrossOrigin
, mMedia
, mReferrerPolicyOrIntegrity
,
65 mScriptReferrerPolicy
, false, mIsAsync
, mIsDefer
, false,
68 case eSpeculativeLoadScriptFromHead
:
69 aExecutor
->PreloadScript(
70 mUrlOrSizes
, mCharsetOrSrcset
,
71 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
72 mCrossOrigin
, mMedia
, mReferrerPolicyOrIntegrity
,
73 mScriptReferrerPolicy
, true, mIsAsync
, mIsDefer
, false,
76 case eSpeculativeLoadNoModuleScript
:
77 aExecutor
->PreloadScript(
78 mUrlOrSizes
, mCharsetOrSrcset
,
79 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
80 mCrossOrigin
, mMedia
, mReferrerPolicyOrIntegrity
,
81 mScriptReferrerPolicy
, false, mIsAsync
, mIsDefer
, true,
84 case eSpeculativeLoadNoModuleScriptFromHead
:
85 aExecutor
->PreloadScript(
86 mUrlOrSizes
, mCharsetOrSrcset
,
87 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
88 mCrossOrigin
, mMedia
, mReferrerPolicyOrIntegrity
,
89 mScriptReferrerPolicy
, true, mIsAsync
, mIsDefer
, true,
92 case eSpeculativeLoadStyle
:
93 aExecutor
->PreloadStyle(
94 mUrlOrSizes
, mCharsetOrSrcset
, mCrossOrigin
, mMedia
,
95 mReferrerPolicyOrIntegrity
,
96 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
,
99 case eSpeculativeLoadManifest
:
102 case eSpeculativeLoadSetDocumentCharset
: {
103 NS_ASSERTION(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
105 "Unexpected charset source string");
107 (int32_t)mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
109 aExecutor
->SetDocumentCharsetAndSource(WrapNotNull(mEncoding
), intSource
);
111 case eSpeculativeLoadSetDocumentMode
: {
112 NS_ASSERTION(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
114 "Unexpected document mode string");
115 nsHtml5DocumentMode mode
=
116 (nsHtml5DocumentMode
)
117 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
119 aExecutor
->SetDocumentMode(mode
);
121 case eSpeculativeLoadPreconnect
:
122 aExecutor
->Preconnect(mUrlOrSizes
, mCrossOrigin
);
124 case eSpeculativeLoadFont
:
125 aExecutor
->PreloadFont(mUrlOrSizes
, mCrossOrigin
, mMedia
,
126 mReferrerPolicyOrIntegrity
);
128 case eSpeculativeLoadFetch
:
129 aExecutor
->PreloadFetch(mUrlOrSizes
, mCrossOrigin
, mMedia
,
130 mReferrerPolicyOrIntegrity
);
133 MOZ_ASSERT_UNREACHABLE("Bogus speculative load.");