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 FetchPreloader_h_
7 #define FetchPreloader_h_
9 #include "mozilla/PreloaderBase.h"
10 #include "mozilla/Variant.h"
12 #include "nsIAsyncOutputStream.h"
13 #include "nsIAsyncInputStream.h"
14 #include "nsIContentPolicy.h"
15 #include "nsIStreamListener.h"
19 class nsIInterfaceRequestor
;
24 enum class ReferrerPolicy
: uint8_t;
27 class FetchPreloader
: public PreloaderBase
, public nsIStreamListener
{
29 NS_DECL_NSIREQUESTOBSERVER
30 NS_DECL_NSISTREAMLISTENER
34 // @param aSupportsPriorityValue see <nsISupportsPriority.idl>.
35 nsresult
OpenChannel(const PreloadHashKey
& aKey
, nsIURI
* aURI
,
36 const CORSMode aCORSMode
,
37 const dom::ReferrerPolicy
& aReferrerPolicy
,
38 dom::Document
* aDocument
, uint64_t aEarlyHintPreloaderId
,
39 int32_t aSupportsPriorityValue
);
42 nsresult
AsyncConsume(nsIStreamListener
* aListener
) override
;
44 static void PrioritizeAsPreload(nsIChannel
* aChannel
);
47 explicit FetchPreloader(nsContentPolicyType aContentPolicyType
);
48 virtual ~FetchPreloader() = default;
50 // Create and setup the channel with necessary security properties and
51 // the nsISupportsPriority value. This is overridable by
52 // subclasses to allow different initial conditions.
54 // @param aSupportsPriorityValue see <nsISupportsPriority.idl>.
55 virtual nsresult
CreateChannel(
56 nsIChannel
** aChannel
, nsIURI
* aURI
, const CORSMode aCORSMode
,
57 const dom::ReferrerPolicy
& aReferrerPolicy
, dom::Document
* aDocument
,
58 nsILoadGroup
* aLoadGroup
, nsIInterfaceRequestor
* aCallbacks
,
59 uint64_t aEarlyHintPreloaderId
, int32_t aSupportsPriorityValue
);
62 // @param aSupportsPriorityValue see <nsISupportsPriority.idl>.
63 static void AdjustPriority(nsIChannel
* aChannel
,
64 int32_t aSupportsPriorityValue
);
66 nsresult
CheckContentPolicy(nsIURI
* aURI
, dom::Document
* aDocument
);
68 class Cache final
: public nsIStreamListener
{
70 NS_DECL_NSIREQUESTOBSERVER
71 NS_DECL_NSISTREAMLISTENER
73 void AsyncConsume(nsIStreamListener
* aListener
);
74 void Consume(nsCOMPtr
<nsIStreamListener
> aListener
);
77 virtual ~Cache() = default;
79 struct StartRequest
{};
80 struct DataAvailable
{
87 typedef Variant
<StartRequest
, DataAvailable
, StopRequest
> Call
;
88 nsCOMPtr
<nsIRequest
> mRequest
;
89 nsCOMPtr
<nsIStreamListener
> mFinalListener
;
90 nsTArray
<Call
> mCalls
;
93 // The listener passed to AsyncConsume in case we haven't started getting the
94 // data from the channel yet.
95 nsCOMPtr
<nsIStreamListener
> mConsumeListener
;
97 // Returned by AsyncConsume when a failure. This remembers the result of
98 // opening the channel and prevents duplicate consumation.
99 nsresult mAsyncConsumeResult
= NS_ERROR_NOT_AVAILABLE
;
101 // The CP type to check against. Derived classes have to override call to CSP
102 // constructor of this class.
103 nsContentPolicyType mContentPolicyType
;
106 } // namespace mozilla