summaryrefslogtreecommitdiffstats
path: root/dom/workers/Worker.cpp
blob: 88df53b8779640dd91779a503bf09ac01a3bdce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Worker.h"

#include "MessageEventRunnable.h"
#include "mozilla/dom/WorkerBinding.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/ProfilerMarkers.h"
#include "mozilla/Unused.h"
#include "nsContentUtils.h"
#include "nsGlobalWindowInner.h"
#include "WorkerPrivate.h"
#include "EventWithOptionsRunnable.h"
#include "js/RootingAPI.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "nsISupports.h"
#include "nsDebug.h"
#include "mozilla/dom/WorkerStatus.h"
#include "mozilla/RefPtr.h"

#ifdef XP_WIN
#  undef PostMessage
#endif

namespace mozilla::dom {

/* static */
already_AddRefed<Worker> Worker::Constructor(const GlobalObject& aGlobal,
                                             const nsAString& aScriptURL,
                                             const WorkerOptions& aOptions,
                                             ErrorResult& aRv) {
  JSContext* cx = aGlobal.Context();

  nsCOMPtr<nsIGlobalObject> globalObject =
      do_QueryInterface(aGlobal.GetAsSupports());

  if (globalObject->GetAsInnerWindow() &&
      !globalObject->GetAsInnerWindow()->IsCurrentInnerWindow()) {
    aRv.ThrowInvalidStateError(
        "Cannot create worker for a going to be discarded document");
    return nullptr;
  }

  RefPtr<WorkerPrivate> workerPrivate = WorkerPrivate::Constructor(
      cx, aScriptURL, false /* aIsChromeWorker */, WorkerKindDedicated,
      aOptions.mCredentials, aOptions.mType, aOptions.mName, VoidCString(),
      nullptr /*aLoadInfo */, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  RefPtr<Worker> worker = new Worker(globalObject, workerPrivate.forget());
  return worker.forget();
}

Worker::Worker(nsIGlobalObject* aGlobalObject,
               already_AddRefed<WorkerPrivate> aWorkerPrivate)
    : DOMEventTargetHelper(aGlobalObject),
      mWorkerPrivate(std::move(aWorkerPrivate)) {
  MOZ_ASSERT(mWorkerPrivate);
  mWorkerPrivate->SetParentEventTargetRef(this);
}

Worker::~Worker() { Terminate(); }

JSObject* Worker::WrapObject(JSContext* aCx,
                             JS::Handle<JSObject*> aGivenProto) {
  JS::Rooted<JSObject*> wrapper(aCx,
                                Worker_Binding::Wrap(aCx, this, aGivenProto));
  if (wrapper) {
    // Most DOM objects don't assume they have a reflector. If they don't have
    // one and need one, they create it. But in workers code, we assume that the
    // reflector is always present.  In order to guarantee that it's always
    // present, we have to preserve it. Otherwise the GC will happily collect it
    // as needed.
    MOZ_ALWAYS_TRUE(TryPreserveWrapper(wrapper));
  }

  return wrapper;
}

bool Worker::IsEligibleForMessaging() {
  NS_ASSERT_OWNINGTHREAD(Worker);

  return mWorkerPrivate && mWorkerPrivate->ParentStatusProtected() <= Running;
}

void Worker::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
                         const Sequence<JSObject*>& aTransferable,
                         ErrorResult& aRv) {
  NS_ASSERT_OWNINGTHREAD(Worker);

  if (!mWorkerPrivate || mWorkerPrivate->ParentStatusProtected() > Running) {
    return;
  }
  RefPtr<WorkerPrivate> workerPrivate = mWorkerPrivate;
  Unused << workerPrivate;

  JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());

