Line data Source code
1 : /* THIS FILE IS AUTOGENERATED FROM Worker.webidl BY Codegen.py - DO NOT EDIT */
2 :
3 : #include "AtomList.h"
4 : #include "EventHandlerBinding.h"
5 : #include "EventTargetBinding.h"
6 : #include "WorkerBinding.h"
7 : #include "WrapperFactory.h"
8 : #include "mozilla/OwningNonNull.h"
9 : #include "mozilla/dom/BindingUtils.h"
10 : #include "mozilla/dom/DOMJSClass.h"
11 : #include "mozilla/dom/NonRefcountedDOMObject.h"
12 : #include "mozilla/dom/Nullable.h"
13 : #include "mozilla/dom/ScriptSettings.h"
14 : #include "mozilla/dom/SimpleGlobalObject.h"
15 : #include "mozilla/dom/WorkerPrivate.h"
16 : #include "mozilla/dom/XrayExpandoClass.h"
17 : #include "nsThreadUtils.h"
18 :
19 : namespace mozilla {
20 : namespace dom {
21 :
22 :
23 0 : WorkerOptions::WorkerOptions()
24 : {
25 : // Safe to pass a null context if we pass a null value
26 0 : Init(nullptr, JS::NullHandleValue);
27 0 : }
28 :
29 :
30 :
31 : bool
32 0 : WorkerOptions::InitIds(JSContext* cx, WorkerOptionsAtoms* atomsCache)
33 : {
34 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
35 :
36 : // Initialize these in reverse order so that any failure leaves the first one
37 : // uninitialized.
38 0 : if (!atomsCache->name_id.init(cx, "name")) {
39 0 : return false;
40 : }
41 0 : return true;
42 : }
43 :
44 : bool
45 0 : WorkerOptions::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
46 : {
47 : // Passing a null JSContext is OK only if we're initing from null,
48 : // Since in that case we will not have to do any property gets
49 : // Also evaluate isNullOrUndefined in order to avoid false-positive
50 : // checkers by static analysis tools
51 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
52 0 : WorkerOptionsAtoms* atomsCache = nullptr;
53 0 : if (cx) {
54 0 : atomsCache = GetAtomCache<WorkerOptionsAtoms>(cx);
55 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
56 0 : return false;
57 : }
58 : }
59 :
60 0 : if (!IsConvertibleToDictionary(val)) {
61 0 : return ThrowErrorMessage(cx, MSG_NOT_DICTIONARY, sourceDescription);
62 : }
63 :
64 0 : bool isNull = val.isNullOrUndefined();
65 : // We only need these if !isNull, in which case we have |cx|.
66 0 : Maybe<JS::Rooted<JSObject *> > object;
67 0 : Maybe<JS::Rooted<JS::Value> > temp;
68 0 : if (!isNull) {
69 0 : MOZ_ASSERT(cx);
70 0 : object.emplace(cx, &val.toObject());
71 0 : temp.emplace(cx);
72 : }
73 0 : if (!isNull) {
74 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->name_id, temp.ptr())) {
75 0 : return false;
76 : }
77 : }
78 0 : if (!isNull && !temp->isUndefined()) {
79 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mName)) {
80 0 : return false;
81 : }
82 : } else {
83 : static const char16_t data[] = { 0 };
84 0 : mName.Rebind(data, ArrayLength(data) - 1);
85 : }
86 0 : mIsAnyMemberPresent = true;
87 0 : return true;
88 : }
89 :
90 : bool
91 0 : WorkerOptions::Init(const nsAString& aJSON)
92 : {
93 0 : AutoJSAPI jsapi;
94 0 : JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
95 0 : if (!cleanGlobal) {
96 0 : return false;
97 : }
98 0 : if (!jsapi.Init(cleanGlobal)) {
99 0 : return false;
100 : }
101 0 : JSContext* cx = jsapi.cx();
102 0 : JS::Rooted<JS::Value> json(cx);
103 0 : bool ok = ParseJSON(cx, aJSON, &json);
104 0 : NS_ENSURE_TRUE(ok, false);
105 0 : return Init(cx, json);
106 : }
107 :
108 : bool
109 0 : WorkerOptions::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
110 : {
111 0 : WorkerOptionsAtoms* atomsCache = GetAtomCache<WorkerOptionsAtoms>(cx);
112 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
113 0 : return false;
114 : }
115 :
116 0 : JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
117 0 : if (!obj) {
118 0 : return false;
119 : }
120 0 : rval.set(JS::ObjectValue(*obj));
121 :
122 : do {
123 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
124 0 : JS::Rooted<JS::Value> temp(cx);
125 0 : nsString const & currentValue = mName;
126 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
127 0 : return false;
128 : }
129 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->name_id, temp, JSPROP_ENUMERATE)) {
130 0 : return false;
131 : }
132 0 : break;
133 : } while(0);
134 :
135 0 : return true;
136 : }
137 :
138 : bool
139 0 : WorkerOptions::ToJSON(nsAString& aJSON) const
140 : {
141 0 : AutoJSAPI jsapi;
142 0 : jsapi.Init();
143 0 : JSContext *cx = jsapi.cx();
144 : // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
145 : // because we'll only be creating objects, in ways that have no
146 : // side-effects, followed by a call to JS::ToJSONMaybeSafely,
147 : // which likewise guarantees no side-effects for the sorts of
148 : // things we will pass it.
149 0 : JSAutoCompartment ac(cx, binding_detail::UnprivilegedJunkScopeOrWorkerGlobal());
150 0 : JS::Rooted<JS::Value> val(cx);
151 0 : if (!ToObjectInternal(cx, &val)) {
152 0 : return false;
153 : }
154 0 : JS::Rooted<JSObject*> obj(cx, &val.toObject());
155 0 : return StringifyToJSON(cx, obj, aJSON);
156 : }
157 :
158 : void
159 0 : WorkerOptions::TraceDictionary(JSTracer* trc)
160 : {
161 0 : }
162 :
163 : WorkerOptions&
164 0 : WorkerOptions::operator=(const WorkerOptions& aOther)
165 : {
166 0 : mName = aOther.mName;
167 0 : return *this;
168 : }
169 :
170 : namespace binding_detail {
171 : } // namespace binding_detail
172 :
173 :
174 : namespace ChromeWorkerBinding {
175 :
176 : static_assert(IsRefcounted<NativeType>::value == IsRefcounted<WorkerBinding::NativeType>::value,
177 : "Can't inherit from an interface with a different ownership model.");
178 :
179 : static bool
180 0 : _addProperty(JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::Handle<JS::Value> val)
181 : {
182 0 : mozilla::dom::workers::ChromeWorkerPrivate* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::workers::ChromeWorkerPrivate>(obj);
183 : // We don't want to preserve if we don't have a wrapper, and we
184 : // obviously can't preserve if we're not initialized.
185 0 : if (self && self->GetWrapperPreserveColor()) {
186 0 : PreserveWrapper(self);
187 : }
188 0 : return true;
189 : }
190 :
191 : static void
192 0 : _finalize(js::FreeOp* fop, JSObject* obj)
193 : {
194 0 : mozilla::dom::workers::ChromeWorkerPrivate* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::workers::ChromeWorkerPrivate>(obj);
195 0 : if (self) {
196 0 : ClearWrapper(self, self, obj);
197 0 : AddForDeferredFinalization<mozilla::dom::workers::ChromeWorkerPrivate>(self);
198 : }
199 0 : }
200 :
201 : static void
202 0 : _objectMoved(JSObject* obj, const JSObject* old)
203 : {
204 0 : mozilla::dom::workers::ChromeWorkerPrivate* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::workers::ChromeWorkerPrivate>(obj);
205 0 : if (self) {
206 0 : UpdateWrapper(self, self, obj, old);
207 : }
208 0 : }
209 :
210 : static bool
211 1 : _constructor(JSContext* cx, unsigned argc, JS::Value* vp)
212 : {
213 1 : JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
214 2 : JS::Rooted<JSObject*> obj(cx, &args.callee());
215 1 : if (!args.isConstructing()) {
216 : // XXXbz wish I could get the name from the callee instead of
217 : // Adding more relocations
218 0 : return ThrowConstructorWithoutNew(cx, "ChromeWorker");
219 : }
220 :
221 2 : GlobalObject global(cx, obj);
222 1 : if (global.Failed()) {
223 0 : return false;
224 : }
225 :
226 2 : JS::Rooted<JSObject*> desiredProto(cx);
227 1 : if (!GetDesiredProto(cx, args, &desiredProto)) {
228 0 : return false;
229 : }
230 :
231 1 : if (MOZ_UNLIKELY(args.length() < 1)) {
232 0 : return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "ChromeWorker");
233 : }
234 1 : bool objIsXray = xpc::WrapperFactory::IsXrayWrapper(obj);
235 2 : binding_detail::FakeString arg0;
236 1 : if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
237 0 : return false;
238 : }
239 1 : NormalizeUSVString(arg0);
240 2 : Maybe<JSAutoCompartment> ac;
241 1 : if (objIsXray) {
242 0 : obj = js::CheckedUnwrap(obj);
243 0 : if (!obj) {
244 0 : return false;
245 : }
246 0 : ac.emplace(cx, obj);
247 0 : if (!JS_WrapObject(cx, &desiredProto)) {
248 0 : return false;
249 : }
250 : }
251 2 : binding_detail::FastErrorResult rv;
252 2 : auto result(StrongOrRawPtr<mozilla::dom::workers::ChromeWorkerPrivate>(mozilla::dom::workers::ChromeWorkerPrivate::Constructor(global, Constify(arg0), rv)));
253 1 : if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) {
254 0 : return false;
255 : }
256 1 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
257 : static_assert(!IsPointer<decltype(result)>::value,
258 : "NewObject implies that we need to keep the object alive with a strong reference.");
259 1 : if (!GetOrCreateDOMReflector(cx, result, args.rval(), desiredProto)) {
260 0 : MOZ_ASSERT(true || JS_IsExceptionPending(cx));
261 0 : return false;
262 : }
263 1 : return true;
264 : }
265 :
266 : static const js::ClassOps sInterfaceObjectClassOps = {
267 : nullptr, /* addProperty */
268 : nullptr, /* delProperty */
269 : nullptr, /* getProperty */
270 : nullptr, /* setProperty */
271 : nullptr, /* enumerate */
272 : nullptr, /* newEnumerate */
273 : nullptr, /* resolve */
274 : nullptr, /* mayResolve */
275 : nullptr, /* finalize */
276 : _constructor, /* call */
277 : nullptr, /* hasInstance */
278 : _constructor, /* construct */
279 : nullptr, /* trace */
280 : };
281 :
282 : static const DOMIfaceAndProtoJSClass sInterfaceObjectClass = {
283 : {
284 : "Function",
285 : JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_SLOTS_BASE),
286 : &sInterfaceObjectClassOps,
287 : JS_NULL_CLASS_SPEC,
288 : JS_NULL_CLASS_EXT,
289 : &sInterfaceObjectClassObjectOps
290 : },
291 : eInterface,
292 : true,
293 : prototypes::id::ChromeWorker,
294 : PrototypeTraits<prototypes::id::ChromeWorker>::Depth,
295 : sNativePropertyHooks,
296 : "function ChromeWorker() {\n [native code]\n}",
297 : WorkerBinding::GetConstructorObject
298 : };
299 :
300 : static const DOMIfaceAndProtoJSClass sPrototypeClass = {
301 : {
302 : "ChromeWorkerPrototype",
303 : JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE),
304 : JS_NULL_CLASS_OPS,
305 : JS_NULL_CLASS_SPEC,
306 : JS_NULL_CLASS_EXT,
307 : JS_NULL_OBJECT_OPS
308 : },
309 : eInterfacePrototype,
310 : false,
311 : prototypes::id::ChromeWorker,
312 : PrototypeTraits<prototypes::id::ChromeWorker>::Depth,
313 : sNativePropertyHooks,
314 : "[object ChromeWorkerPrototype]",
315 : WorkerBinding::GetProtoObject
316 : };
317 :
318 : bool
319 4 : ConstructorEnabled(JSContext* aCx, JS::Handle<JSObject*> aObj)
320 : {
321 4 : if (!NS_IsMainThread()) {
322 1 : const char* name = js::GetObjectClass(aObj)->name;
323 1 : if (strcmp(name, "DedicatedWorkerGlobalScope") && strcmp(name, "SharedWorkerGlobalScope")) {
324 0 : return false;
325 : }
326 : }
327 :
328 4 : return mozilla::dom::workers::ChromeWorkerPrivate::WorkerAvailable(aCx, aObj);
329 : }
330 :
331 : JSObject*
332 0 : DefineDOMInterface(JSContext* aCx, JS::Handle<JSObject*> aGlobal, JS::Handle<jsid> id, bool aDefineOnGlobal)
333 : {
334 0 : return GetConstructorObjectHandle(aCx, aDefineOnGlobal);
335 : }
336 :
337 : static const js::ClassOps sClassOps = {
338 : _addProperty, /* addProperty */
339 : nullptr, /* delProperty */
340 : nullptr, /* getProperty */
341 : nullptr, /* setProperty */
342 : nullptr, /* enumerate */
343 : nullptr, /* newEnumerate */
344 : nullptr, /* resolve */
345 : nullptr, /* mayResolve */
346 : _finalize, /* finalize */
347 : nullptr, /* call */
348 : nullptr, /* hasInstance */
349 : nullptr, /* construct */
350 : nullptr, /* trace */
351 : };
352 :
353 : static const js::ClassExtension sClassExtension = {
354 : nullptr, /* weakmapKeyDelegateOp */
355 : _objectMoved /* objectMovedOp */
356 : };
357 :
358 : static const DOMJSClass sClass = {
359 : { "ChromeWorker",
360 : JSCLASS_IS_DOMJSCLASS | JSCLASS_FOREGROUND_FINALIZE | JSCLASS_HAS_RESERVED_SLOTS(1),
361 : &sClassOps,
362 : JS_NULL_CLASS_SPEC,
363 : &sClassExtension,
364 : JS_NULL_OBJECT_OPS
365 : },
366 : { prototypes::id::EventTarget, prototypes::id::Worker, prototypes::id::ChromeWorker, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count },
367 : IsBaseOf<nsISupports, mozilla::dom::workers::ChromeWorkerPrivate >::value,
368 : sNativePropertyHooks,
369 : FindAssociatedGlobalForNative<mozilla::dom::workers::ChromeWorkerPrivate>::Get,
370 : GetProtoObjectHandle,
371 : GetCCParticipant<mozilla::dom::workers::ChromeWorkerPrivate>::Get()
372 : };
373 : static_assert(1 == DOM_INSTANCE_RESERVED_SLOTS,
374 : "Must have the right minimal number of reserved slots.");
375 : static_assert(1 >= 1,
376 : "Must have enough reserved slots.");
377 :
378 : const JSClass*
379 0 : GetJSClass()
380 : {
381 0 : return sClass.ToJSClass();
382 : }
383 :
384 : bool
385 0 : Wrap(JSContext* aCx, mozilla::dom::workers::ChromeWorkerPrivate* aObject, nsWrapperCache* aCache, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
386 : {
387 : MOZ_ASSERT(static_cast<mozilla::dom::workers::ChromeWorkerPrivate*>(aObject) ==
388 : reinterpret_cast<mozilla::dom::workers::ChromeWorkerPrivate*>(aObject),
389 : "Multiple inheritance for mozilla::dom::workers::ChromeWorkerPrivate is broken.");
390 : MOZ_ASSERT(static_cast<mozilla::dom::workers::WorkerPrivate*>(aObject) ==
391 : reinterpret_cast<mozilla::dom::workers::WorkerPrivate*>(aObject),
392 : "Multiple inheritance for mozilla::dom::workers::WorkerPrivate is broken.");
393 : MOZ_ASSERT(static_cast<mozilla::dom::EventTarget*>(aObject) ==
394 : reinterpret_cast<mozilla::dom::EventTarget*>(aObject),
395 : "Multiple inheritance for mozilla::dom::EventTarget is broken.");
396 0 : MOZ_ASSERT(ToSupportsIsCorrect(aObject));
397 0 : MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx));
398 0 : MOZ_ASSERT(!aCache->GetWrapper(),
399 : "You should probably not be using Wrap() directly; use "
400 : "GetOrCreateDOMReflector instead");
401 :
402 0 : MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache),
403 : "nsISupports must be on our primary inheritance chain");
404 :
405 0 : JS::Rooted<JSObject*> global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject()));
406 0 : if (!global) {
407 0 : return false;
408 : }
409 0 : MOZ_ASSERT(JS_IsGlobalObject(global));
410 0 : MOZ_ASSERT(JS::ObjectIsNotGray(global));
411 :
412 : // That might have ended up wrapping us already, due to the wonders
413 : // of XBL. Check for that, and bail out as needed.
414 0 : aReflector.set(aCache->GetWrapper());
415 0 : if (aReflector) {
416 : #ifdef DEBUG
417 0 : binding_detail::AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto);
418 : #endif // DEBUG
419 0 : return true;
420 : }
421 :
422 0 : JSAutoCompartment ac(aCx, global);
423 0 : JS::Handle<JSObject*> canonicalProto = GetProtoObjectHandle(aCx);
424 0 : if (!canonicalProto) {
425 0 : return false;
426 : }
427 0 : JS::Rooted<JSObject*> proto(aCx);
428 0 : if (aGivenProto) {
429 0 : proto = aGivenProto;
430 : // Unfortunately, while aGivenProto was in the compartment of aCx
431 : // coming in, we changed compartments to that of "parent" so may need
432 : // to wrap the proto here.
433 0 : if (js::GetContextCompartment(aCx) != js::GetObjectCompartment(proto)) {
434 0 : if (!JS_WrapObject(aCx, &proto)) {
435 0 : return false;
436 : }
437 : }
438 : } else {
439 0 : proto = canonicalProto;
440 : }
441 :
442 0 : BindingJSObjectCreator<mozilla::dom::workers::ChromeWorkerPrivate> creator(aCx);
443 0 : creator.CreateObject(aCx, sClass.ToJSClass(), proto, aObject, aReflector);
444 0 : if (!aReflector) {
445 0 : return false;
446 : }
447 :
448 0 : aCache->SetWrapper(aReflector);
449 0 : creator.InitializationSucceeded();
450 :
451 0 : MOZ_ASSERT(aCache->GetWrapperPreserveColor() &&
452 : aCache->GetWrapperPreserveColor() == aReflector);
453 : // If proto != canonicalProto, we have to preserve our wrapper;
454 : // otherwise we won't be able to properly recreate it later, since
455 : // we won't know what proto to use. Note that we don't check
456 : // aGivenProto here, since it's entirely possible (and even
457 : // somewhat common) to have a non-null aGivenProto which is the
458 : // same as canonicalProto.
459 0 : if (proto != canonicalProto) {
460 0 : PreserveWrapper(aObject);
461 : }
462 :
463 0 : return true;
464 : }
465 :
466 : const NativePropertyHooks sNativePropertyHooks[] = { {
467 : nullptr,
468 : nullptr,
469 : nullptr,
470 : { nullptr, nullptr },
471 : prototypes::id::ChromeWorker,
472 : constructors::id::ChromeWorker,
473 : WorkerBinding::sNativePropertyHooks,
474 : &DefaultXrayExpandoObjectClass
475 : } };
476 :
477 : void
478 4 : CreateInterfaceObjects(JSContext* aCx, JS::Handle<JSObject*> aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, bool aDefineOnGlobal)
479 : {
480 4 : JS::Handle<JSObject*> parentProto(WorkerBinding::GetProtoObjectHandle(aCx));
481 4 : if (!parentProto) {
482 0 : return;
483 : }
484 :
485 4 : JS::Handle<JSObject*> constructorProto(WorkerBinding::GetConstructorObjectHandle(aCx));
486 4 : if (!constructorProto) {
487 0 : return;
488 : }
489 :
490 4 : JS::Heap<JSObject*>* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::ChromeWorker);
491 4 : JS::Heap<JSObject*>* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::ChromeWorker);
492 4 : dom::CreateInterfaceObjects(aCx, aGlobal, parentProto,
493 : &sPrototypeClass.mBase, protoCache,
494 : constructorProto, &sInterfaceObjectClass.mBase, 1, nullptr,
495 : interfaceCache,
496 : nullptr,
497 : nullptr,
498 : "ChromeWorker", aDefineOnGlobal,
499 : nullptr,
500 4 : false);
501 : }
502 :
503 : JS::Handle<JSObject*>
504 0 : GetProtoObjectHandle(JSContext* aCx)
505 : {
506 : /* Get the interface prototype object for this class. This will create the
507 : object as needed. */
508 0 : bool aDefineOnGlobal = true;
509 :
510 : /* Make sure our global is sane. Hopefully we can remove this sometime */
511 0 : JSObject* global = JS::CurrentGlobalOrNull(aCx);
512 0 : if (!(js::GetObjectClass(global)->flags & JSCLASS_DOM_GLOBAL)) {
513 0 : return nullptr;
514 : }
515 :
516 : /* Check to see whether the interface objects are already installed */
517 0 : ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
518 0 : if (!protoAndIfaceCache.HasEntryInSlot(prototypes::id::ChromeWorker)) {
519 0 : JS::Rooted<JSObject*> rootedGlobal(aCx, global);
520 0 : CreateInterfaceObjects(aCx, rootedGlobal, protoAndIfaceCache, aDefineOnGlobal);
521 : }
522 :
523 : /*
524 : * The object might _still_ be null, but that's OK.
525 : *
526 : * Calling fromMarkedLocation() is safe because protoAndIfaceCache is
527 : * traced by TraceProtoAndIfaceCache() and its contents are never
528 : * changed after they have been set.
529 : *
530 : * Calling address() avoids the read read barrier that does gray
531 : * unmarking, but it's not possible for the object to be gray here.
532 : */
533 :
534 0 : const JS::Heap<JSObject*>& entrySlot = protoAndIfaceCache.EntrySlotMustExist(prototypes::id::ChromeWorker);
535 0 : MOZ_ASSERT(JS::ObjectIsNotGray(entrySlot));
536 0 : return JS::Handle<JSObject*>::fromMarkedLocation(entrySlot.address());
537 : }
538 :
539 : JS::Handle<JSObject*>
540 4 : GetConstructorObjectHandle(JSContext* aCx, bool aDefineOnGlobal)
541 : {
542 : /* Get the interface object for this class. This will create the object as
543 : needed. */
544 :
545 : /* Make sure our global is sane. Hopefully we can remove this sometime */
546 4 : JSObject* global = JS::CurrentGlobalOrNull(aCx);
547 4 : if (!(js::GetObjectClass(global)->flags & JSCLASS_DOM_GLOBAL)) {
548 0 : return nullptr;
549 : }
550 :
551 : /* Check to see whether the interface objects are already installed */
552 4 : ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
553 4 : if (!protoAndIfaceCache.HasEntryInSlot(constructors::id::ChromeWorker)) {
554 8 : JS::Rooted<JSObject*> rootedGlobal(aCx, global);
555 4 : CreateInterfaceObjects(aCx, rootedGlobal, protoAndIfaceCache, aDefineOnGlobal);
556 : }
557 :
558 : /*
559 : * The object might _still_ be null, but that's OK.
560 : *
561 : * Calling fromMarkedLocation() is safe because protoAndIfaceCache is
562 : * traced by TraceProtoAndIfaceCache() and its contents are never
563 : * changed after they have been set.
564 : *
565 : * Calling address() avoids the read read barrier that does gray
566 : * unmarking, but it's not possible for the object to be gray here.
567 : */
568 :
569 4 : const JS::Heap<JSObject*>& entrySlot = protoAndIfaceCache.EntrySlotMustExist(constructors::id::ChromeWorker);
570 4 : MOZ_ASSERT(JS::ObjectIsNotGray(entrySlot));
571 4 : return JS::Handle<JSObject*>::fromMarkedLocation(entrySlot.address());
572 : }
573 :
574 : JSObject*
575 4 : GetConstructorObject(JSContext* aCx)
576 : {
577 4 : return GetConstructorObjectHandle(aCx);
578 : }
579 :
580 : } // namespace ChromeWorkerBinding
581 :
582 :
583 :
584 : namespace WorkerBinding {
585 :
586 : static_assert(IsRefcounted<NativeType>::value == IsRefcounted<EventTargetBinding::NativeType>::value,
587 : "Can't inherit from an interface with a different ownership model.");
588 :
589 : static bool
590 0 : terminate(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::workers::WorkerPrivate* self, const JSJitMethodCallArgs& args)
591 : {
592 0 : self->Terminate();
593 0 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
594 0 : args.rval().setUndefined();
595 0 : return true;
596 : }
597 :
598 : static const JSJitInfo terminate_methodinfo = {
599 : { (JSJitGetterOp)terminate },
600 : { prototypes::id::Worker },
601 : { PrototypeTraits<prototypes::id::Worker>::Depth },
602 : JSJitInfo::Method,
603 : JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */
604 : JSVAL_TYPE_UNDEFINED, /* returnType. Not relevant for setters. */
605 : true, /* isInfallible. False in setters. */
606 : false, /* isMovable. Not relevant for setters. */
607 : false, /* isEliminatable. Not relevant for setters. */
608 : false, /* isAlwaysInSlot. Only relevant for getters. */
609 : false, /* isLazilyCachedInSlot. Only relevant for getters. */
610 : false, /* isTypedMethod. Only relevant for methods. */
611 : 0 /* Reserved slot index, if we're stored in a slot, else 0. */
612 : };
613 : static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
614 : static_assert(0 < 1, "There is no slot for us");
615 :
616 : static bool
617 1 : postMessage(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::workers::WorkerPrivate* self, const JSJitMethodCallArgs& args)
618 : {
619 1 : if (MOZ_UNLIKELY(args.length() < 1)) {
620 0 : return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "Worker.postMessage");
621 : }
622 2 : JS::Rooted<JS::Value> arg0(cx);
623 1 : arg0 = args[0];
624 2 : binding_detail::AutoSequence<JSObject*> arg1;
625 2 : SequenceRooter<JSObject*> arg1_holder(cx, &arg1);
626 1 : if (args.hasDefined(1)) {
627 1 : if (args[1].isObject()) {
628 2 : JS::ForOfIterator iter(cx);
629 1 : if (!iter.init(args[1], JS::ForOfIterator::AllowNonIterable)) {
630 0 : return false;
631 : }
632 1 : if (!iter.valueIsIterable()) {
633 0 : ThrowErrorMessage(cx, MSG_NOT_SEQUENCE, "Argument 2 of Worker.postMessage");
634 0 : return false;
635 : }
636 1 : binding_detail::AutoSequence<JSObject*> &arr = arg1;
637 2 : JS::Rooted<JS::Value> temp(cx);
638 : while (true) {
639 : bool done;
640 1 : if (!iter.next(&temp, &done)) {
641 0 : return false;
642 : }
643 1 : if (done) {
644 1 : break;
645 : }
646 0 : JSObject** slotPtr = arr.AppendElement(mozilla::fallible);
647 0 : if (!slotPtr) {
648 0 : JS_ReportOutOfMemory(cx);
649 0 : return false;
650 : }
651 0 : JSObject*& slot = *slotPtr;
652 0 : if (temp.isObject()) {
653 0 : slot = &temp.toObject();
654 : } else {
655 0 : ThrowErrorMessage(cx, MSG_NOT_OBJECT, "Element of argument 2 of Worker.postMessage");
656 0 : return false;
657 : }
658 0 : }
659 : } else {
660 0 : ThrowErrorMessage(cx, MSG_NOT_SEQUENCE, "Argument 2 of Worker.postMessage");
661 0 : return false;
662 : }
663 : } else {
664 : /* Array is already empty; nothing to do */
665 : }
666 2 : binding_detail::FastErrorResult rv;
667 1 : self->PostMessage(cx, arg0, Constify(arg1), rv);
668 1 : if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) {
669 0 : return false;
670 : }
671 1 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
672 1 : args.rval().setUndefined();
673 1 : return true;
674 : }
675 :
676 : static const JSJitInfo postMessage_methodinfo = {
677 : { (JSJitGetterOp)postMessage },
678 : { prototypes::id::Worker },
679 : { PrototypeTraits<prototypes::id::Worker>::Depth },
680 : JSJitInfo::Method,
681 : JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */
682 : JSVAL_TYPE_UNDEFINED, /* returnType. Not relevant for setters. */
683 : false, /* isInfallible. False in setters. */
684 : false, /* isMovable. Not relevant for setters. */
685 : false, /* isEliminatable. Not relevant for setters. */
686 : false, /* isAlwaysInSlot. Only relevant for getters. */
687 : false, /* isLazilyCachedInSlot. Only relevant for getters. */
688 : false, /* isTypedMethod. Only relevant for methods. */
689 : 0 /* Reserved slot index, if we're stored in a slot, else 0. */
690 : };
691 : static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
692 : static_assert(0 < 1, "There is no slot for us");
693 :
694 : static bool
695 0 : get_onmessage(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::workers::WorkerPrivate* self, JSJitGetterCallArgs args)
696 : {
697 0 : RefPtr<EventHandlerNonNull> result(self->GetOnmessage());
698 0 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
699 0 : if (result) {
700 0 : args.rval().setObjectOrNull(GetCallbackFromCallbackObject(result));
701 0 : if (!MaybeWrapObjectOrNullValue(cx, args.rval())) {
702 0 : return false;
703 : }
704 0 : return true;
705 : } else {
706 0 : args.rval().setNull();
707 0 : return true;
708 : }
709 : }
710 :
711 : static bool
712 1 : set_onmessage(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::workers::WorkerPrivate* self, JSJitSetterCallArgs args)
713 : {
714 2 : RootedCallback<RefPtr<binding_detail::FastEventHandlerNonNull>> arg0(cx);
715 1 : if (args[0].isObject()) {
716 : { // scope for tempRoot
717 2 : JS::Rooted<JSObject*> tempRoot(cx, &args[0].toObject());
718 2 : arg0 = new binding_detail::FastEventHandlerNonNull(tempRoot);
719 : }
720 : } else {
721 0 : arg0 = nullptr;
722 : }
723 1 : self->SetOnmessage(Constify(arg0));
724 1 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
725 :
726 2 : return true;
727 : }
728 :
729 : static const JSJitInfo onmessage_getterinfo = {
730 : { (JSJitGetterOp)get_onmessage },
731 : { prototypes::id::Worker },
732 : { PrototypeTraits<prototypes::id::Worker>::Depth },
733 : JSJitInfo::Getter,
734 : JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */
735 : JSVAL_TYPE_UNKNOWN, /* returnType. Not relevant for setters. */
736 : false, /* isInfallible. False in setters. */
737 : false, /* isMovable. Not relevant for setters. */
738 : false, /* isEliminatable. Not relevant for setters. */
739 : false, /* isAlwaysInSlot. Only relevant for getters. */
740 : false, /* isLazilyCachedInSlot. Only relevant for getters. */
741 : false, /* isTypedMethod. Only relevant for methods. */
742 : 0 /* Reserved slot index, if we're stored in a slot, else 0. */
743 : };
744 : static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
745 : static_assert(0 < 1, "There is no slot for us");
746 : static const JSJitInfo onmessage_setterinfo = {
747 : { (JSJitGetterOp)set_onmessage },
748 : { prototypes::id::Worker },
749 : { PrototypeTraits<prototypes::id::Worker>::Depth },
750 : JSJitInfo::Setter,
751 : JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */
752 : JSVAL_TYPE_UNDEFINED, /* returnType. Not relevant for setters. */
753 : false, /* isInfallible. False in setters. */
754 : false, /* isMovable. Not relevant for setters. */
755 : false, /* isEliminatable. Not relevant for setters. */
756 : false, /* isAlwaysInSlot. Only relevant for getters. */
757 : false, /* isLazilyCachedInSlot. Only relevant for getters. */
758 : false, /* isTypedMethod. Only relevant for methods. */
759 : 0 /* Reserved slot index, if we're stored in a slot, else 0. */
760 : };
761 : static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
762 : static_assert(0 < 1, "There is no slot for us");
763 :
764 : static bool
765 0 : get_onerror(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::workers::WorkerPrivate* self, JSJitGetterCallArgs args)
766 : {
767 0 : RefPtr<EventHandlerNonNull> result(self->GetOnerror());
768 0 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
769 0 : if (result) {
770 0 : args.rval().setObjectOrNull(GetCallbackFromCallbackObject(result));
771 0 : if (!MaybeWrapObjectOrNullValue(cx, args.rval())) {
772 0 : return false;
773 : }
774 0 : return true;
775 : } else {
776 0 : args.rval().setNull();
777 0 : return true;
778 : }
779 : }
780 :
781 : static bool
782 1 : set_onerror(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::workers::WorkerPrivate* self, JSJitSetterCallArgs args)
783 : {
784 2 : RootedCallback<RefPtr<binding_detail::FastEventHandlerNonNull>> arg0(cx);
785 1 : if (args[0].isObject()) {
786 : { // scope for tempRoot
787 2 : JS::Rooted<JSObject*> tempRoot(cx, &args[0].toObject());
788 2 : arg0 = new binding_detail::FastEventHandlerNonNull(tempRoot);
789 : }
790 : } else {
791 0 : arg0 = nullptr;
792 : }
793 1 : self->SetOnerror(Constify(arg0));
794 1 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
795 :
796 2 : return true;
797 : }
798 :
799 : static const JSJitInfo onerror_getterinfo = {
800 : { (JSJitGetterOp)get_onerror },
801 : { prototypes::id::Worker },
802 : { PrototypeTraits<prototypes::id::Worker>::Depth },
803 : JSJitInfo::Getter,
804 : JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */
805 : JSVAL_TYPE_UNKNOWN, /* returnType. Not relevant for setters. */
806 : false, /* isInfallible. False in setters. */
807 : false, /* isMovable. Not relevant for setters. */
808 : false, /* isEliminatable. Not relevant for setters. */
809 : false, /* isAlwaysInSlot. Only relevant for getters. */
810 : false, /* isLazilyCachedInSlot. Only relevant for getters. */
811 : false, /* isTypedMethod. Only relevant for methods. */
812 : 0 /* Reserved slot index, if we're stored in a slot, else 0. */
813 : };
814 : static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
815 : static_assert(0 < 1, "There is no slot for us");
816 : static const JSJitInfo onerror_setterinfo = {
817 : { (JSJitGetterOp)set_onerror },
818 : { prototypes::id::Worker },
819 : { PrototypeTraits<prototypes::id::Worker>::Depth },
820 : JSJitInfo::Setter,
821 : JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */
822 : JSVAL_TYPE_UNDEFINED, /* returnType. Not relevant for setters. */
823 : false, /* isInfallible. False in setters. */
824 : false, /* isMovable. Not relevant for setters. */
825 : false, /* isEliminatable. Not relevant for setters. */
826 : false, /* isAlwaysInSlot. Only relevant for getters. */
827 : false, /* isLazilyCachedInSlot. Only relevant for getters. */
828 : false, /* isTypedMethod. Only relevant for methods. */
829 : 0 /* Reserved slot index, if we're stored in a slot, else 0. */
830 : };
831 : static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
832 : static_assert(0 < 1, "There is no slot for us");
833 :
834 : static bool
835 0 : _addProperty(JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::Handle<JS::Value> val)
836 : {
837 0 : mozilla::dom::workers::WorkerPrivate* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::workers::WorkerPrivate>(obj);
838 : // We don't want to preserve if we don't have a wrapper, and we
839 : // obviously can't preserve if we're not initialized.
840 0 : if (self && self->GetWrapperPreserveColor()) {
841 0 : PreserveWrapper(self);
842 : }
843 0 : return true;
844 : }
845 :
846 : static void
847 0 : _finalize(js::FreeOp* fop, JSObject* obj)
848 : {
849 0 : mozilla::dom::workers::WorkerPrivate* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::workers::WorkerPrivate>(obj);
850 0 : if (self) {
851 0 : ClearWrapper(self, self, obj);
852 0 : AddForDeferredFinalization<mozilla::dom::workers::WorkerPrivate>(self);
853 : }
854 0 : }
855 :
856 : static void
857 0 : _objectMoved(JSObject* obj, const JSObject* old)
858 : {
859 0 : mozilla::dom::workers::WorkerPrivate* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::workers::WorkerPrivate>(obj);
860 0 : if (self) {
861 0 : UpdateWrapper(self, self, obj, old);
862 : }
863 0 : }
864 :
865 : // We deliberately use brace-elision to make Visual Studio produce better initalization code.
866 : #if defined(__clang__)
867 : #pragma clang diagnostic push
868 : #pragma clang diagnostic ignored "-Wmissing-braces"
869 : #endif
870 : static const JSFunctionSpec sMethods_specs[] = {
871 : JS_FNSPEC("terminate", GenericBindingMethod, reinterpret_cast<const JSJitInfo*>(&terminate_methodinfo), 0, JSPROP_ENUMERATE, nullptr),
872 : JS_FNSPEC("postMessage", GenericBindingMethod, reinterpret_cast<const JSJitInfo*>(&postMessage_methodinfo), 1, JSPROP_ENUMERATE, nullptr),
873 : JS_FS_END
874 : };
875 : #if defined(__clang__)
876 : #pragma clang diagnostic pop
877 : #endif
878 :
879 :
880 : // Can't be const because the pref-enabled boolean needs to be writable
881 : static Prefable<const JSFunctionSpec> sMethods[] = {
882 : { nullptr, &sMethods_specs[0] },
883 : { nullptr, nullptr }
884 : };
885 :
886 : static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
887 : "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
888 : static_assert(2 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
889 : "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
890 :
891 : // We deliberately use brace-elision to make Visual Studio produce better initalization code.
892 : #if defined(__clang__)
893 : #pragma clang diagnostic push
894 : #pragma clang diagnostic ignored "-Wmissing-braces"
895 : #endif
896 : static const JSPropertySpec sAttributes_specs[] = {
897 : { "onmessage", JSPROP_SHARED | JSPROP_ENUMERATE, GenericBindingGetter, &onmessage_getterinfo, GenericBindingSetter, &onmessage_setterinfo },
898 : { "onerror", JSPROP_SHARED | JSPROP_ENUMERATE, GenericBindingGetter, &onerror_getterinfo, GenericBindingSetter, &onerror_setterinfo },
899 : { nullptr, 0, nullptr, nullptr, nullptr, nullptr }
900 : };
901 : #if defined(__clang__)
902 : #pragma clang diagnostic pop
903 : #endif
904 :
905 :
906 : // Can't be const because the pref-enabled boolean needs to be writable
907 : static Prefable<const JSPropertySpec> sAttributes[] = {
908 : { nullptr, &sAttributes_specs[0] },
909 : { nullptr, nullptr }
910 : };
911 :
912 : static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
913 : "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
914 : static_assert(2 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
915 : "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
916 :
917 :
918 : static uint16_t sNativeProperties_sortedPropertyIndices[4];
919 : static PropertyInfo sNativeProperties_propertyInfos[4];
920 :
921 : static const NativePropertiesN<2> sNativeProperties = {
922 : false, 0,
923 : false, 0,
924 : true, 0 /* sMethods */,
925 : true, 1 /* sAttributes */,
926 : false, 0,
927 : false, 0,
928 : false, 0,
929 : -1,
930 : 4,
931 : sNativeProperties_sortedPropertyIndices,
932 : {
933 : { sMethods, &sNativeProperties_propertyInfos[0] },
934 : { sAttributes, &sNativeProperties_propertyInfos[2] }
935 : }
936 : };
937 : static_assert(4 < 1ull << CHAR_BIT * sizeof(sNativeProperties.propertyInfoCount),
938 : "We have a property info count that is oversized");
939 :
940 : static bool
941 0 : _constructor(JSContext* cx, unsigned argc, JS::Value* vp)
942 : {
943 0 : JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
944 0 : JS::Rooted<JSObject*> obj(cx, &args.callee());
945 0 : if (!args.isConstructing()) {
946 : // XXXbz wish I could get the name from the callee instead of
947 : // Adding more relocations
948 0 : return ThrowConstructorWithoutNew(cx, "Worker");
949 : }
950 :
951 0 : GlobalObject global(cx, obj);
952 0 : if (global.Failed()) {
953 0 : return false;
954 : }
955 :
956 0 : JS::Rooted<JSObject*> desiredProto(cx);
957 0 : if (!GetDesiredProto(cx, args, &desiredProto)) {
958 0 : return false;
959 : }
960 :
961 0 : if (MOZ_UNLIKELY(args.length() < 1)) {
962 0 : return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "Worker");
963 : }
964 0 : bool objIsXray = xpc::WrapperFactory::IsXrayWrapper(obj);
965 0 : binding_detail::FakeString arg0;
966 0 : if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
967 0 : return false;
968 : }
969 0 : NormalizeUSVString(arg0);
970 0 : binding_detail::FastWorkerOptions arg1;
971 0 : if (!arg1.Init(cx, (args.hasDefined(1)) ? args[1] : JS::NullHandleValue, "Argument 2 of Worker.constructor", false)) {
972 0 : return false;
973 : }
974 0 : Maybe<JSAutoCompartment> ac;
975 0 : if (objIsXray) {
976 0 : obj = js::CheckedUnwrap(obj);
977 0 : if (!obj) {
978 0 : return false;
979 : }
980 0 : ac.emplace(cx, obj);
981 0 : if (!JS_WrapObject(cx, &desiredProto)) {
982 0 : return false;
983 : }
984 : }
985 0 : binding_detail::FastErrorResult rv;
986 0 : auto result(StrongOrRawPtr<mozilla::dom::workers::WorkerPrivate>(mozilla::dom::workers::WorkerPrivate::Constructor(global, Constify(arg0), Constify(arg1), rv)));
987 0 : if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) {
988 0 : return false;
989 : }
990 0 : MOZ_ASSERT(!JS_IsExceptionPending(cx));
991 : static_assert(!IsPointer<decltype(result)>::value,
992 : "NewObject implies that we need to keep the object alive with a strong reference.");
993 0 : if (!GetOrCreateDOMReflector(cx, result, args.rval(), desiredProto)) {
994 0 : MOZ_ASSERT(true || JS_IsExceptionPending(cx));
995 0 : return false;
996 : }
997 0 : return true;
998 : }
999 :
1000 : static const js::ClassOps sInterfaceObjectClassOps = {
1001 : nullptr, /* addProperty */
1002 : nullptr, /* delProperty */
1003 : nullptr, /* getProperty */
1004 : nullptr, /* setProperty */
1005 : nullptr, /* enumerate */
1006 : nullptr, /* newEnumerate */
1007 : nullptr, /* resolve */
1008 : nullptr, /* mayResolve */
1009 : nullptr, /* finalize */
1010 : _constructor, /* call */
1011 : nullptr, /* hasInstance */
1012 : _constructor, /* construct */
1013 : nullptr, /* trace */
1014 : };
1015 :
1016 : static const DOMIfaceAndProtoJSClass sInterfaceObjectClass = {
1017 : {
1018 : "Function",
1019 : JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_SLOTS_BASE),
1020 : &sInterfaceObjectClassOps,
1021 : JS_NULL_CLASS_SPEC,
1022 : JS_NULL_CLASS_EXT,
1023 : &sInterfaceObjectClassObjectOps
1024 : },
1025 : eInterface,
1026 : true,
1027 : prototypes::id::Worker,
1028 : PrototypeTraits<prototypes::id::Worker>::Depth,
1029 : sNativePropertyHooks,
1030 : "function Worker() {\n [native code]\n}",
1031 : EventTargetBinding::GetConstructorObject
1032 : };
1033 :
1034 : static const DOMIfaceAndProtoJSClass sPrototypeClass = {
1035 : {
1036 : "WorkerPrototype",
1037 : JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE),
1038 : JS_NULL_CLASS_OPS,
1039 : JS_NULL_CLASS_SPEC,
1040 : JS_NULL_CLASS_EXT,
1041 : JS_NULL_OBJECT_OPS
1042 : },
1043 : eInterfacePrototype,
1044 : false,
1045 : prototypes::id::Worker,
1046 : PrototypeTraits<prototypes::id::Worker>::Depth,
1047 : sNativePropertyHooks,
1048 : "[object WorkerPrototype]",
1049 : EventTargetBinding::GetProtoObject
1050 : };
1051 :
1052 : bool
1053 2 : ConstructorEnabled(JSContext* aCx, JS::Handle<JSObject*> aObj)
1054 : {
1055 2 : if (!NS_IsMainThread()) {
1056 1 : const char* name = js::GetObjectClass(aObj)->name;
1057 1 : if (strcmp(name, "DedicatedWorkerGlobalScope") && strcmp(name, "SharedWorkerGlobalScope")) {
1058 0 : return false;
1059 : }
1060 : }
1061 :
1062 2 : return mozilla::dom::workers::WorkerPrivate::WorkerAvailable(aCx, aObj);
1063 : }
1064 :
1065 : JSObject*
1066 0 : DefineDOMInterface(JSContext* aCx, JS::Handle<JSObject*> aGlobal, JS::Handle<jsid> id, bool aDefineOnGlobal)
1067 : {
1068 0 : return GetConstructorObjectHandle(aCx, aDefineOnGlobal);
1069 : }
1070 :
1071 : static const js::ClassOps sClassOps = {
1072 : _addProperty, /* addProperty */
1073 : nullptr, /* delProperty */
1074 : nullptr, /* getProperty */
1075 : nullptr, /* setProperty */
1076 : nullptr, /* enumerate */
1077 : nullptr, /* newEnumerate */
1078 : nullptr, /* resolve */
1079 : nullptr, /* mayResolve */
1080 : _finalize, /* finalize */
1081 : nullptr, /* call */
1082 : nullptr, /* hasInstance */
1083 : nullptr, /* construct */
1084 : nullptr, /* trace */
1085 : };
1086 :
1087 : static const js::ClassExtension sClassExtension = {
1088 : nullptr, /* weakmapKeyDelegateOp */
1089 : _objectMoved /* objectMovedOp */
1090 : };
1091 :
1092 : static const DOMJSClass sClass = {
1093 : { "Worker",
1094 : JSCLASS_IS_DOMJSCLASS | JSCLASS_FOREGROUND_FINALIZE | JSCLASS_HAS_RESERVED_SLOTS(1),
1095 : &sClassOps,
1096 : JS_NULL_CLASS_SPEC,
1097 : &sClassExtension,
1098 : JS_NULL_OBJECT_OPS
1099 : },
1100 : { prototypes::id::EventTarget, prototypes::id::Worker, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count },
1101 : IsBaseOf<nsISupports, mozilla::dom::workers::WorkerPrivate >::value,
1102 : sNativePropertyHooks,
1103 : FindAssociatedGlobalForNative<mozilla::dom::workers::WorkerPrivate>::Get,
1104 : GetProtoObjectHandle,
1105 : GetCCParticipant<mozilla::dom::workers::WorkerPrivate>::Get()
1106 : };
1107 : static_assert(1 == DOM_INSTANCE_RESERVED_SLOTS,
1108 : "Must have the right minimal number of reserved slots.");
1109 : static_assert(1 >= 1,
1110 : "Must have enough reserved slots.");
1111 :
1112 : const JSClass*
1113 0 : GetJSClass()
1114 : {
1115 0 : return sClass.ToJSClass();
1116 : }
1117 :
1118 : bool
1119 1 : Wrap(JSContext* aCx, mozilla::dom::workers::WorkerPrivate* aObject, nsWrapperCache* aCache, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
1120 : {
1121 : MOZ_ASSERT(static_cast<mozilla::dom::workers::WorkerPrivate*>(aObject) ==
1122 : reinterpret_cast<mozilla::dom::workers::WorkerPrivate*>(aObject),
1123 : "Multiple inheritance for mozilla::dom::workers::WorkerPrivate is broken.");
1124 : MOZ_ASSERT(static_cast<mozilla::dom::EventTarget*>(aObject) ==
1125 : reinterpret_cast<mozilla::dom::EventTarget*>(aObject),
1126 : "Multiple inheritance for mozilla::dom::EventTarget is broken.");
1127 1 : MOZ_ASSERT(ToSupportsIsCorrect(aObject));
1128 1 : MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx));
1129 1 : MOZ_ASSERT(!aCache->GetWrapper(),
1130 : "You should probably not be using Wrap() directly; use "
1131 : "GetOrCreateDOMReflector instead");
1132 :
1133 1 : MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache),
1134 : "nsISupports must be on our primary inheritance chain");
1135 :
1136 2 : JS::Rooted<JSObject*> global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject()));
1137 1 : if (!global) {
1138 0 : return false;
1139 : }
1140 1 : MOZ_ASSERT(JS_IsGlobalObject(global));
1141 1 : MOZ_ASSERT(JS::ObjectIsNotGray(global));
1142 :
1143 : // That might have ended up wrapping us already, due to the wonders
1144 : // of XBL. Check for that, and bail out as needed.
1145 1 : aReflector.set(aCache->GetWrapper());
1146 1 : if (aReflector) {
1147 : #ifdef DEBUG
1148 0 : binding_detail::AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto);
1149 : #endif // DEBUG
1150 0 : return true;
1151 : }
1152 :
1153 2 : JSAutoCompartment ac(aCx, global);
1154 1 : JS::Handle<JSObject*> canonicalProto = GetProtoObjectHandle(aCx);
1155 1 : if (!canonicalProto) {
1156 0 : return false;
1157 : }
1158 2 : JS::Rooted<JSObject*> proto(aCx);
1159 1 : if (aGivenProto) {
1160 1 : proto = aGivenProto;
1161 : // Unfortunately, while aGivenProto was in the compartment of aCx
1162 : // coming in, we changed compartments to that of "parent" so may need
1163 : // to wrap the proto here.
1164 1 : if (js::GetContextCompartment(aCx) != js::GetObjectCompartment(proto)) {
1165 0 : if (!JS_WrapObject(aCx, &proto)) {
1166 0 : return false;
1167 : }
1168 : }
1169 : } else {
1170 0 : proto = canonicalProto;
1171 : }
1172 :
1173 2 : BindingJSObjectCreator<mozilla::dom::workers::WorkerPrivate> creator(aCx);
1174 1 : creator.CreateObject(aCx, sClass.ToJSClass(), proto, aObject, aReflector);
1175 1 : if (!aReflector) {
1176 0 : return false;
1177 : }
1178 :
1179 1 : aCache->SetWrapper(aReflector);
1180 1 : creator.InitializationSucceeded();
1181 :
1182 1 : MOZ_ASSERT(aCache->GetWrapperPreserveColor() &&
1183 : aCache->GetWrapperPreserveColor() == aReflector);
1184 : // If proto != canonicalProto, we have to preserve our wrapper;
1185 : // otherwise we won't be able to properly recreate it later, since
1186 : // we won't know what proto to use. Note that we don't check
1187 : // aGivenProto here, since it's entirely possible (and even
1188 : // somewhat common) to have a non-null aGivenProto which is the
1189 : // same as canonicalProto.
1190 1 : if (proto != canonicalProto) {
1191 1 : PreserveWrapper(aObject);
1192 : }
1193 :
1194 1 : return true;
1195 : }
1196 :
1197 : const NativePropertyHooks sNativePropertyHooks[] = { {
1198 : nullptr,
1199 : nullptr,
1200 : nullptr,
1201 : { sNativeProperties.Upcast(), nullptr },
1202 : prototypes::id::Worker,
1203 : constructors::id::Worker,
1204 : EventTargetBinding::sNativePropertyHooks,
1205 : &DefaultXrayExpandoObjectClass
1206 : } };
1207 :
1208 : void
1209 4 : CreateInterfaceObjects(JSContext* aCx, JS::Handle<JSObject*> aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, bool aDefineOnGlobal)
1210 : {
1211 4 : JS::Handle<JSObject*> parentProto(EventTargetBinding::GetProtoObjectHandle(aCx));
1212 4 : if (!parentProto) {
1213 0 : return;
1214 : }
1215 :
1216 4 : JS::Handle<JSObject*> constructorProto(EventTargetBinding::GetConstructorObjectHandle(aCx));
1217 4 : if (!constructorProto) {
1218 0 : return;
1219 : }
1220 :
1221 : static bool sIdsInited = false;
1222 4 : if (!sIdsInited && NS_IsMainThread()) {
1223 1 : if (!InitIds(aCx, sNativeProperties.Upcast())) {
1224 0 : return;
1225 : }
1226 1 : sIdsInited = true;
1227 : }
1228 :
1229 4 : JS::Heap<JSObject*>* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::Worker);
1230 4 : JS::Heap<JSObject*>* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::Worker);
1231 4 : dom::CreateInterfaceObjects(aCx, aGlobal, parentProto,
1232 : &sPrototypeClass.mBase, protoCache,
1233 : constructorProto, &sInterfaceObjectClass.mBase, 1, nullptr,
1234 : interfaceCache,
1235 : sNativeProperties.Upcast(),
1236 : nullptr,
1237 : "Worker", aDefineOnGlobal,
1238 : nullptr,
1239 4 : false);
1240 : }
1241 :
1242 : JS::Handle<JSObject*>
1243 5 : GetProtoObjectHandle(JSContext* aCx)
1244 : {
1245 : /* Get the interface prototype object for this class. This will create the
1246 : object as needed. */
1247 5 : bool aDefineOnGlobal = true;
1248 :
1249 : /* Make sure our global is sane. Hopefully we can remove this sometime */
1250 5 : JSObject* global = JS::CurrentGlobalOrNull(aCx);
1251 5 : if (!(js::GetObjectClass(global)->flags & JSCLASS_DOM_GLOBAL)) {
1252 0 : return nullptr;
1253 : }
1254 :
1255 : /* Check to see whether the interface objects are already installed */
1256 5 : ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
1257 5 : if (!protoAndIfaceCache.HasEntryInSlot(prototypes::id::Worker)) {
1258 6 : JS::Rooted<JSObject*> rootedGlobal(aCx, global);
1259 3 : CreateInterfaceObjects(aCx, rootedGlobal, protoAndIfaceCache, aDefineOnGlobal);
1260 : }
1261 :
1262 : /*
1263 : * The object might _still_ be null, but that's OK.
1264 : *
1265 : * Calling fromMarkedLocation() is safe because protoAndIfaceCache is
1266 : * traced by TraceProtoAndIfaceCache() and its contents are never
1267 : * changed after they have been set.
1268 : *
1269 : * Calling address() avoids the read read barrier that does gray
1270 : * unmarking, but it's not possible for the object to be gray here.
1271 : */
1272 :
1273 5 : const JS::Heap<JSObject*>& entrySlot = protoAndIfaceCache.EntrySlotMustExist(prototypes::id::Worker);
1274 5 : MOZ_ASSERT(JS::ObjectIsNotGray(entrySlot));
1275 5 : return JS::Handle<JSObject*>::fromMarkedLocation(entrySlot.address());
1276 : }
1277 :
1278 : JSObject*
1279 0 : GetProtoObject(JSContext* aCx)
1280 : {
1281 0 : return GetProtoObjectHandle(aCx);
1282 : }
1283 :
1284 : JS::Handle<JSObject*>
1285 6 : GetConstructorObjectHandle(JSContext* aCx, bool aDefineOnGlobal)
1286 : {
1287 : /* Get the interface object for this class. This will create the object as
1288 : needed. */
1289 :
1290 : /* Make sure our global is sane. Hopefully we can remove this sometime */
1291 6 : JSObject* global = JS::CurrentGlobalOrNull(aCx);
1292 6 : if (!(js::GetObjectClass(global)->flags & JSCLASS_DOM_GLOBAL)) {
1293 0 : return nullptr;
1294 : }
1295 :
1296 : /* Check to see whether the interface objects are already installed */
1297 6 : ProtoAndIfaceCache& protoAndIfaceCache = *GetProtoAndIfaceCache(global);
1298 6 : if (!protoAndIfaceCache.HasEntryInSlot(constructors::id::Worker)) {
1299 2 : JS::Rooted<JSObject*> rootedGlobal(aCx, global);
1300 1 : CreateInterfaceObjects(aCx, rootedGlobal, protoAndIfaceCache, aDefineOnGlobal);
1301 : }
1302 :
1303 : /*
1304 : * The object might _still_ be null, but that's OK.
1305 : *
1306 : * Calling fromMarkedLocation() is safe because protoAndIfaceCache is
1307 : * traced by TraceProtoAndIfaceCache() and its contents are never
1308 : * changed after they have been set.
1309 : *
1310 : * Calling address() avoids the read read barrier that does gray
1311 : * unmarking, but it's not possible for the object to be gray here.
1312 : */
1313 :
1314 6 : const JS::Heap<JSObject*>& entrySlot = protoAndIfaceCache.EntrySlotMustExist(constructors::id::Worker);
1315 6 : MOZ_ASSERT(JS::ObjectIsNotGray(entrySlot));
1316 6 : return JS::Handle<JSObject*>::fromMarkedLocation(entrySlot.address());
1317 : }
1318 :
1319 : JSObject*
1320 2 : GetConstructorObject(JSContext* aCx)
1321 : {
1322 2 : return GetConstructorObjectHandle(aCx);
1323 : }
1324 :
1325 : } // namespace WorkerBinding
1326 :
1327 :
1328 :
1329 : } // namespace dom
1330 : } // namespace mozilla
|