1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/modules/RecentWindow.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,70 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +this.EXPORTED_SYMBOLS = ["RecentWindow"]; 1.11 + 1.12 +const Cu = Components.utils; 1.13 + 1.14 +Cu.import("resource://gre/modules/Services.jsm"); 1.15 +Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); 1.16 + 1.17 +#ifdef XP_UNIX 1.18 +#ifndef XP_MACOSX 1.19 +#define BROKEN_WM_Z_ORDER 1.20 +#endif 1.21 +#endif 1.22 + 1.23 +this.RecentWindow = { 1.24 + /* 1.25 + * Get the most recent browser window. 1.26 + * 1.27 + * @param aOptions an object accepting the arguments for the search. 1.28 + * * private: true to restrict the search to private windows 1.29 + * only, false to restrict the search to non-private only. 1.30 + * Omit the property to search in both groups. 1.31 + * * allowPopups: true if popup windows are permissable. 1.32 + */ 1.33 + getMostRecentBrowserWindow: function RW_getMostRecentBrowserWindow(aOptions) { 1.34 + let checkPrivacy = typeof aOptions == "object" && 1.35 + "private" in aOptions; 1.36 + 1.37 + let allowPopups = typeof aOptions == "object" && !!aOptions.allowPopups; 1.38 + 1.39 + function isSuitableBrowserWindow(win) { 1.40 + return (!win.closed && 1.41 + (allowPopups || win.toolbar.visible) && 1.42 + (!checkPrivacy || 1.43 + PrivateBrowsingUtils.permanentPrivateBrowsing || 1.44 + PrivateBrowsingUtils.isWindowPrivate(win) == aOptions.private)); 1.45 + } 1.46 + 1.47 +#ifdef BROKEN_WM_Z_ORDER 1.48 + let win = Services.wm.getMostRecentWindow("navigator:browser"); 1.49 + 1.50 + // if we're lucky, this isn't a popup, and we can just return this 1.51 + if (win && !isSuitableBrowserWindow(win)) { 1.52 + win = null; 1.53 + let windowList = Services.wm.getEnumerator("navigator:browser"); 1.54 + // this is oldest to newest, so this gets a bit ugly 1.55 + while (windowList.hasMoreElements()) { 1.56 + let nextWin = windowList.getNext(); 1.57 + if (isSuitableBrowserWindow(nextWin)) 1.58 + win = nextWin; 1.59 + } 1.60 + } 1.61 + return win; 1.62 +#else 1.63 + let windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true); 1.64 + while (windowList.hasMoreElements()) { 1.65 + let win = windowList.getNext(); 1.66 + if (isSuitableBrowserWindow(win)) 1.67 + return win; 1.68 + } 1.69 + return null; 1.70 +#endif 1.71 + } 1.72 +}; 1.73 +