Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dom/bindings/AddonEventBinding.cpp
Line
Count
Source (jump to first uncovered line)
1
/* THIS FILE IS AUTOGENERATED FROM AddonEvent.webidl BY Codegen.py - DO NOT EDIT */
2
3
#include "AddonEventBinding.h"
4
#include "AtomList.h"
5
#include "EventBinding.h"
6
#include "WrapperFactory.h"
7
#include "mozilla/AddonManagerWebAPI.h"
8
#include "mozilla/OwningNonNull.h"
9
#include "mozilla/dom/AddonEvent.h"
10
#include "mozilla/dom/BindingUtils.h"
11
#include "mozilla/dom/DOMJSClass.h"
12
#include "mozilla/dom/NonRefcountedDOMObject.h"
13
#include "mozilla/dom/PrimitiveConversions.h"
14
#include "mozilla/dom/ScriptSettings.h"
15
#include "mozilla/dom/SimpleGlobalObject.h"
16
#include "mozilla/dom/XrayExpandoClass.h"
17
18
namespace mozilla {
19
namespace dom {
20
21
namespace binding_detail {}; // Just to make sure it's known as a namespace
22
using namespace mozilla::dom::binding_detail;
23
24
25
26
AddonEventInit::AddonEventInit()
27
  : EventInit(FastDictionaryInitializer())
28
0
{
29
0
  // Safe to pass a null context if we pass a null value
30
0
  Init(nullptr, JS::NullHandleValue);
31
0
}
32
33
34
35
bool
36
AddonEventInit::InitIds(JSContext* cx, AddonEventInitAtoms* atomsCache)
37
0
{
38
0
  MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
39
0
40
0
  // Initialize these in reverse order so that any failure leaves the first one
41
0
  // uninitialized.
42
0
  if (!atomsCache->needsRestart_id.init(cx, "needsRestart") ||
43
0
      !atomsCache->id_id.init(cx, "id")) {
44
0
    return false;
45
0
  }
46
0
  return true;
47
0
}
48
49
bool
50
AddonEventInit::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
51
0
{
52
0
  // Passing a null JSContext is OK only if we're initing from null,
53
0
  // Since in that case we will not have to do any property gets
54
0
  // Also evaluate isNullOrUndefined in order to avoid false-positive
55
0
  // checkers by static analysis tools
56
0
  MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
57
0
  AddonEventInitAtoms* atomsCache = nullptr;
58
0
  if (cx) {
59
0
    atomsCache = GetAtomCache<AddonEventInitAtoms>(cx);
60
0
    if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
61
0
      return false;
62
0
    }
63
0
  }
64
0
65
0
  // Per spec, we init the parent's members first
66
0
  if (!EventInit::Init(cx, val)) {
67
0
    return false;
68
0
  }
69
0
70
0
  bool isNull = val.isNullOrUndefined();
71
0
  // We only need these if !isNull, in which case we have |cx|.
72
0
  Maybe<JS::Rooted<JSObject *> > object;
73
0
  Maybe<JS::Rooted<JS::Value> > temp;
74
0
  if (!isNull) {
75
0
    MOZ_ASSERT(cx);
76
0
    object.emplace(cx, &val.toObject());
77
0
    temp.emplace(cx);
78
0
  }
79
0
  if (!isNull) {
80
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->id_id, temp.ptr())) {
81
0
      return false;
82
0
    }
83
0
  }
84
0
  if (!isNull && !temp->isUndefined()) {
85
0
    if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mId)) {
86
0
      return false;
87
0
    }
88
0
    mIsAnyMemberPresent = true;
89
0
  } else if (cx) {
90
0
    // Don't error out if we have no cx.  In that
91
0
    // situation the caller is default-constructing us and we'll
92
0
    // just assume they know what they're doing.
93
0
    return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
94
0
                             "'id' member of AddonEventInit");
95
0
  }
96
0
97
0
  if (!isNull) {
98
0
    if (!JS_GetPropertyById(cx, *object, atomsCache->needsRestart_id, temp.ptr())) {
99
0
      return false;
100
0
    }
101
0
  }
