no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / dom / fetch / Response.h
blob14c0e63bb2106d48cc0ba3e611da8570caefdfd6
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"
19 namespace mozilla {
20 namespace ipc {
21 class PrincipalInfo;
22 } // namespace ipc
24 namespace dom {
26 class Headers;
28 class Response final : public FetchBody<Response>, public nsWrapperCache {
29 NS_DECL_ISUPPORTS_INHERITED
30 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(Response,
31 FetchBody<Response>)
33 public:
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(); }
50 bool Ok() const {
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();
79 Headers* Headers_();
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,
103 uint16_t aStatus,
104 ErrorResult& aRv);
106 static already_AddRefed<Response> CreateFromJson(const GlobalObject&,
107 JSContext*,
108 JS::Handle<JS::Value>,
109 const ResponseInit&,
110 ErrorResult&);
112 static already_AddRefed<Response> Constructor(
113 const GlobalObject& aGlobal,
114 const Nullable<fetch::ResponseBodyInit>& aBody, const ResponseInit& aInit,
115 ErrorResult& rv);
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)
138 return mSignalImpl;
141 private:
142 static already_AddRefed<Response> CreateAndInitializeAResponse(
143 const GlobalObject& aGlobal,
144 const Nullable<fetch::ResponseBodyInit>& aBody,
145 const nsACString& aDefaultContentType, const ResponseInit& aInit,
146 ErrorResult& aRv);
148 ~Response();
150 SafeRefPtr<InternalResponse> mInternalResponse;
151 // Lazily created
152 RefPtr<Headers> mHeaders;
153 RefPtr<AbortSignalImpl> mSignalImpl;
156 } // namespace dom
157 } // namespace mozilla
159 #endif // mozilla_dom_Response_h