Bug 1215140 P1 Add an nsIConsoleReportCollector interface to support navigation channel logging. r=bz a=ritu l10n=ritu
authorBen Kelly <ben@wanderview.com>
Thu, 29 Oct 2015 19:53:25 -0700
changeset 305426 b797300e0d27762ffbc057d4328ea57abe6c2a9f
parent 305425 07fa57ab14896f4ca6f3f91c7b6178c8dea2edb2
child 305427 e3919fe4c6387305b793cf8d103d4521fa21e3db
push id1001
push userraliiev@mozilla.com
push dateMon, 18 Jan 2016 19:06:03 +0000
treeherdermozilla-release@8b89261f3ac4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz, ritu
bugs1215140
milestone44.0a2
Bug 1215140 P1 Add an nsIConsoleReportCollector interface to support navigation channel logging. r=bz a=ritu l10n=ritu
dom/base/ConsoleReportCollector.cpp
dom/base/ConsoleReportCollector.h
dom/base/moz.build
dom/base/nsIConsoleReportCollector.h
new file mode 100644
--- /dev/null
+++ b/dom/base/ConsoleReportCollector.cpp
@@ -0,0 +1,83 @@
+/* -*- 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/ConsoleReportCollector.h"
+
+#include "nsNetUtil.h"
+
+namespace mozilla {
+
+NS_IMPL_ISUPPORTS(ConsoleReportCollector, nsIConsoleReportCollector)
+
+ConsoleReportCollector::ConsoleReportCollector()
+  : mMutex("mozilla::ConsoleReportCollector")
+{
+}
+
+void
+ConsoleReportCollector::AddConsoleReport(uint32_t aErrorFlags,
+                                         const nsACString& aCategory,
+                                         nsContentUtils::PropertiesFile aPropertiesFile,
+                                         const nsACString& aSourceFileURI,
+                                         uint32_t aLineNumber,
+                                         uint32_t aColumnNumber,
+                                         const nsACString& aMessageName,
+                                         const nsTArray<nsString>& aStringParams)
+{
+  // any thread
+  MutexAutoLock lock(mMutex);
+
+  mPendingReports.AppendElement(PendingReport(aErrorFlags, aCategory,
+                                              aPropertiesFile, aSourceFileURI,
+                                              aLineNumber, aColumnNumber,
+                                              aMessageName, aStringParams));
+}
+
+void
+ConsoleReportCollector::FlushConsoleReports(nsIDocument* aDocument)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  MutexAutoLock lock(mMutex);
+
+  for (uint32_t i = 0; i < mPendingReports.Length(); ++i) {
+    PendingReport& report = mPendingReports[i];
+
+    // It would be nice if we did not have to do this since ReportToConsole()
+    // just turns around and converts it back to a spec.
+    nsCOMPtr<nsIURI> uri;
+    nsresult rv = NS_NewURI(getter_AddRefs(uri), report.mSourceFileURI);
+    if (NS_FAILED(rv)) {
+      continue;
+    }
+
+    // Convert back from nsTArray<nsString> to the char16_t** format required
+    // by our l10n libraries and ReportToConsole. (bug 1219762)
+    UniquePtr<char16_t*> params;
+    uint32_t paramsLength = report.mStringParams.Length();
+    if (paramsLength > 0) {
+      params.reset(new char16_t*[paramsLength]);
+      for (uint32_t j = 0; j < paramsLength; ++j) {
+        params.get()[j] = const_cast<char16_t*>(report.mStringParams[j].get());
+      }
+    }
+
+    nsContentUtils::ReportToConsole(report.mErrorFlags, report.mCategory,
+                                    aDocument, report.mPropertiesFile,
+                                    report.mMessageName.get(),
+                                    const_cast<const char16_t**>(params.get()),
+                                    paramsLength, uri, EmptyString(),
+                                    report.mLineNumber, report.mColumnNumber);
+  }
+
+  mPendingReports.Clear();
+}
+
+ConsoleReportCollector::~ConsoleReportCollector()
+{
+}
+
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/base/ConsoleReportCollector.h
@@ -0,0 +1,73 @@
+/* -*- 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/. */
+
+#ifndef mozilla_ConsoleReportCollector_h
+#define mozilla_ConsoleReportCollector_h
+
+#include "mozilla/Mutex.h"
+#include "nsIConsoleReportCollector.h"
+#include "nsTArray.h"
+
+namespace mozilla {
+
+class ConsoleReportCollector final : public nsIConsoleReportCollector
+{
+public:
+  ConsoleReportCollector();
+
+  void
+  AddConsoleReport(uint32_t aErrorFlags, const nsACString& aCategory,
+                   nsContentUtils::PropertiesFile aPropertiesFile,
+                   const nsACString& aSourceFileURI,
+                   uint32_t aLineNumber, uint32_t aColumnNumber,
+                   const nsACString& aMessageName,
+                   const nsTArray<nsString>& aStringParams) override;
+
+  void
+  FlushConsoleReports(nsIDocument* aDocument) override;
+
+private:
+  ~ConsoleReportCollector();
+
+  struct PendingReport
+  {
+    PendingReport(uint32_t aErrorFlags, const nsACString& aCategory,
+               nsContentUtils::PropertiesFile aPropertiesFile,
+               const nsACString& aSourceFileURI, uint32_t aLineNumber,
+               uint32_t aColumnNumber, const nsACString& aMessageName,
+               const nsTArray<nsString>& aStringParams)
+      : mErrorFlags(aErrorFlags)
+      , mCategory(aCategory)
+      , mPropertiesFile(aPropertiesFile)
+      , mSourceFileURI(aSourceFileURI)
+      , mLineNumber(aLineNumber)
+      , mColumnNumber(aColumnNumber)
+      , mMessageName(aMessageName)
+      , mStringParams(aStringParams)
+    { }
+
+    const uint32_t mErrorFlags;
+    const nsCString mCategory;
+    const nsContentUtils::PropertiesFile mPropertiesFile;
+    const nsCString mSourceFileURI;
+    const uint32_t mLineNumber;
+    const uint32_t mColumnNumber;
+    const nsCString mMessageName;
+    const nsTArray<nsString> mStringParams;
+  };
+
+  Mutex mMutex;
+
+  // protected by mMutex
+  nsTArray<PendingReport> mPendingReports;
+
+public:
+  NS_DECL_THREADSAFE_ISUPPORTS
+};
+
+} // namespace mozilla
+
+#endif // mozilla_ConsoleReportCollector_h
--- a/dom/base/moz.build
+++ b/dom/base/moz.build
@@ -79,16 +79,17 @@ EXPORTS += [
     'nsFrameMessageManager.h',
     'nsGenericDOMDataNode.h',
     'nsGkAtomList.h',
     'nsGkAtoms.h',
     'nsHostObjectProtocolHandler.h',
     'nsHostObjectURI.h',
     'nsIAnimationObserver.h',
     'nsIAttribute.h',
+    'nsIConsoleReportCollector.h',
     'nsIContent.h',
     'nsIContentInlines.h',
     'nsIContentIterator.h',
     'nsIContentSerializer.h',
     'nsIDocument.h',
     'nsIDocumentInlines.h',
     'nsIDocumentObserver.h',
     'nsIDOMClassInfo.h',
@@ -137,16 +138,17 @@ EXPORTS += [
 
 if CONFIG['MOZ_WEBRTC']:
     EXPORTS += [
         'nsDOMDataChannel.h',
         'nsDOMDataChannelDeclarations.h',
     ]
 
 EXPORTS.mozilla += [
+    'ConsoleReportCollector.h',
     'CORSMode.h',
     'FeedWriterEnabled.h',
     'TextInputProcessor.h',
     'UseCounter.h',
 ]
 
 EXPORTS.mozilla.dom += [
     'AnonymousContent.h',
@@ -217,16 +219,17 @@ UNIFIED_SOURCES += [
     'AnonymousContent.cpp',
     'Attr.cpp',
     'BarProps.cpp',
     'ChildIterator.cpp',
     'ChromeNodeList.cpp',
     'ChromeUtils.cpp',
     'Comment.cpp',
     'Console.cpp',
+    'ConsoleReportCollector.cpp',
     'Crypto.cpp',
     'DirectionalityUtils.cpp',
     'DocumentFragment.cpp',
     'DocumentType.cpp',
     'DOMCursor.cpp',
     'DOMError.cpp',
     'DOMException.cpp',
     'DOMImplementation.cpp',
new file mode 100644
--- /dev/null
+++ b/dom/base/nsIConsoleReportCollector.h
@@ -0,0 +1,81 @@
+/* -*- 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/. */
+
+#ifndef nsIConsoleReportCollector_h
+#define nsIConsoleReportCollector_h
+
+#include "nsContentUtils.h"
+#include "nsISupports.h"
+#include "nsTArrayForwardDeclare.h"
+
+class nsACString;
+class nsIDocument;
+class nsString;
+
+#define NS_NSICONSOLEREPORTCOLLECTOR_IID \
+  {0xdd98a481, 0xd2c4, 0x4203, {0x8d, 0xfa, 0x85, 0xbf, 0xd7, 0xdc, 0xd7, 0x05}}
+
+// An interface for saving reports until we can flush them to the correct
+// window at a later time.
+class NS_NO_VTABLE nsIConsoleReportCollector : public nsISupports
+{
+public:
+  NS_DECLARE_STATIC_IID_ACCESSOR(NS_NSICONSOLEREPORTCOLLECTOR_IID)
+
+  // Add a pending report to be later displayed on the console.  This may be
+  // called from any thread.
+  //
+  // aErrorFlags      A nsIScriptError flags value.
+  // aCategory        Name of module reporting error.
+  // aPropertiesFile  Properties file containing localized message.
+  // aSourceFileURI   The URI of the script generating the error. Must be a URI
+  //                  spec.
+  // aLineNumber      The line number where the error was generated. May be 0 if
+  //                  the line number is not known.
+  // aColumnNumber    The column number where the error was generated. May be 0
+  //                  if the line number is not known.
+  // aMessageName     The name of the localized message contained in the
+  //                  properties file.
+  // aStringParams    An array of nsString parameters to use when localizing the
+  //                  message.
+  virtual void
+  AddConsoleReport(uint32_t aErrorFlags, const nsACString& aCategory,
+                   nsContentUtils::PropertiesFile aPropertiesFile,
+                   const nsACString& aSourceFileURI, uint32_t aLineNumber,
+                   uint32_t aColumnNumber, const nsACString& aMessageName,
+                   const nsTArray<nsString>& aStringParams) = 0;
+
+  // A version of AddConsoleReport() that accepts the message parameters
+  // as variable nsString arguments.  Note, the parameters must be exactly
+  // nsString and not another string class.  All other args the same as
+  // AddConsoleReport().
+  template<typename... Params>
+  void
+  AddConsoleReport(uint32_t aErrorFlags, const nsACString& aCategory,
+                   nsContentUtils::PropertiesFile aPropertiesFile,
+                   const nsACString& aSourceFileURI, uint32_t aLineNumber,
+                   uint32_t aColumnNumber, const nsACString& aMessageName,
+                   Params... aParams)
+  {
+    nsTArray<nsString> params;
+    mozilla::dom::StringArrayAppender::Append(params, sizeof...(Params), aParams...);
+    AddConsoleReport(aErrorFlags, aCategory, aPropertiesFile, aSourceFileURI,
+                     aLineNumber, aColumnNumber, aMessageName, params);
+  }
+
+  // Flush all pending reports to the console.
+  //
+  // aDocument      An optional document representing where to flush the
+  //                reports.  If provided, then the corresponding window's
+  //                web console will get the reports.  Otherwise the reports
+  //                go to the browser console.
+  virtual void
+  FlushConsoleReports(nsIDocument* aDocument) = 0;
+};
+
+NS_DEFINE_STATIC_IID_ACCESSOR(nsIConsoleReportCollector, NS_NSICONSOLEREPORTCOLLECTOR_IID)
+
+#endif // nsIConsoleReportCollector_h