Bug 1855360 - Fix the skip-if syntax. a=bustage-fix
[gecko.git] / uriloader / preload / FetchPreloader.h
blob24c3babd8961a6091e12d9a4a7da4c0e32887546
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"
11 #include "nsCOMPtr.h"
12 #include "nsIAsyncOutputStream.h"
13 #include "nsIAsyncInputStream.h"
14 #include "nsIContentPolicy.h"
15 #include "nsIStreamListener.h"
17 class nsIChannel;
18 class nsILoadGroup;
19 class nsIInterfaceRequestor;
21 namespace mozilla {
23 namespace dom {
24 enum class ReferrerPolicy : uint8_t;
27 class FetchPreloader : public PreloaderBase, public nsIStreamListener {
28 NS_DECL_ISUPPORTS
29 NS_DECL_NSIREQUESTOBSERVER
30 NS_DECL_NSISTREAMLISTENER
32 FetchPreloader();
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);
41 // PreloaderBase
42 nsresult AsyncConsume(nsIStreamListener* aListener) override;
44 static void PrioritizeAsPreload(nsIChannel* aChannel);
46 protected:
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);
61 private:
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 {
69 NS_DECL_ISUPPORTS
70 NS_DECL_NSIREQUESTOBSERVER
71 NS_DECL_NSISTREAMLISTENER
73 void AsyncConsume(nsIStreamListener* aListener);
74 void Consume(nsCOMPtr<nsIStreamListener> aListener);
76 private:
77 virtual ~Cache() = default;
79 struct StartRequest {};
80 struct DataAvailable {
81 nsCString mData;
83 struct StopRequest {
84 nsresult mStatus;
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
108 #endif