Bug 1427419 - Part 2: Move nsIDOMUtils.getAllStyleSheets to InspectorUtils. r=bz draft
authorCameron McCormack <cam@mcc.id.au>
Sat, 06 Jan 2018 15:08:13 +0800
changeset 716747 27c2c20a1c5b9825871532da9027910fdb3e7a17
parent 716746 1c5cb6ecca60ce21ba63f004c1b0c36b5db20a21
child 716748 e1292dcaf5c989f1add079614221167ce785434b
push id94496
push userbmo:cam@mcc.id.au
push dateSat, 06 Jan 2018 07:08:40 +0000
reviewersbz
bugs1427419
milestone59.0a1
Bug 1427419 - Part 2: Move nsIDOMUtils.getAllStyleSheets to InspectorUtils. r=bz MozReview-Commit-ID: J6lIx4uvMlS
devtools/server/actors/stylesheets.js
dom/tests/mochitest/chrome/test_parsingMode.html
dom/webidl/InspectorUtils.webidl
layout/inspector/InspectorUtils.h
layout/inspector/inDOMUtils.cpp
layout/inspector/inIDOMUtils.idl
layout/inspector/tests/test_get_all_style_sheets.html
--- a/devtools/server/actors/stylesheets.js
+++ b/devtools/server/actors/stylesheets.js
@@ -11,16 +11,17 @@ const defer = require("devtools/shared/d
 const {Task} = require("devtools/shared/task");
 const protocol = require("devtools/shared/protocol");
 const {LongStringActor} = require("devtools/server/actors/string");
 const {fetch} = require("devtools/shared/DevToolsUtils");
 const {mediaRuleSpec, styleSheetSpec,
        styleSheetsSpec} = require("devtools/shared/specs/stylesheets");
 const {
   addPseudoClassLock, removePseudoClassLock } = require("devtools/server/actors/highlighters/utils/markup");
+const InspectorUtils = require("InspectorUtils");
 
 loader.lazyRequireGetter(this, "CssLogic", "devtools/shared/inspector/css-logic");
 loader.lazyRequireGetter(this, "addPseudoClassLock",
   "devtools/server/actors/highlighters/utils/markup", true);
 loader.lazyRequireGetter(this, "removePseudoClassLock",
   "devtools/server/actors/highlighters/utils/markup", true);
 loader.lazyRequireGetter(this, "loadSheet", "devtools/shared/layout/utils", true);
 
@@ -746,17 +747,18 @@ var StyleSheetsActor = protocol.ActorCla
   _addStyleSheets: function (win) {
     return Task.spawn(function* () {
       let doc = win.document;
       // We have to set this flag in order to get the
       // StyleSheetApplicableStateChanged events.  See Document.webidl.
       doc.styleSheetChangeEventsEnabled = true;
 
       let isChrome = Services.scriptSecurityManager.isSystemPrincipal(doc.nodePrincipal);
-      let styleSheets = isChrome ? DOMUtils.getAllStyleSheets(doc) : doc.styleSheets;
+      let styleSheets =
+        isChrome ? InspectorUtils.getAllStyleSheets(doc) : doc.styleSheets;
       let actors = [];
       for (let i = 0; i < styleSheets.length; i++) {
         let sheet = styleSheets[i];
         if (!this._shouldListSheet(sheet)) {
           continue;
         }
 
         let actor = this.parentActor.createStyleSheetActor(sheet);
--- a/dom/tests/mochitest/chrome/test_parsingMode.html
+++ b/dom/tests/mochitest/chrome/test_parsingMode.html
@@ -21,17 +21,17 @@
     utils.loadSheetUsingURIString(userUrl, sss.USER_SHEET);
 
     const agentUrl = encodeURI("data:text/css,body { color: tomato; }");
     utils.loadSheetUsingURIString(agentUrl, sss.AGENT_SHEET);
 
     const authorUrl = "chrome://mochikit/content/tests/SimpleTest/test.css";
 
     let results = [];
-    for (let sheet of domutils.getAllStyleSheets(document)) {
+    for (let sheet of InspectorUtils.getAllStyleSheets(document)) {
       if (sheet.href === agentUrl) {
         is(sheet.parsingMode, "agent", "agent sheet has expected mode");
         results[sss.AGENT_SHEET] = 1;
       } else if (sheet.href === userUrl) {
         is(sheet.parsingMode, "user", "user sheet has expected mode");
         is(sheet.cssRules[0].style.length, 2, "Chrome-only properties are parsed in user sheet")
         results[sss.USER_SHEET] = 1;
       } else if (sheet.href === authorUrl) {
--- a/dom/webidl/InspectorUtils.webidl
+++ b/dom/webidl/InspectorUtils.webidl
@@ -4,16 +4,17 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
 /**
  * A collection of utility methods for use by devtools.
  */
 [ChromeOnly]
 namespace InspectorUtils {
+  sequence<StyleSheet> getAllStyleSheets(Document document);
 };
 
 dictionary InspectorRGBTriple {
   /*
    * NOTE: Using octet for RGB components is not generally OK, because
    * they can be outside the 0-255 range, but for backwards-compatible
    * named colors (which is what we use this dictionary for) the 0-255
    * assumption is fine.
--- a/layout/inspector/InspectorUtils.h
+++ b/layout/inspector/InspectorUtils.h
@@ -3,22 +3,34 @@
 /* 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_dom_InspectorUtils_h
 #define mozilla_dom_InspectorUtils_h
 
+#include "mozilla/dom/BindingDeclarations.h"
+
+class nsIDocument;
+
+namespace mozilla {
+class StyleSheet;
+} // namespace mozilla
+
 namespace mozilla {
 namespace dom {
 
 /**
  * A collection of utility methods for use by devtools.
  */
 class InspectorUtils
 {
+public:
+  static void GetAllStyleSheets(GlobalObject& aGlobal,
+                                nsIDocument& aDocument,
+                                nsTArray<RefPtr<StyleSheet>>& aResult);
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_InspectorUtils_h
--- a/layout/inspector/inDOMUtils.cpp
+++ b/layout/inspector/inDOMUtils.cpp
@@ -49,16 +49,17 @@
 #include "nsColor.h"
 #include "mozilla/StyleSetHandleInlines.h"
 #include "nsStyleUtil.h"
 #include "nsQueryObject.h"
 #include "mozilla/ServoBindings.h"
 #include "mozilla/ServoStyleRule.h"
 #include "mozilla/ServoStyleRuleMap.h"
 #include "mozilla/ServoCSSParser.h"
+#include "mozilla/dom/InspectorUtils.h"
 
 using namespace mozilla;
 using namespace mozilla::css;
 using namespace mozilla::dom;
 
 ///////////////////////////////////////////////////////////////////////////////
 
 inDOMUtils::inDOMUtils()
@@ -69,71 +70,59 @@ inDOMUtils::~inDOMUtils()
 {
 }
 
 NS_IMPL_ISUPPORTS(inDOMUtils, inIDOMUtils)
 
 ///////////////////////////////////////////////////////////////////////////////
 // inIDOMUtils
 
-NS_IMETHODIMP
-inDOMUtils::GetAllStyleSheets(nsIDOMDocument *aDocument, uint32_t *aLength,
-                              nsISupports ***aSheets)
-{
-  NS_ENSURE_ARG_POINTER(aDocument);
+namespace mozilla {
+namespace dom {
 
-  nsTArray<RefPtr<StyleSheet>> sheets;
-
-  nsCOMPtr<nsIDocument> document = do_QueryInterface(aDocument);
-  MOZ_ASSERT(document);
-
+/* static */ void
+InspectorUtils::GetAllStyleSheets(GlobalObject& aGlobalObject,
+                                  nsIDocument& aDocument,
+                                  nsTArray<RefPtr<StyleSheet>>& aResult)
+{
   // Get the agent, then user and finally xbl sheets in the style set.
-  nsIPresShell* presShell = document->GetShell();
+  nsIPresShell* presShell = aDocument.GetShell();
 
   if (presShell) {
     StyleSetHandle styleSet = presShell->StyleSet();
     SheetType sheetType = SheetType::Agent;
     for (int32_t i = 0; i < styleSet->SheetCount(sheetType); i++) {
-      sheets.AppendElement(styleSet->StyleSheetAt(sheetType, i));
+      aResult.AppendElement(styleSet->StyleSheetAt(sheetType, i));
     }
     sheetType = SheetType::User;
     for (int32_t i = 0; i < styleSet->SheetCount(sheetType); i++) {
-      sheets.AppendElement(styleSet->StyleSheetAt(sheetType, i));
+      aResult.AppendElement(styleSet->StyleSheetAt(sheetType, i));
     }
 
     AutoTArray<StyleSheet*, 32> xblSheetArray;
     styleSet->AppendAllXBLStyleSheets(xblSheetArray);
 
     // The XBL stylesheet array will quite often be full of duplicates. Cope:
     nsTHashtable<nsPtrHashKey<StyleSheet>> sheetSet;
     for (StyleSheet* sheet : xblSheetArray) {
       if (!sheetSet.Contains(sheet)) {
         sheetSet.PutEntry(sheet);
-        sheets.AppendElement(sheet);
+        aResult.AppendElement(sheet);
       }
     }
   }
 
   // Get the document sheets.
-  for (size_t i = 0; i < document->SheetCount(); i++) {
-    sheets.AppendElement(document->SheetAt(i));
+  for (size_t i = 0; i < aDocument.SheetCount(); i++) {
+    aResult.AppendElement(aDocument.SheetAt(i));
   }
-
-  nsISupports** ret = static_cast<nsISupports**>(moz_xmalloc(sheets.Length() *
-                                                 sizeof(nsISupports*)));
+}
 
-  for (size_t i = 0; i < sheets.Length(); i++) {
-    NS_ADDREF(ret[i] = NS_ISUPPORTS_CAST(nsIDOMCSSStyleSheet*, sheets[i]));
-  }
-
-  *aLength = sheets.Length();
-  *aSheets = ret;
-
-  return NS_OK;
-}
+} // namespace dom
+} // namespace mozilla
 
 NS_IMETHODIMP
 inDOMUtils::IsIgnorableWhitespace(nsIDOMCharacterData *aDataNode,
                                   bool *aReturn)
 {
   NS_PRECONDITION(aReturn, "Must have an out parameter");
 
   NS_ENSURE_ARG_POINTER(aDataNode);
--- a/layout/inspector/inIDOMUtils.idl
+++ b/layout/inspector/inIDOMUtils.idl
@@ -15,20 +15,16 @@ interface nsIDOMNode;
 interface nsIDOMNodeList;
 interface nsIDOMFontFaceList;
 interface nsIDOMRange;
 interface nsIDOMCSSStyleSheet;
 
 [scriptable, uuid(362e98c3-82c2-4ad8-8dcb-00e8e4eab497)]
 interface inIDOMUtils : nsISupports
 {
-  // CSS utilities
-  void getAllStyleSheets (in nsIDOMDocument aDoc,
-                          [optional] out unsigned long aLength,
-                          [array, size_is (aLength), retval] out nsISupports aSheets);
   nsIArrayExtensions getCSSStyleRules(in nsIDOMElement aElement, [optional] in DOMString aPseudo);
 
   /**
    * Get the line number of a rule.
    *
    * @param nsIDOMCSSRule aRule The rule.
    * @return The rule's line number.  Line numbers are 1-based.
    */
--- a/layout/inspector/tests/test_get_all_style_sheets.html
+++ b/layout/inspector/tests/test_get_all_style_sheets.html
@@ -7,22 +7,22 @@ https://bugzilla.mozilla.org/show_bug.cg
   <meta charset="utf-8">
   <title>Test for Bug 734861</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript">
 
   /** Test for Bug 734861 **/
 
+  const InspectorUtils = SpecialPowers.InspectorUtils;
+
   SimpleTest.waitForExplicitFinish();
 
   function runTest() {
-    var utils = SpecialPowers.Cc["@mozilla.org/inspector/dom-utils;1"]
-                             .getService(SpecialPowers.Ci.inIDOMUtils);
-    var res = utils.getAllStyleSheets(document);
+    var res = InspectorUtils.getAllStyleSheets(document);
 
     var foundUA = false;
     for (var i = 0; i < res.length; i++) {
       if (res[i].href === "resource://gre-resources/ua.css") {
         foundUA = true;
         break;
       }
     }