Bug 940884 - 2.b/3. Move init functions out of MobileMessage constructor. r=gene

This commit is contained in:
Vicamo Yang 2013-12-12 11:45:28 +08:00
Родитель 208f20c884
Коммит 695ea4d1d6
2 изменённых файлов: 77 добавлений и 40 удалений

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

@ -78,40 +78,7 @@ XPCOMUtils.defineLazyGetter(this, "MMS", function () {
/**
* MobileMessageDB
*/
this.MobileMessageDB = function(aDbName, aDbVersion) {
this.dbName = aDbName;
this.dbVersion = aDbVersion || DB_VERSION;
let that = this;
this.newTxn(READ_ONLY, function(error, txn, messageStore){
if (error) {
return;
}
// In order to get the highest key value, we open a key cursor in reverse
// order and get only the first pointed value.
let request = messageStore.openCursor(null, PREV);
request.onsuccess = function onsuccess(event) {
let cursor = event.target.result;
if (!cursor) {
if (DEBUG) {
debug("Could not get the last key from mobile message database. " +
"Probably empty database");
}
return;
}
that.lastMessageId = cursor.key || 0;
if (DEBUG) debug("Last assigned message ID was " + that.lastMessageId);
};
request.onerror = function onerror(event) {
if (DEBUG) {
debug("Could not get the last key from mobile message database " +
event.target.errorCode);
}
};
});
this.updatePendingTransactionToError();
};
this.MobileMessageDB = function() {};
MobileMessageDB.prototype = {
dbName: null,
dbVersion: null,
@ -321,16 +288,84 @@ MobileMessageDB.prototype = {
});
},
/**
* Initialize this MobileMessageDB.
*
* @param aDbName
* A string name for that database.
* @param aDbVersion
* The version that mmdb should upgrade to. 0 for the lastest version.
* @param aCallback
* A function when either the initialization transaction is completed
* or any error occurs. Should take only one argument -- null when
* initialized with success or the error object otherwise.
*/
init: function init(aDbName, aDbVersion, aCallback) {
this.dbName = aDbName;
this.dbVersion = aDbVersion || DB_VERSION;
let self = this;
this.newTxn(READ_ONLY, function(error, txn, messageStore){
if (error) {
if (aCallback) {
aCallback(error);
}
return;
}
if (aCallback) {
txn.oncomplete = function() {
aCallback(null);
};
}
// In order to get the highest key value, we open a key cursor in reverse
// order and get only the first pointed value.
let request = messageStore.openCursor(null, PREV);
request.onsuccess = function onsuccess(event) {
let cursor = event.target.result;
if (!cursor) {
if (DEBUG) {
debug("Could not get the last key from mobile message database. " +
"Probably empty database");
}
return;
}
self.lastMessageId = cursor.key || 0;
if (DEBUG) debug("Last assigned message ID was " + self.lastMessageId);
};
request.onerror = function onerror(event) {
if (DEBUG) {
debug("Could not get the last key from mobile message database " +
event.target.errorCode);
}
};
});
},
close: function close() {
if (!this.db) {
return;
}
this.db.close();
this.db = null;
this.lastMessageId = 0;
},
/**
* Sometimes user might reboot or remove battery while sending/receiving
* message. This is function set the status of message records to error.
*/
updatePendingTransactionToError: function updatePendingTransactionToError() {
updatePendingTransactionToError:
function updatePendingTransactionToError(aError) {
if (aError) {
return;
}
this.newTxn(READ_WRITE, function (error, txn, messageStore) {
if (DEBUG) {
txn.onerror = function onerror(event) {
debug("updatePendingTransactionToError fail, event = " + event);
};
if (error) {
return;
}
let deliveryIndex = messageStore.index("delivery");

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

@ -27,7 +27,9 @@ function MobileMessageDatabaseService() {
// by the time IndexedDB queries for it off the main thread. (See bug 743635.)
Services.dirsvc.get("ProfD", Ci.nsIFile);
this.mmdb = new MMDB.MobileMessageDB(DB_NAME);
let mmdb = new MMDB.MobileMessageDB();
mmdb.init(DB_NAME, 0, mmdb.updatePendingTransactionToError.bind(mmdb));
this.mmdb = mmdb;
}
MobileMessageDatabaseService.prototype = {