window.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. // The panel module currently supports only Firefox.
  6. // See: https://bugzilla.mozilla.org/show_bug.cgi?id=jetpack-panel-apps
  7. module.metadata = {
  8. 'stability': 'unstable',
  9. 'engines': {
  10. 'Firefox': '*'
  11. }
  12. };
  13. const { getMostRecentBrowserWindow, windows: getWindows } = require('../window/utils');
  14. const { ignoreWindow } = require('../private-browsing/utils');
  15. const { isPrivateBrowsingSupported } = require('../self');
  16. const { isGlobalPBSupported } = require('../private-browsing/utils');
  17. function getWindow(anchor) {
  18. let window;
  19. let windows = getWindows("navigator:browser", {
  20. includePrivate: isPrivateBrowsingSupported || isGlobalPBSupported
  21. });
  22. if (anchor) {
  23. let anchorWindow = anchor.ownerDocument.defaultView.top;
  24. let anchorDocument = anchorWindow.document;
  25. // loop thru supported windows
  26. for each(let enumWindow in windows) {
  27. // Check if the anchor is in this browser window.
  28. if (enumWindow == anchorWindow) {
  29. window = anchorWindow;
  30. break;
  31. }
  32. // Check if the anchor is in a browser tab in this browser window.
  33. let browser = enumWindow.gBrowser.getBrowserForDocument(anchorDocument);
  34. if (browser) {
  35. window = enumWindow;
  36. break;
  37. }
  38. // Look in other subdocuments (sidebar, etc.)?
  39. }
  40. }
  41. // If we didn't find the anchor's window (or we have no anchor),
  42. // return the most recent browser window.
  43. if (!window)
  44. window = getMostRecentBrowserWindow();
  45. // if the window is not supported, then it should be ignored
  46. if (ignoreWindow(window)) {
  47. return null;
  48. }
  49. return window;
  50. }
  51. exports.getWindow = getWindow;