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 "PreloadHashKey.h"
7 #include "mozilla/dom/Element.h" // StringToCORSMode
8 #include "mozilla/css/SheetLoadData.h"
9 #include "mozilla/dom/ReferrerPolicyBinding.h"
10 #include "nsIPrincipal.h"
11 #include "nsIReferrerInfo.h"
15 PreloadHashKey::PreloadHashKey(const nsIURI
* aKey
, ResourceType aAs
)
16 : nsURIHashKey(aKey
), mAs(aAs
) {}
18 PreloadHashKey::PreloadHashKey(const PreloadHashKey
* aKey
)
19 : nsURIHashKey(aKey
->mKey
) {
23 PreloadHashKey::PreloadHashKey(PreloadHashKey
&& aToMove
)
24 : nsURIHashKey(std::move(aToMove
)) {
25 mAs
= std::move(aToMove
.mAs
);
26 mCORSMode
= std::move(aToMove
.mCORSMode
);
27 mPrincipal
= std::move(aToMove
.mPrincipal
);
30 case ResourceType::SCRIPT
:
31 mScript
= std::move(aToMove
.mScript
);
33 case ResourceType::STYLE
:
34 mStyle
= std::move(aToMove
.mStyle
);
36 case ResourceType::IMAGE
:
38 case ResourceType::FONT
:
40 case ResourceType::FETCH
:
42 case ResourceType::NONE
:
47 PreloadHashKey
& PreloadHashKey::operator=(const PreloadHashKey
& aOther
) {
48 MOZ_ASSERT(mAs
== ResourceType::NONE
|| aOther
.mAs
== ResourceType::NONE
,
49 "Assigning more than once, only reset is allowed");
51 nsURIHashKey::operator=(aOther
);
54 mCORSMode
= aOther
.mCORSMode
;
55 mPrincipal
= aOther
.mPrincipal
;
58 case ResourceType::SCRIPT
:
59 mScript
= aOther
.mScript
;
61 case ResourceType::STYLE
:
62 mStyle
= aOther
.mStyle
;
64 case ResourceType::IMAGE
:
66 case ResourceType::FONT
:
68 case ResourceType::FETCH
:
70 case ResourceType::NONE
:
78 PreloadHashKey
PreloadHashKey::CreateAsScript(
79 nsIURI
* aURI
, CORSMode aCORSMode
, JS::loader::ScriptKind aScriptKind
) {
80 PreloadHashKey
key(aURI
, ResourceType::SCRIPT
);
81 key
.mCORSMode
= aCORSMode
;
83 key
.mScript
.mScriptKind
= aScriptKind
;
89 PreloadHashKey
PreloadHashKey::CreateAsScript(nsIURI
* aURI
,
90 const nsAString
& aCrossOrigin
,
91 const nsAString
& aType
) {
92 JS::loader::ScriptKind scriptKind
= JS::loader::ScriptKind::eClassic
;
93 if (aType
.LowerCaseEqualsASCII("module")) {
94 scriptKind
= JS::loader::ScriptKind::eModule
;
96 CORSMode crossOrigin
= dom::Element::StringToCORSMode(aCrossOrigin
);
97 return CreateAsScript(aURI
, crossOrigin
, scriptKind
);
101 PreloadHashKey
PreloadHashKey::CreateAsStyle(
102 nsIURI
* aURI
, nsIPrincipal
* aPrincipal
, CORSMode aCORSMode
,
103 css::SheetParsingMode aParsingMode
) {
104 PreloadHashKey
key(aURI
, ResourceType::STYLE
);
105 key
.mCORSMode
= aCORSMode
;
106 key
.mPrincipal
= aPrincipal
;
108 key
.mStyle
.mParsingMode
= aParsingMode
;
114 PreloadHashKey
PreloadHashKey::CreateAsStyle(
115 css::SheetLoadData
& aSheetLoadData
) {
116 return CreateAsStyle(aSheetLoadData
.mURI
, aSheetLoadData
.mTriggeringPrincipal
,
117 aSheetLoadData
.mSheet
->GetCORSMode(),
118 aSheetLoadData
.mSheet
->ParsingMode());
122 PreloadHashKey
PreloadHashKey::CreateAsImage(nsIURI
* aURI
,
123 nsIPrincipal
* aPrincipal
,
124 CORSMode aCORSMode
) {
125 PreloadHashKey
key(aURI
, ResourceType::IMAGE
);
126 key
.mCORSMode
= aCORSMode
;
127 key
.mPrincipal
= aPrincipal
;
133 PreloadHashKey
PreloadHashKey::CreateAsFetch(nsIURI
* aURI
, CORSMode aCORSMode
) {
134 PreloadHashKey
key(aURI
, ResourceType::FETCH
);
135 key
.mCORSMode
= aCORSMode
;
141 PreloadHashKey
PreloadHashKey::CreateAsFetch(nsIURI
* aURI
,
142 const nsAString
& aCrossOrigin
) {
143 return CreateAsFetch(aURI
, dom::Element::StringToCORSMode(aCrossOrigin
));
146 PreloadHashKey
PreloadHashKey::CreateAsFont(nsIURI
* aURI
, CORSMode aCORSMode
) {
147 PreloadHashKey
key(aURI
, ResourceType::FONT
);
148 key
.mCORSMode
= aCORSMode
;
153 bool PreloadHashKey::KeyEquals(KeyTypePointer aOther
) const {
154 if (mAs
!= aOther
->mAs
|| mCORSMode
!= aOther
->mCORSMode
) {
158 if (!mPrincipal
!= !aOther
->mPrincipal
) {
159 // One or the other has a principal, but not both... not equal
163 if (mPrincipal
&& !mPrincipal
->Equals(aOther
->mPrincipal
)) {
167 if (!nsURIHashKey::KeyEquals(
168 static_cast<const nsURIHashKey
*>(aOther
)->GetKey())) {
173 case ResourceType::SCRIPT
:
174 if (mScript
.mScriptKind
!= aOther
->mScript
.mScriptKind
) {
178 case ResourceType::STYLE
: {
179 if (mStyle
.mParsingMode
!= aOther
->mStyle
.mParsingMode
) {
184 case ResourceType::IMAGE
:
185 // No more checks needed. The image cache key consists of the document
186 // (which we are scoped into), origin attributes (which we compare as part
187 // of the principal check) and the URL. imgLoader::ValidateEntry compares
188 // CORS, referrer info and principal, which we do by default.
190 case ResourceType::FONT
:
192 case ResourceType::FETCH
:
193 // No more checks needed.
195 case ResourceType::NONE
:
203 PLDHashNumber
PreloadHashKey::HashKey(KeyTypePointer aKey
) {
204 PLDHashNumber hash
= nsURIHashKey::HashKey(aKey
->mKey
);
206 // Enough to use the common attributes
207 hash
= AddToHash(hash
, static_cast<uint32_t>(aKey
->mAs
));
208 hash
= AddToHash(hash
, static_cast<uint32_t>(aKey
->mCORSMode
));
213 } // namespace mozilla