browser/modules/RecentWindow.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     5 "use strict";
     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");
    14 #ifdef XP_UNIX
    15 #ifndef XP_MACOSX
    16 #define BROKEN_WM_Z_ORDER
    17 #endif
    18 #endif
    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;
    34     let allowPopups = typeof aOptions == "object" && !!aOptions.allowPopups;
    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     }
    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)) {
    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 };

mercurial