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
33 nsresult
OpenChannel(const PreloadHashKey
& aKey
, nsIURI
* aURI
,
34 const CORSMode aCORSMode
,
35 const dom::ReferrerPolicy
& aReferrerPolicy
,
36 dom::Document
* aDocument
,
37 uint64_t aEarlyHintPreloaderId
);
40 nsresult
AsyncConsume(nsIStreamListener
* aListener
) override
;
41 static void PrioritizeAsPreload(nsIChannel
* aChannel
);
42 void PrioritizeAsPreload() override
;
45 explicit FetchPreloader(nsContentPolicyType aContentPolicyType
);
46 virtual ~FetchPreloader() = default;
48 // Create and setup the channel with necessary security properties. This is
49 // overridable by subclasses to allow different initial conditions.
50 virtual nsresult
CreateChannel(nsIChannel
** aChannel
, nsIURI
* aURI
,
51 const CORSMode aCORSMode
,
52 const dom::ReferrerPolicy
& aReferrerPolicy
,
53 dom::Document
* aDocument
,
54 nsILoadGroup
* aLoadGroup
,
55 nsIInterfaceRequestor
* aCallbacks
,
56 uint64_t aEarlyHintPreloaderId
);
59 nsresult
CheckContentPolicy(nsIURI
* aURI
, dom::Document
* aDocument
);
61 class Cache final
: public nsIStreamListener
{
63 NS_DECL_NSIREQUESTOBSERVER
64 NS_DECL_NSISTREAMLISTENER
66 void AsyncConsume(nsIStreamListener
* aListener
);
67 void Consume(nsCOMPtr
<nsIStreamListener
> aListener
);
70 virtual ~Cache() = default;
72 struct StartRequest
{};
73 struct DataAvailable
{
80 typedef Variant
<StartRequest
, DataAvailable
, StopRequest
> Call
;
81 nsCOMPtr
<nsIRequest
> mRequest
;
82 nsCOMPtr
<nsIStreamListener
> mFinalListener
;
83 nsTArray
<Call
> mCalls
;
86 // The listener passed to AsyncConsume in case we haven't started getting the
87 // data from the channel yet.
88 nsCOMPtr
<nsIStreamListener
> mConsumeListener
;
90 // Returned by AsyncConsume when a failure. This remembers the result of
91 // opening the channel and prevents duplicate consumation.
92 nsresult mAsyncConsumeResult
= NS_ERROR_NOT_AVAILABLE
;
94 // The CP type to check against. Derived classes have to override call to CSP
95 // constructor of this class.
96 nsContentPolicyType mContentPolicyType
;
99 } // namespace mozilla