diff --git a/dom/sms/src/Makefile.in b/dom/sms/src/Makefile.in index f8530574cc7..4d139c0ad16 100644 --- a/dom/sms/src/Makefile.in +++ b/dom/sms/src/Makefile.in @@ -62,7 +62,8 @@ EXPORTS_mozilla/dom/sms = \ SmsChild.h \ SmsParent.h \ SmsServiceFactory.h \ - Constants.h + Constants.h \ + Types.h \ $(NULL) CPPSRCS = \ @@ -74,6 +75,7 @@ CPPSRCS = \ SmsMessage.cpp \ SmsEvent.cpp \ Constants.cpp \ + SmsChild.cpp \ $(NULL) LOCAL_INCLUDES = \ diff --git a/dom/sms/src/SmsMessage.cpp b/dom/sms/src/SmsMessage.cpp index 85e316c69fc..49255b55201 100644 --- a/dom/sms/src/SmsMessage.cpp +++ b/dom/sms/src/SmsMessage.cpp @@ -38,6 +38,7 @@ #include "SmsMessage.h" #include "nsIDOMClassInfo.h" #include "jsapi.h" // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec +#include "Constants.h" DOMCI_DATA(MozSmsMessage, mozilla::dom::sms::SmsMessage) @@ -54,35 +55,38 @@ NS_INTERFACE_MAP_END NS_IMPL_ADDREF(SmsMessage) NS_IMPL_RELEASE(SmsMessage) -SmsMessage::SmsMessage(PRInt32 aId, DeliveryState aDelivery, - const nsAString& aSender, const nsAString& aReceiver, - const nsAString& aBody, PRUint64 aTimestamp) - : mId(aId) - , mDelivery(aDelivery) - , mSender(aSender) - , mReceiver(aReceiver) - , mBody(aBody) - , mTimestamp(aTimestamp) +SmsMessage::SmsMessage(const SmsMessageData& aData) + : mData(aData) { } +const SmsMessageData& +SmsMessage::GetData() const +{ + return mData; +} + NS_IMETHODIMP SmsMessage::GetId(PRInt32* aId) { - *aId = mId; + *aId = mData.id(); return NS_OK; } NS_IMETHODIMP SmsMessage::GetDelivery(nsAString& aDelivery) { - switch (mDelivery) { - case eDeliveryState_Sent: - aDelivery.AssignLiteral("sent"); - break; + switch (mData.delivery()) { case eDeliveryState_Received: aDelivery.AssignLiteral("received"); break; + case eDeliveryState_Sent: + aDelivery.AssignLiteral("sent"); + break; + case eDeliveryState_Unknown: + default: + NS_ASSERTION(true, "We shouldn't get an unknown delivery state!"); + return NS_ERROR_UNEXPECTED; } return NS_OK; @@ -91,28 +95,28 @@ SmsMessage::GetDelivery(nsAString& aDelivery) NS_IMETHODIMP SmsMessage::GetSender(nsAString& aSender) { - aSender = mSender; + aSender = mData.sender(); return NS_OK; } NS_IMETHODIMP SmsMessage::GetReceiver(nsAString& aReceiver) { - aReceiver = mReceiver; + aReceiver = mData.receiver(); return NS_OK; } NS_IMETHODIMP SmsMessage::GetBody(nsAString& aBody) { - aBody = mBody; + aBody = mData.body(); return NS_OK; } NS_IMETHODIMP SmsMessage::GetTimestamp(JSContext* cx, jsval* aDate) { - *aDate = OBJECT_TO_JSVAL(JS_NewDateObjectMsec(cx, mTimestamp)); + *aDate = OBJECT_TO_JSVAL(JS_NewDateObjectMsec(cx, mData.timestamp())); return NS_OK; } diff --git a/dom/sms/src/SmsMessage.h b/dom/sms/src/SmsMessage.h index 030b7120895..66d6eb89dc8 100644 --- a/dom/sms/src/SmsMessage.h +++ b/dom/sms/src/SmsMessage.h @@ -38,6 +38,7 @@ #ifndef mozilla_dom_sms_SmsMessage_h #define mozilla_dom_sms_SmsMessage_h +#include "mozilla/dom/sms/PSms.h" #include "nsIDOMSmsMessage.h" #include "nsString.h" @@ -48,28 +49,18 @@ namespace sms { class SmsMessage : public nsIDOMMozSmsMessage { public: - enum DeliveryState { - eDeliveryState_Sent, - eDeliveryState_Received - }; - NS_DECL_ISUPPORTS NS_DECL_NSIDOMMOZSMSMESSAGE - SmsMessage(PRInt32 aId, DeliveryState aDelivery, const nsAString& aSender, - const nsAString& aReceiver, const nsAString& aBody, - PRUint64 aTimestamp); + SmsMessage(const SmsMessageData& aData); + + const SmsMessageData& GetData() const; private: // Don't try to use the default constructor. SmsMessage(); - PRInt32 mId; - DeliveryState mDelivery; - nsString mSender; - nsString mReceiver; - nsString mBody; - PRUint64 mTimestamp; // Milliseconds to epoch. + SmsMessageData mData; }; } // namespace sms diff --git a/dom/sms/src/Types.h b/dom/sms/src/Types.h new file mode 100644 index 00000000000..7e07eef820b --- /dev/null +++ b/dom/sms/src/Types.h @@ -0,0 +1,71 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Mozilla Foundation + * Portions created by the Initial Developer are Copyright (C) 2011 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Mounir Lamouri (Original Author) + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef mozilla_dom_sms_Types_h +#define mozilla_dom_sms_Types_h + +namespace mozilla { +namespace dom { +namespace sms { + +// For SmsMessageDate.delivery. +enum DeliveryState { + eDeliveryState_Sent, + eDeliveryState_Received, + // This state should stay at the end. + eDeliveryState_Unknown +}; + +} // namespace sms +} // namespace dom +} // namespace mozilla + +namespace IPC { + +/** + * Delivery state serializer. + */ +template <> +struct ParamTraits + : public EnumSerializer +{}; + +} // namespace IPC + +#endif // mozilla_dom_sms_Types_h diff --git a/dom/sms/src/ipc/PSms.ipdl b/dom/sms/src/ipc/PSms.ipdl index c88c9092d22..f2333bbec8b 100644 --- a/dom/sms/src/ipc/PSms.ipdl +++ b/dom/sms/src/ipc/PSms.ipdl @@ -38,14 +38,29 @@ * ***** END LICENSE BLOCK ***** */ include protocol PContent; +include "mozilla/dom/sms/Types.h"; + +using DeliveryState; namespace mozilla { namespace dom { namespace sms { +struct SmsMessageData { + PRInt32 id; + DeliveryState delivery; + nsString sender; + nsString receiver; + nsString body; + PRUint64 timestamp; // ms since epoch. +}; + sync protocol PSms { manager PContent; +child: + NotifyReceivedMessage(SmsMessageData aMessageData); + parent: sync HasSupport() returns (bool aHasSupport); diff --git a/dom/sms/src/ipc/SmsChild.cpp b/dom/sms/src/ipc/SmsChild.cpp new file mode 100644 index 00000000000..299012a333d --- /dev/null +++ b/dom/sms/src/ipc/SmsChild.cpp @@ -0,0 +1,64 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Mozilla Foundation + * Portions created by the Initial Developer are Copyright (C) 2011 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Mounir Lamouri (Original Author) + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "SmsChild.h" +#include "SmsMessage.h" +#include "Constants.h" +#include "nsIObserverService.h" +#include "mozilla/Services.h" + +namespace mozilla { +namespace dom { +namespace sms { + +bool +SmsChild::RecvNotifyReceivedMessage(const SmsMessageData& aMessageData) +{ + nsCOMPtr obs = services::GetObserverService(); + if (!obs) { + return true; + } + + nsCOMPtr message = new SmsMessage(aMessageData); + obs->NotifyObservers(message, kSmsReceivedObserverTopic, nsnull); + + return true; +} + +} // namespace sms +} // namespace dom +} // namespace mozilla diff --git a/dom/sms/src/ipc/SmsChild.h b/dom/sms/src/ipc/SmsChild.h index 48076d6f5ee..bdee40bc19f 100644 --- a/dom/sms/src/ipc/SmsChild.h +++ b/dom/sms/src/ipc/SmsChild.h @@ -46,6 +46,8 @@ namespace sms { class SmsChild : public PSmsChild { +public: + NS_OVERRIDE virtual bool RecvNotifyReceivedMessage(const SmsMessageData& aMessage); }; } // namespace sms diff --git a/dom/sms/src/ipc/SmsParent.cpp b/dom/sms/src/ipc/SmsParent.cpp index ab78cf46fff..4f67f940d4b 100644 --- a/dom/sms/src/ipc/SmsParent.cpp +++ b/dom/sms/src/ipc/SmsParent.cpp @@ -37,11 +37,58 @@ #include "SmsParent.h" #include "nsISmsService.h" +#include "nsIObserverService.h" +#include "mozilla/Services.h" +#include "Constants.h" +#include "nsIDOMSmsMessage.h" +#include "mozilla/unused.h" +#include "SmsMessage.h" namespace mozilla { namespace dom { namespace sms { +NS_IMPL_ISUPPORTS1(SmsParent, nsIObserver) + +SmsParent::SmsParent() +{ + nsCOMPtr obs = services::GetObserverService(); + if (!obs) { + return; + } + + obs->AddObserver(this, kSmsReceivedObserverTopic, false); +} + +void +SmsParent::ActorDestroy(ActorDestroyReason why) +{ + nsCOMPtr obs = services::GetObserverService(); + if (!obs) { + return; + } + + obs->RemoveObserver(this, kSmsReceivedObserverTopic); +} + +NS_IMETHODIMP +SmsParent::Observe(nsISupports* aSubject, const char* aTopic, + const PRUnichar* aData) +{ + if (!strcmp(aTopic, kSmsReceivedObserverTopic)) { + nsCOMPtr message = do_QueryInterface(aSubject); + if (!message) { + NS_ERROR("Got a 'sms-received' topic without a valid message!"); + return NS_OK; + } + + unused << SendNotifyReceivedMessage(static_cast(message.get())->GetData()); + return NS_OK; + } + + return NS_OK; +} + bool SmsParent::RecvHasSupport(bool* aHasSupport) { diff --git a/dom/sms/src/ipc/SmsParent.h b/dom/sms/src/ipc/SmsParent.h index 09879d80712..7cb1cda1bf8 100644 --- a/dom/sms/src/ipc/SmsParent.h +++ b/dom/sms/src/ipc/SmsParent.h @@ -39,16 +39,27 @@ #define mozilla_dom_sms_SmsParent_h #include "mozilla/dom/sms/PSmsParent.h" +#include "nsIObserver.h" namespace mozilla { namespace dom { namespace sms { class SmsParent : public PSmsParent + , public nsIObserver { +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIOBSERVER + + SmsParent(); + 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); + +protected: + virtual void ActorDestroy(ActorDestroyReason why); }; } // namespace sms diff --git a/ipc/glue/IPCMessageUtils.h b/ipc/glue/IPCMessageUtils.h index 0daa28d8e7d..35331c63bc9 100644 --- a/ipc/glue/IPCMessageUtils.h +++ b/ipc/glue/IPCMessageUtils.h @@ -96,6 +96,37 @@ struct null_t { namespace IPC { +/** + * Generic enum serializer. + * E is the enum type. + * lowBound is the lowest allowed value of the enum. + * highBound is the value higher than highest allowed value of the enum. + * In other words, it's the lowest unallowed value. + */ +template +struct EnumSerializer { + typedef E paramType; + + static bool IsLegalValue(const paramType &aValue) { + return lowBound <= aValue && aValue < highBound; + } + + static void Write(Message* aMsg, const paramType& aValue) { + MOZ_ASSERT(IsLegalValue(aValue)); + WriteParam(aMsg, (int32)aValue); + } + + static bool Read(const Message* aMsg, void** aIter, paramType* aResult) { + int32 value; + if(!ReadParam(aMsg, aIter, &value) || + !IsLegalValue(paramType(value))) { + return false; + } + *aResult = paramType(value); + return true; + } +}; + template<> struct ParamTraits {