Bug 1378825 - Stop using sdk/stylesheet/utils in DevTools; r=zer0 draft
authorJan Odvarko <odvarko@gmail.com>
Fri, 07 Jul 2017 11:49:24 +0200
changeset 605293 f9a822032a3f923ddc78e1c88f8e7207f5200973
parent 605055 20f32734df750bddada9d1edca665c2ea53946f0
child 636443 2cab19cfd644bb0fd74650cc25168f8641af295f
push id67353
push userjodvarko@mozilla.com
push dateFri, 07 Jul 2017 09:50:21 +0000
reviewerszer0
bugs1378825
milestone56.0a1
Bug 1378825 - Stop using sdk/stylesheet/utils in DevTools; r=zer0 MozReview-Commit-ID: F26gvdnTbhT
devtools/client/shared/stylesheet-utils.js
devtools/client/shared/theme-switching.js
devtools/client/webconsole/net/main.js
--- a/devtools/client/shared/stylesheet-utils.js
+++ b/devtools/client/shared/stylesheet-utils.js
@@ -1,15 +1,24 @@
 /* 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/. */
 
 /* eslint-env browser */
+
 "use strict";
 
+const { Ci } = require("chrome");
+
+const SHEET_TYPE = {
+  "agent": "AGENT_SHEET",
+  "user": "USER_SHEET",
+  "author": "AUTHOR_SHEET"
+};
+
 /*
  * Append a stylesheet to the provided XUL document.
  *
  * @param  {Document} xulDocument
  *         The XUL document where the stylesheet should be appended.
  * @param  {String} url
  *         The url of the stylesheet to load.
  * @return {Object}
@@ -35,9 +44,63 @@ function appendStyleSheet(xulDocument, u
 
     styleSheet.addEventListener("load", onload);
     styleSheet.addEventListener("error", onerror);
   });
   xulDocument.insertBefore(styleSheet, xulDocument.documentElement);
   return {styleSheet, loadPromise};
 }
 
+/**
+ * Synchronously loads a style sheet from `uri` and adds it to the list of
+ * additional style sheets of the document.
+ * The sheets added takes effect immediately, and only on the document of the
+ * `window` given.
+ */
+function loadSheet(window, url, type) {
+  if (!(type && type in SHEET_TYPE)) {
+    type = "author";
+  }
+
+  type = SHEET_TYPE[type];
+
+  if (url instanceof Ci.nsIURI) {
+    url = url.spec;
+  }
+
+  let winUtils = getDOMWindowUtils(window);
+  try {
+    winUtils.loadSheetUsingURIString(url, winUtils[type]);
+  } catch (e) {
+  }
+}
+
+/**
+ * Remove the document style sheet at `sheetURI` from the list of additional
+ * style sheets of the document.  The removal takes effect immediately.
+ */
+function removeSheet(window, url, type) {
+  if (!(type && type in SHEET_TYPE)) {
+    type = "author";
+  }
+
+  type = SHEET_TYPE[type];
+
+  if (url instanceof Ci.nsIURI) {
+    url = url.spec;
+  }
+
+  let winUtils = getDOMWindowUtils(window);
+
+  try {
+    winUtils.removeSheetUsingURIString(url, winUtils[type]);
+  } catch (e) {
+  }
+}
+
+function getDOMWindowUtils(window) {
+  return window.QueryInterface(Ci.nsIInterfaceRequestor)
+    .getInterface(Ci.nsIDOMWindowUtils);
+}
+
 exports.appendStyleSheet = appendStyleSheet;
+exports.loadSheet = loadSheet;
+exports.removeSheet = removeSheet;
--- a/devtools/client/shared/theme-switching.js
+++ b/devtools/client/shared/theme-switching.js
@@ -87,17 +87,17 @@
     let loadEvents = [];
     for (let url of newThemeDef.stylesheets) {
       let {styleSheet, loadPromise} = appendStyleSheet(document, url);
       devtoolsStyleSheets.get(newThemeDef).push(styleSheet);
       loadEvents.push(loadPromise);
     }
 
     try {
-      const StylesheetUtils = require("sdk/stylesheet/utils");
+      const StylesheetUtils = require("devtools/client/shared/stylesheet-utils");
       const SCROLLBARS_URL = "chrome://devtools/skin/floating-scrollbars-dark-theme.css";
 
       // TODO: extensions might want to customize scrollbar styles too.
       if (!Services.appShell.hiddenDOMWindow
         .matchMedia("(-moz-overlay-scrollbars)").matches) {
         if (newTheme == "dark") {
           StylesheetUtils.loadSheet(window, SCROLLBARS_URL, "agent");
         } else if (oldTheme == "dark") {
--- a/devtools/client/webconsole/net/main.js
+++ b/devtools/client/webconsole/net/main.js
@@ -1,27 +1,25 @@
 /* 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/. */
 "use strict";
 
 /* global BrowserLoader */
 
-var { utils: Cu } = Components;
-
 // Initialize module loader and load all modules of the new inline
 // preview feature. The entire code-base doesn't need any extra
 // privileges and runs entirely in content scope.
 const rootUrl = "resource://devtools/client/webconsole/net/";
 const require = BrowserLoader({
   baseURI: rootUrl,
   window}).require;
 
 const NetRequest = require("./net-request");
-const { loadSheet } = require("sdk/stylesheet/utils");
+const { loadSheet } = require("devtools/client/shared/stylesheet-utils");
 
 // Localization
 const {LocalizationHelper} = require("devtools/shared/l10n");
 const L10N = new LocalizationHelper("devtools/client/locales/netmonitor.properties");
 
 // Stylesheets
 var styleSheets = [
   "resource://devtools/client/jsonview/css/toolbar.css",
@@ -53,16 +51,17 @@ styleSheets.forEach(url => {
 // https://github.com/yahoo/react-intl
 this.Locale = {
   $STR: key => {
     try {
       return L10N.getStr(key);
     } catch (err) {
       console.error(key + ": " + err);
     }
+    return key;
   }
 };
 
 // List of NetRequest instances represents the state.
 // As soon as Redux is in place it should be maintained using a reducer.
 var netRequests = new Map();
 
 /**
@@ -83,16 +82,14 @@ function onNetworkEvent(log) {
 
   if (!netRequest) {
     return;
   }
 
   if (log.update) {
     netRequest.updateBody(response);
   }
-
-  return;
 }
 
 // Make the 'onNetworkEvent' accessible from chrome (see webconsole.js)
 this.NetRequest = {
   onNetworkEvent: onNetworkEvent
 };