Bug 674725 - Part AO - Show the first message of message list in SmsCursor. r=smaug,cjones
authorMounir Lamouri <mounir.lamouri@gmail.com>
Mon, 19 Dec 2011 12:07:03 +0100
changeset 85951 731e1914b5985fdc912c36a0771a3f5ac3056e68
parent 85950 358b9db3d4868e03cc11ae39f04a2d89388416ef
child 85952 afca8fef2e4e15e6ccc7703326e8d093735dc425
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, cjones
bugs674725
milestone12.0a1
Bug 674725 - Part AO - Show the first message of message list in SmsCursor. r=smaug,cjones
dom/sms/src/SmsCursor.cpp
dom/sms/src/SmsCursor.h
dom/sms/src/SmsRequestManager.cpp
dom/sms/src/SmsRequestManager.h
dom/sms/src/ipc/PSms.ipdl
dom/sms/src/ipc/SmsChild.cpp
dom/sms/src/ipc/SmsChild.h
widget/android/AndroidJNI.cpp
--- a/dom/sms/src/SmsCursor.cpp
+++ b/dom/sms/src/SmsCursor.cpp
@@ -35,51 +35,57 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "SmsCursor.h"
 #include "nsIDOMClassInfo.h"
 #include "nsDOMError.h"
 #include "nsIDOMSmsFilter.h"
 #include "nsIDOMSmsMessage.h"
+#include "nsIDOMSmsRequest.h"
 
 DOMCI_DATA(MozSmsCursor, mozilla::dom::sms::SmsCursor)
 
 namespace mozilla {
 namespace dom {
 namespace sms {
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SmsCursor)
   NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsCursor)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsCursor)
 NS_INTERFACE_MAP_END
 
-NS_IMPL_CYCLE_COLLECTION_2(SmsCursor, mFilter, mMessage)
+NS_IMPL_CYCLE_COLLECTION_3(SmsCursor, mFilter, mRequest, mMessage)
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(SmsCursor)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(SmsCursor)
 
 SmsCursor::SmsCursor(nsIDOMMozSmsFilter* aFilter)
   : mFilter(aFilter)
 {
 }
 
