Bug 817337 - Add a JS module to get the most recent browser window, with the option of restricting the search to include only private windows; r=dao
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 05 Dec 2012 20:59:26 -0500
changeset 115165 80b3907e5c6a5b2fdfa06930efb1304aa109535e
parent 115164 7e5fee8e5ba03ad9083e55d8ffd7e195250dddc6
child 115166 31d5dca2a3b4fff391dd501ab897d4080007f623
push id23977
push usereakhgari@mozilla.com
push dateThu, 06 Dec 2012 18:12:23 +0000
treeherdermozilla-central@31d5dca2a3b4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdao
bugs817337
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 817337 - Add a JS module to get the most recent browser window, with the option of restricting the search to include only private windows; r=dao
browser/components/nsBrowserGlue.js
browser/modules/Makefile.in
browser/modules/RecentWindow.jsm
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -48,16 +48,19 @@ XPCOMUtils.defineLazyModuleGetter(this, 
                                   "resource:///modules/webrtcUI.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
                                   "resource://gre/modules/PrivateBrowsingUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "KeywordURLResetPrompter",
                                   "resource:///modules/KeywordURLResetPrompter.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "RecentWindow",
+                                  "resource:///modules/RecentWindow.jsm");
+
 const PREF_PLUGINS_NOTIFYUSER = "plugins.update.notifyUser";
 const PREF_PLUGINS_UPDATEURL  = "plugins.update.url";
 
 // We try to backup bookmarks at idle times, to avoid doing that at shutdown.
 // Number of idle seconds before trying to backup bookmarks.  15 minutes.
 const BOOKMARKS_BACKUP_IDLE_TIME = 15 * 60;
 // Minimum interval in milliseconds between backups.
 const BOOKMARKS_BACKUP_INTERVAL = 86400 * 1000;
@@ -1579,51 +1582,19 @@ BrowserGlue.prototype = {
       Components.utils.reportError(ex);
     }
     finally {
       Services.prefs.setIntPref(SMART_BOOKMARKS_PREF, SMART_BOOKMARKS_VERSION);
       Services.prefs.savePrefFile(null);
     }
   },
 
-#ifndef XP_WIN
-#define BROKEN_WM_Z_ORDER
-#endif
-
   // this returns the most recent non-popup browser window
   getMostRecentBrowserWindow: function BG_getMostRecentBrowserWindow() {
-    function isFullBrowserWindow(win) {
-      return !win.closed &&
-             win.toolbar.visible;
-    }
-
-#ifdef BROKEN_WM_Z_ORDER
-    var win = Services.wm.getMostRecentWindow("navigator:browser");
-
-    // if we're lucky, this isn't a popup, and we can just return this
-    if (win && !isFullBrowserWindow(win)) {
-      win = null;
-      let windowList = Services.wm.getEnumerator("navigator:browser");
-      // this is oldest to newest, so this gets a bit ugly
-      while (windowList.hasMoreElements()) {
-        let nextWin = windowList.getNext();
-        if (isFullBrowserWindow(nextWin))
-          win = nextWin;
-      }
-    }
-    return win;
-#else
-    var windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true);
-    while (windowList.hasMoreElements()) {
-      let win = windowList.getNext();
-      if (isFullBrowserWindow(win))
-        return win;
-    }
-    return null;
-#endif
+    return RecentWindow.getMostRecentBrowserWindow();
   },
 
 #ifdef MOZ_SERVICES_SYNC
   /**
    * Called as an observer when Sync's "display URI" notification is fired.
    *
    * We open the received URI in a background tab.
    *
--- a/browser/modules/Makefile.in
+++ b/browser/modules/Makefile.in
@@ -23,18 +23,20 @@ EXTRA_JS_MODULES = \
 	SignInToWebsite.jsm \
 	TelemetryTimestamps.jsm \
 	Social.jsm \
 	webappsUI.jsm \
 	webrtcUI.jsm \
 	KeywordURLResetPrompter.jsm \
 	$(NULL)
 
+EXTRA_PP_JS_MODULES = RecentWindow.jsm
+
 ifeq ($(MOZ_WIDGET_TOOLKIT),windows)
 EXTRA_JS_MODULES += \
 	WindowsPreviewPerTab.jsm \
 	$(NULL)
-EXTRA_PP_JS_MODULES = \
+EXTRA_PP_JS_MODULES += \
 	WindowsJumpLists.jsm \
 	$(NULL)
 endif
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/browser/modules/RecentWindow.jsm
@@ -0,0 +1,65 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = ["RecentWindow"];
+
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
+
+#ifndef XP_WIN
+#define BROKEN_WM_Z_ORDER
+#endif
+
+this.RecentWindow = {
+  /*
+   * Get the most recent browser window.
+   *
+   * @param aOptions an object accepting the arguments for the search.
+   *        Set the private property to true in order to restrict the
+   *        search to private windows only, or to false in order to
+   *        restrict the search to non-private windows only.  To search
+   *        in both groups, don't specify the private property.
+   */
+  getMostRecentBrowserWindow: function RW_getMostRecentBrowserWindow(aOptions) {
+    let checkPrivacy = typeof aOptions == "object" &&
+                       "private" in aOptions;
+
+    function isSuitableBrowserWindow(win) {
+      return (!win.closed &&
+              win.toolbar.visible &&
+              (!checkPrivacy ||
+               PrivateBrowsingUtils.isWindowPrivate(win) == aOptions.private));
+    }
+
+#ifdef BROKEN_WM_Z_ORDER
+    let win = Services.wm.getMostRecentWindow("navigator:browser");
+
+    // if we're lucky, this isn't a popup, and we can just return this
+    if (win && !isSuitableBrowserWindow(win)) {
+      win = null;
+      let windowList = Services.wm.getEnumerator("navigator:browser");
+      // this is oldest to newest, so this gets a bit ugly
+      while (windowList.hasMoreElements()) {
+        let nextWin = windowList.getNext();
+        if (isSuitableBrowserWindow(nextWin))
+          win = nextWin;
+      }
+    }
+    return win;
+#else
+    let windowList = Services.wm.getZOrderDOMWindowEnumerator("navigator:browser", true);
+    while (windowList.hasMoreElements()) {
+      let win = windowList.getNext();
+      if (isSuitableBrowserWindow(win))
+        return win;
+    }
+    return null;
+#endif
+  }
+};
+