Bug 1829125 - Align the PHC area to the jemalloc chunk size r=glandium
[gecko.git] / uriloader / preload / PreloadService.h
blobd785fd0ae546f9b9144a70f13c5c72f5d210a3e1
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 and speculative loads under one roof. This class
30 * is intended to be a member of dom::Document. Provides registration of
31 * speculative loads via a `key` which is defined to consist of the URL,
32 * resource type, and resource-specific attributes that are further
33 * distinguishing loads with otherwise same type and url.
35 class PreloadService {
36 public:
37 explicit PreloadService(dom::Document*);
38 ~PreloadService();
40 // Called by resource loaders to register a running resource load. This is
41 // called for a speculative load when it's started the first time, being it
42 // either regular speculative load or a preload.
44 // Returns false and does nothing if a preload is already registered under
45 // this key, true otherwise.
46 bool RegisterPreload(const PreloadHashKey& aKey, PreloaderBase* aPreload);
48 // Called when the load is about to be cancelled. Exact behavior is to be
49 // determined yet.
50 void DeregisterPreload(const PreloadHashKey& aKey);
52 // Called when the scope is to go away.
53 void ClearAllPreloads();
55 // True when there is a preload registered under the key.
56 bool PreloadExists(const PreloadHashKey& aKey);
58 // Returns an existing preload under the key or null, when there is none
59 // registered under the key.
60 already_AddRefed<PreloaderBase> LookupPreload(
61 const PreloadHashKey& aKey) const;
63 void SetSpeculationBase(nsIURI* aURI) { mSpeculationBaseURI = aURI; }
64 already_AddRefed<nsIURI> GetPreloadURI(const nsAString& aURL);
66 already_AddRefed<PreloaderBase> PreloadLinkElement(
67 dom::HTMLLinkElement* aLink, nsContentPolicyType aPolicyType);
69 // a non-zero aEarlyHintPreloaderId tells this service that a preload for this
70 // link was started by the EarlyHintPreloader and the preloaders should
71 // connect back by setting earlyHintPreloaderId in nsIChannelInternal before
72 // AsyncOpen.
73 void PreloadLinkHeader(nsIURI* aURI, const nsAString& aURL,
74 nsContentPolicyType aPolicyType, const nsAString& aAs,
75 const nsAString& aType, const nsAString& aIntegrity,
76 const nsAString& aSrcset, const nsAString& aSizes,
77 const nsAString& aCORS,
78 const nsAString& aReferrerPolicy,
79 uint64_t aEarlyHintPreloaderId);
81 void PreloadScript(nsIURI* aURI, const nsAString& aType,
82 const nsAString& aCharset, const nsAString& aCrossOrigin,
83 const nsAString& aReferrerPolicy,
84 const nsAString& aIntegrity, bool aScriptFromHead,
85 uint64_t aEarlyHintPreloaderId);
87 void PreloadImage(nsIURI* aURI, const nsAString& aCrossOrigin,
88 const nsAString& aImageReferrerPolicy, bool aIsImgSet,
89 uint64_t aEarlyHintPreloaderId);
91 void PreloadFont(nsIURI* aURI, const nsAString& aCrossOrigin,
92 const nsAString& aReferrerPolicy,
93 uint64_t aEarlyHintPreloaderId);
95 void PreloadFetch(nsIURI* aURI, const nsAString& aCrossOrigin,
96 const nsAString& aReferrerPolicy,
97 uint64_t aEarlyHintPreloaderId);
99 static void NotifyNodeEvent(nsINode* aNode, bool aSuccess);
101 void SetEarlyHintUsed() { mEarlyHintUsed = true; }
102 bool GetEarlyHintUsed() const { return mEarlyHintUsed; }
104 private:
105 dom::ReferrerPolicy PreloadReferrerPolicy(const nsAString& aReferrerPolicy);
106 nsIURI* BaseURIForPreload();
108 struct PreloadOrCoalesceResult {
109 RefPtr<PreloaderBase> mPreloader;
110 bool mAlreadyComplete;
113 PreloadOrCoalesceResult PreloadOrCoalesce(
114 nsIURI* aURI, const nsAString& aURL, nsContentPolicyType aPolicyType,
115 const nsAString& aAs, const nsAString& aType, const nsAString& aCharset,
116 const nsAString& aSrcset, const nsAString& aSizes,
117 const nsAString& aIntegrity, const nsAString& aCORS,
118 const nsAString& aReferrerPolicy, bool aFromHeader,
119 uint64_t aEarlyHintPreloaderId);
121 private:
122 nsRefPtrHashtable<PreloadHashKey, PreloaderBase> mPreloads;
124 // Raw pointer only, we are intended to be a direct member of dom::Document
125 dom::Document* mDocument;
127 // Set by `nsHtml5TreeOpExecutor::SetSpeculationBase`.
128 nsCOMPtr<nsIURI> mSpeculationBaseURI;
130 bool mEarlyHintUsed = false;
133 } // namespace mozilla
135 #endif