102
0
  if (!isNull && !temp->isUndefined()) {
103
0
    if (!ValueToPrimitive<bool, eDefault>(cx, temp.ref(), &mNeedsRestart)) {
104
0
      return false;
105
0
    }
106
0
    mIsAnyMemberPresent = true;
107
0
  } else if (cx) {
108
0
    // Don't error out if we have no cx.  In that
109
0
    // situation the caller is default-constructing us and we'll
110
0
    // just assume they know what they're doing.
111
0
    return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
112
0
                             "'needsRestart' member of AddonEventInit");
113
0
  }
114
0
  return true;
115
0
}
116
117
bool
118
AddonEventInit::Init(const nsAString& aJSON)
119
0
{
120
0
  AutoJSAPI jsapi;
121
0
  JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
122
0
  if (!cleanGlobal) {
123
0
    return false;
124
0
  }
125
0
  if (!jsapi.Init(cleanGlobal)) {
126
0
    return false;
127
0
  }
128
0
  JSContext* cx = jsapi.cx();
129
0
  JS::Rooted<JS::Value> json(cx);
130
0
  bool ok = ParseJSON(cx, aJSON, &json);
131
0
  NS_ENSURE_TRUE(ok, false);
132
0
  return Init(cx, json);
133
0
}
134
135
bool
136
AddonEventInit::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
137
0
{
138
0
  AddonEventInitAtoms* atomsCache = GetAtomCache<AddonEventInitAtoms>(cx);
139
0
  if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
140
0
    return false;
141
0
  }
142
0
143
0
  // Per spec, we define the parent's members first
144
0
  if (!EventInit::ToObjectInternal(cx, rval)) {
145
0
    return false;
146
0
  }
147
0
  JS::Rooted<JSObject*> obj(cx, &rval.toObject());
148
0
149
0
  do {
150
0
    // block for our 'break' successCode and scope for 'temp' and 'currentValue'
151
0
    JS::Rooted<JS::Value> temp(cx);
152
0
    nsString const & currentValue = mId;
153
0
    if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
154
0
      return false;
155
0
    }
156
0
    if (!JS_DefinePropertyById(cx, obj, atomsCache->id_id, temp, JSPROP_ENUMERATE)) {
157
0
      return false;
158
0
    }
159
0
    break;
160
0
  } while(false);
161
0
162
0
  do {
163
0
    // block for our 'break' successCode and scope for 'temp' and 'currentValue'
164
0
    JS::Rooted<JS::Value> temp(cx);
165
0
    bool const & currentValue = mNeedsRestart;
166
0
    temp.setBoolean(currentValue);
167
0
    if (!JS_DefinePropertyById(cx, obj, atomsCache->needsRestart_id, temp, JSPROP_ENUMERATE)) {
168
0
      return false;
169
0
    }
170
0
    break;
171
0
  } while(false);
172
0
173
0
  return true;
