Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_dom_FetchDriver_h
8 : #define mozilla_dom_FetchDriver_h
9 :
10 : #include "nsIChannelEventSink.h"
11 : #include "nsIInterfaceRequestor.h"
12 : #include "nsIStreamListener.h"
13 : #include "nsIThreadRetargetableStreamListener.h"
14 : #include "mozilla/ConsoleReportCollector.h"
15 : #include "mozilla/dom/FetchSignal.h"
16 : #include "mozilla/dom/SRIMetadata.h"
17 : #include "mozilla/RefPtr.h"
18 :
19 : #include "mozilla/DebugOnly.h"
20 : #include "mozilla/net/ReferrerPolicy.h"
21 :
22 : class nsIConsoleReportCollector;
23 : class nsIDocument;
24 : class nsIEventTarget;
25 : class nsIOutputStream;
26 : class nsILoadGroup;
27 : class nsIPrincipal;
28 :
29 : namespace mozilla {
30 : namespace dom {
31 :
32 : class InternalRequest;
33 : class InternalResponse;
34 :
35 : /**
36 : * Provides callbacks to be called when response is available or on error.
37 : * Implemenations usually resolve or reject the promise returned from fetch().
38 : * The callbacks can be called synchronously or asynchronously from FetchDriver::Fetch.
39 : */
40 : class FetchDriverObserver
41 : {
42 : public:
43 2 : FetchDriverObserver() : mReporter(new ConsoleReportCollector())
44 2 : , mGotResponseAvailable(false)
45 1 : { }
46 :
47 6 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FetchDriverObserver);
48 1 : void OnResponseAvailable(InternalResponse* aResponse)
49 : {
50 1 : MOZ_ASSERT(!mGotResponseAvailable);
51 1 : mGotResponseAvailable = true;
52 1 : OnResponseAvailableInternal(aResponse);
53 1 : }
54 :
55 : enum EndReason
56 : {
57 : eAborted,
58 : eByNetworking,
59 : };
60 :
61 0 : virtual void OnResponseEnd(EndReason aReason)
62 0 : { };
63 :
64 0 : nsIConsoleReportCollector* GetReporter() const
65 : {
66 0 : return mReporter;
67 : }
68 :
69 : virtual void FlushConsoleReport() = 0;
70 :
71 : virtual void OnDataAvailable() = 0;
72 :
73 : protected:
74 1 : virtual ~FetchDriverObserver()
75 1 : { };
76 :
77 : virtual void OnResponseAvailableInternal(InternalResponse* aResponse) = 0;
78 :
79 : nsCOMPtr<nsIConsoleReportCollector> mReporter;
80 : private:
81 : bool mGotResponseAvailable;
82 : };
83 :
84 : class FetchDriver final : public nsIStreamListener,
85 : public nsIChannelEventSink,
86 : public nsIInterfaceRequestor,
87 : public nsIThreadRetargetableStreamListener,
88 : public FetchSignal::Follower
89 : {
90 : public:
91 : NS_DECL_ISUPPORTS
92 : NS_DECL_NSIREQUESTOBSERVER
93 : NS_DECL_NSISTREAMLISTENER
94 : NS_DECL_NSICHANNELEVENTSINK
95 : NS_DECL_NSIINTERFACEREQUESTOR
96 : NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
97 :
98 : FetchDriver(InternalRequest* aRequest,
99 : nsIPrincipal* aPrincipal,
100 : nsILoadGroup* aLoadGroup,
101 : nsIEventTarget* aMainThreadEventTarget,
102 : bool aIsTrackingFetch);
103 :
104 : nsresult Fetch(FetchSignal* aSignal,
105 : FetchDriverObserver* aObserver);
106 :
107 : void
108 : SetDocument(nsIDocument* aDocument);
109 :
110 : void
111 0 : SetWorkerScript(const nsACString& aWorkerScirpt)
112 : {
113 0 : MOZ_ASSERT(!aWorkerScirpt.IsEmpty());
114 0 : mWorkerScript = aWorkerScirpt;
115 0 : }
116 :
117 : // FetchSignal::Follower
118 :
119 : void
120 : Aborted() override;
121 :
122 : private:
123 : nsCOMPtr<nsIPrincipal> mPrincipal;
124 : nsCOMPtr<nsILoadGroup> mLoadGroup;
125 : RefPtr<InternalRequest> mRequest;
126 : RefPtr<InternalResponse> mResponse;
127 : nsCOMPtr<nsIOutputStream> mPipeOutputStream;
128 : RefPtr<FetchDriverObserver> mObserver;
129 : nsCOMPtr<nsIDocument> mDocument;
130 : nsCOMPtr<nsIChannel> mChannel;
131 : nsAutoPtr<SRICheckDataVerifier> mSRIDataVerifier;
132 : nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
133 : SRIMetadata mSRIMetadata;
134 : nsCString mWorkerScript;
135 : bool mIsTrackingFetch;
136 :
137 : #ifdef DEBUG
138 : bool mResponseAvailableCalled;
139 : bool mFetchCalled;
140 : #endif
141 :
142 : FetchDriver() = delete;
143 : FetchDriver(const FetchDriver&) = delete;
144 : FetchDriver& operator=(const FetchDriver&) = delete;
145 : ~FetchDriver();
146 :
147 : nsresult HttpFetch();
148 : // Returns the filtered response sent to the observer.
149 : already_AddRefed<InternalResponse>
150 : BeginAndGetFilteredResponse(InternalResponse* aResponse,
151 : bool aFoundOpaqueRedirect);
152 : // Utility since not all cases need to do any post processing of the filtered
153 : // response.
154 : void FailWithNetworkError();
155 :
156 : void SetRequestHeaders(nsIHttpChannel* aChannel) const;
157 : };
158 :
159 : } // namespace dom
160 : } // namespace mozilla
161 :
162 : #endif // mozilla_dom_FetchDriver_h
|