Line data Source code
1 : /* THIS FILE IS AUTOGENERATED FROM KeyAlgorithm.webidl BY Codegen.py - DO NOT EDIT */
2 :
3 : #include "AtomList.h"
4 : #include "KeyAlgorithmBinding.h"
5 : #include "jsfriendapi.h"
6 : #include "mozilla/OwningNonNull.h"
7 : #include "mozilla/dom/BindingUtils.h"
8 : #include "mozilla/dom/NonRefcountedDOMObject.h"
9 : #include "mozilla/dom/PrimitiveConversions.h"
10 : #include "mozilla/dom/ScriptSettings.h"
11 : #include "mozilla/dom/SimpleGlobalObject.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 :
17 0 : KeyAlgorithm::KeyAlgorithm()
18 : {
19 : // Safe to pass a null context if we pass a null value
20 0 : Init(nullptr, JS::NullHandleValue);
21 0 : }
22 :
23 :
24 :
25 : bool
26 0 : KeyAlgorithm::InitIds(JSContext* cx, KeyAlgorithmAtoms* atomsCache)
27 : {
28 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
29 :
30 : // Initialize these in reverse order so that any failure leaves the first one
31 : // uninitialized.
32 0 : if (!atomsCache->name_id.init(cx, "name")) {
33 0 : return false;
34 : }
35 0 : return true;
36 : }
37 :
38 : bool
39 0 : KeyAlgorithm::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
40 : {
41 : // Passing a null JSContext is OK only if we're initing from null,
42 : // Since in that case we will not have to do any property gets
43 : // Also evaluate isNullOrUndefined in order to avoid false-positive
44 : // checkers by static analysis tools
45 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
46 0 : KeyAlgorithmAtoms* atomsCache = nullptr;
47 0 : if (cx) {
48 0 : atomsCache = GetAtomCache<KeyAlgorithmAtoms>(cx);
49 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
50 0 : return false;
51 : }
52 : }
53 :
54 0 : if (!IsConvertibleToDictionary(val)) {
55 0 : return ThrowErrorMessage(cx, MSG_NOT_DICTIONARY, sourceDescription);
56 : }
57 :
58 0 : bool isNull = val.isNullOrUndefined();
59 : // We only need these if !isNull, in which case we have |cx|.
60 0 : Maybe<JS::Rooted<JSObject *> > object;
61 0 : Maybe<JS::Rooted<JS::Value> > temp;
62 0 : if (!isNull) {
63 0 : MOZ_ASSERT(cx);
64 0 : object.emplace(cx, &val.toObject());
65 0 : temp.emplace(cx);
66 : }
67 0 : if (!isNull) {
68 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->name_id, temp.ptr())) {
69 0 : return false;
70 : }
71 : }
72 0 : if (!isNull && !temp->isUndefined()) {
73 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mName)) {
74 0 : return false;
75 : }
76 0 : mIsAnyMemberPresent = true;
77 0 : } else if (cx) {
78 : // Don't error out if we have no cx. In that
79 : // situation the caller is default-constructing us and we'll
80 : // just assume they know what they're doing.
81 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
82 0 : "'name' member of KeyAlgorithm");
83 : }
84 0 : return true;
85 : }
86 :
87 : bool
88 0 : KeyAlgorithm::Init(const nsAString& aJSON)
89 : {
90 0 : AutoJSAPI jsapi;
91 0 : JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
92 0 : if (!cleanGlobal) {
93 0 : return false;
94 : }
95 0 : if (!jsapi.Init(cleanGlobal)) {
96 0 : return false;
97 : }
98 0 : JSContext* cx = jsapi.cx();
99 0 : JS::Rooted<JS::Value> json(cx);
100 0 : bool ok = ParseJSON(cx, aJSON, &json);
101 0 : NS_ENSURE_TRUE(ok, false);
102 0 : return Init(cx, json);
103 : }
104 :
105 : bool
106 0 : KeyAlgorithm::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
107 : {
108 0 : KeyAlgorithmAtoms* atomsCache = GetAtomCache<KeyAlgorithmAtoms>(cx);
109 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
110 0 : return false;
111 : }
112 :
113 0 : JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
114 0 : if (!obj) {
115 0 : return false;
116 : }
117 0 : rval.set(JS::ObjectValue(*obj));
118 :
119 : do {
120 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
121 0 : JS::Rooted<JS::Value> temp(cx);
122 0 : nsString const & currentValue = mName;
123 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
124 0 : return false;
125 : }
126 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->name_id, temp, JSPROP_ENUMERATE)) {
127 0 : return false;
128 : }
129 0 : break;
130 : } while(0);
131 :
132 0 : return true;
133 : }
134 :
135 : bool
136 0 : KeyAlgorithm::ToJSON(nsAString& aJSON) const
137 : {
138 0 : AutoJSAPI jsapi;
139 0 : jsapi.Init();
140 0 : JSContext *cx = jsapi.cx();
141 : // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
142 : // because we'll only be creating objects, in ways that have no
143 : // side-effects, followed by a call to JS::ToJSONMaybeSafely,
144 : // which likewise guarantees no side-effects for the sorts of
145 : // things we will pass it.
146 0 : JSAutoCompartment ac(cx, binding_detail::UnprivilegedJunkScopeOrWorkerGlobal());
147 0 : JS::Rooted<JS::Value> val(cx);
148 0 : if (!ToObjectInternal(cx, &val)) {
149 0 : return false;
150 : }
151 0 : JS::Rooted<JSObject*> obj(cx, &val.toObject());
152 0 : return StringifyToJSON(cx, obj, aJSON);
153 : }
154 :
155 : void
156 0 : KeyAlgorithm::TraceDictionary(JSTracer* trc)
157 : {
158 0 : }
159 :
160 : KeyAlgorithm&
161 0 : KeyAlgorithm::operator=(const KeyAlgorithm& aOther)
162 : {
163 0 : mName = aOther.mName;
164 0 : return *this;
165 : }
166 :
167 : namespace binding_detail {
168 : } // namespace binding_detail
169 :
170 :
171 :
172 0 : AesKeyAlgorithm::AesKeyAlgorithm()
173 0 : : KeyAlgorithm(FastDictionaryInitializer())
174 : {
175 : // Safe to pass a null context if we pass a null value
176 0 : Init(nullptr, JS::NullHandleValue);
177 0 : }
178 :
179 :
180 :
181 : bool
182 0 : AesKeyAlgorithm::InitIds(JSContext* cx, AesKeyAlgorithmAtoms* atomsCache)
183 : {
184 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
185 :
186 : // Initialize these in reverse order so that any failure leaves the first one
187 : // uninitialized.
188 0 : if (!atomsCache->length_id.init(cx, "length")) {
189 0 : return false;
190 : }
191 0 : return true;
192 : }
193 :
194 : bool
195 0 : AesKeyAlgorithm::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
196 : {
197 : // Passing a null JSContext is OK only if we're initing from null,
198 : // Since in that case we will not have to do any property gets
199 : // Also evaluate isNullOrUndefined in order to avoid false-positive
200 : // checkers by static analysis tools
201 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
202 0 : AesKeyAlgorithmAtoms* atomsCache = nullptr;
203 0 : if (cx) {
204 0 : atomsCache = GetAtomCache<AesKeyAlgorithmAtoms>(cx);
205 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
206 0 : return false;
207 : }
208 : }
209 :
210 : // Per spec, we init the parent's members first
211 0 : if (!KeyAlgorithm::Init(cx, val)) {
212 0 : return false;
213 : }
214 :
215 0 : bool isNull = val.isNullOrUndefined();
216 : // We only need these if !isNull, in which case we have |cx|.
217 0 : Maybe<JS::Rooted<JSObject *> > object;
218 0 : Maybe<JS::Rooted<JS::Value> > temp;
219 0 : if (!isNull) {
220 0 : MOZ_ASSERT(cx);
221 0 : object.emplace(cx, &val.toObject());
222 0 : temp.emplace(cx);
223 : }
224 0 : if (!isNull) {
225 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->length_id, temp.ptr())) {
226 0 : return false;
227 : }
228 : }
229 0 : if (!isNull && !temp->isUndefined()) {
230 0 : if (!ValueToPrimitive<uint16_t, eDefault>(cx, temp.ref(), &mLength)) {
231 0 : return false;
232 : }
233 0 : mIsAnyMemberPresent = true;
234 0 : } else if (cx) {
235 : // Don't error out if we have no cx. In that
236 : // situation the caller is default-constructing us and we'll
237 : // just assume they know what they're doing.
238 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
239 0 : "'length' member of AesKeyAlgorithm");
240 : }
241 0 : return true;
242 : }
243 :
244 : bool
245 0 : AesKeyAlgorithm::Init(const nsAString& aJSON)
246 : {
247 0 : AutoJSAPI jsapi;
248 0 : JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
249 0 : if (!cleanGlobal) {
250 0 : return false;
251 : }
252 0 : if (!jsapi.Init(cleanGlobal)) {
253 0 : return false;
254 : }
255 0 : JSContext* cx = jsapi.cx();
256 0 : JS::Rooted<JS::Value> json(cx);
257 0 : bool ok = ParseJSON(cx, aJSON, &json);
258 0 : NS_ENSURE_TRUE(ok, false);
259 0 : return Init(cx, json);
260 : }
261 :
262 : bool
263 0 : AesKeyAlgorithm::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
264 : {
265 0 : AesKeyAlgorithmAtoms* atomsCache = GetAtomCache<AesKeyAlgorithmAtoms>(cx);
266 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
267 0 : return false;
268 : }
269 :
270 : // Per spec, we define the parent's members first
271 0 : if (!KeyAlgorithm::ToObjectInternal(cx, rval)) {
272 0 : return false;
273 : }
274 0 : JS::Rooted<JSObject*> obj(cx, &rval.toObject());
275 :
276 : do {
277 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
278 0 : JS::Rooted<JS::Value> temp(cx);
279 0 : uint16_t const & currentValue = mLength;
280 0 : temp.setInt32(int32_t(currentValue));
281 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->length_id, temp, JSPROP_ENUMERATE)) {
282 0 : return false;
283 : }
284 0 : break;
285 : } while(0);
286 :
287 0 : return true;
288 : }
289 :
290 : bool
291 0 : AesKeyAlgorithm::ToJSON(nsAString& aJSON) const
292 : {
293 0 : AutoJSAPI jsapi;
294 0 : jsapi.Init();
295 0 : JSContext *cx = jsapi.cx();
296 : // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
297 : // because we'll only be creating objects, in ways that have no
298 : // side-effects, followed by a call to JS::ToJSONMaybeSafely,
299 : // which likewise guarantees no side-effects for the sorts of
300 : // things we will pass it.
301 0 : JSAutoCompartment ac(cx, binding_detail::UnprivilegedJunkScopeOrWorkerGlobal());
302 0 : JS::Rooted<JS::Value> val(cx);
303 0 : if (!ToObjectInternal(cx, &val)) {
304 0 : return false;
305 : }
306 0 : JS::Rooted<JSObject*> obj(cx, &val.toObject());
307 0 : return StringifyToJSON(cx, obj, aJSON);
308 : }
309 :
310 : void
311 0 : AesKeyAlgorithm::TraceDictionary(JSTracer* trc)
312 : {
313 0 : KeyAlgorithm::TraceDictionary(trc);
314 0 : }
315 :
316 : AesKeyAlgorithm&
317 0 : AesKeyAlgorithm::operator=(const AesKeyAlgorithm& aOther)
318 : {
319 0 : KeyAlgorithm::operator=(aOther);
320 0 : mLength = aOther.mLength;
321 0 : return *this;
322 : }
323 :
324 : namespace binding_detail {
325 : } // namespace binding_detail
326 :
327 :
328 :
329 0 : DhKeyAlgorithm::DhKeyAlgorithm()
330 0 : : KeyAlgorithm(FastDictionaryInitializer())
331 : {
332 : // Safe to pass a null context if we pass a null value
333 0 : Init(nullptr, JS::NullHandleValue);
334 0 : }
335 :
336 :
337 : bool
338 0 : DhKeyAlgorithm::InitIds(JSContext* cx, DhKeyAlgorithmAtoms* atomsCache)
339 : {
340 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
341 :
342 : // Initialize these in reverse order so that any failure leaves the first one
343 : // uninitialized.
344 0 : if (!atomsCache->prime_id.init(cx, "prime") ||
345 0 : !atomsCache->generator_id.init(cx, "generator")) {
346 0 : return false;
347 : }
348 0 : return true;
349 : }
350 :
351 : bool
352 0 : DhKeyAlgorithm::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
353 : {
354 : // Passing a null JSContext is OK only if we're initing from null,
355 : // Since in that case we will not have to do any property gets
356 : // Also evaluate isNullOrUndefined in order to avoid false-positive
357 : // checkers by static analysis tools
358 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
359 0 : DhKeyAlgorithmAtoms* atomsCache = nullptr;
360 0 : if (cx) {
361 0 : atomsCache = GetAtomCache<DhKeyAlgorithmAtoms>(cx);
362 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
363 0 : return false;
364 : }
365 : }
366 :
367 : // Per spec, we init the parent's members first
368 0 : if (!KeyAlgorithm::Init(cx, val)) {
369 0 : return false;
370 : }
371 :
372 0 : bool isNull = val.isNullOrUndefined();
373 : // We only need these if !isNull, in which case we have |cx|.
374 0 : Maybe<JS::Rooted<JSObject *> > object;
375 0 : Maybe<JS::Rooted<JS::Value> > temp;
376 0 : if (!isNull) {
377 0 : MOZ_ASSERT(cx);
378 0 : object.emplace(cx, &val.toObject());
379 0 : temp.emplace(cx);
380 : }
381 0 : if (!isNull) {
382 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->generator_id, temp.ptr())) {
383 0 : return false;
384 : }
385 : }
386 0 : if (!isNull && !temp->isUndefined()) {
387 0 : if (temp.ref().isObject()) {
388 0 : if (!mGenerator.Init(&temp.ref().toObject())) {
389 0 : ThrowErrorMessage(cx, MSG_DOES_NOT_IMPLEMENT_INTERFACE, "'generator' member of DhKeyAlgorithm", "Uint8Array");
390 0 : return false;
391 : }
392 : } else {
393 0 : ThrowErrorMessage(cx, MSG_NOT_OBJECT, "'generator' member of DhKeyAlgorithm");
394 0 : return false;
395 : }
396 0 : mIsAnyMemberPresent = true;
397 0 : } else if (cx) {
398 : // Don't error out if we have no cx. In that
399 : // situation the caller is default-constructing us and we'll
400 : // just assume they know what they're doing.
401 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
402 0 : "'generator' member of DhKeyAlgorithm");
403 : }
404 :
405 0 : if (!isNull) {
406 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->prime_id, temp.ptr())) {
407 0 : return false;
408 : }
409 : }
410 0 : if (!isNull && !temp->isUndefined()) {
411 0 : if (temp.ref().isObject()) {
412 0 : if (!mPrime.Init(&temp.ref().toObject())) {
413 0 : ThrowErrorMessage(cx, MSG_DOES_NOT_IMPLEMENT_INTERFACE, "'prime' member of DhKeyAlgorithm", "Uint8Array");
414 0 : return false;
415 : }
416 : } else {
417 0 : ThrowErrorMessage(cx, MSG_NOT_OBJECT, "'prime' member of DhKeyAlgorithm");
418 0 : return false;
419 : }
420 0 : mIsAnyMemberPresent = true;
421 0 : } else if (cx) {
422 : // Don't error out if we have no cx. In that
423 : // situation the caller is default-constructing us and we'll
424 : // just assume they know what they're doing.
425 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
426 0 : "'prime' member of DhKeyAlgorithm");
427 : }
428 0 : return true;
429 : }
430 :
431 : bool
432 0 : DhKeyAlgorithm::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
433 : {
434 0 : DhKeyAlgorithmAtoms* atomsCache = GetAtomCache<DhKeyAlgorithmAtoms>(cx);
435 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
436 0 : return false;
437 : }
438 :
439 : // Per spec, we define the parent's members first
440 0 : if (!KeyAlgorithm::ToObjectInternal(cx, rval)) {
441 0 : return false;
442 : }
443 0 : JS::Rooted<JSObject*> obj(cx, &rval.toObject());
444 :
445 : do {
446 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
447 0 : JS::Rooted<JS::Value> temp(cx);
448 0 : Uint8Array const & currentValue = mGenerator;
449 0 : temp.setObject(*currentValue.Obj());
450 0 : if (!MaybeWrapNonDOMObjectValue(cx, &temp)) {
451 0 : return false;
452 : }
453 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->generator_id, temp, JSPROP_ENUMERATE)) {
454 0 : return false;
455 : }
456 0 : break;
457 : } while(0);
458 :
459 : do {
460 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
461 0 : JS::Rooted<JS::Value> temp(cx);
462 0 : Uint8Array const & currentValue = mPrime;
463 0 : temp.setObject(*currentValue.Obj());
464 0 : if (!MaybeWrapNonDOMObjectValue(cx, &temp)) {
465 0 : return false;
466 : }
467 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->prime_id, temp, JSPROP_ENUMERATE)) {
468 0 : return false;
469 : }
470 0 : break;
471 : } while(0);
472 :
473 0 : return true;
474 : }
475 :
476 : void
477 0 : DhKeyAlgorithm::TraceDictionary(JSTracer* trc)
478 : {
479 0 : KeyAlgorithm::TraceDictionary(trc);
480 0 : mGenerator.TraceSelf(trc);
481 :
482 0 : mPrime.TraceSelf(trc);
483 0 : }
484 :
485 : namespace binding_detail {
486 : } // namespace binding_detail
487 :
488 :
489 :
490 0 : EcKeyAlgorithm::EcKeyAlgorithm()
491 0 : : KeyAlgorithm(FastDictionaryInitializer())
492 : {
493 : // Safe to pass a null context if we pass a null value
494 0 : Init(nullptr, JS::NullHandleValue);
495 0 : }
496 :
497 :
498 :
499 : bool
500 0 : EcKeyAlgorithm::InitIds(JSContext* cx, EcKeyAlgorithmAtoms* atomsCache)
501 : {
502 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
503 :
504 : // Initialize these in reverse order so that any failure leaves the first one
505 : // uninitialized.
506 0 : if (!atomsCache->namedCurve_id.init(cx, "namedCurve")) {
507 0 : return false;
508 : }
509 0 : return true;
510 : }
511 :
512 : bool
513 0 : EcKeyAlgorithm::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
514 : {
515 : // Passing a null JSContext is OK only if we're initing from null,
516 : // Since in that case we will not have to do any property gets
517 : // Also evaluate isNullOrUndefined in order to avoid false-positive
518 : // checkers by static analysis tools
519 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
520 0 : EcKeyAlgorithmAtoms* atomsCache = nullptr;
521 0 : if (cx) {
522 0 : atomsCache = GetAtomCache<EcKeyAlgorithmAtoms>(cx);
523 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
524 0 : return false;
525 : }
526 : }
527 :
528 : // Per spec, we init the parent's members first
529 0 : if (!KeyAlgorithm::Init(cx, val)) {
530 0 : return false;
531 : }
532 :
533 0 : bool isNull = val.isNullOrUndefined();
534 : // We only need these if !isNull, in which case we have |cx|.
535 0 : Maybe<JS::Rooted<JSObject *> > object;
536 0 : Maybe<JS::Rooted<JS::Value> > temp;
537 0 : if (!isNull) {
538 0 : MOZ_ASSERT(cx);
539 0 : object.emplace(cx, &val.toObject());
540 0 : temp.emplace(cx);
541 : }
542 0 : if (!isNull) {
543 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->namedCurve_id, temp.ptr())) {
544 0 : return false;
545 : }
546 : }
547 0 : if (!isNull && !temp->isUndefined()) {
548 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mNamedCurve)) {
549 0 : return false;
550 : }
551 0 : mIsAnyMemberPresent = true;
552 0 : } else if (cx) {
553 : // Don't error out if we have no cx. In that
554 : // situation the caller is default-constructing us and we'll
555 : // just assume they know what they're doing.
556 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
557 0 : "'namedCurve' member of EcKeyAlgorithm");
558 : }
559 0 : return true;
560 : }
561 :
562 : bool
563 0 : EcKeyAlgorithm::Init(const nsAString& aJSON)
564 : {
565 0 : AutoJSAPI jsapi;
566 0 : JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
567 0 : if (!cleanGlobal) {
568 0 : return false;
569 : }
570 0 : if (!jsapi.Init(cleanGlobal)) {
571 0 : return false;
572 : }
573 0 : JSContext* cx = jsapi.cx();
574 0 : JS::Rooted<JS::Value> json(cx);
575 0 : bool ok = ParseJSON(cx, aJSON, &json);
576 0 : NS_ENSURE_TRUE(ok, false);
577 0 : return Init(cx, json);
578 : }
579 :
580 : bool
581 0 : EcKeyAlgorithm::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
582 : {
583 0 : EcKeyAlgorithmAtoms* atomsCache = GetAtomCache<EcKeyAlgorithmAtoms>(cx);
584 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
585 0 : return false;
586 : }
587 :
588 : // Per spec, we define the parent's members first
589 0 : if (!KeyAlgorithm::ToObjectInternal(cx, rval)) {
590 0 : return false;
591 : }
592 0 : JS::Rooted<JSObject*> obj(cx, &rval.toObject());
593 :
594 : do {
595 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
596 0 : JS::Rooted<JS::Value> temp(cx);
597 0 : nsString const & currentValue = mNamedCurve;
598 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
599 0 : return false;
600 : }
601 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->namedCurve_id, temp, JSPROP_ENUMERATE)) {
602 0 : return false;
603 : }
604 0 : break;
605 : } while(0);
606 :
607 0 : return true;
608 : }
609 :
610 : bool
611 0 : EcKeyAlgorithm::ToJSON(nsAString& aJSON) const
612 : {
613 0 : AutoJSAPI jsapi;
614 0 : jsapi.Init();
615 0 : JSContext *cx = jsapi.cx();
616 : // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
617 : // because we'll only be creating objects, in ways that have no
618 : // side-effects, followed by a call to JS::ToJSONMaybeSafely,
619 : // which likewise guarantees no side-effects for the sorts of
620 : // things we will pass it.
621 0 : JSAutoCompartment ac(cx, binding_detail::UnprivilegedJunkScopeOrWorkerGlobal());
622 0 : JS::Rooted<JS::Value> val(cx);
623 0 : if (!ToObjectInternal(cx, &val)) {
624 0 : return false;
625 : }
626 0 : JS::Rooted<JSObject*> obj(cx, &val.toObject());
627 0 : return StringifyToJSON(cx, obj, aJSON);
628 : }
629 :
630 : void
631 0 : EcKeyAlgorithm::TraceDictionary(JSTracer* trc)
632 : {
633 0 : KeyAlgorithm::TraceDictionary(trc);
634 0 : }
635 :
636 : EcKeyAlgorithm&
637 0 : EcKeyAlgorithm::operator=(const EcKeyAlgorithm& aOther)
638 : {
639 0 : KeyAlgorithm::operator=(aOther);
640 0 : mNamedCurve = aOther.mNamedCurve;
641 0 : return *this;
642 : }
643 :
644 : namespace binding_detail {
645 : } // namespace binding_detail
646 :
647 :
648 :
649 0 : HmacKeyAlgorithm::HmacKeyAlgorithm()
650 0 : : KeyAlgorithm(FastDictionaryInitializer()),
651 0 : mHash(FastDictionaryInitializer())
652 : {
653 : // Safe to pass a null context if we pass a null value
654 0 : Init(nullptr, JS::NullHandleValue);
655 0 : }
656 :
657 :
658 :
659 : bool
660 0 : HmacKeyAlgorithm::InitIds(JSContext* cx, HmacKeyAlgorithmAtoms* atomsCache)
661 : {
662 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
663 :
664 : // Initialize these in reverse order so that any failure leaves the first one
665 : // uninitialized.
666 0 : if (!atomsCache->length_id.init(cx, "length") ||
667 0 : !atomsCache->hash_id.init(cx, "hash")) {
668 0 : return false;
669 : }
670 0 : return true;
671 : }
672 :
673 : bool
674 0 : HmacKeyAlgorithm::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
675 : {
676 : // Passing a null JSContext is OK only if we're initing from null,
677 : // Since in that case we will not have to do any property gets
678 : // Also evaluate isNullOrUndefined in order to avoid false-positive
679 : // checkers by static analysis tools
680 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
681 0 : HmacKeyAlgorithmAtoms* atomsCache = nullptr;
682 0 : if (cx) {
683 0 : atomsCache = GetAtomCache<HmacKeyAlgorithmAtoms>(cx);
684 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
685 0 : return false;
686 : }
687 : }
688 :
689 : // Per spec, we init the parent's members first
690 0 : if (!KeyAlgorithm::Init(cx, val)) {
691 0 : return false;
692 : }
693 :
694 0 : bool isNull = val.isNullOrUndefined();
695 : // We only need these if !isNull, in which case we have |cx|.
696 0 : Maybe<JS::Rooted<JSObject *> > object;
697 0 : Maybe<JS::Rooted<JS::Value> > temp;
698 0 : if (!isNull) {
699 0 : MOZ_ASSERT(cx);
700 0 : object.emplace(cx, &val.toObject());
701 0 : temp.emplace(cx);
702 : }
703 0 : if (!isNull) {
704 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->hash_id, temp.ptr())) {
705 0 : return false;
706 : }
707 : }
708 0 : if (!isNull && !temp->isUndefined()) {
709 0 : if (!mHash.Init(cx, temp.ref(), "'hash' member of HmacKeyAlgorithm", passedToJSImpl)) {
710 0 : return false;
711 : }
712 0 : mIsAnyMemberPresent = true;
713 0 : } else if (cx) {
714 : // Don't error out if we have no cx. In that
715 : // situation the caller is default-constructing us and we'll
716 : // just assume they know what they're doing.
717 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
718 0 : "'hash' member of HmacKeyAlgorithm");
719 : }
720 :
721 0 : if (!isNull) {
722 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->length_id, temp.ptr())) {
723 0 : return false;
724 : }
725 : }
726 0 : if (!isNull && !temp->isUndefined()) {
727 0 : if (!ValueToPrimitive<uint32_t, eDefault>(cx, temp.ref(), &mLength)) {
728 0 : return false;
729 : }
730 0 : mIsAnyMemberPresent = true;
731 0 : } else if (cx) {
732 : // Don't error out if we have no cx. In that
733 : // situation the caller is default-constructing us and we'll
734 : // just assume they know what they're doing.
735 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
736 0 : "'length' member of HmacKeyAlgorithm");
737 : }
738 0 : return true;
739 : }
740 :
741 : bool
742 0 : HmacKeyAlgorithm::Init(const nsAString& aJSON)
743 : {
744 0 : AutoJSAPI jsapi;
745 0 : JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
746 0 : if (!cleanGlobal) {
747 0 : return false;
748 : }
749 0 : if (!jsapi.Init(cleanGlobal)) {
750 0 : return false;
751 : }
752 0 : JSContext* cx = jsapi.cx();
753 0 : JS::Rooted<JS::Value> json(cx);
754 0 : bool ok = ParseJSON(cx, aJSON, &json);
755 0 : NS_ENSURE_TRUE(ok, false);
756 0 : return Init(cx, json);
757 : }
758 :
759 : bool
760 0 : HmacKeyAlgorithm::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
761 : {
762 0 : HmacKeyAlgorithmAtoms* atomsCache = GetAtomCache<HmacKeyAlgorithmAtoms>(cx);
763 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
764 0 : return false;
765 : }
766 :
767 : // Per spec, we define the parent's members first
768 0 : if (!KeyAlgorithm::ToObjectInternal(cx, rval)) {
769 0 : return false;
770 : }
771 0 : JS::Rooted<JSObject*> obj(cx, &rval.toObject());
772 :
773 : do {
774 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
775 0 : JS::Rooted<JS::Value> temp(cx);
776 0 : KeyAlgorithm const & currentValue = mHash;
777 0 : if (!currentValue.ToObjectInternal(cx, &temp)) {
778 0 : return false;
779 : }
780 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->hash_id, temp, JSPROP_ENUMERATE)) {
781 0 : return false;
782 : }
783 0 : break;
784 : } while(0);
785 :
786 : do {
787 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
788 0 : JS::Rooted<JS::Value> temp(cx);
789 0 : uint32_t const & currentValue = mLength;
790 0 : temp.setNumber(currentValue);
791 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->length_id, temp, JSPROP_ENUMERATE)) {
792 0 : return false;
793 : }
794 0 : break;
795 : } while(0);
796 :
797 0 : return true;
798 : }
799 :
800 : bool
801 0 : HmacKeyAlgorithm::ToJSON(nsAString& aJSON) const
802 : {
803 0 : AutoJSAPI jsapi;
804 0 : jsapi.Init();
805 0 : JSContext *cx = jsapi.cx();
806 : // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
807 : // because we'll only be creating objects, in ways that have no
808 : // side-effects, followed by a call to JS::ToJSONMaybeSafely,
809 : // which likewise guarantees no side-effects for the sorts of
810 : // things we will pass it.
811 0 : JSAutoCompartment ac(cx, binding_detail::UnprivilegedJunkScopeOrWorkerGlobal());
812 0 : JS::Rooted<JS::Value> val(cx);
813 0 : if (!ToObjectInternal(cx, &val)) {
814 0 : return false;
815 : }
816 0 : JS::Rooted<JSObject*> obj(cx, &val.toObject());
817 0 : return StringifyToJSON(cx, obj, aJSON);
818 : }
819 :
820 : void
821 0 : HmacKeyAlgorithm::TraceDictionary(JSTracer* trc)
822 : {
823 0 : KeyAlgorithm::TraceDictionary(trc);
824 0 : }
825 :
826 : HmacKeyAlgorithm&
827 0 : HmacKeyAlgorithm::operator=(const HmacKeyAlgorithm& aOther)
828 : {
829 0 : KeyAlgorithm::operator=(aOther);
830 0 : mHash = aOther.mHash;
831 0 : mLength = aOther.mLength;
832 0 : return *this;
833 : }
834 :
835 : namespace binding_detail {
836 : } // namespace binding_detail
837 :
838 :
839 :
840 0 : RsaHashedKeyAlgorithm::RsaHashedKeyAlgorithm()
841 0 : : KeyAlgorithm(FastDictionaryInitializer()),
842 0 : mHash(FastDictionaryInitializer())
843 : {
844 : // Safe to pass a null context if we pass a null value
845 0 : Init(nullptr, JS::NullHandleValue);
846 0 : }
847 :
848 :
849 : bool
850 0 : RsaHashedKeyAlgorithm::InitIds(JSContext* cx, RsaHashedKeyAlgorithmAtoms* atomsCache)
851 : {
852 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
853 :
854 : // Initialize these in reverse order so that any failure leaves the first one
855 : // uninitialized.
856 0 : if (!atomsCache->publicExponent_id.init(cx, "publicExponent") ||
857 0 : !atomsCache->modulusLength_id.init(cx, "modulusLength") ||
858 0 : !atomsCache->hash_id.init(cx, "hash")) {
859 0 : return false;
860 : }
861 0 : return true;
862 : }
863 :
864 : bool
865 0 : RsaHashedKeyAlgorithm::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
866 : {
867 : // Passing a null JSContext is OK only if we're initing from null,
868 : // Since in that case we will not have to do any property gets
869 : // Also evaluate isNullOrUndefined in order to avoid false-positive
870 : // checkers by static analysis tools
871 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
872 0 : RsaHashedKeyAlgorithmAtoms* atomsCache = nullptr;
873 0 : if (cx) {
874 0 : atomsCache = GetAtomCache<RsaHashedKeyAlgorithmAtoms>(cx);
875 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
876 0 : return false;
877 : }
878 : }
879 :
880 : // Per spec, we init the parent's members first
881 0 : if (!KeyAlgorithm::Init(cx, val)) {
882 0 : return false;
883 : }
884 :
885 0 : bool isNull = val.isNullOrUndefined();
886 : // We only need these if !isNull, in which case we have |cx|.
887 0 : Maybe<JS::Rooted<JSObject *> > object;
888 0 : Maybe<JS::Rooted<JS::Value> > temp;
889 0 : if (!isNull) {
890 0 : MOZ_ASSERT(cx);
891 0 : object.emplace(cx, &val.toObject());
892 0 : temp.emplace(cx);
893 : }
894 0 : if (!isNull) {
895 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->hash_id, temp.ptr())) {
896 0 : return false;
897 : }
898 : }
899 0 : if (!isNull && !temp->isUndefined()) {
900 0 : if (!mHash.Init(cx, temp.ref(), "'hash' member of RsaHashedKeyAlgorithm", passedToJSImpl)) {
901 0 : return false;
902 : }
903 0 : mIsAnyMemberPresent = true;
904 0 : } else if (cx) {
905 : // Don't error out if we have no cx. In that
906 : // situation the caller is default-constructing us and we'll
907 : // just assume they know what they're doing.
908 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
909 0 : "'hash' member of RsaHashedKeyAlgorithm");
910 : }
911 :
912 0 : if (!isNull) {
913 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->modulusLength_id, temp.ptr())) {
914 0 : return false;
915 : }
916 : }
917 0 : if (!isNull && !temp->isUndefined()) {
918 0 : if (!ValueToPrimitive<uint16_t, eDefault>(cx, temp.ref(), &mModulusLength)) {
919 0 : return false;
920 : }
921 0 : mIsAnyMemberPresent = true;
922 0 : } else if (cx) {
923 : // Don't error out if we have no cx. In that
924 : // situation the caller is default-constructing us and we'll
925 : // just assume they know what they're doing.
926 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
927 0 : "'modulusLength' member of RsaHashedKeyAlgorithm");
928 : }
929 :
930 0 : if (!isNull) {
931 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->publicExponent_id, temp.ptr())) {
932 0 : return false;
933 : }
934 : }
935 0 : if (!isNull && !temp->isUndefined()) {
936 0 : if (temp.ref().isObject()) {
937 0 : if (!mPublicExponent.Init(&temp.ref().toObject())) {
938 0 : ThrowErrorMessage(cx, MSG_DOES_NOT_IMPLEMENT_INTERFACE, "'publicExponent' member of RsaHashedKeyAlgorithm", "Uint8Array");
939 0 : return false;
940 : }
941 : } else {
942 0 : ThrowErrorMessage(cx, MSG_NOT_OBJECT, "'publicExponent' member of RsaHashedKeyAlgorithm");
943 0 : return false;
944 : }
945 0 : mIsAnyMemberPresent = true;
946 0 : } else if (cx) {
947 : // Don't error out if we have no cx. In that
948 : // situation the caller is default-constructing us and we'll
949 : // just assume they know what they're doing.
950 0 : return ThrowErrorMessage(cx, MSG_MISSING_REQUIRED_DICTIONARY_MEMBER,
951 0 : "'publicExponent' member of RsaHashedKeyAlgorithm");
952 : }
953 0 : return true;
954 : }
955 :
956 : bool
957 0 : RsaHashedKeyAlgorithm::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
958 : {
959 0 : RsaHashedKeyAlgorithmAtoms* atomsCache = GetAtomCache<RsaHashedKeyAlgorithmAtoms>(cx);
960 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
961 0 : return false;
962 : }
963 :
964 : // Per spec, we define the parent's members first
965 0 : if (!KeyAlgorithm::ToObjectInternal(cx, rval)) {
966 0 : return false;
967 : }
968 0 : JS::Rooted<JSObject*> obj(cx, &rval.toObject());
969 :
970 : do {
971 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
972 0 : JS::Rooted<JS::Value> temp(cx);
973 0 : KeyAlgorithm const & currentValue = mHash;
974 0 : if (!currentValue.ToObjectInternal(cx, &temp)) {
975 0 : return false;
976 : }
977 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->hash_id, temp, JSPROP_ENUMERATE)) {
978 0 : return false;
979 : }
980 0 : break;
981 : } while(0);
982 :
983 : do {
984 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
985 0 : JS::Rooted<JS::Value> temp(cx);
986 0 : uint16_t const & currentValue = mModulusLength;
987 0 : temp.setInt32(int32_t(currentValue));
988 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->modulusLength_id, temp, JSPROP_ENUMERATE)) {
989 0 : return false;
990 : }
991 0 : break;
992 : } while(0);
993 :
994 : do {
995 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
996 0 : JS::Rooted<JS::Value> temp(cx);
997 0 : Uint8Array const & currentValue = mPublicExponent;
998 0 : temp.setObject(*currentValue.Obj());
999 0 : if (!MaybeWrapNonDOMObjectValue(cx, &temp)) {
1000 0 : return false;
1001 : }
1002 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->publicExponent_id, temp, JSPROP_ENUMERATE)) {
1003 0 : return false;
1004 : }
1005 0 : break;
1006 : } while(0);
1007 :
1008 0 : return true;
1009 : }
1010 :
1011 : void
1012 0 : RsaHashedKeyAlgorithm::TraceDictionary(JSTracer* trc)
1013 : {
1014 0 : KeyAlgorithm::TraceDictionary(trc);
1015 0 : mPublicExponent.TraceSelf(trc);
1016 0 : }
1017 :
1018 : namespace binding_detail {
1019 : } // namespace binding_detail
1020 :
1021 :
1022 : } // namespace dom
1023 : } // namespace mozilla
|