174
0
}
175
176
bool
177
AddonEventInit::ToJSON(nsAString& aJSON) const
178
0
{
179
0
  AutoJSAPI jsapi;
180
0
  jsapi.Init();
181
0
  JSContext *cx = jsapi.cx();
182
0
  // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
183
0
  // because we'll only be creating objects, in ways that have no
184
0
  // side-effects, followed by a call to JS::ToJSONMaybeSafely,
185
0
  // which likewise guarantees no side-effects for the sorts of
186
0
  // things we will pass it.
187
0
  JSAutoRealm ar(cx, UnprivilegedJunkScopeOrWorkerGlobal());
188
0
  JS::Rooted<JS::Value> val(cx);
189
0
  if (!ToObjectInternal(cx, &val)) {
190
0
    return false;
191
0
  }
192
0
  JS::Rooted<JSObject*> obj(cx, &val.toObject());
193
0
  return StringifyToJSON(cx, obj, aJSON);
194
0
}
195
196
void
197
AddonEventInit::TraceDictionary(JSTracer* trc)
198
0
{
199
0
  EventInit::TraceDictionary(trc);
200
0
}
201
202
AddonEventInit&
203
AddonEventInit::operator=(const AddonEventInit& aOther)
204
0
{
205
0
  EventInit::operator=(aOther);
206
0
  mId = aOther.mId;
207
0
  mNeedsRestart = aOther.mNeedsRestart;
208
0
  return *this;
209
0
}
210
211
namespace binding_detail {
212
} // namespace binding_detail
213
214
215
namespace AddonEvent_Binding {
216
217
static_assert(IsRefcounted<NativeType>::value == IsRefcounted<Event_Binding::NativeType>::value,
218
              "Can't inherit from an interface with a different ownership model.");
219
220
MOZ_CAN_RUN_SCRIPT static bool
221
get_id(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::AddonEvent* self, JSJitGetterCallArgs args)
222
0
{
223
0
  AUTO_PROFILER_LABEL_FAST("get AddonEvent.id", DOM, cx);
224
0
225
0
  DOMString result;
226
0
  self->GetId(result);
227
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
228
0
  if (!xpc::NonVoidStringToJsval(cx, result, args.rval())) {
229
0
    return false;
230
0
  }
231
0
  return true;
232
0
}
233
234
static const JSJitInfo id_getterinfo = {
235
  { (JSJitGetterOp)get_id },
236
  { prototypes::id::AddonEvent },
237
  { PrototypeTraits<prototypes::id::AddonEvent>::Depth },
238
  JSJitInfo::Getter,
239
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
240
  JSVAL_TYPE_STRING,  /* returnType.  Not relevant for setters. */
241
  false,  /* isInfallible. False in setters. */
242
  false,  /* isMovable.  Not relevant for setters. */
243
  false, /* isEliminatable.  Not relevant for setters. */
244
  false, /* isAlwaysInSlot.  Only relevant for getters. */
245
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
246
  false,  /* isTypedMethod.  Only relevant for methods. */
247
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
248
};
249
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
250
static_assert(0 < 1, "There is no slot for us");
251
252
MOZ_CAN_RUN_SCRIPT static bool
253
get_needsRestart(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::AddonEvent* self, JSJitGetterCallArgs args)
254
0
{
255
0
  AUTO_PROFILER_LABEL_FAST("get AddonEvent.needsRestart", DOM, cx);
256
0
257
0
  bool result(self->NeedsRestart());
258
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
259
0
  args.rval().setBoolean(result);
260
0
  return true;
261
0
}
262
263
static const JSJitInfo needsRestart_getterinfo = {
264
  { (JSJitGetterOp)get_needsRestart },
265
  { prototypes::id::AddonEvent },
266
  { PrototypeTraits<prototypes::id::AddonEvent>::Depth },
267
  JSJitInfo::Getter,
268
  JSJitInfo::AliasEverything, /* aliasSet.  Not relevant for setters. */
269
  JSVAL_TYPE_BOOLEAN,  /* returnType.  Not relevant for setters. */
270
  true,  /* isInfallible. False in setters. */
271
  false,  /* isMovable.  Not relevant for setters. */
272
  false, /* isEliminatable.  Not relevant for setters. */
273
  false, /* isAlwaysInSlot.  Only relevant for getters. */
274
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
275
  false,  /* isTypedMethod.  Only relevant for methods. */
276
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
277
};
278
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
279
static_assert(0 < 1, "There is no slot for us");
280
281
MOZ_CAN_RUN_SCRIPT static bool
282
get_isTrusted(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::AddonEvent* self, JSJitGetterCallArgs args)
283
0
{
284
0
  AUTO_PROFILER_LABEL_FAST("get AddonEvent.isTrusted", DOM, cx);
285
0
286
0
  bool result(self->IsTrusted());
287
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
288
0
  args.rval().setBoolean(result);
289
0
  return true;
290
0
}
291
292
static const JSJitInfo isTrusted_getterinfo = {
293
  { (JSJitGetterOp)get_isTrusted },
294
  { prototypes::id::AddonEvent },
295
  { PrototypeTraits<prototypes::id::AddonEvent>::Depth },
296
  JSJitInfo::Getter,
297
  JSJitInfo::AliasDOMSets, /* aliasSet.  Not relevant for setters. */
298
  JSVAL_TYPE_BOOLEAN,  /* returnType.  Not relevant for setters. */
299
  true,  /* isInfallible. False in setters. */
300
  true,  /* isMovable.  Not relevant for setters. */
301
  true, /* isEliminatable.  Not relevant for setters. */
302
  false, /* isAlwaysInSlot.  Only relevant for getters. */
303
  false, /* isLazilyCachedInSlot.  Only relevant for getters. */
304
  false,  /* isTypedMethod.  Only relevant for methods. */
305
  0   /* Reserved slot index, if we're stored in a slot, else 0. */
306
};
307
static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit");
308
static_assert(0 < 1, "There is no slot for us");
309
310
static bool
311
_addProperty(JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::Handle<JS::Value> val)
312
0
{
313
0
  mozilla::dom::AddonEvent* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::AddonEvent>(obj);
314
0
  // We don't want to preserve if we don't have a wrapper, and we
315
0
  // obviously can't preserve if we're not initialized.
316
0
  if (self && self->GetWrapperPreserveColor()) {
317
0
    PreserveWrapper(self);
318
0
  }
319
0
  return true;
320
0
}
321
322
static void
323
_finalize(js::FreeOp* fop, JSObject* obj)
324
0
{
325
0
  mozilla::dom::AddonEvent* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::AddonEvent>(obj);
326
0
  if (self) {
327
0
    ClearWrapper(self, self, obj);
328
0
    AddForDeferredFinalization<mozilla::dom::AddonEvent>(self);
329
0
  }
330
0
}
331
332
static size_t
333
_objectMoved(JSObject* obj, JSObject* old)
334
0
{
335
0
  mozilla::dom::AddonEvent* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::AddonEvent>(obj);
336
0
  if (self) {
337
0
    UpdateWrapper(self, self, obj, old);
338
0
  }
339
0
340
0
  return 0;
341
0
}
342
343
// We deliberately use brace-elision to make Visual Studio produce better initalization code.
344
#if defined(__clang__)
345
#pragma clang diagnostic push
346
#pragma clang diagnostic ignored "-Wmissing-braces"
347
#endif
348
static const JSPropertySpec sAttributes_specs[] = {
349
  { "id", JSPROP_ENUMERATE, GenericGetter<NormalThisPolicy, ThrowExceptions>, &id_getterinfo, nullptr, nullptr },
350
  { "needsRestart", JSPROP_ENUMERATE, GenericGetter<NormalThisPolicy, ThrowExceptions>, &needsRestart_getterinfo, nullptr, nullptr },
351
  { nullptr, 0, nullptr, nullptr, nullptr, nullptr }
352
};
353
#if defined(__clang__)
354
#pragma clang diagnostic pop
355
#endif
356
357
358
static const Prefable<const JSPropertySpec> sAttributes[] = {
359
  { nullptr, &sAttributes_specs[0] },
360
  { nullptr, nullptr }
361
};
362
363
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
364
    "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
365
static_assert(2 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
366
    "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
367
368
// We deliberately use brace-elision to make Visual Studio produce better initalization code.
369
#if defined(__clang__)
370
#pragma clang diagnostic push
371
#pragma clang diagnostic ignored "-Wmissing-braces"
372
#endif
373
static const JSPropertySpec sUnforgeableAttributes_specs[] = {
374
  { "isTrusted", JSPROP_ENUMERATE | JSPROP_PERMANENT, GenericGetter<NormalThisPolicy, ThrowExceptions>, &isTrusted_getterinfo, nullptr, nullptr },
375
  { nullptr, 0, nullptr, nullptr, nullptr, nullptr }
376
};
377
#if defined(__clang__)
378
#pragma clang diagnostic pop
379
#endif
380
381
382
static const Prefable<const JSPropertySpec> sUnforgeableAttributes[] = {
383
  { nullptr, &sUnforgeableAttributes_specs[0] },
384
  { nullptr, nullptr }
385
};
386
387
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX,
388
    "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)");
389
static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX,
390
    "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)");
