Bug 1302996 - Remove SDK dependencies for JSONView modules loaded on startup. r=jryans
☠☠ backed out by fc08c9691328 ☠ ☠
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 15 Nov 2016 09:30:46 -0800
changeset 324551 8a97f217ebe82241078eb93fcb7540ffdef78109
parent 324550 45bcf45afca2fcea5185abdefc8b878f26764a97
child 324552 758c23c92f087cf1253f97f4cc30201eb349224a
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersjryans
bugs1302996
milestone53.0a1
Bug 1302996 - Remove SDK dependencies for JSONView modules loaded on startup. r=jryans MozReview-Commit-ID: AClST5BXfK1
devtools/client/jsonview/converter-child.js
devtools/client/jsonview/converter-sniffer.js
--- a/devtools/client/jsonview/converter-child.js
+++ b/devtools/client/jsonview/converter-child.js
@@ -1,61 +1,65 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set ft=javascript ts=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/. */
 
 "use strict";
 
-const {Cu, Cc, Ci, components} = require("chrome");
+const {Cc, Ci, Cu, Cm, Cr, components} = require("chrome");
+const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
+const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
 const Services = require("Services");
-const {Class} = require("sdk/core/heritage");
-const {Unknown} = require("sdk/platform/xpcom");
-const xpcom = require("sdk/platform/xpcom");
-const Events = require("sdk/dom/events");
-const Clipboard = require("sdk/clipboard");
 
 loader.lazyRequireGetter(this, "NetworkHelper",
                                "devtools/shared/webconsole/network-helper");
+loader.lazyRequireGetter(this, "Events",
+                               "sdk/dom/events");
+loader.lazyRequireGetter(this, "Clipboard",
+                               "sdk/clipboard");
 loader.lazyRequireGetter(this, "JsonViewUtils",
                                "devtools/client/jsonview/utils");
 
 const childProcessMessageManager =
   Cc["@mozilla.org/childprocessmessagemanager;1"]
     .getService(Ci.nsISyncMessageSender);
 
 // Amount of space that will be allocated for the stream's backing-store.
 // Must be power of 2. Used to copy the data stream in onStopRequest.
 const SEGMENT_SIZE = Math.pow(2, 17);
 
 const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
 const CONTRACT_ID = "@mozilla.org/streamconv;1?from=" +
   JSON_VIEW_MIME_TYPE + "&to=*/*";
-const CLASS_ID = "{d8c9acee-dec5-11e4-8c75-1681e6b88ec1}";
+const CLASS_ID = components.ID("{d8c9acee-dec5-11e4-8c75-1681e6b88ec1}");
+const CLASS_DESCRIPTION = "JSONView converter";
 
 // Localization
-let jsonViewStrings = Services.strings.createBundle(
-  "chrome://devtools/locale/jsonview.properties");
+loader.lazyGetter(this, "jsonViewStrings", () => {
+  return Services.strings.createBundle(
+    "chrome://devtools/locale/jsonview.properties");
+});
 
 /**
  * This object detects 'application/vnd.mozilla.json.view' content type
  * and converts it into a JSON Viewer application that allows simple
  * JSON inspection.
  *
  * Inspired by JSON View: https://github.com/bhollis/jsonview/
  */
