fix bug 738903, closing compose window can cause app to exit in some situations, r=mkmelin,a=Standard8
authorDavid Bienvenu <bienvenu@nventure.com>
Wed, 28 Mar 2012 07:22:51 -0700
changeset 10697 f23162a04301c327f7f30854350de2d88180e551
parent 10696 934cea1f8fcfd25061f2c0b881c25509afd9d846
child 10698 c09e188e7fe170ac36232338dde35547b2ccc6c5
push id433
push userbugzilla@standard8.plus.com
push dateTue, 03 Apr 2012 21:19:55 +0000
treeherdercomm-beta@9bdfa24dedd9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin, Standard8
bugs738903
fix bug 738903, closing compose window can cause app to exit in some situations, r=mkmelin,a=Standard8
mail/components/compose/content/MsgComposeCommands.js
--- a/mail/components/compose/content/MsgComposeCommands.js
+++ b/mail/components/compose/content/MsgComposeCommands.js
@@ -2263,16 +2263,17 @@ function GenericSendMessage(msgType)
       else
         fallbackCharset.value = "UTF-8";
     }
 
     if (fallbackCharset &&
         fallbackCharset.value && fallbackCharset.value != "")
       gMsgCompose.SetDocumentCharset(fallbackCharset.value);
   }
+
   try {
     // Just before we try to send the message, fire off the
     // compose-send-message event for listeners such as smime so they can do
     // any pre-security work such as fetching certificates before sending.
     var event = document.createEvent("UIEvents");
     event.initEvent("compose-send-message", false, true);
     var msgcomposeWindow = document.getElementById("msgcomposeWindow");
     msgcomposeWindow.setAttribute("msgtype", msgType);
@@ -2309,17 +2310,17 @@ function GenericSendMessage(msgType)
                         getCurrentAccountKey(), msgWindow, progress);
   }
   catch (ex) {
     Components.utils.reportError("GenericSendMessage FAILED: " + ex);
     gWindowLocked = false;
     enableEditableFields();
     updateComposeItems();
   }
-  if (originalCharset != gMsgCompose.compFields.characterSet)
+  if (gMsgCompose && originalCharset != gMsgCompose.compFields.characterSet)
     SetDocumentCharacterSet(gMsgCompose.compFields.characterSet);
 }
 
 function CheckValidEmailAddress(to, cc, bcc)
 {
   var invalidStr = null;
   // crude check that the to, cc, and bcc fields contain at least one '@'.
   // We could parse each address, but that might be overkill.
@@ -2848,18 +2849,32 @@ function ComposeCanClose()
                               (gPromptService.BUTTON_TITLE_SAVE * gPromptService.BUTTON_POS_0) +
                               (gPromptService.BUTTON_TITLE_CANCEL * gPromptService.BUTTON_POS_1) +
                               (gPromptService.BUTTON_TITLE_DONT_SAVE * gPromptService.BUTTON_POS_2),
                               null, null, null,
                               null, {value:0});
       switch (result)
       {
         case 0: //Save
+          // Since we're going to save the message, we tell toolkit that
+          // the close command failed, by returning false, and then
+          // we close the window ourselves after the save is done.
           gCloseWindowAfterSave = true;
-          GenericSendMessage(nsIMsgCompDeliverMode.AutoSaveAsDraft);
+          // We catch the exception because we need to tell toolkit that it
+          // shouldn't close the window, because we're going to close it
+          // ourselves. If we don't tell toolkit that, and then close the window
+          // ourselves, the toolkit code that keeps track of the open windows
+          // gets off by one and the app can close unexpectedly on os's that
+          // shutdown the app when the last window is closed.
+          try {
+            GenericSendMessage(nsIMsgCompDeliverMode.AutoSaveAsDraft);
+          }
+          catch (ex) {
+            Components.utils.reportError(ex);
+          }
           return false;
         case 1: //Cancel
           return false;
         case 2: //Don't Save
           // don't delete the draft if we didn't start off editing a draft
           // and the user hasn't explicitly saved it.
           if (!gEditingDraft && gAutoSaveKickedIn)
             RemoveDraft();