Bug 1230544 - JSON Viewer: support for .json URL extensions; r=jryans
authorJan Odvarko <odvarko@gmail.com>
Fri, 04 Dec 2015 15:27:47 +0100
changeset 310317 b4fa22948cf411dddee52b893f2f49bad4dcf9d7
parent 310316 58b6faf4c31c9145981552c22c924d4e7c5d8bae
child 310318 a2ad9909ffa076902b7d85abf5a8646d4753c440
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1230544
milestone45.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 1230544 - JSON Viewer: support for .json URL extensions; r=jryans
devtools/client/jsonview/converter-sniffer.js
devtools/shared/webconsole/network-helper.js
--- a/devtools/client/jsonview/converter-sniffer.js
+++ b/devtools/client/jsonview/converter-sniffer.js
@@ -17,16 +17,17 @@ loader.lazyRequireGetter(this, "NetworkH
                                "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 JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
 const JSON_VIEW_TYPE = "JSON View";
+const JSON_EXTENSION = "json";
 const CONTENT_SNIFFER_CATEGORY = "net-content-sniffers";
 
 /**
  * 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
@@ -58,16 +59,20 @@ var Sniffer = Class({
         // Channel doesn't support content dispositions
       }
 
       // Check the response content type and if it's application/json
       // change it to new internal type consumed by JSON View.
       if (aRequest.contentType == JSON_TYPE) {
         return JSON_VIEW_MIME_TYPE;
       }
+
+      if (NetworkHelper.getFileExtension(aRequest.name) == JSON_EXTENSION) {
+        return JSON_VIEW_MIME_TYPE;
+      }
     }
 
     return "";
   }
 });
 
 var service = xpcom.Service({
   id: components.ID(CLASS_ID),
--- a/devtools/shared/webconsole/network-helper.js
+++ b/devtools/shared/webconsole/network-helper.js
@@ -798,14 +798,44 @@ var NetworkHelper = {
   nsIURL: function(aUrl, aStore = gNSURLStore) {
     if (aStore.has(aUrl)) {
       return aStore.get(aUrl);
     }
 
     let uri = Services.io.newURI(aUrl, null, null).QueryInterface(Ci.nsIURL);
     aStore.set(aUrl, uri);
     return uri;
+  },
+
+  /**
+   * Returns extension for file URLs (e.g. 'json').
+   * Not everyURL has an extension and this method works as follows:
+   * 1) Remove query string
+   * 2) Get part after the last slash (a file name)
+   * 3) Look for the last dot (an extension)
+   */
+  getFileExtension: function(aUrl) {
+    if (!aUrl) {
+      return;
+    }
+
+    // Remove query string from the URL if any.
+    let queryString = aUrl.indexOf("?");
+    if (queryString != -1) {
+      aUrl = aUrl.substr(0, queryString);
+    }
+
+    // Look for the part after last slash
+    var lastSlash = aUrl.lastIndexOf("/");
+    var fileName = aUrl.substr(lastSlash + 1);
+    if (!fileName) {
+      return;
+    }
+
+    // Now get the file extension.
+    var lastDot = fileName.lastIndexOf(".");
+    return fileName.substr(lastDot + 1);
   }
 };
 
 for (let prop of Object.getOwnPropertyNames(NetworkHelper)) {
   exports[prop] = NetworkHelper[prop];
 }