converter-sniffer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. const {Cc, Ci, components} = require("chrome");
  7. const xpcom = require("sdk/platform/xpcom");
  8. const {Unknown} = require("sdk/platform/xpcom");
  9. const {Class} = require("sdk/core/heritage");
  10. const categoryManager = Cc["@mozilla.org/categorymanager;1"]
  11. .getService(Ci.nsICategoryManager);
  12. loader.lazyRequireGetter(this, "NetworkHelper",
  13. "devtools/shared/webconsole/network-helper");
  14. // Constants
  15. const JSON_TYPE = "application/json";
  16. const CONTRACT_ID = "@mozilla.org/devtools/jsonview-sniffer;1";
  17. const CLASS_ID = "{4148c488-dca1-49fc-a621-2a0097a62422}";
  18. const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
  19. const JSON_VIEW_TYPE = "JSON View";
  20. const CONTENT_SNIFFER_CATEGORY = "net-content-sniffers";
  21. /**
  22. * This component represents a sniffer (implements nsIContentSniffer
  23. * interface) responsible for changing top level 'application/json'
  24. * document types to: 'application/vnd.mozilla.json.view'.
  25. *
  26. * This internal type is consequently rendered by JSON View component
  27. * that represents the JSON through a viewer interface.
  28. */
  29. var Sniffer = Class({
  30. extends: Unknown,
  31. interfaces: [
  32. "nsIContentSniffer",
  33. ],
  34. get wrappedJSObject() {
  35. return this;
  36. },
  37. getMIMETypeFromContent: function (request, data, length) {
  38. // JSON View is enabled only for top level loads only.
  39. if (!NetworkHelper.isTopLevelLoad(request)) {
  40. return "";
  41. }
  42. if (request instanceof Ci.nsIChannel) {
  43. try {
  44. if (request.contentDisposition ==
  45. Ci.nsIChannel.DISPOSITION_ATTACHMENT) {
  46. return "";
  47. }
  48. } catch (e) {
  49. // Channel doesn't support content dispositions
  50. }
  51. // Check the response content type and if it's application/json
  52. // change it to new internal type consumed by JSON View.
  53. if (request.contentType == JSON_TYPE) {
  54. return JSON_VIEW_MIME_TYPE;
  55. }
  56. }
  57. return "";
  58. }
  59. });
  60. var service = xpcom.Service({
  61. id: components.ID(CLASS_ID),
  62. contract: CONTRACT_ID,
  63. Component: Sniffer,
  64. register: false,
  65. unregister: false
  66. });
  67. function register() {
  68. if (!xpcom.isRegistered(service)) {
  69. xpcom.register(service);
  70. categoryManager.addCategoryEntry(CONTENT_SNIFFER_CATEGORY, JSON_VIEW_TYPE,
  71. CONTRACT_ID, false, false);
  72. return true;
  73. }
  74. return false;
  75. }
  76. function unregister() {
  77. if (xpcom.isRegistered(service)) {
  78. categoryManager.deleteCategoryEntry(CONTENT_SNIFFER_CATEGORY,
  79. JSON_VIEW_TYPE, false);
  80. xpcom.unregister(service);
  81. return true;
  82. }
  83. return false;
  84. }
  85. exports.JsonViewSniffer = {
  86. register: register,
  87. unregister: unregister
  88. };