Bug 503299 window.close() from a content tab closes Thunderbird window. r=asuth
authorMark Banner <bugzilla@standard8.plus.com>
Tue, 25 Aug 2009 09:20:13 +0100
changeset 3396 06869471491937b11e5df33a59da9369350976c7
parent 3395 6d86faed2dea7b74cda8bdf98d82ee645b819988
child 3397 c36a73422b327994ca6f8b574428c02b8d0c2b4d
push id2759
push userbugzilla@standard8.plus.com
push dateTue, 25 Aug 2009 08:26:48 +0000
treeherdercomm-central@ef841803601b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasuth
bugs503299
Bug 503299 window.close() from a content tab closes Thunderbird window. r=asuth
mail/base/content/specialTabs.js
--- a/mail/base/content/specialTabs.js
+++ b/mail/base/content/specialTabs.js
@@ -1,9 +1,8 @@
-/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* ***** BEGIN LICENSE BLOCK *****
  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
  * The contents of this file are subject to the Mozilla Public License Version
  * 1.1 (the "License"); you may not use this file except in compliance with
  * the License. You may obtain a copy of the License at
  * http://www.mozilla.org/MPL/
  *
@@ -96,49 +95,51 @@ var specialTabs = {
           tabInfo[selectedIndex].browser
                                 .setAttribute("src", aContentPage);
           return selectedIndex;
         }
       }
       return -1;
     },
     openTab: function onTabOpened(aTab, {contentPage: aContentPage}) {
+      // First clone the page and set up the basics.
       let clone = document.getElementById("contentTab").firstChild.cloneNode(true);
 
       clone.setAttribute("id", "contentTab" + this.lastBrowserId);
       clone.setAttribute("collapsed", false);
       clone.setAttribute("type", "content-primary");
 
       aTab.panel.appendChild(clone);
 
+      // Start setting up the browser.
       aTab.browser = aTab.panel.getElementsByTagName("browser")[0];
 
       aTab.browser.setAttribute("id", "contentTabBrowser" + this.lastBrowserId);
 
-      function onDOMTitleChanged(aEvent) {
-        document.getElementById("tabmail").setTabTitle(aTab);
-      }
-      // Save the function we'll use as listener so we can remove it later.
-      aTab.contentTabType = { titleListener: onDOMTitleChanged };
-      // Add the listener.
-      aTab.browser.addEventListener("DOMTitleChanged",
-                                    aTab.contentTabType.titleListener, true);
+      // Now initialise the find bar.
+      aTab.findbar = aTab.panel.getElementsByTagName("findbar")[0];
+      aTab.findbar.setAttribute("browserid",
+                                "contentTabBrowser" + this.lastBrowserId);
 
+      // Now set up the listeners.
+      this._setUpTitleListener(aTab);
+      this._setUpCloseWindowListener(aTab);
+
+      // Now start loading the content.
       aTab.title = this.loadingTabString;
 
       aTab.browser.setAttribute("src", aContentPage);
 
-      aTab.findbar = aTab.panel.getElementsByTagName("findbar")[0];
-      aTab.findbar.setAttribute("browserid",
-                                "contentTabBrowser" + this.lastBrowserId);
       this.lastBrowserId++;
     },
     closeTab: function onTabClosed(aTab) {
       aTab.browser.removeEventListener("DOMTitleChanged",
-                                                aTab.contentTabType.titleListener, true);
+                                       aTab.titleListener, true);
+      aTab.browser.removeEventListener("DOMWindowClose",
+                                       aTab.closeListener, true);
     },
     saveTabState: function onSaveTabState(aTab) {
       aTab.browser.setAttribute("type", "content-targetable");
     },
     showTab: function onShowTab(aTab) {
       aTab.browser.setAttribute("type", "content-primary");
     },
     onTitleChanged: function onTitleChanged(aTab) {
@@ -214,16 +215,47 @@ var specialTabs = {
         // XXX print preview not currently supported - bug 497994 to implement.
         //case "cmd_printpreview":
         //  PrintUtils.printPreview();
         //  break;
       }
     },
     getBrowser: function getBrowser(aTab) {
       return aTab.browser;
+    },
+    // Internal function used to set up the title listener on a content tab.
+    _setUpTitleListener: function setUpTitleListener(aTab) {
+      function onDOMTitleChanged(aEvent) {
+        document.getElementById("tabmail").setTabTitle(aTab);
+      }
+      // Save the function we'll use as listener so we can remove it later.
+      aTab.titleListener = onDOMTitleChanged;
+      // Add the listener.
+      aTab.browser.addEventListener("DOMTitleChanged",
+                                    aTab.titleListener, true);
+    },
+    /**
+     * Internal function used to set up the close window listener on a content
+     * tab.
+     */
+    _setUpCloseWindowListener: function setUpCloseWindowListener(aTab) {
+      function onDOMWindowClose(aEvent) {
+        if (!aEvent.isTrusted)
+          return;
+
+        // Redirect any window.close events to closing the tab. As a 3-pane tab
+        // must be open, we don't need to worry about being the last tab open.
+        document.getElementById("tabmail").closeTab(aTab);
+        aEvent.preventDefault();
+      }
+      // Save the function we'll use as listener so we can remove it later.
+      aTab.closeListener = onDOMWindowClose;
+      // Add the listener.
+      aTab.browser.addEventListener("DOMWindowClose",
+                                    aTab.closeListener, true);
     }
   },
 
   /**
    * Tests whether the application has been upgraded
    * or not. Updates the pref with the latest version,
    * returns true if upgraded, false otherwise.
    */
@@ -281,17 +313,17 @@ var specialTabs = {
     return prefs.getIntPref("mail.rights.version") < this._kAboutRightsVersion;
   },
 
   showAboutRightsNotification: function(prefs) {
     var notifyBox = document.getElementById("mail-notification-box");
 
     var stringBundle =
       Components.classes["@mozilla.org/intl/stringbundle;1"]
-                .getService(Components.interfaces.nsIStringBundleService)
+                .getService(Components.interfaces.nsIStringBundleService);
     var brandBundle =
       stringBundle.createBundle("chrome://branding/locale/brand.properties");
     var rightsBundle =
       stringBundle.createBundle("chrome://messenger/locale/aboutRights.properties");
 
     var productName = brandBundle.GetStringFromName("brandFullName");
     var notifyRightsText = rightsBundle.formatStringFromName("notifyRightsText",
                                                              [productName], 1);
@@ -313,9 +345,9 @@ var specialTabs = {
                                            null, notifyBox.PRIORITY_INFO_LOW,
                                            buttons);
     // arbitrary number, just so bar sticks around for a bit
     box.persistence = 3;
 
     // Set the pref to say we've displayed the notification.
     prefs.setIntPref("mail.rights.version", this._kAboutRightsVersion);
   }
-}
+};