WorkerScope.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_workerscope_h__
  6. #define mozilla_dom_workerscope_h__
  7. #include "Workers.h"
  8. #include "mozilla/DOMEventTargetHelper.h"
  9. #include "mozilla/dom/Headers.h"
  10. #include "mozilla/dom/RequestBinding.h"
  11. #include "nsWeakReference.h"
  12. #include "mozilla/dom/ImageBitmapSource.h"
  13. namespace mozilla {
  14. namespace dom {
  15. class AnyCallback;
  16. struct ChannelPixelLayout;
  17. class Console;
  18. class Crypto;
  19. class Function;
  20. class IDBFactory;
  21. enum class ImageBitmapFormat : uint32_t;
  22. class Performance;
  23. class Promise;
  24. class RequestOrUSVString;
  25. class ServiceWorkerRegistration;
  26. class WorkerLocation;
  27. class WorkerNavigator;
  28. namespace cache {
  29. class CacheStorage;
  30. } // namespace cache
  31. namespace workers {
  32. class ServiceWorkerClients;
  33. class WorkerPrivate;
  34. } // namespace workers
  35. class WorkerGlobalScope : public DOMEventTargetHelper,
  36. public nsIGlobalObject,
  37. public nsSupportsWeakReference
  38. {
  39. typedef mozilla::dom::IDBFactory IDBFactory;
  40. RefPtr<Console> mConsole;
  41. RefPtr<Crypto> mCrypto;
  42. RefPtr<WorkerLocation> mLocation;
  43. RefPtr<WorkerNavigator> mNavigator;
  44. RefPtr<Performance> mPerformance;
  45. RefPtr<IDBFactory> mIndexedDB;
  46. RefPtr<cache::CacheStorage> mCacheStorage;
  47. uint32_t mWindowInteractionsAllowed;
  48. protected:
  49. typedef mozilla::dom::workers::WorkerPrivate WorkerPrivate;
  50. WorkerPrivate* mWorkerPrivate;
  51. explicit WorkerGlobalScope(WorkerPrivate* aWorkerPrivate);
  52. virtual ~WorkerGlobalScope();
  53. public:
  54. virtual JSObject*
  55. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  56. virtual bool
  57. WrapGlobalObject(JSContext* aCx, JS::MutableHandle<JSObject*> aReflector) = 0;
  58. virtual JSObject*
  59. GetGlobalJSObject(void) override
  60. {
  61. return GetWrapper();
  62. }
  63. NS_DECL_ISUPPORTS_INHERITED
  64. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerGlobalScope,
  65. DOMEventTargetHelper)
  66. WorkerGlobalScope*
  67. Self()
  68. {
  69. return this;
  70. }
  71. Console*
  72. GetConsole(ErrorResult& aRv);
  73. Console*
  74. GetConsoleIfExists() const
  75. {
  76. return mConsole;
  77. }
  78. Crypto*
  79. GetCrypto(ErrorResult& aError);
  80. already_AddRefed<WorkerLocation>
  81. Location();
  82. already_AddRefed<WorkerNavigator>
  83. Navigator();
  84. already_AddRefed<WorkerNavigator>
  85. GetExistingNavigator() const;
  86. void
  87. Close(JSContext* aCx, ErrorResult& aRv);
  88. OnErrorEventHandlerNonNull*
  89. GetOnerror();
  90. void
  91. SetOnerror(OnErrorEventHandlerNonNull* aHandler);
  92. void
  93. ImportScripts(const Sequence<nsString>& aScriptURLs, ErrorResult& aRv);
  94. int32_t
  95. SetTimeout(JSContext* aCx, Function& aHandler, const int32_t aTimeout,
  96. const Sequence<JS::Value>& aArguments, ErrorResult& aRv);
  97. int32_t
  98. SetTimeout(JSContext* aCx, const nsAString& aHandler, const int32_t aTimeout,
  99. const Sequence<JS::Value>& /* unused */, ErrorResult& aRv);
  100. void
  101. ClearTimeout(int32_t aHandle);
  102. int32_t
  103. SetInterval(JSContext* aCx, Function& aHandler,
  104. const Optional<int32_t>& aTimeout,
  105. const Sequence<JS::Value>& aArguments, ErrorResult& aRv);
  106. int32_t
  107. SetInterval(JSContext* aCx, const nsAString& aHandler,
  108. const Optional<int32_t>& aTimeout,
  109. const Sequence<JS::Value>& /* unused */, ErrorResult& aRv);
  110. void
  111. ClearInterval(int32_t aHandle);
  112. void
  113. Atob(const nsAString& aAtob, nsAString& aOutput, ErrorResult& aRv) const;
  114. void
  115. Btoa(const nsAString& aBtoa, nsAString& aOutput, ErrorResult& aRv) const;
  116. void
  117. GetOrigin(nsAString& aOrigin) const;
  118. IMPL_EVENT_HANDLER(online)
  119. IMPL_EVENT_HANDLER(offline)
  120. void
  121. Dump(const Optional<nsAString>& aString) const;
  122. Performance* GetPerformance();
  123. already_AddRefed<Promise>
  124. Fetch(const RequestOrUSVString& aInput, const RequestInit& aInit, ErrorResult& aRv);
  125. already_AddRefed<IDBFactory>
  126. GetIndexedDB(ErrorResult& aErrorResult);
  127. already_AddRefed<cache::CacheStorage>
  128. GetCaches(ErrorResult& aRv);
  129. bool IsSecureContext() const;
  130. already_AddRefed<Promise>
  131. CreateImageBitmap(const ImageBitmapSource& aImage, ErrorResult& aRv);
  132. already_AddRefed<Promise>
  133. CreateImageBitmap(const ImageBitmapSource& aImage,
  134. int32_t aSx, int32_t aSy, int32_t aSw, int32_t aSh,
  135. ErrorResult& aRv);
  136. already_AddRefed<mozilla::dom::Promise>
  137. CreateImageBitmap(const ImageBitmapSource& aImage,
  138. int32_t aOffset, int32_t aLength,
  139. mozilla::dom::ImageBitmapFormat aFormat,
  140. const mozilla::dom::Sequence<mozilla::dom::ChannelPixelLayout>& aLayout,
  141. mozilla::ErrorResult& aRv);
  142. bool
  143. WindowInteractionAllowed() const
  144. {
  145. return mWindowInteractionsAllowed > 0;
  146. }
  147. void
  148. AllowWindowInteraction()
  149. {
  150. mWindowInteractionsAllowed++;
  151. }
  152. void
  153. ConsumeWindowInteraction()
  154. {
  155. MOZ_ASSERT(mWindowInteractionsAllowed > 0);
  156. mWindowInteractionsAllowed--;
  157. }
  158. };
  159. class DedicatedWorkerGlobalScope final : public WorkerGlobalScope
  160. {
  161. ~DedicatedWorkerGlobalScope() { }
  162. public:
  163. explicit DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate);
  164. virtual bool
  165. WrapGlobalObject(JSContext* aCx,
  166. JS::MutableHandle<JSObject*> aReflector) override;
  167. void
  168. PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
  169. const Optional<Sequence<JS::Value>>& aTransferable,
  170. ErrorResult& aRv);
  171. IMPL_EVENT_HANDLER(message)
  172. };
  173. class SharedWorkerGlobalScope final : public WorkerGlobalScope
  174. {
  175. const nsCString mName;
  176. ~SharedWorkerGlobalScope() { }
  177. public:
  178. SharedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate,
  179. const nsCString& aName);
  180. virtual bool
  181. WrapGlobalObject(JSContext* aCx,
  182. JS::MutableHandle<JSObject*> aReflector) override;
  183. void GetName(DOMString& aName) const
  184. {
  185. aName.AsAString() = NS_ConvertUTF8toUTF16(mName);
  186. }
  187. IMPL_EVENT_HANDLER(connect)
  188. };
  189. class ServiceWorkerGlobalScope final : public WorkerGlobalScope
  190. {
  191. const nsString mScope;
  192. RefPtr<workers::ServiceWorkerClients> mClients;
  193. RefPtr<ServiceWorkerRegistration> mRegistration;
  194. ~ServiceWorkerGlobalScope();
  195. public:
  196. NS_DECL_ISUPPORTS_INHERITED
  197. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerGlobalScope,
  198. WorkerGlobalScope)
  199. IMPL_EVENT_HANDLER(notificationclick)
  200. IMPL_EVENT_HANDLER(notificationclose)
  201. ServiceWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, const nsACString& aScope);
  202. virtual bool
  203. WrapGlobalObject(JSContext* aCx,
  204. JS::MutableHandle<JSObject*> aReflector) override;
  205. static bool
  206. OpenWindowEnabled(JSContext* aCx, JSObject* aObj);
  207. void
  208. GetScope(nsString& aScope) const
  209. {
  210. aScope = mScope;
  211. }
  212. workers::ServiceWorkerClients*
  213. Clients();
  214. ServiceWorkerRegistration*
  215. Registration();
  216. already_AddRefed<Promise>
  217. SkipWaiting(ErrorResult& aRv);
  218. IMPL_EVENT_HANDLER(activate)
  219. IMPL_EVENT_HANDLER(fetch)
  220. IMPL_EVENT_HANDLER(install)
  221. IMPL_EVENT_HANDLER(message)
  222. IMPL_EVENT_HANDLER(push)
  223. IMPL_EVENT_HANDLER(pushsubscriptionchange)
  224. };
  225. class WorkerDebuggerGlobalScope final : public DOMEventTargetHelper,
  226. public nsIGlobalObject
  227. {
  228. typedef mozilla::dom::workers::WorkerPrivate WorkerPrivate;
  229. WorkerPrivate* mWorkerPrivate;
  230. RefPtr<Console> mConsole;
  231. public:
  232. explicit WorkerDebuggerGlobalScope(WorkerPrivate* aWorkerPrivate);
  233. NS_DECL_ISUPPORTS_INHERITED
  234. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerDebuggerGlobalScope,
  235. DOMEventTargetHelper)
  236. virtual JSObject*
  237. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
  238. {
  239. MOZ_CRASH("Shouldn't get here!");
  240. }
  241. virtual bool
  242. WrapGlobalObject(JSContext* aCx,
  243. JS::MutableHandle<JSObject*> aReflector);
  244. virtual JSObject*
  245. GetGlobalJSObject(void) override
  246. {
  247. return GetWrapper();
  248. }
  249. void
  250. GetGlobal(JSContext* aCx, JS::MutableHandle<JSObject*> aGlobal,
  251. ErrorResult& aRv);
  252. void
  253. CreateSandbox(JSContext* aCx, const nsAString& aName,
  254. JS::Handle<JSObject*> aPrototype,
  255. JS::MutableHandle<JSObject*> aResult,
  256. ErrorResult& aRv);
  257. void
  258. LoadSubScript(JSContext* aCx, const nsAString& aURL,
  259. const Optional<JS::Handle<JSObject*>>& aSandbox,
  260. ErrorResult& aRv);
  261. void
  262. EnterEventLoop();
  263. void
  264. LeaveEventLoop();
  265. void
  266. PostMessage(const nsAString& aMessage);
  267. IMPL_EVENT_HANDLER(message)
  268. void
  269. SetImmediate(Function& aHandler, ErrorResult& aRv);
  270. void
  271. ReportError(JSContext* aCx, const nsAString& aMessage);
  272. void
  273. RetrieveConsoleEvents(JSContext* aCx, nsTArray<JS::Value>& aEvents,
  274. ErrorResult& aRv);
  275. void
  276. SetConsoleEventHandler(JSContext* aCx, AnyCallback* aHandler,
  277. ErrorResult& aRv);
  278. Console*
  279. GetConsole(ErrorResult& aRv);
  280. Console*
  281. GetConsoleIfExists() const
  282. {
  283. return mConsole;
  284. }
  285. void
  286. Dump(JSContext* aCx, const Optional<nsAString>& aString) const;
  287. private:
  288. virtual ~WorkerDebuggerGlobalScope();
  289. };
  290. } // namespace dom
  291. } // namespace mozilla
  292. inline nsISupports*
  293. ToSupports(mozilla::dom::WorkerGlobalScope* aScope)
  294. {
  295. return static_cast<nsIDOMEventTarget*>(aScope);
  296. }
  297. #endif /* mozilla_dom_workerscope_h__ */