1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
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/. */
6 #ifndef mozilla_dom_workers_workerprivate_h__
7 #define mozilla_dom_workers_workerprivate_h__
11 #include "nsIContentSecurityPolicy.h"
12 #include "nsIRunnable.h"
13 #include "nsIThread.h"
14 #include "nsIThreadInternal.h"
15 #include "nsPIDOMWindow.h"
17 #include "mozilla/Assertions.h"
18 #include "mozilla/CondVar.h"
19 #include "mozilla/TimeStamp.h"
20 #include "mozilla/dom/BindingDeclarations.h"
21 #include "nsCycleCollectionParticipant.h"
22 #include "nsDataHashtable.h"
23 #include "nsDOMEventTargetHelper.h"
24 #include "nsEventQueue.h"
25 #include "nsHashKeys.h"
26 #include "nsRefPtrHashtable.h"
29 #include "nsThreadUtils.h"
30 #include "nsTPriorityQueue.h"
31 #include "StructuredCloneTags.h"
34 #include "WorkerFeature.h"
36 class JSAutoStructuredCloneBuffer
;
40 class nsIScriptContext
;
54 BEGIN_WORKERS_NAMESPACE
58 class WorkerGlobalScope
;
61 class WorkerRunnable
: public nsIRunnable
64 enum Target
{ ParentThread
, WorkerThread
};
65 enum BusyBehavior
{ ModifyBusyCount
, UnchangedBusyCount
};
66 enum ClearingBehavior
{ SkipWhenClearing
, RunWhenClearing
};
69 WorkerPrivate
* mWorkerPrivate
;
71 BusyBehavior mBusyBehavior
;
72 ClearingBehavior mClearingBehavior
;
75 NS_DECL_THREADSAFE_ISUPPORTS
78 Dispatch(JSContext
* aCx
);
81 DispatchToMainThread(nsIRunnable
*);
84 WantsToRunDuringClear()
86 return mClearingBehavior
== RunWhenClearing
;
90 WorkerRunnable(WorkerPrivate
* aWorkerPrivate
, Target aTarget
,
91 BusyBehavior aBusyBehavior
,
92 ClearingBehavior aClearingBehavior
)
96 : mWorkerPrivate(aWorkerPrivate
), mTarget(aTarget
),
97 mBusyBehavior(aBusyBehavior
), mClearingBehavior(aClearingBehavior
)
101 virtual ~WorkerRunnable()
105 PreDispatch(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
);
108 PostDispatch(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
,
109 bool aDispatchResult
);
115 WorkerRun(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
) = 0;
118 PostRun(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
, bool aRunResult
);
124 class WorkerSyncRunnable
: public WorkerRunnable
127 uint32_t mSyncQueueKey
;
128 bool mBypassSyncQueue
;
131 friend class WorkerPrivate
;
133 WorkerSyncRunnable(WorkerPrivate
* aWorkerPrivate
, uint32_t aSyncQueueKey
,
134 bool aBypassSyncQueue
= false,
135 ClearingBehavior aClearingBehavior
= SkipWhenClearing
)
136 : WorkerRunnable(aWorkerPrivate
, WorkerThread
, UnchangedBusyCount
,
138 mSyncQueueKey(aSyncQueueKey
), mBypassSyncQueue(aBypassSyncQueue
)
141 virtual ~WorkerSyncRunnable()
145 DispatchInternal() MOZ_OVERRIDE
;
148 class MainThreadSyncRunnable
: public WorkerSyncRunnable
151 MainThreadSyncRunnable(WorkerPrivate
* aWorkerPrivate
,
152 ClearingBehavior aClearingBehavior
,
153 uint32_t aSyncQueueKey
,
154 bool aBypassSyncEventQueue
)
155 : WorkerSyncRunnable(aWorkerPrivate
, aSyncQueueKey
, aBypassSyncEventQueue
,
158 AssertIsOnMainThread();
162 PreDispatch(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
) MOZ_OVERRIDE
164 AssertIsOnMainThread();
169 PostDispatch(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
,
170 bool aDispatchResult
) MOZ_OVERRIDE
172 AssertIsOnMainThread();
176 class WorkerControlRunnable
: public WorkerRunnable
179 WorkerControlRunnable(WorkerPrivate
* aWorkerPrivate
, Target aTarget
,
180 BusyBehavior aBusyBehavior
)
181 : WorkerRunnable(aWorkerPrivate
, aTarget
, aBusyBehavior
, SkipWhenClearing
)
184 virtual ~WorkerControlRunnable()
188 DispatchInternal() MOZ_OVERRIDE
;
191 // SharedMutex is a small wrapper around an (internal) reference-counted Mutex
192 // object. It exists to avoid changing a lot of code to use Mutex* instead of
196 typedef mozilla::Mutex Mutex
;
198 class RefCountedMutex
: public Mutex
201 RefCountedMutex(const char* aName
)
205 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefCountedMutex
)
212 nsRefPtr
<RefCountedMutex
> mMutex
;
215 SharedMutex(const char* aName
)
216 : mMutex(new RefCountedMutex(aName
))
219 SharedMutex(SharedMutex
& aOther
)
220 : mMutex(aOther
.mMutex
)
229 operator const Mutex
&() const
236 AssertCurrentThreadOwns() const
239 mMutex
->AssertCurrentThreadOwns();
243 template <class Derived
>
244 class WorkerPrivateParent
: public nsDOMEventTargetHelper
246 class SynchronizeAndResumeRunnable
;
263 // All of these should be released in ForgetMainThreadObjects.
264 nsCOMPtr
<nsIURI
> mBaseURI
;
265 nsCOMPtr
<nsIURI
> mResolvedScriptURI
;
266 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
267 nsCOMPtr
<nsIScriptContext
> mScriptContext
;
268 nsCOMPtr
<nsPIDOMWindow
> mWindow
;
269 nsCOMPtr
<nsIContentSecurityPolicy
> mCSP
;
270 nsCOMPtr
<nsIChannel
> mChannel
;
275 bool mReportCSPViolations
;
276 bool mXHRParamsAllowed
;
277 bool mPrincipalIsSystem
;
280 : mEvalAllowed(false), mReportCSPViolations(false),
281 mXHRParamsAllowed(false), mPrincipalIsSystem(false)
285 StealFrom(LoadInfo
& aOther
)
287 mBaseURI
= aOther
.mBaseURI
.forget();
288 mResolvedScriptURI
= aOther
.mResolvedScriptURI
.forget();
289 mPrincipal
= aOther
.mPrincipal
.forget();
290 mScriptContext
= aOther
.mScriptContext
.forget();
291 mWindow
= aOther
.mWindow
.forget();
292 mCSP
= aOther
.mCSP
.forget();
293 mChannel
= aOther
.mChannel
.forget();
294 mDomain
= aOther
.mDomain
;
295 mEvalAllowed
= aOther
.mEvalAllowed
;
296 mReportCSPViolations
= aOther
.mReportCSPViolations
;
297 mXHRParamsAllowed
= aOther
.mXHRParamsAllowed
;
298 mPrincipalIsSystem
= aOther
.mPrincipalIsSystem
;
309 typedef mozilla::ErrorResult ErrorResult
;
312 mozilla::CondVar mCondVar
;
313 mozilla::CondVar mMemoryReportCondVar
;
316 WorkerPrivate
* mParent
;
318 nsString mSharedWorkerName
;
319 LocationInfo mLocationInfo
;
320 // The lifetime of these objects within LoadInfo is managed explicitly;
321 // they do not need to be cycle collected.
324 // Only used for top level workers.
325 nsTArray
<nsRefPtr
<WorkerRunnable
> > mQueuedRunnables
;
326 nsRevocableEventPtr
<SynchronizeAndResumeRunnable
> mSynchronizeRunnable
;
328 // Only for ChromeWorkers without window and only touched on the main thread.
329 nsTArray
<nsCString
> mHostObjectURIs
;
331 // Protected by mMutex.
332 JSSettings mJSSettings
;
334 // Only touched on the parent thread (currently this is always the main
335 // thread as SharedWorkers are always top-level).
336 nsDataHashtable
<nsUint64HashKey
, SharedWorker
*> mSharedWorkers
;
339 uint64_t mMessagePortSerial
;
340 Status mParentStatus
;
342 bool mParentSuspended
;
343 bool mIsChromeWorker
;
344 bool mMainThreadObjectsForgotten
;
345 WorkerType mWorkerType
;
348 WorkerPrivateParent(JSContext
* aCx
, WorkerPrivate
* aParent
,
349 const nsAString
& aScriptURL
, bool aIsChromeWorker
,
350 WorkerType aWorkerType
,
351 const nsAString
& aSharedWorkerName
, LoadInfo
& aLoadInfo
);
353 ~WorkerPrivateParent();
357 ParentAsWorkerPrivate() const
359 return static_cast<Derived
*>(const_cast<WorkerPrivateParent
*>(this));
362 // aCx is null when called from the finalizer
364 NotifyPrivate(JSContext
* aCx
, Status aStatus
);
366 // aCx is null when called from the finalizer
368 TerminatePrivate(JSContext
* aCx
)
370 return NotifyPrivate(aCx
, Terminating
);
374 PostMessageInternal(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
375 const Optional
<Sequence
<JS::Value
> >& aTransferable
,
376 bool aToMessagePort
, uint64_t aMessagePortSerial
,
382 WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aScope
) MOZ_OVERRIDE
;
384 NS_DECL_ISUPPORTS_INHERITED
385 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerPrivateParent
,
386 nsDOMEventTargetHelper
)
388 // May be called on any thread...
392 // Called on the parent thread.
394 Notify(JSContext
* aCx
, Status aStatus
)
396 return NotifyPrivate(aCx
, aStatus
);
400 Cancel(JSContext
* aCx
)
402 return Notify(aCx
, Canceling
);
408 return Notify(aCx
, Killing
);
412 Suspend(JSContext
* aCx
, nsPIDOMWindow
* aWindow
);
415 Resume(JSContext
* aCx
, nsPIDOMWindow
* aWindow
);
418 SynchronizeAndResume(JSContext
* aCx
, nsPIDOMWindow
* aWindow
,
419 nsIScriptContext
* aScriptContext
);
422 _finalize(JSFreeOp
* aFop
);
425 Finish(JSContext
* aCx
)
431 Terminate(JSContext
* aCx
)
433 AssertIsOnParentThread();
435 return TerminatePrivate(aCx
);
439 Close(JSContext
* aCx
);
442 ModifyBusyCount(JSContext
* aCx
, bool aIncrease
);
448 ForgetMainThreadObjects(nsTArray
<nsCOMPtr
<nsISupports
> >& aDoomed
);
451 PostMessage(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
452 const Optional
<Sequence
<JS::Value
> >& aTransferable
,
455 PostMessageInternal(aCx
, aMessage
, aTransferable
, false, 0, aRv
);
459 PostMessageToMessagePort(JSContext
* aCx
,
460 uint64_t aMessagePortSerial
,
461 JS::Handle
<JS::Value
> aMessage
,
462 const Optional
<Sequence
<JS::Value
> >& aTransferable
,
466 DispatchMessageEventToMessagePort(
468 uint64_t aMessagePortSerial
,
469 JSAutoStructuredCloneBuffer
& aBuffer
,
470 nsTArray
<nsCOMPtr
<nsISupports
>>& aClonedObjects
);
476 UpdateJSContextOptions(JSContext
* aCx
, const JS::ContextOptions
& aChromeOptions
,
477 const JS::ContextOptions
& aContentOptions
);
480 UpdatePreference(JSContext
* aCx
, WorkerPreference aPref
, bool aValue
);
483 UpdateJSWorkerMemoryParameter(JSContext
* aCx
, JSGCParamKey key
,
488 UpdateGCZeal(JSContext
* aCx
, uint8_t aGCZeal
, uint32_t aFrequency
);
492 UpdateJITHardening(JSContext
* aCx
, bool aJITHardening
);
495 GarbageCollect(JSContext
* aCx
, bool aShrinking
);
498 CycleCollect(JSContext
* aCx
, bool aDummy
);
501 RegisterSharedWorker(JSContext
* aCx
, SharedWorker
* aSharedWorker
);
504 UnregisterSharedWorker(JSContext
* aCx
, SharedWorker
* aSharedWorker
);
507 BroadcastErrorToSharedWorkers(JSContext
* aCx
,
508 const nsAString
& aMessage
,
509 const nsAString
& aFilename
,
510 const nsAString
& aLine
,
511 uint32_t aLineNumber
,
512 uint32_t aColumnNumber
,
516 WorkerScriptLoaded();
519 QueueRunnable(WorkerRunnable
* aRunnable
)
521 AssertIsOnMainThread();
522 mQueuedRunnables
.AppendElement(aRunnable
);
534 AssertIsOnParentThread();
535 return mParentSuspended
;
541 AssertIsOnParentThread();
542 bool acceptingEvents
;
544 mozilla::MutexAutoLock
lock(mMutex
);
545 acceptingEvents
= mParentStatus
< Terminating
;
547 return acceptingEvents
;
553 mMutex
.AssertCurrentThreadOwns();
554 return mParentStatus
;
558 ParentJSContext() const;
561 GetScriptContext() const
563 AssertIsOnMainThread();
564 return mLoadInfo
.mScriptContext
;
576 return mLoadInfo
.mDomain
;
582 AssertIsOnMainThread();
583 return mLoadInfo
.mBaseURI
;
587 SetBaseURI(nsIURI
* aBaseURI
);
590 GetResolvedScriptURI() const
592 AssertIsOnMainThread();
593 return mLoadInfo
.mResolvedScriptURI
;
599 AssertIsOnMainThread();
600 return mLoadInfo
.mPrincipal
;
604 SetPrincipal(nsIPrincipal
* aPrincipal
);
607 UsesSystemPrincipal() const
609 return mLoadInfo
.mPrincipalIsSystem
;
612 already_AddRefed
<nsIChannel
>
613 ForgetWorkerChannel()
615 AssertIsOnMainThread();
616 return mLoadInfo
.mChannel
.forget();
622 AssertIsOnMainThread();
623 return mLoadInfo
.mWindow
? mLoadInfo
.mWindow
->GetExtantDoc() : nullptr;
629 AssertIsOnMainThread();
630 return mLoadInfo
.mWindow
;
633 nsIContentSecurityPolicy
*
636 AssertIsOnMainThread();
637 return mLoadInfo
.mCSP
;
641 SetCSP(nsIContentSecurityPolicy
* aCSP
)
643 AssertIsOnMainThread();
644 mLoadInfo
.mCSP
= aCSP
;
648 IsEvalAllowed() const
650 return mLoadInfo
.mEvalAllowed
;
654 SetEvalAllowed(bool aEvalAllowed
)
656 mLoadInfo
.mEvalAllowed
= aEvalAllowed
;
660 GetReportCSPViolations() const
662 return mLoadInfo
.mReportCSPViolations
;
666 XHRParamsAllowed() const
668 return mLoadInfo
.mXHRParamsAllowed
;
672 SetXHRParamsAllowed(bool aAllowed
)
674 mLoadInfo
.mXHRParamsAllowed
= aAllowed
;
680 return mLocationInfo
;
684 CopyJSSettings(JSSettings
& aSettings
)
686 mozilla::MutexAutoLock
lock(mMutex
);
687 aSettings
= mJSSettings
;
690 // The ability to be a chrome worker is orthogonal to the type of
691 // worker [Dedicated|Shared].
693 IsChromeWorker() const
695 return mIsChromeWorker
;
699 IsDedicatedWorker() const
701 return mWorkerType
== WorkerTypeDedicated
;
705 IsSharedWorker() const
707 return mWorkerType
== WorkerTypeShared
;
711 SharedWorkerName() const
713 return mSharedWorkerName
;
717 NextMessagePortSerial()
719 AssertIsOnMainThread();
720 return mMessagePortSerial
++;
724 GetAllSharedWorkers(nsTArray
<nsRefPtr
<SharedWorker
>>& aSharedWorkers
);
727 CloseSharedWorkersForWindow(nsPIDOMWindow
* aWindow
);
730 RegisterHostObjectURI(const nsACString
& aURI
);
733 UnregisterHostObjectURI(const nsACString
& aURI
);
736 StealHostObjectURIs(nsTArray
<nsCString
>& aArray
);
738 IMPL_EVENT_HANDLER(message
)
739 IMPL_EVENT_HANDLER(error
)
743 AssertIsOnParentThread() const;
746 AssertInnerWindowIsCorrect() const;
749 AssertIsOnParentThread() const
753 AssertInnerWindowIsCorrect() const
758 class WorkerPrivate
: public WorkerPrivateParent
<WorkerPrivate
>
760 friend class WorkerPrivateParent
<WorkerPrivate
>;
761 typedef WorkerPrivateParent
<WorkerPrivate
> ParentType
;
765 typedef Queue
<WorkerRunnable
*, 50> EventQueue
;
767 EventQueue mControlQueue
;
771 Queue
<WorkerRunnable
*, 10> mQueue
;
776 : mComplete(false), mResult(false)
781 WorkerRunnable
* event
;
782 while (mQueue
.Pop(event
)) {
788 class MemoryReporter
;
789 friend class MemoryReporter
;
791 nsTArray
<nsAutoPtr
<SyncQueue
> > mSyncQueues
;
793 // Touched on multiple threads, protected with mMutex.
794 JSContext
* mJSContext
;
795 nsRefPtr
<WorkerCrossThreadDispatcher
> mCrossThreadDispatcher
;
797 // Things touched on worker thread only.
798 nsRefPtr
<WorkerGlobalScope
> mScope
;
799 nsTArray
<ParentType
*> mChildWorkers
;
800 nsTArray
<WorkerFeature
*> mFeatures
;
801 nsTArray
<nsAutoPtr
<TimeoutInfo
> > mTimeouts
;
803 nsCOMPtr
<nsITimer
> mTimer
;
804 nsRefPtr
<MemoryReporter
> mMemoryReporter
;
806 nsRefPtrHashtable
<nsUint64HashKey
, MessagePort
> mWorkerPorts
;
808 mozilla::TimeStamp mKillTime
;
809 uint32_t mErrorHandlerRecursionCount
;
810 uint32_t mNextTimeoutId
;
814 bool mRunningExpiredTimeouts
;
815 bool mCloseHandlerStarted
;
816 bool mCloseHandlerFinished
;
817 bool mMemoryReporterRunning
;
818 bool mBlockedForMemoryReporter
;
821 nsCOMPtr
<nsIThread
> mThread
;
824 bool mPreferences
[WORKERPREF_COUNT
];
830 static already_AddRefed
<WorkerPrivate
>
831 Constructor(const GlobalObject
& aGlobal
, const nsAString
& aScriptURL
,
834 static already_AddRefed
<WorkerPrivate
>
835 Constructor(const GlobalObject
& aGlobal
, const nsAString
& aScriptURL
,
836 bool aIsChromeWorker
, WorkerType aWorkerType
,
837 const nsAString
& aSharedWorkerName
,
838 LoadInfo
* aLoadInfo
, ErrorResult
& aRv
);
841 WorkerAvailable(JSContext
* /* unused */, JSObject
* /* unused */);
844 GetLoadInfo(JSContext
* aCx
, nsPIDOMWindow
* aWindow
, WorkerPrivate
* aParent
,
845 const nsAString
& aScriptURL
, bool aIsChromeWorker
,
846 LoadInfo
* aLoadInfo
);
849 DoRunLoop(JSContext
* aCx
);
852 OperationCallback(JSContext
* aCx
);
855 Dispatch(WorkerRunnable
* aEvent
)
857 return Dispatch(aEvent
, &mQueue
);
861 Dispatch(WorkerSyncRunnable
* aEvent
)
863 if (aEvent
->mBypassSyncQueue
) {
864 return Dispatch(aEvent
, &mQueue
);
867 return DispatchToSyncQueue(aEvent
);
871 Dispatch(WorkerControlRunnable
* aEvent
)
873 return Dispatch(aEvent
, &mControlQueue
);
877 CloseInternal(JSContext
* aCx
)
879 AssertIsOnWorkerThread();
880 return NotifyInternal(aCx
, Closing
);
884 SuspendInternal(JSContext
* aCx
);
887 ResumeInternal(JSContext
* aCx
);
890 TraceTimeouts(const TraceCallbacks
& aCallbacks
, void* aClosure
) const;
893 ModifyBusyCountFromWorker(JSContext
* aCx
, bool aIncrease
);
896 AddChildWorker(JSContext
* aCx
, ParentType
* aChildWorker
);
899 RemoveChildWorker(JSContext
* aCx
, ParentType
* aChildWorker
);
902 AddFeature(JSContext
* aCx
, WorkerFeature
* aFeature
);
905 RemoveFeature(JSContext
* aCx
, WorkerFeature
* aFeature
);
908 NotifyFeatures(JSContext
* aCx
, Status aStatus
);
913 return !(mChildWorkers
.IsEmpty() && mTimeouts
.IsEmpty() &&
914 mFeatures
.IsEmpty());
921 RunSyncLoop(JSContext
* aCx
, uint32_t aSyncLoopKey
);
924 StopSyncLoop(uint32_t aSyncLoopKey
, bool aSyncResult
);
927 DestroySyncLoop(uint32_t aSyncLoopKey
);
930 PostMessageToParent(JSContext
* aCx
,
931 JS::Handle
<JS::Value
> aMessage
,
932 const Optional
<Sequence
<JS::Value
>>& aTransferable
,
935 PostMessageToParentInternal(aCx
, aMessage
, aTransferable
, false, 0, aRv
);
939 PostMessageToParentMessagePort(
941 uint64_t aMessagePortSerial
,
942 JS::Handle
<JS::Value
> aMessage
,
943 const Optional
<Sequence
<JS::Value
>>& aTransferable
,
947 NotifyInternal(JSContext
* aCx
, Status aStatus
);
950 ReportError(JSContext
* aCx
, const char* aMessage
, JSErrorReport
* aReport
);
953 SetTimeout(JSContext
* aCx
,
955 const nsAString
& aStringHandler
,
957 const Sequence
<JS::Value
>& aArguments
,
962 ClearTimeout(int32_t aId
);
965 RunExpiredTimeouts(JSContext
* aCx
);
968 RescheduleTimeoutTimer(JSContext
* aCx
);
971 CloseHandlerStarted()
973 AssertIsOnWorkerThread();
974 mCloseHandlerStarted
= true;
978 CloseHandlerFinished()
980 AssertIsOnWorkerThread();
981 mCloseHandlerFinished
= true;
985 UpdateJSContextOptionsInternal(JSContext
* aCx
, const JS::ContextOptions
& aContentOptions
,
986 const JS::ContextOptions
& aChromeOptions
);
989 UpdatePreferenceInternal(JSContext
* aCx
, WorkerPreference aPref
, bool aValue
);
992 UpdateJSWorkerMemoryParameterInternal(JSContext
* aCx
, JSGCParamKey key
, uint32_t aValue
);
995 ScheduleDeletion(bool aWasPending
);
998 BlockAndCollectRuntimeStats(JS::RuntimeStats
* aRtStats
);
1002 UpdateGCZealInternal(JSContext
* aCx
, uint8_t aGCZeal
, uint32_t aFrequency
);
1006 UpdateJITHardeningInternal(JSContext
* aCx
, bool aJITHardening
);
1009 GarbageCollectInternal(JSContext
* aCx
, bool aShrinking
,
1010 bool aCollectChildren
);
1013 CycleCollectInternal(JSContext
* aCx
, bool aCollectChildren
);
1016 GetJSContext() const
1018 AssertIsOnWorkerThread();
1025 AssertIsOnWorkerThread();
1031 AssertIsOnWorkerThread() const;
1034 SetThread(nsIThread
* aThread
)
1040 AssertIsOnWorkerThread() const
1044 WorkerCrossThreadDispatcher
*
1045 GetCrossThreadDispatcher();
1056 BeginCTypesCallback()
1058 // If a callback is beginning then we need to do the exact same thing as
1059 // when a ctypes call ends.
1066 // If a callback is ending then we need to do the exact same thing as
1067 // when a ctypes call begins.
1072 ConnectMessagePort(JSContext
* aCx
, uint64_t aMessagePortSerial
);
1075 DisconnectMessagePort(uint64_t aMessagePortSerial
);
1078 GetMessagePort(uint64_t aMessagePortSerial
);
1081 CreateGlobalScope(JSContext
* aCx
);
1084 RegisterBindings(JSContext
* aCx
, JS::Handle
<JSObject
*> aGlobal
);
1089 AssertIsOnWorkerThread();
1090 return mPreferences
[WORKERPREF_DUMP
];
1094 PromiseEnabled() const
1096 AssertIsOnWorkerThread();
1097 return mPreferences
[WORKERPREF_PROMISE
];
1101 WorkerPrivate(JSContext
* aCx
, WorkerPrivate
* aParent
,
1102 const nsAString
& aScriptURL
, bool aIsChromeWorker
,
1103 WorkerType aWorkerType
, const nsAString
& aSharedWorkerName
,
1104 LoadInfo
& aLoadInfo
);
1107 Dispatch(WorkerRunnable
* aEvent
, EventQueue
* aQueue
);
1110 DispatchToSyncQueue(WorkerSyncRunnable
* aEvent
);
1113 ClearQueue(EventQueue
* aQueue
);
1116 MayContinueRunning()
1118 AssertIsOnWorkerThread();
1122 mozilla::MutexAutoLock
lock(mMutex
);
1126 if (status
>= Killing
) {
1129 if (status
>= Running
) {
1130 return mKillTime
.IsNull() || RemainingRunTimeMS() > 0;
1136 RemainingRunTimeMS() const;
1139 CancelAllTimeouts(JSContext
* aCx
);
1142 ScheduleKillCloseEventRunnable(JSContext
* aCx
);
1145 StopAcceptingEvents()
1147 AssertIsOnWorkerThread();
1149 mozilla::MutexAutoLock
lock(mMutex
);
1152 mJSContext
= nullptr;
1154 ClearQueue(&mControlQueue
);
1155 ClearQueue(&mQueue
);
1159 ProcessAllControlRunnables();
1162 EnableMemoryReporter();
1165 DisableMemoryReporter();
1168 WaitForWorkerEvents(PRIntervalTime interval
= PR_INTERVAL_NO_TIMEOUT
);
1171 PostMessageToParentInternal(JSContext
* aCx
,
1172 JS::Handle
<JS::Value
> aMessage
,
1173 const Optional
<Sequence
<JS::Value
>>& aTransferable
,
1174 bool aToMessagePort
,
1175 uint64_t aMessagePortSerial
,
1179 GetAllPreferences(bool aPreferences
[WORKERPREF_COUNT
]) const
1181 AssertIsOnWorkerThread();
1182 memcpy(aPreferences
, mPreferences
, WORKERPREF_COUNT
* sizeof(bool));
1186 // This class is only used to trick the DOM bindings. We never create
1187 // instances of it, and static_casting to it is fine since it doesn't add
1188 // anything to WorkerPrivate.
1189 class ChromeWorkerPrivate
: public WorkerPrivate
1192 static already_AddRefed
<ChromeWorkerPrivate
>
1193 Constructor(const GlobalObject
& aGlobal
, const nsAString
& aScriptURL
,
1197 WorkerAvailable(JSContext
* /* unused */, JSObject
* /* unused */);
1200 ChromeWorkerPrivate() MOZ_DELETE
;
1201 ChromeWorkerPrivate(const ChromeWorkerPrivate
& aRHS
) MOZ_DELETE
;
1202 ChromeWorkerPrivate
& operator =(const ChromeWorkerPrivate
& aRHS
) MOZ_DELETE
;
1206 GetWorkerPrivateFromContext(JSContext
* aCx
);
1209 GetCurrentThreadWorkerPrivate();
1212 IsCurrentThreadRunningChromeWorker();
1215 GetCurrentThreadJSContext();
1217 enum WorkerStructuredDataType
1219 DOMWORKER_SCTAG_FILE
= SCTAG_DOM_MAX
,
1220 DOMWORKER_SCTAG_BLOB
,
1225 JSStructuredCloneCallbacks
*
1226 WorkerStructuredCloneCallbacks(bool aMainRuntime
);
1228 JSStructuredCloneCallbacks
*
1229 ChromeWorkerStructuredCloneCallbacks(bool aMainRuntime
);
1231 class AutoSyncLoopHolder
1234 AutoSyncLoopHolder(WorkerPrivate
* aWorkerPrivate
)
1235 : mWorkerPrivate(aWorkerPrivate
), mSyncLoopKey(UINT32_MAX
)
1237 mSyncLoopKey
= mWorkerPrivate
->CreateNewSyncLoop();
1240 ~AutoSyncLoopHolder()
1242 if (mWorkerPrivate
) {
1243 mWorkerPrivate
->StopSyncLoop(mSyncLoopKey
, false);
1244 mWorkerPrivate
->DestroySyncLoop(mSyncLoopKey
);
1249 RunAndForget(JSContext
* aCx
)
1251 WorkerPrivate
* workerPrivate
= mWorkerPrivate
;
1252 mWorkerPrivate
= nullptr;
1253 return workerPrivate
->RunSyncLoop(aCx
, mSyncLoopKey
);
1257 SyncQueueKey() const
1259 return mSyncLoopKey
;
1263 WorkerPrivate
* mWorkerPrivate
;
1264 uint32_t mSyncLoopKey
;
1267 END_WORKERS_NAMESPACE
1269 #endif /* mozilla_dom_workers_workerprivate_h__ */