Bug 1559793 - Fix OTRUI.jsm exception this.globalDoc is null. r=kaie a=jorgk
authorAlessandro Castellani <alessandro@thunderbird.net>
Fri, 05 Jul 2019 11:01:14 -0700
changeset 36054 98ee2d4ceaca634121e90539e09d4a4b12274887
parent 36053 0549ec9b0487dbd1b9a0c4564c7e39192c6ccd9f
child 36055 1e6a4c2024e2054cc2e7ab6659189c5fc63a503f
push id392
push userclokep@gmail.com
push dateMon, 02 Sep 2019 20:17:19 +0000
reviewerskaie, jorgk
bugs1559793
Bug 1559793 - Fix OTRUI.jsm exception this.globalDoc is null. r=kaie a=jorgk
chat/modules/OTRUI.jsm
--- a/chat/modules/OTRUI.jsm
+++ b/chat/modules/OTRUI.jsm
@@ -91,32 +91,35 @@ var windowRefs = new Map();
 var OTRUI = {
   enabled: false,
   stringsLoaded: false,
   globalDoc: null,
   visibleConv: null,
 
   debug: false,
   logMsg(msg) {
-    if (!OTRUI.debug)
+    if (!OTRUI.debug) {
       return;
+    }
     Services.console.logStringMessage(msg);
   },
 
   addMenuObserver() {
     let iter = Services.ww.getWindowEnumerator();
-    while (iter.hasMoreElements())
+    while (iter.hasMoreElements()) {
       OTRUI.addMenus(iter.getNext());
+    }
     Services.obs.addObserver(OTRUI, "domwindowopened");
   },
 
   removeMenuObserver() {
     let iter = Services.ww.getWindowEnumerator();
-    while (iter.hasMoreElements())
+    while (iter.hasMoreElements()) {
       OTRUI.removeMenus(iter.getNext());
+    }
     Services.obs.removeObserver(OTRUI, "domwindowopened");
   },
 
   addMenus(win) {
     let doc = win.document;
     // Account for unready windows
     if (doc.readyState !== "complete") {
       let listen = function() {
@@ -256,24 +259,26 @@ var OTRUI = {
       ChromeUtils.idleDispatch(OTRUI.genMissingKeys);
     }).catch(function(err) {
       // console.log("===> " + err + "\n");
       throw err;
     });
   },
 
   disconnect(aConv) {
-    if (aConv)
+    if (aConv) {
       return OTR.disconnect(aConv, true);
+    }
     let allGood = true;
     let conversations = Services.conversations.getConversations();
     while (conversations.hasMoreElements()) {
       let conv = conversations.getNext();
-      if (conv.isChat)
+      if (conv.isChat) {
         continue;
+      }
       if (!OTR.disconnect(conv, true)) {
         allGood = false;
       }
     }
     return allGood;
   },
 
   openAuth(window, name, mode, uiConv, contactInfo) {
@@ -291,18 +296,19 @@ var OTRUI = {
     window.addEventListener("beforeunload", function() {
       otrAuth.disabled = false;
       windowRefs.delete(name);
     });
   },
 
   closeAuth(context) {
     let win = windowRefs.get(context.username);
-    if (win)
+    if (win) {
       win.close();
+    }
   },
 
   noOtrPossible(otrContainer, context) {
     otrContainer.hidden = true;
 
     if (context) {
       OTRUI.hideUserNotifications(context);
     } else {
@@ -334,30 +340,34 @@ var OTRUI = {
   addButton(aObject) {
     this.globalDoc = aObject.ownerDocument;
     let _conv = aObject._conv;
     OTRUI.visibleConv = _conv;
     OTRUI.setMsgState(_conv, null, this.globalDoc, true);
   },
 
   hideOTRButton() {
-    if (!OTR.libLoaded)
+    if (!OTR.libLoaded) {
       return;
-    if (!this.globalDoc)
+    }
+    if (!this.globalDoc) {
       return;
+    }
     OTRUI.visibleConv = null;
     let otrContainer = this.globalDoc.querySelector(".otr-container");
     OTRUI.noOtrPossible(otrContainer);
   },
 
   updateOTRButton(_conv) {
-    if (!OTR.libLoaded)
+    if (!OTR.libLoaded) {
       return;
-    if (!this.globalDoc)
+    }
+    if (!this.globalDoc) {
       return;
+    }
     OTRUI.visibleConv = _conv;
     let convBinding =
       this.globalDoc.getElementById("conversationsDeck").selectedPanel;
     if (convBinding && convBinding._conv && convBinding._conv.target) {
       OTRUI.setMsgState(_conv, null, this.globalDoc, false);
     } else {
       this.hideOTRButton();
     }
@@ -440,26 +450,30 @@ var OTRUI = {
 
   getTrustSettings(context) {
     let result = trustMap.get(OTR.trust(context));
     return result;
   },
 
   askAuth(aObject) {
     let uiConv = OTR.getUIConvFromContext(aObject.context);
-    if (!uiConv) return;
+    if (!uiConv) {
+      return;
+    }
 
     let window = this.globalDoc.defaultView;
     let name = uiConv.target.normalizedName;
     OTRUI.openAuth(window, name, "ask", uiConv, aObject);
   },
 
   closeUnverified(context) {
     let uiConv = OTR.getUIConvFromContext(context);
-    if (!uiConv) return;
+    if (!uiConv) {
+      return;
+    }
 
     let notifications = this.globalBox.allNotifications;
     for (let i = notifications.length - 1; i >= 0; i--) {
       if (context.username == notifications[i].getAttribute("user") &&
           notifications[i].getAttribute("value") == AUTH_STATUS_UNVERIFIED) {
         notifications[i].close();
       }
     }
@@ -479,24 +493,27 @@ var OTRUI = {
     for (let i = notifications.length - 1; i >= 0; i--) {
       notifications[i].setAttribute("hidden", "true");
     }
   },
 
   showUserNotifications(context) {
     let notifications = this.globalBox.allNotifications;
     for (let i = notifications.length - 1; i >= 0; i--) {
-      if (context.username == notifications[i].getAttribute("user"))
+      if (context.username == notifications[i].getAttribute("user")) {
         notifications[i].removeAttribute("hidden");
+      }
     }
   },
 
   notifyUnverified(context, seen) {
     let uiConv = OTR.getUIConvFromContext(context);
-    if (!uiConv) return;
+    if (!uiConv) {
+      return;
+    }
 
     let window = this.globalDoc.defaultView;
 
     let msg = _strArgs("finger-" + seen, {name: context.username});
     let buttons = [{
       label: _str("finger-verify"),
       accessKey: _str("finger-verify-accessKey"),
       callback() {
@@ -508,16 +525,20 @@ var OTRUI = {
     }];
 
     let priority = this.globalBox.PRIORITY_WARNING_MEDIUM;
     this.globalBox.appendNotification(msg, context.username, null, priority, buttons, null);
 
     let verifyTitle = syncL10n.formatValueSync("verify-title");
     this.updateNotificationUI(context, verifyTitle, context.username, AUTH_STATUS_UNVERIFIED);
 
+    if (!this.visibleConv) {
+      return;
+    }
+
     if (context.username !== this.visibleConv.normalizedName) {
       this.hideUserNotifications(context);
     }
   },
 
   updateNotificationUI(context, typeTitle, username, key) {
     let notification = this.globalBox.getNotificationWithValue(username);
     notification.setAttribute("user", context.username);
@@ -553,27 +574,31 @@ var OTRUI = {
       bottom.appendChild(e);
     });
 
     notification.appendChild(bottom);
   },
 
   closeVerification(context) {
     let uiConv = OTR.getUIConvFromContext(context);
-    if (!uiConv) return;
+    if (!uiConv) {
+      return;
+    }
 
     let prevNotification = OTRUI.globalBox.getNotificationWithValue(context.username);
     if (prevNotification) {
       prevNotification.close();
     }
   },
 
   notifyVerification(context, key, cancelable) {
     let uiConv = OTR.getUIConvFromContext(context);
-    if (!uiConv) return;
+    if (!uiConv) {
+      return;
+    }
 
     // TODO: maybe update the .label property on the notification instead
     // of closing it ... although, buttons need to be updated too.
     OTRUI.closeVerification(context);
 
     let msg = authLabelMap.get(key);
     let typeTitle = authTitleMap.get(key);
     let buttons = [];
@@ -607,34 +632,36 @@ var OTRUI = {
         if (aObj.context.trust) {
           key = "otr:auth-success";
           OTR.notifyTrust(aObj.context);
         } else {
           key = "otr:auth-successThem";
         }
       } else {
         key = "otr:auth-fail";
-        if (!aObj.context.trust)
+        if (!aObj.context.trust) {
           OTR.notifyTrust(aObj.context);
+        }
       }
       OTRUI.notifyVerification(aObj.context, key, false);
     } else {
       // TODO: show the aObj.progress to the user with a
       //   <progressmeter mode="determined" value="10" />
       OTRUI.notifyVerification(aObj.context, "otr:auth-waiting", true);
     }
   },
 
   onAccountCreated(acc) {
     let account = acc.normalizedName;
     let protocol = acc.protocol.normalizedName;
     Promise.resolve();
-    if (OTR.privateKeyFingerprint(account, protocol) === null)
+    if (OTR.privateKeyFingerprint(account, protocol) === null) {
       OTR.generatePrivateKey(account, protocol)
-      .catch(OTRUI.reportKeyGenFailure);
+         .catch(OTRUI.reportKeyGenFailure);
+    }
   },
 
   contactWrapper(contact) {
     // If the conversation already started.
     if (contact.buddy) {
       return {
         account: contact.buddy.normalizedName,
         protocol: contact.buddy.buddy.protocol.normalizedName,
@@ -647,18 +674,20 @@ var OTRUI = {
       account: contact.preferredBuddy.preferredAccountBuddy.account.normalizedName,
       protocol: contact.preferredBuddy.protocol.normalizedName,
       screenname: contact.preferredBuddy.preferredAccountBuddy.userName,
     };
   },
 
   onContactAdded(contact) {
     let args = OTRUI.contactWrapper(contact);
-    if (OTR.getFingerprintsForRecipient(args.account, args.protocol, args.screenname).length > 0)
+    if (OTR.getFingerprintsForRecipient(args.account,
+        args.protocol, args.screenname).length > 0) {
       return;
+    }
     args.wrappedJSObject = args;
     let features = "chrome,modal,centerscreen,resizable=no,minimizable=no";
     Services.ww.openWindow(null, OTR_ADD_FINGER_DIALOG_URL, "", features, args);
   },
 
   observe(aObject, aTopic, aMsg) {
     let doc;
     // console.log("====> observing topic: " + aTopic + " with msg: " + aMsg);
@@ -671,18 +700,19 @@ var OTRUI = {
         doc = aObject.ownerDocument;
         let windowtype = doc.documentElement.getAttribute("windowtype");
         if (windowtype !== "mail:3pane") {
           return;
         }
         OTRUI.addButton(aObject);
         break;
       case "conversation-closed":
-        if (aObject.isChat)
+        if (aObject.isChat) {
           return;
+        }
         this.globalBox.removeAllNotifications();
         OTRUI.closeAuth(OTR.getContext(aObject));
         OTRUI.disconnect(aObject);
         break;
       // case "contact-signed-off":
       //  break;
       case "prpl-quit":
         OTRUI.disconnect(null);
@@ -704,16 +734,25 @@ var OTRUI = {
             OTR.trust(aObject) !== OTR.trustState.TRUST_UNVERIFIED) {
           OTRUI.closeAuth(aObject);
           OTRUI.closeUnverified(aObject);
           OTRUI.closeVerification(aObject);
         }
         OTRUI.setMsgState(null, aObject, this.globalDoc, false);
         break;
       case "otr:unverified":
+        if (!this.globalDoc) {
+          let win = Services.wm.getMostRecentWindow("mail:3pane");
+          if (!win) {
+            return;
+          }
+          win.focus();
+          win.showChatTab();
+          this.globalDoc = win.document;
+        }
         OTRUI.notifyUnverified(aObject, aMsg);
         break;
       case "otr:trust-state":
         OTRUI.alertTrust(aObject);
         break;
       case "otr:log":
         OTRUI.logMsg("otr: " + aObject);
         break;
@@ -735,24 +774,26 @@ var OTRUI = {
   initConv(binding) {
     OTR.addConversation(binding._conv);
     OTRUI.addButton(binding);
   },
 
   resetConv(binding) {
     OTR.removeConversation(binding._conv);
     let otrButton = this.globalDoc.querySelector(".otr-button");
-    if (!otrButton)
+    if (!otrButton) {
       return;
+    }
     otrButton.remove();
   },
 
   destroy() {
-    if (!OTR.libLoaded)
+    if (!OTR.libLoaded) {
       return;
+    }
     OTRUI.disconnect(null);
     Services.obs.removeObserver(OTR, "new-ui-conversation");
     // Services.obs.removeObserver(OTRUI, "contact-added");
     // Services.obs.removeObserver(OTRUI, "contact-signed-off");
     Services.obs.removeObserver(OTRUI, "account-added");
     Services.obs.removeObserver(OTRUI, "conversation-loaded");
     Services.obs.removeObserver(OTRUI, "conversation-closed");
     Services.obs.removeObserver(OTRUI, "prpl-quit");