391
392
393
static uint16_t sNativeProperties_sortedPropertyIndices[3];
394
static PropertyInfo sNativeProperties_propertyInfos[3];
395
396
static const NativePropertiesN<2> sNativeProperties = {
397
  false, 0,
398
  false, 0,
399
  false, 0,
400
  true,  0 /* sAttributes */,
401
  false, 0,
402
  true,  1 /* sUnforgeableAttributes */,
403
  false, 0,
404
  -1,
405
  3,
406
  sNativeProperties_sortedPropertyIndices,
407
  {
408
    { sAttributes, &sNativeProperties_propertyInfos[0] },
409
    { sUnforgeableAttributes, &sNativeProperties_propertyInfos[2] }
410
  }
411
};
412
static_assert(3 < 1ull << CHAR_BIT * sizeof(sNativeProperties.propertyInfoCount),
413
    "We have a property info count that is oversized");
414
415
static bool
416
_constructor(JSContext* cx, unsigned argc, JS::Value* vp)
417
0
{
418
0
  AUTO_PROFILER_LABEL_FAST("AddonEvent constructor", DOM, cx);
419
0
420
0
  JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
421
0
  JS::Rooted<JSObject*> obj(cx, &args.callee());
422
0
  if (!args.isConstructing()) {
423
0
    // XXXbz wish I could get the name from the callee instead of
424
0
    // Adding more relocations
425
0
    return ThrowConstructorWithoutNew(cx, "AddonEvent");
426
0
  }
427
0
428
0
  JS::Rooted<JSObject*> desiredProto(cx);
429
0
  if (!GetDesiredProto(cx, args, &desiredProto)) {
430
0
    return false;
431
0
  }
432
0
433
0
  if (MOZ_UNLIKELY(args.length() < 2)) {
434
0
    return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "AddonEvent");
435
0
  }
