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/. */
9 #include "MessageEventRunnable.h"
10 #include "mozilla/dom/WorkerBinding.h"
11 #include "mozilla/ProfilerLabels.h"
12 #include "mozilla/ProfilerMarkers.h"
13 #include "mozilla/TimelineConsumers.h"
14 #include "mozilla/Unused.h"
15 #include "mozilla/WorkerTimelineMarker.h"
16 #include "nsContentUtils.h"
17 #include "nsGlobalWindowOuter.h"
18 #include "WorkerPrivate.h"
24 namespace mozilla::dom
{
27 already_AddRefed
<Worker
> Worker::Constructor(const GlobalObject
& aGlobal
,
28 const nsAString
& aScriptURL
,
29 const WorkerOptions
& aOptions
,
31 JSContext
* cx
= aGlobal
.Context();
33 nsCOMPtr
<nsIGlobalObject
> globalObject
=
34 do_QueryInterface(aGlobal
.GetAsSupports());
36 if (globalObject
->AsInnerWindow() &&
37 !globalObject
->AsInnerWindow()->IsCurrentInnerWindow()) {
38 aRv
.ThrowInvalidStateError(
39 "Cannot create worker for a going to be discarded document");
43 RefPtr
<WorkerPrivate
> workerPrivate
= WorkerPrivate::Constructor(
44 cx
, aScriptURL
, false /* aIsChromeWorker */, WorkerKindDedicated
,
45 aOptions
.mName
, VoidCString(), nullptr /*aLoadInfo */, aRv
);
46 if (NS_WARN_IF(aRv
.Failed())) {
50 RefPtr
<Worker
> worker
= new Worker(globalObject
, workerPrivate
.forget());
51 return worker
.forget();
54 Worker::Worker(nsIGlobalObject
* aGlobalObject
,
55 already_AddRefed
<WorkerPrivate
> aWorkerPrivate
)
56 : DOMEventTargetHelper(aGlobalObject
),
57 mWorkerPrivate(std::move(aWorkerPrivate
)) {
58 MOZ_ASSERT(mWorkerPrivate
);
59 mWorkerPrivate
->SetParentEventTargetRef(this);
62 Worker::~Worker() { Terminate(); }
64 JSObject
* Worker::WrapObject(JSContext
* aCx
,
65 JS::Handle
<JSObject
*> aGivenProto
) {
66 JS::Rooted
<JSObject
*> wrapper(aCx
,
67 Worker_Binding::Wrap(aCx
, this, aGivenProto
));
69 // Most DOM objects don't assume they have a reflector. If they don't have
70 // one and need one, they create it. But in workers code, we assume that the
71 // reflector is always present. In order to guarantee that it's always
72 // present, we have to preserve it. Otherwise the GC will happily collect it
74 MOZ_ALWAYS_TRUE(TryPreserveWrapper(wrapper
));
80 void Worker::PostMessage(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
81 const Sequence
<JSObject
*>& aTransferable
,
83 NS_ASSERT_OWNINGTHREAD(Worker
);
85 if (!mWorkerPrivate
|| mWorkerPrivate
->ParentStatusProtected() > Running
) {
89 JS::Rooted
<JS::Value
> transferable(aCx
, JS::UndefinedValue());
91 aRv
= nsContentUtils::CreateJSValueFromSequenceOfObject(aCx
, aTransferable
,
93 if (NS_WARN_IF(aRv
.Failed())) {
97 NS_ConvertUTF16toUTF8
nameOrScriptURL(mWorkerPrivate
->WorkerName().IsEmpty()
98 ? mWorkerPrivate
->ScriptURL()
99 : mWorkerPrivate
->WorkerName());
100 AUTO_PROFILER_MARKER_TEXT("Worker.postMessage", DOM
, {}, nameOrScriptURL
);
101 uint32_t flags
= uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS
);
102 if (mWorkerPrivate
->IsChromeWorker()) {
103 flags
|= uint32_t(js::ProfilingStackFrame::Flags::NONSENSITIVE
);
105 mozilla::AutoProfilerLabel
PROFILER_RAII(
106 "Worker.postMessage", nameOrScriptURL
.get(),
107 JS::ProfilingCategoryPair::DOM
, flags
);
109 RefPtr
<MessageEventRunnable
> runnable
= new MessageEventRunnable(
110 mWorkerPrivate
, WorkerRunnable::WorkerThreadModifyBusyCount
);
112 UniquePtr
<AbstractTimelineMarker
> start
;
113 UniquePtr
<AbstractTimelineMarker
> end
;
114 bool isTimelineRecording
= !TimelineConsumers::IsEmpty();
116 if (isTimelineRecording
) {
117 start
= MakeUnique
<WorkerTimelineMarker
>(
119 ? ProfileTimelineWorkerOperationType::SerializeDataOnMainThread
120 : ProfileTimelineWorkerOperationType::SerializeDataOffMainThread
,
121 MarkerTracingType::START
);
124 JS::CloneDataPolicy clonePolicy
;
125 // DedicatedWorkers are always part of the same agent cluster.
126 clonePolicy
.allowIntraClusterClonableSharedObjects();
128 if (NS_IsMainThread()) {
129 nsGlobalWindowInner
* win
= nsContentUtils::CallerInnerWindow();
130 if (win
&& win
->IsSharedMemoryAllowed()) {
131 clonePolicy
.allowSharedMemoryObjects();
134 WorkerPrivate
* worker
= GetCurrentThreadWorkerPrivate();
135 if (worker
&& worker
->IsSharedMemoryAllowed()) {
136 clonePolicy
.allowSharedMemoryObjects();
140 runnable
->Write(aCx
, aMessage
, transferable
, clonePolicy
, aRv
);
142 if (isTimelineRecording
) {
143 end
= MakeUnique
<WorkerTimelineMarker
>(
145 ? ProfileTimelineWorkerOperationType::SerializeDataOnMainThread
146 : ProfileTimelineWorkerOperationType::SerializeDataOffMainThread
,
147 MarkerTracingType::END
);
148 TimelineConsumers::AddMarkerForAllObservedDocShells(start
);
149 TimelineConsumers::AddMarkerForAllObservedDocShells(end
);
152 if (NS_WARN_IF(aRv
.Failed())) {
156 // The worker could have closed between the time we entered this function and
157 // checked ParentStatusProtected and now, which could cause the dispatch to
159 Unused
<< NS_WARN_IF(!runnable
->Dispatch());
162 void Worker::PostMessage(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
163 const StructuredSerializeOptions
& aOptions
,
165 PostMessage(aCx
, aMessage
, aOptions
.mTransfer
, aRv
);
168 void Worker::Terminate() {
169 NS_ASSERT_OWNINGTHREAD(Worker
);
171 if (mWorkerPrivate
) {
172 mWorkerPrivate
->Cancel();
173 mWorkerPrivate
= nullptr;
177 NS_IMPL_CYCLE_COLLECTION_CLASS(Worker
)
179 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Worker
, DOMEventTargetHelper
)
180 if (tmp
->mWorkerPrivate
) {
181 tmp
->mWorkerPrivate
->Traverse(cb
);
183 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
185 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Worker
, DOMEventTargetHelper
)
187 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR
188 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
190 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(Worker
, DOMEventTargetHelper
)
191 NS_IMPL_CYCLE_COLLECTION_TRACE_END
193 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Worker
)
194 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper
)
196 NS_IMPL_ADDREF_INHERITED(Worker
, DOMEventTargetHelper
)
197 NS_IMPL_RELEASE_INHERITED(Worker
, DOMEventTargetHelper
)
199 } // namespace mozilla::dom