utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 { Cu, Cc, Ci } = require("chrome");
  7. const Services = require("Services");
  8. const { getMostRecentBrowserWindow } = require("sdk/window/utils");
  9. const OPEN_FLAGS = {
  10. RDONLY: parseInt("0x01", 16),
  11. WRONLY: parseInt("0x02", 16),
  12. CREATE_FILE: parseInt("0x08", 16),
  13. APPEND: parseInt("0x10", 16),
  14. TRUNCATE: parseInt("0x20", 16),
  15. EXCL: parseInt("0x80", 16)
  16. };
  17. /**
  18. * Open File Save As dialog and let the user to pick proper file location.
  19. */
  20. exports.getTargetFile = function () {
  21. let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  22. let win = getMostRecentBrowserWindow();
  23. fp.init(win, null, Ci.nsIFilePicker.modeSave);
  24. fp.appendFilter("JSON Files", "*.json; *.jsonp;");
  25. fp.appendFilters(Ci.nsIFilePicker.filterText);
  26. fp.appendFilters(Ci.nsIFilePicker.filterAll);
  27. fp.filterIndex = 0;
  28. let rv = fp.show();
  29. if (rv == Ci.nsIFilePicker.returnOK || rv == Ci.nsIFilePicker.returnReplace) {
  30. return fp.file;
  31. }
  32. return null;
  33. };
  34. /**
  35. * Save JSON to a file
  36. */
  37. exports.saveToFile = function (file, jsonString) {
  38. let foStream = Cc["@mozilla.org/network/file-output-stream;1"]
  39. .createInstance(Ci.nsIFileOutputStream);
  40. // write, create, truncate
  41. let openFlags = OPEN_FLAGS.WRONLY | OPEN_FLAGS.CREATE_FILE |
  42. OPEN_FLAGS.TRUNCATE;
  43. let permFlags = parseInt("0666", 8);
  44. foStream.init(file, openFlags, permFlags, 0);
  45. let converter = Cc["@mozilla.org/intl/converter-output-stream;1"]
  46. .createInstance(Ci.nsIConverterOutputStream);
  47. converter.init(foStream, "UTF-8", 0, 0);
  48. // The entire jsonString can be huge so, write the data in chunks.
  49. let chunkLength = 1024 * 1204;
  50. for (let i = 0; i <= jsonString.length; i++) {
  51. let data = jsonString.substr(i, chunkLength + 1);
  52. if (data) {
  53. converter.writeString(data);
  54. }
  55. i = i + chunkLength;
  56. }
  57. // this closes foStream
  58. converter.close();
  59. };
  60. /**
  61. * Get the current theme from preferences.
  62. */
  63. exports.getCurrentTheme = function () {
  64. return Services.prefs.getCharPref("devtools.theme");
  65. };
  66. /**
  67. * Export given object into the target window scope.
  68. */
  69. exports.exportIntoContentScope = function (win, obj, defineAs) {
  70. let clone = Cu.createObjectIn(win, {
  71. defineAs: defineAs
  72. });
  73. let props = Object.getOwnPropertyNames(obj);
  74. for (let i = 0; i < props.length; i++) {
  75. let propName = props[i];
  76. let propValue = obj[propName];
  77. if (typeof propValue == "function") {
  78. Cu.exportFunction(propValue, clone, {
  79. defineAs: propName
  80. });
  81. } else {
  82. clone[propName] = Cu.cloneInto(propValue, win);
  83. }
  84. }
  85. };