|
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 |
|
5 "use strict"; |
|
6 |
|
7 this.EXPORTED_SYMBOLS = ["RecentWindow"]; |
|
8 |
|
9 const Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/Services.jsm"); |
|
12 Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm"); |
|
13 |
|
14 #ifdef XP_UNIX |
|
15 #ifndef XP_MACOSX |
|
16 #define BROKEN_WM_Z_ORDER |
|
17 #endif |
|
18 #endif |
|
19 |
|
20 this.RecentWindow = { |
|
21 /* |
|
22 * Get the most recent browser window. |
|
23 * |
|
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. |
|
29 */ |
|
30 getMostRecentBrowserWindow: function RW_getMostRecentBrowserWindow(aOptions) { |
|
31 let checkPrivacy = typeof aOptions == "object" && |
|
32 "private" in aOptions; |
|
33 |
|
34 let allowPopups = typeof aOptions == "object" && !!aOptions.allowPopups; |
|
35 |
|
36 function isSuitableBrowserWindow(win) { |
|
37 return (!win.closed && |
|
38 (allowPopups || win.toolbar.visible) && |
|
39 (!checkPrivacy || |
|
40 PrivateBrowsingUtils.permanentPrivateBrowsing || |
|
41 PrivateBrowsingUtils.isWindowPrivate(win) == aOptions.private)); |
|
42 } |
|
43 |
|
44 #ifdef BROKEN_WM_Z_ORDER |
|
45 let win = Services.wm.getMostRecentWindow("navigator:browser"); |
|
46 |
|
47 // if we're lucky, this isn't a popup, and we can just return this |
|
48 if (win && !isSuitableBrowserWindow(win)) { |
|
49 win = null; |
|
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)) |
|
55 win = nextWin; |
|
56 } |
|
57 } |
|
58 return win; |
|
59 #else |
|
60 let windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true); |
|
61 while (windowList.hasMoreElements()) { |
|
62 let win = windowList.getNext(); |
|
63 if (isSuitableBrowserWindow(win)) |
|
64 return win; |
|
65 } |
|
66 return null; |
|
67 #endif |
|
68 } |
|
69 }; |
|
70 |