436
0
  GlobalObject global(cx, obj);
437
0
  if (global.Failed()) {
438
0
    return false;
439
0
  }
440
0
441
0
  bool objIsXray = xpc::WrapperFactory::IsXrayWrapper(obj);
442
0
  binding_detail::FakeString arg0;
443
0
  if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) {
444
0
    return false;
445
0
  }
446
0
  binding_detail::FastAddonEventInit arg1;
447
0
  if (!arg1.Init(cx, args[1],  "Argument 2 of AddonEvent.constructor", false)) {
448
0
    return false;
449
0
  }
450
0
  Maybe<JSAutoRealm> ar;
451
0
  if (objIsXray) {
452
0
    obj = js::CheckedUnwrap(obj);
453
0
    if (!obj) {
454
0
      return false;
455
0
    }
456
0
    ar.emplace(cx, obj);
457
0
    if (!JS_WrapObject(cx, &desiredProto)) {
458
0
      return false;
459
0
    }
460
0
  }
461
0
  FastErrorResult rv;
462
0
  auto result(StrongOrRawPtr<mozilla::dom::AddonEvent>(mozilla::dom::AddonEvent::Constructor(global, NonNullHelper(Constify(arg0)), Constify(arg1), rv)));
463
0
  if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) {
464
0
    return false;
465
0
  }
466
0
  MOZ_ASSERT(!JS_IsExceptionPending(cx));
467
0
  static_assert(!IsPointer<decltype(result)>::value,
468
0
                "NewObject implies that we need to keep the object alive with a strong reference.");
469
0
  if (!GetOrCreateDOMReflector(cx, result, args.rval(), desiredProto)) {
470
0
    MOZ_ASSERT(JS_IsExceptionPending(cx));
471
0
    return false;
472
0
  }
473
0
  return true;
