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"
11 #include "nsIAsyncOutputStream.h"
12 #include "nsIAsyncInputStream.h"
13 #include "nsIStreamListener.h"
17 class nsIInterfaceRequestor
;
21 class FetchPreloader
: public PreloaderBase
, public nsIStreamListener
{
23 NS_DECL_NSIREQUESTOBSERVER
24 NS_DECL_NSISTREAMLISTENER
27 nsresult
OpenChannel(PreloadHashKey
* aKey
, nsIURI
* aURI
,
28 const CORSMode aCORSMode
,
29 const dom::ReferrerPolicy
& aReferrerPolicy
,
30 dom::Document
* aDocument
);
33 nsresult
AsyncConsume(nsIStreamListener
* aListener
) override
;
34 static void PrioritizeAsPreload(nsIChannel
* aChannel
);
35 void PrioritizeAsPreload() override
;
38 explicit FetchPreloader(nsContentPolicyType aContentPolicyType
);
39 virtual ~FetchPreloader() = default;
41 // Create and setup the channel with necessary security properties. This is
42 // overridable by subclasses to allow different initial conditions.
43 virtual nsresult
CreateChannel(nsIChannel
** aChannel
, nsIURI
* aURI
,
44 const CORSMode aCORSMode
,
45 const dom::ReferrerPolicy
& aReferrerPolicy
,
46 dom::Document
* aDocument
,
47 nsILoadGroup
* aLoadGroup
,
48 nsIInterfaceRequestor
* aCallbacks
);
51 nsresult
CheckContentPolicy(nsIURI
* aURI
, dom::Document
* aDocument
);
53 class Cache final
: public nsIStreamListener
{
55 NS_DECL_NSIREQUESTOBSERVER
56 NS_DECL_NSISTREAMLISTENER
58 void AsyncConsume(nsIStreamListener
* aListener
);
59 void Consume(nsCOMPtr
<nsIStreamListener
> aListener
);
62 virtual ~Cache() = default;
64 struct StartRequest
{};
65 struct DataAvailable
{
72 typedef Variant
<StartRequest
, DataAvailable
, StopRequest
> Call
;
73 nsCOMPtr
<nsIRequest
> mRequest
;
74 nsCOMPtr
<nsIStreamListener
> mFinalListener
;
75 nsTArray
<Call
> mCalls
;
78 // The listener passed to AsyncConsume in case we haven't started getting the
79 // data from the channel yet.
80 nsCOMPtr
<nsIStreamListener
> mConsumeListener
;
82 // Returned by AsyncConsume when a failure. This remembers the result of
83 // opening the channel and prevents duplicate consumation.
84 nsresult mAsyncConsumeResult
= NS_ERROR_NOT_AVAILABLE
;
86 // The CP type to check against. Derived classes have to override call to CSP
87 // constructor of this class.
88 nsContentPolicyType mContentPolicyType
;
91 } // namespace mozilla