From 545d5bc1624f192582c8f9f23fd6032bfec85b73 Mon Sep 17 00:00:00 2001 From: Bevis Tseng Date: Mon, 28 Apr 2014 12:06:29 +0800 Subject: [PATCH] Bug 824717 - Part 2: Refactor SmsParent::GetMobileMessageDataFromMessage() as a static method to be used in both SmsParent/SmsRequestParent. r=vyang --- dom/mobilemessage/src/ipc/SmsParent.cpp | 118 +++++++++++------------- dom/mobilemessage/src/ipc/SmsParent.h | 3 - 2 files changed, 53 insertions(+), 68 deletions(-) diff --git a/dom/mobilemessage/src/ipc/SmsParent.cpp b/dom/mobilemessage/src/ipc/SmsParent.cpp index 2885e38eb25..a6510699078 100644 --- a/dom/mobilemessage/src/ipc/SmsParent.cpp +++ b/dom/mobilemessage/src/ipc/SmsParent.cpp @@ -126,6 +126,40 @@ GetParamsFromSendMmsMessageRequest(JSContext* aCx, return true; } +static bool +GetMobileMessageDataFromMessage(ContentParent* aParent, + nsISupports *aMsg, + MobileMessageData &aData) +{ + if (!aMsg) { + NS_WARNING("Invalid message to convert!"); + return false; + } + + nsCOMPtr mmsMsg = do_QueryInterface(aMsg); + if (mmsMsg) { + if (!aParent) { + NS_ERROR("Invalid ContentParent to convert MMS Message!"); + return false; + } + MmsMessageData data; + if (!static_cast(mmsMsg.get())->GetData(aParent, data)) { + return false; + } + aData = data; + return true; + } + + nsCOMPtr smsMsg = do_QueryInterface(aMsg); + if (smsMsg) { + aData = static_cast(smsMsg.get())->GetData(); + return true; + } + + NS_WARNING("Cannot get MobileMessageData"); + return false; +} + NS_IMPL_ISUPPORTS(SmsParent, nsIObserver) SmsParent::SmsParent() @@ -172,9 +206,11 @@ NS_IMETHODIMP SmsParent::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData) { + ContentParent *parent = static_cast(Manager()); + if (!strcmp(aTopic, kSmsReceivedObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-received' topic without a valid message!"); return NS_OK; } @@ -185,7 +221,7 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, if (!strcmp(aTopic, kSmsRetrievingObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-retrieving' topic without a valid message!"); return NS_OK; } @@ -196,7 +232,7 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, if (!strcmp(aTopic, kSmsSendingObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-sending' topic without a valid message!"); return NS_OK; } @@ -207,7 +243,7 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, if (!strcmp(aTopic, kSmsSentObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-sent' topic without a valid message!"); return NS_OK; } @@ -218,7 +254,7 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, if (!strcmp(aTopic, kSmsFailedObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-failed' topic without a valid message!"); return NS_OK; } @@ -229,7 +265,7 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, if (!strcmp(aTopic, kSmsDeliverySuccessObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-sending' topic without a valid message!"); return NS_OK; } @@ -240,7 +276,7 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, if (!strcmp(aTopic, kSmsDeliveryErrorObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-delivery-error' topic without a valid message!"); return NS_OK; } @@ -267,10 +303,9 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, return NS_OK; } - if (!strcmp(aTopic, kSmsReadSuccessObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-read-success' topic without a valid message!"); return NS_OK; } @@ -281,7 +316,7 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, if (!strcmp(aTopic, kSmsReadErrorObserverTopic)) { MobileMessageData msgData; - if (!GetMobileMessageDataFromMessage(aSubject, msgData)) { + if (!GetMobileMessageDataFromMessage(parent, aSubject, msgData)) { NS_ERROR("Got a 'sms-read-error' topic without a valid message!"); return NS_OK; } @@ -294,31 +329,6 @@ SmsParent::Observe(nsISupports* aSubject, const char* aTopic, return NS_OK; } -bool -SmsParent::GetMobileMessageDataFromMessage(nsISupports *aMsg, - MobileMessageData &aData) -{ - nsCOMPtr mmsMsg = do_QueryInterface(aMsg); - if (mmsMsg) { - MmsMessageData data; - ContentParent *parent = static_cast(Manager()); - if (!static_cast(mmsMsg.get())->GetData(parent, data)) { - return false; - } - aData = data; - return true; - } - - nsCOMPtr smsMsg = do_QueryInterface(aMsg); - if (smsMsg) { - aData = static_cast(smsMsg.get())->GetData(); - return true; - } - - NS_WARNING("Cannot get MobileMessageData"); - return false; -} - bool SmsParent::RecvAddSilentNumber(const nsString& aNumber) { @@ -619,21 +629,10 @@ SmsRequestParent::NotifyMessageSent(nsISupports *aMessage) { NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); - nsCOMPtr mms = do_QueryInterface(aMessage); - if (mms) { - MmsMessage *msg = static_cast(mms.get()); - ContentParent *parent = static_cast(Manager()->Manager()); - MmsMessageData data; - if (!msg->GetData(parent, data)) { - return NS_ERROR_FAILURE; - } - return SendReply(ReplyMessageSend(MobileMessageData(data))); - } - - nsCOMPtr sms = do_QueryInterface(aMessage); - if (sms) { - SmsMessage* msg = static_cast(sms.get()); - return SendReply(ReplyMessageSend(MobileMessageData(msg->GetData()))); + ContentParent *parent = static_cast(Manager()->Manager()); + MobileMessageData data; + if (GetMobileMessageDataFromMessage(parent, aMessage, data)) { + return SendReply(ReplyMessageSend(data)); } return NS_ERROR_FAILURE; @@ -650,21 +649,10 @@ SmsRequestParent::NotifyMessageGot(nsISupports *aMessage) { NS_ENSURE_TRUE(!mActorDestroyed, NS_ERROR_FAILURE); - nsCOMPtr mms = do_QueryInterface(aMessage); - if (mms) { - MmsMessage *msg = static_cast(mms.get()); - ContentParent *parent = static_cast(Manager()->Manager()); - MmsMessageData data; - if (!msg->GetData(parent, data)) { - return NS_ERROR_FAILURE; - } - return SendReply(ReplyGetMessage(MobileMessageData(data))); - } - - nsCOMPtr sms = do_QueryInterface(aMessage); - if (sms) { - SmsMessage* msg = static_cast(sms.get()); - return SendReply(ReplyGetMessage(MobileMessageData(msg->GetData()))); + ContentParent *parent = static_cast(Manager()->Manager()); + MobileMessageData data; + if (GetMobileMessageDataFromMessage(parent, aMessage, data)) { + return SendReply(ReplyGetMessage(data)); } return NS_ERROR_FAILURE; diff --git a/dom/mobilemessage/src/ipc/SmsParent.h b/dom/mobilemessage/src/ipc/SmsParent.h index e9feff35935..add84dc12ce 100644 --- a/dom/mobilemessage/src/ipc/SmsParent.h +++ b/dom/mobilemessage/src/ipc/SmsParent.h @@ -66,9 +66,6 @@ protected: virtual bool DeallocPMobileMessageCursorParent(PMobileMessageCursorParent* aActor) MOZ_OVERRIDE; - bool - GetMobileMessageDataFromMessage(nsISupports* aMsg, MobileMessageData& aData); - private: nsTArray mSilentNumbers; };