Bug 1787199 [wpt PR 35620] - Add tests for `VisibilityStateEntry`, a=testonly
[gecko.git] / dom / xhr / XMLHttpRequestWorker.h
blob1f7e7351a254ba3937bb65fbb81ba773f42ed26d
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/dom/BodyExtractor.h"
13 #include "mozilla/dom/TypedArray.h"
15 // XXX Avoid including this here by moving function bodies to the cpp file
16 #include "mozilla/dom/BlobImpl.h"
18 namespace mozilla::dom {
20 class Proxy;
21 class DOMString;
22 class SendRunnable;
23 class StrongWorkerRef;
24 class WorkerPrivate;
26 class XMLHttpRequestWorker final : public XMLHttpRequest {
27 public:
28 // This defines the xhr.response value.
29 struct ResponseData {
30 nsresult mResponseResult;
32 // responseType is empty or text.
33 XMLHttpRequestStringSnapshot mResponseText;
35 // responseType is blob
36 RefPtr<BlobImpl> mResponseBlobImpl;
38 // responseType is arrayBuffer;
39 RefPtr<ArrayBufferBuilder> mResponseArrayBufferBuilder;
41 // responseType is json
42 nsString mResponseJSON;
44 ResponseData() : mResponseResult(NS_OK) {}
47 struct StateData {
48 nsString mResponseURL;
49 uint32_t mStatus;
50 nsCString mStatusText;
51 uint16_t mReadyState;
52 bool mFlagSend;
53 nsresult mStatusResult;
55 StateData()
56 : mStatus(0), mReadyState(0), mFlagSend(false), mStatusResult(NS_OK) {}
59 private:
60 RefPtr<XMLHttpRequestUpload> mUpload;
61 WorkerPrivate* mWorkerPrivate;
62 RefPtr<StrongWorkerRef> mWorkerRef;
63 RefPtr<Proxy> mProxy;
65 XMLHttpRequestResponseType mResponseType;
67 UniquePtr<StateData> mStateData;
69 UniquePtr<ResponseData> mResponseData;
70 RefPtr<Blob> mResponseBlob;
71 JS::Heap<JSObject*> mResponseArrayBufferValue;
72 JS::Heap<JS::Value> mResponseJSONValue;
74 uint32_t mTimeout;
76 bool mBackgroundRequest;
77 bool mWithCredentials;
78 bool mCanceled;
79 bool mFlagSendActive;
81 bool mMozAnon;
82 bool mMozSystem;
84 nsString mMimeTypeOverride;
86 public:
87 NS_DECL_ISUPPORTS_INHERITED
88 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(XMLHttpRequestWorker,
89 XMLHttpRequest)
91 static already_AddRefed<XMLHttpRequest> Construct(
92 const GlobalObject& aGlobal, const MozXMLHttpRequestParameters& aParams,
93 ErrorResult& aRv);
95 void Unpin();
97 virtual uint16_t ReadyState() const override {
98 return mStateData->mReadyState;
101 virtual void Open(const nsACString& aMethod, const nsAString& aUrl,
102 ErrorResult& aRv) override {
103 Open(aMethod, aUrl, true, Optional<nsAString>(), Optional<nsAString>(),
104 aRv);
107 virtual void Open(const nsACString& aMethod, const nsAString& aUrl,
108 bool aAsync, const nsAString& aUsername,
109 const nsAString& aPassword, ErrorResult& aRv) override {
110 Optional<nsAString> username;
111 username = &aUsername;
112 Optional<nsAString> password;
113 password = &aPassword;
114 Open(aMethod, aUrl, aAsync, username, password, aRv);
117 void Open(const nsACString& aMethod, const nsAString& aUrl, bool aAsync,
118 const Optional<nsAString>& aUser,
119 const Optional<nsAString>& aPassword, ErrorResult& aRv);
121 virtual void SetRequestHeader(const nsACString& aHeader,
122 const nsACString& aValue,
123 ErrorResult& aRv) override;
125 virtual uint32_t Timeout() const override { return mTimeout; }
127 virtual void SetTimeout(uint32_t aTimeout, ErrorResult& aRv) override;
129 virtual bool WithCredentials() const override { return mWithCredentials; }
131 virtual void SetWithCredentials(bool aWithCredentials,
132 ErrorResult& aRv) override;
134 virtual bool MozBackgroundRequest() const override {
135 return mBackgroundRequest;
138 virtual void SetMozBackgroundRequest(bool aBackgroundRequest,
139 ErrorResult& aRv) override;
141 virtual nsIChannel* GetChannel() const override {
142 MOZ_CRASH("This method cannot be called on workers.");
145 virtual XMLHttpRequestUpload* GetUpload(ErrorResult& aRv) override;
147 virtual void Send(
148 const Nullable<
149 DocumentOrBlobOrArrayBufferViewOrArrayBufferOrFormDataOrURLSearchParamsOrUSVString>&
150 aData,
151 ErrorResult& aRv) override;
153 virtual void SendInputStream(nsIInputStream* aInputStream,
154 ErrorResult& aRv) override {
155 MOZ_CRASH("nsIInputStream is not a valid argument for XHR in workers.");
158 virtual void Abort(ErrorResult& aRv) override;
160 virtual void GetResponseURL(nsAString& aUrl) override {
161 aUrl = mStateData->mResponseURL;
164 uint32_t GetStatus(ErrorResult& aRv) override {
165 aRv = mStateData->mStatusResult;
166 return mStateData->mStatus;
169 virtual void GetStatusText(nsACString& aStatusText,
170 ErrorResult& aRv) override {
171 aStatusText = mStateData->mStatusText;
174 virtual void GetResponseHeader(const nsACString& aHeader,
175 nsACString& aResponseHeader,
176 ErrorResult& aRv) override;
178 virtual void GetAllResponseHeaders(nsACString& aResponseHeaders,
179 ErrorResult& aRv) override;
181 virtual void OverrideMimeType(const nsAString& aMimeType,
182 ErrorResult& aRv) override;
184 virtual XMLHttpRequestResponseType ResponseType() const override {
185 return mResponseType;
188 virtual void SetResponseType(XMLHttpRequestResponseType aResponseType,
189 ErrorResult& aRv) override;
191 virtual void GetResponse(JSContext* /* unused */,
192 JS::MutableHandle<JS::Value> aResponse,
193 ErrorResult& aRv) override;
195 virtual void GetResponseText(DOMString& aResponseText,
196 ErrorResult& aRv) override;
198 virtual Document* GetResponseXML(ErrorResult& aRv) override {
199 MOZ_CRASH("This method should not be called.");
202 virtual void GetInterface(JSContext* aCx, JS::Handle<JS::Value> aIID,
203 JS::MutableHandle<JS::Value> aRetval,
204 ErrorResult& aRv) override {
205 aRv.Throw(NS_ERROR_FAILURE);
208 virtual void SetOriginAttributes(
209 const mozilla::dom::OriginAttributesDictionary& aAttrs) override {
210 MOZ_CRASH("This method cannot be called on workers.");
213 XMLHttpRequestUpload* GetUploadObjectNoCreate() const { return mUpload; }
215 void UpdateState(UniquePtr<StateData>&& aStateData,
216 UniquePtr<ResponseData>&& aResponseData);
218 virtual uint16_t ErrorCode() const override {
219 return 0; // eOK
222 virtual bool MozAnon() const override { return mMozAnon; }
224 virtual bool MozSystem() const override { return mMozSystem; }
226 bool SendInProgress() const { return !!mWorkerRef; }
228 private:
229 XMLHttpRequestWorker(WorkerPrivate* aWorkerPrivate,
230 nsIGlobalObject* aGlobalObject);
231 ~XMLHttpRequestWorker();
233 enum ReleaseType { Default, XHRIsGoingAway, WorkerIsGoingAway };
235 void ReleaseProxy(ReleaseType aType = Default);
237 void MaybePin(ErrorResult& aRv);
239 void MaybeDispatchPrematureAbortEvents(ErrorResult& aRv);
241 void DispatchPrematureAbortEvent(EventTarget* aTarget,
242 const nsAString& aEventType,
243 bool aUploadTarget, ErrorResult& aRv);
245 void Send(JSContext* aCx, JS::Handle<JSObject*> aBody, ErrorResult& aRv);
247 void SendInternal(const BodyExtractorBase* aBody, ErrorResult& aRv);
249 void ResetResponseData();
252 } // namespace mozilla::dom
254 #endif // mozilla_dom_workers_xmlhttprequest_h__