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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_XMLHttpRequestWorker_h
8 #define mozilla_dom_XMLHttpRequestWorker_h
10 #include "XMLHttpRequest.h"
11 #include "XMLHttpRequestString.h"
12 #include "mozilla/WeakPtr.h"
13 #include "mozilla/dom/BodyExtractor.h"
14 #include "mozilla/dom/TypedArray.h"
16 // XXX Avoid including this here by moving function bodies to the cpp file
17 #include "mozilla/dom/BlobImpl.h"
19 namespace mozilla::dom
{
24 class StrongWorkerRef
;
27 class XMLHttpRequestWorker final
: public SupportsWeakPtr
,
28 public XMLHttpRequest
{
30 // This defines the xhr.response value.
32 nsresult mResponseResult
;
34 // responseType is empty or text.
35 XMLHttpRequestStringSnapshot mResponseText
;
37 // responseType is blob
38 RefPtr
<BlobImpl
> mResponseBlobImpl
;
40 // responseType is arrayBuffer;
41 RefPtr
<ArrayBufferBuilder
> mResponseArrayBufferBuilder
;
43 // responseType is json
44 nsString mResponseJSON
;
46 ResponseData() : mResponseResult(NS_OK
) {}
50 nsString mResponseURL
;
52 nsCString mStatusText
;
55 nsresult mStatusResult
;
58 : mStatus(0), mReadyState(0), mFlagSend(false), mStatusResult(NS_OK
) {}
62 RefPtr
<XMLHttpRequestUpload
> mUpload
;
63 WorkerPrivate
* mWorkerPrivate
;
64 RefPtr
<StrongWorkerRef
> mWorkerRef
;
65 RefPtr
<XMLHttpRequestWorker
> mPinnedSelfRef
;
68 XMLHttpRequestResponseType mResponseType
;
70 UniquePtr
<StateData
> mStateData
;
72 UniquePtr
<ResponseData
> mResponseData
;
73 RefPtr
<Blob
> mResponseBlob
;
74 JS::Heap
<JSObject
*> mResponseArrayBufferValue
;
75 JS::Heap
<JS::Value
> mResponseJSONValue
;
79 bool mBackgroundRequest
;
80 bool mWithCredentials
;
87 nsString mMimeTypeOverride
;
90 NS_DECL_ISUPPORTS_INHERITED
91 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(XMLHttpRequestWorker
,
94 static already_AddRefed
<XMLHttpRequest
> Construct(
95 const GlobalObject
& aGlobal
, const MozXMLHttpRequestParameters
& aParams
,
100 virtual uint16_t ReadyState() const override
{
101 return mStateData
->mReadyState
;
104 virtual void Open(const nsACString
& aMethod
, const nsAString
& aUrl
,
105 ErrorResult
& aRv
) override
{
106 Open(aMethod
, aUrl
, true, Optional
<nsAString
>(), Optional
<nsAString
>(),
110 virtual void Open(const nsACString
& aMethod
, const nsAString
& aUrl
,
111 bool aAsync
, const nsAString
& aUsername
,
112 const nsAString
& aPassword
, ErrorResult
& aRv
) override
{
113 Optional
<nsAString
> username
;
114 username
= &aUsername
;
115 Optional
<nsAString
> password
;
116 password
= &aPassword
;
117 Open(aMethod
, aUrl
, aAsync
, username
, password
, aRv
);
120 void Open(const nsACString
& aMethod
, const nsAString
& aUrl
, bool aAsync
,
121 const Optional
<nsAString
>& aUser
,
122 const Optional
<nsAString
>& aPassword
, ErrorResult
& aRv
);
124 virtual void SetRequestHeader(const nsACString
& aHeader
,
125 const nsACString
& aValue
,
126 ErrorResult
& aRv
) override
;
128 virtual uint32_t Timeout() const override
{ return mTimeout
; }
130 virtual void SetTimeout(uint32_t aTimeout
, ErrorResult
& aRv
) override
;
132 virtual bool WithCredentials() const override
{ return mWithCredentials
; }
134 virtual void SetWithCredentials(bool aWithCredentials
,
135 ErrorResult
& aRv
) override
;
137 virtual bool MozBackgroundRequest() const override
{
138 return mBackgroundRequest
;
141 virtual void SetMozBackgroundRequest(bool aBackgroundRequest
,
142 ErrorResult
& aRv
) override
;
144 virtual nsIChannel
* GetChannel() const override
{
145 MOZ_CRASH("This method cannot be called on workers.");
148 virtual XMLHttpRequestUpload
* GetUpload(ErrorResult
& aRv
) override
;
152 DocumentOrBlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrUSVString
>&
154 ErrorResult
& aRv
) override
;
156 virtual void SendInputStream(nsIInputStream
* aInputStream
,
157 ErrorResult
& aRv
) override
{
158 MOZ_CRASH("nsIInputStream is not a valid argument for XHR in workers.");
161 virtual void Abort(ErrorResult
& aRv
) override
;
163 virtual void GetResponseURL(nsAString
& aUrl
) override
{
164 aUrl
= mStateData
->mResponseURL
;
167 uint32_t GetStatus(ErrorResult
& aRv
) override
{
168 aRv
= mStateData
->mStatusResult
;
169 return mStateData
->mStatus
;
172 virtual void GetStatusText(nsACString
& aStatusText
,
173 ErrorResult
& aRv
) override
{
174 aStatusText
= mStateData
->mStatusText
;
177 virtual void GetResponseHeader(const nsACString
& aHeader
,
178 nsACString
& aResponseHeader
,
179 ErrorResult
& aRv
) override
;
181 virtual void GetAllResponseHeaders(nsACString
& aResponseHeaders
,
182 ErrorResult
& aRv
) override
;
184 virtual void OverrideMimeType(const nsAString
& aMimeType
,
185 ErrorResult
& aRv
) override
;
187 virtual XMLHttpRequestResponseType
ResponseType() const override
{
188 return mResponseType
;
191 virtual void SetResponseType(XMLHttpRequestResponseType aResponseType
,
192 ErrorResult
& aRv
) override
;
194 virtual void GetResponse(JSContext
* /* unused */,
195 JS::MutableHandle
<JS::Value
> aResponse
,
196 ErrorResult
& aRv
) override
;
198 virtual void GetResponseText(DOMString
& aResponseText
,
199 ErrorResult
& aRv
) override
;
201 virtual Document
* GetResponseXML(ErrorResult
& aRv
) override
{
202 MOZ_CRASH("This method should not be called.");
205 virtual void GetInterface(JSContext
* aCx
, JS::Handle
<JS::Value
> aIID
,
206 JS::MutableHandle
<JS::Value
> aRetval
,
207 ErrorResult
& aRv
) override
{
208 aRv
.Throw(NS_ERROR_FAILURE
);
211 virtual void SetOriginAttributes(
212 const mozilla::dom::OriginAttributesDictionary
& aAttrs
) override
{
213 MOZ_CRASH("This method cannot be called on workers.");
216 XMLHttpRequestUpload
* GetUploadObjectNoCreate() const { return mUpload
; }
218 void UpdateState(UniquePtr
<StateData
>&& aStateData
,
219 UniquePtr
<ResponseData
>&& aResponseData
);
221 virtual uint16_t ErrorCode() const override
{
225 virtual bool MozAnon() const override
{ return mMozAnon
; }
227 virtual bool MozSystem() const override
{ return mMozSystem
; }
229 bool SendInProgress() const { return !!mWorkerRef
; }
232 XMLHttpRequestWorker(WorkerPrivate
* aWorkerPrivate
,
233 nsIGlobalObject
* aGlobalObject
);
234 ~XMLHttpRequestWorker();
236 enum ReleaseType
{ Default
, XHRIsGoingAway
, WorkerIsGoingAway
};
238 void ReleaseProxy(ReleaseType aType
= Default
);
240 void MaybePin(ErrorResult
& aRv
);
242 void MaybeDispatchPrematureAbortEvents(ErrorResult
& aRv
);
244 void FireEvent(EventTarget
* aTarget
, const EventType
& aEventType
,
245 bool aUploadTarget
, ErrorResult
& aRv
);
247 void Send(JSContext
* aCx
, JS::Handle
<JSObject
*> aBody
, ErrorResult
& aRv
);
249 void SendInternal(const BodyExtractorBase
* aBody
, ErrorResult
& aRv
);
251 void ResetResponseData();
254 } // namespace mozilla::dom
256 #endif // mozilla_dom_workers_xmlhttprequest_h__