1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/workers/XMLHttpRequest.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,296 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_dom_workers_xmlhttprequest_h__ 1.10 +#define mozilla_dom_workers_xmlhttprequest_h__ 1.11 + 1.12 +#include "mozilla/dom/workers/bindings/WorkerFeature.h" 1.13 + 1.14 +// Need this for XMLHttpRequestResponseType. 1.15 +#include "mozilla/dom/XMLHttpRequestBinding.h" 1.16 + 1.17 +#include "mozilla/dom/TypedArray.h" 1.18 + 1.19 +#include "js/StructuredClone.h" 1.20 +#include "nsXMLHttpRequest.h" 1.21 + 1.22 +BEGIN_WORKERS_NAMESPACE 1.23 + 1.24 +class Proxy; 1.25 +class XMLHttpRequestUpload; 1.26 +class WorkerPrivate; 1.27 + 1.28 +class XMLHttpRequest MOZ_FINAL: public nsXHREventTarget, 1.29 + public WorkerFeature 1.30 +{ 1.31 +public: 1.32 + struct StateData 1.33 + { 1.34 + nsString mResponseText; 1.35 + uint32_t mStatus; 1.36 + nsCString mStatusText; 1.37 + uint16_t mReadyState; 1.38 + JS::Heap<JS::Value> mResponse; 1.39 + nsresult mResponseTextResult; 1.40 + nsresult mStatusResult; 1.41 + nsresult mResponseResult; 1.42 + 1.43 + StateData() 1.44 + : mStatus(0), mReadyState(0), mResponse(JSVAL_VOID), 1.45 + mResponseTextResult(NS_OK), mStatusResult(NS_OK), 1.46 + mResponseResult(NS_OK) 1.47 + { } 1.48 + }; 1.49 + 1.50 +private: 1.51 + nsRefPtr<XMLHttpRequestUpload> mUpload; 1.52 + WorkerPrivate* mWorkerPrivate; 1.53 + nsRefPtr<Proxy> mProxy; 1.54 + XMLHttpRequestResponseType mResponseType; 1.55 + StateData mStateData; 1.56 + 1.57 + uint32_t mTimeout; 1.58 + 1.59 + bool mRooted; 1.60 + bool mBackgroundRequest; 1.61 + bool mWithCredentials; 1.62 + bool mCanceled; 1.63 + 1.64 + bool mMozAnon; 1.65 + bool mMozSystem; 1.66 + 1.67 +public: 1.68 + virtual JSObject* 1.69 + WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.70 + 1.71 + NS_DECL_ISUPPORTS_INHERITED 1.72 + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(XMLHttpRequest, 1.73 + nsXHREventTarget) 1.74 + 1.75 + nsISupports* 1.76 + GetParentObject() const 1.77 + { 1.78 + // There's only one global on a worker, so we don't need to specify. 1.79 + return nullptr; 1.80 + } 1.81 + 1.82 + static already_AddRefed<XMLHttpRequest> 1.83 + Constructor(const GlobalObject& aGlobal, 1.84 + const MozXMLHttpRequestParameters& aParams, 1.85 + ErrorResult& aRv); 1.86 + 1.87 + static already_AddRefed<XMLHttpRequest> 1.88 + Constructor(const GlobalObject& aGlobal, const nsAString& ignored, 1.89 + ErrorResult& aRv) 1.90 + { 1.91 + // Pretend like someone passed null, so we can pick up the default values 1.92 + MozXMLHttpRequestParameters params; 1.93 + if (!params.Init(aGlobal.GetContext(), JS::NullHandleValue)) { 1.94 + aRv.Throw(NS_ERROR_UNEXPECTED); 1.95 + return nullptr; 1.96 + } 1.97 + 1.98 + return Constructor(aGlobal, params, aRv); 1.99 + } 1.100 + 1.101 + void 1.102 + Unpin(); 1.103 + 1.104 + bool 1.105 + Notify(JSContext* aCx, Status aStatus) MOZ_OVERRIDE; 1.106 + 1.107 + IMPL_EVENT_HANDLER(readystatechange) 1.108 + 1.109 + uint16_t 1.110 + ReadyState() const 1.111 + { 1.112 + return mStateData.mReadyState; 1.113 + } 1.114 + 1.115 + void Open(const nsACString& aMethod, const nsAString& aUrl, ErrorResult& aRv) 1.116 + { 1.117 + Open(aMethod, aUrl, true, Optional<nsAString>(), 1.118 + Optional<nsAString>(), aRv); 1.119 + } 1.120 + void 1.121 + Open(const nsACString& aMethod, const nsAString& aUrl, bool aAsync, 1.122 + const Optional<nsAString>& aUser, const Optional<nsAString>& aPassword, 1.123 + ErrorResult& aRv); 1.124 + 1.125 + void 1.126 + SetRequestHeader(const nsACString& aHeader, const nsACString& aValue, 1.127 + ErrorResult& aRv); 1.128 + 1.129 + uint32_t 1.130 + Timeout() const 1.131 + { 1.132 + return mTimeout; 1.133 + } 1.134 + 1.135 + void 1.136 + SetTimeout(uint32_t aTimeout, ErrorResult& aRv); 1.137 + 1.138 + bool 1.139 + WithCredentials() const 1.140 + { 1.141 + return mWithCredentials; 1.142 + } 1.143 + 1.144 + void 1.145 + SetWithCredentials(bool aWithCredentials, ErrorResult& aRv); 1.146 + 1.147 + bool 1.148 + MozBackgroundRequest() const 1.149 + { 1.150 + return mBackgroundRequest; 1.151 + } 1.152 + 1.153 + void 1.154 + SetMozBackgroundRequest(bool aBackgroundRequest, ErrorResult& aRv); 1.155 + 1.156 + XMLHttpRequestUpload* 1.157 + GetUpload(ErrorResult& aRv); 1.158 + 1.159 + void 1.160 + Send(ErrorResult& aRv); 1.161 + 1.162 + void 1.163 + Send(const nsAString& aBody, ErrorResult& aRv); 1.164 + 1.165 + void 1.166 + Send(JS::Handle<JSObject*> aBody, ErrorResult& aRv); 1.167 + 1.168 + void 1.169 + Send(const ArrayBuffer& aBody, ErrorResult& aRv); 1.170 + 1.171 + void 1.172 + Send(const ArrayBufferView& aBody, ErrorResult& aRv); 1.173 + 1.174 + void 1.175 + SendAsBinary(const nsAString& aBody, ErrorResult& aRv); 1.176 + 1.177 + void 1.178 + Abort(ErrorResult& aRv); 1.179 + 1.180 + uint16_t 1.181 + GetStatus(ErrorResult& aRv) const 1.182 + { 1.183 + aRv = mStateData.mStatusResult; 1.184 + return mStateData.mStatus; 1.185 + } 1.186 + 1.187 + void 1.188 + GetStatusText(nsACString& aStatusText) const 1.189 + { 1.190 + aStatusText = mStateData.mStatusText; 1.191 + } 1.192 + 1.193 + void 1.194 + GetResponseHeader(const nsACString& aHeader, nsACString& aResponseHeader, 1.195 + ErrorResult& aRv); 1.196 + 1.197 + void 1.198 + GetAllResponseHeaders(nsACString& aResponseHeaders, ErrorResult& aRv); 1.199 + 1.200 + void 1.201 + OverrideMimeType(const nsAString& aMimeType, ErrorResult& aRv); 1.202 + 1.203 + XMLHttpRequestResponseType 1.204 + ResponseType() const 1.205 + { 1.206 + return mResponseType; 1.207 + } 1.208 + 1.209 + void 1.210 + SetResponseType(XMLHttpRequestResponseType aResponseType, ErrorResult& aRv); 1.211 + 1.212 + void 1.213 + GetResponse(JSContext* /* unused */, JS::MutableHandle<JS::Value> aResponse, 1.214 + ErrorResult& aRv); 1.215 + 1.216 + void 1.217 + GetResponseText(nsAString& aResponseText, ErrorResult& aRv); 1.218 + 1.219 + JSObject* 1.220 + GetResponseXML() const 1.221 + { 1.222 + return nullptr; 1.223 + } 1.224 + 1.225 + JSObject* 1.226 + GetChannel() const 1.227 + { 1.228 + return nullptr; 1.229 + } 1.230 + 1.231 + void 1.232 + GetInterface(JSContext* cx, JS::Handle<JSObject*> aIID, 1.233 + JS::MutableHandle<JS::Value> aRetval, ErrorResult& aRv) 1.234 + { 1.235 + aRv.Throw(NS_ERROR_FAILURE); 1.236 + } 1.237 + 1.238 + XMLHttpRequestUpload* 1.239 + GetUploadObjectNoCreate() const 1.240 + { 1.241 + return mUpload; 1.242 + } 1.243 + 1.244 + void 1.245 + UpdateState(const StateData& aStateData); 1.246 + 1.247 + void 1.248 + NullResponseText() 1.249 + { 1.250 + mStateData.mResponseText.SetIsVoid(true); 1.251 + mStateData.mResponse = JSVAL_NULL; 1.252 + } 1.253 + 1.254 + bool MozAnon() const 1.255 + { 1.256 + return mMozAnon; 1.257 + } 1.258 + 1.259 + bool MozSystem() const 1.260 + { 1.261 + return mMozSystem; 1.262 + } 1.263 + 1.264 +private: 1.265 + XMLHttpRequest(WorkerPrivate* aWorkerPrivate); 1.266 + ~XMLHttpRequest(); 1.267 + 1.268 + enum ReleaseType { Default, XHRIsGoingAway, WorkerIsGoingAway }; 1.269 + 1.270 + void 1.271 + ReleaseProxy(ReleaseType aType = Default); 1.272 + 1.273 + void 1.274 + MaybePin(ErrorResult& aRv); 1.275 + 1.276 + void 1.277 + MaybeDispatchPrematureAbortEvents(ErrorResult& aRv); 1.278 + 1.279 + void 1.280 + DispatchPrematureAbortEvent(EventTarget* aTarget, 1.281 + const nsAString& aEventType, bool aUploadTarget, 1.282 + ErrorResult& aRv); 1.283 + 1.284 + bool 1.285 + SendInProgress() const 1.286 + { 1.287 + return mRooted; 1.288 + } 1.289 + 1.290 + void 1.291 + SendInternal(const nsAString& aStringBody, 1.292 + JSAutoStructuredCloneBuffer&& aBody, 1.293 + nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects, 1.294 + ErrorResult& aRv); 1.295 +}; 1.296 + 1.297 +END_WORKERS_NAMESPACE 1.298 + 1.299 +#endif // mozilla_dom_workers_xmlhttprequest_h__