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