From eca0fb63cb4f6e33ed813ff5c078fdb020848a4f Mon Sep 17 00:00:00 2001 From: Chia-hung Tai Date: Fri, 26 Apr 2013 16:40:42 +0800 Subject: [PATCH] Bug 863241 - Part 2: B2G MMS: the return items from getThreads should have type to identify mms or sms. r=vyang --- dom/mobilemessage/src/Constants.h | 3 + .../src/MobileMessageService.cpp | 2 + dom/mobilemessage/src/MobileMessageThread.cpp | 40 ++++++++++++- dom/mobilemessage/src/MobileMessageThread.h | 4 +- dom/mobilemessage/src/Types.h | 18 ++++++ .../src/ril/MobileMessageDatabaseService.js | 56 ++++++++++++++++++- 6 files changed, 117 insertions(+), 6 deletions(-) diff --git a/dom/mobilemessage/src/Constants.h b/dom/mobilemessage/src/Constants.h index b675dd4242f..e6878dd8d8e 100644 --- a/dom/mobilemessage/src/Constants.h +++ b/dom/mobilemessage/src/Constants.h @@ -40,6 +40,9 @@ extern const char* kMmsReceivedObserverTopic; #define MESSAGE_CLASS_CLASS_2 NS_LITERAL_STRING("class-2") #define MESSAGE_CLASS_CLASS_3 NS_LITERAL_STRING("class-3") +#define MESSAGE_TYPE_SMS NS_LITERAL_STRING("sms") +#define MESSAGE_TYPE_MMS NS_LITERAL_STRING("mms") + } // namespace mobilemessage } // namespace dom } // namespace mozilla diff --git a/dom/mobilemessage/src/MobileMessageService.cpp b/dom/mobilemessage/src/MobileMessageService.cpp index f5c7d29151c..be591161bfc 100644 --- a/dom/mobilemessage/src/MobileMessageService.cpp +++ b/dom/mobilemessage/src/MobileMessageService.cpp @@ -107,6 +107,7 @@ MobileMessageService::CreateThread(uint64_t aId, const JS::Value& aTimestamp, const nsAString& aBody, uint64_t aUnreadCount, + const nsAString& aLastMessageType, JSContext* aCx, nsIDOMMozMobileMessageThread** aThread) { @@ -115,6 +116,7 @@ MobileMessageService::CreateThread(uint64_t aId, aTimestamp, aBody, aUnreadCount, + aLastMessageType, aCx, aThread); } diff --git a/dom/mobilemessage/src/MobileMessageThread.cpp b/dom/mobilemessage/src/MobileMessageThread.cpp index 7fdf72dafbf..f0a679a453d 100644 --- a/dom/mobilemessage/src/MobileMessageThread.cpp +++ b/dom/mobilemessage/src/MobileMessageThread.cpp @@ -10,6 +10,8 @@ #include "nsJSUtils.h" // For nsDependentJSString #include "nsContentUtils.h" // For nsTArrayHelpers.h #include "nsTArrayHelpers.h" // For nsTArrayToJSArray +#include "Constants.h" // For MessageType + using namespace mozilla::dom::mobilemessage; @@ -33,6 +35,7 @@ MobileMessageThread::Create(const uint64_t aId, const JS::Value& aTimestamp, const nsAString& aBody, const uint64_t aUnreadCount, + const nsAString& aLastMessageType, JSContext* aCx, nsIDOMMozMobileMessageThread** aThread) { @@ -91,6 +94,19 @@ MobileMessageThread::Create(const uint64_t aId, data.timestamp() = static_cast(number); } + // Set |aLastMessageType|. + { + MessageType lastMessageType; + if (aLastMessageType.Equals(MESSAGE_TYPE_SMS)) { + lastMessageType = eMessageType_SMS; + } else if (aLastMessageType.Equals(MESSAGE_TYPE_MMS)) { + lastMessageType = eMessageType_MMS; + } else { + return NS_ERROR_INVALID_ARG; + } + data.lastMessageType() = lastMessageType; + } + nsCOMPtr thread = new MobileMessageThread(data); thread.forget(aThread); return NS_OK; @@ -100,8 +116,9 @@ MobileMessageThread::MobileMessageThread(const uint64_t aId, const nsTArray& aParticipants, const uint64_t aTimestamp, const nsString& aBody, - const uint64_t aUnreadCount) - : mData(aId, aParticipants, aTimestamp, aBody, aUnreadCount) + const uint64_t aUnreadCount, + MessageType aLastMessageType) + : mData(aId, aParticipants, aTimestamp, aBody, aUnreadCount, aLastMessageType) { MOZ_ASSERT(aParticipants.Length()); } @@ -157,5 +174,24 @@ MobileMessageThread::GetTimestamp(JSContext* aCx, return NS_OK; } +NS_IMETHODIMP +MobileMessageThread::GetLastMessageType(nsAString& aLastMessageType) +{ + switch (mData.lastMessageType()) { + case eMessageType_SMS: + aLastMessageType = MESSAGE_TYPE_SMS; + break; + case eMessageType_MMS: + aLastMessageType = MESSAGE_TYPE_MMS; + break; + case eMessageType_EndGuard: + default: + MOZ_NOT_REACHED("We shouldn't get any other delivery state!"); + return NS_ERROR_UNEXPECTED; + } + + return NS_OK; +} + } // namespace dom } // namespace mozilla diff --git a/dom/mobilemessage/src/MobileMessageThread.h b/dom/mobilemessage/src/MobileMessageThread.h index 7aa915abc6c..56df350ded5 100644 --- a/dom/mobilemessage/src/MobileMessageThread.h +++ b/dom/mobilemessage/src/MobileMessageThread.h @@ -28,7 +28,8 @@ public: const nsTArray& aParticipants, const uint64_t aTimestamp, const nsString& aBody, - const uint64_t aUnreadCount); + const uint64_t aUnreadCount, + mobilemessage::MessageType aLastMessageType); MobileMessageThread(const ThreadData& aData); @@ -37,6 +38,7 @@ public: const JS::Value& aTimestamp, const nsAString& aBody, const uint64_t aUnreadCount, + const nsAString& aLastMessageType, JSContext* aCx, nsIDOMMozMobileMessageThread** aThread); diff --git a/dom/mobilemessage/src/Types.h b/dom/mobilemessage/src/Types.h index c28c8737778..1b908cc6c35 100644 --- a/dom/mobilemessage/src/Types.h +++ b/dom/mobilemessage/src/Types.h @@ -56,6 +56,14 @@ enum MessageClass { eMessageClass_EndGuard }; +// For ThreadData. +enum MessageType { + eMessageType_SMS = 0, + eMessageType_MMS, + // This state should stay at the end. + eMessageType_EndGuard +}; + } // namespace mobilemessage } // namespace dom } // namespace mozilla @@ -102,6 +110,16 @@ struct ParamTraits mozilla::dom::mobilemessage::eMessageClass_EndGuard> {}; +/** + * MessageType class serializer. + */ +template <> +struct ParamTraits + : public EnumSerializer +{}; + } // namespace IPC #endif // mozilla_dom_mobilemessage_Types_h diff --git a/dom/mobilemessage/src/ril/MobileMessageDatabaseService.js b/dom/mobilemessage/src/ril/MobileMessageDatabaseService.js index a5e15f8f941..e91cdc45b7a 100644 --- a/dom/mobilemessage/src/ril/MobileMessageDatabaseService.js +++ b/dom/mobilemessage/src/ril/MobileMessageDatabaseService.js @@ -21,7 +21,7 @@ const RIL_GETTHREADSCURSOR_CID = const DEBUG = false; const DB_NAME = "sms"; -const DB_VERSION = 10; +const DB_VERSION = 11; const MESSAGE_STORE_NAME = "sms"; const THREAD_STORE_NAME = "thread"; const PARTICIPANT_STORE_NAME = "participant"; @@ -209,6 +209,10 @@ MobileMessageDatabaseService.prototype = { if (DEBUG) debug("Upgrade to version 10. Upgrade type if it's not existing."); self.upgradeSchema9(event.target.transaction); break; + case 10: + if (DEBUG) debug("Upgrade to version 11. Add last message type into threadRecord."); + self.upgradeSchema10(event.target.transaction); + break; default: event.target.transaction.abort(); callback("Old database version: " + event.oldVersion, null); @@ -620,6 +624,49 @@ MobileMessageDatabaseService.prototype = { }; }, + upgradeSchema10: function upgradeSchema10(transaction) { + let threadStore = transaction.objectStore(THREAD_STORE_NAME); + + // Add 'lastMessageType' to each thread record. + threadStore.openCursor().onsuccess = function(event) { + let cursor = event.target.result; + if (!cursor) { + return; + } + + let threadRecord = cursor.value; + let lastMessageId = threadRecord.lastMessageId; + let messageStore = transaction.objectStore(MESSAGE_STORE_NAME); + let request = messageStore.mozGetAll(lastMessageId); + + request.onsuccess = function onsuccess() { + let messageRecord = request.result[0]; + if (!messageRecord) { + if (DEBUG) debug("Message ID " + lastMessageId + " not found"); + return; + } + if (messageRecord.id != lastMessageId) { + if (DEBUG) { + debug("Requested message ID (" + lastMessageId + ") is different from" + + " the one we got"); + } + return; + } + threadRecord.lastMessageType = messageRecord.type; + cursor.update(threadRecord); + }; + + request.onerror = function onerror(event) { + if (DEBUG) { + if (event.target) { + debug("Caught error on transaction", event.target.errorCode); + } + } + }; + cursor.continue(); + }; + }, + createDomMessageFromRecord: function createDomMessageFromRecord(aMessageRecord) { if (DEBUG) { debug("createDomMessageFromRecord: " + JSON.stringify(aMessageRecord)); @@ -941,6 +988,7 @@ MobileMessageDatabaseService.prototype = { threadRecord.lastTimestamp = timestamp; threadRecord.subject = aMessageRecord.body; threadRecord.lastMessageId = aMessageRecord.id; + threadRecord.lastMessageType = aMessageRecord.type; needsUpdate = true; } @@ -962,7 +1010,8 @@ MobileMessageDatabaseService.prototype = { lastMessageId: aMessageRecord.id, lastTimestamp: timestamp, subject: aMessageRecord.body, - unreadCount: aMessageRecord.read ? 0 : 1}) + unreadCount: aMessageRecord.read ? 0 : 1, + lastMessageType: aMessageRecord.type}) .onsuccess = function (event) { let threadId = event.target.result; insertMessageRecord(threadId); @@ -2140,7 +2189,8 @@ GetThreadsCursor.prototype = { threadRecord.participantAddresses, threadRecord.lastTimestamp, threadRecord.subject, - threadRecord.unreadCount); + threadRecord.unreadCount, + threadRecord.lastMessageType); self.callback.notifyCursorResult(thread); }; getRequest.onerror = function onerror(event) {