michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["RecentWindow"]; michael@0: michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); michael@0: michael@0: #ifdef XP_UNIX michael@0: #ifndef XP_MACOSX michael@0: #define BROKEN_WM_Z_ORDER michael@0: #endif michael@0: #endif michael@0: michael@0: this.RecentWindow = { michael@0: /* michael@0: * Get the most recent browser window. michael@0: * michael@0: * @param aOptions an object accepting the arguments for the search. michael@0: * * private: true to restrict the search to private windows michael@0: * only, false to restrict the search to non-private only. michael@0: * Omit the property to search in both groups. michael@0: * * allowPopups: true if popup windows are permissable. michael@0: */ michael@0: getMostRecentBrowserWindow: function RW_getMostRecentBrowserWindow(aOptions) { michael@0: let checkPrivacy = typeof aOptions == "object" && michael@0: "private" in aOptions; michael@0: michael@0: let allowPopups = typeof aOptions == "object" && !!aOptions.allowPopups; michael@0: michael@0: function isSuitableBrowserWindow(win) { michael@0: return (!win.closed && michael@0: (allowPopups || win.toolbar.visible) && michael@0: (!checkPrivacy || michael@0: PrivateBrowsingUtils.permanentPrivateBrowsing || michael@0: PrivateBrowsingUtils.isWindowPrivate(win) == aOptions.private)); michael@0: } michael@0: michael@0: #ifdef BROKEN_WM_Z_ORDER michael@0: let win = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: michael@0: // if we're lucky, this isn't a popup, and we can just return this michael@0: if (win && !isSuitableBrowserWindow(win)) { michael@0: win = null; michael@0: let windowList = Services.wm.getEnumerator("navigator:browser"); michael@0: // this is oldest to newest, so this gets a bit ugly michael@0: while (windowList.hasMoreElements()) { michael@0: let nextWin = windowList.getNext(); michael@0: if (isSuitableBrowserWindow(nextWin)) michael@0: win = nextWin; michael@0: } michael@0: } michael@0: return win; michael@0: #else michael@0: let windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true); michael@0: while (windowList.hasMoreElements()) { michael@0: let win = windowList.getNext(); michael@0: if (isSuitableBrowserWindow(win)) michael@0: return win; michael@0: } michael@0: return null; michael@0: #endif michael@0: } michael@0: }; michael@0: