Bug 1885489 - Part 5: Add SnapshotIterator::readInt32(). r=iain
[gecko.git] / parser / html / nsHtml5SpeculativeLoad.cpp
blob4ddc15b957abd8d23c8e4936b36842d9ef8ef148
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),
13 mIsAsync(false),
14 mIsDefer(false),
15 mIsLinkPreload(false),
16 mIsError(false),
17 mEncoding(nullptr) {
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) {
33 switch (mOpCode) {
34 case eSpeculativeLoadBase:
35 aExecutor->SetSpeculationBase(mUrlOrSizes);
36 break;
37 case eSpeculativeLoadCSP:
38 aExecutor->AddSpeculationCSP(
39 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity);
40 break;
41 case eSpeculativeLoadMetaReferrer:
42 aExecutor->UpdateReferrerInfoFromMeta(mReferrerPolicyOrIntegrity);
43 break;
44 case eSpeculativeLoadImage:
45 aExecutor->PreloadImage(
46 mUrlOrSizes, mCrossOrigin, mMedia, mCharsetOrSrcset,
47 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
48 mReferrerPolicyOrIntegrity, mIsLinkPreload, mFetchPriority);
49 break;
50 case eSpeculativeLoadOpenPicture:
51 aExecutor->PreloadOpenPicture();
52 break;
53 case eSpeculativeLoadEndPicture:
54 aExecutor->PreloadEndPicture();
55 break;
56 case eSpeculativeLoadPictureSource:
57 aExecutor->PreloadPictureSource(
58 mCharsetOrSrcset, mUrlOrSizes,
59 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
60 mMedia);
61 break;
62 case eSpeculativeLoadScript:
63 aExecutor->PreloadScript(
64 mUrlOrSizes, mCharsetOrSrcset,
65 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
66 mCrossOrigin, mMedia, mNonce, mFetchPriority,
67 mReferrerPolicyOrIntegrity, mScriptReferrerPolicy, false, mIsAsync,
68 mIsDefer, mIsLinkPreload);
69 break;
70 case eSpeculativeLoadScriptFromHead:
71 aExecutor->PreloadScript(
72 mUrlOrSizes, mCharsetOrSrcset,
73 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
74 mCrossOrigin, mMedia, mNonce, mFetchPriority,
75 mReferrerPolicyOrIntegrity, mScriptReferrerPolicy, true, mIsAsync,
76 mIsDefer, mIsLinkPreload);
77 break;
78 case eSpeculativeLoadStyle:
79 aExecutor->PreloadStyle(
80 mUrlOrSizes, mCharsetOrSrcset, mCrossOrigin, mMedia,
81 mReferrerPolicyOrIntegrity, mNonce,
82 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity,
83 mIsLinkPreload, mFetchPriority);
84 break;
85 case eSpeculativeLoadManifest:
86 // TODO: remove this
87 break;
88 case eSpeculativeLoadSetDocumentCharset: {
89 MOZ_ASSERT(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
90 .Length() == 1,
91 "Unexpected charset source string");
92 nsCharsetSource enumSource =
93 (nsCharsetSource)
94 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
95 .First();
96 aExecutor->SetDocumentCharsetAndSource(WrapNotNull(mEncoding),
97 enumSource);
98 if (mCommitEncodingSpeculation) {
99 aExecutor->CommitToInternalEncoding();
101 } break;
102 case eSpeculativeLoadSetDocumentMode: {
103 NS_ASSERTION(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
104 .Length() == 1,
105 "Unexpected document mode string");
106 nsHtml5DocumentMode mode =
107 (nsHtml5DocumentMode)
108 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
109 .First();
110 aExecutor->SetDocumentMode(mode);
111 } break;
112 case eSpeculativeLoadPreconnect:
113 aExecutor->Preconnect(mUrlOrSizes, mCrossOrigin);
114 break;
115 case eSpeculativeLoadFont:
116 aExecutor->PreloadFont(mUrlOrSizes, mCrossOrigin, mMedia,
117 mReferrerPolicyOrIntegrity, mFetchPriority);
118 break;
119 case eSpeculativeLoadFetch:
120 aExecutor->PreloadFetch(mUrlOrSizes, mCrossOrigin, mMedia,
121 mReferrerPolicyOrIntegrity, mFetchPriority);
122 break;
123 case eSpeculativeLoadMaybeComplainAboutCharset: {
124 MOZ_ASSERT(mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
125 .Length() == 2,
126 "Unexpected line number string");
127 uint32_t high =
128 (uint32_t)
129 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
130 .CharAt(0);
131 uint32_t low =
132 (uint32_t)
133 mTypeOrCharsetSourceOrDocumentModeOrMetaCSPOrSizesOrIntegrity
134 .CharAt(1);
135 uint32_t line = (high << 16) | low;
136 aExecutor->MaybeComplainAboutCharset(mMsgId, mIsError, (int32_t)line);
137 } break;
138 default:
139 MOZ_ASSERT_UNREACHABLE("Bogus speculative load.");
140 break;