Bug 1461278 - Print a note to the web console when a web site is using shadow DOM (v1), preffed off by default, r=emilio
authorOlli Pettay <Olli.Pettay@helsinki.fi>
Tue, 15 May 2018 02:04:32 +0300
changeset 418294 887c21926833c5a0e84b9d5caba84f7a9d4d2091
parent 418293 dbb42b18303aaad9d8985568247b85f1bbb4b7cb
child 418295 7581c1e47d4dd23a4b00c9627fbfc626cf48c087
push id33997
push userncsoregi@mozilla.com
push dateTue, 15 May 2018 09:53:53 +0000
treeherdermozilla-central@cf3ee1402348 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio
bugs1461278
milestone62.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 1461278 - Print a note to the web console when a web site is using shadow DOM (v1), preffed off by default, r=emilio
dom/base/Element.cpp
dom/base/nsDocument.cpp
dom/base/nsIDocument.h
modules/libpref/init/StaticPrefList.h
--- a/dom/base/Element.cpp
+++ b/dom/base/Element.cpp
@@ -9,16 +9,17 @@
  * of DOM Core's Element, implements nsIContent, provides
  * utility methods for subclasses, and so forth.
  */
 
 #include "mozilla/dom/ElementInlines.h"
 
 #include "AnimationCommon.h"
 #include "mozilla/DebugOnly.h"
+#include "mozilla/StaticPrefs.h"
 #include "mozilla/dom/Animation.h"
 #include "mozilla/dom/Attr.h"
 #include "mozilla/dom/Flex.h"
 #include "mozilla/dom/Grid.h"
 #include "mozilla/gfx/Matrix.h"
 #include "nsAtom.h"
 #include "nsCSSFrameConstructor.h"
 #include "nsDOMAttributeMap.h"
@@ -1242,16 +1243,20 @@ Element::AttachShadow(const ShadowRootIn
    *    context object’s node document, host is context object,
    *    and mode is init’s mode.
    */
   RefPtr<ShadowRoot> shadowRoot =
     new ShadowRoot(this, aInit.mMode, nodeInfo.forget());
 
   shadowRoot->SetIsComposedDocParticipant(IsInComposedDoc());
 
+  if (StaticPrefs::dom_webcomponents_shadowdom_report_usage()) {
+    OwnerDoc()->ReportShadowDOMUsage();
+  }
+
   /**
    * 5. Set context object’s shadow root to shadow.
    */
   SetShadowRoot(shadowRoot);
 
   /**
    * 6. Return shadow.
    */
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -1451,16 +1451,17 @@ nsIDocument::nsIDocument()
     mAllowZoom(false),
     mAllowDoubleTapZoom(false),
     mValidScaleFloat(false),
     mValidMaxScale(false),
     mScaleStrEmpty(false),
     mWidthStrEmpty(false),
     mParserAborted(false),
     mReportedUseCounters(false),
+    mHasReportedShadowDOMUsage(false),
 #ifdef DEBUG
     mWillReparent(false),
 #endif
     mPendingFullscreenRequests(0),
     mXMLDeclarationBits(0),
     mOnloadBlockCount(0),
     mAsyncOnloadBlockCount(0),
     mCompatMode(eCompatibility_FullStandards),
@@ -13215,8 +13216,32 @@ nsIDocument::ModuleScriptsEnabled()
   static bool sCachedPref = false;
   if (!sCachedPref) {
     sCachedPref = true;
     Preferences::AddBoolVarCache(&sEnabledForContent, "dom.moduleScripts.enabled", false);
   }
 
   return nsContentUtils::IsChromeDoc(this) || sEnabledForContent;
 }
+
+void
+nsIDocument::ReportShadowDOMUsage()
+{
+  if (mHasReportedShadowDOMUsage) {
+    return;
+  }
+
+  nsIDocument* topLevel = GetTopLevelContentDocument();
+  if (topLevel && !topLevel->mHasReportedShadowDOMUsage) {
+    topLevel->mHasReportedShadowDOMUsage = true;
+    nsString uri;
+    Unused << topLevel->GetDocumentURI(uri);
+    if (!uri.IsEmpty()) {
+      nsAutoString msg = NS_LITERAL_STRING("Shadow DOM used in [") + uri +
+        NS_LITERAL_STRING("] or in some of its subdocuments.");
+      nsContentUtils::ReportToConsoleNonLocalized(msg, nsIScriptError::infoFlag,
+                                                  NS_LITERAL_CSTRING("DOM"),
+                                                  topLevel);
+    }
+  }
+
+  mHasReportedShadowDOMUsage = true;
+}
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -3531,16 +3531,18 @@ public:
    * be aFrame's content node if that content is in this document and not
    * anonymous. Otherwise, when aFrame is in a subdocument, we use the frame
    * element containing the subdocument containing aFrame, and/or find the
    * nearest non-anonymous ancestor in this document.
    * Returns null if there is no such element.
    */
   nsIContent* GetContentInThisDocument(nsIFrame* aFrame) const;
 
+  void ReportShadowDOMUsage();
+
 protected:
   already_AddRefed<nsIPrincipal> MaybeDowngradePrincipal(nsIPrincipal* aPrincipal);
 
   void EnsureOnloadBlocker();
 
   void SendToConsole(nsCOMArray<nsISecurityConsoleMessage>& aMessages);
 
   // Returns true if the scheme for the url for this document is "about".
@@ -4047,16 +4049,18 @@ protected:
   // Whether we have reported use counters for this document with Telemetry yet.
   // Normally this is only done at document destruction time, but for image
   // documents (SVG documents) that are not guaranteed to be destroyed, we
   // report use counters when the image cache no longer has any imgRequestProxys
   // pointing to them.  We track whether we ever reported use counters so
   // that we only report them once for the document.
   bool mReportedUseCounters: 1;
 
+  bool mHasReportedShadowDOMUsage: 1;
+
 #ifdef DEBUG
 public:
   bool mWillReparent: 1;
 protected:
 #endif
 
   uint8_t mPendingFullscreenRequests;
 
--- a/modules/libpref/init/StaticPrefList.h
+++ b/modules/libpref/init/StaticPrefList.h
@@ -83,16 +83,26 @@
 
 VARCACHE_PREF(
   "accessibility.monoaudio.enable",
    accessibility_monoaudio_enable,
   bool, false
 )
 
 //---------------------------------------------------------------------------
+// DOM prefs
+//---------------------------------------------------------------------------
+
+VARCACHE_PREF(
+  "dom.webcomponents.shadowdom.report_usage",
+   dom_webcomponents_shadowdom_report_usage,
+  bool, false
+)
+
+//---------------------------------------------------------------------------
 // Full-screen prefs
 //---------------------------------------------------------------------------
 
 #ifdef RELEASE_OR_BETA
 # define PREF_VALUE false
 #else
 # define PREF_VALUE true
 #endif