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/. */
7 this.EXPORTED_SYMBOLS = ["RecentWindow"];
9 const Cu = Components.utils;
11 Cu.import("resource://gre/modules/Services.jsm");
12 Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
16 #define BROKEN_WM_Z_ORDER
22 * Get the most recent browser window.
24 * @param aOptions an object accepting the arguments for the search.
25 * * private: true to restrict the search to private windows
26 * only, false to restrict the search to non-private only.
27 * Omit the property to search in both groups.
28 * * allowPopups: true if popup windows are permissable.
30 getMostRecentBrowserWindow: function RW_getMostRecentBrowserWindow(aOptions) {
31 let checkPrivacy = typeof aOptions == "object" &&
32 "private" in aOptions;
34 let allowPopups = typeof aOptions == "object" && !!aOptions.allowPopups;
36 function isSuitableBrowserWindow(win) {
37 return (!win.closed &&
38 (allowPopups || win.toolbar.visible) &&
40 PrivateBrowsingUtils.permanentPrivateBrowsing ||
41 PrivateBrowsingUtils.isWindowPrivate(win) == aOptions.private));
44 #ifdef BROKEN_WM_Z_ORDER
45 let win = Services.wm.getMostRecentWindow("navigator:browser");
47 // if we're lucky, this isn't a popup, and we can just return this
48 if (win && !isSuitableBrowserWindow(win)) {
50 let windowList = Services.wm.getEnumerator("navigator:browser");
51 // this is oldest to newest, so this gets a bit ugly
52 while (windowList.hasMoreElements()) {
53 let nextWin = windowList.getNext();
54 if (isSuitableBrowserWindow(nextWin))
60 let windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true);
61 while (windowList.hasMoreElements()) {
62 let win = windowList.getNext();
63 if (isSuitableBrowserWindow(win))