dom/workers/XMLHttpRequest.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial