main.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. /* globals JsonViewUtils*/
  6. "use strict";
  7. const { Cu } = require("chrome");
  8. const Services = require("Services");
  9. const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
  10. XPCOMUtils.defineLazyGetter(this, "JsonViewUtils", function () {
  11. return require("devtools/client/jsonview/utils");
  12. });
  13. /**
  14. * Singleton object that represents the JSON View in-content tool.
  15. * It has the same lifetime as the browser. Initialization done by
  16. * DevTools() object from devtools/client/framework/devtools.js
  17. */
  18. var JsonView = {
  19. initialize: function () {
  20. // Load JSON converter module. This converter is responsible
  21. // for handling 'application/json' documents and converting
  22. // them into a simple web-app that allows easy inspection
  23. // of the JSON data.
  24. Services.ppmm.loadProcessScript(
  25. "resource://devtools/client/jsonview/converter-observer.js",
  26. true);
  27. this.onSaveListener = this.onSave.bind(this);
  28. // Register for messages coming from the child process.
  29. Services.ppmm.addMessageListener(
  30. "devtools:jsonview:save", this.onSaveListener);
  31. },
  32. destroy: function () {
  33. Services.ppmm.removeMessageListener(
  34. "devtools:jsonview:save", this.onSaveListener);
  35. },
  36. // Message handlers for events from child processes
  37. /**
  38. * Save JSON to a file needs to be implemented here
  39. * in the parent process.
  40. */
  41. onSave: function (message) {
  42. let value = message.data;
  43. let file = JsonViewUtils.getTargetFile();
  44. if (file) {
  45. JsonViewUtils.saveToFile(file, value);
  46. }
  47. }
  48. };
  49. // Exports from this module
  50. module.exports.JsonView = JsonView;