Bug 1807268 - Fix verifyOpenAllInNewTabsOptionTest UI test r=ohorvath
[gecko.git] / uriloader / preload / PreloadService.h
blob79f5e7b6d137715012f6e085d8adacaac8f0f02d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef PreloadService_h__
7 #define PreloadService_h__
9 #include "nsIContentPolicy.h"
10 #include "nsIURI.h"
11 #include "nsRefPtrHashtable.h"
12 #include "mozilla/PreloadHashKey.h"
14 class nsINode;
16 namespace mozilla {
18 class PreloaderBase;
20 namespace dom {
22 class HTMLLinkElement;
23 class Document;
24 enum class ReferrerPolicy : uint8_t;
26 } // namespace dom
28 /**
29 * Intended to scope preloads
30 * (https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload)
31 * and speculative loads initiated by the parser under one roof. This class
32 * is intended to be a member of dom::Document. Provides registration of
33 * speculative loads via a `key` which is defined to consist of the URL,
34 * resource type, and resource-specific attributes that are further
35 * distinguishing loads with otherwise same type and url.
37 class PreloadService {
38 public:
39 explicit PreloadService(dom::Document*);
40 ~PreloadService();
42 // Called by resource loaders to register a running resource load. This is
43 // called for a speculative load when it's started the first time, being it
44 // either regular speculative load or a preload.
46 // Returns false and does nothing if a preload is already registered under
47 // this key, true otherwise.
48 bool RegisterPreload(const PreloadHashKey& aKey, PreloaderBase* aPreload);
50 // Called when the load is about to be cancelled. Exact behavior is to be
51 // determined yet.
52 void DeregisterPreload(const PreloadHashKey& aKey);
54 // Called when the scope is to go away.
55 void ClearAllPreloads();
57 // True when there is a preload registered under the key.
58 bool PreloadExists(const PreloadHashKey& aKey);
60 // Returns an existing preload under the key or null, when there is none
61 // registered under the key.
62 already_AddRefed<PreloaderBase> LookupPreload(
63 const PreloadHashKey& aKey) const;
65 void SetSpeculationBase(nsIURI* aURI) { mSpeculationBaseURI = aURI; }
66 already_AddRefed<nsIURI> GetPreloadURI(const nsAString& aURL);
68 already_AddRefed<PreloaderBase> PreloadLinkElement(
69 dom::HTMLLinkElement* aLink, nsContentPolicyType aPolicyType);
71 // a non-zero aEarlyHintPreloaderId tells this service that a preload for this
72 // link was started by the EarlyHintPreloader and the preloaders should
73 // connect back by setting earlyHintPreloaderId in nsIChannelInternal before
74 // AsyncOpen.
75 void PreloadLinkHeader(nsIURI* aURI, const nsAString& aURL,
76 nsContentPolicyType aPolicyType, const nsAString& aAs,
77 const nsAString& aType, const nsAString& aNonce,
78 const nsAString& aIntegrity, const nsAString& aSrcset,
79 const nsAString& aSizes, const nsAString& aCORS,
80 const nsAString& aReferrerPolicy,
81 uint64_t aEarlyHintPreloaderId,
82 const nsAString& aFetchPriority);
84 void PreloadScript(nsIURI* aURI, const nsAString& aType,
85 const nsAString& aCharset, const nsAString& aCrossOrigin,
86 const nsAString& aReferrerPolicy, const nsAString& aNonce,
87 const nsAString& aFetchPriority,
88 const nsAString& aIntegrity, bool aScriptFromHead,
89 uint64_t aEarlyHintPreloaderId);
91 void PreloadImage(nsIURI* aURI, const nsAString& aCrossOrigin,
92 const nsAString& aImageReferrerPolicy, bool aIsImgSet,
93 uint64_t aEarlyHintPreloaderId,
94 const nsAString& aFetchPriority);
96 void PreloadFont(nsIURI* aURI, const nsAString& aCrossOrigin,
97 const nsAString& aReferrerPolicy,
98 uint64_t aEarlyHintPreloaderId,
99 const nsAString& aFetchPriority);
101 void PreloadFetch(nsIURI* aURI, const nsAString& aCrossOrigin,
102 const nsAString& aReferrerPolicy,
103 uint64_t aEarlyHintPreloaderId,
104 const nsAString& aFetchPriority);
106 static void NotifyNodeEvent(nsINode* aNode, bool aSuccess);
108 void SetEarlyHintUsed() { mEarlyHintUsed = true; }
109 bool GetEarlyHintUsed() const { return mEarlyHintUsed; }
111 private:
112 dom::ReferrerPolicy PreloadReferrerPolicy(const nsAString& aReferrerPolicy);
113 nsIURI* BaseURIForPreload();
115 struct PreloadOrCoalesceResult {
116 RefPtr<PreloaderBase> mPreloader;
117 bool mAlreadyComplete;
120 PreloadOrCoalesceResult PreloadOrCoalesce(
121 nsIURI* aURI, const nsAString& aURL, nsContentPolicyType aPolicyType,
122 const nsAString& aAs, const nsAString& aType, const nsAString& aCharset,
123 const nsAString& aSrcset, const nsAString& aSizes,
124 const nsAString& aNonce, const nsAString& aIntegrity,
125 const nsAString& aCORS, const nsAString& aReferrerPolicy,
126 const nsAString& aFetchPriority, bool aFromHeader,
127 uint64_t aEarlyHintPreloaderId);
129 private:
130 nsRefPtrHashtable<PreloadHashKey, PreloaderBase> mPreloads;
132 // Raw pointer only, we are intended to be a direct member of dom::Document
133 dom::Document* mDocument;
135 // Set by `nsHtml5TreeOpExecutor::SetSpeculationBase`.
136 nsCOMPtr<nsIURI> mSpeculationBaseURI;
138 bool mEarlyHintUsed = false;
141 } // namespace mozilla
143 #endif