Bug 920801 - Port chat/ changes from Instantbird to comm-central - 6 - Bio 608 - Exception from buddies of an unknown account type, r=clokep.
authorFlorian Quèze <florian@instantbird.org>
Mon, 23 Sep 2013 01:02:40 +0200
changeset 17238 c47804c65bb49b7def37f785eaebe046bb785fbf
parent 17237 7f24d4cea1c92cb5a97b5b3f8b1ed7af339641a7
child 17239 9468904a2b53974ceca4f3bb71acc7ea2bf19330
push id1103
push usermbanner@mozilla.com
push dateTue, 18 Mar 2014 07:44:06 +0000
treeherdercomm-beta@50c6279a0af0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersclokep
bugs920801
Bug 920801 - Port chat/ changes from Instantbird to comm-central - 6 - Bio 608 - Exception from buddies of an unknown account type, r=clokep.
chat/components/src/imAccounts.js
chat/components/src/imContacts.js
chat/components/src/test/test_accounts.js
--- a/chat/components/src/imAccounts.js
+++ b/chat/components/src/imAccounts.js
@@ -1,15 +1,16 @@
 /* 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/. */
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 Cu.import("resource:///modules/imXPCOMUtils.jsm");
 Cu.import("resource:///modules/imServices.jsm");
+Cu.import("resource:///modules/jsProtoHelper.jsm");
 
 const kPrefAutologinPending = "messenger.accounts.autoLoginPending";
 const kPrefMessengerAccounts = "messenger.accounts";
 const kPrefAccountPrefix = "messenger.account.";
 const kAccountKeyPrefix = "account";
 const kAccountOptionPrefPrefix = "options.";
 const kPrefAccountName = "name";
 const kPrefAccountPrpl = "prpl";
@@ -94,16 +95,21 @@ UnknownProtocol.prototype = {
   get imagesInIM() false,
   get passwordOptional() true,
   get usePointSize() true,
   get registerNoScreenName() false,
   get slashCommandsNative() false,
   get usePurpleProxy() false
 };
 
+function UnknownAccountBuddy(aAccount, aBuddy, aTag) {
+  this._init({imAccount: aAccount}, aBuddy, aTag);
+}
+UnknownAccountBuddy.prototype = GenericAccountBuddyPrototype;
+
 // aName and aPrplId are provided as parameter only if this is a new
 // account that doesn't exist in the preferences. In this case, these
 // 2 values should be stored.
 function imAccount(aKey, aName, aPrplId)
 {
   if (!aKey.startsWith(kAccountKeyPrefix))
     throw Cr.NS_ERROR_INVALID_ARG;
 
@@ -435,17 +441,20 @@ imAccount.prototype = {
           errorReason != Ci.imIAccount.ERROR_MISSING_PASSWORD &&
           errorReason != Ci.imIAccount.ERROR_CRASHED &&
           errorReason != Ci.imIAccount.ERROR_UNKNOWN_PRPL) {
         this.connect();
       }
     }.bind(this));
   },
 
