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"
11 #include "nsRefPtrHashtable.h"
12 #include "mozilla/PreloadHashKey.h"
22 class HTMLLinkElement
;
24 enum class ReferrerPolicy
: uint8_t;
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
{
37 explicit PreloadService(dom::Document
*);
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
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 void PreloadLinkHeader(nsIURI
* aURI
, const nsAString
& aURL
,
70 nsContentPolicyType aPolicyType
, const nsAString
& aAs
,
71 const nsAString
& aType
, const nsAString
& aIntegrity
,
72 const nsAString
& aSrcset
, const nsAString
& aSizes
,
73 const nsAString
& aCORS
,
74 const nsAString
& aReferrerPolicy
);
76 void PreloadScript(nsIURI
* aURI
, const nsAString
& aType
,
77 const nsAString
& aCharset
, const nsAString
& aCrossOrigin
,
78 const nsAString
& aReferrerPolicy
,
79 const nsAString
& aIntegrity
, bool aScriptFromHead
);
81 void PreloadImage(nsIURI
* aURI
, const nsAString
& aCrossOrigin
,
82 const nsAString
& aImageReferrerPolicy
, bool aIsImgSet
);
84 void PreloadFont(nsIURI
* aURI
, const nsAString
& aCrossOrigin
,
85 const nsAString
& aReferrerPolicy
);
87 void PreloadFetch(nsIURI
* aURI
, const nsAString
& aCrossOrigin
,
88 const nsAString
& aReferrerPolicy
);
90 static void NotifyNodeEvent(nsINode
* aNode
, bool aSuccess
);
93 dom::ReferrerPolicy
PreloadReferrerPolicy(const nsAString
& aReferrerPolicy
);
94 nsIURI
* BaseURIForPreload();
96 struct PreloadOrCoalesceResult
{
97 RefPtr
<PreloaderBase
> mPreloader
;
98 bool mAlreadyComplete
;
101 PreloadOrCoalesceResult
PreloadOrCoalesce(
102 nsIURI
* aURI
, const nsAString
& aURL
, nsContentPolicyType aPolicyType
,
103 const nsAString
& aAs
, const nsAString
& aType
, const nsAString
& aCharset
,
104 const nsAString
& aSrcset
, const nsAString
& aSizes
,
105 const nsAString
& aIntegrity
, const nsAString
& aCORS
,
106 const nsAString
& aReferrerPolicy
, bool aFromHeader
);
109 nsRefPtrHashtable
<PreloadHashKey
, PreloaderBase
> mPreloads
;
111 // Raw pointer only, we are intended to be a direct member of dom::Document
112 dom::Document
* mDocument
;
114 // Set by `nsHtml5TreeOpExecutor::SetSpeculationBase`.
115 nsCOMPtr
<nsIURI
> mSpeculationBaseURI
;
118 } // namespace mozilla