CSPViolationReportBody.cpp (4711B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/CSPViolationReportBody.h" 8 9 #include "mozilla/JSONWriter.h" 10 #include "mozilla/dom/ReportingBinding.h" 11 12 namespace mozilla::dom { 13 14 CSPViolationReportBody::CSPViolationReportBody( 15 nsIGlobalObject* aGlobal, 16 const mozilla::dom::SecurityPolicyViolationEventInit& aEvent) 17 : ReportBody(aGlobal), 18 mDocumentURL(aEvent.mDocumentURI), 19 mBlockedURL(aEvent.mBlockedURI), 20 mReferrer(aEvent.mReferrer), 21 mEffectiveDirective(aEvent.mEffectiveDirective), 22 mOriginalPolicy(aEvent.mOriginalPolicy), 23 mSourceFile(NS_ConvertUTF16toUTF8(aEvent.mSourceFile)), 24 mSample(aEvent.mSample), 25 mDisposition(aEvent.mDisposition), 26 mStatusCode(aEvent.mStatusCode), 27 mLineNumber(Nullable<uint32_t>(aEvent.mLineNumber)), 28 mColumnNumber(Nullable<uint32_t>(aEvent.mColumnNumber)) {} 29 30 CSPViolationReportBody::~CSPViolationReportBody() = default; 31 32 JSObject* CSPViolationReportBody::WrapObject( 33 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { 34 return CSPViolationReportBody_Binding::Wrap(aCx, this, aGivenProto); 35 } 36 37 void CSPViolationReportBody::GetBlockedURL(nsAString& aURL) const { 38 aURL = mBlockedURL; 39 } 40 41 void CSPViolationReportBody::GetDocumentURL(nsAString& aURL) const { 42 aURL = mDocumentURL; 43 } 44 45 void CSPViolationReportBody::GetReferrer(nsAString& aReferrer) const { 46 aReferrer = mReferrer; 47 } 48 49 void CSPViolationReportBody::GetEffectiveDirective( 50 nsAString& aDirective) const { 51 aDirective = mEffectiveDirective; 52 } 53 54 void CSPViolationReportBody::GetOriginalPolicy(nsAString& aPolicy) const { 55 aPolicy = mOriginalPolicy; 56 } 57 58 void CSPViolationReportBody::GetSourceFile(nsACString& aFile) const { 59 aFile = mSourceFile; 60 } 61 62 void CSPViolationReportBody::GetSample(nsAString& aSample) const { 63 aSample = mSample; 64 } 65 66 mozilla::dom::SecurityPolicyViolationEventDisposition 67 CSPViolationReportBody::Disposition() const { 68 return mDisposition; 69 } 70 71 uint16_t CSPViolationReportBody::StatusCode() const { return mStatusCode; } 72 73 Nullable<uint32_t> CSPViolationReportBody::GetLineNumber() const { 74 return mLineNumber; 75 } 76 77 Nullable<uint32_t> CSPViolationReportBody::GetColumnNumber() const { 78 return mColumnNumber; 79 } 80 81 void CSPViolationReportBody::ToJSON(JSONWriter& aJSONWriter) const { 82 if (mDocumentURL.IsEmpty()) { 83 aJSONWriter.NullProperty("documentURL"); 84 } else { 85 aJSONWriter.StringProperty("documentURL", 86 NS_ConvertUTF16toUTF8(mDocumentURL)); 87 } 88 89 if (mBlockedURL.IsEmpty()) { 90 aJSONWriter.NullProperty("blockedURL"); 91 } else { 92 aJSONWriter.StringProperty("blockedURL", 93 NS_ConvertUTF16toUTF8(mBlockedURL)); 94 } 95 96 if (mReferrer.IsEmpty()) { 97 aJSONWriter.NullProperty("referrer"); 98 } else { 99 aJSONWriter.StringProperty("referrer", NS_ConvertUTF16toUTF8(mReferrer)); 100 } 101 102 if (mEffectiveDirective.IsEmpty()) { 103 aJSONWriter.NullProperty("effectiveDirective"); 104 } else { 105 aJSONWriter.StringProperty("effectiveDirective", 106 NS_ConvertUTF16toUTF8(mEffectiveDirective)); 107 } 108 109 if (mOriginalPolicy.IsEmpty()) { 110 aJSONWriter.NullProperty("originalPolicy"); 111 } else { 112 aJSONWriter.StringProperty("originalPolicy", 113 NS_ConvertUTF16toUTF8(mOriginalPolicy)); 114 } 115 116 if (mSourceFile.IsEmpty()) { 117 aJSONWriter.NullProperty("sourceFile"); 118 } else { 119 aJSONWriter.StringProperty("sourceFile", mSourceFile); 120 } 121 122 if (mSample.IsEmpty()) { 123 aJSONWriter.NullProperty("sample"); 124 } else { 125 aJSONWriter.StringProperty("sample", NS_ConvertUTF16toUTF8(mSample)); 126 } 127 128 switch (mDisposition) { 129 case mozilla::dom::SecurityPolicyViolationEventDisposition::Report: 130 aJSONWriter.StringProperty("disposition", "report"); 131 break; 132 case mozilla::dom::SecurityPolicyViolationEventDisposition::Enforce: 133 aJSONWriter.StringProperty("disposition", "enforce"); 134 break; 135 default: 136 MOZ_ASSERT_UNREACHABLE("Invalid disposition"); 137 break; 138 } 139 140 aJSONWriter.IntProperty("statusCode", mStatusCode); 141 142 if (mLineNumber.IsNull()) { 143 aJSONWriter.NullProperty("lineNumber"); 144 } else { 145 aJSONWriter.IntProperty("lineNumber", mLineNumber.Value()); 146 } 147 148 if (mColumnNumber.IsNull()) { 149 aJSONWriter.NullProperty("columnNumber"); 150 } else { 151 aJSONWriter.IntProperty("columnNumber", mColumnNumber.Value()); 152 } 153 } 154 155 } // namespace mozilla::dom