Bug 1297361 - JSON Viewer: use listeners instead of exporting postChromeMessage; r=Gijs
☠☠ backed out by 1e7ee9c3b114 ☠ ☠
authorJan Odvarko <odvarko@gmail.com>
Wed, 30 Nov 2016 13:55:51 +0100
changeset 325289 bbbe3c47bd0b88bd6f1b6ca39a1f8d68bd89f4e7
parent 325288 eb6839ca47ea10a1ac0f241b896ea2d0ac01ce2d
child 325290 2d08aeb18ac99e1078a762cf0146a6a124a76018
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersGijs
bugs1297361
milestone53.0a1
Bug 1297361 - JSON Viewer: use listeners instead of exporting postChromeMessage; r=Gijs MozReview-Commit-ID: 2LIEU10LAV1
devtools/client/jsonview/converter-child.js
devtools/client/jsonview/json-viewer.js
--- a/devtools/client/jsonview/converter-child.js
+++ b/devtools/client/jsonview/converter-child.js
@@ -1,17 +1,17 @@
 /* -*- 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, components} = require("chrome");
 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",
@@ -135,19 +135,18 @@ let Converter = Class({
           return undefined;
         }
       }
     };
 
     JsonViewUtils.exportIntoContentScope(win, Locale, "Locale");
 
     Events.once(win, "DOMContentLoaded", event => {
-      Cu.exportFunction(this.postChromeMessage.bind(this), win, {
-        defineAs: "postChromeMessage"
-      });
+      win.addEventListener("contentMessage",
+        this.onContentMessage.bind(this), false, true);
     });
 
     // The request doesn't have to be always nsIHttpChannel
     // (e.g. in case of data: URLs)
     if (request instanceof Ci.nsIHttpChannel) {
       request.visitResponseHeaders({
         visitHeader: function (name, value) {
           headers.response.push({name: name, value: value});
@@ -258,20 +257,25 @@ let Converter = Class({
       "<base href=\"" + this.htmlEncode(this.data.url()) + "\">" +
       "</head><body>" +
       output +
       "</body></html>";
   },
 
   // Chrome <-> Content communication
 
-  postChromeMessage: function (type, args, objects) {
-    let value = args;
+  onContentMessage: function (e) {
+    // Do not handle events from different documents.
+    let win = NetworkHelper.getWindowForRequest(this.channel);
+    if (win != e.target) {
+      return;
+    }
 
-    switch (type) {
+    let value = e.detail.value;
+    switch (e.detail.type) {
       case "copy":
         Clipboard.set(value, "text");
         break;
 
       case "copy-headers":
         this.copyHeaders(value);
         break;
 
--- a/devtools/client/jsonview/json-viewer.js
+++ b/devtools/client/jsonview/json-viewer.js
@@ -37,27 +37,25 @@ define(function (require, exports, modul
   headers.remove();
 
   /**
    * Application actions/commands. This list implements all commands
    * available for the JSON viewer.
    */
   input.actions = {
     onCopyJson: function () {
-      let value = input.prettified ? input.jsonPretty : input.jsonText;
-      postChromeMessage("copy", value);
+      dispatchEvent("copy", input.prettified ? input.jsonPretty : input.jsonText);
     },
 
     onSaveJson: function () {
-      let value = input.prettified ? input.jsonPretty : input.jsonText;
-      postChromeMessage("save", value);
+      dispatchEvent("save", input.prettified ? input.jsonPretty : input.jsonText);
     },
 
     onCopyHeaders: function () {
-      postChromeMessage("copy-headers", input.headers);
+      dispatchEvent("copy-headers", input.headers);
     },
 
     onSearch: function (value) {
       theApp.setState({searchFilter: value});
     },
 
     onPrettify: function (data) {
       if (input.prettified) {
@@ -69,16 +67,34 @@ define(function (require, exports, modul
         theApp.setState({jsonText: input.jsonPretty});
       }
 
       input.prettified = !input.prettified;
     },
   };
 
   /**
+   * Helper for dispatching an event. It's handled in chrome scope.
+   *
+   * @param {String} type Event detail type
+   * @param {Object} value Event detail value
+   */
+  function dispatchEvent (type, value) {
+    let data = {
+      detail: {
+        type,
+        value,
+      }
+    };
+
+    let contentMessageEvent = new CustomEvent("contentMessage", data);
+    window.dispatchEvent(contentMessageEvent);
+  }
+
+  /**
    * Render the main application component. It's the main tab bar displayed
    * at the top of the window. This component also represents ReacJS root.
    */
   let content = document.getElementById("content");
   let theApp = render(MainTabbedArea(input), content);
 
   let onResize = event => {
     window.document.body.style.height = window.innerHeight + "px";