Response.h (5148B)
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 "InternalHeaders.h" 11 #include "InternalResponse.h" 12 #include "mozilla/dom/Fetch.h" 13 #include "mozilla/dom/ResponseBinding.h" 14 #include "nsISupportsImpl.h" 15 #include "nsWrapperCache.h" 16 17 namespace mozilla { 18 namespace ipc { 19 class PrincipalInfo; 20 } // namespace ipc 21 22 namespace dom { 23 24 class Headers; 25 26 class Response final : public FetchBody<Response>, public nsWrapperCache { 27 NS_DECL_ISUPPORTS_INHERITED 28 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(Response, 29 FetchBody<Response>) 30 31 public: 32 Response(nsIGlobalObject* aGlobal, 33 SafeRefPtr<InternalResponse> aInternalResponse, 34 AbortSignalImpl* aSignalImpl); 35 36 Response(const Response& aOther) = delete; 37 38 JSObject* WrapObject(JSContext* aCx, 39 JS::Handle<JSObject*> aGivenProto) override { 40 return Response_Binding::Wrap(aCx, this, aGivenProto); 41 } 42 43 ResponseType Type() const { return mInternalResponse->Type(); } 44 void GetUrl(nsACString& aUrl) const { aUrl = mInternalResponse->GetURL(); } 45 bool Redirected() const { return mInternalResponse->IsRedirected(); } 46 uint16_t Status() const { return mInternalResponse->GetStatus(); } 47 48 bool Ok() const { 49 return mInternalResponse->GetStatus() >= 200 && 50 mInternalResponse->GetStatus() <= 299; 51 } 52 53 void GetStatusText(nsCString& aStatusText) const { 54 aStatusText = mInternalResponse->GetStatusText(); 55 } 56 57 InternalHeaders* GetInternalHeaders() const { 58 return mInternalResponse->Headers(); 59 } 60 61 void InitChannelInfo(nsIChannel* aChannel) { 62 mInternalResponse->InitChannelInfo(aChannel); 63 } 64 65 const ChannelInfo& GetChannelInfo() const { 66 return mInternalResponse->GetChannelInfo(); 67 } 68 69 const UniquePtr<mozilla::ipc::PrincipalInfo>& GetPrincipalInfo() const { 70 return mInternalResponse->GetPrincipalInfo(); 71 } 72 73 bool HasCacheInfoChannel() const { 74 return mInternalResponse->HasCacheInfoChannel(); 75 } 76 77 Headers* Headers_(); 78 79 void GetBody(nsIInputStream** aStream, int64_t* aBodyLength = nullptr) { 80 mInternalResponse->GetBody(aStream, aBodyLength); 81 } 82 83 using FetchBody::GetBody; 84 85 using FetchBody::BodyBlobURISpec; 86 87 const nsACString& BodyBlobURISpec() const { 88 return mInternalResponse->BodyBlobURISpec(); 89 } 90 91 using FetchBody::BodyLocalPath; 92 93 const nsAString& BodyLocalPath() const { 94 return mInternalResponse->BodyLocalPath(); 95 } 96 97 static already_AddRefed<Response> Error(const GlobalObject& aGlobal); 98 99 static already_AddRefed<Response> Redirect(const GlobalObject& aGlobal, 100 const nsACString& aUrl, 101 uint16_t aStatus, 102 ErrorResult& aRv); 103 104 static already_AddRefed<Response> CreateFromJson(const GlobalObject&, 105 JSContext*, 106 JS::Handle<JS::Value>, 107 const ResponseInit&, 108 ErrorResult&); 109 110 static already_AddRefed<Response> Constructor( 111 const GlobalObject& aGlobal, 112 const Nullable<fetch::ResponseBodyInit>& aBody, const ResponseInit& aInit, 113 ErrorResult& rv); 114 115 nsIGlobalObject* GetParentObject() const { return mOwner; } 116 117 already_AddRefed<Response> Clone(JSContext* aCx, ErrorResult& aRv); 118 119 already_AddRefed<Response> CloneUnfiltered(JSContext* aCx, ErrorResult& aRv); 120 121 void SetBody(nsIInputStream* aBody, int64_t aBodySize); 122 123 SafeRefPtr<InternalResponse> GetInternalResponse() const; 124 125 AbortSignalImpl* GetSignalImpl() const override { return mSignalImpl; } 126 AbortSignalImpl* GetSignalImplToConsumeBody() const final { 127 // XXX: BodyConsumer is supposed to work in terms of ReadableStream and 128 // should be affected by: https://fetch.spec.whatwg.org/#abort-fetch 129 // 130 // Step 6: If response’s body is not null and is readable, then error 131 // response’s body with error. 132 // 133 // But since it's written before streams work, it's currently depending on 134 // abort signal to be aborted. 135 // Please fix this when DOM ReadableStream is ready. (Bug 1730584) 136 return mSignalImpl; 137 } 138 139 private: 140 static already_AddRefed<Response> CreateAndInitializeAResponse( 141 const GlobalObject& aGlobal, 142 const Nullable<fetch::ResponseBodyInit>& aBody, 143 const nsACString& aDefaultContentType, const ResponseInit& aInit, 144 ErrorResult& aRv); 145 146 ~Response(); 147 148 SafeRefPtr<InternalResponse> mInternalResponse; 149 // Lazily created 150 RefPtr<Headers> mHeaders; 151 RefPtr<AbortSignalImpl> mSignalImpl; 152 }; 153 154 } // namespace dom 155 } // namespace mozilla 156 157 #endif // mozilla_dom_Response_h