Bug 1641886 [wpt PR 23851] - Support interpolating contain-intrinsic-size, a=testonly
[gecko.git] / uriloader / preload / FetchPreloader.h
blob95c5a9f5fd944173c4079c64d9611746552ab319
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 "nsCOMPtr.h"
11 #include "nsIAsyncOutputStream.h"
12 #include "nsIAsyncInputStream.h"
13 #include "nsIStreamListener.h"
15 class nsIChannel;
16 class nsILoadGroup;
17 class nsIInterfaceRequestor;
19 namespace mozilla {
21 class FetchPreloader : public PreloaderBase, public nsIStreamListener {
22 NS_DECL_ISUPPORTS
23 NS_DECL_NSIREQUESTOBSERVER
24 NS_DECL_NSISTREAMLISTENER
26 FetchPreloader();
27 nsresult OpenChannel(PreloadHashKey* aKey, nsIURI* aURI,
28 const CORSMode aCORSMode,
29 const dom::ReferrerPolicy& aReferrerPolicy,
30 dom::Document* aDocument);
32 // PreloaderBase
33 nsresult AsyncConsume(nsIStreamListener* aListener) override;
34 static void PrioritizeAsPreload(nsIChannel* aChannel);
35 void PrioritizeAsPreload() override;
37 protected:
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);
50 private:
51 nsresult CheckContentPolicy(nsIURI* aURI, dom::Document* aDocument);
53 class Cache final : public nsIStreamListener {
54 NS_DECL_ISUPPORTS
55 NS_DECL_NSIREQUESTOBSERVER
56 NS_DECL_NSISTREAMLISTENER
58 void AsyncConsume(nsIStreamListener* aListener);
59 void Consume(nsCOMPtr<nsIStreamListener> aListener);
61 private:
62 virtual ~Cache() = default;
64 struct StartRequest {};
65 struct DataAvailable {
66 nsCString mData;
68 struct StopRequest {
69 nsresult mStatus;
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
93 #endif