Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = ["RecentWindow"]; |
michael@0 | 8 | |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | |
michael@0 | 11 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 12 | Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); |
michael@0 | 13 | |
michael@0 | 14 | #ifdef XP_UNIX |
michael@0 | 15 | #ifndef XP_MACOSX |
michael@0 | 16 | #define BROKEN_WM_Z_ORDER |
michael@0 | 17 | #endif |
michael@0 | 18 | #endif |
michael@0 | 19 | |
michael@0 | 20 | this.RecentWindow = { |
michael@0 | 21 | /* |
michael@0 | 22 | * Get the most recent browser window. |
michael@0 | 23 | * |
michael@0 | 24 | * @param aOptions an object accepting the arguments for the search. |
michael@0 | 25 | * * private: true to restrict the search to private windows |
michael@0 | 26 | * only, false to restrict the search to non-private only. |
michael@0 | 27 | * Omit the property to search in both groups. |
michael@0 | 28 | * * allowPopups: true if popup windows are permissable. |
michael@0 | 29 | */ |
michael@0 | 30 | getMostRecentBrowserWindow: function RW_getMostRecentBrowserWindow(aOptions) { |
michael@0 | 31 | let checkPrivacy = typeof aOptions == "object" && |
michael@0 | 32 | "private" in aOptions; |
michael@0 | 33 | |
michael@0 | 34 | let allowPopups = typeof aOptions == "object" && !!aOptions.allowPopups; |
michael@0 | 35 | |
michael@0 | 36 | function isSuitableBrowserWindow(win) { |
michael@0 | 37 | return (!win.closed && |
michael@0 | 38 | (allowPopups || win.toolbar.visible) && |
michael@0 | 39 | (!checkPrivacy || |
michael@0 | 40 | PrivateBrowsingUtils.permanentPrivateBrowsing || |
michael@0 | 41 | PrivateBrowsingUtils.isWindowPrivate(win) == aOptions.private)); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | #ifdef BROKEN_WM_Z_ORDER |
michael@0 | 45 | let win = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 46 | |
michael@0 | 47 | // if we're lucky, this isn't a popup, and we can just return this |
michael@0 | 48 | if (win && !isSuitableBrowserWindow(win)) { |
michael@0 | 49 | win = null; |
michael@0 | 50 | let windowList = Services.wm.getEnumerator("navigator:browser"); |
michael@0 | 51 | // this is oldest to newest, so this gets a bit ugly |
michael@0 | 52 | while (windowList.hasMoreElements()) { |
michael@0 | 53 | let nextWin = windowList.getNext(); |
michael@0 | 54 | if (isSuitableBrowserWindow(nextWin)) |
michael@0 | 55 | win = nextWin; |
michael@0 | 56 | } |
michael@0 | 57 | } |
michael@0 | 58 | return win; |
michael@0 | 59 | #else |
michael@0 | 60 | let windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true); |
michael@0 | 61 | while (windowList.hasMoreElements()) { |
michael@0 | 62 | let win = windowList.getNext(); |
michael@0 | 63 | if (isSuitableBrowserWindow(win)) |
michael@0 | 64 | return win; |
michael@0 | 65 | } |
michael@0 | 66 | return null; |
michael@0 | 67 | #endif |
michael@0 | 68 | } |
michael@0 | 69 | }; |
michael@0 | 70 |