Bug 1655413 [wpt PR 24763] - Make CSP default-src without 'unsafe-eval' block eval...
[gecko.git] / dom / workers / WorkerThread.h
blob78ac19eaae3fdf4a198f4ad04ac473179ad4a86c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_workers_WorkerThread_h__
8 #define mozilla_dom_workers_WorkerThread_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/CondVar.h"
12 #include "mozilla/DebugOnly.h"
13 #include "mozilla/dom/SafeRefPtr.h"
14 #include "nsISupportsImpl.h"
15 #include "mozilla/RefPtr.h"
16 #include "nsThread.h"
18 class nsIRunnable;
20 namespace mozilla {
21 namespace dom {
23 class WorkerRunnable;
24 class WorkerPrivate;
25 template <class>
26 class WorkerPrivateParent;
28 namespace workerinternals {
29 class RuntimeService;
32 // This class lets us restrict the public methods that can be called on
33 // WorkerThread to RuntimeService and WorkerPrivate without letting them gain
34 // full access to private methods (as would happen if they were simply friends).
35 class WorkerThreadFriendKey {
36 friend class workerinternals::RuntimeService;
37 friend class WorkerPrivate;
38 friend class WorkerPrivateParent<WorkerPrivate>;
40 WorkerThreadFriendKey();
41 ~WorkerThreadFriendKey();
44 class WorkerThread final : public nsThread {
45 class Observer;
47 Mutex mLock;
48 CondVar mWorkerPrivateCondVar;
50 // Protected by nsThread::mLock.
51 WorkerPrivate* mWorkerPrivate;
53 // Only touched on the target thread.
54 RefPtr<Observer> mObserver;
56 // Protected by nsThread::mLock and waited on with mWorkerPrivateCondVar.
57 uint32_t mOtherThreadsDispatchingViaEventTarget;
59 #ifdef DEBUG
60 // Protected by nsThread::mLock.
61 bool mAcceptingNonWorkerRunnables;
62 #endif
64 // Using this struct we restrict access to the constructor while still being
65 // able to use MakeSafeRefPtr.
66 struct ConstructorKey {};
68 public:
69 explicit WorkerThread(ConstructorKey);
71 static SafeRefPtr<WorkerThread> Create(const WorkerThreadFriendKey& aKey);
73 void SetWorker(const WorkerThreadFriendKey& aKey,
74 WorkerPrivate* aWorkerPrivate);
76 nsresult DispatchPrimaryRunnable(const WorkerThreadFriendKey& aKey,
77 already_AddRefed<nsIRunnable> aRunnable);
79 nsresult DispatchAnyThread(const WorkerThreadFriendKey& aKey,
80 already_AddRefed<WorkerRunnable> aWorkerRunnable);
82 uint32_t RecursionDepth(const WorkerThreadFriendKey& aKey) const;
84 PerformanceCounter* GetPerformanceCounter(nsIRunnable* aEvent) const override;
86 NS_INLINE_DECL_REFCOUNTING_INHERITED(WorkerThread, nsThread)
88 private:
89 ~WorkerThread();
91 // This should only be called by consumers that have an
92 // nsIEventTarget/nsIThread pointer.
93 NS_IMETHOD
94 Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) override;
96 NS_IMETHOD
97 DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags) override;
99 NS_IMETHOD
100 DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) override;
102 void IncrementDispatchCounter();
105 } // namespace dom
106 } // namespace mozilla
108 #endif // mozilla_dom_workers_WorkerThread_h__