1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "PreloadService.h"
#include "FetchPreloader.h"
#include "PreloaderBase.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/dom/WindowGlobalChild.h"
#include "mozilla/dom/HTMLLinkElement.h"
#include "mozilla/dom/ScriptLoader.h"
#include "mozilla/Encoding.h"
#include "mozilla/FontPreloader.h"
#include "mozilla/StaticPrefs_network.h"
#include "nsNetUtil.h"
namespace mozilla {
PreloadService::PreloadService(dom::Document* aDoc) : mDocument(aDoc) {}
PreloadService::~PreloadService() = default;
bool PreloadService::RegisterPreload(const PreloadHashKey& aKey,
PreloaderBase* aPreload) {
if (PreloadExists(aKey)) {
return false;
}
mPreloads.Put(aKey, RefPtr{aPreload});
return true;
}
void PreloadService::DeregisterPreload(const PreloadHashKey& aKey) {
mPreloads.Remove(aKey);
}
void PreloadService::ClearAllPreloads() { mPreloads.Clear(); }
bool PreloadService::PreloadExists(const PreloadHashKey& aKey) {
bool found;
mPreloads.GetWeak(aKey, &found);
return found;
}
already_AddRefed<PreloaderBase> PreloadService::LookupPreload(
const PreloadHashKey& aKey) const {
return mPreloads.Get(aKey);
}
already_AddRefed<nsIURI> PreloadService::GetPreloadURI(const nsAString& aURL) {
nsIURI* base = BaseURIForPreload();
auto encoding = mDocument->GetDocumentCharacterSet();
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, encoding, base);
if (NS_FAILED(rv)) {
return nullptr;
}
return uri.forget();
}
already_AddRefed<PreloaderBase> PreloadService::PreloadLinkElement(
dom::HTMLLinkElement* aLinkElement, nsContentPolicyType aPolicyType) {
// Even if the pref is disabled, we still want to collect telemetry about
// attempted preloads.
const bool preloadEnabled = StaticPrefs::network_preload();
if (aPolicyType == nsIContentPolicy::TYPE_INVALID) {
MOZ_ASSERT_UNREACHABLE("Caller should check");
return nullptr;
}
if (auto* wgc = mDocument->GetWindowGlobalChild()) {
wgc->MaybeSendUpdateDocumentWouldPreloadResources();
}
if (!preloadEnabled) {
return nullptr;
}
nsAutoString as, charset, crossOrigin, integrity, referrerPolicy, srcset,
sizes, type, url;
nsCOMPtr<nsIURI> uri = aLinkElement->GetURI();
aLinkElement->GetAs(as);
aLinkElement->GetCharset(charset);
aLinkElement->GetImageSrcset(srcset);
aLinkElement->GetImageSizes(sizes);
aLinkElement->GetHref(url);
aLinkElement->GetCrossOrigin(crossOrigin);
aLinkElement->GetIntegrity(integrity);
aLinkElement->GetReferrerPolicy(referrerPolicy);
aLinkElement->GetType(type);
auto result =
PreloadOrCoalesce(uri, url, aPolicyType, as, type, charset, srcset, sizes,
integrity, crossOrigin, referrerPolicy);
if (!result.mPreloader) {
NotifyNodeEvent(aLinkElement, result.mAlreadyComplete);
return nullptr;
}
result.mPreloader->AddLinkPreloadNode(aLinkElement);
return result.mPreloader.forget();
}
void PreloadService::PreloadLinkHeader(
nsIURI* aURI, const nsAString& aURL, nsContentPolicyType aPolicyType,
const nsAString& aAs, const nsAString& aType, const nsAString& aIntegrity,
const nsAString& aSrcset, const nsAString& aSizes, const nsAString& aCORS,
const nsAString& aReferrerPolicy) {
// Even if the pref is disabled, we still want to collect telemetry about
// attempted preloads.
const bool preloadEnabled = StaticPrefs::network_preload();
if (aPolicyType == nsIContentPolicy::TYPE_INVALID) {
MOZ_ASSERT_UNREACHABLE("Caller should check");
return;
}
if (auto* wgc = mDocument->GetWindowGlobalChild()) {
wgc->MaybeSendUpdateDocumentWouldPreloadResources();
}
if (!preloadEnabled) {
return;
}
PreloadOrCoalesce(aURI, aURL, aPolicyType, aAs, aType, u""_ns, aSrcset,
aSizes, aIntegrity, aCORS, aReferrerPolicy);
}
PreloadService::PreloadOrCoalesceResult PreloadService::PreloadOrCoalesce(
nsIURI* aURI, const nsAString& aURL, nsContentPolicyType aPolicyType,
const nsAString& aAs, const nsAString& aType, const nsAString& aCharset,
const nsAString& aSrcset, const nsAString& aSizes,
const nsAString& aIntegrity, const nsAString& aCORS,
const nsAString& aReferrerPolicy) {
if (!aURI) {
MOZ_ASSERT_UNREACHABLE("Should not pass null nsIURI");
return {nullptr, false};
}
bool isImgSet = false;
PreloadHashKey preloadKey;
nsCOMPtr<nsIURI> uri = aURI;
if (aAs.LowerCaseEqualsASCII("script")) {
preloadKey = PreloadHashKey::CreateAsScript(uri, aCORS, aType);
} else if (aAs.LowerCaseEqualsASCII("style")) {
preloadKey = PreloadHashKey::CreateAsStyle(
uri, mDocument->NodePrincipal(), dom::Element::StringToCORSMode(aCORS),
css::eAuthorSheetFeatures /* see Loader::LoadSheet */);
} else if (aAs.LowerCaseEqualsASCII("image")) {
uri = mDocument->ResolvePreloadImage(BaseURIForPreload(), aURL, aSrcset,
aSizes, &isImgSet);
if (!uri) {
return {nullptr, false};
}
preloadKey = PreloadHashKey::CreateAsImage(
uri, mDocument->NodePrincipal(), dom::Element::StringToCORSMode(aCORS));
} else if (aAs.LowerCaseEqualsASCII("font")) {
preloadKey = PreloadHashKey::CreateAsFont(
uri, dom::Element::StringToCORSMode(aCORS));
} else if (aAs.LowerCaseEqualsASCII("fetch")) {
preloadKey = PreloadHashKey::CreateAsFetch(
uri, dom::Element::StringToCORSMode(aCORS));
} else {
return {nullptr, false};
}
if (RefPtr<PreloaderBase> preload = LookupPreload(preloadKey)) {
return {std::move(preload), false};
}
if (aAs.LowerCaseEqualsASCII("script")) {
PreloadScript(uri, aType, aCharset, aCORS, aReferrerPolicy, aIntegrity,
true /* isInHead - TODO */);
} else if (aAs.LowerCaseEqualsASCII("style")) {
switch (PreloadStyle(uri, aCharset, aCORS, aReferrerPolicy, aIntegrity)) {
case dom::SheetPreloadStatus::AlreadyComplete:
return {nullptr, /* already_complete = */ true};
case dom::SheetPreloadStatus::Errored:
case dom::SheetPreloadStatus::InProgress:
break;
}
} else if (aAs.LowerCaseEqualsASCII("image")) {
PreloadImage(uri, aCORS, aReferrerPolicy, isImgSet);
} else if (aAs.LowerCaseEqualsASCII("font")) {
PreloadFont(uri, aCORS, aReferrerPolicy);
} else if (aAs.LowerCaseEqualsASCII("fetch")) {
PreloadFetch(uri, aCORS, aReferrerPolicy);
}
return {LookupPreload(preloadKey), false};
}
void PreloadService::PreloadScript(nsIURI* aURI, const nsAString& aType,
const nsAString& aCharset,
const nsAString& aCrossOrigin,
const nsAString& aReferrerPolicy,
const nsAString& aIntegrity,
bool aScriptFromHead) {
mDocument->ScriptLoader()->PreloadURI(
aURI, aCharset, aType, aCrossOrigin, aIntegrity, aScriptFromHead, false,
false, false, true, PreloadReferrerPolicy(aReferrerPolicy));
}
dom::SheetPreloadStatus PreloadService::PreloadStyle(
nsIURI* aURI, const nsAString& aCharset, const nsAString& aCrossOrigin,
const nsAString& aReferrerPolicy, const nsAString& aIntegrity) {
return mDocument->PreloadStyle(
aURI, Encoding::ForLabel(aCharset), aCrossOrigin,
PreloadReferrerPolicy(aReferrerPolicy), aIntegrity, true);
}
void PreloadService::PreloadImage(nsIURI* aURI, const nsAString& aCrossOrigin,
const nsAString& aImageReferrerPolicy,
bool aIsImgSet) {
mDocument->PreLoadImage(aURI, aCrossOrigin,
PreloadReferrerPolicy(aImageReferrerPolicy),
aIsImgSet, true);
}
void PreloadService::PreloadFont(nsIURI* aURI, const nsAString& aCrossOrigin,
const nsAString& aReferrerPolicy) {
CORSMode cors = dom::Element::StringToCORSMode(aCrossOrigin);
auto key = PreloadHashKey::CreateAsFont(aURI, cors);
// * Bug 1618549: Depending on where we decide to do the deduplication, we may
// want to check if the font is already being preloaded here.
RefPtr<FontPreloader> preloader = new FontPreloader();
dom::ReferrerPolicy referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
preloader->OpenChannel(key, aURI, cors, referrerPolicy, mDocument);
}
void PreloadService::PreloadFetch(nsIURI* aURI, const nsAString& aCrossOrigin,
const nsAString& aReferrerPolicy) {
CORSMode cors = dom::Element::StringToCORSMode(aCrossOrigin);
auto key = PreloadHashKey::CreateAsFetch(aURI, cors);
// * Bug 1618549: Depending on where we decide to do the deduplication, we may
// want to check if a fetch is already being preloaded here.
RefPtr<FetchPreloader> preloader = new FetchPreloader();
dom::ReferrerPolicy referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
preloader->OpenChannel(key, aURI, cors, referrerPolicy, mDocument);
}
// static
void PreloadService::NotifyNodeEvent(nsINode* aNode, bool aSuccess) {
if (!aNode->IsInComposedDoc()) {
return;
}
// We don't dispatch synchronously since |node| might be in a DocGroup
// that we're not allowed to touch. (Our network request happens in the
// DocGroup of one of the mSources nodes--not necessarily this one).
RefPtr<AsyncEventDispatcher> dispatcher = new AsyncEventDispatcher(
aNode, aSuccess ? u"load"_ns : u"error"_ns, CanBubble::eNo);
dispatcher->RequireNodeInDocument();
dispatcher->PostDOMEvent();
}
dom::ReferrerPolicy PreloadService::PreloadReferrerPolicy(
const nsAString& aReferrerPolicy) {
dom::ReferrerPolicy referrerPolicy =
dom::ReferrerInfo::ReferrerPolicyAttributeFromString(aReferrerPolicy);
if (referrerPolicy == dom::ReferrerPolicy::_empty) {
referrerPolicy = mDocument->GetPreloadReferrerInfo()->ReferrerPolicy();
}
return referrerPolicy;
}
nsIURI* PreloadService::BaseURIForPreload() {
nsIURI* documentURI = mDocument->GetDocumentURI();
nsIURI* documentBaseURI = mDocument->GetDocBaseURI();
return (documentURI == documentBaseURI)
? (mSpeculationBaseURI ? mSpeculationBaseURI.get() : documentURI)
: documentBaseURI;
}
} // namespace mozilla
|