-let Converter = Class({
-  extends: Unknown,
+function Converter() {}
 
-  interfaces: [
-    "nsIStreamConverter",
-    "nsIStreamListener",
-    "nsIRequestObserver"
-  ],
+Converter.prototype = {
+  QueryInterface: XPCOMUtils.generateQI([
+    Ci.nsIStreamConverter,
+    Ci.nsIStreamListener,
+    Ci.nsIRequestObserver
+  ]),
 
   get wrappedJSObject() {
     return this;
   },
 
   /**
    * This component works as such:
    * 1. asyncConvertData captures the listener
@@ -296,38 +300,42 @@ let Converter = Class({
     let requestHeaders = headers.request;
     for (let i = 0; i < requestHeaders.length; i++) {
       let header = requestHeaders[i];
       value += header.name + ": " + header.value + eol;
     }
 
     Clipboard.set(value, "text");
   }
-});
+};
 
-// Stream converter component definition
-let service = xpcom.Service({
-  id: components.ID(CLASS_ID),
-  contract: CONTRACT_ID,
-  Component: Converter,
-  register: false,
-  unregister: false
-});
+const Factory = {
+  createInstance: function (outer, iid) {
+    if (outer) {
+      throw Cr.NS_ERROR_NO_AGGREGATION;
+    }
+    return new Converter();
+  }
+};
 
 function register() {
-  if (!xpcom.isRegistered(service)) {
-    xpcom.register(service);
+  if (!registrar.isCIDRegistered(CLASS_ID)) {
+    registrar.registerFactory(CLASS_ID,
+      CLASS_DESCRIPTION,
+      CONTRACT_ID,
+      Factory);
     return true;
   }
+
   return false;
 }
 
 function unregister() {
-  if (xpcom.isRegistered(service)) {
-    xpcom.unregister(service);
+  if (registrar.isCIDRegistered(CLASS_ID)) {
+    registrar.unregisterFactory(CLASS_ID, Factory);
     return true;
   }
   return false;
 }
 
 exports.JsonViewService = {
   register: register,
   unregister: unregister
--- a/devtools/client/jsonview/converter-sniffer.js
+++ b/devtools/client/jsonview/converter-sniffer.js
@@ -1,61 +1,63 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set ft=javascript ts=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/. */
 
 "use strict";
 
-const {Cc, Ci, components} = require("chrome");
-const xpcom = require("sdk/platform/xpcom");
-const {Unknown} = require("sdk/platform/xpcom");
-const {Class} = require("sdk/core/heritage");
+const {Cc, Ci, Cu, Cm, Cr, components} = require("chrome");
+const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
+const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
 
 const categoryManager = Cc["@mozilla.org/categorymanager;1"]
   .getService(Ci.nsICategoryManager);
 
-loader.lazyRequireGetter(this, "NetworkHelper",
-  "devtools/shared/webconsole/network-helper");
-
 // Constants
 const JSON_TYPE = "application/json";
 const CONTRACT_ID = "@mozilla.org/devtools/jsonview-sniffer;1";
-const CLASS_ID = "{4148c488-dca1-49fc-a621-2a0097a62422}";
+const CLASS_ID = components.ID("{4148c488-dca1-49fc-a621-2a0097a62422}");
+const CLASS_DESCRIPTION = "JSONView content sniffer";
 const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
 const JSON_VIEW_TYPE = "JSON View";
 const CONTENT_SNIFFER_CATEGORY = "net-content-sniffers";
 
+function isTopLevelLoad(request) {
+  let loadInfo = request.loadInfo;
+  if (loadInfo && loadInfo.isTopLevelLoad) {
+    return (request.loadFlags & Ci.nsIChannel.LOAD_DOCUMENT_URI);
+  }
+  return false;
+}
+
 /**
  * This component represents a sniffer (implements nsIContentSniffer
  * interface) responsible for changing top level 'application/json'
  * document types to: 'application/vnd.mozilla.json.view'.
  *
  * This internal type is consequently rendered by JSON View component
  * that represents the JSON through a viewer interface.
  */
-var Sniffer = Class({
-  extends: Unknown,
+function Sniffer() {}
 
-  interfaces: [
-    "nsIContentSniffer",
-  ],
+Sniffer.prototype = {
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentSniffer]),
 
   get wrappedJSObject() {
     return this;
   },
 
   getMIMETypeFromContent: function (request, data, length) {
-    // JSON View is enabled only for top level loads only.
-    if (!NetworkHelper.isTopLevelLoad(request)) {
-      return "";
-    }
-
     if (request instanceof Ci.nsIChannel) {
+      // JSON View is enabled only for top level loads only.
+      if (!isTopLevelLoad(request)) {
+        return "";
+      }
       try {
         if (request.contentDisposition ==
           Ci.nsIChannel.DISPOSITION_ATTACHMENT) {
           return "";
         }
       } catch (e) {
         // Channel doesn't support content dispositions
       }
@@ -64,42 +66,46 @@ var Sniffer = Class({
       // change it to new internal type consumed by JSON View.
       if (request.contentType == JSON_TYPE) {
         return JSON_VIEW_MIME_TYPE;
       }
     }
 
     return "";
   }
-});
+};
 
-var service = xpcom.Service({
-  id: components.ID(CLASS_ID),
-  contract: CONTRACT_ID,
-  Component: Sniffer,
-  register: false,
-  unregister: false
-});
+const Factory = {
+  createInstance: function (outer, iid) {
+    if (outer) {
+      throw Cr.NS_ERROR_NO_AGGREGATION;
+    }
+    return new Sniffer();
+  }
+};
 
 function register() {
-  if (!xpcom.isRegistered(service)) {
-    xpcom.register(service);
+  if (!registrar.isCIDRegistered(CLASS_ID)) {
+    registrar.registerFactory(CLASS_ID,
+      CLASS_DESCRIPTION,
+      CONTRACT_ID,
+      Factory);
     categoryManager.addCategoryEntry(CONTENT_SNIFFER_CATEGORY, JSON_VIEW_TYPE,
       CONTRACT_ID, false, false);
     return true;
   }
 
   return false;
 }
 
 function unregister() {
-  if (xpcom.isRegistered(service)) {
+  if (registrar.isCIDRegistered(CLASS_ID)) {
+    registrar.unregisterFactory(CLASS_ID, Factory);
     categoryManager.deleteCategoryEntry(CONTENT_SNIFFER_CATEGORY,
       JSON_VIEW_TYPE, false);
-    xpcom.unregister(service);
     return true;
   }
   return false;
 }
 
 exports.JsonViewSniffer = {
   register: register,
   unregister: unregister