Bug 847744: Part 2 - Use sms-* topic to notify mms observers. r=vicamo, a=leo+
authorVicamo Yang <vyang@mozilla.com>
Thu, 18 Apr 2013 09:09:50 +0800
changeset 119106 23fdaa699d6ce33c937e249d1e43805ced90ee2e
parent 119105 90f543e4a4e030eb22227a80b481398870b2b5f3
child 119107 898d1ece6313bf26076a5bf8d7444bafffdb48e0
push id680
push uservyang@mozilla.com
push dateThu, 18 Apr 2013 01:11:41 +0000
reviewersvicamo, leo
bugs847744
milestone18.0
Bug 847744: Part 2 - Use sms-* topic to notify mms observers. r=vicamo, a=leo+
dom/mms/src/ril/MmsService.js
dom/mobilemessage/src/Constants.cpp
dom/mobilemessage/src/MobileMessageManager.cpp
dom/mobilemessage/src/MobileMessageManager.h
--- a/dom/mms/src/ril/MmsService.js
+++ b/dom/mms/src/ril/MmsService.js
@@ -13,20 +13,20 @@ Cu.import("resource://gre/modules/Servic
 
 Cu.import("resource://gre/modules/NetUtil.jsm");
 
 const RIL_MMSSERVICE_CONTRACTID = "@mozilla.org/mms/rilmmsservice;1";
 const RIL_MMSSERVICE_CID = Components.ID("{217ddd76-75db-4210-955d-8806cd8d87f9}");
 
 const DEBUG = false;
 
-const kMmsSendingObserverTopic           = "mms-sending";
-const kMmsSentObserverTopic              = "mms-sent";
-const kMmsFailedObserverTopic            = "mms-failed";
-const kMmsReceivedObserverTopic          = "mms-received";
+const kSmsSendingObserverTopic           = "sms-sending";
+const kSmsSentObserverTopic              = "sms-sent";
+const kSmsFailedObserverTopic            = "sms-failed";
+const kSmsReceivedObserverTopic          = "sms-received";
 
 const kNetworkInterfaceStateChangedTopic = "network-interface-state-changed";
 const kXpcomShutdownObserverTopic        = "xpcom-shutdown";
 const kPrefenceChangedObserverTopic      = "nsPref:changed";
 
 // HTTP status codes:
 // @see http://tools.ietf.org/html/rfc2616#page-39
 const HTTP_STATUS_OK = 200;
@@ -1079,17 +1079,17 @@ MmsService.prototype = {
         // for the resent notification indication.
         return;
       }
 
       // Broadcasting an 'sms-received' system message to open apps.
       this.broadcastMmsSystemMessage("sms-received", domMessage);
 
       // Notifying observers a new notification indication is coming.
-      Services.obs.notifyObservers(domMessage, kMmsReceivedObserverTopic, null);
+      Services.obs.notifyObservers(domMessage, kSmsReceivedObserverTopic, null);
 
       let retrievalMode = RETRIEVAL_MODE_MANUAL;
       try {
         retrievalMode = Services.prefs.getCharPref(PREF_RETRIEVAL_MODE);
       } catch (e) {}
 
       let isRoaming = gMmsConnection.isDataConnRoaming();
       if ((retrievalMode === RETRIEVAL_MODE_AUTOMATIC_HOME && isRoaming) ||
@@ -1159,17 +1159,17 @@ MmsService.prototype = {
                   ", error code " + rv);
             return;
           }
 
           // Broadcasting an 'sms-received' system message to open apps.
           this.broadcastMmsSystemMessage("sms-received", domMessage);
 
           // Notifying observers an MMS message is received.
-          Services.obs.notifyObservers(domMessage, kMmsReceivedObserverTopic, null);
+          Services.obs.notifyObservers(domMessage, kSmsReceivedObserverTopic, null);
         }).bind(this));
       }).bind(this));
     }).bind(this));
   },
 
   /**
    * Handle incoming M-Delivery.ind PDU.
    *
@@ -1299,33 +1299,33 @@ MmsService.prototype = {
                             null,
                             aIsSentSuccess ? DELIVERY_SENT : DELIVERY_ERROR,
                             aIsSentSuccess ? null : DELIVERY_STATUS_ERROR,
                             function notifySetDeliveryResult(aRv, aDomMessage) {
         debug("Marking the delivery state/staus is done. Notify sent or failed.");
         // TODO bug 832140 handle !Components.isSuccessCode(aRv)
         if (!aIsSentSuccess) {
           aRequest.notifySendMessageFailed(Ci.nsIMobileMessageCallback.INTERNAL_ERROR);
-          Services.obs.notifyObservers(aDomMessage, kMmsFailedObserverTopic, null);
+          Services.obs.notifyObservers(aDomMessage, kSmsFailedObserverTopic, null);
           return;
         }
 
         self.broadcastMmsSystemMessage("sms-sent", aDomMessage);
         aRequest.notifyMessageSent(aDomMessage);
-        Services.obs.notifyObservers(aDomMessage, kMmsSentObserverTopic, null);
+        Services.obs.notifyObservers(aDomMessage, kSmsSentObserverTopic, null);
       });
     };
 
     let savableMessage = this.createSavableFromParams(aParams);
     gMobileMessageDatabaseService
       .saveSendingMessage(savableMessage,
                           function notifySendingResult(aRv, aDomMessage) {
       debug("Saving sending message is done. Start to send.");
       // TODO bug 832140 handle !Components.isSuccessCode(aRv)
-      Services.obs.notifyObservers(aDomMessage, kMmsSendingObserverTopic, null);
+      Services.obs.notifyObservers(aDomMessage, kSmsSendingObserverTopic, null);
       let sendTransaction;
       try {
         sendTransaction = new SendTransaction(savableMessage);
       } catch (e) {
         debug("Exception: fail to create a SendTransaction instance.");
         sendTransactionCb(aDomMessage.id, false);
         return;
       }
@@ -1409,17 +1409,17 @@ MmsService.prototype = {
                   ", error code " + rv);
             aRequest.notifyGetMessageFailed(Ci.nsIMobileMessageCallback.INTERNAL_ERROR);
             return;
           }
           // Notifying observers a new MMS message is retrieved.
           aRequest.notifyMessageGot(domMessage);
           // Broadcasting an 'sms-received' system message to open apps.
           this.broadcastMmsSystemMessage("sms-received", domMessage);
-          Services.obs.notifyObservers(domMessage, kMmsReceivedObserverTopic, null);
+          Services.obs.notifyObservers(domMessage, kSmsReceivedObserverTopic, null);
           let transaction = new AcknowledgeTransaction(transactionId, reportAllowed);
           transaction.run();
         }).bind(this));
       }).bind(this));
     }).bind(this));
   },
 
   // nsIWapPushApplication
--- a/dom/mobilemessage/src/Constants.cpp
+++ b/dom/mobilemessage/src/Constants.cpp
@@ -9,16 +9,11 @@ namespace mobilemessage {
 
 const char* kSmsReceivedObserverTopic        = "sms-received";
 const char* kSmsSendingObserverTopic         = "sms-sending";
 const char* kSmsSentObserverTopic            = "sms-sent";
 const char* kSmsFailedObserverTopic          = "sms-failed";
 const char* kSmsDeliverySuccessObserverTopic = "sms-delivery-success";
 const char* kSmsDeliveryErrorObserverTopic   = "sms-delivery-error";
 
-const char* kMmsSendingObserverTopic         = "mms-sending";
-const char* kMmsSentObserverTopic            = "mms-sent";
-const char* kMmsFailedObserverTopic          = "mms-failed";
-const char* kMmsReceivedObserverTopic        = "mms-received";
-
 } // namespace mobilemessage
 } // namespace dom
 } // namespace mozilla
--- a/dom/mobilemessage/src/MobileMessageManager.cpp
+++ b/dom/mobilemessage/src/MobileMessageManager.cpp
@@ -81,21 +81,16 @@ MobileMessageManager::Init(nsPIDOMWindow
   }
 
   obs->AddObserver(this, kSmsReceivedObserverTopic, false);
   obs->AddObserver(this, kSmsSendingObserverTopic, false);
   obs->AddObserver(this, kSmsSentObserverTopic, false);
   obs->AddObserver(this, kSmsFailedObserverTopic, false);
   obs->AddObserver(this, kSmsDeliverySuccessObserverTopic, false);
   obs->AddObserver(this, kSmsDeliveryErrorObserverTopic, false);
-
-  obs->AddObserver(this, kMmsSendingObserverTopic, false);
-  obs->AddObserver(this, kMmsSentObserverTopic, false);
-  obs->AddObserver(this, kMmsFailedObserverTopic, false);
-  obs->AddObserver(this, kMmsReceivedObserverTopic, false);
 }
 
 void
 MobileMessageManager::Shutdown()
 {
   nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
   // GetObserverService() can return null is some situations like shutdown.
   if (!obs) {
@@ -103,21 +98,16 @@ MobileMessageManager::Shutdown()
   }
 
   obs->RemoveObserver(this, kSmsReceivedObserverTopic);
   obs->RemoveObserver(this, kSmsSendingObserverTopic);
   obs->RemoveObserver(this, kSmsSentObserverTopic);
   obs->RemoveObserver(this, kSmsFailedObserverTopic);
   obs->RemoveObserver(this, kSmsDeliverySuccessObserverTopic);
   obs->RemoveObserver(this, kSmsDeliveryErrorObserverTopic);
-
-  obs->RemoveObserver(this, kMmsSendingObserverTopic);
-  obs->RemoveObserver(this, kMmsSentObserverTopic);
-  obs->RemoveObserver(this, kMmsFailedObserverTopic);
-  obs->RemoveObserver(this, kMmsReceivedObserverTopic);
 }
 
 NS_IMETHODIMP
 MobileMessageManager::GetSegmentInfoForText(const nsAString& aText,
                                             nsIDOMMozSmsSegmentInfo** aResult)
 {
   nsCOMPtr<nsISmsService> smsService = do_GetService(SMS_SERVICE_CONTRACTID);
   NS_ENSURE_TRUE(smsService, NS_ERROR_FAILURE);
@@ -362,153 +352,74 @@ MobileMessageManager::RetrieveMMS(int32_
     nsresult rv = mmsService->Retrieve(id, msgCallback);
     NS_ENSURE_SUCCESS(rv, rv);
 
     request.forget(aRequest);
     return NS_OK;
 }
 
 nsresult
-MobileMessageManager::DispatchTrustedSmsEventToSelf(const nsAString& aEventName,
-                                                    nsIDOMMozSmsMessage* aMessage)
+MobileMessageManager::DispatchTrustedSmsEventToSelf(const char* aTopic,
+                                                    const nsAString& aEventName,
+                                                    nsISupports* aMsg)
 {
-  nsRefPtr<nsDOMEvent> event = new SmsEvent(nullptr, nullptr);
-  nsresult rv = static_cast<SmsEvent*>(event.get())->Init(aEventName, false,
-                                                          false, aMessage);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return DispatchTrustedEvent(event);
-}
+  nsCOMPtr<nsIDOMMozSmsMessage> sms = do_QueryInterface(aMsg);
+  if (sms) {
+    nsRefPtr<nsDOMEvent> event = new SmsEvent(nullptr, nullptr);
+    nsresult rv = static_cast<SmsEvent*>(event.get())->Init(aEventName, false,
+                                                            false, sms);
+    NS_ENSURE_SUCCESS(rv, rv);
+    return DispatchTrustedEvent(event);
+  }
 
-nsresult
-MobileMessageManager::DispatchTrustedMmsEventToSelf(const nsAString& aEventName,
-                                                    nsIDOMMozMmsMessage* aMessage)
-{
-  nsCOMPtr<nsIDOMEvent> event;
-  NS_NewDOMMozMmsEvent(getter_AddRefs(event), nullptr, nullptr);
-  NS_ASSERTION(event, "This should never fail!");
+  nsCOMPtr<nsIDOMMozMmsMessage> mms = do_QueryInterface(aMsg);
+  if (mms) {
+    nsCOMPtr<nsIDOMEvent> event;
+    NS_NewDOMMozMmsEvent(getter_AddRefs(event), nullptr, nullptr);
+    NS_ASSERTION(event, "This should never fail!");
 
-  nsCOMPtr<nsIDOMMozMmsEvent> se = do_QueryInterface(event);
-  nsresult rv = se->InitMozMmsEvent(aEventName, false, false, aMessage);
-  NS_ENSURE_SUCCESS(rv, rv);
+    nsCOMPtr<nsIDOMMozMmsEvent> se = do_QueryInterface(event);
+    nsresult rv = se->InitMozMmsEvent(aEventName, false, false, mms);
+    NS_ENSURE_SUCCESS(rv, rv);
+    return DispatchTrustedEvent(event);
+  }
 
-  return DispatchTrustedEvent(event);
+  nsAutoCString errorMsg;
+  errorMsg.AssignLiteral("Got a '");
+  errorMsg.Append(aTopic);
+  errorMsg.AppendLiteral("' topic without a valid message!");
+  NS_ERROR(errorMsg.get());
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 MobileMessageManager::Observe(nsISupports* aSubject, const char* aTopic,
                               const PRUnichar* aData)
 {
   if (!strcmp(aTopic, kSmsReceivedObserverTopic)) {
-    nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'sms-received' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedSmsEventToSelf(RECEIVED_EVENT_NAME, message);
-    return NS_OK;
+    return DispatchTrustedSmsEventToSelf(aTopic, RECEIVED_EVENT_NAME, aSubject);
   }
 
   if (!strcmp(aTopic, kSmsSendingObserverTopic)) {
-    nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'sms-sending' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedSmsEventToSelf(SENDING_EVENT_NAME, message);
-    return NS_OK;
+    return DispatchTrustedSmsEventToSelf(aTopic, SENDING_EVENT_NAME, aSubject);
   }
 
   if (!strcmp(aTopic, kSmsSentObserverTopic)) {
-    nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'sms-sent' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedSmsEventToSelf(SENT_EVENT_NAME, message);
-    return NS_OK;
+    return DispatchTrustedSmsEventToSelf(aTopic, SENT_EVENT_NAME, aSubject);
   }
 
   if (!strcmp(aTopic, kSmsFailedObserverTopic)) {
-    nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'sms-failed' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedSmsEventToSelf(FAILED_EVENT_NAME, message);
-    return NS_OK;
+    return DispatchTrustedSmsEventToSelf(aTopic, FAILED_EVENT_NAME, aSubject);
   }
 
   if (!strcmp(aTopic, kSmsDeliverySuccessObserverTopic)) {
-    nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'sms-delivery-success' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedSmsEventToSelf(DELIVERY_SUCCESS_EVENT_NAME, message);
-    return NS_OK;
+    return DispatchTrustedSmsEventToSelf(aTopic, DELIVERY_SUCCESS_EVENT_NAME, aSubject);
   }
 
   if (!strcmp(aTopic, kSmsDeliveryErrorObserverTopic)) {
-    nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'sms-delivery-error' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedSmsEventToSelf(DELIVERY_ERROR_EVENT_NAME, message);
-    return NS_OK;
-  }
-
-  if (!strcmp(aTopic, kMmsSendingObserverTopic)) {
-    nsCOMPtr<nsIDOMMozMmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'mms-sending' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedMmsEventToSelf(SENDING_EVENT_NAME, message);
-    return NS_OK;
-  }
-
-  if (!strcmp(aTopic, kMmsSentObserverTopic)) {
-    nsCOMPtr<nsIDOMMozMmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'mms-sent' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedMmsEventToSelf(SENT_EVENT_NAME, message);
-    return NS_OK;
-  }
-
-  if (!strcmp(aTopic, kMmsFailedObserverTopic)) {
-    nsCOMPtr<nsIDOMMozMmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'mms-failed' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedMmsEventToSelf(FAILED_EVENT_NAME, message);
-    return NS_OK;
-  }
-
-  if (!strcmp(aTopic, kMmsReceivedObserverTopic)) {
-    nsCOMPtr<nsIDOMMozMmsMessage> message = do_QueryInterface(aSubject);
-    if (!message) {
-      NS_ERROR("Got a 'mms-received' topic without a valid message!");
-      return NS_OK;
-    }
-
-    DispatchTrustedMmsEventToSelf(RECEIVED_EVENT_NAME, message);
-    return NS_OK;
+    return DispatchTrustedSmsEventToSelf(aTopic, DELIVERY_ERROR_EVENT_NAME, aSubject);
   }
 
   return NS_OK;
 }
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/mobilemessage/src/MobileMessageManager.h
+++ b/dom/mobilemessage/src/MobileMessageManager.h
@@ -40,19 +40,17 @@ private:
   nsresult Send(JSContext* aCx, JSObject* aGlobal, JSString* aNumber,
                 const nsAString& aMessage, jsval* aRequest);
 
   /**
    * Internal Delete() method used to delete a message.
    */
   nsresult Delete(int32_t aId, nsIDOMDOMRequest** aRequest);
 
-  nsresult DispatchTrustedSmsEventToSelf(const nsAString& aEventName,
-                                         nsIDOMMozSmsMessage* aMessage);
-
-  nsresult DispatchTrustedMmsEventToSelf(const nsAString& aEventName,
-                                         nsIDOMMozMmsMessage* aMessage);
+  nsresult DispatchTrustedSmsEventToSelf(const char* aTopic,
+                                         const nsAString& aEventName,
+                                         nsISupports* aMsg);
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_mobilemessage_MobileMessageManager_h