Bug 674725 - Part AP - Implementation of cursor.continue() in the DOM code. r=smaug
authorMounir Lamouri <mounir.lamouri@gmail.com>
Tue, 17 Jan 2012 19:42:39 +0100
changeset 84727 afca8fef2e4e15e6ccc7703326e8d093735dc425
parent 84726 731e1914b5985fdc912c36a0771a3f5ac3056e68
child 84728 cd7e8a34d4f39c89741250d6d576d02872d5eba9
push id21873
push usermlamouri@mozilla.com
push dateWed, 18 Jan 2012 10:29:07 +0000
treeherdermozilla-central@7538f4d4697c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs674725
milestone12.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 674725 - Part AP - Implementation of cursor.continue() in the DOM code. r=smaug
dom/sms/interfaces/nsISmsDatabaseService.idl
dom/sms/src/SmsCursor.cpp
dom/sms/src/SmsCursor.h
dom/sms/src/SmsRequest.cpp
dom/sms/src/SmsRequest.h
dom/sms/src/SmsRequestManager.cpp
dom/sms/src/SmsRequestManager.h
dom/sms/src/android/SmsDatabaseService.cpp
dom/sms/src/fallback/SmsDatabaseService.cpp
dom/sms/src/ipc/PSms.ipdl
dom/sms/src/ipc/SmsIPCService.cpp
dom/sms/src/ipc/SmsParent.cpp
dom/sms/src/ipc/SmsParent.h
--- a/dom/sms/interfaces/nsISmsDatabaseService.idl
+++ b/dom/sms/interfaces/nsISmsDatabaseService.idl
@@ -40,19 +40,20 @@
 #define SMS_DATABASE_SERVICE_CID \
 { 0xcb971459, 0xe85a, 0x49b3,    \
 { 0xbc, 0x1c, 0x10, 0x40, 0x27, 0x1e, 0x04, 0x6c } }
 #define SMS_DATABASE_SERVICE_CONTRACTID "@mozilla.org/sms/smsdatabaseservice;1"
 %}
 
 interface nsIDOMMozSmsFilter;
 
-[scriptable, function, uuid(33358749-d1b3-4bd6-835c-ef3869f0e966)]
+[scriptable, function, uuid(a253ec42-142e-490b-a479-1e9c605c0a58)]
 interface nsISmsDatabaseService : nsISupports
 {
   // Takes some information required to save the message and returns its id.
   long saveSentMessage(in DOMString aReceiver, in DOMString aBody, in unsigned long long aDate);
 
   [binaryname(GetMessageMoz)] void getMessage(in long messageId, in long requestId, [optional] in unsigned long long processId);
   void deleteMessage(in long messageId, in long requestId, [optional] in unsigned long long processId);
 
   void createMessageList(in nsIDOMMozSmsFilter filter, in boolean reverse, in long requestId, [optional] in unsigned long long processId);
+  void getNextMessageInList(in long listId, in long requestId, in unsigned long long processId);
 };
--- a/dom/sms/src/SmsCursor.cpp
+++ b/dom/sms/src/SmsCursor.cpp
@@ -36,16 +36,19 @@
  * ***** END LICENSE BLOCK ***** */
 
 #include "SmsCursor.h"
 #include "nsIDOMClassInfo.h"
 #include "nsDOMError.h"
 #include "nsIDOMSmsFilter.h"
 #include "nsIDOMSmsMessage.h"
 #include "nsIDOMSmsRequest.h"
