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_Response_h
8 #define mozilla_dom_Response_h
9 
10 #include "nsWrapperCache.h"
11 #include "nsISupportsImpl.h"
12 
13 #include "mozilla/dom/Fetch.h"
14 #include "mozilla/dom/ResponseBinding.h"
15 
16 #include "InternalHeaders.h"
17 #include "InternalResponse.h"
18 
19 namespace mozilla {
20 namespace ipc {
21 class PrincipalInfo;
22 }  // namespace ipc
23 
24 namespace dom {
25 
26 class Headers;
27 
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>)
32 
33  public:
34   Response(nsIGlobalObject* aGlobal, InternalResponse* aInternalResponse,
35            AbortSignalImpl* aSignalImpl);
36 
37   Response(const Response& aOther) = delete;
38 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)39   JSObject* WrapObject(JSContext* aCx,
40                        JS::Handle<JSObject*> aGivenProto) override {
41     return Response_Binding::Wrap(aCx, this, aGivenProto);
42   }
43 
Type()44   ResponseType Type() const { return mInternalResponse->Type(); }
GetUrl(nsAString & aUrl)45   void GetUrl(nsAString& aUrl) const {
46     CopyUTF8toUTF16(mInternalResponse->GetURL(), aUrl);
47   }
Redirected()48   bool Redirected() const { return mInternalResponse->IsRedirected(); }
Status()49   uint16_t Status() const { return mInternalResponse->GetStatus(); }
50 
Ok()51   bool Ok() const {
52     return mInternalResponse->GetStatus() >= 200 &&
53            mInternalResponse->GetStatus() <= 299;
54   }
55 
GetStatusText(nsCString & aStatusText)56   void GetStatusText(nsCString& aStatusText) const {
57     aStatusText = mInternalResponse->GetStatusText();
58   }
59 
GetInternalHeaders()60   InternalHeaders* GetInternalHeaders() const {
61     return mInternalResponse->Headers();
62   }
63 
InitChannelInfo(nsIChannel * aChannel)64   void InitChannelInfo(nsIChannel* aChannel) {
65     mInternalResponse->InitChannelInfo(aChannel);
66   }
67 
GetChannelInfo()68   const ChannelInfo& GetChannelInfo() const {
69     return mInternalResponse->GetChannelInfo();
70   }
71 
GetPrincipalInfo()72   const UniquePtr<mozilla::ipc::PrincipalInfo>& GetPrincipalInfo() const {
73     return mInternalResponse->GetPrincipalInfo();
74   }
75 
HasCacheInfoChannel()76   bool HasCacheInfoChannel() const {
77     return mInternalResponse->HasCacheInfoChannel();
78   }
79 
80   Headers* Headers_();
81 
82   void GetBody(nsIInputStream** aStream, int64_t* aBodyLength = nullptr) {
83     mInternalResponse->GetBody(aStream, aBodyLength);
84   }
85 
86   using FetchBody::GetBody;
87 
88   using FetchBody::BodyBlobURISpec;
89 
BodyBlobURISpec()90   const nsACString& BodyBlobURISpec() const {
91     return mInternalResponse->BodyBlobURISpec();
92   }
93 
94   using FetchBody::BodyLocalPath;
95 
BodyLocalPath()96   const nsAString& BodyLocalPath() const {
97     return mInternalResponse->BodyLocalPath();
98   }
99 
100   static already_AddRefed<Response> Error(const GlobalObject& aGlobal);
101 
102   static already_AddRefed<Response> Redirect(const GlobalObject& aGlobal,
103                                              const nsAString& aUrl,
104                                              uint16_t aStatus,
105                                              ErrorResult& aRv);
106 
107   static already_AddRefed<Response> Constructor(
108       const GlobalObject& aGlobal,
109       const Nullable<fetch::ResponseBodyInit>& aBody, const ResponseInit& aInit,
110       ErrorResult& rv);
111 
GetParentObject()112   nsIGlobalObject* GetParentObject() const { return mOwner; }
113 
114   already_AddRefed<Response> Clone(JSContext* aCx, ErrorResult& aRv);
115 
116   already_AddRefed<Response> CloneUnfiltered(JSContext* aCx, ErrorResult& aRv);
117 
118   void SetBody(nsIInputStream* aBody, int64_t aBodySize);
119 
120   already_AddRefed<InternalResponse> GetInternalResponse() const;
121 
GetSignalImpl()122   AbortSignalImpl* GetSignalImpl() const override { return mSignalImpl; }
123 
124  private:
125   ~Response();
126 
127   RefPtr<InternalResponse> mInternalResponse;
128   // Lazily created
129   RefPtr<Headers> mHeaders;
130   RefPtr<AbortSignalImpl> mSignalImpl;
131 };
132 
133 }  // namespace dom
134 }  // namespace mozilla
135 
136 #endif  // mozilla_dom_Response_h
137