474
0
}
475
476
static const js::ClassOps sInterfaceObjectClassOps = {
477
    nullptr,               /* addProperty */
478
    nullptr,               /* delProperty */
479
    nullptr,               /* enumerate */
480
    nullptr,               /* newEnumerate */
481
    nullptr,               /* resolve */
482
    nullptr,               /* mayResolve */
483
    nullptr,               /* finalize */
484
    _constructor, /* call */
485
    nullptr,               /* hasInstance */
486
    _constructor, /* construct */
487
    nullptr,               /* trace */
488
};
489
490
static const DOMIfaceAndProtoJSClass sInterfaceObjectClass = {
491
  {
492
    "Function",
493
    JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_SLOTS_BASE),
494
    &sInterfaceObjectClassOps,
495
    JS_NULL_CLASS_SPEC,
496
    JS_NULL_CLASS_EXT,
497
    &sInterfaceObjectClassObjectOps
498
  },
499
  eInterface,
500
  true,
501
  prototypes::id::AddonEvent,
502
  PrototypeTraits<prototypes::id::AddonEvent>::Depth,
503
  sNativePropertyHooks,
504
  "function AddonEvent() {\n    [native code]\n}",
505
  Event_Binding::GetConstructorObject
506
};
507
508
static const DOMIfaceAndProtoJSClass sPrototypeClass = {
509
  {
510
    "AddonEventPrototype",
511
    JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE + 1 /* slot for the JSObject holding the unforgeable properties */),
512
    JS_NULL_CLASS_OPS,
513
    JS_NULL_CLASS_SPEC,
514
    JS_NULL_CLASS_EXT,
515
    JS_NULL_OBJECT_OPS
516
  },
517
  eInterfacePrototype,
518
  false,
519
  prototypes::id::AddonEvent,
520
  PrototypeTraits<prototypes::id::AddonEvent>::Depth,
521
  sNativePropertyHooks,
522
  "[object AddonEventPrototype]",
523
  Event_Binding::GetProtoObject
524
};
525
526
bool
527
ConstructorEnabled(JSContext* aCx, JS::Handle<JSObject*> aObj)
528
0
{
529
0
  return mozilla::AddonManagerWebAPI::IsAPIEnabled(aCx, aObj);
530
0
}
531
532
static const js::ClassOps sClassOps = {
533
  _addProperty, /* addProperty */
534
  nullptr,               /* delProperty */
535
  nullptr,               /* enumerate */
536
  nullptr, /* newEnumerate */
537
  nullptr, /* resolve */
538
  nullptr, /* mayResolve */
539
  _finalize, /* finalize */
540
  nullptr, /* call */
541
  nullptr,               /* hasInstance */
542
  nullptr,               /* construct */
543
  nullptr, /* trace */
544
};
545
546
static const js::ClassExtension sClassExtension = {
547
  nullptr, /* weakmapKeyDelegateOp */
548
  _objectMoved /* objectMovedOp */
549
};
550
551
static const DOMJSClass sClass = {
552
  { "AddonEvent",
553
    JSCLASS_IS_DOMJSCLASS | JSCLASS_FOREGROUND_FINALIZE | JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_SKIP_NURSERY_FINALIZE,
554
    &sClassOps,
555
    JS_NULL_CLASS_SPEC,
556
    &sClassExtension,
557
    JS_NULL_OBJECT_OPS
558
  },
559
  { prototypes::id::Event, prototypes::id::AddonEvent, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count },
560
  IsBaseOf<nsISupports, mozilla::dom::AddonEvent >::value,
561
  sNativePropertyHooks,
562
  FindAssociatedGlobalForNative<mozilla::dom::AddonEvent>::Get,
563
  GetProtoObjectHandle,
564
  GetCCParticipant<mozilla::dom::AddonEvent>::Get()
565
};
566
static_assert(1 == DOM_INSTANCE_RESERVED_SLOTS,
567
              "Must have the right minimal number of reserved slots.");
568
static_assert(1 >= 1,
569
              "Must have enough reserved slots.");
