Bug 1807268 - Fix verifyOpenAllInNewTabsOptionTest UI test r=ohorvath
[gecko.git] / uriloader / preload / PreloadHashKey.h
blob8db49c1b185c0239296d8a352ef10bfe0cfd3087
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 #ifndef PreloadHashKey_h__
6 #define PreloadHashKey_h__
8 #include "mozilla/CORSMode.h"
9 #include "mozilla/css/SheetParsingMode.h"
10 #include "js/loader/ScriptKind.h"
11 #include "nsURIHashKey.h"
13 class nsIPrincipal;
15 namespace JS::loader {
16 enum class ScriptKind;
19 namespace mozilla {
21 namespace css {
22 class SheetLoadData;
25 /**
26 * This key is used for coalescing and lookup of preloading or regular
27 * speculative loads. It consists of:
28 * - the resource type, which is the value of the 'as' attribute
29 * - the URI of the resource
30 * - set of attributes that is common to all resource types
31 * - resource-type-specific attributes that we use to distinguish loads that has
32 * to be treated separately, some of these attributes may remain at their
33 * default values
35 class PreloadHashKey : public nsURIHashKey {
36 public:
37 enum class ResourceType : uint8_t { NONE, SCRIPT, STYLE, IMAGE, FONT, FETCH };
39 using KeyType = const PreloadHashKey&;
40 using KeyTypePointer = const PreloadHashKey*;
42 PreloadHashKey() = default;
43 PreloadHashKey(const nsIURI* aKey, ResourceType aAs);
44 explicit PreloadHashKey(const PreloadHashKey* aKey);
45 PreloadHashKey(PreloadHashKey&& aToMove);
47 PreloadHashKey& operator=(const PreloadHashKey& aOther);
49 // Construct key for "script"
50 static PreloadHashKey CreateAsScript(nsIURI* aURI, CORSMode aCORSMode,
51 JS::loader::ScriptKind aScriptKind);
52 static PreloadHashKey CreateAsScript(nsIURI* aURI,
53 const nsAString& aCrossOrigin,
54 const nsAString& aType);
56 // Construct key for "style"
57 static PreloadHashKey CreateAsStyle(nsIURI* aURI, nsIPrincipal* aPrincipal,
58 CORSMode aCORSMode,
59 css::SheetParsingMode aParsingMode);
60 static PreloadHashKey CreateAsStyle(css::SheetLoadData&);
62 // Construct key for "image"
63 static PreloadHashKey CreateAsImage(nsIURI* aURI, nsIPrincipal* aPrincipal,
64 CORSMode aCORSMode);
66 // Construct key for "fetch"
67 static PreloadHashKey CreateAsFetch(nsIURI* aURI, CORSMode aCORSMode);
68 static PreloadHashKey CreateAsFetch(nsIURI* aURI,
69 const nsAString& aCrossOrigin);
71 // Construct key for "font"
72 static PreloadHashKey CreateAsFont(nsIURI* aURI, CORSMode aCORSMode);
74 KeyType GetKey() const { return *this; }
75 KeyTypePointer GetKeyPointer() const { return this; }
76 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
78 bool KeyEquals(KeyTypePointer aOther) const;
79 static PLDHashNumber HashKey(KeyTypePointer aKey);
80 ResourceType As() const { return mAs; }
82 #ifdef MOZILLA_INTERNAL_API
83 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
84 // Bug 1627752
85 return 0;
87 #endif
89 enum { ALLOW_MEMMOVE = true };
91 private:
92 // Attributes common to all resource types
93 ResourceType mAs = ResourceType::NONE;
95 CORSMode mCORSMode = CORS_NONE;
96 nsCOMPtr<nsIPrincipal> mPrincipal;
98 struct {
99 JS::loader::ScriptKind mScriptKind = JS::loader::ScriptKind::eClassic;
100 } mScript;
102 struct {
103 css::SheetParsingMode mParsingMode = css::eAuthorSheetFeatures;
104 } mStyle;
107 } // namespace mozilla
109 #endif