+#include "SmsRequest.h"
+#include "SmsRequestManager.h"
+#include "nsISmsDatabaseService.h"
 
 DOMCI_DATA(MozSmsCursor, mozilla::dom::sms::SmsCursor)
 
 namespace mozilla {
 namespace dom {
 namespace sms {
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SmsCursor)
@@ -55,22 +58,24 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(
 NS_INTERFACE_MAP_END
 
 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)
+  : mListId(-1)
+  , mFilter(aFilter)
 {
 }
 
-SmsCursor::SmsCursor(nsIDOMMozSmsFilter* aFilter, nsIDOMMozSmsRequest* aRequest)
-  : mFilter(aFilter)
+SmsCursor::SmsCursor(PRInt32 aListId, nsIDOMMozSmsFilter* aFilter, nsIDOMMozSmsRequest* aRequest)
+  : mListId(aListId)
+  , mFilter(aFilter)
   , mRequest(aRequest)
 {
 }
 
 NS_IMETHODIMP
 SmsCursor::GetFilter(nsIDOMMozSmsFilter** aFilter)
 {
   NS_ADDREF(*aFilter = mFilter);
@@ -87,19 +92,26 @@ SmsCursor::GetMessage(nsIDOMMozSmsMessag
 NS_IMETHODIMP
 SmsCursor::Continue()
 {
   // No message means we are waiting for a message or we got the last one.
   if (!mMessage) {
     return NS_ERROR_DOM_INVALID_STATE_ERR;
   }
 
-  // TODO: ask for the next message and reset the request
-  // TODO: add the associated request to the request manager
-  //       and send the id to the backend
+  mMessage = nsnull;
+  static_cast<SmsRequest*>(mRequest.get())->Reset();
+
+  PRInt32 requestId = SmsRequestManager::GetInstance()->AddRequest(mRequest);
+
+  nsCOMPtr<nsISmsDatabaseService> smsDBService =
+    do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
+  NS_ENSURE_TRUE(smsDBService, NS_ERROR_FAILURE);
+
+  smsDBService->GetNextMessageInList(mListId, requestId, 0);
 
   return NS_OK;
 }
 
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
 
--- a/dom/sms/src/SmsCursor.h
+++ b/dom/sms/src/SmsCursor.h
@@ -54,21 +54,22 @@ class SmsCursor : public nsIDOMMozSmsCur
 {
 public:
   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   NS_DECL_NSIDOMMOZSMSCURSOR
 
   NS_DECL_CYCLE_COLLECTION_CLASS(SmsCursor)
 
   SmsCursor(nsIDOMMozSmsFilter* aFilter);
-  SmsCursor(nsIDOMMozSmsFilter* aFilter, nsIDOMMozSmsRequest* aRequest);
+  SmsCursor(PRInt32 aListId, nsIDOMMozSmsFilter* aFilter, nsIDOMMozSmsRequest* aRequest);
 
   void SetMessage(nsIDOMMozSmsMessage* aMessage);
 
 private:
+  PRInt32                       mListId;
   nsCOMPtr<nsIDOMMozSmsFilter>  mFilter;
   nsCOMPtr<nsIDOMMozSmsRequest> mRequest;
   nsCOMPtr<nsIDOMMozSmsMessage> mMessage;
 };
 
 inline void
 SmsCursor::SetMessage(nsIDOMMozSmsMessage* aMessage)
 {
--- a/dom/sms/src/SmsRequest.cpp
+++ b/dom/sms/src/SmsRequest.cpp
@@ -101,16 +101,31 @@ SmsRequest::SmsRequest(nsPIDOMWindow* aW
 SmsRequest::~SmsRequest()
 {
   if (mResultRooted) {
     UnrootResult();
   }
 }
 
 void
+SmsRequest::Reset()
+{
+  NS_ASSERTION(mDone, "mDone should be true if we try to reset!");
+  NS_ASSERTION(mResult != JSVAL_VOID, "mResult should be set if we try to reset!");
+  NS_ASSERTION(mError == eNoError, "There should be no error if we try to reset!");
+
+  if (mResultRooted) {
+    UnrootResult();
+  }
+
+  mResult = JSVAL_VOID;
+  mDone = false;
+}
+
+void
 SmsRequest::RootResult()
 {
   NS_ASSERTION(!mResultRooted, "Don't call RootResult() if already rooted!");
   NS_HOLD_JS_OBJECTS(this, SmsRequest);
   mResultRooted = true;
 }
 
 void
--- a/dom/sms/src/SmsRequest.h
+++ b/dom/sms/src/SmsRequest.h
@@ -70,16 +70,18 @@ public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIDOMMOZSMSREQUEST
 
   NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetWrapperCache::)
 
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(SmsRequest,
                                                          nsDOMEventTargetWrapperCache)
 
+  void Reset();
+
 private:
   SmsRequest() MOZ_DELETE;
 
   SmsRequest(nsPIDOMWindow* aWindow, nsIScriptContext* aScriptContext);
   ~SmsRequest();
 
   /**
    * Root mResult (jsval) to prevent garbage collection.
--- a/dom/sms/src/SmsRequestManager.cpp
+++ b/dom/sms/src/SmsRequestManager.cpp
@@ -72,16 +72,36 @@ SmsRequestManager::Shutdown()
 
 /* static */ SmsRequestManager*
 SmsRequestManager::GetInstance()
 {
   return sInstance;
 }
 
 PRInt32
+SmsRequestManager::AddRequest(nsIDOMMozSmsRequest* aRequest)
+{
+  // TODO: merge with CreateRequest
+  PRInt32 size = mRequests.Count();
+
+  // Look for empty slots.
+  for (PRInt32 i=0; i<size; ++i) {
+    if (mRequests[i]) {
+      continue;
+    }
+
+    mRequests.ReplaceObjectAt(aRequest, i);
+    return i;
+  }
+
+  mRequests.AppendObject(aRequest);
+  return size;
+}
+
+PRInt32
 SmsRequestManager::CreateRequest(nsPIDOMWindow* aWindow,
                                  nsIScriptContext* aScriptContext,
                                  nsIDOMMozSmsRequest** aRequest)
 {
   nsCOMPtr<nsIDOMMozSmsRequest> request =
     new SmsRequest(aWindow, aScriptContext);
 
   PRInt32 size = mRequests.Count();
@@ -199,17 +219,17 @@ SmsRequestManager::NotifyNoMessageInList
 
 void
 SmsRequestManager::NotifyCreateMessageList(PRInt32 aRequestId, PRInt32 aListId,
                                            nsIDOMMozSmsMessage* aMessage)
 {
   // TODO: use Filter!
   SmsRequest* request = GetRequest(aRequestId);
 
-  nsCOMPtr<SmsCursor> cursor = new SmsCursor(nsnull, request);
+  nsCOMPtr<SmsCursor> cursor = new SmsCursor(aListId, 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
@@ -57,16 +57,18 @@ public:
   static void Init();
   static void Shutdown();
   static SmsRequestManager* GetInstance();
 
   PRInt32 CreateRequest(nsPIDOMWindow* aWindow,
                         nsIScriptContext* aScriptContext,
                         nsIDOMMozSmsRequest** aRequest);
 
+  PRInt32 AddRequest(nsIDOMMozSmsRequest* aRequest);
+
   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);
--- a/dom/sms/src/android/SmsDatabaseService.cpp
+++ b/dom/sms/src/android/SmsDatabaseService.cpp
@@ -94,11 +94,19 @@ SmsDatabaseService::CreateMessageList(ns
   }
 
   AndroidBridge::Bridge()->CreateMessageList(
     static_cast<SmsFilter*>(aFilter)->GetData(), aReverse, aRequestId, aProcessId
   );
   return NS_OK;
 }
 
+NS_IMETHODIMP
+SmsDatabaseService::GetNextMessageInList(PRInt32 aListId, PRInt32 aRequestId,
+                                         PRUint64 aProcessId)
+{
+  // TODO: implement
+  return NS_OK;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/fallback/SmsDatabaseService.cpp
+++ b/dom/sms/src/fallback/SmsDatabaseService.cpp
@@ -73,11 +73,19 @@ NS_IMETHODIMP
 SmsDatabaseService::CreateMessageList(nsIDOMMozSmsFilter* aFilter,
                                       bool aReverse, PRInt32 aRequestId,
                                       PRUint64 aProcessId)
 {
   NS_ERROR("We should not be here!");
   return NS_OK;
 }
 
+NS_IMETHODIMP
+SmsDatabaseService::GetNextMessageInList(PRInt32 aListId, PRInt32 aRequestId,
+                                         PRUint64 aProcessId)
+{
+  NS_ERROR("We should not be here!");
+  return NS_OK;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/PSms.ipdl
+++ b/dom/sms/src/ipc/PSms.ipdl
@@ -108,14 +108,16 @@ parent:
         returns (PRInt32 aId);
 
     GetMessage(PRInt32 aMessageId, PRInt32 aRequestId, PRUint64 aProcessId);
 
     DeleteMessage(PRInt32 aMessageId, PRInt32 aRequestId, PRUint64 aProcessId);
 
     CreateMessageList(SmsFilterData aFilter, bool aReverse, PRInt32 aRequestId, PRUint64 aProcessId);
 
+    GetNextMessageInList(PRInt32 aListId, PRInt32 aRequestId, PRUint64 aProcessId);
+
     __delete__();
 };
 
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/SmsIPCService.cpp
+++ b/dom/sms/src/ipc/SmsIPCService.cpp
@@ -142,11 +142,20 @@ SmsIPCService::CreateMessageList(nsIDOMM
 {
   SmsFilter* filter = static_cast<SmsFilter*>(aFilter);
   GetSmsChild()->SendCreateMessageList(filter->GetData(), aReverse, aRequestId,
                                        ContentChild::GetSingleton()->GetID());
 
   return NS_OK;
 }
 
+NS_IMETHODIMP
+SmsIPCService::GetNextMessageInList(PRInt32 aListId, PRInt32 aRequestId,
+                                    PRUint64 aProcessId)
+{
+  GetSmsChild()->SendGetNextMessageInList(aListId, aRequestId,
+                                          ContentChild::GetSingleton()->GetID());
+  return NS_OK;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/SmsParent.cpp
+++ b/dom/sms/src/ipc/SmsParent.cpp
@@ -228,11 +228,25 @@ SmsParent::RecvCreateMessageList(const S
   NS_ENSURE_TRUE(smsDBService, true);
 
   nsCOMPtr<nsIDOMMozSmsFilter> filter = new SmsFilter(aFilter);
   smsDBService->CreateMessageList(filter, aReverse, aRequestId, aProcessId);
 
   return true;
 }
 
+bool
+SmsParent::RecvGetNextMessageInList(const PRInt32& aListId,
+                                    const PRInt32& aRequestId,
+                                    const PRUint64& aProcessId)
+{
+  nsCOMPtr<nsISmsDatabaseService> smsDBService =
+    do_GetService(SMS_DATABASE_SERVICE_CONTRACTID);
+  NS_ENSURE_TRUE(smsDBService, true);
+
+  smsDBService->GetNextMessageInList(aListId, aRequestId, aProcessId);
+
+  return true;
+}
+
 } // namespace sms
 } // namespace dom
 } // namespace mozilla
--- a/dom/sms/src/ipc/SmsParent.h
+++ b/dom/sms/src/ipc/SmsParent.h
@@ -58,16 +58,17 @@ public:
 
   NS_OVERRIDE virtual bool RecvHasSupport(bool* aHasSupport);
   NS_OVERRIDE virtual bool RecvGetNumberOfMessagesForText(const nsString& aText, PRUint16* aResult);
   NS_OVERRIDE virtual bool RecvSendMessage(const nsString& aNumber, const nsString& aMessage, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvSaveSentMessage(const nsString& aRecipient, const nsString& aBody, const PRUint64& aDate, PRInt32* aId);
   NS_OVERRIDE virtual bool RecvGetMessage(const PRInt32& aMessageId, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvDeleteMessage(const PRInt32& aMessageId, const PRInt32& aRequestId, const PRUint64& aProcessId);
   NS_OVERRIDE virtual bool RecvCreateMessageList(const SmsFilterData& aFilter, const bool& aReverse, const PRInt32& aRequestId, const PRUint64& aProcessId);
+  NS_OVERRIDE virtual bool RecvGetNextMessageInList(const PRInt32& aListId, const PRInt32& aRequestId, const PRUint64& aProcessId);
 
 protected:
   virtual void ActorDestroy(ActorDestroyReason why);
 
 private:
   static nsTArray<SmsParent*>* gSmsParents;
 };