570
571
const JSClass*
572
GetJSClass()
573
0
{
574
0
  return sClass.ToJSClass();
575
0
}
576
577
bool
578
Wrap(JSContext* aCx, mozilla::dom::AddonEvent* aObject, nsWrapperCache* aCache, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
579
0
{
580
0
  static_assert(!IsBaseOf<NonRefcountedDOMObject, mozilla::dom::AddonEvent>::value,
581
0
                "Shouldn't have wrappercached things that are not refcounted.");
582
0
  MOZ_ASSERT(static_cast<mozilla::dom::AddonEvent*>(aObject) ==
583
0
             reinterpret_cast<mozilla::dom::AddonEvent*>(aObject),
584
0
             "Multiple inheritance for mozilla::dom::AddonEvent is broken.");
585
0
  MOZ_ASSERT(static_cast<mozilla::dom::Event*>(aObject) ==
586
0
             reinterpret_cast<mozilla::dom::Event*>(aObject),
587
0
             "Multiple inheritance for mozilla::dom::Event is broken.");
588
0
  MOZ_ASSERT(ToSupportsIsCorrect(aObject));
589
0
  MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx));
590
0
  MOZ_ASSERT(!aCache->GetWrapper(),
591
0
             "You should probably not be using Wrap() directly; use "
592
0
             "GetOrCreateDOMReflector instead");
593
0
594
0
  MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache),
595
0
             "nsISupports must be on our primary inheritance chain");
596
0
597
0
  JS::Rooted<JSObject*> global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject()));
598
0
  if (!global) {
599
0
    return false;
600
0
  }
601
0
  MOZ_ASSERT(JS_IsGlobalObject(global));
602
0
  MOZ_ASSERT(JS::ObjectIsNotGray(global));
603
0
604
0
  // That might have ended up wrapping us already, due to the wonders
605
0
  // of XBL.  Check for that, and bail out as needed.
606
0
  aReflector.set(aCache->GetWrapper());
607
0
  if (aReflector) {
608
#ifdef DEBUG
609
    AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto);
610
#endif // DEBUG
611
    return true;
612
0
  }
613
0
614
0
  JSAutoRealm ar(aCx, global);
615
0
  JS::Handle<JSObject*> canonicalProto = GetProtoObjectHandle(aCx);
616
0
  if (!canonicalProto) {
617
0
    return false;
618
0
  }
619
0
  JS::Rooted<JSObject*> proto(aCx);
620
0
  if (aGivenProto) {
621
0
    proto = aGivenProto;
622
0
    // Unfortunately, while aGivenProto was in the compartment of aCx
623
0
    // coming in, we changed compartments to that of "parent" so may need
624
0
    // to wrap the proto here.
625
0
    if (js::GetContextCompartment(aCx) != js::GetObjectCompartment(proto)) {
626
0
      if (!JS_WrapObject(aCx, &proto)) {
627
0
        return false;
628
0
      }
629
0
    }
630
0
  } else {
631
0
    proto = canonicalProto;
632
0
  }
633
0
634
0
  BindingJSObjectCreator<mozilla::dom::AddonEvent> creator(aCx);
635
0
  creator.CreateObject(aCx, sClass.ToJSClass(), proto, aObject, aReflector);
636
0
  if (!aReflector) {
637
0
    return false;
638
0
  }
639
0
640
0
  aCache->SetWrapper(aReflector);
641
0
642
0
  // Important: do unforgeable property setup after we have handed
643
0
  // over ownership of the C++ object to obj as needed, so that if
644
0
  // we fail and it ends up GCed it won't have problems in the
645
0
  // finalizer trying to drop its ownership of the C++ object.
646
0
  JS::Rooted<JSObject*> unforgeableHolder(aCx,
647
0
    &js::GetReservedSlot(canonicalProto, DOM_INTERFACE_PROTO_SLOTS_BASE).toObject());
648
0
  if (!JS_InitializePropertiesFromCompatibleNativeObject(aCx, aReflector, unforgeableHolder)) {
649
0
    aCache->ReleaseWrapper(aObject);
650
0
    aCache->ClearWrapper();
651
0
    return false;
652
0
  }
653
0
  creator.InitializationSucceeded();
654
0
655
0
  MOZ_ASSERT(aCache->GetWrapperPreserveColor() &&
656
0
             aCache->GetWrapperPreserveColor() == aReflector);
