summaryrefslogtreecommitdiffstats
path: root/uriloader/preload/PreloadHashKey.cpp
blob: 989bb169496387953e8fc50e03d01c836b388038 (plain)
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
/* 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 "PreloadHashKey.h"

#include "mozilla/dom/Element.h"  // StringToCORSMode
#include "mozilla/css/SheetLoadData.h"
#include "mozilla/dom/ReferrerPolicyBinding.h"
#include "nsIPrincipal.h"
#include "nsIReferrerInfo.h"

namespace mozilla {

PreloadHashKey::PreloadHashKey(const nsIURI* aKey, ResourceType aAs)
    : nsURIHashKey(aKey), mAs(aAs) {}

PreloadHashKey::PreloadHashKey(const PreloadHashKey* aKey)
    : nsURIHashKey(aKey->mKey) {
  *this = *aKey;
}

PreloadHashKey::PreloadHashKey(PreloadHashKey&& aToMove)
    : nsURIHashKey(std::move(aToMove)) {
  mAs = std::move(aToMove.mAs);
  mCORSMode = std::move(aToMove.mCORSMode);
  mPrincipal = std::move(aToMove.mPrincipal);

  switch (mAs) {
    case ResourceType::SCRIPT:
      mScript = std::move(aToMove.mScript);
      break;
    case ResourceType::STYLE:
      mStyle = std::move(aToMove.mStyle);
      break;
    case ResourceType::IMAGE:
      break;
    case ResourceType::FONT:
      break;
    case ResourceType::FETCH:
      break;
    case ResourceType::NONE:
      break;
  }
}

PreloadHashKey& PreloadHashKey::operator=(const PreloadHashKey& aOther) {
  MOZ_ASSERT(mAs == ResourceType::NONE || aOther.mAs == ResourceType::NONE,
             "Assigning more than once, only reset is allowed");

  nsURIHashKey::operator=(aOther);

  mAs = aOther.mAs;
  mCORSMode = aOther.mCORSMode;
  mPrincipal = aOther.mPrincipal;

  switch (mAs) {
    case ResourceType::SCRIPT:
      mScript = aOther.mScript;
      break;
    case ResourceType::STYLE:
      mStyle = aOther.mStyle;
      break;
    case ResourceType::IMAGE:
      break;
    case ResourceType::FONT:
      break;
    case ResourceType::FETCH:
      break;
    case ResourceType::NONE:
      break;
  }

  return *this;
}

// static
PreloadHashKey PreloadHashKey::CreateAsScript(
    nsIURI* aURI, CORSMode aCORSMode, JS::loader::ScriptKind aScriptKind) {
  PreloadHashKey key(aURI, ResourceType::SCRIPT);
  key.mCORSMode = aCORSMode;

  key.mScript.mScriptKind = aScriptKind;

  return key;
}

// static
PreloadHashKey PreloadHashKey::CreateAsScript(nsIURI* aURI,
                                              const nsAString& aCrossOrigin,
                                              const nsAString& aType) {
  JS::loader::ScriptKind scriptKind = JS::loader::ScriptKind::eClassic;
  if (aType.LowerCaseEqualsASCII("module")) {
    scriptKind = JS::loader::ScriptKind::eModule;
  }
  CORSMode crossOrigin = dom::Element::StringToCORSMode(aCrossOrigin);
  return CreateAsScript(aURI, crossOrigin, scriptKind);
}

// static
PreloadHashKey PreloadHashKey::CreateAsStyle(
    nsIURI* aURI, nsIPrincipal* aPrincipal, CORSMode aCORSMode,
    css::SheetParsingMode aParsingMode) {
  PreloadHashKey key(aURI, ResourceType::STYLE);
  key.mCORSMode = aCORSMode;
  key.mPrincipal = aPrincipal;

  key.mStyle.mParsingMode = aParsingMode;

  return key;
}

// static
PreloadHashKey PreloadHashKey::CreateAsStyle(
    css::SheetLoadData& aSheetLoadData) {
  return CreateAsStyle(aSheetLoadData.mURI, aSheetLoadData.mTriggeringPrincipal,
                       aSheetLoadData.mSheet->GetCORSMode(),
                       aSheetLoadData.mSheet->ParsingMode());
}

// static
PreloadHashKey PreloadHashKey::CreateAsImage(nsIURI* aURI,
                                             nsIPrincipal* aPrincipal,
                                             CORSMode aCORSMode) {
  PreloadHashKey key(aURI, ResourceType::IMAGE);
  key.mCORSMode = aCORSMode;
  key.mPrincipal = aPrincipal;

  return key;
}

// static
PreloadHashKey PreloadHashKey::CreateAsFetch(nsIURI* aURI, CORSMode aCORSMode) {
  PreloadHashKey key(aURI, ResourceType::FETCH);
  key.mCORSMode = aCORSMode;

  return key;
}

// static
PreloadHashKey PreloadHashKey::CreateAsFetch(nsIURI* aURI,
                                             const nsAString& aCrossOrigin) {
  return CreateAsFetch(aURI, dom::Element::StringToCORSMode(aCrossOrigin));
}

PreloadHashKey PreloadHashKey::CreateAsFont(nsIURI* aURI, CORSMode aCORSMode) {
  PreloadHashKey key(aURI, ResourceType::FONT);
  key.mCORSMode = aCORSMode;

  return key;
}

bool PreloadHashKey::KeyEquals(KeyTypePointer aOther) const {
  if (mAs != aOther->mAs || mCORSMode != aOther->mCORSMode) {
    return false;
  }

  if (!mPrincipal != !aOther->mPrincipal) {
    // One or the other has a principal, but not both... not equal
    return false;
  }

  if (mPrincipal && !mPrincipal->Equals(aOther->mPrincipal)) {
    return false;
  }

  if (!nsURIHashKey::KeyEquals(
          static_cast<const nsURIHashKey*>(aOther)->GetKey())) {
    return false;
  }

  switch (mAs) {
    case ResourceType::SCRIPT:
      if (mScript.mScriptKind != aOther->mScript.mScriptKind) {
        return false;
      }
      break;
    case ResourceType::STYLE: {
      if (mStyle.mParsingMode != aOther->mStyle.mParsingMode) {
        return false;
      }
      break;
    }
    case ResourceType::IMAGE:
      // No more checks needed.  The image cache key consists of the document
      // (which we are scoped into), origin attributes (which we compare as part
      // of the principal check) and the URL.  imgLoader::ValidateEntry compares
      // CORS, referrer info and principal, which we do by default.
      break;
    case ResourceType::FONT:
      break;
    case ResourceType::FETCH:
      // No more checks needed.
      break;
    case ResourceType::NONE:
      break;
  }

  return true;
}

// static
PLDHashNumber PreloadHashKey::HashKey(KeyTypePointer aKey) {
  PLDHashNumber hash = nsURIHashKey::HashKey(aKey->mKey);

  // Enough to use the common attributes
  hash = AddToHash(hash, static_cast<uint32_t>(aKey->mAs));
  hash = AddToHash(hash, static_cast<uint32_t>(aKey->mCORSMode));

  return hash;
}

}  // namespace mozilla