Bug 855610 - B2G MMS: Scan pending sending/receiving transaction and set the status to fail when b2g bootup. r=vyang

This commit is contained in:
Chia-hung Tai 2013-06-06 09:48:10 +08:00
parent 82686326ce
commit c4db695298

View File

@ -31,6 +31,7 @@ const DELIVERY_SENDING = "sending";
const DELIVERY_SENT = "sent";
const DELIVERY_RECEIVED = "received";
const DELIVERY_NOT_DOWNLOADED = "not-downloaded";
const DELIVERY_ERROR = "error";
const DELIVERY_STATUS_NOT_APPLICABLE = "not-applicable";
const DELIVERY_STATUS_SUCCESS = "success";
@ -105,6 +106,7 @@ function MobileMessageDatabaseService() {
}
};
});
this.updatePendingTransactionToError();
}
MobileMessageDatabaseService.prototype = {
@ -279,6 +281,79 @@ MobileMessageDatabaseService.prototype = {
});
},
/**
* 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() {
this.newTxn(READ_WRITE, function (error, txn, messageStore) {
if (DEBUG) {
txn.onerror = function onerror(event) {
debug("updatePendingTransactionToError fail, event = " + event);
};
}
let deliveryIndex = messageStore.index("delivery");
// Set all 'delivery: sending' records to 'delivery: error' and 'deliveryStatus:
// error'.
let keyRange = IDBKeyRange.bound([DELIVERY_SENDING, 0], [DELIVERY_SENDING, ""]);
let cursorRequestSending = deliveryIndex.openCursor(keyRange);
cursorRequestSending.onsuccess = function(event) {
let messageCursor = event.target.result;
if (!messageCursor) {
return;
}
let messageRecord = messageCursor.value;
// Set delivery to error.
messageRecord.delivery = DELIVERY_ERROR;
messageRecord.deliveryIndex = [DELIVERY_ERROR, messageRecord.timestamp];
if (messageRecord.type == "sms") {
messageRecord.deliveryStatus = DELIVERY_STATUS_ERROR;
} else {
// Set delivery status to error.
for (let i = 0; i < messageRecord.deliveryStatus.length; i++) {
messageRecord.deliveryStatus[i] = DELIVERY_STATUS_ERROR;
}
}
messageCursor.update(messageRecord);
messageCursor.continue();
};
// Set all 'delivery: not-downloaded' and 'deliveryStatus: pending'
// records to 'delivery: not-downloaded' and 'deliveryStatus: error'.
keyRange = IDBKeyRange.bound([DELIVERY_NOT_DOWNLOADED, 0], [DELIVERY_NOT_DOWNLOADED, ""]);
let cursorRequestNotDownloaded = deliveryIndex.openCursor(keyRange);
cursorRequestNotDownloaded.onsuccess = function(event) {
let messageCursor = event.target.result;
if (!messageCursor) {
return;
}
let messageRecord = messageCursor.value;
// We have no "not-downloaded" SMS messages.
if (messageRecord.type == "sms") {
messageCursor.continue();
return;
}
// Set delivery status to error.
if (messageRecord.deliveryStatus.length == 1 &&
messageRecord.deliveryStatus[0] == DELIVERY_STATUS_PENDING) {
messageRecord.deliveryStatus = [DELIVERY_STATUS_ERROR];
}
messageCursor.update(messageRecord);
messageCursor.continue();
};
});
},
/**
* Create the initial database schema.
*