Response.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_Response_h
  6. #define mozilla_dom_Response_h
  7. #include "nsWrapperCache.h"
  8. #include "nsISupportsImpl.h"
  9. #include "mozilla/dom/Fetch.h"
  10. #include "mozilla/dom/ResponseBinding.h"
  11. #include "InternalHeaders.h"
  12. #include "InternalResponse.h"
  13. namespace mozilla {
  14. namespace ipc {
  15. class PrincipalInfo;
  16. } // namespace ipc
  17. namespace dom {
  18. class Headers;
  19. class Response final : public nsISupports
  20. , public FetchBody<Response>
  21. , public nsWrapperCache
  22. {
  23. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  24. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Response)
  25. public:
  26. Response(nsIGlobalObject* aGlobal, InternalResponse* aInternalResponse, AbortSignal* aSignal);
  27. Response(const Response& aOther) = delete;
  28. JSObject*
  29. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
  30. {
  31. return ResponseBinding::Wrap(aCx, this, aGivenProto);
  32. }
  33. ResponseType
  34. Type() const
  35. {
  36. return mInternalResponse->Type();
  37. }
  38. void
  39. GetUrl(nsAString& aUrl) const
  40. {
  41. CopyUTF8toUTF16(mInternalResponse->GetURL(), aUrl);
  42. }
  43. bool
  44. Redirected() const
  45. {
  46. return mInternalResponse->IsRedirected();
  47. }
  48. uint16_t
  49. Status() const
  50. {
  51. return mInternalResponse->GetStatus();
  52. }
  53. bool
  54. Ok() const
  55. {
  56. return mInternalResponse->GetStatus() >= 200 &&
  57. mInternalResponse->GetStatus() <= 299;
  58. }
  59. void
  60. GetStatusText(nsCString& aStatusText) const
  61. {
  62. aStatusText = mInternalResponse->GetStatusText();
  63. }
  64. InternalHeaders*
  65. GetInternalHeaders() const
  66. {
  67. return mInternalResponse->Headers();
  68. }
  69. void
  70. InitChannelInfo(nsIChannel* aChannel)
  71. {
  72. mInternalResponse->InitChannelInfo(aChannel);
  73. }
  74. const ChannelInfo&
  75. GetChannelInfo() const
  76. {
  77. return mInternalResponse->GetChannelInfo();
  78. }
  79. const UniquePtr<mozilla::ipc::PrincipalInfo>&
  80. GetPrincipalInfo() const
  81. {
  82. return mInternalResponse->GetPrincipalInfo();
  83. }
  84. Headers* Headers_();
  85. void
  86. GetBody(nsIInputStream** aStream) { return mInternalResponse->GetBody(aStream); }
  87. static already_AddRefed<Response>
  88. Error(const GlobalObject& aGlobal);
  89. static already_AddRefed<Response>
  90. Redirect(const GlobalObject& aGlobal, const nsAString& aUrl, uint16_t aStatus, ErrorResult& aRv);
  91. static already_AddRefed<Response>
  92. Constructor(const GlobalObject& aGlobal,
  93. const Optional<Nullable<ArrayBufferOrArrayBufferViewOrBlobOrFormDataOrUSVStringOrURLSearchParams>>& aBody,
  94. const ResponseInit& aInit, ErrorResult& rv);
  95. nsIGlobalObject* GetParentObject() const
  96. {
  97. return mOwner;
  98. }
  99. already_AddRefed<Response>
  100. Clone(ErrorResult& aRv) const;
  101. already_AddRefed<Response>
  102. CloneUnfiltered(ErrorResult& aRv) const;
  103. void
  104. SetBody(nsIInputStream* aBody, int64_t aBodySize);
  105. already_AddRefed<InternalResponse>
  106. GetInternalResponse() const;
  107. AbortSignal*
  108. GetSignal() const override
  109. {
  110. return mSignal;
  111. }
  112. private:
  113. ~Response();
  114. nsCOMPtr<nsIGlobalObject> mOwner;
  115. RefPtr<InternalResponse> mInternalResponse;
  116. // Lazily created
  117. RefPtr<Headers> mHeaders;
  118. RefPtr<AbortSignal> mSignal;
  119. };
  120. } // namespace dom
  121. } // namespace mozilla
  122. #endif // mozilla_dom_Response_h