RecentWindow.jsm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. this.EXPORTED_SYMBOLS = ["RecentWindow"];
  6. const Cu = Components.utils;
  7. Cu.import("resource://gre/modules/Services.jsm");
  8. Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
  9. #ifndef XP_WIN
  10. #define BROKEN_WM_Z_ORDER
  11. #endif
  12. this.RecentWindow = {
  13. /*
  14. * Get the most recent browser window.
  15. *
  16. * @param aOptions an object accepting the arguments for the search.
  17. * * private: true to restrict the search to private windows
  18. * only, false to restrict the search to non-private only.
  19. * Omit the property to search in both groups.
  20. * * allowPopups: true if popup windows are permissable.
  21. */
  22. getMostRecentBrowserWindow: function(aOptions) {
  23. let checkPrivacy = typeof aOptions == "object" &&
  24. "private" in aOptions;
  25. let allowPopups = typeof aOptions == "object" && !!aOptions.allowPopups;
  26. function isSuitableBrowserWindow(win) {
  27. return (!win.closed &&
  28. (allowPopups || win.toolbar.visible) &&
  29. (!checkPrivacy ||
  30. PrivateBrowsingUtils.permanentPrivateBrowsing ||
  31. PrivateBrowsingUtils.isWindowPrivate(win) == aOptions.private));
  32. }
  33. #ifdef BROKEN_WM_Z_ORDER
  34. let win = Services.wm.getMostRecentWindow("navigator:browser");
  35. // if we're lucky, this isn't a popup, and we can just return this
  36. if (win && !isSuitableBrowserWindow(win)) {
  37. win = null;
  38. let windowList = Services.wm.getEnumerator("navigator:browser");
  39. // this is oldest to newest, so this gets a bit ugly
  40. while (windowList.hasMoreElements()) {
  41. let nextWin = windowList.getNext();
  42. if (isSuitableBrowserWindow(nextWin))
  43. win = nextWin;
  44. }
  45. }
  46. return win;
  47. #else
  48. let windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true);
  49. while (windowList.hasMoreElements()) {
  50. let win = windowList.getNext();
  51. if (isSuitableBrowserWindow(win))
  52. return win;
  53. }
  54. return null;
  55. #endif
  56. }
  57. };