Bug 1508310 - Implement Report-to header support - part 3 - JSON serialization of ReportBody, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Sat, 01 Dec 2018 21:26:09 +0100
changeset 505577 bbcbf470d213b8556c770f85a5cf3ffe216b2c13
parent 505576 b8e709a8076fa6a5dfd68a045993e2650e124bf6
child 505578 ad2c155ce3518b4304161897a850620aa8912291
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1508310
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1508310 - Implement Report-to header support - part 3 - JSON serialization of ReportBody, r=smaug
dom/reporting/DeprecationReportBody.cpp
dom/reporting/DeprecationReportBody.h
dom/reporting/FeaturePolicyViolationReportBody.cpp
dom/reporting/FeaturePolicyViolationReportBody.h
dom/reporting/ReportBody.cpp
dom/reporting/ReportBody.h
--- a/dom/reporting/DeprecationReportBody.cpp
+++ b/dom/reporting/DeprecationReportBody.cpp
@@ -1,16 +1,17 @@
 /* -*- 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/DeprecationReportBody.h"
 #include "mozilla/dom/ReportingBinding.h"
+#include "mozilla/JSONWriter.h"
 
 namespace mozilla {
 namespace dom {
 
 DeprecationReportBody::DeprecationReportBody(
     nsPIDOMWindowInner* aWindow, const nsAString& aId,
     const Nullable<Date>& aDate, const nsAString& aMessage,
     const nsAString& aSourceFile, const Nullable<uint32_t>& aLineNumber,
@@ -49,10 +50,35 @@ void DeprecationReportBody::GetSourceFil
 Nullable<uint32_t> DeprecationReportBody::GetLineNumber() const {
   return mLineNumber;
 }
 
 Nullable<uint32_t> DeprecationReportBody::GetColumnNumber() const {
   return mColumnNumber;
 }
 
+void DeprecationReportBody::ToJSONInternal(JSONWriter& aWriter) const {
+  aWriter.StringProperty("id", NS_ConvertUTF16toUTF8(mId).get());
+  // TODO: anticipatedRemoval? https://github.com/w3c/reporting/issues/132
+  aWriter.StringProperty("message", NS_ConvertUTF16toUTF8(mMessage).get());
+
+  if (mSourceFile.IsEmpty()) {
+    aWriter.NullProperty("sourceFile");
+  } else {
+    aWriter.StringProperty("sourceFile",
+                           NS_ConvertUTF16toUTF8(mSourceFile).get());
+  }
+
+  if (mLineNumber.IsNull()) {
+    aWriter.NullProperty("lineNumber");
+  } else {
+    aWriter.IntProperty("lineNumber", mLineNumber.Value());
+  }
+
+  if (mColumnNumber.IsNull()) {
+    aWriter.NullProperty("columnNumber");
+  } else {
+    aWriter.IntProperty("columnNumber", mColumnNumber.Value());
+  }
+}
+
 }  // namespace dom
 }  // namespace mozilla
--- a/dom/reporting/DeprecationReportBody.h
+++ b/dom/reporting/DeprecationReportBody.h
@@ -31,16 +31,19 @@ class DeprecationReportBody final : publ
   void GetMessage(nsAString& aMessage) const;
 
   void GetSourceFile(nsAString& aSourceFile) const;
 
   Nullable<uint32_t> GetLineNumber() const;
 
   Nullable<uint32_t> GetColumnNumber() const;
 
+ protected:
+  void ToJSONInternal(JSONWriter& aJSONWriter) const override;
+
  private:
   ~DeprecationReportBody();
 
   const nsString mId;
   const Nullable<Date> mDate;
   const nsString mMessage;
   const nsString mSourceFile;
   const Nullable<uint32_t> mLineNumber;
--- a/dom/reporting/FeaturePolicyViolationReportBody.cpp
+++ b/dom/reporting/FeaturePolicyViolationReportBody.cpp
@@ -46,10 +46,37 @@ Nullable<int32_t> FeaturePolicyViolation
   return mColumnNumber;
 }
 
 void FeaturePolicyViolationReportBody::GetDisposition(
     nsAString& aDisposition) const {
   aDisposition = mDisposition;
 }
 
+void FeaturePolicyViolationReportBody::ToJSONInternal(
+    JSONWriter& aWriter) const {
+  aWriter.StringProperty("featureId", NS_ConvertUTF16toUTF8(mFeatureId).get());
+
+  if (mSourceFile.IsEmpty()) {
+    aWriter.NullProperty("sourceFile");
+  } else {
+    aWriter.StringProperty("sourceFile",
+                           NS_ConvertUTF16toUTF8(mSourceFile).get());
+  }
+
+  if (mLineNumber.IsNull()) {
+    aWriter.NullProperty("lineNumber");
+  } else {
+    aWriter.IntProperty("lineNumber", mLineNumber.Value());
+  }
+
+  if (mColumnNumber.IsNull()) {
+    aWriter.NullProperty("columnNumber");
+  } else {
+    aWriter.IntProperty("columnNumber", mColumnNumber.Value());
+  }
+
+  aWriter.StringProperty("disposition",
+                         NS_ConvertUTF16toUTF8(mDisposition).get());
+}
+
 }  // namespace dom
 }  // namespace mozilla
--- a/dom/reporting/FeaturePolicyViolationReportBody.h
+++ b/dom/reporting/FeaturePolicyViolationReportBody.h
@@ -30,16 +30,19 @@ class FeaturePolicyViolationReportBody f
   void GetSourceFile(nsAString& aSourceFile) const;
 
   Nullable<int32_t> GetLineNumber() const;
 
   Nullable<int32_t> GetColumnNumber() const;
 
   void GetDisposition(nsAString& aDisposition) const;
 
+ protected:
+  void ToJSONInternal(JSONWriter& aJSONWriter) const override;
+
  private:
   ~FeaturePolicyViolationReportBody();
 
   const nsString mFeatureId;
   const nsString mSourceFile;
   const Nullable<int32_t> mLineNumber;
   const Nullable<int32_t> mColumnNumber;
   const nsString mDisposition;
--- a/dom/reporting/ReportBody.cpp
+++ b/dom/reporting/ReportBody.cpp
@@ -26,10 +26,31 @@ ReportBody::ReportBody(nsPIDOMWindowInne
 
 ReportBody::~ReportBody() = default;
 
 JSObject* ReportBody::WrapObject(JSContext* aCx,
                                  JS::Handle<JSObject*> aGivenProto) {
   return ReportBody_Binding::Wrap(aCx, this, aGivenProto);
 }
 
+namespace {
+
+struct StringWriteFunc : public JSONWriteFunc {
+  nsAString& mBuffer;  // The lifetime of the struct must be bound to the buffer
+  explicit StringWriteFunc(nsAString& aBuffer) : mBuffer(aBuffer) {}
+
+  void Write(const char* aStr) override {
+    mBuffer.Append(NS_ConvertUTF8toUTF16(aStr));
+  }
+};
+
+}  // namespace
+
+void ReportBody::ToJSON(nsAString& aJSON) const {
+  JSONWriter w(MakeUnique<StringWriteFunc>(aJSON));
+
+  w.Start();
+  ToJSONInternal(w);
+  w.End();
+}
+
 }  // namespace dom
 }  // namespace mozilla
--- a/dom/reporting/ReportBody.h
+++ b/dom/reporting/ReportBody.h
@@ -10,32 +10,39 @@
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/BindingUtils.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsWrapperCache.h"
 
 class nsPIDOMWindowInner;
 
 namespace mozilla {
+
+class JSONWriter;
+
 namespace dom {
 
 class ReportBody : public nsISupports, public nsWrapperCache {
  public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ReportBody)
 
   explicit ReportBody(nsPIDOMWindowInner* aWindow);
 
   virtual JSObject* WrapObject(JSContext* aCx,
                                JS::Handle<JSObject*> aGivenProto) override;
 
   nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
 
+  void ToJSON(nsAString& aJSON) const;
+
  protected:
   virtual ~ReportBody();
 
+  virtual void ToJSONInternal(JSONWriter& aJSONWriter) const = 0;
+
   nsCOMPtr<nsPIDOMWindowInner> mWindow;
 };
 
 }  // namespace dom
 }  // namespace mozilla
 
 #endif  // mozilla_dom_ReportBody_h