657
0
  // If proto != canonicalProto, we have to preserve our wrapper;
658
0
  // otherwise we won't be able to properly recreate it later, since
659
0
  // we won't know what proto to use.  Note that we don't check
660
0
  // aGivenProto here, since it's entirely possible (and even
661
0
  // somewhat common) to have a non-null aGivenProto which is the
662
0
  // same as canonicalProto.
663
0
  if (proto != canonicalProto) {
664
0
    PreserveWrapper(aObject);
665
0
  }
666
0
667
0
  return true;
668
0
}
669
670
const NativePropertyHooks sNativePropertyHooks[] = { {
671
  nullptr,
672
  nullptr,
673
  nullptr,
674
  { sNativeProperties.Upcast(), nullptr },
675
  prototypes::id::AddonEvent,
676
  constructors::id::AddonEvent,
677
  Event_Binding::sNativePropertyHooks,
678
  &DefaultXrayExpandoObjectClass
679
} };
680
681
void
682
CreateInterfaceObjects(JSContext* aCx, JS::Handle<JSObject*> aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, bool aDefineOnGlobal)
683
0
{
684
0
  JS::Handle<JSObject*> parentProto(Event_Binding::GetProtoObjectHandle(aCx));
685
0
  if (!parentProto) {
686
0
    return;
687
0
  }
688
0
689
0
  JS::Handle<JSObject*> constructorProto(Event_Binding::GetConstructorObjectHandle(aCx));
690
0
  if (!constructorProto) {
691
0
    return;
692
0
  }
693
0
694
0
  static bool sIdsInited = false;
695
0
  if (!sIdsInited && NS_IsMainThread()) {
696
0
    if (!InitIds(aCx, sNativeProperties.Upcast())) {
697
0
      return;
698
0
    }
699
0
    sIdsInited = true;
700
0
  }
701
0
702
0
  JS::Heap<JSObject*>* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::AddonEvent);
703
0
  JS::Heap<JSObject*>* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::AddonEvent);
704
0
  dom::CreateInterfaceObjects(aCx, aGlobal, parentProto,
705
0
                              &sPrototypeClass.mBase, protoCache,
706
0
                              nullptr,
707
0
                              constructorProto, &sInterfaceObjectClass.mBase, 2, nullptr,
708
0
                              interfaceCache,
709
0
                              sNativeProperties.Upcast(),
710
0
                              nullptr,
711
0
                              "AddonEvent", aDefineOnGlobal,
712
0
                              nullptr,
713
0
                              false);
714
0
715
0
  JS::Rooted<JSObject*> unforgeableHolder(aCx);
716
0
  {
717
0
    JS::Rooted<JSObject*> holderProto(aCx, *protoCache);
718
0
    unforgeableHolder = JS_NewObjectWithoutMetadata(aCx, sClass.ToJSClass(), holderProto);
719
0
    if (!unforgeableHolder) {
720
0
      *protoCache = nullptr;
721
0
      if (interfaceCache) {
722
0
        *interfaceCache = nullptr;
723
0
      }
724
0
      return;
725
0
    }
726
0
  }
727
0
728
0
  if (!DefineUnforgeableAttributes(aCx, unforgeableHolder, sUnforgeableAttributes)) {
729
0
    *protoCache = nullptr;
730
0
    if (interfaceCache) {
731
0
      *interfaceCache = nullptr;
732
0
    }
733
0
    return;
734
0
  }
735
0
736
0
  if (*protoCache) {
737
0
    js::SetReservedSlot(*protoCache, DOM_INTERFACE_PROTO_SLOTS_BASE,
738
0
                        JS::ObjectValue(*unforgeableHolder));
739
0
  }
740
0
}
741
742
JSObject*
743
GetConstructorObject(JSContext* aCx)
744
0
{
745
0
  return GetConstructorObjectHandle(aCx);
746
0
}
747
748
} // namespace AddonEvent_Binding
749
750
751
752
} // namespace dom
753
} // namespace mozilla