gecko/dom/mobilemessage/src/SmsMessage.cpp

277 lines
7.4 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "SmsMessage.h"
#include "nsIDOMClassInfo.h"
#include "jsapi.h" // For OBJECT_TO_JSVAL and JS_NewDateObjectMsec
#include "jsfriendapi.h" // For js_DateGetMsecSinceEpoch
#include "Constants.h"
Bug 844429 - B2G SMS & MMS: move SMS codes into dom/mobilemessage to make it generic for MMS. r=vicamo --HG-- rename : dom/sms/interfaces/nsIDOMMozSmsEvent.idl => dom/mobilemessage/interfaces/nsIDOMMozSmsEvent.idl rename : dom/sms/interfaces/nsIDOMMozSmsMessage.idl => dom/mobilemessage/interfaces/nsIDOMMozSmsMessage.idl rename : dom/sms/interfaces/nsIDOMNavigatorSms.idl => dom/mobilemessage/interfaces/nsIDOMNavigatorSms.idl rename : dom/sms/interfaces/nsIDOMSmsCursor.idl => dom/mobilemessage/interfaces/nsIDOMSmsCursor.idl rename : dom/sms/interfaces/nsIDOMSmsFilter.idl => dom/mobilemessage/interfaces/nsIDOMSmsFilter.idl rename : dom/sms/interfaces/nsIDOMSmsManager.idl => dom/mobilemessage/interfaces/nsIDOMSmsManager.idl rename : dom/sms/interfaces/nsIDOMSmsRequest.idl => dom/mobilemessage/interfaces/nsIDOMSmsRequest.idl rename : dom/sms/interfaces/nsIDOMSmsSegmentInfo.idl => dom/mobilemessage/interfaces/nsIDOMSmsSegmentInfo.idl rename : dom/sms/interfaces/nsISmsRequest.idl => dom/mobilemessage/interfaces/nsISmsRequest.idl rename : dom/sms/interfaces/nsISmsService.idl => dom/mobilemessage/interfaces/nsISmsService.idl rename : dom/sms/src/Constants.cpp => dom/mobilemessage/src/Constants.cpp rename : dom/sms/src/Constants.h => dom/mobilemessage/src/Constants.h rename : dom/sms/src/SmsCursor.cpp => dom/mobilemessage/src/SmsCursor.cpp rename : dom/sms/src/SmsCursor.h => dom/mobilemessage/src/SmsCursor.h rename : dom/sms/src/SmsFilter.cpp => dom/mobilemessage/src/SmsFilter.cpp rename : dom/sms/src/SmsFilter.h => dom/mobilemessage/src/SmsFilter.h rename : dom/sms/src/SmsManager.cpp => dom/mobilemessage/src/SmsManager.cpp rename : dom/sms/src/SmsManager.h => dom/mobilemessage/src/SmsManager.h rename : dom/sms/src/SmsMessage.cpp => dom/mobilemessage/src/SmsMessage.cpp rename : dom/sms/src/SmsMessage.h => dom/mobilemessage/src/SmsMessage.h rename : dom/sms/src/SmsRequest.cpp => dom/mobilemessage/src/SmsRequest.cpp rename : dom/sms/src/SmsRequest.h => dom/mobilemessage/src/SmsRequest.h rename : dom/sms/src/SmsSegmentInfo.cpp => dom/mobilemessage/src/SmsSegmentInfo.cpp rename : dom/sms/src/SmsSegmentInfo.h => dom/mobilemessage/src/SmsSegmentInfo.h rename : dom/sms/src/SmsServicesFactory.cpp => dom/mobilemessage/src/SmsServicesFactory.cpp rename : dom/sms/src/SmsServicesFactory.h => dom/mobilemessage/src/SmsServicesFactory.h rename : dom/sms/src/Types.h => dom/mobilemessage/src/Types.h rename : dom/sms/src/android/SmsService.cpp => dom/mobilemessage/src/android/SmsService.cpp rename : dom/sms/src/android/SmsService.h => dom/mobilemessage/src/android/SmsService.h rename : dom/sms/src/fallback/SmsService.cpp => dom/mobilemessage/src/fallback/SmsService.cpp rename : dom/sms/src/fallback/SmsService.h => dom/mobilemessage/src/fallback/SmsService.h rename : dom/sms/src/ipc/PSms.ipdl => dom/mobilemessage/src/ipc/PSms.ipdl rename : dom/sms/src/ipc/PSmsRequest.ipdl => dom/mobilemessage/src/ipc/PSmsRequest.ipdl rename : dom/sms/src/ipc/SmsChild.cpp => dom/mobilemessage/src/ipc/SmsChild.cpp rename : dom/sms/src/ipc/SmsChild.h => dom/mobilemessage/src/ipc/SmsChild.h rename : dom/sms/src/ipc/SmsIPCService.cpp => dom/mobilemessage/src/ipc/SmsIPCService.cpp rename : dom/sms/src/ipc/SmsIPCService.h => dom/mobilemessage/src/ipc/SmsIPCService.h rename : dom/sms/src/ipc/SmsParent.cpp => dom/mobilemessage/src/ipc/SmsParent.cpp rename : dom/sms/src/ipc/SmsParent.h => dom/mobilemessage/src/ipc/SmsParent.h rename : dom/sms/src/ipc/SmsTypes.ipdlh => dom/mobilemessage/src/ipc/SmsTypes.ipdlh rename : dom/sms/src/ipc/ipdl.mk => dom/mobilemessage/src/ipc/ipdl.mk rename : dom/sms/src/ril/SmsService.cpp => dom/mobilemessage/src/ril/SmsService.cpp rename : dom/sms/src/ril/SmsService.h => dom/mobilemessage/src/ril/SmsService.h rename : dom/sms/tests/Makefile.in => dom/mobilemessage/tests/Makefile.in rename : dom/sms/tests/marionette/manifest.ini => dom/mobilemessage/tests/marionette/manifest.ini rename : dom/sms/tests/marionette/test_between_emulators.py => dom/mobilemessage/tests/marionette/test_between_emulators.py rename : dom/sms/tests/marionette/test_bug814761.js => dom/mobilemessage/tests/marionette/test_bug814761.js rename : dom/sms/tests/marionette/test_emulator_loopback.js => dom/mobilemessage/tests/marionette/test_emulator_loopback.js rename : dom/sms/tests/marionette/test_filter_date.js => dom/mobilemessage/tests/marionette/test_filter_date.js rename : dom/sms/tests/marionette/test_filter_date_notfound.js => dom/mobilemessage/tests/marionette/test_filter_date_notfound.js rename : dom/sms/tests/marionette/test_filter_mixed.js => dom/mobilemessage/tests/marionette/test_filter_mixed.js rename : dom/sms/tests/marionette/test_filter_number_multiple.js => dom/mobilemessage/tests/marionette/test_filter_number_multiple.js rename : dom/sms/tests/marionette/test_filter_number_single.js => dom/mobilemessage/tests/marionette/test_filter_number_single.js rename : dom/sms/tests/marionette/test_filter_read.js => dom/mobilemessage/tests/marionette/test_filter_read.js rename : dom/sms/tests/marionette/test_filter_received.js => dom/mobilemessage/tests/marionette/test_filter_received.js rename : dom/sms/tests/marionette/test_filter_sent.js => dom/mobilemessage/tests/marionette/test_filter_sent.js rename : dom/sms/tests/marionette/test_filter_unread.js => dom/mobilemessage/tests/marionette/test_filter_unread.js rename : dom/sms/tests/marionette/test_getmessage.js => dom/mobilemessage/tests/marionette/test_getmessage.js rename : dom/sms/tests/marionette/test_getmessage_notfound.js => dom/mobilemessage/tests/marionette/test_getmessage_notfound.js rename : dom/sms/tests/marionette/test_getmessages.js => dom/mobilemessage/tests/marionette/test_getmessages.js rename : dom/sms/tests/marionette/test_incoming.js => dom/mobilemessage/tests/marionette/test_incoming.js rename : dom/sms/tests/marionette/test_incoming_delete.js => dom/mobilemessage/tests/marionette/test_incoming_delete.js rename : dom/sms/tests/marionette/test_incoming_max_segments.js => dom/mobilemessage/tests/marionette/test_incoming_max_segments.js rename : dom/sms/tests/marionette/test_incoming_multipart.js => dom/mobilemessage/tests/marionette/test_incoming_multipart.js rename : dom/sms/tests/marionette/test_mark_msg_read.js => dom/mobilemessage/tests/marionette/test_mark_msg_read.js rename : dom/sms/tests/marionette/test_mark_msg_read_error.js => dom/mobilemessage/tests/marionette/test_mark_msg_read_error.js rename : dom/sms/tests/marionette/test_message_classes.js => dom/mobilemessage/tests/marionette/test_message_classes.js rename : dom/sms/tests/marionette/test_outgoing.js => dom/mobilemessage/tests/marionette/test_outgoing.js rename : dom/sms/tests/marionette/test_outgoing_delete.js => dom/mobilemessage/tests/marionette/test_outgoing_delete.js rename : dom/sms/tests/marionette/test_outgoing_max_segments.js => dom/mobilemessage/tests/marionette/test_outgoing_max_segments.js rename : dom/sms/tests/marionette/test_segment_info.js => dom/mobilemessage/tests/marionette/test_segment_info.js rename : dom/sms/tests/marionette/test_strict_7bit_encoding.js => dom/mobilemessage/tests/marionette/test_strict_7bit_encoding.js rename : dom/sms/tests/moz.build => dom/mobilemessage/tests/moz.build rename : dom/sms/tests/test_sms_basics.html => dom/mobilemessage/tests/test_sms_basics.html rename : dom/sms/tests/test_smsdatabaseservice.xul => dom/mobilemessage/tests/test_smsdatabaseservice.xul rename : dom/sms/tests/test_smsfilter.html => dom/mobilemessage/tests/test_smsfilter.html rename : dom/sms/tests/test_smsservice_createsmsmessage.js => dom/mobilemessage/tests/test_smsservice_createsmsmessage.js rename : dom/sms/tests/xpcshell.ini => dom/mobilemessage/tests/xpcshell.ini
2013-03-01 00:38:47 -08:00
using namespace mozilla::dom::mobilemessage;
DOMCI_DATA(MozSmsMessage, mozilla::dom::SmsMessage)
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)
SmsMessage::SmsMessage(int32_t aId,
2013-04-05 05:15:52 -07:00
const uint64_t aThreadId,
DeliveryState aDelivery,
DeliveryStatus aDeliveryStatus,
const nsString& aSender,
const nsString& aReceiver,
const nsString& aBody,
MessageClass aMessageClass,
uint64_t aTimestamp,
bool aRead)
2013-04-05 05:15:52 -07:00
: mData(aId, aThreadId, aDelivery, aDeliveryStatus, aSender, aReceiver, aBody,
aMessageClass, aTimestamp, aRead)
{
}
SmsMessage::SmsMessage(const SmsMessageData& aData)
: mData(aData)
{
}
/* static */ nsresult
SmsMessage::Create(int32_t aId,
2013-04-05 05:15:52 -07:00
const uint64_t aThreadId,
const nsAString& aDelivery,
const nsAString& aDeliveryStatus,
const nsAString& aSender,
const nsAString& aReceiver,
const nsAString& aBody,
const nsAString& aMessageClass,
const JS::Value& aTimestamp,
const bool aRead,
JSContext* aCx,
nsIDOMMozSmsMessage** aMessage)
{
*aMessage = nullptr;
// 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;
data.sender() = nsString(aSender);
data.receiver() = nsString(aReceiver);
data.body() = nsString(aBody);
data.read() = aRead;
if (aDelivery.Equals(DELIVERY_RECEIVED)) {
data.delivery() = eDeliveryState_Received;
} else if (aDelivery.Equals(DELIVERY_SENDING)) {
data.delivery() = eDeliveryState_Sending;
} else if (aDelivery.Equals(DELIVERY_SENT)) {
data.delivery() = eDeliveryState_Sent;
} else if (aDelivery.Equals(DELIVERY_ERROR)) {
data.delivery() = eDeliveryState_Error;
} else {
return NS_ERROR_INVALID_ARG;
}
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;
}
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;
}
// We support both a Date object and a millisecond timestamp as a number.
if (aTimestamp.isObject()) {
JS::Rooted<JSObject*> obj(aCx, &aTimestamp.toObject());
if (!JS_ObjectIsDate(aCx, obj)) {
return NS_ERROR_INVALID_ARG;
}
data.timestamp() = js_DateGetMsecSinceEpoch(obj);
} else {
if (!aTimestamp.isNumber()) {
return NS_ERROR_INVALID_ARG;
}
double number = aTimestamp.toNumber();
if (static_cast<uint64_t>(number) != number) {
return NS_ERROR_INVALID_ARG;
}
data.timestamp() = static_cast<uint64_t>(number);
}
nsCOMPtr<nsIDOMMozSmsMessage> message = new SmsMessage(data);
message.swap(*aMessage);
return NS_OK;
}
const SmsMessageData&
SmsMessage::GetData() const
{
return mData;
}
NS_IMETHODIMP
SmsMessage::GetType(nsAString& aType)
{
aType = NS_LITERAL_STRING("sms");
return NS_OK;
}
NS_IMETHODIMP
SmsMessage::GetId(int32_t* aId)
{
*aId = mData.id();
return NS_OK;
}
2013-04-05 05:15:52 -07:00
NS_IMETHODIMP
SmsMessage::GetThreadId(uint64_t* aThreadId)
{
*aThreadId = mData.threadId();
return NS_OK;
}
NS_IMETHODIMP
SmsMessage::GetDelivery(nsAString& aDelivery)
{
switch (mData.delivery()) {
case eDeliveryState_Received:
aDelivery = DELIVERY_RECEIVED;
break;
case eDeliveryState_Sending:
aDelivery = DELIVERY_SENDING;
break;
case eDeliveryState_Sent:
aDelivery = DELIVERY_SENT;
break;
case eDeliveryState_Error:
aDelivery = DELIVERY_ERROR;
break;
case eDeliveryState_Unknown:
case eDeliveryState_EndGuard:
default:
MOZ_CRASH("We shouldn't get any other delivery state!");
}
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:
MOZ_CRASH("We shouldn't get any other delivery status!");
}
return NS_OK;
}
NS_IMETHODIMP
SmsMessage::GetSender(nsAString& aSender)
{
aSender = mData.sender();
return NS_OK;
}
NS_IMETHODIMP
SmsMessage::GetReceiver(nsAString& aReceiver)
{
aReceiver = mData.receiver();
return NS_OK;
}
NS_IMETHODIMP
SmsMessage::GetBody(nsAString& aBody)
{
aBody = mData.body();
return NS_OK;
}
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:
MOZ_CRASH("We shouldn't get any other message class!");
}
return NS_OK;
}
NS_IMETHODIMP
SmsMessage::GetTimestamp(JSContext* cx, JS::Value* aDate)
{
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);
return NS_OK;
}
NS_IMETHODIMP
SmsMessage::GetRead(bool* aRead)
{
*aRead = mData.read();
return NS_OK;
}
} // namespace dom
} // namespace mozilla