|
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
|
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 file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef mozilla_dom_workers_xmlhttprequest_h__ |
|
7 #define mozilla_dom_workers_xmlhttprequest_h__ |
|
8 |
|
9 #include "mozilla/dom/workers/bindings/WorkerFeature.h" |
|
10 |
|
11 // Need this for XMLHttpRequestResponseType. |
|
12 #include "mozilla/dom/XMLHttpRequestBinding.h" |
|
13 |
|
14 #include "mozilla/dom/TypedArray.h" |
|
15 |
|
16 #include "js/StructuredClone.h" |
|
17 #include "nsXMLHttpRequest.h" |
|
18 |
|
19 BEGIN_WORKERS_NAMESPACE |
|
20 |
|
21 class Proxy; |
|
22 class XMLHttpRequestUpload; |
|
23 class WorkerPrivate; |
|
24 |
|
25 class XMLHttpRequest MOZ_FINAL: public nsXHREventTarget, |
|
26 public WorkerFeature |
|
27 { |
|
28 public: |
|
29 struct StateData |
|
30 { |
|
31 nsString mResponseText; |
|
32 uint32_t mStatus; |
|
33 nsCString mStatusText; |
|
34 uint16_t mReadyState; |
|
35 JS::Heap<JS::Value> mResponse; |
|
36 nsresult mResponseTextResult; |
|
37 nsresult mStatusResult; |
|
38 nsresult mResponseResult; |
|
39 |
|
40 StateData() |
|
41 : mStatus(0), mReadyState(0), mResponse(JSVAL_VOID), |
|
42 mResponseTextResult(NS_OK), mStatusResult(NS_OK), |
|
43 mResponseResult(NS_OK) |
|
44 { } |
|
45 }; |
|
46 |
|
47 private: |
|
48 nsRefPtr<XMLHttpRequestUpload> mUpload; |
|
49 WorkerPrivate* mWorkerPrivate; |
|
50 nsRefPtr<Proxy> mProxy; |
|
51 XMLHttpRequestResponseType mResponseType; |
|
52 StateData mStateData; |
|
53 |
|
54 uint32_t mTimeout; |
|
55 |
|
56 bool mRooted; |
|
57 bool mBackgroundRequest; |
|
58 bool mWithCredentials; |
|
59 bool mCanceled; |
|
60 |
|
61 bool mMozAnon; |
|
62 bool mMozSystem; |
|
63 |
|
64 public: |
|
65 virtual JSObject* |
|
66 WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
67 |
|
68 NS_DECL_ISUPPORTS_INHERITED |
|
69 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(XMLHttpRequest, |
|
70 nsXHREventTarget) |
|
71 |
|
72 nsISupports* |
|
73 GetParentObject() const |
|
74 { |
|
75 // There's only one global on a worker, so we don't need to specify. |
|
76 return nullptr; |
|
77 } |
|
78 |
|
79 static already_AddRefed<XMLHttpRequest> |
|
80 Constructor(const GlobalObject& aGlobal, |
|
81 const MozXMLHttpRequestParameters& aParams, |
|
82 ErrorResult& aRv); |
|
83 |
|
84 static already_AddRefed<XMLHttpRequest> |
|
85 Constructor(const GlobalObject& aGlobal, const nsAString& ignored, |
|
86 ErrorResult& aRv) |
|
87 { |
|
88 // Pretend like someone passed null, so we can pick up the default values |
|
89 MozXMLHttpRequestParameters params; |
|
90 if (!params.Init(aGlobal.GetContext(), JS::NullHandleValue)) { |
|
91 aRv.Throw(NS_ERROR_UNEXPECTED); |
|
92 return nullptr; |
|
93 } |
|
94 |
|
95 return Constructor(aGlobal, params, aRv); |
|
96 } |
|
97 |
|
98 void |
|
99 Unpin(); |
|
100 |
|
101 bool |
|
102 Notify(JSContext* aCx, Status aStatus) MOZ_OVERRIDE; |
|
103 |
|
104 IMPL_EVENT_HANDLER(readystatechange) |
|
105 |
|
106 uint16_t |
|
107 ReadyState() const |
|
108 { |
|
109 return mStateData.mReadyState; |
|
110 } |
|
111 |
|
112 void Open(const nsACString& aMethod, const nsAString& aUrl, ErrorResult& aRv) |
|
113 { |
|
114 Open(aMethod, aUrl, true, Optional<nsAString>(), |
|
115 Optional<nsAString>(), aRv); |
|
116 } |
|
117 void |
|
118 Open(const nsACString& aMethod, const nsAString& aUrl, bool aAsync, |
|
119 const Optional<nsAString>& aUser, const Optional<nsAString>& aPassword, |
|
120 ErrorResult& aRv); |
|
121 |
|
122 void |
|
123 SetRequestHeader(const nsACString& aHeader, const nsACString& aValue, |
|
124 ErrorResult& aRv); |
|
125 |
|
126 uint32_t |
|
127 Timeout() const |
|
128 { |
|
129 return mTimeout; |
|
130 } |
|
131 |
|
132 void |
|
133 SetTimeout(uint32_t aTimeout, ErrorResult& aRv); |
|
134 |
|
135 bool |
|
136 WithCredentials() const |
|
137 { |
|
138 return mWithCredentials; |
|
139 } |
|
140 |
|
141 void |
|
142 SetWithCredentials(bool aWithCredentials, ErrorResult& aRv); |
|
143 |
|
144 bool |
|
145 MozBackgroundRequest() const |
|
146 { |
|
147 return mBackgroundRequest; |
|
148 } |
|
149 |
|
150 void |
|
151 SetMozBackgroundRequest(bool aBackgroundRequest, ErrorResult& aRv); |
|
152 |
|
153 XMLHttpRequestUpload* |
|
154 GetUpload(ErrorResult& aRv); |
|
155 |
|
156 void |
|
157 Send(ErrorResult& aRv); |
|
158 |
|
159 void |
|
160 Send(const nsAString& aBody, ErrorResult& aRv); |
|
161 |
|
162 void |
|
163 Send(JS::Handle<JSObject*> aBody, ErrorResult& aRv); |
|
164 |
|
165 void |
|
166 Send(const ArrayBuffer& aBody, ErrorResult& aRv); |
|
167 |
|
168 void |
|
169 Send(const ArrayBufferView& aBody, ErrorResult& aRv); |
|
170 |
|
171 void |
|
172 SendAsBinary(const nsAString& aBody, ErrorResult& aRv); |
|
173 |
|
174 void |
|
175 Abort(ErrorResult& aRv); |
|
176 |
|
177 uint16_t |
|
178 GetStatus(ErrorResult& aRv) const |
|
179 { |
|
180 aRv = mStateData.mStatusResult; |
|
181 return mStateData.mStatus; |
|
182 } |
|
183 |
|
184 void |
|
185 GetStatusText(nsACString& aStatusText) const |
|
186 { |
|
187 aStatusText = mStateData.mStatusText; |
|
188 } |
|
189 |
|
190 void |
|
191 GetResponseHeader(const nsACString& aHeader, nsACString& aResponseHeader, |
|
192 ErrorResult& aRv); |
|
193 |
|
194 void |
|
195 GetAllResponseHeaders(nsACString& aResponseHeaders, ErrorResult& aRv); |
|
196 |
|
197 void |
|
198 OverrideMimeType(const nsAString& aMimeType, ErrorResult& aRv); |
|
199 |
|
200 XMLHttpRequestResponseType |
|
201 ResponseType() const |
|
202 { |
|
203 return mResponseType; |
|
204 } |
|
205 |
|
206 void |
|
207 SetResponseType(XMLHttpRequestResponseType aResponseType, ErrorResult& aRv); |
|
208 |
|
209 void |
|
210 GetResponse(JSContext* /* unused */, JS::MutableHandle<JS::Value> aResponse, |
|
211 ErrorResult& aRv); |
|
212 |
|
213 void |
|
214 GetResponseText(nsAString& aResponseText, ErrorResult& aRv); |
|
215 |
|
216 JSObject* |
|
217 GetResponseXML() const |
|
218 { |
|
219 return nullptr; |
|
220 } |
|
221 |
|
222 JSObject* |
|
223 GetChannel() const |
|
224 { |
|
225 return nullptr; |
|
226 } |
|
227 |
|
228 void |
|
229 GetInterface(JSContext* cx, JS::Handle<JSObject*> aIID, |
|
230 JS::MutableHandle<JS::Value> aRetval, ErrorResult& aRv) |
|
231 { |
|
232 aRv.Throw(NS_ERROR_FAILURE); |
|
233 } |
|
234 |
|
235 XMLHttpRequestUpload* |
|
236 GetUploadObjectNoCreate() const |
|
237 { |
|
238 return mUpload; |
|
239 } |
|
240 |
|
241 void |
|
242 UpdateState(const StateData& aStateData); |
|
243 |
|
244 void |
|
245 NullResponseText() |
|
246 { |
|
247 mStateData.mResponseText.SetIsVoid(true); |
|
248 mStateData.mResponse = JSVAL_NULL; |
|
249 } |
|
250 |
|
251 bool MozAnon() const |
|
252 { |
|
253 return mMozAnon; |
|
254 } |
|
255 |
|
256 bool MozSystem() const |
|
257 { |
|
258 return mMozSystem; |
|
259 } |
|
260 |
|
261 private: |
|
262 XMLHttpRequest(WorkerPrivate* aWorkerPrivate); |
|
263 ~XMLHttpRequest(); |
|
264 |
|
265 enum ReleaseType { Default, XHRIsGoingAway, WorkerIsGoingAway }; |
|
266 |
|
267 void |
|
268 ReleaseProxy(ReleaseType aType = Default); |
|
269 |
|
270 void |
|
271 MaybePin(ErrorResult& aRv); |
|
272 |
|
273 void |
|
274 MaybeDispatchPrematureAbortEvents(ErrorResult& aRv); |
|
275 |
|
276 void |
|
277 DispatchPrematureAbortEvent(EventTarget* aTarget, |
|
278 const nsAString& aEventType, bool aUploadTarget, |
|
279 ErrorResult& aRv); |
|
280 |
|
281 bool |
|
282 SendInProgress() const |
|
283 { |
|
284 return mRooted; |
|
285 } |
|
286 |
|
287 void |
|
288 SendInternal(const nsAString& aStringBody, |
|
289 JSAutoStructuredCloneBuffer&& aBody, |
|
290 nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects, |
|
291 ErrorResult& aRv); |
|
292 }; |
|
293 |
|
294 END_WORKERS_NAMESPACE |
|
295 |
|
296 #endif // mozilla_dom_workers_xmlhttprequest_h__ |