Bug 910226 - Adding finalizations to imContacts.js;r=flo,a=bustage-fix

This commit is contained in:
David Rajchenbach-Teller 2013-08-29 12:18:12 +02:00
Родитель e90545edf0
Коммит 4054e481f6
1 изменённых файлов: 186 добавлений и 101 удалений

Просмотреть файл

@ -8,6 +8,12 @@ Cu.import("resource:///modules/imServices.jsm");
var gDBConnection = null;
function executeAsyncThenFinalize(statement)
{
statement.executeAsync();
statement.finalize();
}
function getDBConnection()
{
const NS_APP_USER_PROFILE_50_DIR = "ProfD";
@ -116,8 +122,12 @@ TagsService.prototype = {
return tag;
let statement = DBConn.createStatement("INSERT INTO tags (name, position) VALUES(:name, 0)");
statement.params.name = aName;
statement.executeStep();
try {
statement.params.name = aName;
statement.executeStep();
} finally {
statement.finalize();
}
tag = new Tag(DBConn.lastInsertRowID, aName);
Tags.push(tag);
@ -130,8 +140,12 @@ TagsService.prototype = {
getTagByName: function(aName) {
let statement = DBConn.createStatement("SELECT id FROM tags where name = :name");
statement.params.name = aName;
if (!statement.executeStep())
return null;
try {
if (!statement.executeStep())
return null;
} finally {
statement.finalize();
}
return this.getTagById(statement.row.id);
},
// Get an array of all existing tags.
@ -172,9 +186,13 @@ Tag.prototype = {
get name() this._name,
set name(aNewName) {
let statement = DBConn.createStatement("UPDATE tags SET name = :name WHERE id = :id");
statement.params.name = aNewName;
statement.params.id = this._id;
statement.execute();
try {
statement.params.name = aNewName;
statement.params.id = this._id;
statement.execute();
} finally {
statement.finalize();
}
//FIXME move the account buddies if some use this tag as their group
return aNewName;
@ -370,7 +388,7 @@ Contact.prototype = {
let statement = DBConn.createStatement("UPDATE contacts SET alias = :alias WHERE id = :id");
statement.params.alias = aNewAlias;
statement.params.id = this._id;
statement.executeAsync();
executeAsyncThenFinalize(statement);
let oldDisplayName = this.displayName;
this._alias = aNewAlias;
@ -386,7 +404,11 @@ Contact.prototype = {
// Create a real contact for this dummy contact
let statement = DBConn.createStatement("INSERT INTO contacts DEFAULT VALUES");
statement.execute();
try {
statement.execute();
} finally {
statement.finalize();
}
delete ContactsById[this._id];
let oldId = this._id;
this._id = DBConn.lastInsertRowID;
@ -396,7 +418,7 @@ Contact.prototype = {
statement = DBConn.createStatement("UPDATE buddies SET contact_id = :id WHERE id = :buddy_id");
statement.params.id = this._id;
statement.params.buddy_id = this._buddies[0].id;
statement.executeAsync();
executeAsyncThenFinalize(statement);
},
getTags: function(aTagCount) {
@ -415,7 +437,7 @@ Contact.prototype = {
"VALUES(:contactId, :tagId)");
statement.params.contactId = this.id;
statement.params.tagId = aTag.id;
statement.executeAsync();
executeAsyncThenFinalize(statement);
}
aTag = TagsById[aTag.id];
@ -449,7 +471,7 @@ Contact.prototype = {
"AND tag_id = :tagId");
statement.params.contactId = this.id;
statement.params.tagId = aTag.id;
statement.executeAsync();
executeAsyncThenFinalize(statement);
},
hasTag: function(aTag) this._tags.some(function (t) t.id == aTag.id),
_massMove: false,
@ -611,7 +633,7 @@ Contact.prototype = {
let statement =
DBConn.createStatement("DELETE FROM contacts WHERE id = :id");
statement.params.id = this._id;
statement.executeAsync();
executeAsyncThenFinalize(statement);
}
this._notifyObservers("removed");
delete ContactsById[this._id];
@ -621,7 +643,7 @@ Contact.prototype = {
let statement =
DBConn.createStatement("DELETE FROM contact_tag WHERE contact_id = :id");
statement.params.id = this._id;
statement.executeAsync();
executeAsyncThenFinalize(statement);
delete this._tags;
delete this._buddies;
@ -659,7 +681,7 @@ Contact.prototype = {
for (let i = aIndexBegin; i <= aIndexEnd; ++i) {
statement.params.position = i;
statement.params.buddyId = this._buddies[i].id;
statement.executeAsync();
executeAsyncThenFinalize(statement);
}
},
@ -928,7 +950,7 @@ Buddy.prototype = {
statement.params.contactId = aContact.id > 0 ? aContact.id : 0;
statement.params.position = aContact._buddies.length - 1;
statement.params.buddyId = this.id;
statement.executeAsync();
executeAsyncThenFinalize(statement);
this._notifyObservers("moved-into-contact");
return aContact;
@ -1121,7 +1143,7 @@ Buddy.prototype = {
"WHERE id = :buddyId");
statement.params.buddyId = this.id;
statement.params.srvAlias = this._srvAlias;
statement.executeAsync();
executeAsyncThenFinalize(statement);
this._notifyObservers("display-name-changed", aData);
}
break;
@ -1147,9 +1169,12 @@ Buddy.prototype = {
if (this._accounts.length == 1) {
let statement =
DBConn.createStatement("DELETE FROM buddies WHERE id = :id");
statement.params.id = this.id;
statement.execute();
try {
statement.params.id = this.id;
statement.execute();
} finally {
statement.finalize();
}
this._notifyObservers("removed");
delete BuddiesById[this._id];
@ -1187,64 +1212,84 @@ function ContactsService() { }
ContactsService.prototype = {
initContacts: function() {
let statement = DBConn.createStatement("SELECT id, name FROM tags");
while (statement.executeStep())
Tags.push(new Tag(statement.getInt32(0), statement.getUTF8String(1)));
try {
while (statement.executeStep())
Tags.push(new Tag(statement.getInt32(0), statement.getUTF8String(1)));
} finally {
statement.finalize();
}
statement = DBConn.createStatement("SELECT id, alias FROM contacts");
while (statement.executeStep())
new Contact(statement.getInt32(0), statement.getUTF8String(1));
try {
while (statement.executeStep())
new Contact(statement.getInt32(0), statement.getUTF8String(1));
} finally {
statement.finalize();
}
statement =
DBConn.createStatement("SELECT contact_id, tag_id FROM contact_tag");
while (statement.executeStep()) {
let contact = ContactsById[statement.getInt32(0)];
let tag = TagsById[statement.getInt32(1)];
contact._tags.push(tag);
tag._addContact(contact);
try {
while (statement.executeStep()) {
let contact = ContactsById[statement.getInt32(0)];
let tag = TagsById[statement.getInt32(1)];
contact._tags.push(tag);
tag._addContact(contact);
}
} finally {
statement.finalize();
}
statement = DBConn.createStatement("SELECT id, key, name, srv_alias, contact_id FROM buddies ORDER BY position");
while (statement.executeStep())
new Buddy(statement.getInt32(0), statement.getUTF8String(1),
try {
while (statement.executeStep()) {
new Buddy(statement.getInt32(0), statement.getUTF8String(1),
statement.getUTF8String(2), statement.getUTF8String(3),
statement.getInt32(4));
// FIXME is there a way to enforce that all AccountBuddies of a Buddy have the same protocol?
statement = DBConn.createStatement("SELECT account_id, buddy_id, tag_id FROM account_buddy");
while (statement.executeStep()) {
let accountId = statement.getInt32(0);
let buddyId = statement.getInt32(1);
let tagId = statement.getInt32(2);
if (!BuddiesById.hasOwnProperty(buddyId)) {
Cu.reportError("Corrupted database: account_buddy entry for account " +
accountId + " and tag " + tagId +
" references unknown buddy with id " + buddyId);
continue;
}
let buddy = BuddiesById[buddyId];
if (buddy._hasAccountBuddy(accountId, tagId)) {
Cu.reportError("Corrupted database: duplicated account_buddy entry: " +
"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);
} 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);
dump(e + "\n");
// FIXME is there a way to enforce that all AccountBuddies of a Buddy have the same protocol?
}
} finally {
statement.finalize();
}
statement = DBConn.createStatement("SELECT account_id, buddy_id, tag_id FROM account_buddy");
try {
while (statement.executeStep()) {
let accountId = statement.getInt32(0);
let buddyId = statement.getInt32(1);
let tagId = statement.getInt32(2);
if (!BuddiesById.hasOwnProperty(buddyId)) {
Cu.reportError("Corrupted database: account_buddy entry for account " +
accountId + " and tag " + tagId +
" references unknown buddy with id " + buddyId);
continue;
}
let buddy = BuddiesById[buddyId];
if (buddy._hasAccountBuddy(accountId, tagId)) {
Cu.reportError("Corrupted database: duplicated account_buddy entry: " +
"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);
} 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);
dump(e + "\n");
}
}
} finally {
statement.finalize();
}
otherContactsTag._initHiddenTags();
},
unInitContacts: function() {
@ -1268,8 +1313,12 @@ ContactsService.prototype = {
"WHERE b.key = :buddyName and a.prpl = :prplId");
statement.params.buddyName = aNormalizedName;
statement.params.prplId = aPrpl.id;
if (!statement.executeStep())
return null;
try {
if (!statement.executeStep())
return null;
} finally {
statement.finalize();
}
return BuddiesById[statement.row.id];
},
@ -1282,12 +1331,16 @@ ContactsService.prototype = {
DBConn.createStatement("INSERT INTO buddies " +
"(key, name, srv_alias, position) " +
"VALUES(:key, :name, :srvAlias, 0)");
let name = aAccountBuddy.userName;
let srvAlias = aAccountBuddy.serverAlias;
statement.params.key = normalizedName;
statement.params.name = name;
statement.params.srvAlias = srvAlias;
statement.execute();
try {
let name = aAccountBuddy.userName;
let srvAlias = aAccountBuddy.serverAlias;
statement.params.key = normalizedName;
statement.params.name = name;
statement.params.srvAlias = srvAlias;
statement.execute();
} finally {
statement.finalize();
}
buddy =
new Buddy(DBConn.lastInsertRowID, normalizedName, name, srvAlias, 0);
}
@ -1310,10 +1363,14 @@ ContactsService.prototype = {
DBConn.createStatement("INSERT INTO account_buddy " +
"(account_id, buddy_id, tag_id) " +
"VALUES(:accountId, :buddyId, :tagId)");
statement.params.accountId = accountId;
statement.params.buddyId = buddy.id;
statement.params.tagId = tagId;
statement.execute();
try {
statement.params.accountId = accountId;
statement.params.buddyId = buddy.id;
statement.params.tagId = tagId;
statement.execute();
} finally {
statement.finalize();
}
// Fire the notifications.
buddy.observe(aAccountBuddy, "account-buddy-added");
@ -1325,10 +1382,14 @@ ContactsService.prototype = {
"WHERE account_id = :accountId AND " +
"buddy_id = :buddyId AND " +
"tag_id = :tagId");
statement.params.accountId = aAccountBuddy.account.numericId;
statement.params.buddyId = buddy.id;
statement.params.tagId = aAccountBuddy.tag.id;
statement.execute();
try {
statement.params.accountId = aAccountBuddy.account.numericId;
statement.params.buddyId = buddy.id;
statement.params.tagId = aAccountBuddy.tag.id;
statement.execute();
} finally {
statement.finalize();
}
buddy.observe(aAccountBuddy, "account-buddy-removed");
},
@ -1341,11 +1402,15 @@ ContactsService.prototype = {
"WHERE account_id = :accountId AND " +
"buddy_id = :buddyId AND " +
"tag_id = :oldTagId");
statement.params.accountId = aAccountBuddy.account.numericId;
statement.params.buddyId = buddy.id;
statement.params.oldTagId = aOldTag.id;
statement.params.newTagId = aNewTag.id;
statement.execute();
try {
statement.params.accountId = aAccountBuddy.account.numericId;
statement.params.buddyId = buddy.id;
statement.params.oldTagId = aOldTag.id;
statement.params.newTagId = aNewTag.id;
statement.execute();
} finally {
statement.finalize();
}
let contact = ContactsById[buddy.contact.id];
@ -1361,39 +1426,59 @@ ContactsService.prototype = {
let statement =
DBConn.createStatement("SELECT name, prpl FROM accounts WHERE id = :id");
statement.params.id = aId;
if (statement.executeStep()) {
if (statement.getUTF8String(0) == aUserName &&
statement.getUTF8String(1) == aPrplId)
return; // The account is already stored correctly.
throw Cr.NS_ERROR_UNEXPECTED; // Corrupted database?!?
try {
if (statement.executeStep()) {
if (statement.getUTF8String(0) == aUserName &&
statement.getUTF8String(1) == aPrplId)
return; // The account is already stored correctly.
throw Cr.NS_ERROR_UNEXPECTED; // Corrupted database?!?
}
} finally {
statement.finalize();
}
// Actually store the account.
statement = DBConn.createStatement("INSERT INTO accounts (id, name, prpl) " +
"VALUES(:id, :userName, :prplId)");
statement.params.id = aId;
statement.params.userName = aUserName;
statement.params.prplId = aPrplId;
statement.execute();
try {
statement.params.id = aId;
statement.params.userName = aUserName;
statement.params.prplId = aPrplId;
statement.execute();
} finally {
statement.finalize();
}
},
accountIdExists: function(aId) {
let statement =
DBConn.createStatement("SELECT id FROM accounts WHERE id = :id");
statement.params.id = aId;
return statement.executeStep();
try {
statement.params.id = aId;
return statement.executeStep();
} finally {
statement.finalize();
}
},
forgetAccount: function(aId) {
let statement =
DBConn.createStatement("DELETE FROM accounts WHERE id = :accountId");
statement.params.accountId = aId;
statement.execute();
try {
statement.params.accountId = aId;
statement.execute();
} finally {
statement.finalize();
}
// removing the account from the accounts table is not enought,
// we need to remove all the associated account_buddy entries too
statement = DBConn.createStatement("DELETE FROM account_buddy " +
"WHERE account_id = :accountId");
statement.params.accountId = aId;
statement.execute();
try {
statement.params.accountId = aId;
statement.execute();
} finally {
statement.finalize();
}
},
QueryInterface: XPCOMUtils.generateQI([Ci.imIContactsService]),