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/. */
7 #ifndef mozilla_dom_Response_h
8 #define mozilla_dom_Response_h
10 #include "nsWrapperCache.h"
11 #include "nsISupportsImpl.h"
13 #include "mozilla/dom/Fetch.h"
14 #include "mozilla/dom/ResponseBinding.h"
16 #include "InternalHeaders.h"
17 #include "InternalResponse.h"
28 class Response final
: public FetchBody
<Response
>, public nsWrapperCache
{
29 NS_DECL_ISUPPORTS_INHERITED
30 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(Response
,
34 Response(nsIGlobalObject
* aGlobal
,
35 SafeRefPtr
<InternalResponse
> aInternalResponse
,
36 AbortSignalImpl
* aSignalImpl
);
38 Response(const Response
& aOther
) = delete;
40 JSObject
* WrapObject(JSContext
* aCx
,
41 JS::Handle
<JSObject
*> aGivenProto
) override
{
42 return Response_Binding::Wrap(aCx
, this, aGivenProto
);
45 ResponseType
Type() const { return mInternalResponse
->Type(); }
46 void GetUrl(nsACString
& aUrl
) const { aUrl
= mInternalResponse
->GetURL(); }
47 bool Redirected() const { return mInternalResponse
->IsRedirected(); }
48 uint16_t Status() const { return mInternalResponse
->GetStatus(); }
51 return mInternalResponse
->GetStatus() >= 200 &&
52 mInternalResponse
->GetStatus() <= 299;
55 void GetStatusText(nsCString
& aStatusText
) const {
56 aStatusText
= mInternalResponse
->GetStatusText();
59 InternalHeaders
* GetInternalHeaders() const {
60 return mInternalResponse
->Headers();
63 void InitChannelInfo(nsIChannel
* aChannel
) {
64 mInternalResponse
->InitChannelInfo(aChannel
);
67 const ChannelInfo
& GetChannelInfo() const {
68 return mInternalResponse
->GetChannelInfo();
71 const UniquePtr
<mozilla::ipc::PrincipalInfo
>& GetPrincipalInfo() const {
72 return mInternalResponse
->GetPrincipalInfo();
75 bool HasCacheInfoChannel() const {
76 return mInternalResponse
->HasCacheInfoChannel();
81 void GetBody(nsIInputStream
** aStream
, int64_t* aBodyLength
= nullptr) {
82 mInternalResponse
->GetBody(aStream
, aBodyLength
);
85 using FetchBody::GetBody
;
87 using FetchBody::BodyBlobURISpec
;
89 const nsACString
& BodyBlobURISpec() const {
90 return mInternalResponse
->BodyBlobURISpec();
93 using FetchBody::BodyLocalPath
;
95 const nsAString
& BodyLocalPath() const {
96 return mInternalResponse
->BodyLocalPath();
99 static already_AddRefed
<Response
> Error(const GlobalObject
& aGlobal
);
101 static already_AddRefed
<Response
> Redirect(const GlobalObject
& aGlobal
,
102 const nsACString
& aUrl
,
106 static already_AddRefed
<Response
> CreateFromJson(const GlobalObject
&,
108 JS::Handle
<JS::Value
>,
112 static already_AddRefed
<Response
> Constructor(
113 const GlobalObject
& aGlobal
,
114 const Nullable
<fetch::ResponseBodyInit
>& aBody
, const ResponseInit
& aInit
,
117 nsIGlobalObject
* GetParentObject() const { return mOwner
; }
119 already_AddRefed
<Response
> Clone(JSContext
* aCx
, ErrorResult
& aRv
);
121 already_AddRefed
<Response
> CloneUnfiltered(JSContext
* aCx
, ErrorResult
& aRv
);
123 void SetBody(nsIInputStream
* aBody
, int64_t aBodySize
);
125 SafeRefPtr
<InternalResponse
> GetInternalResponse() const;
127 AbortSignalImpl
* GetSignalImpl() const override
{ return mSignalImpl
; }
128 AbortSignalImpl
* GetSignalImplToConsumeBody() const final
{
129 // XXX: BodyConsumer is supposed to work in terms of ReadableStream and
130 // should be affected by: https://fetch.spec.whatwg.org/#abort-fetch
132 // Step 6: If response’s body is not null and is readable, then error
133 // response’s body with error.
135 // But since it's written before streams work, it's currently depending on
136 // abort signal to be aborted.
137 // Please fix this when DOM ReadableStream is ready. (Bug 1730584)
142 static already_AddRefed
<Response
> CreateAndInitializeAResponse(
143 const GlobalObject
& aGlobal
,
144 const Nullable
<fetch::ResponseBodyInit
>& aBody
,
145 const nsACString
& aDefaultContentType
, const ResponseInit
& aInit
,
150 SafeRefPtr
<InternalResponse
> mInternalResponse
;
152 RefPtr
<Headers
> mHeaders
;
153 RefPtr
<AbortSignalImpl
> mSignalImpl
;
157 } // namespace mozilla
159 #endif // mozilla_dom_Response_h