DeprecationReportBody.cpp (2716B)
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/DeprecationReportBody.h" 8 9 #include "js/Date.h" 10 #include "mozilla/JSONWriter.h" 11 #include "mozilla/dom/ReportingBinding.h" 12 13 namespace mozilla::dom { 14 15 DeprecationReportBody::DeprecationReportBody( 16 nsIGlobalObject* aGlobal, const nsAString& aId, 17 const Nullable<uint64_t>& aDate, const nsAString& aMessage, 18 const nsACString& aSourceFile, const Nullable<uint32_t>& aLineNumber, 19 const Nullable<uint32_t>& aColumnNumber) 20 : ReportBody(aGlobal), 21 mId(aId), 22 mDate(aDate), 23 mMessage(aMessage), 24 mSourceFile(aSourceFile), 25 mLineNumber(aLineNumber), 26 mColumnNumber(aColumnNumber) { 27 MOZ_ASSERT(aGlobal); 28 } 29 30 DeprecationReportBody::~DeprecationReportBody() = default; 31 32 JSObject* DeprecationReportBody::WrapObject(JSContext* aCx, 33 JS::Handle<JSObject*> aGivenProto) { 34 return DeprecationReportBody_Binding::Wrap(aCx, this, aGivenProto); 35 } 36 37 void DeprecationReportBody::GetId(nsAString& aId) const { aId = mId; } 38 39 void DeprecationReportBody::GetAnticipatedRemoval( 40 JSContext* aCx, JS::MutableHandle<JSObject*> aResult) const { 41 if (!mDate.IsNull()) { 42 JSObject* date = JS::NewDateObject(aCx, JS::TimeClip(mDate.Value())); 43 aResult.set(date); 44 } 45 } 46 47 void DeprecationReportBody::GetMessage(nsAString& aMessage) const { 48 aMessage = mMessage; 49 } 50 51 void DeprecationReportBody::GetSourceFile(nsACString& aSourceFile) const { 52 aSourceFile = mSourceFile; 53 } 54 55 Nullable<uint32_t> DeprecationReportBody::GetLineNumber() const { 56 return mLineNumber; 57 } 58 59 Nullable<uint32_t> DeprecationReportBody::GetColumnNumber() const { 60 return mColumnNumber; 61 } 62 63 void DeprecationReportBody::ToJSON(JSONWriter& aWriter) const { 64 aWriter.StringProperty("id", NS_ConvertUTF16toUTF8(mId)); 65 // TODO: anticipatedRemoval? https://github.com/w3c/reporting/issues/132 66 aWriter.StringProperty("message", NS_ConvertUTF16toUTF8(mMessage)); 67 68 if (mSourceFile.IsEmpty()) { 69 aWriter.NullProperty("sourceFile"); 70 } else { 71 aWriter.StringProperty("sourceFile", mSourceFile); 72 } 73 74 if (mLineNumber.IsNull()) { 75 aWriter.NullProperty("lineNumber"); 76 } else { 77 aWriter.IntProperty("lineNumber", mLineNumber.Value()); 78 } 79 80 if (mColumnNumber.IsNull()) { 81 aWriter.NullProperty("columnNumber"); 82 } else { 83 aWriter.IntProperty("columnNumber", mColumnNumber.Value()); 84 } 85 } 86 87 } // namespace mozilla::dom