XMLHttpRequestWorker.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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. #ifndef mozilla_dom_XMLHttpRequestWorker_h
  6. #define mozilla_dom_XMLHttpRequestWorker_h
  7. #include "WorkerHolder.h"
  8. #include "XMLHttpRequest.h"
  9. #include "XMLHttpRequestString.h"
  10. #include "mozilla/dom/TypedArray.h"
  11. namespace mozilla {
  12. namespace dom {
  13. class Proxy;
  14. class SendRunnable;
  15. class DOMString;
  16. namespace workers {
  17. class WorkerPrivate;
  18. }
  19. class XMLHttpRequestWorker final : public XMLHttpRequest,
  20. public workers::WorkerHolder
  21. {
  22. public:
  23. struct StateData
  24. {
  25. XMLHttpRequestStringSnapshot mResponseText;
  26. nsString mResponseURL;
  27. uint32_t mStatus;
  28. nsCString mStatusText;
  29. uint16_t mReadyState;
  30. bool mFlagSend;
  31. JS::Heap<JS::Value> mResponse;
  32. nsresult mResponseTextResult;
  33. nsresult mStatusResult;
  34. nsresult mResponseResult;
  35. StateData()
  36. : mStatus(0), mReadyState(0), mFlagSend(false),
  37. mResponse(JS::UndefinedValue()), mResponseTextResult(NS_OK),
  38. mStatusResult(NS_OK), mResponseResult(NS_OK)
  39. { }
  40. void trace(JSTracer* trc);
  41. };
  42. private:
  43. RefPtr<XMLHttpRequestUpload> mUpload;
  44. workers::WorkerPrivate* mWorkerPrivate;
  45. RefPtr<Proxy> mProxy;
  46. XMLHttpRequestResponseType mResponseType;
  47. StateData mStateData;
  48. uint32_t mTimeout;
  49. bool mRooted;
  50. bool mBackgroundRequest;
  51. bool mWithCredentials;
  52. bool mCanceled;
  53. bool mMozAnon;
  54. bool mMozSystem;
  55. public:
  56. NS_DECL_ISUPPORTS_INHERITED
  57. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(XMLHttpRequestWorker,
  58. XMLHttpRequest)
  59. static already_AddRefed<XMLHttpRequest>
  60. Construct(const GlobalObject& aGlobal,
  61. const MozXMLHttpRequestParameters& aParams,
  62. ErrorResult& aRv);
  63. void
  64. Unpin();
  65. bool
  66. Notify(workers::Status aStatus) override;
  67. virtual uint16_t
  68. ReadyState() const override
  69. {
  70. return mStateData.mReadyState;
  71. }
  72. virtual void
  73. Open(const nsACString& aMethod, const nsAString& aUrl,
  74. ErrorResult& aRv) override
  75. {
  76. Open(aMethod, aUrl, true, Optional<nsAString>(),
  77. Optional<nsAString>(), aRv);
  78. }
  79. virtual void
  80. Open(const nsACString& aMethod, const nsAString& aUrl, bool aAsync,
  81. const nsAString& aUsername, const nsAString& aPassword,
  82. ErrorResult& aRv) override
  83. {
  84. Optional<nsAString> username;
  85. username = &aUsername;
  86. Optional<nsAString> password;
  87. password = &aPassword;
  88. Open(aMethod, aUrl, aAsync, username, password, aRv);
  89. }
  90. void
  91. Open(const nsACString& aMethod, const nsAString& aUrl,
  92. bool aAsync, const Optional<nsAString>& aUser,
  93. const Optional<nsAString>& aPassword, ErrorResult& aRv);
  94. virtual void
  95. SetRequestHeader(const nsACString& aHeader, const nsACString& aValue,
  96. ErrorResult& aRv) override;
  97. virtual uint32_t
  98. Timeout() const override
  99. {
  100. return mTimeout;
  101. }
  102. virtual void
  103. SetTimeout(uint32_t aTimeout, ErrorResult& aRv) override;
  104. virtual bool
  105. WithCredentials() const override
  106. {
  107. return mWithCredentials;
  108. }
  109. virtual void
  110. SetWithCredentials(bool aWithCredentials, ErrorResult& aRv) override;
  111. virtual bool
  112. MozBackgroundRequest() const override
  113. {
  114. return mBackgroundRequest;
  115. }
  116. virtual void
  117. SetMozBackgroundRequest(bool aBackgroundRequest, ErrorResult& aRv) override;
  118. virtual nsIChannel*
  119. GetChannel() const override
  120. {
  121. MOZ_CRASH("This method cannot be called on workers.");
  122. }
  123. virtual void
  124. GetNetworkInterfaceId(nsACString& aId) const override
  125. {
  126. MOZ_CRASH("This method cannot be called on workers.");
  127. }
  128. virtual void
  129. SetNetworkInterfaceId(const nsACString& aId) override
  130. {
  131. MOZ_CRASH("This method cannot be called on workers.");
  132. }
  133. virtual XMLHttpRequestUpload*
  134. GetUpload(ErrorResult& aRv) override;
  135. virtual void
  136. Send(JSContext* aCx, ErrorResult& aRv) override;
  137. virtual void
  138. Send(JSContext* aCx, const nsAString& aBody, ErrorResult& aRv) override;
  139. virtual void
  140. Send(JSContext* aCx, nsIInputStream* aStream, ErrorResult& aRv) override
  141. {
  142. MOZ_CRASH("This method cannot be called on workers.");
  143. }
  144. virtual void
  145. Send(JSContext* aCx, Blob& aBody, ErrorResult& aRv) override;
  146. virtual void
  147. Send(JSContext* aCx, FormData& aBody, ErrorResult& aRv) override;
  148. virtual void
  149. Send(JSContext* aCx, const ArrayBuffer& aBody, ErrorResult& aRv) override;
  150. virtual void
  151. Send(JSContext* aCx, const ArrayBufferView& aBody, ErrorResult& aRv) override;
  152. virtual void
  153. Send(JSContext* aCx, URLSearchParams& aBody, ErrorResult& aRv) override;
  154. virtual void
  155. Send(JSContext* aCx, nsIDocument& aDoc, ErrorResult& aRv) override
  156. {
  157. MOZ_CRASH("This method cannot be called on workers.");
  158. }
  159. virtual void
  160. Abort(ErrorResult& aRv) override;
  161. virtual void
  162. GetResponseURL(nsAString& aUrl) override
  163. {
  164. aUrl = mStateData.mResponseURL;
  165. }
  166. uint32_t
  167. GetStatus(ErrorResult& aRv) override
  168. {
  169. aRv = mStateData.mStatusResult;
  170. return mStateData.mStatus;
  171. }
  172. virtual void
  173. GetStatusText(nsACString& aStatusText, ErrorResult& aRv) override
  174. {
  175. aStatusText = mStateData.mStatusText;
  176. }
  177. virtual void
  178. GetResponseHeader(const nsACString& aHeader, nsACString& aResponseHeader,
  179. ErrorResult& aRv) override;
  180. virtual void
  181. GetAllResponseHeaders(nsACString& aResponseHeaders,
  182. ErrorResult& aRv) override;
  183. virtual void
  184. OverrideMimeType(const nsAString& aMimeType, ErrorResult& aRv) override;
  185. virtual XMLHttpRequestResponseType
  186. ResponseType() const override
  187. {
  188. return mResponseType;
  189. }
  190. virtual void
  191. SetResponseType(XMLHttpRequestResponseType aResponseType,
  192. ErrorResult& aRv) override;
  193. virtual void
  194. GetResponse(JSContext* /* unused */, JS::MutableHandle<JS::Value> aResponse,
  195. ErrorResult& aRv) override;
  196. virtual void
  197. GetResponseText(DOMString& aResponseText, ErrorResult& aRv) override;
  198. virtual nsIDocument*
  199. GetResponseXML(ErrorResult& aRv) override
  200. {
  201. MOZ_CRASH("This method should not be called.");
  202. }
  203. virtual void
  204. GetInterface(JSContext* aCx, nsIJSID* aIID,
  205. JS::MutableHandle<JS::Value> aRetval,
  206. ErrorResult& aRv) override
  207. {
  208. aRv.Throw(NS_ERROR_FAILURE);
  209. }
  210. virtual void
  211. SetOriginAttributes(const mozilla::dom::OriginAttributesDictionary& aAttrs) override
  212. {
  213. MOZ_CRASH("This method cannot be called on workers.");
  214. }
  215. XMLHttpRequestUpload*
  216. GetUploadObjectNoCreate() const
  217. {
  218. return mUpload;
  219. }
  220. void
  221. UpdateState(const StateData& aStateData, bool aUseCachedArrayBufferResponse);
  222. void
  223. NullResponseText()
  224. {
  225. mStateData.mResponseText.SetVoid();
  226. mStateData.mResponse.setNull();
  227. }
  228. virtual bool MozAnon() const override
  229. {
  230. return mMozAnon;
  231. }
  232. virtual bool MozSystem() const override
  233. {
  234. return mMozSystem;
  235. }
  236. bool
  237. SendInProgress() const
  238. {
  239. return mRooted;
  240. }
  241. private:
  242. explicit XMLHttpRequestWorker(workers::WorkerPrivate* aWorkerPrivate);
  243. ~XMLHttpRequestWorker();
  244. enum ReleaseType { Default, XHRIsGoingAway, WorkerIsGoingAway };
  245. void
  246. ReleaseProxy(ReleaseType aType = Default);
  247. void
  248. MaybePin(ErrorResult& aRv);
  249. void
  250. MaybeDispatchPrematureAbortEvents(ErrorResult& aRv);
  251. void
  252. DispatchPrematureAbortEvent(EventTarget* aTarget,
  253. const nsAString& aEventType, bool aUploadTarget,
  254. ErrorResult& aRv);
  255. void
  256. Send(JSContext* aCx, JS::Handle<JSObject*> aBody, ErrorResult& aRv);
  257. void
  258. SendInternal(SendRunnable* aRunnable,
  259. ErrorResult& aRv);
  260. };
  261. } // dom namespace
  262. } // mozilla namespace
  263. #endif // mozilla_dom_workers_xmlhttprequest_h__