Bug 1616142 - Wait for compose window to open before returning from compose API functions. r=mkmelin a=jorgk
authorGeoff Lankow <geoff@darktrojan.net>
Tue, 18 Feb 2020 12:07:47 +1300
changeset 38189 fdc806bd568ced6bb94bc8324a27cecaadaf2c94
parent 38188 db119ad339d16caedefb445ae11a5ffb5714de24
child 38190 e5186eaf6d21744ea6404a6b351db3a28a0cf40d
push id398
push userclokep@gmail.com
push dateMon, 09 Mar 2020 19:10:28 +0000
reviewersmkmelin, jorgk
bugs1616142
Bug 1616142 - Wait for compose window to open before returning from compose API functions. r=mkmelin a=jorgk
mail/components/extensions/parent/ext-compose.js
--- a/mail/components/extensions/parent/ext-compose.js
+++ b/mail/components/extensions/parent/ext-compose.js
@@ -40,35 +40,51 @@ async function parseComposeRecipientList
       }
     }
     return recipients.join(",");
   }
   return list;
 }
 
 async function openComposeWindow(relatedMessageId, type, composeParams) {
+  function waitForWindow() {
+    return new Promise(resolve => {
+      function observer(subject, topic, data) {
+        if (
+          subject.location.href ==
+          "chrome://messenger/content/messengercompose/messengercompose.xhtml"
+        ) {
+          Services.obs.removeObserver(observer, "chrome-document-loaded");
+          resolve();
+        }
+      }
+      Services.obs.addObserver(observer, "chrome-document-loaded");
+    });
+  }
+
   // ForwardInline is totally broken, see bug 1513824.
   if (type == Ci.nsIMsgCompType.ForwardInline) {
     let msgHdr = null;
     let msgURI = null;
     let hdrIdentity = null;
     if (relatedMessageId) {
       msgHdr = messageTracker.getMessage(relatedMessageId);
       msgURI = msgHdr.folder.getUriForMsg(msgHdr);
     }
+    let newWindowPromise = waitForWindow();
     MailServices.compose.OpenComposeWindow(
       null,
       msgHdr,
       msgURI,
       type,
       0,
       hdrIdentity,
       null
     );
-    return;
+    return newWindowPromise;
   }
 
   let params = Cc[
     "@mozilla.org/messengercompose/composeparams;1"
   ].createInstance(Ci.nsIMsgComposeParams);
   let composeFields = Cc[
     "@mozilla.org/messengercompose/composefields;1"
   ].createInstance(Ci.nsIMsgCompFields);
@@ -94,17 +110,20 @@ async function openComposeWindow(related
     for (let field of ["subject", "body"]) {
       if (composeParams[field]) {
         composeFields[field] = composeParams[field];
       }
     }
   }
 
   params.composeFields = composeFields;
+
+  let newWindowPromise = waitForWindow();
   MailServices.compose.OpenComposeWindowWithParams(null, params);
+  return newWindowPromise;
 }
 
 function getComposeDetails(composeWindow) {
   let composeFields = composeWindow.GetComposeDetails();
 
   let details = {
     to: composeFields.splitRecipients(composeFields.to, false),
     cc: composeFields.splitRecipients(composeFields.cc, false),
@@ -221,39 +240,39 @@ this.compose = class extends ExtensionAP
             };
 
             composeEventTracker.on("compose-before-send", listener);
             return () => {
               composeEventTracker.off("compose-before-send", listener);
             };
           },
         }).api(),
-        async beginNew(composeParams) {
-          openComposeWindow(null, Ci.nsIMsgCompType.New, composeParams);
+        beginNew(composeParams) {
+          return openComposeWindow(null, Ci.nsIMsgCompType.New, composeParams);
         },
         beginReply(messageId, replyType) {
           let type = Ci.nsIMsgCompType.Reply;
           if (replyType == "replyToList") {
             type = Ci.nsIMsgCompType.ReplyToList;
           } else if (replyType == "replyToAll") {
             type = Ci.nsIMsgCompType.ReplyAll;
           }
-          openComposeWindow(messageId, type);
+          return openComposeWindow(messageId, type);
         },
         beginForward(messageId, forwardType, composeParams) {
           let type = Ci.nsIMsgCompType.ForwardInline;
           if (forwardType == "forwardAsAttachment") {
             type = Ci.nsIMsgCompType.ForwardAsAttachment;
           } else if (
             forwardType === null &&
             Services.prefs.getIntPref("mail.forward_message_mode") == 0
           ) {
             type = Ci.nsIMsgCompType.ForwardAsAttachment;
           }
-          openComposeWindow(messageId, type, composeParams);
+          return openComposeWindow(messageId, type, composeParams);
         },
         getComposeDetails(tabId) {
           let tab = getComposeTab(tabId);
           return getComposeDetails(tab.nativeTab);
         },
         setComposeDetails(tabId, details) {
           let tab = getComposeTab(tabId);
           return setComposeDetails(tab.nativeTab, details);