-  get normalizedName() this._ensurePrplAccount.normalizedName,
+  // If the protocol plugin is missing, we can't access the normalizedName,
+  // but in lots of cases this.name is equivalent.
+  get normalizedName()
+    this.prplAccount ? this.prplAccount.normalizedName : this.name,
 
   _sendUpdateNotification: function() {
     this._sendNotification("account-updated");
   },
 
   set alias(val) {
     if (val) {
       let str = Cc["@mozilla.org/supports-string;1"]
@@ -591,21 +600,19 @@ imAccount.prototype = {
     AutoLoginCounter.finishedAutoLogin();
   },
 
   // Delete the account (from the preferences, mozStorage, and call unInit).
   remove: function() {
     let login = Cc["@mozilla.org/login-manager/loginInfo;1"]
                 .createInstance(Ci.nsILoginInfo);
     let passwordURI = "im://" + this.protocol.id;
-    // The password is stored with the normalizedName. If the protocol
-    // plugin is missing, we can't access the normalizedName, but in
-    // lots of cases this.name is equivalent.
-    let name = this.prplAccount ? this.normalizedName : this.name;
-    login.init(passwordURI, null, passwordURI, name, "", "", "");
+    // Note: the normalizedName may not be exactly right if the
+    // protocol plugin is missing.
+    login.init(passwordURI, null, passwordURI, this.normalizedName, "", "", "");
     let logins = Services.logins.findLogins({}, passwordURI, null, passwordURI);
     for each (let l in logins) {
       if (login.matches(l, true)) {
         Services.logins.removeLogin(l);
         break;
       }
     }
     if (this.prplAccount)
@@ -732,18 +739,22 @@ imAccount.prototype = {
 
     this._cancelReconnection();
   },
   createConversation: function(aName)
     this._ensurePrplAccount.createConversation(aName),
   addBuddy: function(aTag, aName) {
     this._ensurePrplAccount.addBuddy(aTag, aName);
   },
-  loadBuddy: function(aBuddy, aTag)
-    this._ensurePrplAccount.loadBuddy(aBuddy, aTag), // FIXME for unknown proto
+  loadBuddy: function(aBuddy, aTag) {
+    if (this.prplAccount)
+      return this.prplAccount.loadBuddy(aBuddy, aTag);
+    // Generate dummy account buddies for unknown protocols.
+    return new UnknownAccountBuddy(this, aBuddy, aTag);
+  },
   requestBuddyInfo: function(aBuddyName) {
     this._ensurePrplAccount.requestBuddyInfo(aBuddyName);
   },
   getChatRoomFields: function() this._ensurePrplAccount.getChatRoomFields(),
   getChatRoomDefaultFieldValues: function(aDefaultChatName)
     this._ensurePrplAccount.getChatRoomDefaultFieldValues(aDefaultChatName),
   get canJoinChat() this.prplAccount ? this.prplAccount.canJoinChat : false,
   joinChat: function(aComponents) {
--- a/chat/components/src/imContacts.js
+++ b/chat/components/src/imContacts.js
@@ -1282,23 +1282,19 @@ ContactsService.prototype = {
                          "account_id = " + accountId + ", buddy_id = " + buddyId +
                          ", tag_id = " + tagId);
           continue;
         }
 
         let account = Services.accounts.getAccountByNumericId(accountId);
         let tag = TagsById[tagId];
         try {
-          let accountBuddy = account.loadBuddy(buddy, tag);
-          if (accountBuddy)
-            buddy._addAccount(accountBuddy, tag);
+          buddy._addAccount(account.loadBuddy(buddy, tag), tag);
         } catch (e) {
-          // FIXME accountBuddy shouldn't be NULL (once imAccounts.js is finished)
-          // It currently doesn't work right with unknown protocols.
-          Components.utils.reportError(e);
+          Cu.reportError(e);
           dump(e + "\n");
         }
       }
     } finally {
       statement.finalize();
     }
     otherContactsTag._initHiddenTags();
   },
--- a/chat/components/src/test/test_accounts.js
+++ b/chat/components/src/test/test_accounts.js
@@ -24,16 +24,17 @@ function run_test() {
     // Having an implementation of nsIXULAppInfo is required for
     // Services.core.init to work.
     XULAppInfo.init();
     Services.core.init();
 
     let account = Services.accounts.getAccountByNumericId(1);
     do_check_true(account instanceof Ci.imIAccount);
     do_check_eq(account.name, kAccountName);
+    do_check_eq(account.normalizedName, kAccountName);
     do_check_eq(account.protocol.id, kPrplId);
     do_check_eq(account.connectionErrorReason, Ci.imIAccount.ERROR_UNKNOWN_PRPL);
   } finally {
     Services.core.quit();
 
     prefs.deleteBranch("messenger");
   }
 }