+SmsCursor::SmsCursor(nsIDOMMozSmsFilter* aFilter, nsIDOMMozSmsRequest* aRequest)
+  : mFilter(aFilter)
+  , mRequest(aRequest)
+{
+}
+
 NS_IMETHODIMP
 SmsCursor::GetFilter(nsIDOMMozSmsFilter** aFilter)
 {
   NS_ADDREF(*aFilter = mFilter);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 SmsCursor::GetMessage(nsIDOMMozSmsMessage** aMessage)
 {
-  // TODO: implement
-  *aMessage = nsnull;
+  NS_IF_ADDREF(*aMessage = mMessage);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 SmsCursor::Continue()
 {
   // No message means we are waiting for a message or we got the last one.
   if (!mMessage) {
--- a/dom/sms/src/SmsCursor.h
+++ b/dom/sms/src/SmsCursor.h
@@ -39,33 +39,44 @@
 #define mozilla_dom_sms_SmsCursor_h
 
 #include "nsIDOMSmsCursor.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsCOMPtr.h"
 
 class nsIDOMMozSmsFilter;
 class nsIDOMMozSmsMessage;
+class nsIDOMMozSmsRequest;
 
 namespace mozilla {
 namespace dom {
 namespace sms {
 
 class SmsCursor : public nsIDOMMozSmsCursor
 {
 public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_NSIDOMMOZSMSCURSOR
 
   NS_DECL_CYCLE_COLLECTION_CLASS(SmsCursor)
 
   SmsCursor(nsIDOMMozSmsFilter* aFilter);
+  SmsCursor(nsIDOMMozSmsFilter* aFilter, nsIDOMMozSmsRequest* aRequest);
+
+  void SetMessage(nsIDOMMozSmsMessage* aMessage);
 
 private:
   nsCOMPtr<nsIDOMMozSmsFilter>  mFilter;
+  nsCOMPtr<nsIDOMMozSmsRequest> mRequest;
   nsCOMPtr<nsIDOMMozSmsMessage> mMessage;
 };
 
+inline void
+SmsCursor::SetMessage(nsIDOMMozSmsMessage* aMessage)
+{
+  mMessage = aMessage;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_sms_SmsCursor_h
--- a/dom/sms/src/SmsRequestManager.cpp
+++ b/dom/sms/src/SmsRequestManager.cpp
@@ -112,42 +112,43 @@ SmsRequestManager::DispatchTrustedEventT
 
   rv = event->SetTrusted(PR_TRUE);
   NS_ENSURE_SUCCESS(rv, rv);
 
   bool dummy;
   return aRequest->DispatchEvent(event, &dummy);
 }
 
-template <class T>
-void
-SmsRequestManager::NotifySuccess(PRInt32 aRequestId, T aParam)
+SmsRequest*
+SmsRequestManager::GetRequest(PRInt32 aRequestId)
 {
   NS_ASSERTION(mRequests.Count() > aRequestId && mRequests[aRequestId],
                "Got an invalid request id or it has been already deleted!");
 
   // It's safe to use the static_cast here given that we did call
   // |new SmsRequest()|.
-  SmsRequest* request = static_cast<SmsRequest*>(mRequests[aRequestId]);
+  return static_cast<SmsRequest*>(mRequests[aRequestId]);
+}
+
+template <class T>
+void
+SmsRequestManager::NotifySuccess(PRInt32 aRequestId, T aParam)
+{
+  SmsRequest* request = GetRequest(aRequestId);
   request->SetSuccess(aParam);
 
   DispatchTrustedEventToRequest(SUCCESS_EVENT_NAME, request);
 
   mRequests.ReplaceObjectAt(nsnull, aRequestId);
 }
 
 void
 SmsRequestManager::NotifyError(PRInt32 aRequestId, SmsRequest::ErrorType aError)
 {
-  NS_ASSERTION(mRequests.Count() > aRequestId && mRequests[aRequestId],
-               "Got an invalid request id or it has been already deleted!");
-
-  // It's safe to use the static_cast here given that we did call
-  // |new SmsRequest()|.
-  SmsRequest* request = static_cast<SmsRequest*>(mRequests[aRequestId]);
+  SmsRequest* request = GetRequest(aRequestId);
   request->SetError(aError);
 
   DispatchTrustedEventToRequest(ERROR_EVENT_NAME, request);
 
   mRequests.ReplaceObjectAt(nsnull, aRequestId);
 }
 
 void
@@ -191,11 +192,24 @@ void
 SmsRequestManager::NotifyNoMessageInList(PRInt32 aRequestId)
 {
   // TODO: use Filter!
   nsCOMPtr<nsIDOMMozSmsCursor> cursor = new SmsCursor(nsnull);
 
   NotifySuccess<nsIDOMMozSmsCursor*>(aRequestId, cursor);
 }
 
+void
+SmsRequestManager::NotifyCreateMessageList(PRInt32 aRequestId, PRInt32 aListId,
+                                           nsIDOMMozSmsMessage* aMessage)
+{
+  // TODO: use Filter!
+  SmsRequest* request = GetRequest(aRequestId);
+
+  nsCOMPtr<SmsCursor> cursor = new SmsCursor(nsnull, request);
+  cursor->SetMessage(aMessage);
+
+  NotifySuccess<nsIDOMMozSmsCursor*>(aRequestId, cursor);
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/SmsRequestManager.h
+++ b/dom/sms/src/SmsRequestManager.h
@@ -64,22 +64,25 @@ public:
 
   void NotifySmsSent(PRInt32 aRequestId, nsIDOMMozSmsMessage* aMessage);
   void NotifySmsSendFailed(PRInt32 aRequestId, SmsRequest::ErrorType aError);
   void NotifyGotSms(PRInt32 aRequestId, nsIDOMMozSmsMessage* aMessage);
   void NotifyGetSmsFailed(PRInt32 aRequestId, SmsRequest::ErrorType aError);
   void NotifySmsDeleted(PRInt32 aRequestId, bool aDeleted);
   void NotifySmsDeleteFailed(PRInt32 aRequestId, SmsRequest::ErrorType aError);
   void NotifyNoMessageInList(PRInt32 aRequestId);
+  void NotifyCreateMessageList(PRInt32 aRequestId, PRInt32 aListId, nsIDOMMozSmsMessage* aMessage);
 
 private:
   static SmsRequestManager* sInstance;
 
   nsresult DispatchTrustedEventToRequest(const nsAString& aEventName,
                                          nsIDOMMozSmsRequest* aRequest);
+  SmsRequest* GetRequest(PRInt32 aRequestId);
+
   template <class T>
   void NotifySuccess(PRInt32 aRequestId, T aParam);
   void NotifyError(PRInt32 aRequestId, SmsRequest::ErrorType aError);
 
   nsCOMArray<nsIDOMMozSmsRequest> mRequests;
 };
 
 } // namespace sms
--- a/dom/sms/src/ipc/PSms.ipdl
+++ b/dom/sms/src/ipc/PSms.ipdl
@@ -87,16 +87,18 @@ child:
     NotifyRequestSmsDeleted(bool aDeleted, PRInt32 aRequestId,
                             PRUint64 aProcessId);
 
     NotifyRequestSmsDeleteFailed(PRInt32 aError, PRInt32 aRequestId,
                                  PRUint64 aProcessId);
 
     NotifyRequestNoMessageInList(PRInt32 aRequestId, PRUint64 aProcessId);
 
+    NotifyRequestCreateMessageList(PRInt32 aListId, SmsMessageData aMessageData, PRInt32 aRequestId, PRUint64 aProcessId);
+
 parent:
     sync HasSupport()
         returns (bool aHasSupport);
 
     sync GetNumberOfMessagesForText(nsString aText)
         returns (PRUint16 aNumber);
 
     SendMessage(nsString aNumber, nsString aMessage, PRInt32 aRequestId,
--- a/dom/sms/src/ipc/SmsChild.cpp
+++ b/dom/sms/src/ipc/SmsChild.cpp
@@ -186,11 +186,26 @@ SmsChild::RecvNotifyRequestNoMessageInLi
   if (ContentChild::GetSingleton()->GetID() != aProcessId) {
     return true;
   }
 
   SmsRequestManager::GetInstance()->NotifyNoMessageInList(aRequestId);
   return true;
 }
 
+bool
+SmsChild::RecvNotifyRequestCreateMessageList(const PRInt32& aListId,
+                                             const SmsMessageData& aMessageData,
+                                             const PRInt32& aRequestId,
+                                             const PRUint64& aProcessId)
+{
+  if (ContentChild::GetSingleton()->GetID() != aProcessId) {
+    return true;
+  }
+
+  nsCOMPtr<nsIDOMMozSmsMessage> message = new SmsMessage(aMessageData);
+  SmsRequestManager::GetInstance()->NotifyCreateMessageList(aRequestId, aListId, message);
+  return true;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/SmsChild.h
+++ b/dom/sms/src/ipc/SmsChild.h
@@ -52,15 +52,16 @@ public:
   NS_OVERRIDE virtual bool RecvNotifyDeliveredMessage(const SmsMessageData& aMessage);
   NS_OVERRIDE virtual bool RecvNotifyRequestSmsSent(const SmsMessageData& aMessage, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvNotifyRequestSmsSendFailed(const PRInt32& aError, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvNotifyRequestGotSms(const SmsMessageData& aMessage, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvNotifyRequestGetSmsFailed(const PRInt32& aError, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvNotifyRequestSmsDeleted(const bool& aDeleted, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvNotifyRequestSmsDeleteFailed(const PRInt32& aError, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvNotifyRequestNoMessageInList(const PRInt32& aRequestId, const PRUint64& aProcessId);
+  NS_OVERRIDE virtual bool RecvNotifyRequestCreateMessageList(const PRInt32& aListId, const SmsMessageData& aMessage, const PRInt32& aRequestId, const PRUint64& aProcessId);
 };
 
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_sms_SmsChild_h
--- a/widget/android/AndroidJNI.cpp
+++ b/widget/android/AndroidJNI.cpp
@@ -673,19 +673,76 @@ Java_org_mozilla_gecko_GeckoAppShell_not
 
 
     nsCOMPtr<nsIRunnable> runnable =
       new NotifyNoMessageInListRunnable(aRequestId, aProcessId);
     NS_DispatchToMainThread(runnable);
 }
 
 NS_EXPORT void JNICALL
-Java_org_mozilla_gecko_GeckoAppShell_notifyListCreated(JNIEnv* jenv, jclass, jint, jint, jstring, jstring, jstring, jlong, jint, jlong)
+Java_org_mozilla_gecko_GeckoAppShell_notifyListCreated(JNIEnv* jenv, jclass,
+                                                       jint aListId,
+                                                       jint aMessageId,
+                                                       jstring aReceiver,
+                                                       jstring aSender,
+                                                       jstring aBody,
+                                                       jlong aTimestamp,
+                                                       jint aRequestId,
+                                                       jlong aProcessId)
 {
-  // TODO: implement
+    class NotifyCreateMessageListRunnable : public nsRunnable {
+    public:
+      NotifyCreateMessageListRunnable(PRInt32 aListId,
+                                      const SmsMessageData& aMessage,
+                                      PRInt32 aRequestId, PRUint64 aProcessId)
+        : mListId(aListId)
+        , mMessage(aMessage)
+        , mRequestId(aRequestId)
+        , mProcessId(aProcessId)
+      {}
+
+      NS_IMETHODIMP Run() {
+        if (mProcessId == 0) { // Parent process.
+          nsCOMPtr<nsIDOMMozSmsMessage> message = new SmsMessage(mMessage);
+          SmsRequestManager::GetInstance()->NotifyCreateMessageList(mRequestId,
+                                                                    mListId,
+                                                                    message);
+        } else { // Content process.
+          nsTArray<SmsParent*> spList;
+          SmsParent::GetAll(spList);
+
+          for (PRUint32 i=0; i<spList.Length(); ++i) {
+            unused << spList[i]->SendNotifyRequestCreateMessageList(mListId,
+                                                                    mMessage,
+                                                                    mRequestId,
+                                                                    mProcessId);
+          }
+        }
+
+        return NS_OK;
+      }
+
+    private:
+      PRInt32        mListId;
+      SmsMessageData mMessage;
+      PRInt32        mRequestId;
+      PRUint64       mProcessId;
+    };
+
+
+    nsJNIString receiver = nsJNIString(aReceiver, jenv);
+    DeliveryState state = receiver.IsEmpty() ? eDeliveryState_Received
+                                             : eDeliveryState_Sent;
+
+    SmsMessageData message(aMessageId, state, nsJNIString(aSender, jenv),
+                           receiver, nsJNIString(aBody, jenv), aTimestamp);
+
+    nsCOMPtr<nsIRunnable> runnable =
+      new NotifyCreateMessageListRunnable(aListId, message, aRequestId, aProcessId);
+    NS_DispatchToMainThread(runnable);
 }
 
 #ifdef MOZ_JAVA_COMPOSITOR
 
 NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_bindWidgetTexture(JNIEnv* jenv, jclass)
 {
     nsWindow::BindToTexture();