  aRv = nsContentUtils::CreateJSValueFromSequenceOfObject(aCx, aTransferable,
                                                          &transferable);
  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }

  NS_ConvertUTF16toUTF8 nameOrScriptURL(
      mWorkerPrivate->WorkerName().IsEmpty()
          ? Substring(
                mWorkerPrivate->ScriptURL(), 0,
                std::min(size_t(1024), mWorkerPrivate->ScriptURL().Length()))
          : Substring(
                mWorkerPrivate->WorkerName(), 0,
                std::min(size_t(1024), mWorkerPrivate->WorkerName().Length())));
  AUTO_PROFILER_MARKER_TEXT("Worker.postMessage", DOM, {}, nameOrScriptURL);
  uint32_t flags = uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS);
  if (mWorkerPrivate->IsChromeWorker()) {
    flags |= uint32_t(js::ProfilingStackFrame::Flags::NONSENSITIVE);
  }
  mozilla::AutoProfilerLabel PROFILER_RAII(
      "Worker.postMessage", nameOrScriptURL.get(),
      JS::ProfilingCategoryPair::DOM, flags);

  RefPtr<MessageEventRunnable> runnable =
      new MessageEventRunnable(mWorkerPrivate, WorkerRunnable::WorkerThread);

  JS::CloneDataPolicy clonePolicy;
  // DedicatedWorkers are always part of the same agent cluster.
  clonePolicy.allowIntraClusterClonableSharedObjects();

  if (NS_IsMainThread()) {
    nsGlobalWindowInner* win = nsContentUtils::IncumbentInnerWindow();
    if (win && win->IsSharedMemoryAllowed()) {
      clonePolicy.allowSharedMemoryObjects();
    }
  } else {
    WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
    if (worker && worker->IsSharedMemoryAllowed()) {
      clonePolicy.allowSharedMemoryObjects();
    }
  }

  runnable->Write(aCx, aMessage, transferable, clonePolicy, aRv);

  if (!mWorkerPrivate || mWorkerPrivate->ParentStatusProtected() > Running) {
    return;
  }

  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }

  // The worker could have closed between the time we entered this function and
  // checked ParentStatusProtected and now, which could cause the dispatch to
  // fail.
  Unused << NS_WARN_IF(!runnable->Dispatch());
}

void Worker::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
                         const StructuredSerializeOptions& aOptions,
                         ErrorResult& aRv) {
  PostMessage(aCx, aMessage, aOptions.mTransfer, aRv);
}

void Worker::PostEventWithOptions(JSContext* aCx,
                                  JS::Handle<JS::Value> aOptions,
                                  const Sequence<JSObject*>& aTransferable,
                                  EventWithOptionsRunnable* aRunnable,
                                  ErrorResult& aRv) {
  NS_ASSERT_OWNINGTHREAD(Worker);

  if (NS_WARN_IF(!mWorkerPrivate ||
                 mWorkerPrivate->ParentStatusProtected() > Running)) {
    return;
  }
  RefPtr<WorkerPrivate> workerPrivate = mWorkerPrivate;
  Unused << workerPrivate;

  aRunnable->InitOptions(aCx, aOptions, aTransferable, aRv);

  if (NS_WARN_IF(!mWorkerPrivate ||
                 mWorkerPrivate->ParentStatusProtected() > Running)) {
    return;
  }

  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }

  Unused << NS_WARN_IF(!aRunnable->Dispatch());
}

void Worker::Terminate() {
  NS_ASSERT_OWNINGTHREAD(Worker);

  if (mWorkerPrivate) {
    mWorkerPrivate->Cancel();
    mWorkerPrivate = nullptr;
  }
}

NS_IMPL_CYCLE_COLLECTION_CLASS(Worker)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(Worker, DOMEventTargetHelper)
  if (tmp->mWorkerPrivate) {
    tmp->mWorkerPrivate->Traverse(cb);
  }
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(Worker, DOMEventTargetHelper)
  tmp->Terminate();
  NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(Worker, DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Worker)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

NS_IMPL_ADDREF_INHERITED(Worker, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(Worker, DOMEventTargetHelper)

}  // namespace mozilla::dom