Line data Source code
1 : /* THIS FILE IS AUTOGENERATED FROM CSPReport.webidl BY Codegen.py - DO NOT EDIT */
2 :
3 : #include "AtomList.h"
4 : #include "CSPReportBinding.h"
5 : #include "mozilla/OwningNonNull.h"
6 : #include "mozilla/dom/BindingUtils.h"
7 : #include "mozilla/dom/NonRefcountedDOMObject.h"
8 : #include "mozilla/dom/PrimitiveConversions.h"
9 : #include "mozilla/dom/ScriptSettings.h"
10 : #include "mozilla/dom/SimpleGlobalObject.h"
11 :
12 : namespace mozilla {
13 : namespace dom {
14 :
15 :
16 0 : CSPReportProperties::CSPReportProperties()
17 : {
18 : // Safe to pass a null context if we pass a null value
19 0 : Init(nullptr, JS::NullHandleValue);
20 0 : }
21 :
22 :
23 :
24 : bool
25 0 : CSPReportProperties::InitIds(JSContext* cx, CSPReportPropertiesAtoms* atomsCache)
26 : {
27 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
28 :
29 : // Initialize these in reverse order so that any failure leaves the first one
30 : // uninitialized.
31 0 : if (!atomsCache->violated_directive_id.init(cx, "violated-directive") ||
32 0 : !atomsCache->source_file_id.init(cx, "source-file") ||
33 0 : !atomsCache->script_sample_id.init(cx, "script-sample") ||
34 0 : !atomsCache->referrer_id.init(cx, "referrer") ||
35 0 : !atomsCache->original_policy_id.init(cx, "original-policy") ||
36 0 : !atomsCache->line_number_id.init(cx, "line-number") ||
37 0 : !atomsCache->document_uri_id.init(cx, "document-uri") ||
38 0 : !atomsCache->blocked_uri_id.init(cx, "blocked-uri")) {
39 0 : return false;
40 : }
41 0 : return true;
42 : }
43 :
44 : bool
45 0 : CSPReportProperties::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 : CSPReportPropertiesAtoms* atomsCache = nullptr;
53 0 : if (cx) {
54 0 : atomsCache = GetAtomCache<CSPReportPropertiesAtoms>(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->blocked_uri_id, temp.ptr())) {
75 0 : return false;
76 : }
77 : }
78 0 : if (!isNull && !temp->isUndefined()) {
79 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mBlocked_uri)) {
80 0 : return false;
81 : }
82 : } else {
83 : static const char16_t data[] = { 0 };
84 0 : mBlocked_uri.Rebind(data, ArrayLength(data) - 1);
85 : }
86 0 : mIsAnyMemberPresent = true;
87 :
88 0 : if (!isNull) {
89 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->document_uri_id, temp.ptr())) {
90 0 : return false;
91 : }
92 : }
93 0 : if (!isNull && !temp->isUndefined()) {
94 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mDocument_uri)) {
95 0 : return false;
96 : }
97 : } else {
98 : static const char16_t data[] = { 0 };
99 0 : mDocument_uri.Rebind(data, ArrayLength(data) - 1);
100 : }
101 0 : mIsAnyMemberPresent = true;
102 :
103 0 : if (!isNull) {
104 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->line_number_id, temp.ptr())) {
105 0 : return false;
106 : }
107 : }
108 0 : if (!isNull && !temp->isUndefined()) {
109 0 : mLine_number.Construct();
110 0 : if (!ValueToPrimitive<int32_t, eDefault>(cx, temp.ref(), &(mLine_number.Value()))) {
111 0 : return false;
112 : }
113 0 : mIsAnyMemberPresent = true;
114 : }
115 :
116 0 : if (!isNull) {
117 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->original_policy_id, temp.ptr())) {
118 0 : return false;
119 : }
120 : }
121 0 : if (!isNull && !temp->isUndefined()) {
122 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mOriginal_policy)) {
123 0 : return false;
124 : }
125 : } else {
126 : static const char16_t data[] = { 0 };
127 0 : mOriginal_policy.Rebind(data, ArrayLength(data) - 1);
128 : }
129 0 : mIsAnyMemberPresent = true;
130 :
131 0 : if (!isNull) {
132 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->referrer_id, temp.ptr())) {
133 0 : return false;
134 : }
135 : }
136 0 : if (!isNull && !temp->isUndefined()) {
137 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mReferrer)) {
138 0 : return false;
139 : }
140 : } else {
141 : static const char16_t data[] = { 0 };
142 0 : mReferrer.Rebind(data, ArrayLength(data) - 1);
143 : }
144 0 : mIsAnyMemberPresent = true;
145 :
146 0 : if (!isNull) {
147 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->script_sample_id, temp.ptr())) {
148 0 : return false;
149 : }
150 : }
151 0 : if (!isNull && !temp->isUndefined()) {
152 0 : mScript_sample.Construct();
153 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, (mScript_sample.Value()))) {
154 0 : return false;
155 : }
156 0 : mIsAnyMemberPresent = true;
157 : }
158 :
159 0 : if (!isNull) {
160 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->source_file_id, temp.ptr())) {
161 0 : return false;
162 : }
163 : }
164 0 : if (!isNull && !temp->isUndefined()) {
165 0 : mSource_file.Construct();
166 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, (mSource_file.Value()))) {
167 0 : return false;
168 : }
169 0 : mIsAnyMemberPresent = true;
170 : }
171 :
172 0 : if (!isNull) {
173 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->violated_directive_id, temp.ptr())) {
174 0 : return false;
175 : }
176 : }
177 0 : if (!isNull && !temp->isUndefined()) {
178 0 : if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mViolated_directive)) {
179 0 : return false;
180 : }
181 : } else {
182 : static const char16_t data[] = { 0 };
183 0 : mViolated_directive.Rebind(data, ArrayLength(data) - 1);
184 : }
185 0 : mIsAnyMemberPresent = true;
186 0 : return true;
187 : }
188 :
189 : bool
190 0 : CSPReportProperties::Init(const nsAString& aJSON)
191 : {
192 0 : AutoJSAPI jsapi;
193 0 : JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
194 0 : if (!cleanGlobal) {
195 0 : return false;
196 : }
197 0 : if (!jsapi.Init(cleanGlobal)) {
198 0 : return false;
199 : }
200 0 : JSContext* cx = jsapi.cx();
201 0 : JS::Rooted<JS::Value> json(cx);
202 0 : bool ok = ParseJSON(cx, aJSON, &json);
203 0 : NS_ENSURE_TRUE(ok, false);
204 0 : return Init(cx, json);
205 : }
206 :
207 : bool
208 0 : CSPReportProperties::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
209 : {
210 0 : CSPReportPropertiesAtoms* atomsCache = GetAtomCache<CSPReportPropertiesAtoms>(cx);
211 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
212 0 : return false;
213 : }
214 :
215 0 : JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
216 0 : if (!obj) {
217 0 : return false;
218 : }
219 0 : rval.set(JS::ObjectValue(*obj));
220 :
221 : do {
222 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
223 0 : JS::Rooted<JS::Value> temp(cx);
224 0 : nsString const & currentValue = mBlocked_uri;
225 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
226 0 : return false;
227 : }
228 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->blocked_uri_id, temp, JSPROP_ENUMERATE)) {
229 0 : return false;
230 : }
231 0 : break;
232 : } while(0);
233 :
234 : do {
235 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
236 0 : JS::Rooted<JS::Value> temp(cx);
237 0 : nsString const & currentValue = mDocument_uri;
238 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
239 0 : return false;
240 : }
241 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->document_uri_id, temp, JSPROP_ENUMERATE)) {
242 0 : return false;
243 : }
244 0 : break;
245 : } while(0);
246 :
247 0 : if (mLine_number.WasPassed()) {
248 : do {
249 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
250 0 : JS::Rooted<JS::Value> temp(cx);
251 0 : int32_t const & currentValue = mLine_number.InternalValue();
252 0 : temp.setInt32(int32_t(currentValue));
253 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->line_number_id, temp, JSPROP_ENUMERATE)) {
254 0 : return false;
255 : }
256 0 : break;
257 : } while(0);
258 : }
259 :
260 : do {
261 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
262 0 : JS::Rooted<JS::Value> temp(cx);
263 0 : nsString const & currentValue = mOriginal_policy;
264 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
265 0 : return false;
266 : }
267 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->original_policy_id, temp, JSPROP_ENUMERATE)) {
268 0 : return false;
269 : }
270 0 : break;
271 : } while(0);
272 :
273 : do {
274 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
275 0 : JS::Rooted<JS::Value> temp(cx);
276 0 : nsString const & currentValue = mReferrer;
277 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
278 0 : return false;
279 : }
280 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->referrer_id, temp, JSPROP_ENUMERATE)) {
281 0 : return false;
282 : }
283 0 : break;
284 : } while(0);
285 :
286 0 : if (mScript_sample.WasPassed()) {
287 : do {
288 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
289 0 : JS::Rooted<JS::Value> temp(cx);
290 0 : nsString const & currentValue = mScript_sample.InternalValue();
291 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
292 0 : return false;
293 : }
294 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->script_sample_id, temp, JSPROP_ENUMERATE)) {
295 0 : return false;
296 : }
297 0 : break;
298 : } while(0);
299 : }
300 :
301 0 : if (mSource_file.WasPassed()) {
302 : do {
303 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
304 0 : JS::Rooted<JS::Value> temp(cx);
305 0 : nsString const & currentValue = mSource_file.InternalValue();
306 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
307 0 : return false;
308 : }
309 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->source_file_id, temp, JSPROP_ENUMERATE)) {
310 0 : return false;
311 : }
312 0 : break;
313 : } while(0);
314 : }
315 :
316 : do {
317 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
318 0 : JS::Rooted<JS::Value> temp(cx);
319 0 : nsString const & currentValue = mViolated_directive;
320 0 : if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) {
321 0 : return false;
322 : }
323 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->violated_directive_id, temp, JSPROP_ENUMERATE)) {
324 0 : return false;
325 : }
326 0 : break;
327 : } while(0);
328 :
329 0 : return true;
330 : }
331 :
332 : bool
333 0 : CSPReportProperties::ToJSON(nsAString& aJSON) const
334 : {
335 0 : AutoJSAPI jsapi;
336 0 : jsapi.Init();
337 0 : JSContext *cx = jsapi.cx();
338 : // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
339 : // because we'll only be creating objects, in ways that have no
340 : // side-effects, followed by a call to JS::ToJSONMaybeSafely,
341 : // which likewise guarantees no side-effects for the sorts of
342 : // things we will pass it.
343 0 : JSAutoCompartment ac(cx, binding_detail::UnprivilegedJunkScopeOrWorkerGlobal());
344 0 : JS::Rooted<JS::Value> val(cx);
345 0 : if (!ToObjectInternal(cx, &val)) {
346 0 : return false;
347 : }
348 0 : JS::Rooted<JSObject*> obj(cx, &val.toObject());
349 0 : return StringifyToJSON(cx, obj, aJSON);
350 : }
351 :
352 : void
353 0 : CSPReportProperties::TraceDictionary(JSTracer* trc)
354 : {
355 0 : }
356 :
357 : CSPReportProperties&
358 0 : CSPReportProperties::operator=(const CSPReportProperties& aOther)
359 : {
360 0 : mBlocked_uri = aOther.mBlocked_uri;
361 0 : mDocument_uri = aOther.mDocument_uri;
362 0 : mLine_number.Reset();
363 0 : if (aOther.mLine_number.WasPassed()) {
364 0 : mLine_number.Construct(aOther.mLine_number.Value());
365 : }
366 0 : mOriginal_policy = aOther.mOriginal_policy;
367 0 : mReferrer = aOther.mReferrer;
368 0 : mScript_sample.Reset();
369 0 : if (aOther.mScript_sample.WasPassed()) {
370 0 : mScript_sample.Construct(aOther.mScript_sample.Value());
371 : }
372 0 : mSource_file.Reset();
373 0 : if (aOther.mSource_file.WasPassed()) {
374 0 : mSource_file.Construct(aOther.mSource_file.Value());
375 : }
376 0 : mViolated_directive = aOther.mViolated_directive;
377 0 : return *this;
378 : }
379 :
380 : namespace binding_detail {
381 : } // namespace binding_detail
382 :
383 :
384 :
385 0 : CSPReport::CSPReport()
386 0 : : mCsp_report(FastDictionaryInitializer())
387 : {
388 : // Safe to pass a null context if we pass a null value
389 0 : Init(nullptr, JS::NullHandleValue);
390 0 : }
391 :
392 :
393 :
394 : bool
395 0 : CSPReport::InitIds(JSContext* cx, CSPReportAtoms* atomsCache)
396 : {
397 0 : MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache));
398 :
399 : // Initialize these in reverse order so that any failure leaves the first one
400 : // uninitialized.
401 0 : if (!atomsCache->csp_report_id.init(cx, "csp-report")) {
402 0 : return false;
403 : }
404 0 : return true;
405 : }
406 :
407 : bool
408 0 : CSPReport::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl)
409 : {
410 : // Passing a null JSContext is OK only if we're initing from null,
411 : // Since in that case we will not have to do any property gets
412 : // Also evaluate isNullOrUndefined in order to avoid false-positive
413 : // checkers by static analysis tools
414 0 : MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined());
415 0 : CSPReportAtoms* atomsCache = nullptr;
416 0 : if (cx) {
417 0 : atomsCache = GetAtomCache<CSPReportAtoms>(cx);
418 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
419 0 : return false;
420 : }
421 : }
422 :
423 0 : if (!IsConvertibleToDictionary(val)) {
424 0 : return ThrowErrorMessage(cx, MSG_NOT_DICTIONARY, sourceDescription);
425 : }
426 :
427 0 : bool isNull = val.isNullOrUndefined();
428 : // We only need these if !isNull, in which case we have |cx|.
429 0 : Maybe<JS::Rooted<JSObject *> > object;
430 0 : Maybe<JS::Rooted<JS::Value> > temp;
431 0 : if (!isNull) {
432 0 : MOZ_ASSERT(cx);
433 0 : object.emplace(cx, &val.toObject());
434 0 : temp.emplace(cx);
435 : }
436 0 : if (!isNull) {
437 0 : if (!JS_GetPropertyById(cx, *object, atomsCache->csp_report_id, temp.ptr())) {
438 0 : return false;
439 : }
440 : }
441 0 : if (!mCsp_report.Init(cx, (!isNull && !temp->isUndefined()) ? temp.ref() : JS::NullHandleValue, "'csp-report' member of CSPReport", passedToJSImpl)) {
442 0 : return false;
443 : }
444 0 : mIsAnyMemberPresent = true;
445 0 : return true;
446 : }
447 :
448 : bool
449 0 : CSPReport::Init(const nsAString& aJSON)
450 : {
451 0 : AutoJSAPI jsapi;
452 0 : JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail);
453 0 : if (!cleanGlobal) {
454 0 : return false;
455 : }
456 0 : if (!jsapi.Init(cleanGlobal)) {
457 0 : return false;
458 : }
459 0 : JSContext* cx = jsapi.cx();
460 0 : JS::Rooted<JS::Value> json(cx);
461 0 : bool ok = ParseJSON(cx, aJSON, &json);
462 0 : NS_ENSURE_TRUE(ok, false);
463 0 : return Init(cx, json);
464 : }
465 :
466 : bool
467 0 : CSPReport::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const
468 : {
469 0 : CSPReportAtoms* atomsCache = GetAtomCache<CSPReportAtoms>(cx);
470 0 : if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) {
471 0 : return false;
472 : }
473 :
474 0 : JS::Rooted<JSObject*> obj(cx, JS_NewPlainObject(cx));
475 0 : if (!obj) {
476 0 : return false;
477 : }
478 0 : rval.set(JS::ObjectValue(*obj));
479 :
480 : do {
481 : // block for our 'break' successCode and scope for 'temp' and 'currentValue'
482 0 : JS::Rooted<JS::Value> temp(cx);
483 0 : CSPReportProperties const & currentValue = mCsp_report;
484 0 : if (!currentValue.ToObjectInternal(cx, &temp)) {
485 0 : return false;
486 : }
487 0 : if (!JS_DefinePropertyById(cx, obj, atomsCache->csp_report_id, temp, JSPROP_ENUMERATE)) {
488 0 : return false;
489 : }
490 0 : break;
491 : } while(0);
492 :
493 0 : return true;
494 : }
495 :
496 : bool
497 0 : CSPReport::ToJSON(nsAString& aJSON) const
498 : {
499 0 : AutoJSAPI jsapi;
500 0 : jsapi.Init();
501 0 : JSContext *cx = jsapi.cx();
502 : // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here
503 : // because we'll only be creating objects, in ways that have no
504 : // side-effects, followed by a call to JS::ToJSONMaybeSafely,
505 : // which likewise guarantees no side-effects for the sorts of
506 : // things we will pass it.
507 0 : JSAutoCompartment ac(cx, binding_detail::UnprivilegedJunkScopeOrWorkerGlobal());
508 0 : JS::Rooted<JS::Value> val(cx);
509 0 : if (!ToObjectInternal(cx, &val)) {
510 0 : return false;
511 : }
512 0 : JS::Rooted<JSObject*> obj(cx, &val.toObject());
513 0 : return StringifyToJSON(cx, obj, aJSON);
514 : }
515 :
516 : void
517 0 : CSPReport::TraceDictionary(JSTracer* trc)
518 : {
519 0 : }
520 :
521 : CSPReport&
522 0 : CSPReport::operator=(const CSPReport& aOther)
523 : {
524 0 : mCsp_report = aOther.mCsp_report;
525 0 : return *this;
526 : }
527 :
528 : namespace binding_detail {
529 : } // namespace binding_detail
530 :
531 :
532 : } // namespace dom
533 : } // namespace mozilla
|