2011-11-22 01:34:21 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-17 12:42:38 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-22 01:34:21 -08:00
|
|
|
|
|
|
|
#include "SmsMessage.h"
|
|
|
|
#include "nsIDOMClassInfo.h"
|
|
|
|
#include "jsapi.h" // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec
|
2012-01-15 00:13:07 -08:00
|
|
|
#include "jsfriendapi.h" // For js_DateGetMsecSinceEpoch
|
2013-09-11 20:17:24 -07:00
|
|
|
#include "mozilla/dom/mobilemessage/Constants.h" // For MessageType
|
2011-11-22 01:34:21 -08:00
|
|
|
|
2013-03-01 00:38:47 -08:00
|
|
|
using namespace mozilla::dom::mobilemessage;
|
|
|
|
|
|
|
|
DOMCI_DATA(MozSmsMessage, mozilla::dom::SmsMessage)
|
2011-11-22 01:34:21 -08:00
|
|
|
|
2013-11-01 05:00:55 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function to convert the JS value to an integer value for time.
|
|
|
|
*
|
|
|
|
* @params aCx
|
|
|
|
* The JS context.
|
|
|
|
* @params aTime
|
|
|
|
* Can be an object or a number.
|
|
|
|
* @params aReturn
|
|
|
|
* The integer value to return.
|
|
|
|
* @return NS_OK if the convertion succeeds.
|
|
|
|
*/
|
|
|
|
static nsresult
|
|
|
|
convertTimeToInt(JSContext* aCx, const JS::Value& aTime, uint64_t& aReturn)
|
|
|
|
{
|
|
|
|
if (aTime.isObject()) {
|
|
|
|
JS::Rooted<JSObject*> timestampObj(aCx, &aTime.toObject());
|
|
|
|
if (!JS_ObjectIsDate(aCx, timestampObj)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
aReturn = js_DateGetMsecSinceEpoch(timestampObj);
|
|
|
|
} else {
|
|
|
|
if (!aTime.isNumber()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
double number = aTime.toNumber();
|
|
|
|
if (static_cast<uint64_t>(number) != number) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
aReturn = static_cast<uint64_t>(number);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-11-22 01:34:21 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(SmsMessage)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMozSmsMessage)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozSmsMessage)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(SmsMessage)
|
|
|
|
NS_IMPL_RELEASE(SmsMessage)
|
|
|
|
|
2012-10-30 03:53:24 -07:00
|
|
|
SmsMessage::SmsMessage(int32_t aId,
|
2013-04-05 05:15:52 -07:00
|
|
|
const uint64_t aThreadId,
|
2012-10-30 03:53:24 -07:00
|
|
|
DeliveryState aDelivery,
|
|
|
|
DeliveryStatus aDeliveryStatus,
|
|
|
|
const nsString& aSender,
|
|
|
|
const nsString& aReceiver,
|
|
|
|
const nsString& aBody,
|
2012-10-31 02:10:44 -07:00
|
|
|
MessageClass aMessageClass,
|
2012-10-30 03:53:24 -07:00
|
|
|
uint64_t aTimestamp,
|
2013-11-01 05:00:55 -07:00
|
|
|
uint64_t aDeliveryTimestamp,
|
2012-10-30 03:53:24 -07:00
|
|
|
bool aRead)
|
2013-04-05 05:15:52 -07:00
|
|
|
: mData(aId, aThreadId, aDelivery, aDeliveryStatus, aSender, aReceiver, aBody,
|
2013-11-01 05:00:55 -07:00
|
|
|
aMessageClass, aTimestamp, aDeliveryTimestamp, aRead)
|
2012-01-10 11:34:50 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-24 03:45:55 -08:00
|
|
|
SmsMessage::SmsMessage(const SmsMessageData& aData)
|
|
|
|
: mData(aData)
|
2011-11-22 01:34:21 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-23 21:02:52 -08:00
|
|
|
/* static */ nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
SmsMessage::Create(int32_t aId,
|
2013-04-05 05:15:52 -07:00
|
|
|
const uint64_t aThreadId,
|
2011-12-23 21:02:52 -08:00
|
|
|
const nsAString& aDelivery,
|
2012-10-30 03:53:24 -07:00
|
|
|
const nsAString& aDeliveryStatus,
|
2011-12-23 21:02:52 -08:00
|
|
|
const nsAString& aSender,
|
|
|
|
const nsAString& aReceiver,
|
|
|
|
const nsAString& aBody,
|
2012-10-31 02:10:44 -07:00
|
|
|
const nsAString& aMessageClass,
|
2013-04-02 15:42:37 -07:00
|
|
|
const JS::Value& aTimestamp,
|
2013-11-01 05:00:55 -07:00
|
|
|
const JS::Value& aDeliveryTimestamp,
|
2012-05-17 12:42:38 -07:00
|
|
|
const bool aRead,
|
2011-12-23 21:02:52 -08:00
|
|
|
JSContext* aCx,
|
|
|
|
nsIDOMMozSmsMessage** aMessage)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aMessage = nullptr;
|
2011-12-23 21:02:52 -08:00
|
|
|
|
|
|
|
// SmsMessageData exposes these as references, so we can simply assign
|
|
|
|
// to them.
|
|
|
|
SmsMessageData data;
|
|
|
|
data.id() = aId;
|
2013-04-05 05:15:52 -07:00
|
|
|
data.threadId() = aThreadId;
|
2011-12-23 21:02:52 -08:00
|
|
|
data.sender() = nsString(aSender);
|
|
|
|
data.receiver() = nsString(aReceiver);
|
|
|
|
data.body() = nsString(aBody);
|
2012-05-17 12:42:38 -07:00
|
|
|
data.read() = aRead;
|
2011-12-23 21:02:52 -08:00
|
|
|
|
|
|
|
if (aDelivery.Equals(DELIVERY_RECEIVED)) {
|
|
|
|
data.delivery() = eDeliveryState_Received;
|
2012-12-11 01:09:42 -08:00
|
|
|
} else if (aDelivery.Equals(DELIVERY_SENDING)) {
|
|
|
|
data.delivery() = eDeliveryState_Sending;
|
2011-12-23 21:02:52 -08:00
|
|
|
} else if (aDelivery.Equals(DELIVERY_SENT)) {
|
|
|
|
data.delivery() = eDeliveryState_Sent;
|
2012-12-11 01:09:42 -08:00
|
|
|
} else if (aDelivery.Equals(DELIVERY_ERROR)) {
|
|
|
|
data.delivery() = eDeliveryState_Error;
|
2011-12-23 21:02:52 -08:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2012-10-30 03:53:24 -07:00
|
|
|
if (aDeliveryStatus.Equals(DELIVERY_STATUS_NOT_APPLICABLE)) {
|
|
|
|
data.deliveryStatus() = eDeliveryStatus_NotApplicable;
|
|
|
|
} else if (aDeliveryStatus.Equals(DELIVERY_STATUS_SUCCESS)) {
|
|
|
|
data.deliveryStatus() = eDeliveryStatus_Success;
|
|
|
|
} else if (aDeliveryStatus.Equals(DELIVERY_STATUS_PENDING)) {
|
|
|
|
data.deliveryStatus() = eDeliveryStatus_Pending;
|
|
|
|
} else if (aDeliveryStatus.Equals(DELIVERY_STATUS_ERROR)) {
|
|
|
|
data.deliveryStatus() = eDeliveryStatus_Error;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2012-10-31 02:10:44 -07:00
|
|
|
if (aMessageClass.Equals(MESSAGE_CLASS_NORMAL)) {
|
|
|
|
data.messageClass() = eMessageClass_Normal;
|
|
|
|
} else if (aMessageClass.Equals(MESSAGE_CLASS_CLASS_0)) {
|
|
|
|
data.messageClass() = eMessageClass_Class0;
|
|
|
|
} else if (aMessageClass.Equals(MESSAGE_CLASS_CLASS_1)) {
|
|
|
|
data.messageClass() = eMessageClass_Class1;
|
|
|
|
} else if (aMessageClass.Equals(MESSAGE_CLASS_CLASS_2)) {
|
|
|
|
data.messageClass() = eMessageClass_Class2;
|
|
|
|
} else if (aMessageClass.Equals(MESSAGE_CLASS_CLASS_3)) {
|
|
|
|
data.messageClass() = eMessageClass_Class3;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-11-01 05:00:55 -07:00
|
|
|
// Set |timestamp|.
|
|
|
|
nsresult rv = convertTimeToInt(aCx, aTimestamp, data.timestamp());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Set |deliveryTimestamp|.
|
|
|
|
rv = convertTimeToInt(aCx, aDeliveryTimestamp, data.deliveryTimestamp());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-12-23 21:02:52 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMMozSmsMessage> message = new SmsMessage(data);
|
|
|
|
message.swap(*aMessage);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-24 03:45:55 -08:00
|
|
|
const SmsMessageData&
|
|
|
|
SmsMessage::GetData() const
|
|
|
|
{
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
2013-03-07 19:46:16 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetType(nsAString& aType)
|
|
|
|
{
|
|
|
|
aType = NS_LITERAL_STRING("sms");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-22 01:34:21 -08:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
SmsMessage::GetId(int32_t* aId)
|
2011-11-22 01:34:21 -08:00
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
*aId = mData.id();
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-05 05:15:52 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetThreadId(uint64_t* aThreadId)
|
|
|
|
{
|
|
|
|
*aThreadId = mData.threadId();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-02 03:16:59 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetIccId(nsAString& aIccId)
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-22 01:34:21 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetDelivery(nsAString& aDelivery)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
switch (mData.delivery()) {
|
2011-11-22 01:34:21 -08:00
|
|
|
case eDeliveryState_Received:
|
2012-01-17 10:48:58 -08:00
|
|
|
aDelivery = DELIVERY_RECEIVED;
|
2011-11-22 01:34:21 -08:00
|
|
|
break;
|
2012-12-11 01:09:42 -08:00
|
|
|
case eDeliveryState_Sending:
|
|
|
|
aDelivery = DELIVERY_SENDING;
|
|
|
|
break;
|
2011-11-24 03:45:55 -08:00
|
|
|
case eDeliveryState_Sent:
|
2012-01-17 10:48:58 -08:00
|
|
|
aDelivery = DELIVERY_SENT;
|
2011-11-24 03:45:55 -08:00
|
|
|
break;
|
2012-12-11 01:09:42 -08:00
|
|
|
case eDeliveryState_Error:
|
|
|
|
aDelivery = DELIVERY_ERROR;
|
|
|
|
break;
|
2011-11-24 03:45:55 -08:00
|
|
|
case eDeliveryState_Unknown:
|
2012-01-17 10:42:05 -08:00
|
|
|
case eDeliveryState_EndGuard:
|
2011-11-24 03:45:55 -08:00
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("We shouldn't get any other delivery state!");
|
2012-10-30 03:53:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetDeliveryStatus(nsAString& aDeliveryStatus)
|
|
|
|
{
|
|
|
|
switch (mData.deliveryStatus()) {
|
|
|
|
case eDeliveryStatus_NotApplicable:
|
|
|
|
aDeliveryStatus = DELIVERY_STATUS_NOT_APPLICABLE;
|
|
|
|
break;
|
|
|
|
case eDeliveryStatus_Success:
|
|
|
|
aDeliveryStatus = DELIVERY_STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
case eDeliveryStatus_Pending:
|
|
|
|
aDeliveryStatus = DELIVERY_STATUS_PENDING;
|
|
|
|
break;
|
|
|
|
case eDeliveryStatus_Error:
|
|
|
|
aDeliveryStatus = DELIVERY_STATUS_ERROR;
|
|
|
|
break;
|
|
|
|
case eDeliveryStatus_EndGuard:
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("We shouldn't get any other delivery status!");
|
2011-11-22 01:34:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetSender(nsAString& aSender)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
aSender = mData.sender();
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetReceiver(nsAString& aReceiver)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
aReceiver = mData.receiver();
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetBody(nsAString& aBody)
|
|
|
|
{
|
2011-11-24 03:45:55 -08:00
|
|
|
aBody = mData.body();
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-31 02:10:44 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetMessageClass(nsAString& aMessageClass)
|
|
|
|
{
|
|
|
|
switch (mData.messageClass()) {
|
|
|
|
case eMessageClass_Normal:
|
|
|
|
aMessageClass = MESSAGE_CLASS_NORMAL;
|
|
|
|
break;
|
|
|
|
case eMessageClass_Class0:
|
|
|
|
aMessageClass = MESSAGE_CLASS_CLASS_0;
|
|
|
|
break;
|
|
|
|
case eMessageClass_Class1:
|
|
|
|
aMessageClass = MESSAGE_CLASS_CLASS_1;
|
|
|
|
break;
|
|
|
|
case eMessageClass_Class2:
|
|
|
|
aMessageClass = MESSAGE_CLASS_CLASS_2;
|
|
|
|
break;
|
|
|
|
case eMessageClass_Class3:
|
|
|
|
aMessageClass = MESSAGE_CLASS_CLASS_3;
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("We shouldn't get any other message class!");
|
2012-10-31 02:10:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-22 01:34:21 -08:00
|
|
|
NS_IMETHODIMP
|
2013-04-02 15:42:37 -07:00
|
|
|
SmsMessage::GetTimestamp(JSContext* cx, JS::Value* aDate)
|
2011-11-22 01:34:21 -08:00
|
|
|
{
|
2013-04-08 19:21:54 -07:00
|
|
|
JSObject *obj = JS_NewDateObjectMsec(cx, mData.timestamp());
|
|
|
|
NS_ENSURE_TRUE(obj, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
*aDate = OBJECT_TO_JSVAL(obj);
|
2011-11-22 01:34:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-01 05:00:55 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetDeliveryTimestamp(JSContext* aCx, JS::Value* aDate)
|
|
|
|
{
|
|
|
|
if (mData.deliveryTimestamp() == 0) {
|
|
|
|
*aDate = JSVAL_NULL;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *obj = JS_NewDateObjectMsec(aCx, mData.deliveryTimestamp());
|
|
|
|
NS_ENSURE_TRUE(obj, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
*aDate = OBJECT_TO_JSVAL(obj);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-17 12:42:38 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SmsMessage::GetRead(bool* aRead)
|
|
|
|
{
|
|
|
|
*aRead = mData.read();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-22 01:34:21 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|