browser-window.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @fileoverview Defines the environment when in the browser.xhtml window.
  3. * Imports many globals from various files.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. "use strict";
  10. // -----------------------------------------------------------------------------
  11. // Rule Definition
  12. // -----------------------------------------------------------------------------
  13. var fs = require("fs");
  14. var helpers = require("../helpers");
  15. var { getScriptGlobals } = require("./utils");
  16. // When updating EXTRA_SCRIPTS or MAPPINGS, be sure to also update the
  17. // 'support-files' config in `tools/lint/eslint.yml`.
  18. // These are scripts not loaded from browser.xhtml or global-scripts.inc
  19. // but via other includes.
  20. const EXTRA_SCRIPTS = [
  21. "browser/base/content/nsContextMenu.js",
  22. "browser/components/downloads/content/downloads.js",
  23. "browser/components/downloads/content/indicator.js",
  24. "toolkit/content/customElements.js",
  25. "toolkit/content/editMenuOverlay.js",
  26. ];
  27. const extraDefinitions = [
  28. // Via Components.utils, defineModuleGetter, defineLazyModuleGetters or
  29. // defineLazyScriptGetter (and map to
  30. // single) variable.
  31. { name: "XPCOMUtils", writable: false },
  32. { name: "Task", writable: false },
  33. { name: "windowGlobalChild", writable: false },
  34. // structuredClone is a new global that would be defined for the `browser`
  35. // environment in ESLint, but only Firefox has implemented it currently and so
  36. // it isn't in ESLint's globals yet.
  37. // https://developer.mozilla.org/docs/Web/API/structuredClone
  38. { name: "structuredClone", writable: false },
  39. ];
  40. // Some files in global-scripts.inc need mapping to specific locations.
  41. const MAPPINGS = {
  42. "printUtils.js": "toolkit/components/printing/content/printUtils.js",
  43. "panelUI.js": "browser/components/customizableui/content/panelUI.js",
  44. "viewSourceUtils.js":
  45. "toolkit/components/viewsource/content/viewSourceUtils.js",
  46. "places-tree.js": "browser/components/places/content/places-tree.js",
  47. "places-menupopup.js":
  48. "browser/components/places/content/places-menupopup.js",
  49. };
  50. const globalScriptsRegExp = /^\s*Services.scriptloader.loadSubScript\(\"(.*?)\", this\);$/;
  51. function getGlobalScriptIncludes(scriptPath) {
  52. let fileData;
  53. try {
  54. fileData = fs.readFileSync(scriptPath, { encoding: "utf8" });
  55. } catch (ex) {
  56. // The file isn't present, so this isn't an m-c repository.
  57. return null;
  58. }
  59. fileData = fileData.split("\n");
  60. let result = [];
  61. for (let line of fileData) {
  62. let match = line.match(globalScriptsRegExp);
  63. if (match) {
  64. let sourceFile = match[1]
  65. .replace(
  66. "chrome://browser/content/search/",
  67. "browser/components/search/content/"
  68. )
  69. .replace(
  70. "chrome://browser/content/screenshots/",
  71. "browser/components/screenshots/content/"
  72. )
  73. .replace("chrome://browser/content/", "browser/base/content/")
  74. .replace("chrome://global/content/", "toolkit/content/");
  75. for (let mapping of Object.getOwnPropertyNames(MAPPINGS)) {
  76. if (sourceFile.includes(mapping)) {
  77. sourceFile = MAPPINGS[mapping];
  78. }
  79. }
  80. result.push(sourceFile);
  81. }
  82. }
  83. return result;
  84. }
  85. function getGlobalScripts() {
  86. let results = [];
  87. for (let scriptPath of helpers.globalScriptPaths) {
  88. results = results.concat(getGlobalScriptIncludes(scriptPath));
  89. }
  90. return results;
  91. }
  92. module.exports = getScriptGlobals(
  93. "browser-window",
  94. getGlobalScripts().concat(EXTRA_SCRIPTS),
  95. extraDefinitions,
  96. {
  97. browserjsScripts: getGlobalScripts().concat(EXTRA_SCRIPTS),
  98. }
  99. );